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