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