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 currently issued books......
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');
123 # if the barcode is set
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');
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);
145 ##################################################################################
151 if ($rejected == -1) {
153 $rejectedtext = << "EOF";
154 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
155 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
156 <tr><td><font color=red size=5>$rejected</font></td></tr>
164 if ($borrowerslist) {
165 $selectborrower = <<"EOF";
166 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
167 <input type=hidden name=branch value=$branch>
168 <input type=hidden name=printer value=$printer>
169 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
170 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
171 <font color=black><b>Select a borrower</b></font></th></tr>\n
172 <tr><td align=center>
173 <select name=borrnumber size=7>
175 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
176 $selectborrower .= <<"EOF";
177 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
180 $selectborrower .= <<"EOF";
189 <table align="right"><tr><td>
190 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
191 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
193 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
194  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
196 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
197 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
198 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
199 <a href=selectbranchprinter.pl>Change Settings</a></td>
200 <input type=hidden name=branch value=$branch>
201 <input type=hidden name=printer value=$printer>
205 my $titlenoborrower = <<"EOF";
206 <table align="right"><tr><td>
208 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
209  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
211 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
212 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
213 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
214 <a href=selectbranchprinter.pl>Change Settings</a></td>
215 <input type=hidden name=branch value=$branch>
216 <input type=hidden name=printer value=$printer>
222 my $cardnumberinput = << "EOF";
223 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
224 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
225 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
226 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
227 <tr><td><input name=findborrower></td></tr>
228 <input type=hidden name=branch value=$branch>
229 <input type=hidden name=printer value=$printer>
234 my $responsesform = '';
235 foreach (keys %responses) {
236 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
240 my $stickyduedate=$query->param('stickyduedate');
241 $questionform = <<"EOF";
242 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
243 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
244 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
245 <tr><td><table border=0 cellpadding=10><tr><td>
246 Attempting to issue $iteminformation->{'title'}
247 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
250 </td></tr></table></td></tr>
251 <tr><td align=center>
255 <input type=hidden name=borrnumber value=$borrowernumber>
256 <input type=hidden name=barcode value=$barcode>
257 <input type=hidden name=questionnumber value=$questionnumber>
258 <input type=hidden name=day value=$day>
259 <input type=hidden name=month value=$month>
260 <input type=hidden name=year value=$year>
261 <input type=hidden name=stickyduedate value=$stickyduedate>
262 <input type=hidden name=branch value=$branch>
263 <input type=hidden name=printer value=$printer>
265 <input type=hidden name=answer value=Y>
266 <input type=submit value=Yes>
271 <input type=hidden name=borrnumber value=$borrowernumber>
272 <input type=hidden name=barcode value=$barcode>
273 <input type=hidden name=questionnumber value=$questionnumber>
274 <input type=hidden name=day value=$day>
275 <input type=hidden name=month value=$month>
276 <input type=hidden name=year value=$year>
277 <input type=hidden name=stickyduedate value=$stickyduedate>
278 <input type=hidden name=branch value=$branch>
279 <input type=hidden name=printer value=$printer>
281 <input type=hidden name=answer value=N>
282 <input type=submit value=No>
295 # Barcode entry box, with hidden inputs attached....
298 my $monthoptions = '';
299 my $yearoptions = '';
300 for (my $i=1; $i<32; $i++) {
302 if (($query->param('stickyduedate')) && ($day==$i)) {
303 $selected='selected';
305 $dayoptions.="<option value=$i $selected>$i";
307 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
309 if (($query->param('stickyduedate')) && ($month==$counter)) {
310 $selected='selected';
312 $monthoptions.="<option value=$counter $selected>$_";
315 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
317 if (($query->param('stickyduedate')) && ($year==$i)) {
318 $selected='selected';
320 $yearoptions.="<option value=$i $selected>$i";
323 ($query->param('stickyduedate')) && ($selected='checked');
326 my $barcodeentrytext = <<"EOF";
327 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
328 <table border=1 cellpadding=5>
330 <td align=center valign=top>
331 <table border=0 cellspacing=0 cellpadding=5>
332 <tr><th align=center background=$backgroundimage>
333 <font color=black><b>Enter Book Barcode</b></font></th></tr>
334 <tr><td align=center>
336 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
337 <tr><td colspan=3 align=center>
338 <table border=0 cellpadding=0 cellspacing=0>
340 <select name=day><option value=0>Day$dayoptions</select>
342 <select name=month><option value=0>Month$monthoptions</select>
344 <select name=year><option value=0>Year$yearoptions</select>
347 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
350 <input type=hidden name=borrnumber value=$borrowernumber>
351 <input type=hidden name=branch value=$branch>
352 <input type=hidden name=printer value=$printer>
353 <input type=hidden name=print value=maybe>
355 if ($flags->{'CHARGES'}){
356 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
358 my $amountold=$flags->{'CHARGES'}->{'message'};
359 my @temp=split(/\$/,$amountold);
361 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
362 $barcodeentrytext.=<<"EOF";
369 # collect the messages and put into message table....
372 $messagetable = << "EOF";
373 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
374 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
375 <tr><td> $message </td></tr></table>
381 # make the issued books table.....
387 my $issueslist = getissues($borrower);
388 foreach my $it (keys %$issueslist) {
389 my $issuedate = $issueslist->{$it}->{'timestamp'};
390 $issuedate = substr($issuedate, 0, 8);
391 if ($todaysdate == $issuedate) {
392 push @todaysissues, $issueslist->{$it};
394 push @previousissues, $issueslist->{$it};
399 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
400 my $dd = $book->{'date_due'};
401 my $datedue = $book->{'date_due'};
402 #convert to nz style dates
403 #this should be set with some kinda config variable
404 my @tempdate=split(/-/,$dd);
405 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
407 if ($datedue < $todaysdate) {
408 $dd="<font color=red>$dd</font>\n";
410 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
411 $todaysissues .=<< "EOF";
412 <tr><td bgcolor=$tcolor align=center>$dd</td>
413 <td bgcolor=$tcolor align=center>
414 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
415 <td bgcolor=$tcolor>$book->{'title'}</td>
416 <td bgcolor=$tcolor>$book->{'author'}</td>
417 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
420 foreach my $book (sort {$a->{'due_date'} <=> $b->{'due_date'}} @previousissues){
421 my $dd = $book->{'date_due'};
422 my $datedue = $book->{'date_due'};
423 #convert to nz style dates
424 #this should be set with some kinda config variable
425 my @tempdate=split(/-/,$dd);
426 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
428 if ($datedue < $todaysdate) {
429 $dd="<font color=red>$dd</font>\n";
431 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
432 $previssues .= << "EOF";
433 <tr><td bgcolor=$pcolor align=center>$dd</td>
434 <td bgcolor=$pcolor align=center>
435 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
436 <td bgcolor=$pcolor>$book->{'title'}</td>
437 <td bgcolor=$pcolor>$book->{'author'}</td>
438 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
443 my $issuedbookstable;
445 $issuedbookstable .= <<"EOF";
446 <table border=1 cellpadding=5 cellspacing=0 width=80%>
447 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
448 <b>Todays Issues</b></font></th></tr>
449 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
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>Previous 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>
469 # actually print the page!
472 if ($branchcookie && $printercookie) {
473 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
475 print $query->header();
479 my @inp=startmenu('circulation');
480 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
482 for (my $i=0;$i<$count;$i++){
483 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
490 #print startmenu('circulation');
494 print $titlenoborrower;
509 if ($borrowerslist) {
510 print $selectborrower;
512 print $cardnumberinput;
519 my ($patrontable, $flaginfotable) = patrontable($borrower);
521 print $flaginfotable;
522 print $barcodeentrytext;
523 print "<p clear=all><br><br>";
524 print $issuedbookstable;
530 print endmenu('circulation');
534 ####################################################################
535 # Extra subroutines,,,
537 sub cuecatbarcodedecode {
540 my @fields = split(/\./,$barcode);
541 my @results = map(decode($_), @fields[1..$#fields]);
550 my ($year, $month, $day) = @_;
553 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
556 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
557 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
559 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
560 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
561 } elsif (($day > 29) && ($month == 2)) {
562 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
563 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
564 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
566 $date="$year-$month-$day";
570 return ($date, $invalidduedate);
576 my $flags = $borrower->{'flags'};
577 my $flaginfotable='';
581 foreach $flag (sort keys %$flags) {
582 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
583 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
584 if ($flags->{$flag}->{'noissues'}) {
585 if ($flag eq 'CHARGES') {
586 $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";
588 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
591 if ($flag eq 'CHARGES') {
592 $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";
593 } elsif ($flag eq 'WAITING') {
595 my $items=$flags->{$flag}->{'itemlist'};
596 foreach my $item (@$items) {
597 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
598 $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";
600 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
601 } elsif ($flag eq 'ODUES') {
602 my $items=$flags->{$flag}->{'itemlist'};
603 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
604 my $currentcolor=$color;
606 my $color=$currentcolor;
607 foreach my $item (@$items) {
608 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
609 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
610 $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";
613 $itemswaiting.="</table>\n";
614 if ($query->param('module') ne 'returns'){
615 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
617 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
620 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
624 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
625 $flaginfotext.="</table>";
626 my $patrontable= << "EOF";
628 <table border=1 cellpadding=5 cellspacing=0 align=right>
629 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
631 <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>
633 return($patrontable, $flaginfotext);
639 my ($env,$borrowernumber)=@_;
640 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
641 $env->{'todaysissues'}=1;
642 my ($borrowerissues) = currentissues($env, $borrower);
643 $env->{'nottodaysissues'}=1;
644 $env->{'todaysissues'}=0;
645 my ($borroweriss2)=currentissues($env, $borrower);
646 $env->{'nottodaysissues'}=0;
649 foreach (sort keys %$borrowerissues) {
650 $issues[$i]=$borrowerissues->{$_};
651 my $dd=$issues[$i]->{'date_due'};
652 #convert to nz style dates
653 #this should be set with some kinda config variable
654 my @tempdate=split(/-/,$dd);
655 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
658 foreach (sort keys %$borroweriss2) {
659 $issues[$i]=$borroweriss2->{$_};
660 my $dd=$issues[$i]->{'date_due'};
661 #convert to nz style dates
662 #this should be set with some kinda config variable
663 my @tempdate=split(/-/,$dd);
664 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
667 remoteprint($env,\@issues,$borrower);