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;
39 #my ($loggedinuser, $sessioncookie, $sessionID) = checkauth
40 # ($query, 0, { circulate => 1 });
42 my ($template, $loggedinuser, $cookie) = get_template_and_user
44 template_name => 'circ/circulation.tmpl',
48 flagsrequired => { circulate => 1 },
53 my $linecolor1='#ffffcc';
54 my $linecolor2='white';
56 my $branches = getbranches();
57 my $printers = getprinters(\%env);
59 my $branch = getbranch($query, $branches);
60 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 ($count,$borrowers)=BornameSearch(\%env,$findborrower,'web');
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','');
113 # get the borrower information.....
116 if ($borrowernumber) {
117 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
120 # get the responses to any questions.....
122 foreach (sort $query->param) {
123 if ($_ =~ /response-(\d*)/) {
124 $responses{$1} = $query->param($_);
127 if (my $qnumber = $query->param('questionnumber')) {
128 $responses{$qnumber} = $query->param('answer');
131 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
133 my $year=$query->param('year');
134 my $month=$query->param('month');
135 my $day=$query->param('day');
137 # if the barcode is set
139 $barcode = cuecatbarcodedecode($barcode);
140 my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
141 unless ($invalidduedate) {
142 $env{'datedue'}=$datedue;
143 my @time=localtime(time);
144 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
145 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
146 = issuebook(\%env, $borrower, $barcode, \%responses, $date);
150 # reload the borrower info for the sake of reseting the flags.....
151 if ($borrowernumber) {
152 ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
155 ##################################################################################
158 my $responsesform = '';
159 foreach (keys %responses) {
160 $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
165 $stickyduedate=$query->param('stickyduedate');
169 # Barcode entry box, with hidden inputs attached....
172 my $monthoptions = '';
173 my $yearoptions = '';
174 for (my $i=1; $i<32; $i++) {
176 if (($query->param('stickyduedate')) && ($day==$i)) {
177 $selected='selected';
179 $dayoptions.="<option value=$i $selected>$i";
181 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
183 if (($query->param('stickyduedate')) && ($month==$counter)) {
184 $selected='selected';
186 $monthoptions.="<option value=$counter $selected>$_";
189 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
191 if (($query->param('stickyduedate')) && ($year==$i)) {
192 $selected='selected';
194 $yearoptions.="<option value=$i $selected>$i";
197 ($query->param('stickyduedate')) && ($selected='checked');
200 # make the issued books table.....
208 ($borrower, $flags,$hash) = getpatroninformation(\%env,$borrowernumber,0);
209 $allowborrow= $hash->{'borrow'};
212 my $issueslist = getissues($borrower);
213 foreach my $it (keys %$issueslist) {
214 my $issuedate = $issueslist->{$it}->{'timestamp'};
215 $issuedate = substr($issuedate, 0, 8);
216 if ($todaysdate == $issuedate) {
217 push @todaysissues, $issueslist->{$it};
219 push @previousissues, $issueslist->{$it};
224 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
225 my $dd = $book->{'date_due'};
226 my $datedue = $book->{'date_due'};
227 $dd=format_date($dd);
229 if ($datedue < $todaysdate) {
230 $dd="<font color=red>$dd</font>\n";
232 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
234 $book->{'tcolor'}=$tcolor;
235 push @realtodayissues,$book;
239 # FIXME - For small and private libraries, it'd be nice if this
240 # table included a "Return" link next to each book, so that you
241 # don't have to remember the book's bar code and type it in on the
244 # This is in the template now, so its possible for a small library to make that link in their
247 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
248 my $dd = $book->{'date_due'};
249 my $datedue = $book->{'date_due'};
250 $dd=format_date($dd);
253 if ($datedue < $todaysdate) {
254 $dd="<font color=red>$dd</font>\n";
256 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
258 $book->{'tcolor'}=$pcolor;
259 push @realprevissues,$book
265 my $CGIselectborrower;
266 if ($borrowerslist) {
267 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
268 push @values,$_->{'borrowernumber'};
269 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
271 $CGIselectborrower=CGI::scrolling_list( -name => 'borrnumber',
279 my ($patrontable, $flaginfotable) = patrontable($borrower);
280 my $amountold=$flags->{'CHARGES'}->{'message'};
281 my @temp=split(/\$/,$amountold);
284 findborrower => $findborrower,
285 borrower => $borrower,
286 borrowernumber => $borrowernumber,
289 branchname => $branches->{$branch}->{'branchname'},
290 printername => $printers->{$printer}->{'printername'},
291 allowborrow =>$allowborrow,
294 question => $question,
295 title => $iteminformation->{'title'},
296 author => $iteminformation->{'author'},
297 firstname => $borrower->{'firstname'},
298 surname => $borrower->{'surname'},
299 question => $question,
301 questionnumber => $questionnumber,
302 dayoptions => $dayoptions,
303 monthoptions => $monthoptions,
304 yearoptions => $yearoptions,
305 stickyduedate => $stickyduedate,
306 responseform => $responsesform,
307 rejected => $rejected,
309 CGIselectborrower => $CGIselectborrower,
311 patrontable => $patrontable,
312 flaginfotable => $flaginfotable,
313 CHARGES => $flags->{'CHARGES'},
314 amountold => $amountold,
315 todayissues => \@realtodayissues,
316 previssues => \@realprevissues
320 $cookie=[$cookie, $branchcookie, $printercookie];
323 output_html_with_http_headers $query, $cookie, $template->output;
325 ####################################################################
326 # Extra subroutines,,,
328 sub cuecatbarcodedecode {
331 my @fields = split(/\./,$barcode);
332 my @results = map(decode($_), @fields[1..$#fields]);
341 my ($year, $month, $day) = @_;
344 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
347 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
348 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
350 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
351 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
352 } elsif (($day > 29) && ($month == 2)) {
353 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
354 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
355 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
357 $date="$year-$month-$day";
361 return ($date, $invalidduedate);
367 my $flags = $borrower->{'flags'};
368 my $flaginfotable='';
372 foreach $flag (sort keys %$flags) {
373 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
374 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
375 if ($flags->{$flag}->{'noissues'}) {
376 if ($flag eq 'CHARGES') {
377 $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";
379 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
382 if ($flag eq 'CHARGES') {
383 $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";
384 } elsif ($flag eq 'WAITING') {
386 my $items=$flags->{$flag}->{'itemlist'};
387 foreach my $item (@$items) {
388 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
389 $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";
391 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
392 } elsif ($flag eq 'ODUES') {
393 my $items=$flags->{$flag}->{'itemlist'};
394 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
395 my $currentcolor=$color;
397 my $color=$currentcolor;
398 foreach my $item (@$items) {
399 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
400 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
401 $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";
404 $itemswaiting.="</table>\n";
405 if ($query->param('module') ne 'returns'){
406 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
408 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
411 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
415 ($flaginfotext) && ($flaginfotext="<tr><td colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
416 $flaginfotext.="</table>";
417 my $patrontable= << "EOF";
419 <table border=1 cellpadding=5 cellspacing=0 align=right>
420 <tr><td colspan=2><font color=black><b>Patron Information</b></font></td></tr>
422 <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>
424 return($patrontable, $flaginfotext);
428 # FIXME - This clashes with &C4::Print::printslip
430 my ($env,$borrowernumber)=@_;
431 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
432 $env->{'todaysissues'}=1;
433 my ($borrowerissues) = currentissues($env, $borrower);
434 $env->{'nottodaysissues'}=1;
435 $env->{'todaysissues'}=0;
436 my ($borroweriss2)=currentissues($env, $borrower);
437 $env->{'nottodaysissues'}=0;
440 foreach (sort {$a <=> $b} keys %$borrowerissues) {
441 $issues[$i]=$borrowerissues->{$_};
442 my $dd=$issues[$i]->{'date_due'};
443 #convert to nz style dates
444 #this should be set with some kinda config variable
445 my @tempdate=split(/-/,$dd);
446 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
449 foreach (sort {$a <=> $b} keys %$borroweriss2) {
450 $issues[$i]=$borroweriss2->{$_};
451 my $dd=$issues[$i]->{'date_due'};
452 #convert to nz style dates
453 #this should be set with some kinda config variable
454 my @tempdate=split(/-/,$dd);
455 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
458 remoteprint($env,\@issues,$borrower);