Removed trailing whitespace.
[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<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     }    if ($res->{'ResFound'} eq "Reserved") {
223         my @da = localtime(time());
224         my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
225         my $slip =  <<"EOF";
226 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227 Date: $todaysdate;
228
229 ITEM RESERVED:$iteminfo->{'title'} ($iteminfo->{'author'})
230 barcode: $iteminfo->{'barcode'}
231 itemtype: $iteminfo->{'itemtype'}
232 COLLECT AT: $branchname
233
234 BORROWER:
235 $borr->{'title'} $borr->{'firstname'} $borr->{'surname'}
236 card number: $borr->{'cardnumber'}
237 Phone: $borr->{'phone'}
238 $borr->{'streetaddress'}
239 $borr->{'suburb'}
240 $borr->{'town'}
241 $borr->{'emailaddress'}
242
243 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244
245 EOF
246
247         $reservetext = <<"EOF";
248 <font color='red' size='+2'>Reserve found:</font> Item: $iteminfo->{'title'} ($iteminfo->{'author'}) <a href=/cgi-bin/koha/detail.pl?bib=$iteminfo->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a> <br>
249 for $name ($number) to be collected at <b>$branchname</b>.
250 <table cellpadding=5 cellspacing=0>
251 <tr><td valign="top">Change status to waiting and print<a href="" onClick='alert(document.forms[0].resslip.value); return false'>slip</a>?: </td>
252 <td valign="top">
253 <form method=post action='returns.pl'>
254 $ritext
255 <input type=hidden name=itemnumber value=$res->{'itemnumber'}>
256 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
257 <input type=hidden name=resbarcode value=$barcode>
258 <input type=hidden name=resslip value="$slip">
259 <input type=submit value="Print">
260 </form>
261 </td></tr>
262 </table>
263 EOF
264     }
265 }
266 my $reservefoundtext;
267 if ($reservetext) {
268     $reservefoundtext = <<"EOF";
269 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
270 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Reserve Found</font></th></tr>
271 <tr><td> $reservetext </td></tr></table>
272 EOF
273 }
274
275 # collect the messages and put into message table....
276 foreach my $code (keys %$messages) {
277 #    warn $code;
278     if ($code eq 'BadBarcode'){
279         $messagetext .= "<font color='red' size='+2'> No Item with barcode: $messages->{'BadBarcode'} </font> <br>";
280     }
281     if ($code eq 'NotIssued'){
282         my $braname = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
283         $messagetext .= "<font color='red' size='+2'> Item not on issue. </font> <br>";
284     }
285     if ($code eq 'WasLost'){
286         $messagetext .= "<font color='red' size='+2'> Item was lost, now found. </font> <br>";
287     }
288     if ($code eq 'wthdrawn'){
289         $messagetext = "<font color='red' size='+2'> Item Cancelled. </font> <br>";
290         last;
291     }
292     if (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
293         if ($messages->{'IsPermanent'} ne $branch) {
294             $messagetext .= "<font color='red' size='+2'> Please return to $branches->{$messages->{'IsPermanent'}}->{'branchname'} </font> <br>";
295         }
296     }
297 }
298 $messagetext = substr($messagetext, 0, -4);
299
300 my $messagetable;
301 if ($messagetext) {
302     $messagetable = << "EOF";
303 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
304 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
305 <tr><td> $messagetext </td></tr></table>
306 EOF
307 }
308
309
310 # patrontable ....
311 my $borrowertable;
312 my $flaginfotable;
313 if ($borrower) {
314     $borrowertable = << "EOF";
315 <table border=1 cellpadding=5 cellspacing=0>
316 <tr><td colspan=2 bgcolor=$headerbackgroundcolor background=$backgroundimage>
317 <font color=black><b>Borrower Information</b></font></td></tr>
318 <tr><td colspan=2>
319 <a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'}onClick="openWindow(this,'Member', 480, 640)">$borrower->{'cardnumber'}</a>
320 $borrower->{'surname'}, $borrower->{'title'} $borrower->{'firstname'}<br>
321 </td></tr>
322 EOF
323     my $flags = $borrower->{'flags'};
324     my $flaginfotext='';
325     my $displayflag = '';
326     my $color = '';
327     foreach my $flag (sort keys %$flags) {
328         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
329         if ($flags->{$flag}->{'noissues'}) {
330             $displayflag = "<font color=red>$flag</font>";
331         } else {
332             $displayflag = $flag;
333         }       if ($flag eq 'CHARGES') {
334             $flaginfotext.= <<"EOF";
335 <tr><td valign=top>$displayflag</td>
336 <td bgcolor=$color><b>$flags->{$flag}->{'message'}</b><a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'}onClick="openWindow(this, 'Payment', 480,640)">Payment</a></td></tr>
337 EOF
338         } elsif ($flag eq 'WAITING') {
339             my $itemswaiting='';
340             my $items = $flags->{$flag}->{'itemlist'};
341             foreach my $item (@$items) {
342                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
343                 $itemswaiting .= <<"EOF";
344 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intraonClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a>$iteminformation->{'title'}($branches->{$iteminformation->{'holdingbranch'}}->{'branchname'})<br>
345 EOF
346             }
347             $flaginfotext.="<tr><td valign=top>$displayflag</td><td>$itemswaiting</td></tr>\n";
348         } elsif ($flag eq 'ODUES') {
349             my $itemsoverdue = '';
350             my $items = $flags->{$flag}->{'itemlist'};
351             foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
352                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
353                 $itemsoverdue .=  <<"EOF";
354 <font color=red>$item->{'date_due'}</font>
355 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intraonClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a>$iteminformation->{'title'}
356 <br>
357 EOF
358             }
359             $flaginfotext .= "<tr><td valign=top>$displayflag</td><td>$itemsoverdue</td></tr>\n";
360         } else {
361             $flaginfotext.= <<"EOF";
362 <tr><td valign=top>$displayflag</td>
363 <td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>
364 EOF
365         }
366     }
367     if ($flaginfotext) {
368         $borrowertable .= << "EOF";
369 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2>
370 <b>Flags</b></td></tr>
371 $flaginfotextEOF
372     }
373     $borrowertable .= "</table>";
374 }
375
376 # the returned items.....
377 my $returneditemstable = << "EOF";
378 <table border=1 cellpadding=5 cellspacing=0>
379 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage>
380 <font color=black>Returned Items</font></th></tr>
381 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
382 EOF
383
384 my $color='';
385 #set up so only the lat 8 returned items display (make for faster loading pages)
386 my $count=0;
387 foreach (sort {$a <=> $b} keys %returneditems) {
388     if ($count < 8) {
389         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
390         my $barcode = $returneditems{$_};
391         my $duedate = $riduedate{$_};
392         my $overduetext;
393         my $borrowerinfo;
394         if ($duedate) {
395             my @tempdate = split ( /-/ , $duedate ) ;
396             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
397             my @datearr = localtime(time());
398             my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
399             $overduetext = "$duedatenz";
400             ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
401             ($duedatenz) || ($overduetext = "<img src=/images/blackdot.gif>");
402             my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
403             $borrowerinfo = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this,'Member', 480, 640)\">$borrower->{'cardnumber'}</a> $borrower->{'firstname'} $borrower->{'surname'}";
404         } else {
405             $overduetext = "Not on Issue.";
406             $borrowerinfo = $riborrowernumber{$_};
407         }
408         my ($iteminformation) = getiteminformation(\%env, 0, $barcode);;
409         $returneditemstable .= << "EOF";
410 <tr><td bgcolor=$color>$overduetext</td>
411 <td bgcolor=$color align=center>
412 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
413 <td bgcolor=$color>$iteminformation->{'title'}</td>
414 <td bgcolor=$color>$iteminformation->{'author'}</td>
415 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
416 <td bgcolor=$color>
417 $borrowerinfo</td></tr>
418 EOF
419     } else {
420         last;
421     }
422     $count++;
423 }
424 $returneditemstable .= "</table>\n";
425
426
427 # actually print the page!
428 print $query->header();
429 print startpage();
430 print startmenu('circulation');
431
432
433 #debug
434 #    foreach my $key (keys %$messages) {
435 #       print "$key : $messages->{$key}<br>";
436 #    }
437
438
439 print <<"EOF";
440 $links
441 $title
442
443
444 <table cellpadding=5 cellspacing=0 width=100%>
445 EOF
446
447 if ($reservefoundtext) {
448     print <<"EOF";
449 <tr>
450 <td colspan=2>$reservefoundtext</td>
451 </tr>
452 <tr>
453 <td colspan=2>$messagetable</td>
454 </tr>
455
456 EOF
457 } else {
458     print <<"EOF";
459 <tr>
460 <td valign=top align=left>$barcodeentrytext</td>
461 <td valign=top align=left>$messagetable</td>
462 </tr>
463 EOF
464 }
465 if ($returned) {
466     print <<"EOF";
467 <tr>
468 <td valign=top align=left>$itemtable</td>
469 <td valign=top align=left>$borrowertable</td>
470 <tr>
471 EOF
472 }
473 if (%returneditems) {
474     print "<tr><td colspan=2>$returneditemstable</td></tr>";
475 }
476
477 print "</table>";
478
479 print endmenu('circulation');
480 print endpage();
481
482 sub cuecatbarcodedecode {
483     my ($barcode) = @_;
484     chomp($barcode);
485     my @fields = split(/\./,$barcode);
486     my @results = map(decode($_), @fields[1..$#fields]);
487     if ($#results == 2){
488         return $results[2];
489     } else {
490         return $barcode;
491     }}