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