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