2 # Please use 8-character tabs for this file (indents are every 4 characters)
4 #written 8/5/2002 by Finlay
5 #script to execute issuing of books
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Circulation::Circ2;
35 my ($loggedinuser, $sessioncookie, $sessionID) = checkauth($query);
39 my $headerbackgroundcolor='#99cc33';
40 my $circbackgroundcolor='#ffffcc'; # FIXME - Never used
41 my $circbackgroundcolor='white'; # FIXME - Never used
42 my $linecolor1='#ffffcc';
43 my $linecolor2='white';
44 my $backgroundimage="/images/background-mem.gif";
46 my $branches = getbranches();
47 my $printers = getprinters(\%env);
51 my $branch = getbranch($query, $branches);
52 my $printer = getprinter($query, $printers);
59 if ($query->param('setcookies')) {
60 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
61 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
64 $env{'branchcode'}=$branch;
65 $env{'printer'}=$printer;
66 $env{'queue'}=$printer;
68 my @datearr = localtime(time());
69 my $tday = localtime(time()); # FIXME - Unused
70 #warn "today: $tday \n";
71 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
72 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
78 # if there is a list of find borrowers....
79 my $findborrower = $query->param('findborrower');
81 my ($borrowers, $flags) = findborrower(\%env, $findborrower);
82 my @borrowers=@$borrowers;
83 if ($#borrowers == -1) {
84 $query->param('findborrower', '');
85 $message = "No borrower matched '$findborrower'";
86 } elsif ($#borrowers == 0) {
87 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
88 $query->param('barcode','');
90 $borrowerslist = \@borrowers;
94 my $borrowernumber = $query->param('borrnumber');
95 my $bornum = $query->param('borrnumber');
96 # check and see if we should print
97 my $print=$query->param('print');
98 my $barcode = $query->param('barcode');
99 if ($barcode eq '' && $print eq 'maybe'){
102 if ($print eq 'yes' && $borrowernumber ne ''){
103 printslip(\%env,$borrowernumber);
104 $query->param('borrnumber','');
110 # get the borrower information.....
113 if ($borrowernumber) {
114 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
117 # get the responses to any questions.....
119 foreach (sort $query->param) {
120 if ($_ =~ /response-(\d*)/) {
121 $responses{$1} = $query->param($_);
124 if (my $qnumber = $query->param('questionnumber')) {
125 $responses{$qnumber} = $query->param('answer');
130 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
132 my $year=$query->param('year');
133 my $month=$query->param('month');
134 my $day=$query->param('day');
136 # if the barcode is set
138 $barcode = cuecatbarcodedecode($barcode);
139 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
141 unless ($invalidduedate) {
142 $env{'datedue'}=$datedue;
143 my @time=localtime(time);
144 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
145 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
146 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
150 # reload the borrower info for the sake of reseting the flags.....
151 if ($borrowernumber) {
152 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
156 ##################################################################################
162 if ($rejected == -1) {
164 $rejectedtext = << "EOF";
165 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
166 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
167 <tr><td><font color=red size=5>$rejected</font></td></tr>
175 if ($borrowerslist) {
176 $selectborrower = <<"EOF";
177 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
178 <input type=hidden name=branch value=$branch>
179 <input type=hidden name=printer value=$printer>
180 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
181 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
182 <font color=black><b>Select a borrower</b></font></th></tr>\n
183 <tr><td align=center>
184 <select name=borrnumber size=7>
186 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
187 $selectborrower .= <<"EOF";
188 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
191 $selectborrower .= <<"EOF";
200 <table align="right"><tr><td>
201 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
202 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
204 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
205  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
207 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
208 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
209 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
210 <a href=selectbranchprinter.pl>Change Settings</a></td>
211 <input type=hidden name=branch value=$branch>
212 <input type=hidden name=printer value=$printer>
216 my $titlenoborrower = <<"EOF";
217 <table align="right"><tr><td>
219 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
220  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
222 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
223 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
224 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
225 <a href=selectbranchprinter.pl>Change Settings</a></td>
226 <input type=hidden name=branch value=$branch>
227 <input type=hidden name=printer value=$printer>
233 my $cardnumberinput = << "EOF";
234 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
235 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
236 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
237 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
238 <tr><td><input name=findborrower></td></tr>
239 <input type=hidden name=branch value=$branch>
240 <input type=hidden name=printer value=$printer>
245 my $responsesform = '';
246 foreach (keys %responses) {
247 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
251 my $stickyduedate=$query->param('stickyduedate');
252 $questionform = <<"EOF";
253 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
254 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
255 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
256 <tr><td><table border=0 cellpadding=10><tr><td>
257 Attempting to issue $iteminformation->{'title'}
258 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
261 </td></tr></table></td></tr>
262 <tr><td align=center>
266 <input type=hidden name=borrnumber value=$borrowernumber>
267 <input type=hidden name=barcode value=$barcode>
268 <input type=hidden name=questionnumber value=$questionnumber>
269 <input type=hidden name=day value=$day>
270 <input type=hidden name=month value=$month>
271 <input type=hidden name=year value=$year>
272 <input type=hidden name=stickyduedate value=$stickyduedate>
273 <input type=hidden name=branch value=$branch>
274 <input type=hidden name=printer value=$printer>
276 <input type=hidden name=answer value=Y>
277 <input type=submit value=Yes>
282 <input type=hidden name=borrnumber value=$borrowernumber>
283 <input type=hidden name=barcode value=$barcode>
284 <input type=hidden name=questionnumber value=$questionnumber>
285 <input type=hidden name=day value=$day>
286 <input type=hidden name=month value=$month>
287 <input type=hidden name=year value=$year>
288 <input type=hidden name=stickyduedate value=$stickyduedate>
289 <input type=hidden name=branch value=$branch>
290 <input type=hidden name=printer value=$printer>
292 <input type=hidden name=answer value=N>
293 <input type=submit value=No>
306 # Barcode entry box, with hidden inputs attached....
309 my $monthoptions = '';
310 my $yearoptions = '';
311 for (my $i=1; $i<32; $i++) {
313 if (($query->param('stickyduedate')) && ($day==$i)) {
314 $selected='selected';
316 $dayoptions.="<option value=$i $selected>$i";
318 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
320 if (($query->param('stickyduedate')) && ($month==$counter)) {
321 $selected='selected';
323 $monthoptions.="<option value=$counter $selected>$_";
326 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
328 if (($query->param('stickyduedate')) && ($year==$i)) {
329 $selected='selected';
331 $yearoptions.="<option value=$i $selected>$i";
334 ($query->param('stickyduedate')) && ($selected='checked');
337 my $barcodeentrytext = <<"EOF";
338 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
339 <table border=1 cellpadding=5>
341 <td align=center valign=top>
342 <table border=0 cellspacing=0 cellpadding=5>
343 <tr><th align=center background=$backgroundimage>
344 <font color=black><b>Enter Book Barcode</b></font></th></tr>
345 <tr><td align=center>
347 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
348 <tr><td colspan=3 align=center>
349 <table border=0 cellpadding=0 cellspacing=0>
351 <select name=day><option value=0>Day$dayoptions</select>
353 <select name=month><option value=0>Month$monthoptions</select>
355 <select name=year><option value=0>Year$yearoptions</select>
358 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
361 <input type=hidden name=borrnumber value=$borrowernumber>
362 <input type=hidden name=branch value=$branch>
363 <input type=hidden name=printer value=$printer>
364 <input type=hidden name=print value=maybe>
366 if ($flags->{'CHARGES'}){
367 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
369 my $amountold=$flags->{'CHARGES'}->{'message'};
370 my @temp=split(/\$/,$amountold);
372 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
373 $barcodeentrytext.=<<"EOF";
380 # collect the messages and put into message table....
383 $messagetable = << "EOF";
384 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
385 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
386 <tr><td> $message </td></tr></table>
392 # make the issued books table.....
398 my $issueslist = getissues($borrower);
399 foreach my $it (keys %$issueslist) {
400 my $issuedate = $issueslist->{$it}->{'timestamp'};
401 $issuedate = substr($issuedate, 0, 8);
402 if ($todaysdate == $issuedate) {
403 push @todaysissues, $issueslist->{$it};
405 push @previousissues, $issueslist->{$it};
410 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
411 my $dd = $book->{'date_due'};
412 my $datedue = $book->{'date_due'};
413 #convert to nz style dates
414 #this should be set with some kinda config variable
415 my @tempdate=split(/-/,$dd);
416 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
418 if ($datedue < $todaysdate) {
419 $dd="<font color=red>$dd</font>\n";
421 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
422 $todaysissues .=<< "EOF";
423 <tr><td bgcolor=$tcolor align=center>$dd</td>
424 <td bgcolor=$tcolor align=center>
425 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
426 <td bgcolor=$tcolor>$book->{'title'}</td>
427 <td bgcolor=$tcolor>$book->{'author'}</td>
428 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
431 # FIXME - For small and private libraries, it'd be nice if this
432 # table included a "Return" link next to each book, so that you
433 # don't have to remember the book's bar code and type it in on the
435 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
436 my $dd = $book->{'date_due'};
437 my $datedue = $book->{'date_due'};
438 #convert to nz style dates
439 #this should be set with some kinda config variable
440 my @tempdate=split(/-/,$dd);
441 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
443 if ($datedue < $todaysdate) {
444 $dd="<font color=red>$dd</font>\n";
446 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
447 $previssues .= << "EOF";
448 <tr><td bgcolor=$pcolor align=center>$dd</td>
449 <td bgcolor=$pcolor align=center>
450 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
451 <td bgcolor=$pcolor>$book->{'title'}</td>
452 <td bgcolor=$pcolor>$book->{'author'}</td>
453 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
458 my $issuedbookstable;
460 $issuedbookstable .= <<"EOF";
461 <table border=1 cellpadding=5 cellspacing=0 width=80%>
462 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
463 <b>Todays Issues</b></font></th></tr>
464 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
470 $issuedbookstable .= <<"EOF";
471 <table border=1 cellpadding=5 cellspacing=0 width=80%>
472 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
473 <b>Previous Issues</b></font></th></tr>
474 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
484 # actually print the page!
487 if ($branchcookie && $printercookie) {
488 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
490 print $query->header(-cookie=>[$sessioncookie]);
494 my @inp=startmenu('circulation');
495 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
497 for (my $i=0;$i<$count;$i++){
498 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
505 #print startmenu('circulation');
509 print $titlenoborrower;
524 if ($borrowerslist) {
525 print $selectborrower;
527 print $cardnumberinput;
534 my ($patrontable, $flaginfotable) = patrontable($borrower);
536 print $flaginfotable;
537 print $barcodeentrytext;
538 print "<p clear=all><br><br>";
539 print $issuedbookstable;
545 print endmenu('circulation');
549 ####################################################################
550 # Extra subroutines,,,
552 sub cuecatbarcodedecode {
555 my @fields = split(/\./,$barcode);
556 my @results = map(decode($_), @fields[1..$#fields]);
565 my ($year, $month, $day) = @_;
568 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
571 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
572 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
574 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
575 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
576 } elsif (($day > 29) && ($month == 2)) {
577 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
578 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
579 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
581 $date="$year-$month-$day";
585 return ($date, $invalidduedate);
591 my $flags = $borrower->{'flags'};
592 my $flaginfotable='';
596 foreach $flag (sort keys %$flags) {
597 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
598 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
599 if ($flags->{$flag}->{'noissues'}) {
600 if ($flag eq 'CHARGES') {
601 $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";
603 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
606 if ($flag eq 'CHARGES') {
607 $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";
608 } elsif ($flag eq 'WAITING') {
610 my $items=$flags->{$flag}->{'itemlist'};
611 foreach my $item (@$items) {
612 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
613 $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";
615 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
616 } elsif ($flag eq 'ODUES') {
617 my $items=$flags->{$flag}->{'itemlist'};
618 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
619 my $currentcolor=$color;
621 my $color=$currentcolor;
622 foreach my $item (@$items) {
623 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
624 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
625 $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";
628 $itemswaiting.="</table>\n";
629 if ($query->param('module') ne 'returns'){
630 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
632 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
635 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
639 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
640 $flaginfotext.="</table>";
641 my $patrontable= << "EOF";
643 <table border=1 cellpadding=5 cellspacing=0 align=right>
644 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
646 <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>
648 return($patrontable, $flaginfotext);
652 # FIXME - This clashes with &C4::Print::printslip
654 my ($env,$borrowernumber)=@_;
655 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
656 $env->{'todaysissues'}=1;
657 my ($borrowerissues) = currentissues($env, $borrower);
658 $env->{'nottodaysissues'}=1;
659 $env->{'todaysissues'}=0;
660 my ($borroweriss2)=currentissues($env, $borrower);
661 $env->{'nottodaysissues'}=0;
664 foreach (sort {$a <=> $b} keys %$borrowerissues) {
665 $issues[$i]=$borrowerissues->{$_};
666 my $dd=$issues[$i]->{'date_due'};
668 #convert to nz style dates
669 #this should be set with some kinda config variable
670 my @tempdate=split(/-/,$dd);
671 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
674 foreach (sort {$a <=> $b} keys %$borroweriss2) {
675 $issues[$i]=$borroweriss2->{$_};
676 my $dd=$issues[$i]->{'date_due'};
678 #convert to nz style dates
679 #this should be set with some kinda config variable
680 my @tempdate=split(/-/,$dd);
681 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
684 remoteprint($env,\@issues,$borrower);