Add comments, make branchrelations consistent with other table additions
[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
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=6>Error Issuing Book</font></th></tr>
122 <tr><td><font color=red size=6>$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=get action=/cgi-bin/koha/circ/issues.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 <p>
156 <table border=0 cellpadding=5 width=90%><tr>
157 <td align="left"><FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
158 <b>Branch:</b> $branches->{$branch}->{'branchname'} &nbsp 
159 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
160 <a href=selectbranchprinter.pl>Change Settings</a></td>
161 <td align="right" valign="top">
162 <FONT SIZE=2  face="arial, helvetica">
163 <a href=circulation.pl>Next Borrower</a> || 
164 <a href=returns.pl>Returns</a> || 
165 <a href=branchtransfers.pl>Transfers</a></font><p>
166 </td></tr></table>
167 <input type=hidden name=branch value=$branch>
168 <input type=hidden name=printer value=$printer>
169 </p>
170 EOF
171
172
173
174 my $cardnumberinput = << "EOF";
175 <form method=get action=/cgi-bin/koha/circ/issues.pl>
176 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
177 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
178 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
179 <tr><td><input name=findborrower></td></tr>
180 </table>
181 <input type=hidden name=branch value=$branch>
182 <input type=hidden name=printer value=$printer>
183 </form>
184 EOF
185
186 my $responsesform = '';
187 foreach (keys %responses) {
188     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
189 }
190 my $questionform;
191 if ($question) {
192     my $stickyduedate=$query->param('stickyduedate');
193     $questionform = <<"EOF";
194 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
195 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
196 <font color=black><b>Issuing Question</b></font></th></tr>
197 <tr><td><table border=0 cellpadding=10><tr><td> 
198 Attempting to issue $iteminformation->{'title'} 
199 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
200 <p>
201 $question
202 </td></tr></table></td></tr>
203 <tr><td align=center>
204 <table border=0>
205 <tr><td>
206 <form method=get>
207 <input type=hidden name=borrnumber value=$borrowernumber>
208 <input type=hidden name=barcode value=$barcode>
209 <input type=hidden name=questionnumber value=$questionnumber>
210 <input type=hidden name=day value=$day>
211 <input type=hidden name=month value=$month>
212 <input type=hidden name=year value=$year>
213 <input type=hidden name=stickyduedate value=$stickyduedate>
214 <input type=hidden name=branch value=$branch>
215 <input type=hidden name=printer value=$printer>
216 $responsesform
217 <input type=hidden name=answer value=Y>
218 <input type=submit value=Yes>
219 </form>
220 </td>
221 <td>
222 <form method=get>
223 <input type=hidden name=borrnumber value=$borrowernumber>
224 <input type=hidden name=barcode value=$barcode>
225 <input type=hidden name=questionnumber value=$questionnumber>
226 <input type=hidden name=day value=$day>
227 <input type=hidden name=month value=$month>
228 <input type=hidden name=year value=$year>
229 <input type=hidden name=stickyduedate value=$stickyduedate>
230 <input type=hidden name=branch value=$branch>
231 <input type=hidden name=printer value=$printer>
232 $responsesform
233 <input type=hidden name=answer value=N>
234 <input type=submit value=No>
235 </form>
236 </td>
237 </tr>
238 </table>
239 </td></tr>
240 </table>
241 </td></tr>
242 </table>
243 EOF
244 }
245
246
247 # Barcode entry box, with hidden inputs attached....
248 my $counter = 1;
249 my $dayoptions = '';
250 my $monthoptions = '';
251 my $yearoptions = '';
252 for (my $i=1; $i<32; $i++) {
253     my $selected='';
254     if (($query->param('stickyduedate')) && ($day==$i)) {
255         $selected='selected';
256     }
257     $dayoptions.="<option value=$i $selected>$i";
258 }
259 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
260     my $selected='';
261     if (($query->param('stickyduedate')) && ($month==$counter)) {
262         $selected='selected';
263     }
264     $monthoptions.="<option value=$counter $selected>$_";
265     $counter++;
266 }
267 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
268     my $selected='';
269     if (($query->param('stickyduedate')) && ($year==$i)) {
270         $selected='selected';
271     }
272     $yearoptions.="<option value=$i $selected>$i";
273 }
274 my $selected='';
275 ($query->param('stickyduedate')) && ($selected='checked');
276
277
278 my $barcodeentrytext = <<"EOF";
279 <form method=post action=/cgi-bin/koha/circ/issues.pl>
280 <table border=1 cellpadding=5>
281 <tr>
282 <td align=center valign=top>
283 <table border=0 cellspacing=0 cellpadding=5>
284 <tr><th align=center background=$backgroundimage>
285 <font color=black><b>Enter Book Barcode</b></font></th></tr>
286 <tr><td align=center>
287 <table border=0>
288 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
289 <tr><td colspan=3 align=center>
290 <table border=0 cellpadding=0 cellspacing=0>
291 <tr><td>
292 <select name=day><option value=0>Day$dayoptions</select>
293 </td><td>
294 <select name=month><option value=0>Month$monthoptions</select>
295 </td><td>
296 <select name=year><option value=0>Year$yearoptions</select>
297 </td></tr>
298 </table>
299 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
300 </td></tr>
301 </table>
302 <input type=hidden name=borrnumber value=$borrowernumber>
303 <input type=hidden name=branch value=$branch>
304 <input type=hidden name=printer value=$printer>
305 </td></tr></table>
306 </td></tr></table>
307 </form>
308 EOF
309
310
311 # collect the messages and put into message table....
312 my $messagetable;
313 if ($message) {
314     $messagetable = << "EOF";
315 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
316 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
317 <tr><td> $message </td></tr></table>
318 EOF
319 }
320
321
322
323 # make the issued books table.....
324 my $todaysissues='';
325 my $previssues='';
326 if ($borrower) {
327     my $issueslist = getissues($borrower);
328     my $tcolor = '';
329     my $pcolor = '';
330     foreach my $it (sort keys %$issueslist) {
331         my $dd = $issueslist->{$it}->{'date_due'};
332         my $issuedate = $issueslist->{$it}->{'timestamp'};
333         $issuedate = substr($issuedate, 0, 8);
334
335         my $bookissue = $issueslist->{$it};
336         my $bgcolor='';
337         my $datedue = $bookissue->{'date_due'};
338         #convert to nz style dates
339         #this should be set with some kinda config variable         
340         my @tempdate=split(/-/,$dd);
341         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
342         $datedue=~s/-//g;
343         if ($datedue < $todaysdate) {
344             $dd="<font color=red>$dd</font>\n";
345         }
346         if ($todaysdate == $issuedate) {
347             ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
348             $todaysissues .=<< "EOF";
349 <tr><td bgcolor=$tcolor align=center>$dd</td>
350 <td bgcolor=$tcolor align=center>
351 <a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td>
352 <td bgcolor=$tcolor>$bookissue->{'title'}</td>
353 <td bgcolor=$tcolor>$bookissue->{'author'}</td>
354 <td bgcolor=$tcolor align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>
355 EOF
356         } else {
357             ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
358             $previssues .= << "EOF";
359 <tr><td bgcolor=$pcolor align=center>$dd</td>
360 <td bgcolor=$pcolor align=center>
361 <a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td>
362 <td bgcolor=$pcolor>$bookissue->{'title'}</td>
363 <td bgcolor=$pcolor>$bookissue->{'author'}</td>
364 <td bgcolor=$pcolor align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>
365 EOF
366         }
367     }
368 }
369
370 my $issuedbookstable;
371 if ($todaysissues) {
372     $issuedbookstable .= <<"EOF";
373 <table border=1 cellpadding=5 cellspacing=0 width=80%>
374 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
375 <b>Todays Issues</b></font></th></tr>
376 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
377 $todaysissues
378 </table>
379 EOF
380 }
381 if ($previssues) {
382     $issuedbookstable .= <<"EOF";
383 <table border=1 cellpadding=5 cellspacing=0 width=80%>
384 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
385 <b>Previous Issues</b></font></th></tr>
386 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
387 $previssues
388 </table>
389 EOF
390 }
391
392
393
394
395
396 # actually print the page!
397
398
399 if ($branchcookie && $printercookie) {
400     print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
401 } else {
402     print $query->header();
403 }
404
405 print startpage();
406 print startmenu('circulation');
407
408 print $title;
409
410
411 print $info;
412
413 if ($question) {
414     print $questionform;
415 }
416
417 print $rejectedtext;
418 print $messagetable;
419
420 unless ($borrower) {
421     if ($borrowerslist) {
422         print $selectborrower;
423     } else {
424         print $cardnumberinput;
425     }
426 }
427
428
429
430 if ($borrower) {
431     my ($patrontable, $flaginfotable) = patrontable($borrower);
432     print $patrontable;
433     print $flaginfotable;
434     print $barcodeentrytext;
435     print "<p clear=all>";
436     print $issuedbookstable;
437 }
438
439
440
441
442 print endmenu('circulation');
443 print endpage();
444
445
446 ####################################################################
447 # Extra subroutines,,,
448
449 sub cuecatbarcodedecode {
450     my ($barcode) = @_;
451     chomp($barcode);
452     my @fields = split(/\./,$barcode);
453     my @results = map(decode($_), @fields[1..$#fields]);
454     if ($#results == 2){
455         return $results[2];
456     } else {
457         return $barcode;
458     } 
459
460
461 sub fixdate {
462     my ($year, $month, $day) = @_;
463     my $invalidduedate;
464     my $date;
465     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
466         $env{'datedue'}='';
467     } else {
468         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
469             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
470         } else {
471             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
472                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
473             } elsif (($day > 29) && ($month == 2)) {
474                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
475             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
476                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
477             } else {
478                 $date="$year-$month-$day";
479             }
480         }
481     }
482     return ($date, $invalidduedate);
483 }
484
485
486 sub patrontable {
487     my ($borrower) = @_;
488     my $flags = $borrower->{'flags'};
489     my $flaginfotable='';
490     my $flaginfotext='';
491     my $flag;
492     my $color='';
493     foreach $flag (sort keys %$flags) {
494         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
495         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
496         if ($flags->{$flag}->{'noissues'}) {
497             if ($flag eq 'CHARGES') {
498                 $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";
499             } else {
500                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
501             }
502         } else {
503             if ($flag eq 'CHARGES') {
504                 $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";
505             } elsif ($flag eq 'WAITING') {
506                 my $itemswaiting='';
507                 my $items=$flags->{$flag}->{'itemlist'};
508                 foreach my $item (@$items) {
509                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
510                     $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";
511                 }
512                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
513             } elsif ($flag eq 'ODUES') {
514                 my $items=$flags->{$flag}->{'itemlist'};
515                 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
516                 my $currentcolor=$color;
517                 {
518                     my $color=$currentcolor;
519                     foreach my $item (@$items) {
520                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
521                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
522                         $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";
523                     }               
524                 }
525                 $itemswaiting.="</table>\n";
526                 if ($query->param('module') ne 'returns'){
527                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
528                 } else {
529                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n"; 
530                 }
531             } else {
532                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
533             }
534         }
535     }
536     ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
537     $flaginfotext.="</table>";
538     my $patrontable= << "EOF";
539 <br><p>
540     <table border=1 cellpadding=5 cellspacing=0 align=right>
541     <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
542     <tr><td colspan=2>
543     <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>
544 EOF
545     return($patrontable, $flaginfotext);
546 }
547
548
549
550