3 #written 8/5/2002 by Finlay
4 #script to execute issuing of books
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Circulation::Circ2;
32 my $headerbackgroundcolor='#99cc33';
33 my $circbackgroundcolor='#ffffcc';
34 my $circbackgroundcolor='white';
35 my $linecolor1='#ffffcc';
36 my $linecolor2='white';
37 my $backgroundimage="/images/background-mem.gif";
39 my $branches = getbranches();
40 my $printers = getprinters(\%env);
44 my $branch = $query->param("branch");
45 my $printer = $query->param("printer");
47 ($branch) || ($branch=$query->cookie('branch')) ;
48 ($printer) || ($printer=$query->cookie('printer')) ;
54 if ($query->param('setcookies')) {
55 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
56 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
59 $env{'branchcode'}=$branch;
60 $env{'printer'}=$printer;
61 $env{'queue'}=$printer;
63 my @datearr = localtime(time());
64 my $tday = localtime(time());
65 warn "today: $tday \n";
66 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
72 # if there is a list of find borrowers....
73 my $findborrower = $query->param('findborrower');
75 my ($borrowers, $flags) = findborrower(\%env, $findborrower);
76 my @borrowers=@$borrowers;
77 if ($#borrowers == -1) {
78 $query->param('findborrower', '');
79 $message = "No borrower matched '$findborrower'";
80 } elsif ($#borrowers == 0) {
81 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
82 $query->param('barcode','');
84 $borrowerslist = \@borrowers;
88 my $borrowernumber = $query->param('borrnumber');
89 my $bornum = $query->param('borrnumber');
90 # check and see if we should print
91 my $print=$query->param('print');
92 my $barcode = $query->param('barcode');
93 if ($barcode eq '' && $print eq 'maybe'){
96 if ($print eq 'yes' && $borrowernumber ne ''){
97 printslip(\%env,$borrowernumber);
98 $query->param('borrnumber','');
104 # get the borrower information.....
107 if ($borrowernumber) {
108 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
111 # get the responses to any questions.....
113 foreach (sort $query->param) {
114 if ($_ =~ /response-(\d*)/) {
115 $responses{$1} = $query->param($_);
118 if (my $qnumber = $query->param('questionnumber')) {
119 $responses{$qnumber} = $query->param('answer');
124 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
126 my $year=$query->param('year');
127 my $month=$query->param('month');
128 my $day=$query->param('day');
130 # if the barcode is set
132 $barcode = cuecatbarcodedecode($barcode);
133 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
135 unless ($invalidduedate) {
136 $env{'datedue'}=$datedue;
137 my @time=localtime(time);
138 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
139 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
140 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
144 # reload the borrower info for the sake of reseting the flags.....
145 if ($borrowernumber) {
146 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
150 ##################################################################################
156 if ($rejected == -1) {
158 $rejectedtext = << "EOF";
159 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
160 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
161 <tr><td><font color=red size=5>$rejected</font></td></tr>
169 if ($borrowerslist) {
170 $selectborrower = <<"EOF";
171 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
172 <input type=hidden name=branch value=$branch>
173 <input type=hidden name=printer value=$printer>
174 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
175 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
176 <font color=black><b>Select a borrower</b></font></th></tr>\n
177 <tr><td align=center>
178 <select name=borrnumber size=7>
180 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
181 $selectborrower .= <<"EOF";
182 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
185 $selectborrower .= <<"EOF";
194 <table align="right"><tr><td>
195 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
196 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
198 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
199  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
201 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
202 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
203 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
204 <a href=selectbranchprinter.pl>Change Settings</a></td>
205 <input type=hidden name=branch value=$branch>
206 <input type=hidden name=printer value=$printer>
210 my $titlenoborrower = <<"EOF";
211 <table align="right"><tr><td>
213 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
214  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
216 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
217 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
218 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
219 <a href=selectbranchprinter.pl>Change Settings</a></td>
220 <input type=hidden name=branch value=$branch>
221 <input type=hidden name=printer value=$printer>
227 my $cardnumberinput = << "EOF";
228 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
229 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
230 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
231 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
232 <tr><td><input name=findborrower></td></tr>
233 <input type=hidden name=branch value=$branch>
234 <input type=hidden name=printer value=$printer>
239 my $responsesform = '';
240 foreach (keys %responses) {
241 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
245 my $stickyduedate=$query->param('stickyduedate');
246 $questionform = <<"EOF";
247 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
248 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
249 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
250 <tr><td><table border=0 cellpadding=10><tr><td>
251 Attempting to issue $iteminformation->{'title'}
252 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
255 </td></tr></table></td></tr>
256 <tr><td align=center>
260 <input type=hidden name=borrnumber value=$borrowernumber>
261 <input type=hidden name=barcode value=$barcode>
262 <input type=hidden name=questionnumber value=$questionnumber>
263 <input type=hidden name=day value=$day>
264 <input type=hidden name=month value=$month>
265 <input type=hidden name=year value=$year>
266 <input type=hidden name=stickyduedate value=$stickyduedate>
267 <input type=hidden name=branch value=$branch>
268 <input type=hidden name=printer value=$printer>
270 <input type=hidden name=answer value=Y>
271 <input type=submit value=Yes>
276 <input type=hidden name=borrnumber value=$borrowernumber>
277 <input type=hidden name=barcode value=$barcode>
278 <input type=hidden name=questionnumber value=$questionnumber>
279 <input type=hidden name=day value=$day>
280 <input type=hidden name=month value=$month>
281 <input type=hidden name=year value=$year>
282 <input type=hidden name=stickyduedate value=$stickyduedate>
283 <input type=hidden name=branch value=$branch>
284 <input type=hidden name=printer value=$printer>
286 <input type=hidden name=answer value=N>
287 <input type=submit value=No>
300 # Barcode entry box, with hidden inputs attached....
303 my $monthoptions = '';
304 my $yearoptions = '';
305 for (my $i=1; $i<32; $i++) {
307 if (($query->param('stickyduedate')) && ($day==$i)) {
308 $selected='selected';
310 $dayoptions.="<option value=$i $selected>$i";
312 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
314 if (($query->param('stickyduedate')) && ($month==$counter)) {
315 $selected='selected';
317 $monthoptions.="<option value=$counter $selected>$_";
320 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
322 if (($query->param('stickyduedate')) && ($year==$i)) {
323 $selected='selected';
325 $yearoptions.="<option value=$i $selected>$i";
328 ($query->param('stickyduedate')) && ($selected='checked');
331 my $barcodeentrytext = <<"EOF";
332 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
333 <table border=1 cellpadding=5>
335 <td align=center valign=top>
336 <table border=0 cellspacing=0 cellpadding=5>
337 <tr><th align=center background=$backgroundimage>
338 <font color=black><b>Enter Book Barcode</b></font></th></tr>
339 <tr><td align=center>
341 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
342 <tr><td colspan=3 align=center>
343 <table border=0 cellpadding=0 cellspacing=0>
345 <select name=day><option value=0>Day$dayoptions</select>
347 <select name=month><option value=0>Month$monthoptions</select>
349 <select name=year><option value=0>Year$yearoptions</select>
352 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
355 <input type=hidden name=borrnumber value=$borrowernumber>
356 <input type=hidden name=branch value=$branch>
357 <input type=hidden name=printer value=$printer>
358 <input type=hidden name=print value=maybe>
360 if ($flags->{'CHARGES'}){
361 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
363 my $amountold=$flags->{'CHARGES'}->{'message'};
364 my @temp=split(/\$/,$amountold);
366 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
367 $barcodeentrytext.=<<"EOF";
374 # collect the messages and put into message table....
377 $messagetable = << "EOF";
378 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
379 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
380 <tr><td> $message </td></tr></table>
386 # make the issued books table.....
392 my $issueslist = getissues($borrower);
393 foreach my $it (keys %$issueslist) {
394 my $issuedate = $issueslist->{$it}->{'timestamp'};
395 $issuedate = substr($issuedate, 0, 8);
396 if ($todaysdate == $issuedate) {
397 push @todaysissues, $issueslist->{$it};
399 push @previousissues, $issueslist->{$it};
404 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
405 my $dd = $book->{'date_due'};
406 my $datedue = $book->{'date_due'};
407 #convert to nz style dates
408 #this should be set with some kinda config variable
409 my @tempdate=split(/-/,$dd);
410 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
412 if ($datedue < $todaysdate) {
413 $dd="<font color=red>$dd</font>\n";
415 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
416 $todaysissues .=<< "EOF";
417 <tr><td bgcolor=$tcolor align=center>$dd</td>
418 <td bgcolor=$tcolor align=center>
419 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
420 <td bgcolor=$tcolor>$book->{'title'}</td>
421 <td bgcolor=$tcolor>$book->{'author'}</td>
422 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
425 # FIXME - For small and private libraries, it'd be nice if this
426 # table included a "Return" link next to each book, so that you
427 # don't have to remember the book's bar code and type it in on the
429 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
430 my $dd = $book->{'date_due'};
431 my $datedue = $book->{'date_due'};
432 #convert to nz style dates
433 #this should be set with some kinda config variable
434 my @tempdate=split(/-/,$dd);
435 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
437 if ($datedue < $todaysdate) {
438 $dd="<font color=red>$dd</font>\n";
440 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
441 $previssues .= << "EOF";
442 <tr><td bgcolor=$pcolor align=center>$dd</td>
443 <td bgcolor=$pcolor align=center>
444 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
445 <td bgcolor=$pcolor>$book->{'title'}</td>
446 <td bgcolor=$pcolor>$book->{'author'}</td>
447 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
452 my $issuedbookstable;
454 $issuedbookstable .= <<"EOF";
455 <table border=1 cellpadding=5 cellspacing=0 width=80%>
456 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
457 <b>Todays Issues</b></font></th></tr>
458 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
464 $issuedbookstable .= <<"EOF";
465 <table border=1 cellpadding=5 cellspacing=0 width=80%>
466 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
467 <b>Previous Issues</b></font></th></tr>
468 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
478 # actually print the page!
481 if ($branchcookie && $printercookie) {
482 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
484 print $query->header();
488 my @inp=startmenu('circulation');
489 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
491 for (my $i=0;$i<$count;$i++){
492 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
499 #print startmenu('circulation');
503 print $titlenoborrower;
518 if ($borrowerslist) {
519 print $selectborrower;
521 print $cardnumberinput;
528 my ($patrontable, $flaginfotable) = patrontable($borrower);
530 print $flaginfotable;
531 print $barcodeentrytext;
532 print "<p clear=all><br><br>";
533 print $issuedbookstable;
539 print endmenu('circulation');
543 ####################################################################
544 # Extra subroutines,,,
546 sub cuecatbarcodedecode {
549 my @fields = split(/\./,$barcode);
550 my @results = map(decode($_), @fields[1..$#fields]);
559 my ($year, $month, $day) = @_;
562 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
565 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
566 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
568 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
569 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
570 } elsif (($day > 29) && ($month == 2)) {
571 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
572 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
573 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
575 $date="$year-$month-$day";
579 return ($date, $invalidduedate);
585 my $flags = $borrower->{'flags'};
586 my $flaginfotable='';
590 foreach $flag (sort keys %$flags) {
591 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
592 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
593 if ($flags->{$flag}->{'noissues'}) {
594 if ($flag eq 'CHARGES') {
595 $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";
597 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
600 if ($flag eq 'CHARGES') {
601 $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";
602 } elsif ($flag eq 'WAITING') {
604 my $items=$flags->{$flag}->{'itemlist'};
605 foreach my $item (@$items) {
606 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
607 $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";
609 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
610 } elsif ($flag eq 'ODUES') {
611 my $items=$flags->{$flag}->{'itemlist'};
612 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
613 my $currentcolor=$color;
615 my $color=$currentcolor;
616 foreach my $item (@$items) {
617 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
618 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
619 $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";
622 $itemswaiting.="</table>\n";
623 if ($query->param('module') ne 'returns'){
624 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
626 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
629 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
633 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
634 $flaginfotext.="</table>";
635 my $patrontable= << "EOF";
637 <table border=1 cellpadding=5 cellspacing=0 align=right>
638 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
640 <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>
642 return($patrontable, $flaginfotext);
648 my ($env,$borrowernumber)=@_;
649 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
650 $env->{'todaysissues'}=1;
651 my ($borrowerissues) = currentissues($env, $borrower);
652 $env->{'nottodaysissues'}=1;
653 $env->{'todaysissues'}=0;
654 my ($borroweriss2)=currentissues($env, $borrower);
655 $env->{'nottodaysissues'}=0;
658 foreach (sort {$a <=> $b} keys %$borrowerissues) {
659 $issues[$i]=$borrowerissues->{$_};
660 my $dd=$issues[$i]->{'date_due'};
662 #convert to nz style dates
663 #this should be set with some kinda config variable
664 my @tempdate=split(/-/,$dd);
665 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
668 foreach (sort {$a <=> $b} keys %$borroweriss2) {
669 $issues[$i]=$borroweriss2->{$_};
670 my $dd=$issues[$i]->{'date_due'};
672 #convert to nz style dates
673 #this should be set with some kinda config variable
674 my @tempdate=split(/-/,$dd);
675 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
678 remoteprint($env,\@issues,$borrower);