HTML::Template => HTML::Template::Pro
[koha.git] / opac / sco / printer.pl
1 #!/usr/bin/perl
2 #this code has been modified (slightly) by Trendsetters (originally from circulation.pl)
3 # Please use 8-character tabs for this file (indents are every 4 characters)
4
5 #written 8/5/2002 by Finlay
6 #script to execute issuing of books
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 use CGI;
28 use C4::Circulation;
29 use C4::Search;
30 use C4::Output;
31 use C4::Print;
32 use DBI;
33 use C4::Authsco;
34 use C4::Output;
35 use C4::Koha;
36 use HTML::Template::Pro;
37 use C4::Date;
38
39 my $query=new CGI;
40 #my ($loggedinuser, $sessioncookie, $sessionID) = checkauth
41 #       ($query, 0, { circulate => 1 });
42
43 my ($template, $loggedinuser, $cookie) = get_template_and_user
44     ({
45 #Begin code modified by Christina Lee
46         template_name   => 'sco/receipt.tmpl',
47         query           => $query,
48         type            => "opac",
49         authnotrequired => 0,
50         flagsrequired   => { borrow => 1 },
51 # End Code Modified by Christina Lee
52     });
53
54 #Begin code by Christina Lee--Sets variable $borr equal to loggedinuser's data
55 my ($borr, $flags) = getpatroninformation(undef, $loggedinuser);
56 # End code by Christina Lee
57
58 my %env;
59 my $linecolor1='#339999';
60 my $linecolor2='white';
61
62 my $branches = getbranches();
63 my $printers = getprinters(\%env);
64
65 my $branch = "APL"; #getbranch($query, $branches);
66 my $printer = getprinter($query, $printers);
67
68
69 #set up cookie.....
70 my $branchcookie;
71 my $printercookie;
72 if ($query->param('setcookies')) {
73         $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
74         $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
75 }
76
77 $env{'branchcode'}=$branch;
78 $env{'printer'}=$printer;
79 $env{'queue'}=$printer;
80
81 my @datearr = localtime(time());
82 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
83 my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", ($datearr[3]));
84 #warn $todaysdate;
85
86 ################# Start code modified by Christina Lee###########################
87 # get borrower information ....
88 #my ($borr, $flags) = getpatroninformation(undef, $loggedinusername);
89 #my @bordat;
90 #$bordat[0] = $borr;
91
92 #$template->param(BORROWER_INFO => \@bordat);
93
94
95
96
97 ######################End code modified by christina Lee############################
98
99 my $message;
100 my $borrowerslist;
101 # if there is a list of find borrowers....
102 my $findborrower = $query->param('findborrower');
103 if ($findborrower) {
104         my ($count,$borrowers)=BornameSearch(\%env,$findborrower,'web');
105         my @borrowers=@$borrowers;
106         if ($#borrowers == -1) {
107                 $query->param('findborrower', '');
108                 $message =  "'$findborrower'";
109         } elsif ($#borrowers == 0) {
110                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
111                 $query->param('barcode','');
112         } else {
113                 $borrowerslist = \@borrowers;
114         }
115 }
116
117
118 my $borrowernumber = $query->param('borrnumber');
119 my $bornum = $query->param('borrnumber');
120 # check and see if we should print
121 my $print=$query->param('print');
122 my $barcode = $query->param('barcode');
123 if ($barcode eq ''  && $print eq 'maybe'){
124         $print = 'yes';
125 }
126 if ($print eq 'yes' && $borrowernumber ne ''){
127         printslip(\%env,$borrowernumber);
128         $query->param('borrnumber','');
129         $borrowernumber='';
130 }
131
132 # get the borrower information.....
133 my $borrower;
134 my $flags;
135 if ($borrowernumber) {
136     ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
137 }
138
139 # get the responses to any questions.....
140 my %responses;
141 foreach (sort $query->param) {
142         if ($_ =~ /response-(\d*)/) {
143                 $responses{$1} = $query->param($_);
144         }
145 }
146 if (my $qnumber = $query->param('questionnumber')) {
147         $responses{$qnumber} = $query->param('answer');
148 }
149
150
151
152 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer);
153 #Begin code edited by Christina Lee
154 #my $barc = 123456789;
155 my $barc = cuecatbarcodedecode($barcode);
156
157
158 (my $year, my $month, my $day) = set_duedate($barc);
159 #End code edited by Christina Lee
160
161 # if the barcode is set
162 if ($barcode) {
163         $barcode = cuecatbarcodedecode($barcode);
164
165
166  
167 #note: edit code here --Christina Lee
168         my ($datedue, $invalidduedate) = fixdate($year, $month, $day);
169         unless ($invalidduedate) {
170                 $env{'datedue'}=$datedue;
171                 my @time=localtime(time);
172                 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
173                 ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message)
174                                         = issuebook(\%env, $borr, $barcode, \%responses, $date);
175         }
176 }
177
178 # reload the borrower info for the sake of reseting the flags.....
179 if ($borrowernumber) {
180         ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
181 }
182
183 ##################################################################################
184 # HTML code....
185
186 my %responseform;
187 my @responsearray;
188 foreach (keys %responses) {
189 #    $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
190     $responseform{'name'}=$_;
191     $responseform{'value'}=$responses{$_};
192     push @responsearray,\%responseform;
193 }
194 my $questionform;
195 my $stickyduedate;
196 if ($question) {
197     $stickyduedate=$query->param('stickyduedate');
198 }
199
200
201 # Barcode entry box, with hidden inputs attached....
202
203 # FIXME - How can we move this HTML into the template?  Can we create
204 # arrays of the months, dates, etc and use <TMPL_LOOP> in the template to 
205 # output the data that's getting built here?
206 my $counter = 1;
207 my $dayoptions = '';
208 my $monthoptions = '';
209 my $yearoptions = '';
210 for (my $i=1; $i<32; $i++) {
211     my $selected='';
212     if (($query->param('stickyduedate')) && ($day==$i)) {
213         $selected='selected';
214     }
215     $dayoptions.="<option value=$i $selected>$i";
216 }
217 foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
218     my $selected='';
219     if (($query->param('stickyduedate')) && ($month==$counter)) {
220         $selected='selected';
221     }
222     $monthoptions.="<option value=$counter $selected>$_";
223     $counter++;
224 }
225 for (my $i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
226     my $selected='';
227     if (($query->param('stickyduedate')) && ($year==$i)) {
228         $selected='selected';
229     }
230     $yearoptions.="<option value=$i $selected>$i";
231 }
232 my $selected='';
233 ($query->param('stickyduedate')) && ($selected='checked');
234
235
236 # make the issued books table.....
237 my $todaysissues='';
238 my $previssues='';
239 my @realtodayissues;
240 my @realprevissues;
241 my $allowborrow;
242 my $hash;
243 # Begin code altered by christina Lee
244 if ($borr) {
245     ($borr, $flags,$hash) = getpatroninformation(\%env,$loggedinuser,0);
246 # End code altered by Christina Lee
247     $allowborrow= $hash->{'borrow'};
248     my @todaysissues;
249     my @previousissues;
250 # Begin code altered by Christina Lee
251     my $issueslist = getissues($borr);
252 # End code altered by Christina Lee
253     foreach my $it (keys %$issueslist) {
254         my $issuedate = $issueslist->{$it}->{'timestamp'};
255         $issuedate = substr($issuedate, 0, 8);
256         if ($todaysdate == $issuedate) {
257             push @todaysissues, $issueslist->{$it};
258         } else {
259             push @previousissues, $issueslist->{$it};
260         }
261     }
262         my $tcolor = '';
263         my $pcolor = '';
264         my $od = '';
265         foreach my $book (sort {$b->{'timestamp'} <=> $a->{'timestamp'}} @todaysissues){        
266                  my $dd = $book->{'date_due'};
267                 
268                 my $datedue = $book->{'date_due'};
269                 $dd=format_date($dd);
270                 $datedue=~s/-//g;
271                 if ($datedue < $todaysdate) {
272                         $od = 'true';
273                         $dd="$dd\n";
274                 }
275                 ($tcolor eq $linecolor1) ? ($tcolor=$linecolor2) : ($tcolor=$linecolor1);
276                 $book->{'od'}=$od;
277                 $book->{'dd'}=$dd;
278                 $book->{'tcolor'}=$tcolor;
279                 if ($book->{'author'} eq ''){
280                     $book->{'author'}=' ';
281                 }    
282                 push @realtodayissues,$book;
283         }
284     
285
286     # FIXME - For small and private libraries, it'd be nice if this
287     # table included a "Return" link next to each book, so that you
288     # don't have to remember the book's bar code and type it in on the
289     # "Returns" page.
290
291     # This is in the template now, so its possible for a small library to make that link in their
292     # template
293
294     foreach my $book (sort {$a->{'date_due'} cmp $b->{'date_due'}} @previousissues){
295          my $dd = $book->{'date_due'};
296         
297
298         my $datedue = $book->{'date_due'};
299         $dd=format_date($dd);
300         my $pcolor = '';
301         my $od = '';
302         $datedue=~s/-//g;
303         if ($datedue < $todaysdate) {
304                 $od = 'true';
305             $dd="$dd\n";
306         }
307         ($pcolor eq $linecolor1) ? ($pcolor=$linecolor2) : ($pcolor=$linecolor1); 
308         $book->{'dd'}=$dd; 
309         $book->{'od'}=$od;
310         $book->{'tcolor'}=$pcolor;
311         if ($book->{'author'} eq ''){
312             $book->{'author'}=' ';
313         }    
314         push @realprevissues,$book
315    }
316 }
317
318 my @values;
319 my %labels;
320 my $CGIselectborrower;
321 if ($borrowerslist) {
322         foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowerslist){
323                 push @values,$_->{'borrowernumber'};
324                 $labels{$_->{'borrowernumber'}} ="$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})";
325         }
326         $CGIselectborrower=CGI::scrolling_list( -name     => 'borrnumber',
327                                 -values   => \@values,
328                                 -labels   => \%labels,
329                                 -size     => 7,
330                                 -multiple => 0 );
331 }
332 #title
333
334 my ($patrontable, $flaginfotable) = patrontable($borrower);
335 my $amountold=$flags->{'CHARGES'}->{'message'};
336 my @temp=split(/\$/,$amountold);
337 $amountold=$temp[1];
338 $template->param(
339                 findborrower => $findborrower,
340                 borrower => $borrower,
341                 borrowernumber => $borrowernumber,
342                 branch => $branch,
343                 printer => $printer,
344                 branchname => $branches->{$branch}->{'branchname'},
345                 printername => $printers->{$printer}->{'printername'},
346                 allowborrow =>$allowborrow,
347                 #question form
348                 question => $question,
349                 title => $iteminformation->{'title'},
350                 author => $iteminformation->{'author'},
351 #Begin code by Christina Lee
352                 firstname => $borr->{'firstname'},
353                 surname => $borr->{'surname'},
354                 categorycode => $borr->{'categorycode'},
355                 streetaddress => $borr->{'streetaddress'},
356                 city => $borr->{'city'},
357                 phone => $borr->{'phone'},
358                 cardnumber => $borr->{'cardnumber'},
359 #End code by Christina Lee
360                 question => $question,
361                 barcode => $barcode,
362                 questionnumber => $questionnumber,
363                 dayoptions => $dayoptions,
364                 monthoptions => $monthoptions,
365                 yearoptions => $yearoptions,
366                 stickyduedate => $stickyduedate,
367                 rejected => $rejected,
368                 message => $message,
369                 CGIselectborrower => $CGIselectborrower,
370                 amountold => $amountold,
371                 todayissues => \@realtodayissues,
372                 previssues => \@realprevissues,
373                 responseloop => \@responsearray,
374                  month=>$month,
375                  day=>$day,
376                  year=>$year
377                  
378         );
379
380 if ($branchcookie) {
381     $cookie=[$cookie, $branchcookie, $printercookie];
382 }
383
384 output_html_with_http_headers $query, $cookie, $template->output;
385
386 ####################################################################
387 # Extra subroutines,,,
388
389 sub cuecatbarcodedecode {
390     my ($barcode) = @_;
391     chomp($barcode);
392     my @fields = split(/\./,$barcode);
393     my @results = map(decode($_), @fields[1..$#fields]);
394     if ($#results == 2){
395         return $results[2];
396     } else {
397         return $barcode;
398     }
399 }
400
401 sub fixdate {
402     my ($year, $month, $day) = @_;
403     my $invalidduedate;
404     my $date;
405     if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
406         $env{'datedue'}='';
407     } else {
408         
409 # FIXME - Can we set two flags here, one that says 'invalidduedate', so that 
410 # the template can check for it, and then one for a particular message?
411 # Ex: <TMPL_IF NAME="invalidduedate">  <TMPL_IF NAME="daysinFeb">
412 # Invalid Due Date Specified. Book was not issued.  Never that many days
413 # in February! </TMPL_IF> </TMPL_IF>
414
415         if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
416             $invalidduedate="Invalid Due Date Specified. Book was not issued.<p>\n";
417         } else {
418             if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
419                 $invalidduedate = "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
420             } elsif (($day > 29) && ($month == 2)) {
421                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
422             } elsif (($month == 2) && ($day > 28) && (($year%4) && ((!($year%100) || ($year%400))))) {
423                 $invalidduedate="Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
424             } else {
425                 $date="$year-$month-$day";
426             }
427         }
428     }
429     return ($date, $invalidduedate);
430 }
431
432
433 sub patrontable {
434     my ($borrower) = @_;
435     my $flags = $borrower->{'flags'};
436     my $flaginfotable='';
437     my $flaginfotext;
438     #my $flaginfotext='';
439     my $flag;
440     my $color='';
441     foreach $flag (sort keys %$flags) {
442         warn $flag;
443 #       my @itemswaiting='';
444         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
445         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
446         if ($flags->{$flag}->{'noissues'}) {
447                 $template->param(
448                         noissues => 'true',
449                         color => $color,
450                          );
451                 if ($flag eq 'GNA'){
452                         $template->param(
453                                 gna => 'true'
454                                 );
455                         }
456                 if ($flag eq 'LOST'){
457                         $template->param(
458                                 lost => 'true'
459                         );
460                         }
461                 if ($flag eq 'DBARRED'){
462                         $template->param(
463                                 dbarred => 'true'
464                         );
465                         }
466                 if ($flag eq 'CHARGES') {
467                         $template->param(
468                                 charges => 'true',
469                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
470                                  );
471                 }
472         } else {
473                  if ($flag eq 'CHARGES') {
474                         $template->param(
475                                 charges => 'true',
476                                 chargesmsg => $flags->{'CHARGES'}->{'message'}
477                          );
478                 }
479                 if ($flag eq 'WAITING') {
480                         my $items=$flags->{$flag}->{'itemlist'};
481                         my @itemswaiting;
482                         foreach my $item (@$items) {
483                         my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
484                         $iteminformation->{'branchname'} = $branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
485                         push @itemswaiting, $iteminformation;
486                         }
487                         $template->param(
488                                 waiting => 'true',
489                                 waitingmsg => $flags->{'WAITING'}->{'message'},
490                                 itemswaiting => \@itemswaiting,
491                                  );
492                 }
493                 if ($flag eq 'ODUES') {
494                         $template->param(
495                                 odues => 'true',
496                                 oduesmsg => $flags->{'ODUES'}->{'message'}
497                                  );
498
499                         my $items=$flags->{$flag}->{'itemlist'};
500                         my $currentcolor=$color;
501                         {
502                         my $color=$currentcolor;
503                             my @itemswaiting;
504                         foreach my $item (@$items) {
505                                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
506                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
507                                 push @itemswaiting, $iteminformation;
508                         }
509                         }
510                         if ($query->param('module') ne 'returns'){
511                                 $template->param( nonreturns => 'true' );
512                         }
513                 }
514                 if ($flag eq 'NOTES') {
515                         $template->param(
516                                 notes => 'true',
517                                 notesmsg => $flags->{'NOTES'}->{'message'}
518                                  );
519                 }
520         }
521     }
522     return($patrontable, $flaginfotext);
523 }
524
525
526 # FIXME - This clashes with &C4::Print::printslip
527 sub printslip {
528     my ($env,$borrowernumber)=@_;
529     my ($borrower, $flags) = getpatroninformation($env,$borrowernumber,0);
530     $env->{'todaysissues'}=1;
531     my ($borrowerissues) = currentissues($env, $borrower);
532     $env->{'nottodaysissues'}=1;
533     $env->{'todaysissues'}=0;
534     my ($borroweriss2)=currentissues($env, $borrower);
535     $env->{'nottodaysissues'}=0;
536     my $i=0;
537     my @issues;
538     foreach (sort {$a <=> $b} keys %$borrowerissues) {
539         $issues[$i]=$borrowerissues->{$_};
540         my $dd=$issues[$i]->{'date_due'};
541         #convert to nz style dates
542         #this should be set with some kinda config variable
543         my @tempdate=split(/-/,$dd);
544         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
545         $i++;
546     }
547     foreach (sort {$a <=> $b} keys %$borroweriss2) {
548         $issues[$i]=$borroweriss2->{$_};
549         my $dd=$issues[$i]->{'date_due'};
550         #convert to nz style dates
551         #this should be set with some kinda config variable
552         my @tempdate=split(/-/,$dd);
553         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
554         $i++;
555     }
556     remoteprint($env,\@issues,$borrower);
557 }
558
559 # Begin code added by Christina Lee
560 sub set_duedate
561 {
562   my $loanlength;
563
564   my $dbh = C4::Context->dbh;
565   my $sth = $dbh->prepare ("select loanlength from biblioitems, biblio,itemtypes, items where barcode = ? and biblio.biblionumber = biblioitems.biblionumber and biblioitems.biblionumber = items.biblionumber and biblioitems.itemtype=itemtypes.itemtype;"); 
566   $sth->execute($barc);
567   while (my @val = $sth->fetchrow_array())
568   {
569     $loanlength = @val[0];
570   }
571   (my $s, my $min, my $hr, my $mday, my $mo, my $year, my $wday, my $yday) = localtime(time + $loanlength * 86400);
572
573   #adjust month and date for output
574   $year = $year - 100;
575   $mo = $mo + 1;
576
577   return ($year, $mo, $mday);
578 }
579
580 sub get_due_date
581 {
582  my $duedate;
583
584  my $dbh = C4::Context->dbh;
585
586
587 }
588
589 # End code added by Christina Lee
590
591 # Local Variables:
592 # tab-width: 8
593 # End:
594