Add comments, make branchrelations consistent with other table additions
[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
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
32
33 #
34 # Some code to handle the error if there is no branch or printer setting.....
35 #
36
37 $env{'branchcode'}=$branch;
38 $env{'printer'}=$printer;
39 $env{'queue'}=$printer;
40
41 # Set up the item stack ....
42 my $ritext = '';
43 my %returneditems;
44 my %riduedate;
45 my %riborrowernumber;
46 foreach ($query->param) {
47     (next) unless (/ri-(\d*)/);
48     my $counter=$1;
49     (next) if ($counter>20);
50     my $barcode=$query->param("ri-$counter");
51     my $duedate=$query->param("dd-$counter");
52     my $borrowernumber=$query->param("bn-$counter");
53     $counter++;
54     # decode cuecat
55     $barcode = cuecatbarcodedecode($barcode);
56     $returneditems{$counter}=$barcode;
57     $riduedate{$counter}=$duedate;
58     $riborrowernumber{$counter}=$borrowernumber;
59     $ritext.="<input type=hidden name=ri-$counter value=$barcode>\n";
60     $ritext.="<input type=hidden name=dd-$counter value=$duedate>\n";
61     $ritext.="<input type=hidden name=bn-$counter value=$borrowernumber>\n";
62 }
63
64 my $iteminformation;
65 my $borrower;
66 my $returned = 0;
67 my $messages;
68 # actually return book (SQL CALL) and prepare item table.....
69 if (my $barcode = $query->param('barcode')) {
70     # decode cuecat
71     $barcode = cuecatbarcodedecode($barcode);
72     ($returned, $messages, $iteminformation, $borrower) = returnbook($barcode, $branch);
73     if ($returned) {
74         $returneditems{0} = $barcode;
75         $riborrowernumber{0} = $borrower->{'borrowernumber'};
76         $riduedate{0} = $iteminformation->{'date_due'};
77         $ritext.= "<input type=hidden name=ri-0 value=$barcode>\n";
78         $ritext.= "<input type=hidden name=dd-0 value=$iteminformation->{'date_due'}>\n";
79         $ritext.= "<input type=hidden name=bn-0 value=$borrower->{'borrowernumber'}>\n";
80     }
81 }
82
83 ##################################################################################
84 # HTML code....
85 # title....
86 my $title = <<"EOF";
87 <p>
88 <table border=0 cellpadding=5 width=90%><tr>
89 <td align="left"><FONT SIZE=6><em>Circulation: Returns</em></FONT><br>
90 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp 
91 <b>Printer:</b> $printers->{$printer}->{'printername'}<br>
92 <a href=selectbranchprinter.pl>Change Settings</a>
93 </td>
94 <td align="right">
95 <FONT SIZE=2  face="arial, helvetica">
96 <a href=circulation.pl>Next Borrower</a> || 
97 <a href=returns.pl>Returns</a> || 
98 <a href=branchtransfers.pl>Transfers</a></font><p>
99 </td></tr></table>
100 <input type=hidden name=branch value=$branch>
101 <input type=hidden name=printer value=$printer>
102 </p>
103 EOF
104
105 my $itemtable;
106 if ($iteminformation) {
107     $itemtable = <<"EOF";
108 <table border=1 cellpadding=5 cellspacing=0>
109 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
110 <font color=black>Returned Item Information</font></th></tr>
111 <tr><td>
112 Title: $iteminformation->{'title'}<br>
113 <!--Hlt decided they dont want these showing, uncoment the html to make it work
114
115 Author: $iteminformation->{'author'}<br>
116 Barcode: <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}
117 &type=intra onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a><br>
118 Date Due: $iteminformation->{'date_due'}-->
119 </td></tr>
120 </table>
121 <p>
122 EOF
123 }
124
125 # Barcode entry box, with hidden inputs attached....
126 my $barcodeentrytext = << "EOF";
127 <form method=post action=/cgi-bin/koha/circ/returns.pl>
128 <table border=1 cellpadding=5 cellspacing=0 align=left>
129 <tr><td colspan=2 bgcolor=$headerbackgroundcolor align=center background=$backgroundimage>
130 <font color=black><b>Enter Book Barcode</b></font></td></tr>
131 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
132 </table>
133 <input type=hidden name=branch value=$branch>
134 <input type=hidden name=printer value=$printer>
135 $ritext
136 </form>
137 EOF
138
139 my $reservefoundtext;
140 if ($messages->{'ResFound'}) {
141     my $resrec = $messages->{'ResFound'};
142     my ($borr) = getpatroninformation(\%env, $resrec->{'borrowernumber'}, 0);
143     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
144     my $number = "<a href=/cgi-bin/koha/moremember.pl?bornum=$borr->{'borrowernumber'} onClick='openWindow(this,'Member', 480, 640)'>$borr->{'cardnumber'}</a>";
145     my $branch = $branches->{$resrec->{'branchcode'}}->{'branchname'};
146     my $reservetext = "<font size='+2' color='red'>RESERVED</font><font size='+2'> for collection by <br>$name ($number) at $branch </font>";
147     $reservefoundtext = <<"EOF";
148 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
149 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Reserve Found</font></th></tr>
150 <tr><td> $reservetext </td></tr></table>
151 EOF
152 }
153
154 # collect the messages and put into message table....
155 my $messagetext='';
156 foreach my $code (keys %$messages) {
157     if ($code eq 'BadBarcode'){
158         $messagetext .= "<font color='red' size='+2'> No Item with barcode: $messages->{'BadBarcode'} </font> <br>";
159     }
160     if ($code eq 'NotIssued'){
161         my $braname = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
162         $messagetext .= "<font color='red' size='+2'> Item is not Issued, cannot be returned. </font> <br>";
163     }
164     if ($code eq 'WasLost'){
165         $messagetext .= "<font color='red' size='+2'> Item was lost, now found. </font> <br>";
166     }
167     if (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
168         if ($messages->{'IsPermanent'} ne $branch) {
169             $messagetext .= "<font color='red' size='+2'> Item is part of permanent collection, please return to $branches->{$messages->{'IsPermanent'}}->{'branchname'} </font> <br>";
170         }
171     }
172 }
173 $messagetext = substr($messagetext, 0, -4);
174
175 my $messagetable;
176 if ($messagetext) {
177     $messagetable = << "EOF";
178 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
179 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
180 <tr><td> $messagetext </td></tr></table>
181 EOF
182 }
183
184
185 # patrontable ....
186 my $borrowertable;
187 if ($borrower) {
188     my $patrontable = << "EOF";
189 <table border=1 cellpadding=5 cellspacing=0 align=right>
190 <tr><td colspan=2 bgcolor=$headerbackgroundcolor background=$backgroundimage>
191 <font color=black><b>Patron Information</b></font></td></tr>
192 <tr><td colspan=2>
193 <a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} 
194 onClick="openWindow(this,'Member', 480, 640)">$borrower->{'cardnumber'}</a>
195 $borrower->{'surname'}, $borrower->{'title'} $borrower->{'firstname'}<br>
196 </td></tr></table>
197 EOF
198     my $flags = $borrower->{'flags'};
199     my $flaginfotext='';
200     my $flag;
201     my $color = '';
202     foreach $flag (sort keys %$flags) {
203         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
204         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
205         if ($flags->{$flag}->{'noissues'}) {
206             if ($flag eq 'CHARGES') {
207                 $flaginfotext.= <<"EOF";
208 <tr><td valign=top><font color=red>$flag</font></td>
209 <td bgcolor=$color><b>$flags->{$flag}->{'message'}</b> 
210 <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} 
211 onClick=\"openWindow(this, 'Payment', 480,640)\">Payment</a></td></tr>
212 EOF
213             } else {
214                 $flaginfotext.= <<"EOF";
215 <tr><td valign=top><font color=red>$flag</font></td>
216 <td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>
217 EOF
218             }
219         } else {
220             if ($flag eq 'CHARGES') {
221                 $flaginfotext .= << "EOF";
222 <tr><td valign=top>$flag</td>
223 <td> $flags->{$flag}->{'message'} <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} 
224 onClick=\"openWindow(this, 'Payment', 480,640)\">Payment</a></td></tr>
225 EOF
226             } elsif ($flag eq 'WAITING') {
227                 my $itemswaiting='';
228                 my $items = $flags->{$flag}->{'itemlist'};
229                 foreach my $item (@$items) {
230                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
231                     $itemswaiting .= <<"EOF";
232 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra 
233 onClick=\"openWindow(this, 'Item', 480, 640)\">$iteminformation->{'barcode'}</a> 
234 $iteminformation->{'title'} ($branches->{$iteminformation->{'holdingbranch'}}->{'branchname'})<br>
235 EOF
236                 }
237                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
238             } elsif ($flag eq 'ODUES') {
239                 my $items = $flags->{$flag}->{'itemlist'};
240                 $flaginfotext .=  <<"EOF";
241 <tr><td bgcolor=$color><font color=red>$flag</font></td>
242 <td bgcolor=$color>Patron has Overdue books</td></tr>
243 EOF
244             } else {
245                 $flaginfotext .= "<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
246             }
247         }
248     }
249     if ($flaginfotext) {
250         $flaginfotext = << "EOF";
251 <table border=1 cellpadding=5 cellspacing=0> <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>
252 $flaginfotext 
253 </table>
254 EOF
255     }
256     $borrowertable = << "EOF";
257 <table border=0 cellpadding=5>
258 <tr>
259 <td valign=top>$patrontable</td>
260 <td valign=top>$flaginfotext</td>
261 </tr>
262 </table>
263 EOF
264 }
265
266 # the returned items.....
267 my $returneditemstable = << "EOF";
268 <br><p>
269 <table border=1 cellpadding=5 cellspacing=0 align=left>
270 <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage>
271 <font color=black>Returned Items</font></th></tr>
272 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
273 EOF
274
275 my $color='';
276 #set up so only the lat 8 returned items display (make for faster loading pages)
277 my $count=0;
278 foreach (sort {$a <=> $b} keys %returneditems) {
279     if ($count < 8) {
280         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
281         my $barcode = $returneditems{$_};
282         my $duedate = $riduedate{$_};
283         my @datearr = localtime(time());
284         ###
285         # convert to nz date format
286         my @tempdate = split(/-/,$duedate);
287         $duedate = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
288         ####
289         my $todaysdate 
290             = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
291         my $overduetext = "$duedate";
292         ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
293         ($duedate) || ($overduetext = "<img src=/images/blackdot.gif>");
294         my $borrowernumber = $riborrowernumber{$_};
295         my ($borrower) = getpatroninformation(\%env,$borrowernumber,0);
296         my ($iteminformation) = getiteminformation(\%env, 0, $barcode);;
297         $returneditemstable .= << "EOF";
298 <tr><td bgcolor=$color>$overduetext</td>
299 <td bgcolor=$color align=center>
300 <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td>
301 <td bgcolor=$color>$iteminformation->{'title'}</td>
302 <td bgcolor=$color>$iteminformation->{'author'}</td>
303 <td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td>
304 <td bgcolor=$color>
305 <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>
306 EOF
307     } else {
308         last;
309     }
310     $count++;
311 }
312 $returneditemstable .= "</table>\n";
313
314
315 # actually print the page!
316 print $query->header();
317 print startpage();
318 print startmenu('circulation');
319
320 print $title;
321
322 print $reservefoundtext;
323
324 print $barcodeentrytext;
325
326 print $messagetable;
327
328
329 if ($returned) {
330     print $itemtable;
331     print $borrowertable;
332 }
333 (print $returneditemstable) if (%returneditems); 
334
335 print endmenu('circulation');
336 print endpage();
337
338 sub cuecatbarcodedecode {
339     my ($barcode) = @_;
340     chomp($barcode);
341     my @fields = split(/\./,$barcode);
342     my @results = map(decode($_), @fields[1..$#fields]);
343     if ($#results == 2){
344         return $results[2];
345     } else {
346         return $barcode;
347     } 
348
349