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