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