Fix for bug 436, thanks to Pierre
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2 # Please use 8-character tabs for this file (indents are every 4 characters)
3
4 #written 8/5/2002 by Finlay
5 #script to execute issuing of books
6
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
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
15 # version.
16 #
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.
20 #
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
24
25 use strict;
26 use CGI;
27 use C4::Circulation::Circ2;
28 use C4::Search;
29 use C4::Output;
30 use C4::Print;
31 use DBI;
32 use C4::Auth;
33 use C4::Interface::CGI::Output;
34 use C4::Koha;
35 use HTML::Template;
36 use C4::Date;
37
38 my $query=new CGI;
39 #my ($loggedinuser, $sessioncookie, $sessionID) = checkauth
40 #       ($query, 0, { circulate => 1 });
41
42 my ($template, $loggedinuser, $cookie) = get_template_and_user
43     ({
44         template_name   => 'circ/circulation.tmpl',
45         query           => $query,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { circulate => 1 },
49     });
50
51
52 my %env;
53 my $linecolor1='#ffffcc';
54 my $linecolor2='white';
55
56 my $branches = getbranches();
57 my $printers = getprinters(\%env);
58
59 my $branch = getbranch($query, $branches);
60 my $printer = getprinter($query, $printers);
61
62
63 #set up cookie.....
64 my $branchcookie;
65 my $printercookie;
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');
69 }
70
71 $env{'branchcode'}=$branch;
72 $env{'printer'}=$printer;
73 $env{'queue'}=$printer;
74
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]));
78 #warn $todaysdate;
79
80
81 my $message;
82 my $borrowerslist;
83 # if there is a list of find borrowers....
84 my $findborrower = $query->param('findborrower');
85 if ($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','');
94         } else {
95                 $borrowerslist = \@borrowers;
96         }
97 }
98
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'){
105         $print = 'yes';
106 }
107 if ($print eq 'yes' && $borrowernumber ne ''){
108         printslip(\%env,$borrowernumber);
109         $query->param('borrnumber','');
110         $borrowernumber='';
111 }
112
113 # get the borrower information.....
114 my $borrower;
115 my $flags;
116 if ($borrowernumber) {
117     ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
118 }
119
120 # get the responses to any questions.....
121 my %responses;
122 foreach (sort $query->param) {
123         if ($_ =~ /response-(\d*)/) {
124                 $responses{$1} = $query->param($_);
125         }
126 }
127 if (my $qnumber = $query->param('questionnumber')) {
128         $responses{$qnumber} = $query->param('answer');
129 }
130
131 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
132
133 my $year=$query->param('year');
134 my $month=$query->param('month');
135 my $day=$query->param('day');
136
137 # if the barcode is set
138 if ($barcode) {
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);
147         }
148 }
149
150 # reload the borrower info for the sake of reseting the flags.....
151 if ($borrowernumber) {
152         ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
153 }
154
155 ##################################################################################
156 # HTML code....
157
158 my $responsesform = '';
159 foreach (keys %responses) {
160     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
161 }
162 my $questionform;
163 my $stickyduedate;
164 if ($question) {
165     $stickyduedate=$query->param('stickyduedate');
166 }
167
168
169 # Barcode entry box, with hidden inputs attached....
170 my $counter = 1;
171 my $dayoptions = '';
172 my $monthoptions = '';
173 my $yearoptions = '';
174 for (my $i=1; $i<32; $i++) {
175     my $selected='';
176     if (($query->param('stickyduedate')) && ($day==$i)) {
177         $selected='selected';
178     }
179     $dayoptions.="<option value=$i $selected>$i";
180 }
181 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
182     my $selected='';
183     if (($query->param('stickyduedate')) && ($month==$counter)) {
184         $selected='selected';
185     }
186     $monthoptions.="<option value=$counter $selected>$_";
187     $counter++;
188 }
189 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
190     my $selected='';
191     if (($query->param('stickyduedate')) && ($year==$i)) {
192         $selected='selected';
193     }
194     $yearoptions.="<option value=$i $selected>$i";
195 }
196 my $selected='';
197 ($query->param('stickyduedate')) && ($selected='checked');
198
199
200 # make the issued books table.....
201 my $todaysissues='';
202 my $previssues='';
203 my @realtodayissues;
204 my @realprevissues;
205 my $allowborrow;
206 my $hash;
207 if ($borrower) {
208     ($borrower, $flags,$hash) = getpatroninformation(\%env,$borrowernumber,0);
209     $allowborrow= $hash->{'borrow'};
210     my @todaysissues;
211     my @previousissues;
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};
218         } else {
219             push @previousissues, $issueslist->{$it};
220         }
221     }
222         my $tcolor = '';
223         my $pcolor = '';
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);
228                 $datedue=~s/-//g;
229                 if ($datedue < $todaysdate) {
230                         $dd="<font color=red>$dd</font>\n";
231                 }
232                 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
233                 $book->{'dd'}=$dd;
234                 $book->{'tcolor'}=$tcolor;
235                 push @realtodayissues,$book;
236         }
237     
238
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
242     # "Returns" page.
243
244     # This is in the template now, so its possible for a small library to make that link in their
245     # template
246
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);
251         my $pcolor = '';
252         $datedue=~s/-//g;
253         if ($datedue < $todaysdate) {
254             $dd="<font color=red>$dd</font>\n";
255         }
256         ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1); 
257         $book->{'dd'}=$dd; 
258         $book->{'tcolor'}=$pcolor; 
259         push @realprevissues,$book
260    }
261 }
262
263 my @values;
264 my %labels;
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'})";
270         }
271         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
272                                 -values   => \@values,
273                                 -labels   => \%labels,
274                                 -size     => 7,
275                                 -multiple => 0 );
276 }
277 #title
278
279 my ($patrontable, $flaginfotable) = patrontable($borrower);
280 my $amountold=$flags->{'CHARGES'}->{'message'};
281 my @temp=split(/\$/,$amountold);
282 $amountold=$temp[1];
283 $template->param(
284                 findborrower => $findborrower,
285                 borrower => $borrower,
286                 borrowernumber => $borrowernumber,
287                 branch => $branch,
288                 printer => $printer,
289                 branchname => $branches->{$branch}->{'branchname'},
290                 printername => $printers->{$printer}->{'printername'},
291                 allowborrow =>$allowborrow,
292
293                 #question form
294                 question => $question,
295                 title => $iteminformation->{'title'},
296                 author => $iteminformation->{'author'},
297                 firstname => $borrower->{'firstname'},
298                 surname => $borrower->{'surname'},
299                 categorycode => $borrower->{'categorycode'},
300                 question => $question,
301                 barcode => $barcode,
302                 questionnumber => $questionnumber,
303                 dayoptions => $dayoptions,
304                 monthoptions => $monthoptions,
305                 yearoptions => $yearoptions,
306                 stickyduedate => $stickyduedate,
307                 responseform => $responsesform,
308                 rejected => $rejected,
309                 message => $message,
310                 CGIselectborrower => $CGIselectborrower,
311
312                 amountold => $amountold,
313                 todayissues => \@realtodayissues,
314                 previssues => \@realprevissues
315         );
316
317 if ($branchcookie) {
318     $cookie=[$cookie, $branchcookie, $printercookie];
319 }
320
321 output_html_with_http_headers $query, $cookie, $template->output;
322
323 ####################################################################
324 # Extra subroutines,,,
325
326 sub cuecatbarcodedecode {
327     my ($barcode) = @_;
328     chomp($barcode);
329     my @fields = split(/\./,$barcode);
330     my @results = map(decode($_), @fields[1..$#fields]);
331     if ($#results == 2){
332         return $results[2];
333     } else {
334         return $barcode;
335     }
336 }
337
338 sub fixdate {
339     my ($year, $month, $day) = @_;
340     my $invalidduedate;
341     my $date;
342     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
343         $env{'datedue'}='';
344     } else {
345         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
346             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
347         } else {
348             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
349                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
350             } elsif (($day > 29) && ($month == 2)) {
351                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
352             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
353                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
354             } else {
355                 $date="$year-$month-$day";
356             }
357         }
358     }
359     return ($date, $invalidduedate);
360 }
361
362
363 sub patrontable {
364     my ($borrower) = @_;
365     my $flags = $borrower->{'flags'};
366     my $flaginfotable='';
367     my $flaginfotext;
368     #my $flaginfotext='';
369     my $flag;
370     my $color='';
371     foreach $flag (sort keys %$flags) {
372         warn $flag;
373         my @itemswaiting='';
374         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
375         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
376         if ($flags->{$flag}->{'noissues'}) {
377                 $template->param(
378                         noissues => 'true',
379                         color => $color,
380                          );
381                 if ($flag eq 'CHARGES') {
382                         $template->param(
383                                 charges => 'true',
384                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
385                                  );
386                 }
387         } else {
388                  if ($flag eq 'CHARGES') {
389                         $template->param(
390                                 charges => 'true',
391                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
392                          );
393                 }
394                 if ($flag eq 'WAITING') {
395                         my $items=$flags->{$flag}->{'itemlist'};
396                         foreach my $item (@$items) {
397                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
398                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
399                         push @itemswaiting, $iteminformation;
400                         }
401                         $template->param(
402                                 waiting => 'true',
403                                 waitingmsg => $flags->{'WAITING'}->{'message'},
404                                 itemswaiting => \@itemswaiting,
405                                  );
406                 }
407                 if ($flag eq 'ODUES') {
408                         $template->param(
409                                 odues => 'true',
410                                 oduesmsg => $flags->{'ODUES'}->{'message'}
411                                  );
412
413                         my $items=$flags->{$flag}->{'itemlist'};
414                         my $currentcolor=$color;
415                         {
416                         my $color=$currentcolor;
417                         foreach my $item (@$items) {
418                                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
419                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
420                                 push @itemswaiting, $iteminformation;
421                         }
422                         }
423                         if ($query->param('module') ne 'returns'){
424                                 $template->param( nonreturns => 'true' );
425                         }
426                 }
427                 if ($flag eq 'NOTES') {
428                         $template->param(
429                                 notes => 'true',
430                                 notesmsg => $flags->{'NOTES'}->{'message'}
431                                  );
432                 }
433         }
434     }
435     return($patrontable, $flaginfotext);
436 }
437
438
439 # FIXME - This clashes with &C4::Print::printslip
440 sub printslip {
441     my ($env,$borrowernumber)=@_;
442     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
443     $env->{'todaysissues'}=1;
444     my ($borrowerissues) = currentissues($env, $borrower);
445     $env->{'nottodaysissues'}=1;
446     $env->{'todaysissues'}=0;
447     my ($borroweriss2)=currentissues($env, $borrower);
448     $env->{'nottodaysissues'}=0;
449     my $i=0;
450     my @issues;
451     foreach (sort {$a <=> $b} keys %$borrowerissues) {
452         $issues[$i]=$borrowerissues->{$_};
453         my $dd=$issues[$i]->{'date_due'};
454         #convert to nz style dates
455         #this should be set with some kinda config variable
456         my @tempdate=split(/-/,$dd);
457         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
458         $i++;
459     }
460     foreach (sort {$a <=> $b} keys %$borroweriss2) {
461         $issues[$i]=$borroweriss2->{$_};
462         my $dd=$issues[$i]->{'date_due'};
463         #convert to nz style dates
464         #this should be set with some kinda config variable
465         my @tempdate=split(/-/,$dd);
466         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
467         $i++;
468     }
469     remoteprint($env,\@issues,$borrower);
470 }
471
472 # Local Variables:
473 # tab-width: 8
474 # End: