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