fixed bug caused by last commit
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2
3 #written 11/3/2002 by Finlay
4 #script to execute returns of books
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Circulation::Circ2;
27 use C4::Search;
28 use C4::Output;
29 use C4::Print;
30 use C4::Reserves2;
31
32 my %env;
33 my $headerbackgroundcolor='#99cc33';
34 my $circbackgroundcolor='#ffffcc';
35 my $circbackgroundcolor='white';
36 my $linecolor1='#ffffcc';
37 my $linecolor2='white';
38 my $backgroundimage="/images/background-mem.gif";
39
40 my $query=new CGI;
41 my $branches = getbranches();
42 my $printers = getprinters(\%env);
43
44 my $branch = $query->param("branch");
45 my $printer = $query->param("printer");
46
47 ($branch) || ($branch=$query->cookie('branch')) ;
48 ($printer) || ($printer=$query->cookie('printer')) ;
49
50
51 #
52 # Some code to handle the error if there is no branch or printer setting.....
53 #
54
55
56 $env{'branchcode'}=$branch;
57 $env{'printer'}=$printer;
58 $env{'queue'}=$printer;
59
60 # Set up the item stack ....
61 my $ritext = '';
62 my %returneditems;
63 my %riduedate;
64 my %riborrowernumber;
65 foreach ($query->param) {
66     (next) unless (/ri-(\d*)/);
67     my $counter=$1;
68     (next) if ($counter>20);
69     my $barcode=$query->param("ri-$counter");
70     my $duedate=$query->param("dd-$counter");
71     my $borrowernumber=$query->param("bn-$counter");
72     $counter++;
73     # decode cuecat
74     $barcode = cuecatbarcodedecode($barcode);
75     $returneditems{$counter}=$barcode;
76     $riduedate{$counter}=$duedate;
77     $riborrowernumber{$counter}=$borrowernumber;
78     $ritext.="<input type=hidden name=ri-$counter value=$barcode>\n";
79     $ritext.="<input type=hidden name=dd-$counter value=$duedate>\n";
80     $ritext.="<input type=hidden name=bn-$counter value=$borrowernumber>\n";
81 }
82
83 # Collect a few messages here...
84 my $messagetext='';
85 my $reservetext='';
86
87 ############
88 # Deal with the requests....
89 if ($query->param('resbarcode')) {
90     my $item = $query->param('itemnumber');
91     my $borrnum = $query->param('borrowernumber');
92     my $resbarcode = $query->param('resbarcode');
93 # set to waiting....
94     my $iteminfo = getiteminformation(\%env, $item);
95     my $tobranchcd = ReserveWaiting($item, $borrnum);
96     my $branchname = $branches->{$tobranchcd}->{'branchname'};
97     my ($borr) = getpatroninformation(\%env, $borrnum, 0);
98     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
99     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
100     my $slip = $query->param('resslip');
101     printslip(\%env, $slip);
102     if ($tobranchcd ne $branch) {
103 #       my ($transfered, $messages, $iteminfo) = transferbook($tobranchcd, $resbarcode, 1);
104         $reservetext .= <<"EOF";
105 <font color='red' size='+2'>Item marked Waiting:</font><br>
106     Item: $iteminfo->{'title'} ($iteminfo->{'author'})<br>
107  needs to be transfered to <b>$branchname</b> <br>
108 to be picked up by $name ($number).
109 <center><form method=post action='returns.pl'>
110 $ritext
111 <input type=hidden name=barcode value=0>
112 <input type=submit value="OK">
113 </form></center>
114 EOF
115     }
116 }
117
118
119 my $iteminformation;
120 my $borrower;
121 my $returned = 0;
122 my $messages;
123 my $barcode = $query->param('barcode');
124 # actually return book and prepare item table.....
125 if ($barcode) {
126     # decode cuecat
127     $barcode = cuecatbarcodedecode($barcode);
128     ($returned, $messages, $iteminformation, $borrower) = returnbook($barcode, $branch);
129     $ritext.= "<input type=hidden name=ri-0 value=$barcode>\n";
130     if ($returned) {
131         $returneditems{0} = $barcode;
132         $riborrowernumber{0} = $borrower->{'borrowernumber'};
133         $riduedate{0} = $iteminformation->{'date_due'};
134         $ritext.= "<input type=hidden name=dd-0 value=$iteminformation->{'date_due'}>\n";
135         $ritext.= "<input type=hidden name=bn-0 value=$borrower->{'borrowernumber'}>\n";
136     } elsif (! $messages->{'BadBarcode'}) {
137         $returneditems{0} = $barcode;
138         $riduedate{0} = 0;
139         $ritext.= "<input type=hidden name=dd-0 value=0>\n";
140         if ($messages->{'wthdrawn'}) {
141             $ritext.= "<input type=hidden name=bn-0 value='Item Cancelled'>\n";
142             $riborrowernumber{0} = 'Item Cancelled';
143         } else {
144             $ritext.= "<input type=hidden name=bn-0 value='&nbsp;'>\n";
145             $riborrowernumber{0} = '&nbsp;';
146         }
147     }
148 }
149
150 ##################################################################################
151 # HTML code....
152 # title....
153 my $title = <<"EOF";
154 <FONT SIZE=6><em>Circulation: Returns</em></FONT><br>
155 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp 
156 <b>Printer:</b> $printers->{$printer}->{'printername'}<br>
157 <a href=selectbranchprinter.pl>Change Settings</a>
158 <input type=hidden name=branch value=$branch>
159 <input type=hidden name=printer value=$printer>
160 <p>
161 EOF
162
163 my $links = <<"EOF";
164 <table align="right"><tr><td>
165 <a href=circulation.pl>
166 <img src="/images/button-issues.gif" width="99" height="42" border="0" alt="Issues"></a>
167 &nbsp<a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Issues"></a>
168 </td></tr></table>
169 EOF
170
171
172 my $itemtable;
173 if ($iteminformation) {
174     $itemtable = <<"EOF";
175 <table border=1 cellpadding=5 cellspacing=0>
176 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
177 <font color=black>Returned Item Information</font></th></tr>
178 <tr><td>
179 Title: $iteminformation->{'title'}<br>
180 <!--Hlt decided they dont want these showing, uncoment the html to make it work
181
182 Author: $iteminformation->{'author'}<br>
183 Barcode: <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}
184 &type=intra onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a><br>
185 Date Due: $iteminformation->{'date_due'}-->
186 </td></tr>
187 </table>
188 EOF
189 }
190
191 # Barcode entry box, with hidden inputs attached....
192 my $barcodeentrytext = << "EOF";
193 <form method=post action=/cgi-bin/koha/circ/returns.pl>
194 <table border=1 cellpadding=5 cellspacing=0>
195 <tr><td colspan=2 bgcolor=$headerbackgroundcolor align=center background=$backgroundimage>
196 <font color=black><b>Enter Book Barcode</b></font></td></tr>
197 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
198 </table>
199 $ritext
200 </form>
201 EOF
202
203
204 if ($messages->{'ResFound'}) {
205     my $res = $messages->{'ResFound'};
206     my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
207     my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
208     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
209     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
210     my ($iteminfo) = getiteminformation(\%env, 0, $barcode);
211
212     if ($res->{'ResFound'} eq "Waiting") {
213         $reservetext = <<"EOF";
214 <font color='red' size='+2'>Item marked Waiting:</font><br>
215     Item $iteminfo->{'title'} ($iteminfo->{'author'}) <br>
216 is marked waiting at <b>$branchname</b> for $name ($number).
217 <center><form method=post action='returns.pl'>
218 $ritext
219 <input type=hidden name=barcode value=0>
220 <input type=submit value="OK">
221 </form></center>
222 EOF
223     } 
224     if ($res->{'ResFound'} eq "Reserved") {
225         my @da = localtime(time());
226         my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
227         my $slip =  <<"EOF";
228 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229 Date: $todaysdate;
230
231 ITEM RESERVED: 
232 $iteminfo->{'title'} ($iteminfo->{'author'})
233 barcode: $iteminfo->{'barcode'}
234 itemtype: $iteminfo->{'itemtype'} 
235
236 COLLECT AT: $branchname
237
238 BORROWER:
239 $borr->{'title'} $borr->{'firstname'} $borr->{'surname'}
240 card number: $borr->{'cardnumber'}
241 Phone: $borr->{'phone'}
242 $borr->{'streetaddress'}
243 $borr->{'suburb'}
244 $borr->{'town'}
245 $borr->{'emailaddress'}
246
247 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248
249 EOF
250
251         $reservetext = <<"EOF";
252 <font color='red' size='+2'>Reserve found:</font> Item: $iteminfo->{'title'} ($iteminfo->{'author'}) <br>
253 for $name ($number) to be collected at <b>$branchname</b>.
254 <table cellpadding=5 cellspacing=0>
255 <tr><td valign="top">Change status to waiting and print 
256 <a href="" onClick='alert(document.forms[0].resslip.value); return false'>slip</a>?: </td>
257 <td valign="top">
258 <form method=post action='returns.pl'>
259 $ritext
260 <input type=hidden name=itemnumber value=$res->{'itemnumber'}>
261 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
262 <input type=hidden name=resbarcode value=$barcode>
263 <input type=hidden name=resslip value="$slip">
264 <input type=submit value="Print">
265 </form>
266 </td></tr>
267 </table>
268 EOF
269     }
270 }
271 my $reservefoundtext;
272 if ($reservetext) {
273     $reservefoundtext = <<"EOF";
274 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
275 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Reserve Found</font></th></tr>
276 <tr><td> $reservetext </td></tr></table>
277 EOF
278 }
279
280 # collect the messages and put into message table....
281 foreach my $code (keys %$messages) {
282     warn $code;
283     if ($code eq 'BadBarcode'){
284         $messagetext .= "<font color='red' size='+2'> No Item with barcode: $messages->{'BadBarcode'} </font> <br>";
285     }
286     if ($code eq 'NotIssued'){
287         my $braname = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
288         $messagetext .= "<font color='red' size='+2'> Item not on issue. </font> <br>";
289     }
290     if ($code eq 'WasLost'){
291         $messagetext .= "<font color='red' size='+2'> Item was lost, now found. </font> <br>";
292     }
293     if ($code eq 'wthdrawn'){
294         $messagetext = "<font color='red' size='+2'> Item Cancelled. </font> <br>";
295         last;
296     }
297     if (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
298         if ($messages->{'IsPermanent'} ne $branch) {
299             $messagetext .= "<font color='red' size='+2'> Please return to $branches->{$messages->{'IsPermanent'}}->{'branchname'} </font> <br>";
300         }
301     }
302 }
303 $messagetext = substr($messagetext, 0, -4);
304
305 my $messagetable;
306 if ($messagetext) {
307     $messagetable = << "EOF";
308 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
309 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
310 <tr><td> $messagetext </td></tr></table>
311 EOF
312 }
313
314
315 # patrontable ....
316 my $borrowertable;
317 my $flaginfotable;
318 if ($borrower) {
319     $borrowertable = << "EOF";
320 <table border=1 cellpadding=5 cellspacing=0>
321 <tr><td colspan=2 bgcolor=$headerbackgroundcolor background=$backgroundimage>
322 <font color=black><b>Borrower Information</b></font></td></tr>
323 <tr><td colspan=2>
324 <a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} 
325 onClick="openWindow(this,'Member', 480, 640)">$borrower->{'cardnumber'}</a>
326 $borrower->{'surname'}, $borrower->{'title'} $borrower->{'firstname'}<br>
327 </td></tr>
328 EOF
329     my $flags = $borrower->{'flags'};
330     my $flaginfotext='';
331     my $color = '';
332     foreach my $flag (sort keys %$flags) {
333         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
334         if ($flags->{$flag}->{'noissues'}) {
335             $flag = "<font color=red>$flag</font>";
336         }
337         if ($flag eq 'CHARGES') {
338             $flaginfotext.= <<"EOF";
339 <tr><td valign=top>$flag</td>
340 <td bgcolor=$color><b>$flags->{$flag}->{'message'}</b> 
341 <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} 
342 onClick="openWindow(this, 'Payment', 480,640)">Payment</a></td></tr>
343 EOF
344         } elsif ($flag eq 'WAITING') {
345             my $itemswaiting='';
346             my $items = $flags->{$flag}->{'itemlist'};
347             foreach my $item (@$items) {
348                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
349                 $itemswaiting .= <<"EOF";
350 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
351 onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a> 
352 $iteminformation->{'title'} 
353 ($branches->{$iteminformation->{'holdingbranch'}}->{'branchname'})<br>
354 EOF
355             }
356             $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
357         } elsif ($flag eq 'ODUES') {
358             my $itemsoverdue = '';
359             my $items = $flags->{$flag}->{'itemlist'};
360             foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
361                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
362                 $itemsoverdue .=  <<"EOF";
363 <font color=red>$item->{'date_due'}</font>
364 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
365 onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a> 
366 $iteminformation->{'title'}
367 <br>
368 EOF
369             }
370             $flaginfotext .= "<tr><td valign=top>$flag</td><td>$itemsoverdue</td></tr>\n";
371         } else {
372             $flaginfotext.= <<"EOF";
373 <tr><td valign=top>$flag</td>
374 <td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>
375 EOF
376         }
377     }
378     if ($flaginfotext) {
379         $borrowertable .= << "EOF";
380 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2>
381 <b>Flags</b></td></tr>
382 $flaginfotext 
383 EOF
384     }
385     $borrowertable .= "</table>";
386 }
387
388 # the returned items.....
389 my $returneditemstable = << "EOF";
390 <table border=1 cellpadding=5 cellspacing=0>
391 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage>
392 <font color=black>Returned Items</font></th></tr>
393 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
394 EOF
395
396 my $color='';
397 #set up so only the lat 8 returned items display (make for faster loading pages)
398 my $count=0;
399 foreach (sort {$a <=> $b} keys %returneditems) {
400     if ($count < 8) {
401         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
402         my $barcode = $returneditems{$_};
403         my $duedate = $riduedate{$_};
404         my $overduetext;
405         my $borrowerinfo;
406         if ($duedate) {
407             my @tempdate = split ( /-/ , $duedate ) ;
408             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
409             my @datearr = localtime(time());
410             my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
411             $overduetext = "$duedatenz";
412             ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
413             ($duedatenz) || ($overduetext = "<img src=/images/blackdot.gif>");
414             my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
415             $borrowerinfo = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this,'Member', 480, 640)\">$borrower->{'cardnumber'}</a> $borrower->{'firstname'} $borrower->{'surname'}";
416         } else {
417             $overduetext = "Not on Issue.";
418             $borrowerinfo = $riborrowernumber{$_};
419         }
420         my ($iteminformation) = getiteminformation(\%env, 0, $barcode);;
421         $returneditemstable .= << "EOF";
422 <tr><td bgcolor=$color>$overduetext</td>
423 <td bgcolor=$color align=center>
424 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
425 <td bgcolor=$color>$iteminformation->{'title'}</td>
426 <td bgcolor=$color>$iteminformation->{'author'}</td>
427 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
428 <td bgcolor=$color>
429 $borrowerinfo</td></tr>
430 EOF
431     } else {
432         last;
433     }
434     $count++;
435 }
436 $returneditemstable .= "</table>\n";
437
438
439 # actually print the page!
440 print $query->header();
441 print startpage();
442 print startmenu('circulation');
443
444
445 #debug
446 #    foreach my $key (keys %$messages) {
447 #       print "$key : $messages->{$key}<br>";
448 #    }
449
450
451 print <<"EOF";
452 $links
453 $title
454
455
456 <table cellpadding=5 cellspacing=0 width=100%>
457 EOF
458
459 if ($reservefoundtext) {
460     print <<"EOF";
461 <tr>
462 <td colspan=2>$reservefoundtext</td>
463 </tr>
464 <tr>
465 <td colspan=2>$messagetable</td>
466 </tr>
467
468 EOF
469 } else {
470     print <<"EOF";
471 <tr>
472 <td valign=top align=left>$barcodeentrytext</td>
473 <td valign=top align=left>$messagetable</td>
474 </tr>
475 EOF
476 }
477 if ($returned) {
478     print <<"EOF";
479 <tr>
480 <td valign=top align=left>$itemtable</td>
481 <td valign=top align=left>$borrowertable</td>
482 <tr>
483 EOF
484 }
485 if (%returneditems) {
486     print "<tr><td colspan=2>$returneditemstable</td></tr>";
487 }
488
489 print "</table>";
490
491 print endmenu('circulation');
492 print endpage();
493
494 sub cuecatbarcodedecode {
495     my ($barcode) = @_;
496     chomp($barcode);
497     my @fields = split(/\./,$barcode);
498     my @results = map(decode($_), @fields[1..$#fields]);
499     if ($#results == 2){
500         return $results[2];
501     } else {
502         return $barcode;
503     } 
504
505