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