*** empty log message ***
[koha_gimpoz] / 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 $linecolor1='#ffffcc';
53 my $linecolor2='white';
54
55 my $branches = getbranches();
56 my $printers = getprinters(\%env);
57
58 my $branch = getbranch($query, $branches);
59 my $printer = getprinter($query, $printers);
60
61
62 #set up cookie.....
63 my $branchcookie;
64 my $printercookie;
65 if ($query->param('setcookies')) {
66         $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
67         $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
68 }
69
70 $env{'branchcode'}=$branch;
71 $env{'printer'}=$printer;
72 $env{'queue'}=$printer;
73
74 my @datearr = localtime(time());
75 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
76 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
77 #warn $todaysdate;
78
79
80 my $message;
81 my $borrowerslist;
82 # if there is a list of find borrowers....
83 my $findborrower = $query->param('findborrower');
84 if ($findborrower) {
85         my ($borrowers, $flags) = findborrower(\%env, $findborrower);
86         my @borrowers=@$borrowers;
87         if ($#borrowers == -1) {
88                 $query->param('findborrower', '');
89                 $message =  "No borrower matched '$findborrower'";
90         } elsif ($#borrowers == 0) {
91                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
92                 $query->param('barcode','');
93         } else {
94                 $borrowerslist = \@borrowers;
95         }
96 }
97
98 my $borrowernumber = $query->param('borrnumber');
99 my $bornum = $query->param('borrnumber');
100 # check and see if we should print
101 my $print=$query->param('print');
102 my $barcode = $query->param('barcode');
103 if ($barcode eq ''  && $print eq 'maybe'){
104         $print = 'yes';
105 }
106 if ($print eq 'yes' && $borrowernumber ne ''){
107         printslip(\%env,$borrowernumber);
108         $query->param('borrnumber','');
109         $borrowernumber='';
110 }
111
112 # get the borrower information.....
113 my $borrower;
114 my $flags;
115 if ($borrowernumber) {
116     ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
117 }
118
119 # get the responses to any questions.....
120 my %responses;
121 foreach (sort $query->param) {
122         if ($_ =~ /response-(\d*)/) {
123                 $responses{$1} = $query->param($_);
124         }
125 }
126 if (my $qnumber = $query->param('questionnumber')) {
127         $responses{$qnumber} = $query->param('answer');
128 }
129
130 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
131
132 my $year=$query->param('year');
133 my $month=$query->param('month');
134 my $day=$query->param('day');
135
136 # if the barcode is set
137 if ($barcode) {
138         $barcode = cuecatbarcodedecode($barcode);
139         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
140
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 if ($borrower) {
204     my @todaysissues;
205     my @previousissues;
206     my $issueslist = getissues($borrower);
207     foreach my $it (keys %$issueslist) {
208         my $issuedate = $issueslist->{$it}->{'timestamp'};
209         $issuedate = substr($issuedate, 0, 8);
210         if ($todaysdate == $issuedate) {
211             push @todaysissues, $issueslist->{$it};
212         } else {
213             push @previousissues, $issueslist->{$it};
214         }
215     }
216     my $tcolor = '';
217     my $pcolor = '';
218     foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){
219         my $dd = $book->{'date_due'};
220         my $datedue = $book->{'date_due'};
221         #convert to nz style dates
222         #this should be set with some kinda config variable
223         my @tempdate=split(/-/,$dd);
224         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
225         $datedue=~s/-//g;
226         if ($datedue < $todaysdate) {
227             $dd="<font color=red>$dd</font>\n";
228         }
229         ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
230         $todaysissues .=<< "EOF";
231 <tr><td bgcolor=$tcolor align=center>$dd</td>
232 <td bgcolor=$tcolor align=center>
233 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
234 <td bgcolor=$tcolor>$book->{'title'}</td>
235 <td bgcolor=$tcolor>$book->{'author'}</td>
236 <td bgcolor=$tcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
237 EOF
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     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
244         my $dd = $book->{'date_due'};
245         my $datedue = $book->{'date_due'};
246         #convert to nz style dates
247         #this should be set with some kinda config variable
248         my @tempdate=split(/-/,$dd);
249         $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
250         $datedue=~s/-//g;
251         if ($datedue < $todaysdate) {
252             $dd="<font color=red>$dd</font>\n";
253         }
254         ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1);
255         $previssues .= << "EOF";
256 <tr><td bgcolor=$pcolor align=center>$dd</td>
257 <td bgcolor=$pcolor align=center>
258 <a href=/cgi-bin/koha/detail.pl?bib=$book->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$book->{'barcode'}</a></td>
259 <td bgcolor=$pcolor>$book->{'title'}</td>
260 <td bgcolor=$pcolor>$book->{'author'}</td>
261 <td bgcolor=$pcolor align=center>$book->{'dewey'} $book->{'subclass'}</td></tr>
262 EOF
263     }
264 }
265
266 # actually print the page!
267 #if ($branchcookie && $printercookie) {
268 #    print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie,$sessioncookie]);
269 #} else {
270 #    print $query->header(-cookie=>[$sessioncookie]);
271 #}
272
273 #if ($query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
274 #    my $count=@inp;
275 #     for (my $i=0;$i<$count;$i++){
276 #        $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\",700,600\)\;window1.focus\(\)/;
277 #     }
278 #}
279
280 my @values;
281 my %labels;
282 my $CGIselectborrower;
283 if ($borrowerslist) {
284         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
285                 push @values,$_->{'borrowernumber'};
286                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
287         }
288         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
289                                 -values   => \@values,
290                                 -labels   => \%labels,
291                                 -size     => 7,
292                                 -multiple => 0 );
293 }
294 #title
295
296 my ($patrontable, $flaginfotable) = patrontable($borrower);
297 my $amountold=$flags->{'CHARGES'}->{'message'};
298 my @temp=split(/\$/,$amountold);
299 $amountold=$temp[1];
300 $template->param(
301                 findborrower => $findborrower,
302                 borrower => $borrower,
303                 borrowernumber => $borrowernumber,
304                 branch => $branch,
305                 printer => $printer,
306                 branchname => $branches->{$branch}->{'branchname'},
307                 printername => $printers->{$printer}->{'printername'},
308
309                 #question form
310                 question => $question,
311                 title => $iteminformation->{'title'},
312                 author => $iteminformation->{'author'},
313                 firstname => $borrower->{'firstname'},
314                 surname => $borrower->{'surname'},
315                 question => $question,
316                 barcode => $barcode,
317                 questionnumber => $questionnumber,
318                 dayoptions => $dayoptions,
319                 monthoptions => $monthoptions,
320                 yearoptions => $yearoptions,
321                 stickyduedate => $stickyduedate,
322                 responseform => $responsesform,
323                 rejected => $rejected,
324                 message => $message,
325                 CGIselectborrower => $CGIselectborrower,
326
327                 patrontable => $patrontable,
328                 flaginfotable => $flaginfotable,
329                 CHARGES => $flags->{'CHARGES'},
330                 amountold => $amountold,
331                 todayissues => $todaysissues,
332                 previssues => $previssues,
333         );
334
335 if ($branchcookie) {
336     $cookie=[$cookie, $branchcookie, $printercookie];
337 }
338
339 output_html_with_http_headers $query, $cookie, $template->output;
340
341 ####################################################################
342 # Extra subroutines,,,
343
344 sub cuecatbarcodedecode {
345     my ($barcode) = @_;
346     chomp($barcode);
347     my @fields = split(/\./,$barcode);
348     my @results = map(decode($_), @fields[1..$#fields]);
349     if ($#results == 2){
350         return $results[2];
351     } else {
352         return $barcode;
353     }
354 }
355
356 sub fixdate {
357     my ($year, $month, $day) = @_;
358     my $invalidduedate;
359     my $date;
360     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
361         $env{'datedue'}='';
362     } else {
363         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
364             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
365         } else {
366             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
367                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
368             } elsif (($day > 29) && ($month == 2)) {
369                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
370             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
371                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
372             } else {
373                 $date="$year-$month-$day";
374             }
375         }
376     }
377     return ($date, $invalidduedate);
378 }
379
380
381 sub patrontable {
382     my ($borrower) = @_;
383     my $flags = $borrower->{'flags'};
384     my $flaginfotable='';
385     my $flaginfotext='';
386     my $flag;
387     my $color='';
388     foreach $flag (sort keys %$flags) {
389         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
390         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
391         if ($flags->{$flag}->{'noissues'}) {
392             if ($flag eq 'CHARGES') {
393                 $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";
394             } else {
395                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
396             }
397         } else {
398             if ($flag eq 'CHARGES') {
399                 $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";
400             } elsif ($flag eq 'WAITING') {
401                 my $itemswaiting='';
402                 my $items=$flags->{$flag}->{'itemlist'};
403                 foreach my $item (@$items) {
404                     my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
405                     $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";
406                 }
407                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
408             } elsif ($flag eq 'ODUES') {
409                 my $items=$flags->{$flag}->{'itemlist'};
410                 my $itemswaiting="<table border=1 cellspacing=0 cellpadding=2>\n";
411                 my $currentcolor=$color;
412                 {
413                     my $color=$currentcolor;
414                     foreach my $item (@$items) {
415                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
416                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
417                         $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";
418                     }
419                 }
420                 $itemswaiting.="</table>\n";
421                 if ($query->param('module') ne 'returns'){
422                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
423                 } else {
424                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
425                 }
426             } else {
427                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
428             }
429         }
430     }
431     ($flaginfotext) && ($flaginfotext="<tr><td  colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
432     $flaginfotext.="</table>";
433     my $patrontable= << "EOF";
434 <br><p>
435     <table border=1 cellpadding=5 cellspacing=0 align=right>
436     <tr><td colspan=2><font color=black><b>Patron Information</b></font></td></tr>
437     <tr><td colspan=2>
438     <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>
439 EOF
440     return($patrontable, $flaginfotext);
441 }
442
443
444 # FIXME - This clashes with &C4::Print::printslip
445 sub printslip {
446     my ($env,$borrowernumber)=@_;
447     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
448     $env->{'todaysissues'}=1;
449     my ($borrowerissues) = currentissues($env, $borrower);
450     $env->{'nottodaysissues'}=1;
451     $env->{'todaysissues'}=0;
452     my ($borroweriss2)=currentissues($env, $borrower);
453     $env->{'nottodaysissues'}=0;
454     my $i=0;
455     my @issues;
456     foreach (sort {$a <=> $b} keys %$borrowerissues) {
457         $issues[$i]=$borrowerissues->{$_};
458         my $dd=$issues[$i]->{'date_due'};
459         #convert to nz style dates
460         #this should be set with some kinda config variable
461         my @tempdate=split(/-/,$dd);
462         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
463         $i++;
464     }
465     foreach (sort {$a <=> $b} keys %$borroweriss2) {
466         $issues[$i]=$borroweriss2->{$_};
467         my $dd=$issues[$i]->{'date_due'};
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: