Fixed bug in flags display
[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'}) <a href=/cgi-bin/koha/detail.pl?bib=$iteminfo->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a> <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 $displayflag = '';
332     my $color = '';
333     foreach my $flag (sort keys %$flags) {
334         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
335         if ($flags->{$flag}->{'noissues'}) {
336             $displayflag = "<font color=red>$flag</font>";
337         } else {
338             $displayflag = $flag;
339         }
340         if ($flag eq 'CHARGES') {
341             $flaginfotext.= <<"EOF";
342 <tr><td valign=top>$displayflag</td>
343 <td bgcolor=$color><b>$flags->{$flag}->{'message'}</b> 
344 <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} 
345 onClick="openWindow(this, 'Payment', 480,640)">Payment</a></td></tr>
346 EOF
347         } elsif ($flag eq 'WAITING') {
348             my $itemswaiting='';
349             my $items = $flags->{$flag}->{'itemlist'};
350             foreach my $item (@$items) {
351                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
352                 $itemswaiting .= <<"EOF";
353 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
354 onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a> 
355 $iteminformation->{'title'} 
356 ($branches->{$iteminformation->{'holdingbranch'}}->{'branchname'})<br>
357 EOF
358             }
359             $flaginfotext.="<tr><td valign=top>$displayflag</td><td>$itemswaiting</td></tr>\n";
360         } elsif ($flag eq 'ODUES') {
361             my $itemsoverdue = '';
362             my $items = $flags->{$flag}->{'itemlist'};
363             foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
364                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
365                 $itemsoverdue .=  <<"EOF";
366 <font color=red>$item->{'date_due'}</font>
367 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
368 onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a> 
369 $iteminformation->{'title'}
370 <br>
371 EOF
372             }
373             $flaginfotext .= "<tr><td valign=top>$displayflag</td><td>$itemsoverdue</td></tr>\n";
374         } else {
375             $flaginfotext.= <<"EOF";
376 <tr><td valign=top>$displayflag</td>
377 <td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>
378 EOF
379         }
380     }
381     if ($flaginfotext) {
382         $borrowertable .= << "EOF";
383 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2>
384 <b>Flags</b></td></tr>
385 $flaginfotext 
386 EOF
387     }
388     $borrowertable .= "</table>";
389 }
390
391 # the returned items.....
392 my $returneditemstable = << "EOF";
393 <table border=1 cellpadding=5 cellspacing=0>
394 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage>
395 <font color=black>Returned Items</font></th></tr>
396 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
397 EOF
398
399 my $color='';
400 #set up so only the lat 8 returned items display (make for faster loading pages)
401 my $count=0;
402 foreach (sort {$a <=> $b} keys %returneditems) {
403     if ($count < 8) {
404         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
405         my $barcode = $returneditems{$_};
406         my $duedate = $riduedate{$_};
407         my $overduetext;
408         my $borrowerinfo;
409         if ($duedate) {
410             my @tempdate = split ( /-/ , $duedate ) ;
411             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
412             my @datearr = localtime(time());
413             my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
414             $overduetext = "$duedatenz";
415             ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
416             ($duedatenz) || ($overduetext = "<img src=/images/blackdot.gif>");
417             my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
418             $borrowerinfo = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this,'Member', 480, 640)\">$borrower->{'cardnumber'}</a> $borrower->{'firstname'} $borrower->{'surname'}";
419         } else {
420             $overduetext = "Not on Issue.";
421             $borrowerinfo = $riborrowernumber{$_};
422         }
423         my ($iteminformation) = getiteminformation(\%env, 0, $barcode);;
424         $returneditemstable .= << "EOF";
425 <tr><td bgcolor=$color>$overduetext</td>
426 <td bgcolor=$color align=center>
427 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
428 <td bgcolor=$color>$iteminformation->{'title'}</td>
429 <td bgcolor=$color>$iteminformation->{'author'}</td>
430 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
431 <td bgcolor=$color>
432 $borrowerinfo</td></tr>
433 EOF
434     } else {
435         last;
436     }
437     $count++;
438 }
439 $returneditemstable .= "</table>\n";
440
441
442 # actually print the page!
443 print $query->header();
444 print startpage();
445 print startmenu('circulation');
446
447
448 #debug
449 #    foreach my $key (keys %$messages) {
450 #       print "$key : $messages->{$key}<br>";
451 #    }
452
453
454 print <<"EOF";
455 $links
456 $title
457
458
459 <table cellpadding=5 cellspacing=0 width=100%>
460 EOF
461
462 if ($reservefoundtext) {
463     print <<"EOF";
464 <tr>
465 <td colspan=2>$reservefoundtext</td>
466 </tr>
467 <tr>
468 <td colspan=2>$messagetable</td>
469 </tr>
470
471 EOF
472 } else {
473     print <<"EOF";
474 <tr>
475 <td valign=top align=left>$barcodeentrytext</td>
476 <td valign=top align=left>$messagetable</td>
477 </tr>
478 EOF
479 }
480 if ($returned) {
481     print <<"EOF";
482 <tr>
483 <td valign=top align=left>$itemtable</td>
484 <td valign=top align=left>$borrowertable</td>
485 <tr>
486 EOF
487 }
488 if (%returneditems) {
489     print "<tr><td colspan=2>$returneditemstable</td></tr>";
490 }
491
492 print "</table>";
493
494 print endmenu('circulation');
495 print endpage();
496
497 sub cuecatbarcodedecode {
498     my ($barcode) = @_;
499     chomp($barcode);
500     my @fields = split(/\./,$barcode);
501     my @results = map(decode($_), @fields[1..$#fields]);
502     if ($#results == 2){
503         return $results[2];
504     } else {
505         return $barcode;
506     } 
507
508