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;
34 my ($loggedinuser, $sessioncookie, $sessionID) = checkauth($query);
38 my $headerbackgroundcolor='#99cc33';
39 my $circbackgroundcolor='#ffffcc'; # FIXME - Never used
40 my $circbackgroundcolor='white'; # FIXME - Never used
41 my $linecolor1='#ffffcc';
42 my $linecolor2='white';
43 my $backgroundimage="/images/background-mem.gif";
45 my $branches = getbranches();
46 my $printers = getprinters(\%env);
50 my $branch = $query->param("branch");
51 my $printer = $query->param("printer");
53 ($branch) || ($branch=$query->cookie('branch')) ;
54 ($printer) || ($printer=$query->cookie('printer')) ;
56 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
57 ($printers->{$printer}) || ($printer=(keys %$printers)[0]);
65 if ($query->param('setcookies')) {
66 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
67 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
70 $env{'branchcode'}=$branch;
71 $env{'printer'}=$printer;
72 $env{'queue'}=$printer;
74 my @datearr = localtime(time());
75 my $tday = localtime(time()); # FIXME - Unused
76 #warn "today: $tday \n";
77 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
78 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
84 # if there is a list of find borrowers....
85 my $findborrower = $query->param('findborrower');
87 my ($borrowers, $flags) = findborrower(\%env, $findborrower);
88 my @borrowers=@$borrowers;
89 if ($#borrowers == -1) {
90 $query->param('findborrower', '');
91 $message = "No borrower matched '$findborrower'";
92 } elsif ($#borrowers == 0) {
93 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
94 $query->param('barcode','');
96 $borrowerslist = \@borrowers;
100 my $borrowernumber = $query->param('borrnumber');
101 my $bornum = $query->param('borrnumber');
102 # check and see if we should print
103 my $print=$query->param('print');
104 my $barcode = $query->param('barcode');
105 if ($barcode eq '' && $print eq 'maybe'){
108 if ($print eq 'yes' && $borrowernumber ne ''){
109 printslip(\%env,$borrowernumber);
110 $query->param('borrnumber','');
116 # get the borrower information.....
119 if ($borrowernumber) {
120 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
123 # get the responses to any questions.....
125 foreach (sort $query->param) {
126 if ($_ =~ /response-(\d*)/) {
127 $responses{$1} = $query->param($_);
130 if (my $qnumber = $query->param('questionnumber')) {
131 $responses{$qnumber} = $query->param('answer');
136 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
138 my $year=$query->param('year');
139 my $month=$query->param('month');
140 my $day=$query->param('day');
142 # if the barcode is set
144 $barcode = cuecatbarcodedecode($barcode);
145 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
147 unless ($invalidduedate) {
148 $env{'datedue'}=$datedue;
149 my @time=localtime(time);
150 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
151 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
152 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
156 # reload the borrower info for the sake of reseting the flags.....
157 if ($borrowernumber) {
158 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
162 ##################################################################################
168 if ($rejected == -1) {
170 $rejectedtext = << "EOF";
171 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
172 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
173 <tr><td><font color=red size=5>$rejected</font></td></tr>
181 if ($borrowerslist) {
182 $selectborrower = <<"EOF";
183 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
184 <input type=hidden name=branch value=$branch>
185 <input type=hidden name=printer value=$printer>
186 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
187 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
188 <font color=black><b>Select a borrower</b></font></th></tr>\n
189 <tr><td align=center>
190 <select name=borrnumber size=7>
192 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
193 $selectborrower .= <<"EOF";
194 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
197 $selectborrower .= <<"EOF";
206 <table align="right"><tr><td>
207 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
208 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
210 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
211  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
213 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
214 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
215 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
216 <a href=selectbranchprinter.pl>Change Settings</a></td>
217 <input type=hidden name=branch value=$branch>
218 <input type=hidden name=printer value=$printer>
222 my $titlenoborrower = <<"EOF";
223 <table align="right"><tr><td>
225 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
226  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
228 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
229 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
230 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
231 <a href=selectbranchprinter.pl>Change Settings</a></td>
232 <input type=hidden name=branch value=$branch>
233 <input type=hidden name=printer value=$printer>
239 my $cardnumberinput = << "EOF";
240 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
241 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
242 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
243 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
244 <tr><td><input name=findborrower></td></tr>
245 <input type=hidden name=branch value=$branch>
246 <input type=hidden name=printer value=$printer>
251 my $responsesform = '';
252 foreach (keys %responses) {
253 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
257 my $stickyduedate=$query->param('stickyduedate');
258 $questionform = <<"EOF";
259 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
260 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
261 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
262 <tr><td><table border=0 cellpadding=10><tr><td>
263 Attempting to issue $iteminformation->{'title'}
264 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
267 </td></tr></table></td></tr>
268 <tr><td align=center>
272 <input type=hidden name=borrnumber value=$borrowernumber>
273 <input type=hidden name=barcode value=$barcode>
274 <input type=hidden name=questionnumber value=$questionnumber>
275 <input type=hidden name=day value=$day>
276 <input type=hidden name=month value=$month>
277 <input type=hidden name=year value=$year>
278 <input type=hidden name=stickyduedate value=$stickyduedate>
279 <input type=hidden name=branch value=$branch>
280 <input type=hidden name=printer value=$printer>
282 <input type=hidden name=answer value=Y>
283 <input type=submit value=Yes>
288 <input type=hidden name=borrnumber value=$borrowernumber>
289 <input type=hidden name=barcode value=$barcode>
290 <input type=hidden name=questionnumber value=$questionnumber>
291 <input type=hidden name=day value=$day>
292 <input type=hidden name=month value=$month>
293 <input type=hidden name=year value=$year>
294 <input type=hidden name=stickyduedate value=$stickyduedate>
295 <input type=hidden name=branch value=$branch>
296 <input type=hidden name=printer value=$printer>
298 <input type=hidden name=answer value=N>
299 <input type=submit value=No>
312 # Barcode entry box, with hidden inputs attached....
315 my $monthoptions = '';
316 my $yearoptions = '';
317 for (my $i=1; $i<32; $i++) {
319 if (($query->param('stickyduedate')) && ($day==$i)) {
320 $selected='selected';
322 $dayoptions.="<option value=$i $selected>$i";
324 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
326 if (($query->param('stickyduedate')) && ($month==$counter)) {
327 $selected='selected';
329 $monthoptions.="<option value=$counter $selected>$_";
332 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
334 if (($query->param('stickyduedate')) && ($year==$i)) {
335 $selected='selected';
337 $yearoptions.="<option value=$i $selected>$i";
340 ($query->param('stickyduedate')) && ($selected='checked');
343 my $barcodeentrytext = <<"EOF";
344 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
345 <table border=1 cellpadding=5>
347 <td align=center valign=top>
348 <table border=0 cellspacing=0 cellpadding=5>
349 <tr><th align=center background=$backgroundimage>
350 <font color=black><b>Enter Book Barcode</b></font></th></tr>
351 <tr><td align=center>
353 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
354 <tr><td colspan=3 align=center>
355 <table border=0 cellpadding=0 cellspacing=0>
357 <select name=day><option value=0>Day$dayoptions</select>
359 <select name=month><option value=0>Month$monthoptions</select>
361 <select name=year><option value=0>Year$yearoptions</select>
364 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
367 <input type=hidden name=borrnumber value=$borrowernumber>
368 <input type=hidden name=branch value=$branch>
369 <input type=hidden name=printer value=$printer>
370 <input type=hidden name=print value=maybe>
372 if ($flags->{'CHARGES'}){
373 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
375 my $amountold=$flags->{'CHARGES'}->{'message'};
376 my @temp=split(/\$/,$amountold);
378 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
379 $barcodeentrytext.=<<"EOF";
386 # collect the messages and put into message table....
389 $messagetable = << "EOF";
390 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
391 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
392 <tr><td> $message </td></tr></table>
398 # make the issued books table.....
404 my $issueslist = getissues($borrower);
405 foreach my $it (keys %$issueslist) {
406 my $issuedate = $issueslist->{$it}->{'timestamp'};
407 $issuedate = substr($issuedate, 0, 8);
408 if ($todaysdate == $issuedate) {
409 push @todaysissues, $issueslist->{$it};
411 push @previousissues, $issueslist->{$it};
416 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
417 my $dd = $book->{'date_due'};
418 my $datedue = $book->{'date_due'};
419 #convert to nz style dates
420 #this should be set with some kinda config variable
421 my @tempdate=split(/-/,$dd);
422 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
424 if ($datedue < $todaysdate) {
425 $dd="<font color=red>$dd</font>\n";
427 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
428 $todaysissues .=<< "EOF";
429 <tr><td bgcolor=$tcolor align=center>$dd</td>
430 <td bgcolor=$tcolor align=center>
431 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
432 <td bgcolor=$tcolor>$book->{'title'}</td>
433 <td bgcolor=$tcolor>$book->{'author'}</td>
434 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
437 # FIXME - For small and private libraries, it'd be nice if this
438 # table included a "Return" link next to each book, so that you
439 # don't have to remember the book's bar code and type it in on the
441 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
442 my $dd = $book->{'date_due'};
443 my $datedue = $book->{'date_due'};
444 #convert to nz style dates
445 #this should be set with some kinda config variable
446 my @tempdate=split(/-/,$dd);
447 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
449 if ($datedue < $todaysdate) {
450 $dd="<font color=red>$dd</font>\n";
452 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
453 $previssues .= << "EOF";
454 <tr><td bgcolor=$pcolor align=center>$dd</td>
455 <td bgcolor=$pcolor align=center>
456 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
457 <td bgcolor=$pcolor>$book->{'title'}</td>
458 <td bgcolor=$pcolor>$book->{'author'}</td>
459 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
464 my $issuedbookstable;
466 $issuedbookstable .= <<"EOF";
467 <table border=1 cellpadding=5 cellspacing=0 width=80%>
468 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
469 <b>Todays Issues</b></font></th></tr>
470 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
476 $issuedbookstable .= <<"EOF";
477 <table border=1 cellpadding=5 cellspacing=0 width=80%>
478 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
479 <b>Previous Issues</b></font></th></tr>
480 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
490 # actually print the page!
493 if ($branchcookie && $printercookie) {
494 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
496 print $query->header(-cookie=>[$sessioncookie]);
500 my @inp=startmenu('circulation');
501 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
503 for (my $i=0;$i<$count;$i++){
504 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
511 #print startmenu('circulation');
515 print $titlenoborrower;
530 if ($borrowerslist) {
531 print $selectborrower;
533 print $cardnumberinput;
540 my ($patrontable, $flaginfotable) = patrontable($borrower);
542 print $flaginfotable;
543 print $barcodeentrytext;
544 print "<p clear=all><br><br>";
545 print $issuedbookstable;
551 print endmenu('circulation');
555 ####################################################################
556 # Extra subroutines,,,
558 sub cuecatbarcodedecode {
561 my @fields = split(/\./,$barcode);
562 my @results = map(decode($_), @fields[1..$#fields]);
571 my ($year, $month, $day) = @_;
574 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
577 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
578 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
580 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
581 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
582 } elsif (($day > 29) && ($month == 2)) {
583 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
584 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
585 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
587 $date="$year-$month-$day";
591 return ($date, $invalidduedate);
597 my $flags = $borrower->{'flags'};
598 my $flaginfotable='';
602 foreach $flag (sort keys %$flags) {
603 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
604 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
605 if ($flags->{$flag}->{'noissues'}) {
606 if ($flag eq 'CHARGES') {
607 $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";
609 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
612 if ($flag eq 'CHARGES') {
613 $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";
614 } elsif ($flag eq 'WAITING') {
616 my $items=$flags->{$flag}->{'itemlist'};
617 foreach my $item (@$items) {
618 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
619 $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";
621 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
622 } elsif ($flag eq 'ODUES') {
623 my $items=$flags->{$flag}->{'itemlist'};
624 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
625 my $currentcolor=$color;
627 my $color=$currentcolor;
628 foreach my $item (@$items) {
629 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
630 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
631 $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";
634 $itemswaiting.="</table>\n";
635 if ($query->param('module') ne 'returns'){
636 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
638 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
641 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
645 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
646 $flaginfotext.="</table>";
647 my $patrontable= << "EOF";
649 <table border=1 cellpadding=5 cellspacing=0 align=right>
650 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
652 <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>
654 return($patrontable, $flaginfotext);
658 # FIXME - This clashes with &C4::Print::printslip
660 my ($env,$borrowernumber)=@_;
661 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
662 $env->{'todaysissues'}=1;
663 my ($borrowerissues) = currentissues($env, $borrower);
664 $env->{'nottodaysissues'}=1;
665 $env->{'todaysissues'}=0;
666 my ($borroweriss2)=currentissues($env, $borrower);
667 $env->{'nottodaysissues'}=0;
670 foreach (sort {$a <=> $b} keys %$borrowerissues) {
671 $issues[$i]=$borrowerissues->{$_};
672 my $dd=$issues[$i]->{'date_due'};
674 #convert to nz style dates
675 #this should be set with some kinda config variable
676 my @tempdate=split(/-/,$dd);
677 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
680 foreach (sort {$a <=> $b} keys %$borroweriss2) {
681 $issues[$i]=$borroweriss2->{$_};
682 my $dd=$issues[$i]->{'date_due'};
684 #convert to nz style dates
685 #this should be set with some kinda config variable
686 my @tempdate=split(/-/,$dd);
687 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
690 remoteprint($env,\@issues,$borrower);