2 # Please use 8-character tabs for this file (indents are every 4 characters)
4 #written 8/5/2002 by Finlay
5 #script to execute issuing of books
8 # Copyright 2000-2002 Katipo Communications
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Circulation::Circ2;
33 use C4::Interface::CGI::Output;
36 my ($loggedinuser, $sessioncookie, $sessionID) = checkauth
37 ($query, 0, { circulate => 1 });
39 #my ($template, $loggedinuser, $sessioncookie) = get_template_and_user
41 # template_name => 'circ/circulation.tmpl',
44 # authnotrequired => 0,
45 # flagsrequired => { circulate => 1 },
50 my $headerbackgroundcolor='#99cc33';
51 my $linecolor1='#ffffcc';
52 my $linecolor2='white';
53 my $backgroundimage="/images/background-mem.gif";
55 my $branches = getbranches();
56 my $printers = getprinters(\%env);
58 my $branch = getbranch($query, $branches);
59 my $printer = getprinter($query, $printers);
66 if ($query->param('setcookies')) {
67 $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
68 $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
71 $env{'branchcode'}=$branch;
72 $env{'printer'}=$printer;
73 $env{'queue'}=$printer;
75 my @datearr = localtime(time());
76 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
77 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
83 # if there is a list of find borrowers....
84 my $findborrower = $query->param('findborrower');
86 my ($borrowers, $flags) = findborrower(\%env, $findborrower);
87 my @borrowers=@$borrowers;
88 if ($#borrowers == -1) {
89 $query->param('findborrower', '');
90 $message = "No borrower matched '$findborrower'";
91 } elsif ($#borrowers == 0) {
92 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
93 $query->param('barcode','');
95 $borrowerslist = \@borrowers;
99 my $borrowernumber = $query->param('borrnumber');
100 my $bornum = $query->param('borrnumber');
101 # check and see if we should print
102 my $print=$query->param('print');
103 my $barcode = $query->param('barcode');
104 if ($barcode eq '' && $print eq 'maybe'){
107 if ($print eq 'yes' && $borrowernumber ne ''){
108 printslip(\%env,$borrowernumber);
109 $query->param('borrnumber','');
115 # get the borrower information.....
118 if ($borrowernumber) {
119 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
122 # get the responses to any questions.....
124 foreach (sort $query->param) {
125 if ($_ =~ /response-(\d*)/) {
126 $responses{$1} = $query->param($_);
129 if (my $qnumber = $query->param('questionnumber')) {
130 $responses{$qnumber} = $query->param('answer');
135 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
137 my $year=$query->param('year');
138 my $month=$query->param('month');
139 my $day=$query->param('day');
141 # if the barcode is set
143 $barcode = cuecatbarcodedecode($barcode);
144 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
146 unless ($invalidduedate) {
147 $env{'datedue'}=$datedue;
148 my @time=localtime(time);
149 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
150 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
151 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
155 # reload the borrower info for the sake of reseting the flags.....
156 if ($borrowernumber) {
157 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
161 ##################################################################################
167 if ($rejected == -1) {
169 $rejectedtext = << "EOF";
170 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
171 <tr><th><font color=black size=5>Error Issuing Book</font></th></tr>
172 <tr><td><font color=red size=5>$rejected</font></td></tr>
180 if ($borrowerslist) {
181 $selectborrower = <<"EOF";
182 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
183 <input type=hidden name=branch value=$branch>
184 <input type=hidden name=printer value=$printer>
185 <table border=1 cellspacing=0 cellpadding=5 bgcolor="#dddddd">
186 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
187 <font color=black><b>Select a borrower</b></font></th></tr>\n
188 <tr><td align=center>
189 <select name=borrnumber size=7>
191 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
192 $selectborrower .= <<"EOF";
193 <option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})
196 $selectborrower .= <<"EOF";
205 <table align="right"><tr><td>
206 <a href=circulation.pl?borrnumber=$borrowernumber&branch=$branch&printer=$printer&print=yes>
207 <img src="/images/button-next-borrower.gif" width="171" height="42" border="0" alt="Next Borrower"></a>  
209 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
210  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
212 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
213 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
214 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
215 <a href=selectbranchprinter.pl>Change Settings</a></td>
216 <input type=hidden name=branch value=$branch>
217 <input type=hidden name=printer value=$printer>
221 my $titlenoborrower = <<"EOF";
222 <table align="right"><tr><td>
224 <img src="/images/button-returns.gif" width="110" height="42" border="0" alt="Returns"></a>
225  <a href=branchtransfers.pl><img src="/images/button-transfers.gif" width="127" height="42" border="0" alt="Transfers"></a>
227 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
228 <b>Branch:</b> $branches->{$branch}->{'branchname'}  
229 <b>Printer:</b> $printers->{$printer}->{'printername'} <br>
230 <a href=selectbranchprinter.pl>Change Settings</a></td>
231 <input type=hidden name=branch value=$branch>
232 <input type=hidden name=printer value=$printer>
238 my $cardnumberinput = << "EOF";
239 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
240 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
241 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
242 <font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
243 <tr><td><input name=findborrower></td></tr>
244 <input type=hidden name=branch value=$branch>
245 <input type=hidden name=printer value=$printer>
250 my $responsesform = '';
251 foreach (keys %responses) {
252 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
256 my $stickyduedate=$query->param('stickyduedate');
257 $questionform = <<"EOF";
258 <table border=1 cellpadding=5 cellspacing=0 bgcolor="#dddddd">
259 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage>
260 <font size=+2 color=red><b>Issuing Question</b></font></th></tr>
261 <tr><td><table border=0 cellpadding=10><tr><td>
262 Attempting to issue $iteminformation->{'title'}
263 by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
266 </td></tr></table></td></tr>
267 <tr><td align=center>
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=Y>
282 <input type=submit value=Yes>
287 <input type=hidden name=borrnumber value=$borrowernumber>
288 <input type=hidden name=barcode value=$barcode>
289 <input type=hidden name=questionnumber value=$questionnumber>
290 <input type=hidden name=day value=$day>
291 <input type=hidden name=month value=$month>
292 <input type=hidden name=year value=$year>
293 <input type=hidden name=stickyduedate value=$stickyduedate>
294 <input type=hidden name=branch value=$branch>
295 <input type=hidden name=printer value=$printer>
297 <input type=hidden name=answer value=N>
298 <input type=submit value=No>
311 # Barcode entry box, with hidden inputs attached....
314 my $monthoptions = '';
315 my $yearoptions = '';
316 for (my $i=1; $i<32; $i++) {
318 if (($query->param('stickyduedate')) && ($day==$i)) {
319 $selected='selected';
321 $dayoptions.="<option value=$i $selected>$i";
323 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
325 if (($query->param('stickyduedate')) && ($month==$counter)) {
326 $selected='selected';
328 $monthoptions.="<option value=$counter $selected>$_";
331 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
333 if (($query->param('stickyduedate')) && ($year==$i)) {
334 $selected='selected';
336 $yearoptions.="<option value=$i $selected>$i";
339 ($query->param('stickyduedate')) && ($selected='checked');
342 my $barcodeentrytext = <<"EOF";
343 <form method=post action=/cgi-bin/koha/circ/circulation.pl>
344 <table border=1 cellpadding=5>
346 <td align=center valign=top>
347 <table border=0 cellspacing=0 cellpadding=5>
348 <tr><th align=center background=$backgroundimage>
349 <font color=black><b>Enter Book Barcode</b></font></th></tr>
350 <tr><td align=center>
352 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></td></tr>
353 <tr><td colspan=3 align=center>
354 <table border=0 cellpadding=0 cellspacing=0>
356 <select name=day><option value=0>Day$dayoptions</select>
358 <select name=month><option value=0>Month$monthoptions</select>
360 <select name=year><option value=0>Year$yearoptions</select>
363 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
366 <input type=hidden name=borrnumber value=$borrowernumber>
367 <input type=hidden name=branch value=$branch>
368 <input type=hidden name=printer value=$printer>
369 <input type=hidden name=print value=maybe>
371 if ($flags->{'CHARGES'}){
372 $barcodeentrytext.="<input type=hidden name=charges value=yes>";
374 my $amountold=$flags->{'CHARGES'}->{'message'};
375 my @temp=split(/\$/,$amountold);
377 $barcodeentrytext.="<input type=hidden name=oldamount value=$amountold>";
378 $barcodeentrytext.=<<"EOF";
385 # collect the messages and put into message table....
388 $messagetable = << "EOF";
389 <table border=1 cellpadding=5 cellspacing=0 bgcolor='#dddddd'>
390 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font>Messages</font></th></tr>
391 <tr><td> $message </td></tr></table>
397 # make the issued books table.....
403 my $issueslist = getissues($borrower);
404 foreach my $it (keys %$issueslist) {
405 my $issuedate = $issueslist->{$it}->{'timestamp'};
406 $issuedate = substr($issuedate, 0, 8);
407 if ($todaysdate == $issuedate) {
408 push @todaysissues, $issueslist->{$it};
410 push @previousissues, $issueslist->{$it};
415 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
416 my $dd = $book->{'date_due'};
417 my $datedue = $book->{'date_due'};
418 #convert to nz style dates
419 #this should be set with some kinda config variable
420 my @tempdate=split(/-/,$dd);
421 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
423 if ($datedue < $todaysdate) {
424 $dd="<font color=red>$dd</font>\n";
426 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
427 $todaysissues .=<< "EOF";
428 <tr><td bgcolor=$tcolor align=center>$dd</td>
429 <td bgcolor=$tcolor align=center>
430 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
431 <td bgcolor=$tcolor>$book->{'title'}</td>
432 <td bgcolor=$tcolor>$book->{'author'}</td>
433 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
436 # FIXME - For small and private libraries, it'd be nice if this
437 # table included a "Return" link next to each book, so that you
438 # don't have to remember the book's bar code and type it in on the
440 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
441 my $dd = $book->{'date_due'};
442 my $datedue = $book->{'date_due'};
443 #convert to nz style dates
444 #this should be set with some kinda config variable
445 my @tempdate=split(/-/,$dd);
446 $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
448 if ($datedue < $todaysdate) {
449 $dd="<font color=red>$dd</font>\n";
451 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
452 $previssues .= << "EOF";
453 <tr><td bgcolor=$pcolor align=center>$dd</td>
454 <td bgcolor=$pcolor align=center>
455 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
456 <td bgcolor=$pcolor>$book->{'title'}</td>
457 <td bgcolor=$pcolor>$book->{'author'}</td>
458 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
463 my $issuedbookstable;
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>Todays 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>
475 $issuedbookstable .= <<"EOF";
476 <table border=1 cellpadding=5 cellspacing=0 width=80%>
477 <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>
478 <b>Previous Issues</b></font></th></tr>
479 <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
489 # actually print the page!
492 if ($branchcookie && $printercookie) {
493 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
495 print $query->header(-cookie=>[$sessioncookie]);
499 my @inp=startmenu('circulation');
500 if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
502 for (my $i=0;$i<$count;$i++){
503 $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
510 #print startmenu('circulation');
514 print $titlenoborrower;
529 if ($borrowerslist) {
530 print $selectborrower;
532 print $cardnumberinput;
539 my ($patrontable, $flaginfotable) = patrontable($borrower);
541 print $flaginfotable;
542 print $barcodeentrytext;
543 print "<p clear=all><br><br>";
544 print $issuedbookstable;
550 print endmenu('circulation');
554 ####################################################################
555 # Extra subroutines,,,
557 sub cuecatbarcodedecode {
560 my @fields = split(/\./,$barcode);
561 my @results = map(decode($_), @fields[1..$#fields]);
570 my ($year, $month, $day) = @_;
573 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
576 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
577 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
579 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
580 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
581 } elsif (($day > 29) && ($month == 2)) {
582 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
583 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
584 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
586 $date="$year-$month-$day";
590 return ($date, $invalidduedate);
596 my $flags = $borrower->{'flags'};
597 my $flaginfotable='';
601 foreach $flag (sort keys %$flags) {
602 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
603 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
604 if ($flags->{$flag}->{'noissues'}) {
605 if ($flag eq 'CHARGES') {
606 $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";
608 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
611 if ($flag eq 'CHARGES') {
612 $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";
613 } elsif ($flag eq 'WAITING') {
615 my $items=$flags->{$flag}->{'itemlist'};
616 foreach my $item (@$items) {
617 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
618 $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";
620 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
621 } elsif ($flag eq 'ODUES') {
622 my $items=$flags->{$flag}->{'itemlist'};
623 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
624 my $currentcolor=$color;
626 my $color=$currentcolor;
627 foreach my $item (@$items) {
628 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
629 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
630 $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";
633 $itemswaiting.="</table>\n";
634 if ($query->param('module') ne 'returns'){
635 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
637 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
640 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
644 ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
645 $flaginfotext.="</table>";
646 my $patrontable= << "EOF";
648 <table border=1 cellpadding=5 cellspacing=0 align=right>
649 <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
651 <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>
653 return($patrontable, $flaginfotext);
657 # FIXME - This clashes with &C4::Print::printslip
659 my ($env,$borrowernumber)=@_;
660 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
661 $env->{'todaysissues'}=1;
662 my ($borrowerissues) = currentissues($env, $borrower);
663 $env->{'nottodaysissues'}=1;
664 $env->{'todaysissues'}=0;
665 my ($borroweriss2)=currentissues($env, $borrower);
666 $env->{'nottodaysissues'}=0;
669 foreach (sort {$a <=> $b} keys %$borrowerissues) {
670 $issues[$i]=$borrowerissues->{$_};
671 my $dd=$issues[$i]->{'date_due'};
673 #convert to nz style dates
674 #this should be set with some kinda config variable
675 my @tempdate=split(/-/,$dd);
676 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
679 foreach (sort {$a <=> $b} keys %$borroweriss2) {
680 $issues[$i]=$borroweriss2->{$_};
681 my $dd=$issues[$i]->{'date_due'};
683 #convert to nz style dates
684 #this should be set with some kinda config variable
685 my @tempdate=split(/-/,$dd);
686 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
689 remoteprint($env,\@issues,$borrower);