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