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';
40 my $circbackgroundcolor='white';
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')) ;
60 if ($query->param('setcookies')) {
61 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
62 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
65 $env{'branchcode'}=$branch;
66 $env{'printer'}=$printer;
67 $env{'queue'}=$printer;
69 my @datearr = localtime(time());
70 my $tday = localtime(time());
71 warn "today: $tday \n";
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 currently issued books......
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');
129 # if the barcode is set
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');
138 $barcode = cuecatbarcodedecode($barcode);
139 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
142 unless ($invalidduedate) {
143 $env{'datedue'}=$datedue;
144 my @time=localtime(time);
145 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
146 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
147 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
154 ##################################################################################
160 if ($rejected == -1) {
162 $rejectedtext = << "EOF";
163 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
164 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
165 <tr><td><font color=red size=5>$rejected</font></td></tr>
173 if ($borrowerslist) {
174 $selectborrower = <<"EOF";
175 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
176 <input type=hidden name=branch value=$branch>
177 <input type=hidden name=printer value=$printer>
178 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
179 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
180 <font color=black><b>Select a borrower</b></font></th></tr>\n
181 <tr><td align=center>
182 <select name=borrnumber size=7>
184 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
185 $selectborrower .= <<"EOF";
186 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
189 $selectborrower .= <<"EOF";
198 <table align="right"><tr><td>
199 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
200 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
202 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
203  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
205 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
206 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
207 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
208 <a href=selectbranchprinter.pl>Change Settings</a></td>
209 <input type=hidden name=branch value=$branch>
210 <input type=hidden name=printer value=$printer>
214 my $titlenoborrower = <<"EOF";
215 <table align="right"><tr><td>
217 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
218  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
220 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
221 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
222 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
223 <a href=selectbranchprinter.pl>Change Settings</a></td>
224 <input type=hidden name=branch value=$branch>
225 <input type=hidden name=printer value=$printer>
231 my $cardnumberinput = << "EOF";
232 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
233 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
234 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
235 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
236 <tr><td><input name=findborrower></td></tr>
237 <input type=hidden name=branch value=$branch>
238 <input type=hidden name=printer value=$printer>
243 my $responsesform = '';
244 foreach (keys %responses) {
245 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
249 my $stickyduedate=$query->param('stickyduedate');
250 $questionform = <<"EOF";
251 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
252 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
253 <font color=black><b>Issuing Question</b></font></th></tr>
254 <tr><td><table border=0 cellpadding=10><tr><td>
255 Attempting to issue $iteminformation->{'title'}
256 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
259 </td></tr></table></td></tr>
260 <tr><td align=center>
264 <input type=hidden name=borrnumber value=$borrowernumber>
265 <input type=hidden name=barcode value=$barcode>
266 <input type=hidden name=questionnumber value=$questionnumber>
267 <input type=hidden name=day value=$day>
268 <input type=hidden name=month value=$month>
269 <input type=hidden name=year value=$year>
270 <input type=hidden name=stickyduedate value=$stickyduedate>
271 <input type=hidden name=branch value=$branch>
272 <input type=hidden name=printer value=$printer>
274 <input type=hidden name=answer value=Y>
275 <input type=submit value=Yes>
280 <input type=hidden name=borrnumber value=$borrowernumber>
281 <input type=hidden name=barcode value=$barcode>
282 <input type=hidden name=questionnumber value=$questionnumber>
283 <input type=hidden name=day value=$day>
284 <input type=hidden name=month value=$month>
285 <input type=hidden name=year value=$year>
286 <input type=hidden name=stickyduedate value=$stickyduedate>
287 <input type=hidden name=branch value=$branch>
288 <input type=hidden name=printer value=$printer>
290 <input type=hidden name=answer value=N>
291 <input type=submit value=No>
304 # Barcode entry box, with hidden inputs attached....
307 my $monthoptions = '';
308 my $yearoptions = '';
309 for (my $i=1; $i<32; $i++) {
311 if (($query->param('stickyduedate')) && ($day==$i)) {
312 $selected='selected';
314 $dayoptions.="<option value=$i $selected>$i";
316 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
318 if (($query->param('stickyduedate')) && ($month==$counter)) {
319 $selected='selected';
321 $monthoptions.="<option value=$counter $selected>$_";
324 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
326 if (($query->param('stickyduedate')) && ($year==$i)) {
327 $selected='selected';
329 $yearoptions.="<option value=$i $selected>$i";
332 ($query->param('stickyduedate')) && ($selected='checked');
335 my $barcodeentrytext = <<"EOF";
336 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
337 <table border=1 cellpadding=5>
339 <td align=center valign=top>
340 <table border=0 cellspacing=0 cellpadding=5>
341 <tr><th align=center background=$backgroundimage>
342 <font color=black><b>Enter Book Barcode</b></font></th></tr>
343 <tr><td align=center>
345 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
346 <tr><td colspan=3 align=center>
347 <table border=0 cellpadding=0 cellspacing=0>
349 <select name=day><option value=0>Day$dayoptions</select>
351 <select name=month><option value=0>Month$monthoptions</select>
353 <select name=year><option value=0>Year$yearoptions</select>
356 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
359 <input type=hidden name=borrnumber value=$borrowernumber>
360 <input type=hidden name=branch value=$branch>
361 <input type=hidden name=printer value=$printer>
362 <input type=hidden name=print value=maybe>
364 if ($flags->{'CHARGES'}){
365 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
367 my $amountold=$flags->{'CHARGES'}->{'message'};
368 my @temp=split(/\$/,$amountold);
370 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
371 $barcodeentrytext.=<<"EOF";
378 # collect the messages and put into message table....
381 $messagetable = << "EOF";
382 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
383 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
384 <tr><td> $message </td></tr></table>
391 # make the issued books table.....
397 my $issueslist = getissues($borrower);
398 foreach my $it (keys %$issueslist) {
399 my $issuedate = $issueslist->{$it}->{'timestamp'};
400 $issuedate = substr($issuedate, 0, 8);
401 if ($todaysdate == $issuedate) {
402 push @todaysissues, $issueslist->{$it};
404 push @previousissues, $issueslist->{$it};
409 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
410 my $dd = $book->{'date_due'};
411 my $datedue = $book->{'date_due'};
412 #convert to nz style dates
413 #this should be set with some kinda config variable
414 my @tempdate=split(/-/,$dd);
415 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
417 if ($datedue < $todaysdate) {
418 $dd="<font color=red>$dd</font>\n";
420 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
421 $todaysissues .=<< "EOF";
422 <tr><td bgcolor=$tcolor align=center>$dd</td>
423 <td bgcolor=$tcolor align=center>
424 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
425 <td bgcolor=$tcolor>$book->{'title'}</td>
426 <td bgcolor=$tcolor>$book->{'author'}</td>
427 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
430 foreach my $book (sort {$a->{'due_date'} <=> $b->{'due_date'}} @previousissues){
431 my $dd = $book->{'date_due'};
432 my $datedue = $book->{'date_due'};
433 #convert to nz style dates
434 #this should be set with some kinda config variable
435 my @tempdate=split(/-/,$dd);
436 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
438 if ($datedue < $todaysdate) {
439 $dd="<font color=red>$dd</font>\n";
441 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
442 $previssues .= << "EOF";
443 <tr><td bgcolor=$pcolor align=center>$dd</td>
444 <td bgcolor=$pcolor align=center>
445 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
446 <td bgcolor=$pcolor>$book->{'title'}</td>
447 <td bgcolor=$pcolor>$book->{'author'}</td>
448 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
453 my $issuedbookstable;
455 $issuedbookstable .= <<"EOF";
456 <table border=1 cellpadding=5 cellspacing=0 width=80%>
457 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
458 <b>Todays Issues</b></font></th></tr>
459 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
465 $issuedbookstable .= <<"EOF";
466 <table border=1 cellpadding=5 cellspacing=0 width=80%>
467 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
468 <b>Previous Issues</b></font></th></tr>
469 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
479 # actually print the page!
482 if ($branchcookie && $printercookie) {
483 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
485 print $query->header(-cookie=>[$sessioncookie]);
489 my @inp=startmenu('circulation');
490 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
492 for (my $i=0;$i<$count;$i++){
493 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
500 #print startmenu('circulation');
504 print $titlenoborrower;
519 if ($borrowerslist) {
520 print $selectborrower;
522 print $cardnumberinput;
529 my ($patrontable, $flaginfotable) = patrontable($borrower);
531 print $flaginfotable;
532 print $barcodeentrytext;
533 print "<p clear=all><br><br>";
534 print $issuedbookstable;
540 print endmenu('circulation');
544 ####################################################################
545 # Extra subroutines,,,
547 sub cuecatbarcodedecode {
550 my @fields = split(/\./,$barcode);
551 my @results = map(decode($_), @fields[1..$#fields]);
560 my ($year, $month, $day) = @_;
563 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
566 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
567 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
569 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
570 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
571 } elsif (($day > 29) && ($month == 2)) {
572 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
573 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
574 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
576 $date="$year-$month-$day";
580 return ($date, $invalidduedate);
586 my $flags = $borrower->{'flags'};
587 my $flaginfotable='';
591 foreach $flag (sort keys %$flags) {
592 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
593 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
594 if ($flags->{$flag}->{'noissues'}) {
595 if ($flag eq 'CHARGES') {
596 $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";
598 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
601 if ($flag eq 'CHARGES') {
602 $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";
603 } elsif ($flag eq 'WAITING') {
605 my $items=$flags->{$flag}->{'itemlist'};
606 foreach my $item (@$items) {
607 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
608 $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";
610 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
611 } elsif ($flag eq 'ODUES') {
612 my $items=$flags->{$flag}->{'itemlist'};
613 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
614 my $currentcolor=$color;
616 my $color=$currentcolor;
617 foreach my $item (@$items) {
618 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
619 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
620 $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";
623 $itemswaiting.="</table>\n";
624 if ($query->param('module') ne 'returns'){
625 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
627 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
630 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
634 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
635 $flaginfotext.="</table>";
636 my $patrontable= << "EOF";
638 <table border=1 cellpadding=5 cellspacing=0 align=right>
639 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
641 <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>
643 return($patrontable, $flaginfotext);
649 my ($env,$borrowernumber)=@_;
650 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
651 $env->{'todaysissues'}=1;
652 my ($borrowerissues) = currentissues($env, $borrower);
653 $env->{'nottodaysissues'}=1;
654 $env->{'todaysissues'}=0;
655 my ($borroweriss2)=currentissues($env, $borrower);
656 $env->{'nottodaysissues'}=0;
659 foreach (sort keys %$borrowerissues) {
660 $issues[$i]=$borrowerissues->{$_};
661 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 keys %$borroweriss2) {
669 $issues[$i]=$borroweriss2->{$_};
670 my $dd=$issues[$i]->{'date_due'};
671 #convert to nz style dates
672 #this should be set with some kinda config variable
673 my @tempdate=split(/-/,$dd);
674 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
677 remoteprint($env,\@issues,$borrower);