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