fixed up the reserves. Can view the printed slip before it is printed!
[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::Print;
12 use C4::Reserves2;
13
14 my %env;
15 my $headerbackgroundcolor='#99cc33';
16 my $circbackgroundcolor='#ffffcc';
17 my $circbackgroundcolor='white';
18 my $linecolor1='#ffffcc';
19 my $linecolor2='white';
20 my $backgroundimage="/images/background-mem.gif";
21
22 my $query=new CGI;
23 my $branches = getbranches();
24 my $printers = getprinters(\%env);
25
26 my $branch = $query->param("branch");
27 my $printer = $query->param("printer");
28
29 ($branch) || ($branch=$query->cookie('branch')) ;
30 ($printer) || ($printer=$query->cookie('printer')) ;
31
32
33 #
34 # Some code to handle the error if there is no branch or printer setting.....
35 #
36
37
38 $env{'branchcode'}=$branch;
39 $env{'printer'}=$printer;
40 $env{'queue'}=$printer;
41
42 # Set up the item stack ....
43 my $ritext = '';
44 my %returneditems;
45 my %riduedate;
46 my %riborrowernumber;
47 foreach ($query->param) {
48     (next) unless (/ri-(\d*)/);
49     my $counter=$1;
50     (next) if ($counter>20);
51     my $barcode=$query->param("ri-$counter");
52     my $duedate=$query->param("dd-$counter");
53     my $borrowernumber=$query->param("bn-$counter");
54     $counter++;
55     # decode cuecat
56     $barcode = cuecatbarcodedecode($barcode);
57     $returneditems{$counter}=$barcode;
58     $riduedate{$counter}=$duedate;
59     $riborrowernumber{$counter}=$borrowernumber;
60     $ritext.="<input type=hidden name=ri-$counter value=$barcode>\n";
61     $ritext.="<input type=hidden name=dd-$counter value=$duedate>\n";
62     $ritext.="<input type=hidden name=bn-$counter value=$borrowernumber>\n";
63 }
64
65 # Collect a few messages here...
66 my $messagetext='';
67 my $reservetext='';
68
69 ############
70 # Deal with the requests....
71 if ($query->param('resbarcode')) {
72     my $item = $query->param('itemnumber');
73     my $borrnum = $query->param('borrowernumber');
74     my $resbarcode = $query->param('resbarcode');
75     my $tobranchcd = ReserveWaiting($item, $borrnum);
76     my $branchname = $branches->{$tobranchcd}->{'branchname'};
77     my ($borr) = getpatroninformation(\%env, $borrnum, 0);
78     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
79     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
80     my $slip = $query->param('resslip');
81     printslip(\%env, $slip);
82     if ($tobranchcd ne $branch) {
83         my ($transfered, $messages, $iteminfo) = transferbook($tobranchcd, $resbarcode, 1);
84         $reservetext .= <<"EOF";
85 <font color='red' size='+2'>Item marked Waiting:</font><br>
86     Item needs to be transfered to <b>$branchname</b> <br>
87 to be picked up by $name ($number).
88 <center><form method=post action='returns.pl'>
89 $ritext
90 <input type=hidden name=barcode value=0>
91 <input type=submit value="OK">
92 </form></center>
93 EOF
94     }
95 }
96
97
98 my $iteminformation;
99 my $borrower;
100 my $returned = 0;
101 my $messages;
102 my $barcode = $query->param('barcode');
103 # actually return book and prepare item table.....
104 if ($barcode) {
105     # decode cuecat
106     $barcode = cuecatbarcodedecode($barcode);
107     ($returned, $messages, $iteminformation, $borrower) = returnbook($barcode, $branch);
108     if ($returned) {
109         $returneditems{0} = $barcode;
110         $riborrowernumber{0} = $borrower->{'borrowernumber'};
111         $riduedate{0} = $iteminformation->{'date_due'};
112         $ritext.= "<input type=hidden name=ri-0 value=$barcode>\n";
113         $ritext.= "<input type=hidden name=dd-0 value=$iteminformation->{'date_due'}>\n";
114         $ritext.= "<input type=hidden name=bn-0 value=$borrower->{'borrowernumber'}>\n";
115     }
116 }
117
118 ##################################################################################
119 # HTML code....
120 # title....
121 my $title = <<"EOF";
122 <FONT SIZE=6><em>Circulation: Returns</em></FONT><br>
123 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp 
124 <b>Printer:</b> $printers->{$printer}->{'printername'}<br>
125 <a href=selectbranchprinter.pl>Change Settings</a>
126 <input type=hidden name=branch value=$branch>
127 <input type=hidden name=printer value=$printer>
128 <p>
129 EOF
130
131 my $links = <<"EOF";
132 <table align="right"><tr><td>
133 <a href=circulation.pl>
134 <img src="/images/button-issues.gif" width="99" height="42" border="0" alt="Issues"></a>
135 &nbsp<a href=branchtransfers.pl>
136 <img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Issues"></a>
137 </td></tr></table>
138 EOF
139
140
141 my $itemtable;
142 if ($iteminformation) {
143     $itemtable = <<"EOF";
144 <table border=1 cellpadding=5 cellspacing=0>
145 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
146 <font color=black>Returned Item Information</font></th></tr>
147 <tr><td>
148 Title: $iteminformation->{'title'}<br>
149 <!--Hlt decided they dont want these showing, uncoment the html to make it work
150
151 Author: $iteminformation->{'author'}<br>
152 Barcode: <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}
153 &type=intra onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a><br>
154 Date Due: $iteminformation->{'date_due'}-->
155 </td></tr>
156 </table>
157 EOF
158 }
159
160 # Barcode entry box, with hidden inputs attached....
161 my $barcodeentrytext = << "EOF";
162 <form method=post action=/cgi-bin/koha/circ/returns.pl>
163 <table border=1 cellpadding=5 cellspacing=0>
164 <tr><td colspan=2 bgcolor=$headerbackgroundcolor align=center background=$backgroundimage>
165 <font color=black><b>Enter Book Barcode</b></font></td></tr>
166 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
167 </table>
168 $ritext
169 </form>
170 EOF
171
172
173 if ($messages->{'ResFound'}) {
174     my $res = $messages->{'ResFound'};
175     my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
176     my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
177     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
178     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
179     my ($iteminfo) = getiteminformation(\%env, 0, $barcode);
180
181     if ($res->{'ResFound'} eq "Waiting") {
182         $reservetext = <<"EOF";
183 <font color='red' size='+2'>Item marked Waiting:</font><br>
184     Item is marked waiting at <b>$branchname</b> for $name ($number).
185 <center><form method=post action='returns.pl'>
186 $ritext
187 <input type=hidden name=barcode value=0>
188 <input type=submit value="OK">
189 </form></center>
190 EOF
191     } 
192     if ($res->{'ResFound'} eq "Reserved") {
193         my @da = localtime(time());
194         my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
195         my $slip =  <<"EOF";
196 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197 Date: $todaysdate;
198
199 ITEM RESERVED: 
200 $iteminfo->{'title'} ($iteminfo->{'author'})
201 barcode: $iteminfo->{'barcode'}
202
203 COLLECT AT: $branchname
204
205 BORROWER:
206 $borr->{'surname'}, $borr->{'firstname'}
207 card number: $borr->{'cardnumber'}
208 Phone: $borr->{'phone'}
209 $borr->{'streetaddress'}
210 $borr->{'suburb'}
211 $borr->{'town'}
212 $borr->{'emailaddress'}
213
214 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215 EOF
216
217         $reservetext = <<"EOF";
218 <font color='red' size='+2'>Reserved found:</font> for $name ($number).
219 <table cellpadding=5 cellspacing=0>
220 <tr><td valign="top">Change status to waiting and print 
221 <a href="" onClick='alert(document.forms[0].resslip.value); return false'>slip</a>?: </td>
222 <td valign="top">
223 <form method=post action='returns.pl'>
224 $ritext
225 <input type=hidden name=itemnumber value=$res->{'itemnumber'}>
226 <input type=hidden name=borrowernumber value=$res->{'borrowernumber'}>
227 <input type=hidden name=resbarcode value=$barcode>
228 <input type=hidden name=resslip value="$slip">
229 <input type=submit value="Print">
230 </form>
231 </td></tr>
232 </table>
233 EOF
234     }
235 }
236 my $reservefoundtext;
237 if ($reservetext) {
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 not on issue. </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'> 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                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
324                 $itemsoverdue .=  <<"EOF";
325 <font color=red>$item->{'date_due'}</font>
326 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
327 onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a> 
328 $iteminformation->{'title'}
329 <br>
330 EOF
331             }
332             $flaginfotext .= "<tr><td valign=top>$flag</td><td>$itemsoverdue</td></tr>\n";
333         } else {
334             $flaginfotext.= <<"EOF";
335 <tr><td valign=top>$flag</td>
336 <td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>
337 EOF
338         }
339     }
340     if ($flaginfotext) {
341         $borrowertable .= << "EOF";
342 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2>
343 <b>Flags</b></td></tr>
344 $flaginfotext 
345 </table>
346 EOF
347     }
348 }
349
350 # the returned items.....
351 my $returneditemstable = << "EOF";
352 <table border=1 cellpadding=5 cellspacing=0>
353 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage>
354 <font color=black>Returned Items</font></th></tr>
355 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
356 EOF
357
358 my $color='';
359 #set up so only the lat 8 returned items display (make for faster loading pages)
360 my $count=0;
361 foreach (sort {$a <=> $b} keys %returneditems) {
362     if ($count < 8) {
363         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
364         my $barcode = $returneditems{$_};
365         my $duedate = $riduedate{$_};
366         my @datearr = localtime(time());
367         ###
368         # convert to nz date format
369         my @tempdate = split(/-/,$duedate);
370         my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
371         ####
372         my $todaysdate 
373             = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
374         my $overduetext = "$duedatenz";
375         ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
376         ($duedatenz) || ($overduetext = "<img src=/images/blackdot.gif>");
377         my $borrowernumber = $riborrowernumber{$_};
378         my ($borrower) = getpatroninformation(\%env,$borrowernumber,0);
379         my ($iteminformation) = getiteminformation(\%env, 0, $barcode);;
380         $returneditemstable .= << "EOF";
381 <tr><td bgcolor=$color>$overduetext</td>
382 <td bgcolor=$color align=center>
383 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
384 <td bgcolor=$color>$iteminformation->{'title'}</td>
385 <td bgcolor=$color>$iteminformation->{'author'}</td>
386 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
387 <td bgcolor=$color>
388 <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>
389 EOF
390     } else {
391         last;
392     }
393     $count++;
394 }
395 $returneditemstable .= "</table>\n";
396
397
398 # actually print the page!
399 print $query->header();
400 print startpage();
401 print startmenu('circulation');
402
403 print <<"EOF";
404 $links
405 $title
406 <table cellpadding=5 cellspacing=0 width=100%>
407 EOF
408
409 if ($reservefoundtext) {
410     print <<"EOF";
411 <tr>
412 <td colspan=2>$reservefoundtext</td>
413 </tr>
414 <tr>
415 <td colspan=2>$messagetable</td>
416 </tr>
417
418 EOF
419 } else {
420     print <<"EOF";
421 <tr>
422 <td valign=top align=left>$barcodeentrytext</td>
423 <td valign=top align=left>$messagetable</td>
424 </tr>
425 EOF
426 }
427 if ($returned) {
428     print <<"EOF";
429 <tr>
430 <td valign=top align=left>$itemtable</td>
431 <td valign=top align=left>$borrowertable</td>
432 <tr>
433 EOF
434 }
435 if (%returneditems) {
436     print "<tr><td colspan=2>$returneditemstable</td></tr>";
437 }
438
439 print "</table>";
440
441 print endmenu('circulation');
442 print endpage();
443
444 sub cuecatbarcodedecode {
445     my ($barcode) = @_;
446     chomp($barcode);
447     my @fields = split(/\./,$barcode);
448     my @results = map(decode($_), @fields[1..$#fields]);
449     if ($#results == 2){
450         return $results[2];
451     } else {
452         return $barcode;
453     } 
454
455