Fixing the sticky due date problem reported by jerome
[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 if ($borrower) {
206     my @todaysissues;
207     my @previousissues;
208     my $issueslist = getissues($borrower);
209     foreach my $it (keys %$issueslist) {
210         my $issuedate = $issueslist->{$it}->{'timestamp'};
211         $issuedate = substr($issuedate, 0, 8);
212         if ($todaysdate == $issuedate) {
213             push @todaysissues, $issueslist->{$it};
214         } else {
215             push @previousissues, $issueslist->{$it};
216         }
217     }
218     my $tcolor = '';
219     my $pcolor = '';
220     foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
221         my $dd = $book->{'date_due'};
222         my $datedue = $book->{'date_due'};
223         $dd=format_date($dd);
224         $datedue=~s/-//g;
225         if ($datedue < $todaysdate) {
226             $dd="<font color=red>$dd</font>\n";
227         }
228         ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
229         $book->{'dd'}=$dd;
230         $book->{'tcolor'}=$tcolor;
231         push @realtodayissues,$book
232     }
233     # FIXME - For small and private libraries, it'd be nice if this
234     # table included a "Return" link next to each book, so that you
235     # don't have to remember the book's bar code and type it in on the
236     # "Returns" page.
237     
238     # This is in the template now, so its possible for a small library to make that link in their
239     # template
240     
241     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
242         my $dd = $book->{'date_due'};
243         my $datedue = $book->{'date_due'};
244         $dd=format_date($dd);
245         $datedue=~s/-//g;
246         if ($datedue < $todaysdate) {
247             $dd="<font color=red>$dd</font>\n";
248         }
249         ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
250         $book->{'dd'}=$dd;
251         $book->{'tcolor'}=$pcolor;
252         push @realprevissues,$book
253     }
254 }
255
256 # actually print the page!
257 #if ($branchcookie && $printercookie) {
258 #    print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
259 #} else {
260 #    print $query->header(-cookie=>[$sessioncookie]);
261 #}
262
263 #if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
264 #    my $count=@inp;
265 #     for (my $i=0;$i<$count;$i++){
266 #        $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
267 #     }
268 #}
269
270 my @values;
271 my %labels;
272 my $CGIselectborrower;
273 if ($borrowerslist) {
274         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
275                 push @values,$_->{'borrowernumber'};
276                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
277         }
278         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
279                                 -values   => \@values,
280                                 -labels   => \%labels,
281                                 -size     => 7,
282                                 -multiple => 0 );
283 }
284 #title
285
286 my ($patrontable, $flaginfotable) = patrontable($borrower);
287 my $amountold=$flags->{'CHARGES'}->{'message'};
288 my @temp=split(/\$/,$amountold);
289 $amountold=$temp[1];
290 $template->param(
291                 findborrower => $findborrower,
292                 borrower => $borrower,
293                 borrowernumber => $borrowernumber,
294                 branch => $branch,
295                 printer => $printer,
296                 branchname => $branches->{$branch}->{'branchname'},
297                 printername => $printers->{$printer}->{'printername'},
298
299                 #question form
300                 question => $question,
301                 title => $iteminformation->{'title'},
302                 author => $iteminformation->{'author'},
303                 firstname => $borrower->{'firstname'},
304                 surname => $borrower->{'surname'},
305                 question => $question,
306                 barcode => $barcode,
307                 questionnumber => $questionnumber,
308                 dayoptions => $dayoptions,
309                 monthoptions => $monthoptions,
310                 yearoptions => $yearoptions,
311                 stickyduedate => $stickyduedate,
312                 responseform => $responsesform,
313                 rejected => $rejected,
314                 message => $message,
315                 CGIselectborrower => $CGIselectborrower,
316
317                 patrontable => $patrontable,
318                 flaginfotable => $flaginfotable,
319                 CHARGES => $flags->{'CHARGES'},
320                 amountold => $amountold,
321                 todayissues => \@realtodayissues,
322                 previssues => \@realprevissues,
323         );
324
325 if ($branchcookie) {
326     $cookie=[$cookie, $branchcookie, $printercookie];
327 }
328
329 output_html_with_http_headers $query, $cookie, $template->output;
330
331 ####################################################################
332 # Extra subroutines,,,
333
334 sub cuecatbarcodedecode {
335     my ($barcode) = @_;
336     chomp($barcode);
337     my @fields = split(/\./,$barcode);
338     my @results = map(decode($_), @fields[1..$#fields]);
339     if ($#results == 2){
340         return $results[2];
341     } else {
342         return $barcode;
343     }
344 }
345
346 sub fixdate {
347     my ($year, $month, $day) = @_;
348     my $invalidduedate;
349     my $date;
350     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
351         $env{'datedue'}='';
352     } else {
353         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
354             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
355         } else {
356             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
357                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
358             } elsif (($day > 29) && ($month == 2)) {
359                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
360             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
361                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
362             } else {
363                 $date="$year-$month-$day";
364             }
365         }
366     }
367     return ($date, $invalidduedate);
368 }
369
370
371 sub patrontable {
372     my ($borrower) = @_;
373     my $flags = $borrower->{'flags'};
374     my $flaginfotable='';
375     my $flaginfotext='';
376     my $flag;
377     my $color='';
378     foreach $flag (sort keys %$flags) {
379         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
380         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
381         if ($flags->{$flag}->{'noissues'}) {
382             if ($flag eq 'CHARGES') {
383                 $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";
384             } else {
385                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
386             }
387         } else {
388             if ($flag eq 'CHARGES') {
389                 $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";
390             } elsif ($flag eq 'WAITING') {
391                 my $itemswaiting='';
392                 my $items=$flags->{$flag}->{'itemlist'};
393                 foreach my $item (@$items) {
394                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
395                     $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";
396                 }
397                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
398             } elsif ($flag eq 'ODUES') {
399                 my $items=$flags->{$flag}->{'itemlist'};
400                 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
401                 my $currentcolor=$color;
402                 {
403                     my $color=$currentcolor;
404                     foreach my $item (@$items) {
405                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
406                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
407                         $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";
408                     }
409                 }
410                 $itemswaiting.="</table>\n";
411                 if ($query->param('module') ne 'returns'){
412                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
413                 } else {
414                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
415                 }
416             } else {
417                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
418             }
419         }
420     }
421     ($flaginfotext) && ($flaginfotext="<tr><td  colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
422     $flaginfotext.="</table>";
423     my $patrontable= << "EOF";
424 <br><p>
425     <table border=1 cellpadding=5 cellspacing=0 align=right>
426     <tr><td colspan=2><font color=black><b>Patron Information</b></font></td></tr>
427     <tr><td colspan=2>
428     <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>
429 EOF
430     return($patrontable, $flaginfotext);
431 }
432
433
434 # FIXME - This clashes with &C4::Print::printslip
435 sub printslip {
436     my ($env,$borrowernumber)=@_;
437     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
438     $env->{'todaysissues'}=1;
439     my ($borrowerissues) = currentissues($env, $borrower);
440     $env->{'nottodaysissues'}=1;
441     $env->{'todaysissues'}=0;
442     my ($borroweriss2)=currentissues($env, $borrower);
443     $env->{'nottodaysissues'}=0;
444     my $i=0;
445     my @issues;
446     foreach (sort {$a <=> $b} keys %$borrowerissues) {
447         $issues[$i]=$borrowerissues->{$_};
448         my $dd=$issues[$i]->{'date_due'};
449         #convert to nz style dates
450         #this should be set with some kinda config variable
451         my @tempdate=split(/-/,$dd);
452         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
453         $i++;
454     }
455     foreach (sort {$a <=> $b} keys %$borroweriss2) {
456         $issues[$i]=$borroweriss2->{$_};
457         my $dd=$issues[$i]->{'date_due'};
458         #convert to nz style dates
459         #this should be set with some kinda config variable
460         my @tempdate=split(/-/,$dd);
461         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
462         $i++;
463     }
464     remoteprint($env,\@issues,$borrower);
465 }
466
467 # Local Variables:
468 # tab-width: 8
469 # End: