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 foreach my $book (sort {$a->{'due_date'} <=> $b->{'due_date'}} @previousissues){
426 my $dd = $book->{'date_due'};
427 my $datedue = $book->{'date_due'};
428 #convert to nz style dates
429 #this should be set with some kinda config variable
430 my @tempdate=split(/-/,$dd);
431 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
433 if ($datedue < $todaysdate) {
434 $dd="<font color=red>$dd</font>\n";
436 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
437 $previssues .= << "EOF";
438 <tr><td bgcolor=$pcolor align=center>$dd</td>
439 <td bgcolor=$pcolor align=center>
440 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
441 <td bgcolor=$pcolor>$book->{'title'}</td>
442 <td bgcolor=$pcolor>$book->{'author'}</td>
443 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
448 my $issuedbookstable;
450 $issuedbookstable .= <<"EOF";
451 <table border=1 cellpadding=5 cellspacing=0 width=80%>
452 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
453 <b>Todays Issues</b></font></th></tr>
454 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
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>Previous 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>
474 # actually print the page!
477 if ($branchcookie && $printercookie) {
478 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
480 print $query->header();
484 my @inp=startmenu('circulation');
485 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
487 for (my $i=0;$i<$count;$i++){
488 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
495 #print startmenu('circulation');
499 print $titlenoborrower;
514 if ($borrowerslist) {
515 print $selectborrower;
517 print $cardnumberinput;
524 my ($patrontable, $flaginfotable) = patrontable($borrower);
526 print $flaginfotable;
527 print $barcodeentrytext;
528 print "<p clear=all><br><br>";
529 print $issuedbookstable;
535 print endmenu('circulation');
539 ####################################################################
540 # Extra subroutines,,,
542 sub cuecatbarcodedecode {
545 my @fields = split(/\./,$barcode);
546 my @results = map(decode($_), @fields[1..$#fields]);
555 my ($year, $month, $day) = @_;
558 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
561 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
562 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
564 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
565 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
566 } elsif (($day > 29) && ($month == 2)) {
567 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
568 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
569 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
571 $date="$year-$month-$day";
575 return ($date, $invalidduedate);
581 my $flags = $borrower->{'flags'};
582 my $flaginfotable='';
586 foreach $flag (sort keys %$flags) {
587 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
588 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
589 if ($flags->{$flag}->{'noissues'}) {
590 if ($flag eq 'CHARGES') {
591 $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";
593 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
596 if ($flag eq 'CHARGES') {
597 $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";
598 } elsif ($flag eq 'WAITING') {
600 my $items=$flags->{$flag}->{'itemlist'};
601 foreach my $item (@$items) {
602 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
603 $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";
605 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
606 } elsif ($flag eq 'ODUES') {
607 my $items=$flags->{$flag}->{'itemlist'};
608 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
609 my $currentcolor=$color;
611 my $color=$currentcolor;
612 foreach my $item (@$items) {
613 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
614 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
615 $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";
618 $itemswaiting.="</table>\n";
619 if ($query->param('module') ne 'returns'){
620 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
622 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
625 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
629 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
630 $flaginfotext.="</table>";
631 my $patrontable= << "EOF";
633 <table border=1 cellpadding=5 cellspacing=0 align=right>
634 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
636 <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>
638 return($patrontable, $flaginfotext);
644 my ($env,$borrowernumber)=@_;
645 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
646 $env->{'todaysissues'}=1;
647 my ($borrowerissues) = currentissues($env, $borrower);
648 $env->{'nottodaysissues'}=1;
649 $env->{'todaysissues'}=0;
650 my ($borroweriss2)=currentissues($env, $borrower);
651 $env->{'nottodaysissues'}=0;
654 foreach (sort keys %$borrowerissues) {
655 $issues[$i]=$borrowerissues->{$_};
656 my $dd=$issues[$i]->{'date_due'};
657 #convert to nz style dates
658 #this should be set with some kinda config variable
659 my @tempdate=split(/-/,$dd);
660 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
663 foreach (sort keys %$borroweriss2) {
664 $issues[$i]=$borroweriss2->{$_};
665 my $dd=$issues[$i]->{'date_due'};
666 #convert to nz style dates
667 #this should be set with some kinda config variable
668 my @tempdate=split(/-/,$dd);
669 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
672 remoteprint($env,\@issues,$borrower);