fixed minor bugs
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 #written 8/5/2002 by Finlay
4 #script to execute issuing 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 $branches = getbranches();
22 my $printers = getprinters(\%env);
23
24 my $query = new CGI;
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 #set up cookie.....
33 my $info = '';
34 my $branchcookie;
35 my $printercookie;
36 if ($query->param('setcookies')) {
37     $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
38     $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
39 }
40
41 $env{'branchcode'}=$branch;
42 $env{'printer'}=$printer;
43 $env{'queue'}=$printer;
44
45 my @datearr = localtime(time());
46 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
47 warn $todaysdate;
48
49
50 my $message;
51 my $borrowerslist;
52 # if there is a list of find borrowers....
53 my $findborrower = $query->param('findborrower');
54 if ($findborrower) {
55     my ($borrowers, $flags) = findborrower(\%env, $findborrower);
56     my @borrowers=@$borrowers;
57     if ($#borrowers == -1) {
58         $query->param('findborrower', '');
59         $message =  "No borrower matched '$findborrower'";
60     } elsif ($#borrowers == 0) {
61         $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
62         $query->param('barcode','');
63     } else {
64         $borrowerslist = \@borrowers;
65     }
66 }
67
68 my $borrowernumber = $query->param('borrnumber');    
69
70 # get the currently issued books......
71 my $borrower;
72 my $flags;
73 if ($borrowernumber) {
74     ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
75 }
76
77 # get the responses to any questions.....
78 my %responses;
79 foreach (sort $query->param) {
80     if ($_ =~ /response-(\d*)/) {
81         $responses{$1} = $query->param($_);
82     }
83 }
84 if (my $qnumber = $query->param('questionnumber')) {
85     $responses{$qnumber} = $query->param('answer');
86 }
87
88
89 # if the barcode is set    
90 my $barcode = $query->param('barcode');
91 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
92
93 my $year=$query->param('year');
94 my $month=$query->param('month');
95 my $day=$query->param('day');
96
97
98 if ($barcode) {
99     $barcode = cuecatbarcodedecode($barcode);
100     my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
101
102     unless ($invalidduedate) {
103         my @time=localtime(time);
104         my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
105         ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message) 
106                      = issuebook(\%env, $borrower, $barcode, \%responses, $date);
107     }
108 }
109
110
111 ##################################################################################
112 # HTML code....
113
114
115 my $rejectedtext;
116 if ($rejected) {
117     if ($rejected == -1) {
118     } else {
119         $rejectedtext = << "EOF";
120 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
121 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
122 <tr><td><font color=red size=5>$rejected</font></td></tr>
123 </table>
124 <br>
125 EOF
126     }
127 }
128
129 my $selectborrower;
130 if ($borrowerslist) {
131     $selectborrower = <<"EOF";
132 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
133 <input type=hidden name=branch value=$branch>
134 <input type=hidden name=printer value=$printer>
135 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
136 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
137 <font color=black><b>Select a borrower</b></font></th></tr>\n
138 <tr><td align=center>
139 <select name=borrnumber size=7>
140 EOF
141     foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
142         $selectborrower .= <<"EOF";
143 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
144 EOF
145     }
146     $selectborrower .= <<"EOF";
147 </select><br>
148 <input type=submit>
149 </td></tr></table>
150 EOF
151 }
152
153 # title....
154 my $title = <<"EOF";
155 <table align="right"><tr><td>
156 <a href=circulation.pl>
157 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a> &nbsp
158 <a href=returns.pl>
159 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
160 &nbsp<a href=branchtransfers.pl>
161 <img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers">
162 </a>
163 </td></tr></table>
164 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
165 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp 
166 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
167 <a href=selectbranchprinter.pl>Change Settings</a></td>
168 <input type=hidden name=branch value=$branch>
169 <input type=hidden name=printer value=$printer>
170 <p>
171 EOF
172
173
174
175 my $cardnumberinput = << "EOF";
176 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
177 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
178 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
179 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
180 <tr><td><input name=findborrower></td></tr>
181 </table>
182 </form>
183 EOF
184
185 my $responsesform = '';
186 foreach (keys %responses) {
187     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
188 }
189 my $questionform;
190 if ($question) {
191     my $stickyduedate=$query->param('stickyduedate');
192     $questionform = <<"EOF";
193 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
194 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
195 <font color=black><b>Issuing Question</b></font></th></tr>
196 <tr><td><table border=0 cellpadding=10><tr><td> 
197 Attempting to issue $iteminformation->{'title'} 
198 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
199 <p>
200 $question
201 </td></tr></table></td></tr>
202 <tr><td align=center>
203 <table border=0>
204 <tr><td>
205 <form method=get>
206 <input type=hidden name=borrnumber value=$borrowernumber>
207 <input type=hidden name=barcode value=$barcode>
208 <input type=hidden name=questionnumber value=$questionnumber>
209 <input type=hidden name=day value=$day>
210 <input type=hidden name=month value=$month>
211 <input type=hidden name=year value=$year>
212 <input type=hidden name=stickyduedate value=$stickyduedate>
213 <input type=hidden name=branch value=$branch>
214 <input type=hidden name=printer value=$printer>
215 $responsesform
216 <input type=hidden name=answer value=Y>
217 <input type=submit value=Yes>
218 </form>
219 </td>
220 <td>
221 <form method=get>
222 <input type=hidden name=borrnumber value=$borrowernumber>
223 <input type=hidden name=barcode value=$barcode>
224 <input type=hidden name=questionnumber value=$questionnumber>
225 <input type=hidden name=day value=$day>
226 <input type=hidden name=month value=$month>
227 <input type=hidden name=year value=$year>
228 <input type=hidden name=stickyduedate value=$stickyduedate>
229 <input type=hidden name=branch value=$branch>
230 <input type=hidden name=printer value=$printer>
231 $responsesform
232 <input type=hidden name=answer value=N>
233 <input type=submit value=No>
234 </form>
235 </td>
236 </tr>
237 </table>
238 </td></tr>
239 </table>
240 </td></tr>
241 </table>
242 EOF
243 }
244
245
246 # Barcode entry box, with hidden inputs attached....
247 my $counter = 1;
248 my $dayoptions = '';
249 my $monthoptions = '';
250 my $yearoptions = '';
251 for (my $i=1; $i<32; $i++) {
252     my $selected='';
253     if (($query->param('stickyduedate')) && ($day==$i)) {
254         $selected='selected';
255     }
256     $dayoptions.="<option value=$i $selected>$i";
257 }
258 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
259     my $selected='';
260     if (($query->param('stickyduedate')) && ($month==$counter)) {
261         $selected='selected';
262     }
263     $monthoptions.="<option value=$counter $selected>$_";
264     $counter++;
265 }
266 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
267     my $selected='';
268     if (($query->param('stickyduedate')) && ($year==$i)) {
269         $selected='selected';
270     }
271     $yearoptions.="<option value=$i $selected>$i";
272 }
273 my $selected='';
274 ($query->param('stickyduedate')) && ($selected='checked');
275
276
277 my $barcodeentrytext = <<"EOF";
278 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
279 <table border=1 cellpadding=5>
280 <tr>
281 <td align=center valign=top>
282 <table border=0 cellspacing=0 cellpadding=5>
283 <tr><th align=center background=$backgroundimage>
284 <font color=black><b>Enter Book Barcode</b></font></th></tr>
285 <tr><td align=center>
286 <table border=0>
287 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
288 <tr><td colspan=3 align=center>
289 <table border=0 cellpadding=0 cellspacing=0>
290 <tr><td>
291 <select name=day><option value=0>Day$dayoptions</select>
292 </td><td>
293 <select name=month><option value=0>Month$monthoptions</select>
294 </td><td>
295 <select name=year><option value=0>Year$yearoptions</select>
296 </td></tr>
297 </table>
298 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
299 </td></tr>
300 </table>
301 <input type=hidden name=borrnumber value=$borrowernumber>
302 <input type=hidden name=branch value=$branch>
303 <input type=hidden name=printer value=$printer>
304 </td></tr></table>
305 </td></tr></table>
306 </form>
307 EOF
308
309
310 # collect the messages and put into message table....
311 my $messagetable;
312 if ($message) {
313     $messagetable = << "EOF";
314 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
315 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
316 <tr><td> $message </td></tr></table>
317 EOF
318 }
319
320
321
322 # make the issued books table.....
323 my $todaysissues='';
324 my $previssues='';
325 if ($borrower) {
326     my $issueslist = getissues($borrower);
327     my $tcolor = '';
328     my $pcolor = '';
329     foreach my $it (sort keys %$issueslist) {
330         my $dd = $issueslist->{$it}->{'date_due'};
331         my $issuedate = $issueslist->{$it}->{'timestamp'};
332         $issuedate = substr($issuedate, 0, 8);
333
334         my $bookissue = $issueslist->{$it};
335         my $bgcolor='';
336         my $datedue = $bookissue->{'date_due'};
337         #convert to nz style dates
338         #this should be set with some kinda config variable         
339         my @tempdate=split(/-/,$dd);
340         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
341         $datedue=~s/-//g;
342         if ($datedue < $todaysdate) {
343             $dd="<font color=red>$dd</font>\n";
344         }
345         if ($todaysdate == $issuedate) {
346             ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
347             $todaysissues .=<< "EOF";
348 <tr><td bgcolor=$tcolor align=center>$dd</td>
349 <td bgcolor=$tcolor align=center>
350 <a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td>
351 <td bgcolor=$tcolor>$bookissue->{'title'}</td>
352 <td bgcolor=$tcolor>$bookissue->{'author'}</td>
353 <td bgcolor=$tcolor align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>
354 EOF
355         } else {
356             ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
357             $previssues .= << "EOF";
358 <tr><td bgcolor=$pcolor align=center>$dd</td>
359 <td bgcolor=$pcolor align=center>
360 <a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td>
361 <td bgcolor=$pcolor>$bookissue->{'title'}</td>
362 <td bgcolor=$pcolor>$bookissue->{'author'}</td>
363 <td bgcolor=$pcolor align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>
364 EOF
365         }
366     }
367 }
368
369 my $issuedbookstable;
370 if ($todaysissues) {
371     $issuedbookstable .= <<"EOF";
372 <table border=1 cellpadding=5 cellspacing=0 width=80%>
373 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
374 <b>Todays Issues</b></font></th></tr>
375 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
376 $todaysissues
377 </table>
378 EOF
379 }
380 if ($previssues) {
381     $issuedbookstable .= <<"EOF";
382 <table border=1 cellpadding=5 cellspacing=0 width=80%>
383 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
384 <b>Previous Issues</b></font></th></tr>
385 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
386 $previssues
387 </table>
388 EOF
389 }
390
391
392
393
394
395 # actually print the page!
396
397
398 if ($branchcookie && $printercookie) {
399     print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
400 } else {
401     print $query->header();
402 }
403
404 print startpage();
405 print startmenu('circulation');
406
407 print $title;
408
409
410 print $info;
411
412 if ($question) {
413     print $questionform;
414 }
415
416 print $rejectedtext;
417 print $messagetable;
418
419 unless ($borrower) {
420     if ($borrowerslist) {
421         print $selectborrower;
422     } else {
423         print $cardnumberinput;
424     }
425 }
426
427
428
429 if ($borrower) {
430     my ($patrontable, $flaginfotable) = patrontable($borrower);
431     print $patrontable;
432     print $flaginfotable;
433     print $barcodeentrytext;
434     print "<p clear=all><br><br>";
435     print $issuedbookstable;
436 }
437
438
439
440
441 print endmenu('circulation');
442 print endpage();
443
444
445 ####################################################################
446 # Extra subroutines,,,
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
460 sub fixdate {
461     my ($year, $month, $day) = @_;
462     my $invalidduedate;
463     my $date;
464     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
465         $env{'datedue'}='';
466     } else {
467         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
468             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
469         } else {
470             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
471                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
472             } elsif (($day > 29) && ($month == 2)) {
473                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
474             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
475                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
476             } else {
477                 $date="$year-$month-$day";
478             }
479         }
480     }
481     return ($date, $invalidduedate);
482 }
483
484
485 sub patrontable {
486     my ($borrower) = @_;
487     my $flags = $borrower->{'flags'};
488     my $flaginfotable='';
489     my $flaginfotext='';
490     my $flag;
491     my $color='';
492     foreach $flag (sort keys %$flags) {
493         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
494         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
495         if ($flags->{$flag}->{'noissues'}) {
496             if ($flag eq 'CHARGES') {
497                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color><b>$flags->{$flag}->{'message'}</b> <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this, 'Payment', 480,640)\">Payment</a></td></tr>\n";
498             } else {
499                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
500             }
501         } else {
502             if ($flag eq 'CHARGES') {
503                 $flaginfotext.="<tr><td valign=top>$flag</td><td> $flags->{$flag}->{'message'} <a href=/cgi-bin/koha/pay.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this, 'Payment', 480,640)\">Payment</a></td></tr>\n";
504             } elsif ($flag eq 'WAITING') {
505                 my $itemswaiting='';
506                 my $items=$flags->{$flag}->{'itemlist'};
507                 foreach my $item (@$items) {
508                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
509                     $itemswaiting.="<a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$iteminformation->{'barcode'}</a> $iteminformation->{'title'} ($branches->{$iteminformation->{'holdingbranch'}}->{'branchname'})<br>\n";
510                 }
511                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
512             } elsif ($flag eq 'ODUES') {
513                 my $items=$flags->{$flag}->{'itemlist'};
514                 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
515                 my $currentcolor=$color;
516                 {
517                     my $color=$currentcolor;
518                     foreach my $item (@$items) {
519                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
520                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
521                         $itemswaiting.="<tr><td><font color=red>$iteminformation->{'date_due'}</font></td><td bgcolor=$color><a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$iteminformation->{'barcode'}</a></td><td>$iteminformation->{'title'}</td></tr>\n";
522                     }               
523                 }
524                 $itemswaiting.="</table>\n";
525                 if ($query->param('module') ne 'returns'){
526                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
527                 } else {
528                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n"; 
529                 }
530             } else {
531                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
532             }
533         }
534     }
535     ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
536     $flaginfotext.="</table>";
537     my $patrontable= << "EOF";
538 <br><p>
539     <table border=1 cellpadding=5 cellspacing=0 align=right>
540     <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
541     <tr><td colspan=2>
542     <a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} onClick="openWindow(this,'Member', 480, 640)">$borrower->{'cardnumber'}</a> $borrower->{'surname'}, $borrower->{'title'} $borrower->{'firstname'}<br>$borrower->{'streetaddress'} $borrower->{'city'} Cat: $borrower->{'categorycode'} </td></tr>
543 EOF
544     return($patrontable, $flaginfotext);
545 }
546
547
548
549