Small changes to help fix Bug 436
[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 =  "'$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 %responseform;
159 my @responsearray;
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;
165 }
166 my $questionform;
167 my $stickyduedate;
168 if ($question) {
169     $stickyduedate=$query->param('stickyduedate');
170 }
171
172
173 # Barcode entry box, with hidden inputs attached....
174
175 # FIXME - How can we move this HTML into the template?  Can we create
176 # arrays of the months, dates, etc and use <TMPL_LOOP> in the template to 
177 # output the data that's getting built here?
178 my $counter = 1;
179 my $dayoptions = '';
180 my $monthoptions = '';
181 my $yearoptions = '';
182 for (my $i=1; $i<32; $i++) {
183     my $selected='';
184     if (($query->param('stickyduedate')) && ($day==$i)) {
185         $selected='selected';
186     }
187     $dayoptions.="<option value=$i $selected>$i";
188 }
189 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
190     my $selected='';
191     if (($query->param('stickyduedate')) && ($month==$counter)) {
192         $selected='selected';
193     }
194     $monthoptions.="<option value=$counter $selected>$_";
195     $counter++;
196 }
197 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
198     my $selected='';
199     if (($query->param('stickyduedate')) && ($year==$i)) {
200         $selected='selected';
201     }
202     $yearoptions.="<option value=$i $selected>$i";
203 }
204 my $selected='';
205 ($query->param('stickyduedate')) && ($selected='checked');
206
207
208 # make the issued books table.....
209 my $todaysissues='';
210 my $previssues='';
211 my @realtodayissues;
212 my @realprevissues;
213 my $allowborrow;
214 my $hash;
215 if ($borrower) {
216     ($borrower, $flags,$hash) = getpatroninformation(\%env,$borrowernumber,0);
217     $allowborrow= $hash->{'borrow'};
218     my @todaysissues;
219     my @previousissues;
220     my $issueslist = getissues($borrower);
221     foreach my $it (keys %$issueslist) {
222         my $issuedate = $issueslist->{$it}->{'timestamp'};
223         $issuedate = substr($issuedate, 0, 8);
224         if ($todaysdate == $issuedate) {
225             push @todaysissues, $issueslist->{$it};
226         } else {
227             push @previousissues, $issueslist->{$it};
228         }
229     }
230         my $tcolor = '';
231         my $pcolor = '';
232         my $od = '';
233         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
234                 my $dd = $book->{'date_due'};
235                 my $datedue = $book->{'date_due'};
236                 $dd=format_date($dd);
237                 $datedue=~s/-//g;
238                 if ($datedue < $todaysdate) {
239                         $od = 'true';
240                         $dd="$dd\n";
241                 }
242                 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
243                 $book->{'od'}=$od;
244                 $book->{'dd'}=$dd;
245                 $book->{'tcolor'}=$tcolor;
246                 if ($book->{'author'} eq ''){
247                     $book->{'author'}=' ';
248                 }    
249                 push @realtodayissues,$book;
250         }
251     
252
253     # FIXME - For small and private libraries, it'd be nice if this
254     # table included a "Return" link next to each book, so that you
255     # don't have to remember the book's bar code and type it in on the
256     # "Returns" page.
257
258     # This is in the template now, so its possible for a small library to make that link in their
259     # template
260
261     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
262         my $dd = $book->{'date_due'};
263         my $datedue = $book->{'date_due'};
264         $dd=format_date($dd);
265         my $pcolor = '';
266         my $od = '';
267         $datedue=~s/-//g;
268         if ($datedue < $todaysdate) {
269                 $od = 'true';
270             $dd="$dd\n";
271         }
272         ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1); 
273         $book->{'dd'}=$dd; 
274         $book->{'od'}=$od;
275         $book->{'tcolor'}=$pcolor;
276         if ($book->{'author'} eq ''){
277             $book->{'author'}=' ';
278         }    
279         push @realprevissues,$book
280    }
281 }
282
283 my @values;
284 my %labels;
285 my $CGIselectborrower;
286 if ($borrowerslist) {
287         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
288                 push @values,$_->{'borrowernumber'};
289                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
290         }
291         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
292                                 -values   => \@values,
293                                 -labels   => \%labels,
294                                 -size     => 7,
295                                 -multiple => 0 );
296 }
297 #title
298
299 my ($patrontable, $flaginfotable) = patrontable($borrower);
300 my $amountold=$flags->{'CHARGES'}->{'message'};
301 my @temp=split(/\$/,$amountold);
302 $amountold=$temp[1];
303 $template->param(
304                 findborrower => $findborrower,
305                 borrower => $borrower,
306                 borrowernumber => $borrowernumber,
307                 branch => $branch,
308                 printer => $printer,
309                 branchname => $branches->{$branch}->{'branchname'},
310                 printername => $printers->{$printer}->{'printername'},
311                 allowborrow =>$allowborrow,
312                 #question form
313                 question => $question,
314                 title => $iteminformation->{'title'},
315                 author => $iteminformation->{'author'},
316                 firstname => $borrower->{'firstname'},
317                 surname => $borrower->{'surname'},
318                 categorycode => $borrower->{'categorycode'},
319                 streetaddress => $borrower->{'streetaddress'},
320                 city => $borrower->{'city'},
321                 phone => $borrower->{'phone'},
322                 cardnumber => $borrower->{'cardnumber'},
323                 question => $question,
324                 barcode => $barcode,
325                 questionnumber => $questionnumber,
326                 dayoptions => $dayoptions,
327                 monthoptions => $monthoptions,
328                 yearoptions => $yearoptions,
329                 stickyduedate => $stickyduedate,
330                 rejected => $rejected,
331                 message => $message,
332                 CGIselectborrower => $CGIselectborrower,
333                 amountold => $amountold,
334                 todayissues => \@realtodayissues,
335                 previssues => \@realprevissues,
336                 responseloop => \@responsearray,
337                  month=>$month,
338                  day=>$day,
339                  year=>$year
340                  
341         );
342
343 if ($branchcookie) {
344     $cookie=[$cookie, $branchcookie, $printercookie];
345 }
346
347 output_html_with_http_headers $query, $cookie, $template->output;
348
349 ####################################################################
350 # Extra subroutines,,,
351
352 sub cuecatbarcodedecode {
353     my ($barcode) = @_;
354     chomp($barcode);
355     my @fields = split(/\./,$barcode);
356     my @results = map(decode($_), @fields[1..$#fields]);
357     if ($#results == 2){
358         return $results[2];
359     } else {
360         return $barcode;
361     }
362 }
363
364 sub fixdate {
365     my ($year, $month, $day) = @_;
366     my $invalidduedate;
367     my $date;
368     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
369         $env{'datedue'}='';
370     } else {
371         
372 # FIXME - Can we set two flags here, one that says 'invalidduedate', so that 
373 # the template can check for it, and then one for a particular message?
374 # Ex: <TMPL_IF NAME="invalidduedate">  <TMPL_IF NAME="daysinFeb">
375 # Invalid Due Date Specified. Book was not issued.  Never that many days
376 # in February! </TMPL_IF> </TMPL_IF>
377
378         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
379             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
380         } else {
381             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
382                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
383             } elsif (($day > 29) && ($month == 2)) {
384                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
385             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
386                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
387             } else {
388                 $date="$year-$month-$day";
389             }
390         }
391     }
392     return ($date, $invalidduedate);
393 }
394
395
396 sub patrontable {
397     my ($borrower) = @_;
398     my $flags = $borrower->{'flags'};
399     my $flaginfotable='';
400     my $flaginfotext;
401     #my $flaginfotext='';
402     my $flag;
403     my $color='';
404     foreach $flag (sort keys %$flags) {
405         warn $flag;
406 #       my @itemswaiting='';
407         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
408         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
409         if ($flags->{$flag}->{'noissues'}) {
410                 $template->param(
411                         noissues => 'true',
412                         color => $color,
413                          );
414                 if ($flag eq 'GNA'){
415                         $template->param(
416                                 gna => 'true'
417                                 );
418                         }
419                 if ($flag eq 'LOST'){
420                         $template->param(
421                                 lost => 'true'
422                         );
423                         }
424                 if ($flag eq 'DBARRED'){
425                         $template->param(
426                                 dbarred => 'true'
427                         );
428                         }
429                 if ($flag eq 'CHARGES') {
430                         $template->param(
431                                 charges => 'true',
432                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
433                                  );
434                 }
435         } else {
436                  if ($flag eq 'CHARGES') {
437                         $template->param(
438                                 charges => 'true',
439                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
440                          );
441                 }
442                 if ($flag eq 'WAITING') {
443                         my $items=$flags->{$flag}->{'itemlist'};
444                         my @itemswaiting;
445                         foreach my $item (@$items) {
446                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
447                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
448                         push @itemswaiting, $iteminformation;
449                         }
450                         $template->param(
451                                 waiting => 'true',
452                                 waitingmsg => $flags->{'WAITING'}->{'message'},
453                                 itemswaiting => \@itemswaiting,
454                                  );
455                 }
456                 if ($flag eq 'ODUES') {
457                         $template->param(
458                                 odues => 'true',
459                                 oduesmsg => $flags->{'ODUES'}->{'message'}
460                                  );
461
462                         my $items=$flags->{$flag}->{'itemlist'};
463                         my $currentcolor=$color;
464                         {
465                         my $color=$currentcolor;
466                             my @itemswaiting;
467                         foreach my $item (@$items) {
468                                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
469                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
470                                 push @itemswaiting, $iteminformation;
471                         }
472                         }
473                         if ($query->param('module') ne 'returns'){
474                                 $template->param( nonreturns => 'true' );
475                         }
476                 }
477                 if ($flag eq 'NOTES') {
478                         $template->param(
479                                 notes => 'true',
480                                 notesmsg => $flags->{'NOTES'}->{'message'}
481                                  );
482                 }
483         }
484     }
485     return($patrontable, $flaginfotext);
486 }
487
488
489 # FIXME - This clashes with &C4::Print::printslip
490 sub printslip {
491     my ($env,$borrowernumber)=@_;
492     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
493     $env->{'todaysissues'}=1;
494     my ($borrowerissues) = currentissues($env, $borrower);
495     $env->{'nottodaysissues'}=1;
496     $env->{'todaysissues'}=0;
497     my ($borroweriss2)=currentissues($env, $borrower);
498     $env->{'nottodaysissues'}=0;
499     my $i=0;
500     my @issues;
501     foreach (sort {$a <=> $b} keys %$borrowerissues) {
502         $issues[$i]=$borrowerissues->{$_};
503         my $dd=$issues[$i]->{'date_due'};
504         #convert to nz style dates
505         #this should be set with some kinda config variable
506         my @tempdate=split(/-/,$dd);
507         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
508         $i++;
509     }
510     foreach (sort {$a <=> $b} keys %$borroweriss2) {
511         $issues[$i]=$borroweriss2->{$_};
512         my $dd=$issues[$i]->{'date_due'};
513         #convert to nz style dates
514         #this should be set with some kinda config variable
515         my @tempdate=split(/-/,$dd);
516         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
517         $i++;
518     }
519     remoteprint($env,\@issues,$borrower);
520 }
521
522 # Local Variables:
523 # tab-width: 8
524 # End: