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