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 ##################################################################################
160 foreach (keys %responses) {
161 # $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
162 $responseform{'name'}=$_;
163 $responseform{'value'}=$responses{$_};
164 push @responsearray,\%responseform;
169 $stickyduedate=$query->param('stickyduedate');
173 # Barcode entry box, with hidden inputs attached....
176 my $monthoptions = '';
177 my $yearoptions = '';
178 for (my $i=1; $i<32; $i++) {
180 if (($query->param('stickyduedate')) && ($day==$i)) {
181 $selected='selected';
183 $dayoptions.="<option value=$i $selected>$i";
185 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
187 if (($query->param('stickyduedate')) && ($month==$counter)) {
188 $selected='selected';
190 $monthoptions.="<option value=$counter $selected>$_";
193 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
195 if (($query->param('stickyduedate')) && ($year==$i)) {
196 $selected='selected';
198 $yearoptions.="<option value=$i $selected>$i";
201 ($query->param('stickyduedate')) && ($selected='checked');
204 # make the issued books table.....
212 ($borrower, $flags,$hash) = getpatroninformation(\%env,$borrowernumber,0);
213 $allowborrow= $hash->{'borrow'};
216 my $issueslist = getissues($borrower);
217 foreach my $it (keys %$issueslist) {
218 my $issuedate = $issueslist->{$it}->{'timestamp'};
219 $issuedate = substr($issuedate, 0, 8);
220 if ($todaysdate == $issuedate) {
221 push @todaysissues, $issueslist->{$it};
223 push @previousissues, $issueslist->{$it};
228 foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
229 my $dd = $book->{'date_due'};
230 my $datedue = $book->{'date_due'};
231 $dd=format_date($dd);
233 if ($datedue < $todaysdate) {
234 $dd="<font color=red>$dd</font>\n";
236 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
238 $book->{'tcolor'}=$tcolor;
239 if ($book->{'author'} eq ''){
240 $book->{'author'}=' ';
242 push @realtodayissues,$book;
246 # FIXME - For small and private libraries, it'd be nice if this
247 # table included a "Return" link next to each book, so that you
248 # don't have to remember the book's bar code and type it in on the
251 # This is in the template now, so its possible for a small library to make that link in their
254 foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
255 my $dd = $book->{'date_due'};
256 my $datedue = $book->{'date_due'};
257 $dd=format_date($dd);
260 if ($datedue < $todaysdate) {
261 $dd="<font color=red>$dd</font>\n";
263 ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
265 $book->{'tcolor'}=$pcolor;
266 if ($book->{'author'} eq ''){
267 $book->{'author'}=' ';
269 push @realprevissues,$book
275 my $CGIselectborrower;
276 if ($borrowerslist) {
277 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
278 push @values,$_->{'borrowernumber'};
279 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
281 $CGIselectborrower=CGI::scrolling_list( -name => 'borrnumber',
289 my ($patrontable, $flaginfotable) = patrontable($borrower);
290 my $amountold=$flags->{'CHARGES'}->{'message'};
291 my @temp=split(/\$/,$amountold);
294 findborrower => $findborrower,
295 borrower => $borrower,
296 borrowernumber => $borrowernumber,
299 branchname => $branches->{$branch}->{'branchname'},
300 printername => $printers->{$printer}->{'printername'},
301 allowborrow =>$allowborrow,
303 question => $question,
304 title => $iteminformation->{'title'},
305 author => $iteminformation->{'author'},
306 firstname => $borrower->{'firstname'},
307 surname => $borrower->{'surname'},
308 categorycode => $borrower->{'categorycode'},
309 question => $question,
311 questionnumber => $questionnumber,
312 dayoptions => $dayoptions,
313 monthoptions => $monthoptions,
314 yearoptions => $yearoptions,
315 stickyduedate => $stickyduedate,
316 rejected => $rejected,
318 CGIselectborrower => $CGIselectborrower,
319 amountold => $amountold,
320 todayissues => \@realtodayissues,
321 previssues => \@realprevissues,
322 responseloop => \@responsearray,
330 $cookie=[$cookie, $branchcookie, $printercookie];
333 output_html_with_http_headers $query, $cookie, $template->output;
335 ####################################################################
336 # Extra subroutines,,,
338 sub cuecatbarcodedecode {
341 my @fields = split(/\./,$barcode);
342 my @results = map(decode($_), @fields[1..$#fields]);
351 my ($year, $month, $day) = @_;
354 if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
357 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
358 $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
360 if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
361 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
362 } elsif (($day > 29) && ($month == 2)) {
363 $invalidduedate="Invalid Due Date Specified. Book was not issued. Never that many days in February!<p>\n";
364 } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
365 $invalidduedate="Invalid Due Date Specified. Book was not issued. $year is not a leap year.<p>\n";
367 $date="$year-$month-$day";
371 return ($date, $invalidduedate);
377 my $flags = $borrower->{'flags'};
378 my $flaginfotable='';
380 #my $flaginfotext='';
383 foreach $flag (sort keys %$flags) {
386 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
387 $flags->{$flag}->{'message'}=~s/\n/<br>/g;
388 if ($flags->{$flag}->{'noissues'}) {
393 if ($flag eq 'CHARGES') {
396 chargesmsg => $flags->{'CHARGES'}->{'message'}
400 if ($flag eq 'CHARGES') {
403 chargesmsg => $flags->{'CHARGES'}->{'message'}
406 if ($flag eq 'WAITING') {
407 my $items=$flags->{$flag}->{'itemlist'};
408 foreach my $item (@$items) {
409 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
410 $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
411 push @itemswaiting, $iteminformation;
415 waitingmsg => $flags->{'WAITING'}->{'message'},
416 itemswaiting => \@itemswaiting,
419 if ($flag eq 'ODUES') {
422 oduesmsg => $flags->{'ODUES'}->{'message'}
425 my $items=$flags->{$flag}->{'itemlist'};
426 my $currentcolor=$color;
428 my $color=$currentcolor;
429 foreach my $item (@$items) {
430 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
431 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
432 push @itemswaiting, $iteminformation;
435 if ($query->param('module') ne 'returns'){
436 $template->param( nonreturns => 'true' );
439 if ($flag eq 'NOTES') {
442 notesmsg => $flags->{'NOTES'}->{'message'}
447 return($patrontable, $flaginfotext);
451 # FIXME - This clashes with &C4::Print::printslip
453 my ($env,$borrowernumber)=@_;
454 my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
455 $env->{'todaysissues'}=1;
456 my ($borrowerissues) = currentissues($env, $borrower);
457 $env->{'nottodaysissues'}=1;
458 $env->{'todaysissues'}=0;
459 my ($borroweriss2)=currentissues($env, $borrower);
460 $env->{'nottodaysissues'}=0;
463 foreach (sort {$a <=> $b} keys %$borrowerissues) {
464 $issues[$i]=$borrowerissues->{$_};
465 my $dd=$issues[$i]->{'date_due'};
466 #convert to nz style dates
467 #this should be set with some kinda config variable
468 my @tempdate=split(/-/,$dd);
469 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
472 foreach (sort {$a <=> $b} keys %$borroweriss2) {
473 $issues[$i]=$borroweriss2->{$_};
474 my $dd=$issues[$i]->{'date_due'};
475 #convert to nz style dates
476 #this should be set with some kinda config variable
477 my @tempdate=split(/-/,$dd);
478 $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
481 remoteprint($env,\@issues,$borrower);