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>
195 <img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers">
198 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
199 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
200 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
201 <a href=selectbranchprinter.pl>Change Settings</a></td>
202 <input type=hidden name=branch value=$branch>
203 <input type=hidden name=printer value=$printer>
207 my $titlenoborrower = <<"EOF";
208 <table align="right"><tr><td>
210 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
211  <a href=branchtransfers.pl>
212 <img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers">
215 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
216 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
217 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
218 <a href=selectbranchprinter.pl>Change Settings</a></td>
219 <input type=hidden name=branch value=$branch>
220 <input type=hidden name=printer value=$printer>
226 my $cardnumberinput = << "EOF";
227 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
228 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
229 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
230 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
231 <tr><td><input name=findborrower></td></tr>
232 <input type=hidden name=branch value=$branch>
233 <input type=hidden name=printer value=$printer>
238 my $responsesform = '';
239 foreach (keys %responses) {
240 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
244 my $stickyduedate=$query->param('stickyduedate');
245 $questionform = <<"EOF";
246 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
247 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
248 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
249 <tr><td><table border=0 cellpadding=10><tr><td>
250 Attempting to issue $iteminformation->{'title'}
251 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
254 </td></tr></table></td></tr>
255 <tr><td align=center>
259 <input type=hidden name=borrnumber value=$borrowernumber>
260 <input type=hidden name=barcode value=$barcode>
261 <input type=hidden name=questionnumber value=$questionnumber>
262 <input type=hidden name=day value=$day>
263 <input type=hidden name=month value=$month>
264 <input type=hidden name=year value=$year>
265 <input type=hidden name=stickyduedate value=$stickyduedate>
266 <input type=hidden name=branch value=$branch>
267 <input type=hidden name=printer value=$printer>
269 <input type=hidden name=answer value=Y>
270 <input type=submit value=Yes>
275 <input type=hidden name=borrnumber value=$borrowernumber>
276 <input type=hidden name=barcode value=$barcode>
277 <input type=hidden name=questionnumber value=$questionnumber>
278 <input type=hidden name=day value=$day>
279 <input type=hidden name=month value=$month>
280 <input type=hidden name=year value=$year>
281 <input type=hidden name=stickyduedate value=$stickyduedate>
282 <input type=hidden name=branch value=$branch>
283 <input type=hidden name=printer value=$printer>
285 <input type=hidden name=answer value=N>
286 <input type=submit value=No>
299 # Barcode entry box, with hidden inputs attached....
302 my $monthoptions = '';
303 my $yearoptions = '';
304 for (my $i=1; $i<32; $i++) {
306 if (($query->param('stickyduedate')) && ($day==$i)) {
307 $selected='selected';
309 $dayoptions.="<option value=$i $selected>$i";
311 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
313 if (($query->param('stickyduedate')) && ($month==$counter)) {
314 $selected='selected';
316 $monthoptions.="<option value=$counter $selected>$_";
319 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
321 if (($query->param('stickyduedate')) && ($year==$i)) {
322 $selected='selected';
324 $yearoptions.="<option value=$i $selected>$i";
327 ($query->param('stickyduedate')) && ($selected='checked');
330 my $barcodeentrytext = <<"EOF";
331 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
332 <table border=1 cellpadding=5>
334 <td align=center valign=top>
335 <table border=0 cellspacing=0 cellpadding=5>
336 <tr><th align=center background=$backgroundimage>
337 <font color=black><b>Enter Book Barcode</b></font></th></tr>
338 <tr><td align=center>
340 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
341 <tr><td colspan=3 align=center>
342 <table border=0 cellpadding=0 cellspacing=0>
344 <select name=day><option value=0>Day$dayoptions</select>
346 <select name=month><option value=0>Month$monthoptions</select>
348 <select name=year><option value=0>Year$yearoptions</select>
351 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
354 <input type=hidden name=borrnumber value=$borrowernumber>
355 <input type=hidden name=branch value=$branch>
356 <input type=hidden name=printer value=$printer>
357 <input type=hidden name=print value=maybe>
359 if ($flags->{'CHARGES'}){
360 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
362 my $amountold=$flags->{'CHARGES'}->{'message'};
363 my @temp=split(/\$/,$amountold);
365 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
366 $barcodeentrytext.=<<"EOF";
373 # collect the messages and put into message table....
376 $messagetable = << "EOF";
377 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
378 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
379 <tr><td> $message </td></tr></table>
385 # make the issued books table.....
391 my $issueslist = getissues($borrower);
392 foreach my $it (keys %$issueslist) {
393 my $issuedate = $issueslist->{$it}->{'timestamp'};
394 $issuedate = substr($issuedate, 0, 8);
395 if ($todaysdate == $issuedate) {
396 push @todaysissues, $issueslist->{$it};
398 push @previousissues, $issueslist->{$it};
403 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
404 my $dd = $book->{'date_due'};
405 my $datedue = $book->{'date_due'};
406 #convert to nz style dates
407 #this should be set with some kinda config variable
408 my @tempdate=split(/-/,$dd);
409 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
411 if ($datedue < $todaysdate) {
412 $dd="<font color=red>$dd</font>\n";
414 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
415 $todaysissues .=<< "EOF";
416 <tr><td bgcolor=$tcolor align=center>$dd</td>
417 <td bgcolor=$tcolor align=center>
418 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
419 <td bgcolor=$tcolor>$book->{'title'}</td>
420 <td bgcolor=$tcolor>$book->{'author'}</td>
421 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
424 foreach my $book (sort {$a->{'due_date'} <=> $b->{'due_date'}} @previousissues){
425 my $dd = $book->{'date_due'};
426 my $datedue = $book->{'date_due'};
427 #convert to nz style dates
428 #this should be set with some kinda config variable
429 my @tempdate=split(/-/,$dd);
430 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
432 if ($datedue < $todaysdate) {
433 $dd="<font color=red>$dd</font>\n";
435 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
436 $previssues .= << "EOF";
437 <tr><td bgcolor=$pcolor align=center>$dd</td>
438 <td bgcolor=$pcolor align=center>
439 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
440 <td bgcolor=$pcolor>$book->{'title'}</td>
441 <td bgcolor=$pcolor>$book->{'author'}</td>
442 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
447 my $issuedbookstable;
449 $issuedbookstable .= <<"EOF";
450 <table border=1 cellpadding=5 cellspacing=0 width=80%>
451 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
452 <b>Todays Issues</b></font></th></tr>
453 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
459 $issuedbookstable .= <<"EOF";
460 <table border=1 cellpadding=5 cellspacing=0 width=80%>
461 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
462 <b>Previous Issues</b></font></th></tr>
463 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
473 # actually print the page!
476 if ($branchcookie && $printercookie) {
477 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
479 print $query->header();
483 my @inp=startmenu('circulation');
484 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
486 for (my $i=0;$i<$count;$i++){
487 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
494 #print startmenu('circulation');
498 print $titlenoborrower;
513 if ($borrowerslist) {
514 print $selectborrower;
516 print $cardnumberinput;
523 my ($patrontable, $flaginfotable) = patrontable($borrower);
525 print $flaginfotable;
526 print $barcodeentrytext;
527 print "<p clear=all><br><br>";
528 print $issuedbookstable;
534 print endmenu('circulation');
538 ####################################################################
539 # Extra subroutines,,,
541 sub cuecatbarcodedecode {
544 my @fields = split(/\./,$barcode);
545 my @results = map(decode($_), @fields[1..$#fields]);
554 my ($year, $month, $day) = @_;
557 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
560 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
561 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
563 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
564 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
565 } elsif (($day > 29) && ($month == 2)) {
566 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
567 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
568 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
570 $date="$year-$month-$day";
574 return ($date, $invalidduedate);
580 my $flags = $borrower->{'flags'};
581 my $flaginfotable='';
585 foreach $flag (sort keys %$flags) {
586 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
587 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
588 if ($flags->{$flag}->{'noissues'}) {
589 if ($flag eq 'CHARGES') {
590 $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";
592 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
595 if ($flag eq 'CHARGES') {
596 $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";
597 } elsif ($flag eq 'WAITING') {
599 my $items=$flags->{$flag}->{'itemlist'};
600 foreach my $item (@$items) {
601 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
602 $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";
604 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
605 } elsif ($flag eq 'ODUES') {
606 my $items=$flags->{$flag}->{'itemlist'};
607 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
608 my $currentcolor=$color;
610 my $color=$currentcolor;
611 foreach my $item (@$items) {
612 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
613 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
614 $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";
617 $itemswaiting.="</table>\n";
618 if ($query->param('module') ne 'returns'){
619 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
621 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
624 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
628 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
629 $flaginfotext.="</table>";
630 my $patrontable= << "EOF";
632 <table border=1 cellpadding=5 cellspacing=0 align=right>
633 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
635 <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>
637 return($patrontable, $flaginfotext);
643 my ($env,$borrowernumber)=@_;
644 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
645 $env->{'todaysissues'}=1;
646 my ($borrowerissues) = currentissues($env, $borrower);
647 $env->{'nottodaysissues'}=1;
648 $env->{'todaysissues'}=0;
649 my ($borroweriss2)=currentissues($env, $borrower);
650 $env->{'nottodaysissues'}=0;
653 foreach (sort keys %$borrowerissues) {
654 $issues[$i]=$borrowerissues->{$_};
655 my $dd=$issues[$i]->{'date_due'};
656 #convert to nz style dates
657 #this should be set with some kinda config variable
658 my @tempdate=split(/-/,$dd);
659 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
662 foreach (sort keys %$borroweriss2) {
663 $issues[$i]=$borroweriss2->{$_};
664 my $dd=$issues[$i]->{'date_due'};
665 #convert to nz style dates
666 #this should be set with some kinda config variable
667 my @tempdate=split(/-/,$dd);
668 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
671 remoteprint($env,\@issues,$borrower);