templating circulation.pl
[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
37 my $query=new CGI;
38 #my ($loggedinuser, $sessioncookie, $sessionID) = checkauth
39 #       ($query, 0, { circulate => 1 });
40
41 my ($template, $loggedinuser, $cookie) = get_template_and_user
42     ({
43         template_name   => 'circ/circulation.tmpl',
44         query           => $query,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { circulate => 1 },
48     });
49
50
51 my %env;
52 #my $headerbackgroundcolor='#99cc33';
53 my $linecolor1='#ffffcc';
54 my $linecolor2='white';
55 #my $backgroundimage="/images/background-mem.gif";
56
57 my $branches = getbranches();
58 my $printers = getprinters(\%env);
59
60 my $branch = getbranch($query, $branches);
61 my $printer = getprinter($query, $printers);
62
63
64 #set up cookie.....
65 my $branchcookie;
66 my $printercookie;
67 if ($query->param('setcookies')) {
68         $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
69         $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
70 }
71
72 $env{'branchcode'}=$branch;
73 $env{'printer'}=$printer;
74 $env{'queue'}=$printer;
75
76 my @datearr = localtime(time());
77 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
78 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
79 #warn $todaysdate;
80
81
82 my $message;
83 my $borrowerslist;
84 # if there is a list of find borrowers....
85 my $findborrower = $query->param('findborrower');
86 if ($findborrower) {
87         my ($borrowers, $flags) = findborrower(\%env, $findborrower);
88         my @borrowers=@$borrowers;
89         if ($#borrowers == -1) {
90                 $query->param('findborrower', '');
91                 $message =  "No borrower matched '$findborrower'";
92         } elsif ($#borrowers == 0) {
93                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
94                 $query->param('barcode','');
95         } else {
96                 $borrowerslist = \@borrowers;
97         }
98 }
99
100 my $borrowernumber = $query->param('borrnumber');
101 my $bornum = $query->param('borrnumber');
102 # check and see if we should print
103 my $print=$query->param('print');
104 my $barcode = $query->param('barcode');
105 if ($barcode eq ''  && $print eq 'maybe'){
106         $print = 'yes';
107 }
108 if ($print eq 'yes' && $borrowernumber ne ''){
109         printslip(\%env,$borrowernumber);
110         $query->param('borrnumber','');
111         $borrowernumber='';
112 }
113
114 # get the borrower information.....
115 my $borrower;
116 my $flags;
117 if ($borrowernumber) {
118     ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
119 }
120
121 # get the responses to any questions.....
122 my %responses;
123 foreach (sort $query->param) {
124         if ($_ =~ /response-(\d*)/) {
125                 $responses{$1} = $query->param($_);
126         }
127 }
128 if (my $qnumber = $query->param('questionnumber')) {
129         $responses{$qnumber} = $query->param('answer');
130 }
131
132 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
133
134 my $year=$query->param('year');
135 my $month=$query->param('month');
136 my $day=$query->param('day');
137
138 # if the barcode is set
139 if ($barcode) {
140         $barcode = cuecatbarcodedecode($barcode);
141         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
142
143         unless ($invalidduedate) {
144                 $env{'datedue'}=$datedue;
145                 my @time=localtime(time);
146                 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
147                 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
148                                         = issuebook(\%env, $borrower, $barcode, \%responses, $date);
149         }
150 }
151
152 # reload the borrower info for the sake of reseting the flags.....
153 if ($borrowernumber) {
154         ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
155 }
156
157 ##################################################################################
158 # HTML code....
159
160 my $responsesform = '';
161 foreach (keys %responses) {
162     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
163 }
164 my $questionform;
165 my $stickyduedate;
166 if ($question) {
167     $stickyduedate=$query->param('stickyduedate');
168 }
169
170
171 # Barcode entry box, with hidden inputs attached....
172 my $counter = 1;
173 my $dayoptions = '';
174 my $monthoptions = '';
175 my $yearoptions = '';
176 for (my $i=1; $i<32; $i++) {
177     my $selected='';
178     if (($query->param('stickyduedate')) && ($day==$i)) {
179         $selected='selected';
180     }
181     $dayoptions.="<option value=$i $selected>$i";
182 }
183 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
184     my $selected='';
185     if (($query->param('stickyduedate')) && ($month==$counter)) {
186         $selected='selected';
187     }
188     $monthoptions.="<option value=$counter $selected>$_";
189     $counter++;
190 }
191 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
192     my $selected='';
193     if (($query->param('stickyduedate')) && ($year==$i)) {
194         $selected='selected';
195     }
196     $yearoptions.="<option value=$i $selected>$i";
197 }
198 my $selected='';
199 ($query->param('stickyduedate')) && ($selected='checked');
200
201
202 # make the issued books table.....
203 my $todaysissues='';
204 my $previssues='';
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         #convert to nz style dates
224         #this should be set with some kinda config variable
225         my @tempdate=split(/-/,$dd);
226         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
227         $datedue=~s/-//g;
228         if ($datedue < $todaysdate) {
229             $dd="<font color=red>$dd</font>\n";
230         }
231         ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
232         $todaysissues .=<< "EOF";
233 <tr><td bgcolor=$tcolor align=center>$dd</td>
234 <td bgcolor=$tcolor align=center>
235 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
236 <td bgcolor=$tcolor>$book->{'title'}</td>
237 <td bgcolor=$tcolor>$book->{'author'}</td>
238 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
239 EOF
240     }
241     # FIXME - For small and private libraries, it'd be nice if this
242     # table included a "Return" link next to each book, so that you
243     # don't have to remember the book's bar code and type it in on the
244     # "Returns" page.
245     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
246         my $dd = $book->{'date_due'};
247         my $datedue = $book->{'date_due'};
248         #convert to nz style dates
249         #this should be set with some kinda config variable
250         my @tempdate=split(/-/,$dd);
251         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
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         $previssues .= << "EOF";
258 <tr><td bgcolor=$pcolor align=center>$dd</td>
259 <td bgcolor=$pcolor align=center>
260 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
261 <td bgcolor=$pcolor>$book->{'title'}</td>
262 <td bgcolor=$pcolor>$book->{'author'}</td>
263 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
264 EOF
265     }
266 }
267
268 # actually print the page!
269 #if ($branchcookie && $printercookie) {
270 #    print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
271 #} else {
272 #    print $query->header(-cookie=>[$sessioncookie]);
273 #}
274
275 #if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
276 #    my $count=@inp;
277 #     for (my $i=0;$i<$count;$i++){
278 #        $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
279 #     }
280 #}
281
282 my @values;
283 my %labels;
284 my $CGIselectborrower;
285 if ($borrowerslist) {
286         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
287                 push @values,$_->{'borrowernumber'};
288                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
289         }
290         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
291                                 -values   => \@values,
292                                 -labels   => \%labels,
293                                 -size     => 7,
294                                 -multiple => 0 );
295 }
296 #title
297
298 my ($patrontable, $flaginfotable) = patrontable($borrower);
299 my $amountold=$flags->{'CHARGES'}->{'message'};
300 my @temp=split(/\$/,$amountold);
301 $amountold=$temp[1];
302 $template->param(
303                 findborrower => $findborrower,
304                 borrower => $borrower,
305                 borrowernumber => $borrowernumber,
306                 branch => $branch,
307                 printer => $printer,
308                 branchname => $branches->{$branch}->{'branchname'},
309                 printername => $printers->{$printer}->{'printername'},
310
311                 #question form
312                 question => $question,
313                 title => $iteminformation->{'title'},
314                 author => $iteminformation->{'author'},
315                 firstname => $borrower->{'firstname'},
316                 surname => $borrower->{'surname'},
317                 question => $question,
318                 barcode => $barcode,
319                 questionnumber => $questionnumber,
320                 dayoptions => $dayoptions,
321                 monthoptions => $monthoptions,
322                 yearoptions => $yearoptions,
323                 stickyduedate => $stickyduedate,
324                 responseform => $responsesform,
325                 rejected => $rejected,
326                 message => $message,
327                 CGIseleborrower => $CGIselectborrower,
328
329                 patrontable => $patrontable,
330                 flaginfotable => $flaginfotable,
331                 CHARGES => $flags->{'CHARGES'},
332                 amountold => $amountold,
333                 todayissues => $todaysissues,
334                 previssues => $previssues,
335         );
336
337 output_html_with_http_headers $query, $cookie, $template->output;
338
339 ####################################################################
340 # Extra subroutines,,,
341
342 sub cuecatbarcodedecode {
343     my ($barcode) = @_;
344     chomp($barcode);
345     my @fields = split(/\./,$barcode);
346     my @results = map(decode($_), @fields[1..$#fields]);
347     if ($#results == 2){
348         return $results[2];
349     } else {
350         return $barcode;
351     }
352 }
353
354 sub fixdate {
355     my ($year, $month, $day) = @_;
356     my $invalidduedate;
357     my $date;
358     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
359         $env{'datedue'}='';
360     } else {
361         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
362             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
363         } else {
364             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
365                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
366             } elsif (($day > 29) && ($month == 2)) {
367                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
368             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
369                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
370             } else {
371                 $date="$year-$month-$day";
372             }
373         }
374     }
375     return ($date, $invalidduedate);
376 }
377
378
379 sub patrontable {
380     my ($borrower) = @_;
381     my $flags = $borrower->{'flags'};
382     my $flaginfotable='';
383     my $flaginfotext='';
384     my $flag;
385     my $color='';
386     foreach $flag (sort keys %$flags) {
387         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
388         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
389         if ($flags->{$flag}->{'noissues'}) {
390             if ($flag eq 'CHARGES') {
391                 $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";
392             } else {
393                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
394             }
395         } else {
396             if ($flag eq 'CHARGES') {
397                 $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";
398             } elsif ($flag eq 'WAITING') {
399                 my $itemswaiting='';
400                 my $items=$flags->{$flag}->{'itemlist'};
401                 foreach my $item (@$items) {
402                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
403                     $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";
404                 }
405                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
406             } elsif ($flag eq 'ODUES') {
407                 my $items=$flags->{$flag}->{'itemlist'};
408                 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
409                 my $currentcolor=$color;
410                 {
411                     my $color=$currentcolor;
412                     foreach my $item (@$items) {
413                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
414                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
415                         $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";
416                     }
417                 }
418                 $itemswaiting.="</table>\n";
419                 if ($query->param('module') ne 'returns'){
420                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
421                 } else {
422                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
423                 }
424             } else {
425                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
426             }
427         }
428     }
429     ($flaginfotext) && ($flaginfotext="<tr><td  colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
430     $flaginfotext.="</table>";
431     my $patrontable= << "EOF";
432 <br><p>
433     <table border=1 cellpadding=5 cellspacing=0 align=right>
434     <tr><td colspan=2 background="/images/background-mem.gif"><font color=black><b>Patron Information</b></font></td></tr>
435     <tr><td colspan=2>
436     <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>
437 EOF
438     return($patrontable, $flaginfotext);
439 }
440
441
442 # FIXME - This clashes with &C4::Print::printslip
443 sub printslip {
444     my ($env,$borrowernumber)=@_;
445     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
446     $env->{'todaysissues'}=1;
447     my ($borrowerissues) = currentissues($env, $borrower);
448     $env->{'nottodaysissues'}=1;
449     $env->{'todaysissues'}=0;
450     my ($borroweriss2)=currentissues($env, $borrower);
451     $env->{'nottodaysissues'}=0;
452     my $i=0;
453     my @issues;
454     foreach (sort {$a <=> $b} keys %$borrowerissues) {
455         $issues[$i]=$borrowerissues->{$_};
456         my $dd=$issues[$i]->{'date_due'};
457 #       warn $_,$dd;
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     foreach (sort {$a <=> $b} keys %$borroweriss2) {
465         $issues[$i]=$borroweriss2->{$_};
466         my $dd=$issues[$i]->{'date_due'};
467 #       warn $_,$dd;
468         #convert to nz style dates
469         #this should be set with some kinda config variable
470         my @tempdate=split(/-/,$dd);
471         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
472         $i++;
473     }
474     remoteprint($env,\@issues,$borrower);
475 }
476
477 # Local Variables:
478 # tab-width: 8
479 # End: