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