Changed the sorting of the hash of items on issue. To sort numerically, (so
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 use CGI qw/:standard/;
4 use C4::Circulation::Circ2;
5 use C4::Output;
6 use C4::Print;
7 use DBI;
8
9
10 my %env;
11 my $headerbackgroundcolor='#99cc33';
12 my $circbackgroundcolor='#ffffcc';
13 my $circbackgroundcolor='white';
14 my $linecolor1='#ffffcc';
15 my $linecolor2='white';
16 my $backgroundimage="/images/background-mem.gif";
17 my $query=new CGI;
18 my $branches=getbranches(\%env);
19 my $printers=getprinters(\%env);
20 my $branch=$query->param('branch');
21 my $printer=$query->param('printer');
22 #print $query->header;
23 ($branch) || ($branch=$query->cookie('branch'));
24 ($printer) || ($printer=$query->cookie('printer'));
25 my ($oldbranch, $oldprinter);
26 if ($query->param('selectnewbranchprinter')) {
27     $oldbranch=$branch;
28     $oldprinter=$printer;
29     $branch='';
30     $printer='';
31 }
32 $env{'branchcode'}=$branch;
33 $env{'printer'}=$printer;
34 $env{'queue'}=$printer;
35 my $branchcount=0;
36 my $printercount=0;
37 my $branchoptions;
38 my $printeroptions;
39 foreach (keys %$branches) {
40     (next) unless ($_);
41     (next) if (/^TR$/);
42     $branchcount++;
43     my $selected='';
44     ($selected='selected') if ($_ eq $oldbranch);
45     $branchoptions.="<option value=$_ $selected>$branches->{$_}->{'branchname'}\n";
46 }
47 foreach (keys %$printers) {
48     (next) unless ($_);
49     $printercount++;
50     my $selected='';
51     ($selected='selected') if ($_ eq $oldprinter);
52     $printeroptions.="<option value=$_ $selected>$printers->{$_}->{'printername'}\n";
53 }
54 if ($printercount==1) {
55     ($printer)=keys %$printers;
56 }
57 if ($branchcount==1) {
58     ($branch)=keys %$branches;
59 }
60
61
62 my $branchname='';
63 my $printername='';
64 if ($branch && $printer) {
65     $branchname=$branches->{$branch}->{'branchname'};
66     $printername=$printers->{$printer}->{'printername'};
67 }
68
69
70 my $branchcookie=$query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
71 my $printercookie=$query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
72
73 print $query->header(-type=>'text/html',-expires=>'now', -cookie=>[$branchcookie,$printercookie]);
74 #print $query->dump;
75 print startpage();
76 #print startmenu('circulation');
77 my @inp=startmenu('circulation');
78 if ($query->param('module') eq 'issues' && $query->param('barcode') eq '' && $query->param('charges') eq 'yes'){
79  my $count=@inp;
80  for (my $i=0;$i<$count;$i++){
81   my $bornum=$query->param('borrnumber');
82   $inp[$i]=~ s/onLoad=focusinput\(\)/onLoad=focusinput\(\)\;messenger\(\"\/cgi-bin\/koha\/pay.pl?bornum=$bornum\"\)\;window1.focus\(\)/;
83  }
84 }
85 print @inp;
86
87 print <<EOF
88 <script language="javascript" type="text/javascript">
89 <!--
90 function messenger(url){
91    window1=window.open(url,"window1","height=700,width=600,left=150,top=50,350,screenY=50");
92 }
93 //-->
94 </script>
95 EOF
96 ;
97 if ($printer && $branch) {
98
99     SWITCH: {
100         if ($query->param('module') eq 'issues') { issues(); last SWITCH; }
101         if ($query->param('module') eq 'returns') { returns(); last SWITCH; }
102         issues();
103     }
104 } else {
105     my ($printerform, $branchform);
106     if ($printercount>1) {
107         $printerform=<<"EOF";
108
109 <table border=0 cellspacing=0 cellpadding=5>
110 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Choose a Printer</font></td></tr>
111 <tr><td>
112 <select name=printer>
113 $printeroptions
114 </select>
115 </td></tr>
116 </table>
117 EOF
118     } else {
119         my ($printer) = keys %$printers;
120         $printerform=<<"EOF";
121         <input type=hidden name=printer value=$printer>
122 EOF
123     }
124
125     if ($branchcount>1) {
126         $branchform=<<"EOF";
127 <table border=0 cellpadding=5 cellspacing=0>
128 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Choose a Branch</font></td></tr>
129 <tr><td>
130 <select name=branch>
131 $branchoptions
132 </select>
133 </td></tr>
134 </table>
135 EOF
136     }
137     print << "EOF";
138     <center>
139     Select a branch and a printer
140     <form method=get>
141     <table border=0>
142     <tr><td>
143     $branchform
144     </td><td>
145     $printerform
146     </td></tr>
147     </table>
148     <input type=submit>
149     </form>
150 EOF
151 }
152
153
154 print endmenu('circulation');
155 print endpage();
156 sub default {
157 print << "EOF";
158 <a href=circulation.pl?module=issues&branch=$branch&printer=$printer>Issues</a>
159 <a href=circulation.pl?module=returns&branch=$branch&printer=$printer>Returns</a>
160 EOF
161 }
162
163
164 sub decode {
165     ($encoded) = @_;
166     $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
167     @s = map { index($seq,$_); } split(//,$encoded);
168     $l = ($#s+1) % 4;
169     if ($l)
170     {
171         if ($l == 1)
172         {
173             print "Error!";
174             return;
175         }
176         $l = 4-$l;
177         $#s += $l;
178     }
179     $r = '';
180     while ($#s >= 0)
181     {
182         $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
183         $r .=chr(($n >> 16) ^ 67) .
184              chr(($n >> 8 & 255) ^ 67) .
185              chr(($n & 255) ^ 67);
186         @s = @s[4..$#s];
187     }
188     $r = substr($r,0,length($r)-$l);
189     return $r;
190 }
191
192 sub returns {
193     my %returneditems;
194     print << "EOF";
195     <table border=0 cellpadding=10 cellspacing=0 cellpadding=5><tr><th background=$backgroundimage>Circulation - Returns</th></tr><tr><td bgcolor=$circbackgroundcolor align=center>
196 EOF
197     foreach ($query->param) {
198         (next) unless (/ri-(\d*)/);
199         my $counter=$1;
200         (next) if ($counter>20);
201         my $barcode=$query->param("ri-$counter");
202       # I'm not POSITIVE this decode belongs here - can't tell what causes it to run.
203       # decode cuecat
204       chomp($barcode);
205       @fields = split(/\./,$barcode);
206       @results = map(decode($_), @fields[1..$#fields]);
207       if ($#results == 2)
208       {
209         $barcode=$results[2];
210       }
211       # end decode
212         my $duedate=$query->param("dd-$counter");
213         my $borrowernumber=$query->param("bn-$counter");
214         $counter++;
215         $returneditems{$counter}=$barcode;
216         $riduedate{$counter}=$duedate;
217         $riborrowernumber{$counter}=$borrowernumber;
218         $ritext.="<input type=hidden name=ri-$counter value=$barcode>\n";
219         $ritext.="<input type=hidden name=dd-$counter value=$duedate>\n";
220         $ritext.="<input type=hidden name=bn-$counter value=$borrowernumber>\n";
221     }
222     if (my $barcode=$query->param('barcode')) {
223       # decode cuecat
224       chomp($barcode);
225       @fields = split(/\./,$barcode);
226       @results = map(decode($_), @fields[1..$#fields]);
227       if ($#results == 2)
228       {
229         $barcode=$results[2];
230       }
231       # end decode
232         $ritext.="<input type=hidden name=ri-0 value=$barcode>\n";
233         $returneditems{0}=$barcode;
234     }
235         
236     my $barcodeentrytext= << "EOF";
237     <form method=post action=/cgi-bin/koha/circ/circulation.pl>
238     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
239         <tr><td colspan=2 bgcolor=$headerbackgroundcolor align=center background=$backgroundimage><font color=black><b>Enter Book Barcode</b></font></td></tr>
240         <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
241     </table>
242     <input type=hidden name=module value=returns>
243     <input type=hidden name=branch value=$branch>
244     <input type=hidden name=printer value=$printer>
245     $ritext
246 EOF
247     if ((my $barcode=$query->param('barcode')) || (%returneditems)) {
248       # decode cuecat
249       chomp($barcode);
250       @fields = split(/\./,$barcode);
251       @results = map(decode($_), @fields[1..$#fields]);
252       if ($#results == 2)
253       {
254         $barcode=$results[2];
255       }
256       # end decode
257         my ($iteminformation, $borrower, $messages, $overduecharge) = returnbook(\%env, $barcode);
258         
259         (my $nosuchitem=1) unless ($iteminformation);
260         $riborrowernumber{0}=$borrower->{'borrowernumber'};
261         $riduedate{0}=$iteminformation->{'date_due'};
262         $barcodeentrytext.= "<input type=hidden name=dd-0 value=$iteminformation->{'date_due'}>\n";
263         $barcodeentrytext.= "<input type=hidden name=bn-0 value=$borrower->{'borrowernumber'}>\n";
264         my @datearr = localtime(time());
265         my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
266         my $itemtable=<<"EOF";
267 <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
268 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Returned Item Information</font></th></tr>
269 <tr><td>
270 Title: $iteminformation->{'title'}<br>
271 <!--Hlt decided they dont want these showing, uncoment the html to make it work
272
273 Author: $iteminformation->{'author'}<br>
274 Barcode: <a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick="openWindow(this, 'Item', 480, 640)">$iteminformation->{'barcode'}</a><br>
275 Date Due: $iteminformation->{'date_due'}-->
276 </td></tr>
277 </table>
278 EOF
279         if ($messages) {
280             my $messagetext='';
281             foreach (@$messages) {
282                 $messagetext.="$_<p>\n";
283             }
284             print << "EOF";
285 <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
286 <tr><th bgcolor=$headerbackgroundcolor $background=$backgroundimage><font color=black>Messages</font></th></tr>
287 <tr><td>
288 $messagetext
289 </td></tr>
290 </table>
291 <p>
292 EOF
293         }
294         if (($nosuchitem) && ($barcode)) {
295             print << "EOF";
296             <table border=0 cellpadding=1>
297             <tr>
298             <td valign=top>
299             $barcodeentrytext
300             </td>
301             <td valign=top>
302             <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
303             <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Error</font></th></tr>
304             <tr><td>
305             <table border=0 cellpadding=5>
306             <tr><td>
307             $barcode is not a valid barcode.
308             </td></tr>
309             </table>
310             </td></tr>
311             </table>
312             </td>
313             </tr>
314             </table>
315 EOF
316         } elsif ($nosuchitem) {
317             print << "EOF";
318             <table border=0 cellpadding=1>
319             <tr>
320             <td valign=top>
321             $barcodeentrytext
322             </td>
323             </tr>
324             </table>
325 EOF
326         } else {
327             if ($borrower->{'borrowernumber'}) {
328                 my ($patrontable, $flaginfotext) = patrontable($borrower);
329                 print << "EOF";
330                 <table border=0 cellpadding=5>
331                     <tr>
332                         <td>
333                         $barcodeentrytext
334                         </td>
335                         <td align=center valign=top>
336                         $itemtable
337                         </td>
338                     </tr>
339                     <tr>
340                         <td valign=top align=center colspan=2>
341                         <table border=0>
342                         <tr><td valign=top>
343                         $patrontable
344                         </td>
345                         <td valign=top>
346                         $flaginfotext
347                         </td>
348                         </tr>
349                         </table>
350                         </td>
351                     </tr>
352                 </table>
353                 <br>
354 EOF
355             } else {
356                 print << "EOF";
357                 <table border=0 cellpadding=5>
358                 <tr><td>
359                 $barcodeentrytext
360                 </td>
361                 <td>
362
363                 <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
364                 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Error</font></th></tr>
365                 <tr><td>
366                 <table border=0 cellpadding=5>
367                 <tr><td>
368                 $iteminformation->{'title'} by $iteminformation->{'author'} was not loaned out.
369                 </td></tr>
370                 </table>
371                 </td></tr>
372                 </table>
373                 </td>
374                 </tr>
375                 </table>
376 EOF
377             }
378         }
379         print << "EOF";
380         <p>
381         <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
382         <tr><th colspan=6 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Returned Items</font></th></tr>
383         <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Type</th><th>Borrower</th></tr>
384 EOF
385         my $color='';
386         #set up so only the lat 8 returned items display (make for faster loading pages)
387         my $count=0;
388         foreach (sort {$a <=> $b} keys %returneditems) {
389           if ($count < 8){
390             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
391             my $barcode=$returneditems{$_};
392             my $duedate=$riduedate{$_};
393             my @datearr = localtime(time());
394             ###
395             # convert to nz date format
396             my @tempdate=split(/-/,$duedate);
397             $duedate="$tempdate[2]/$tempdate[1]/$tempdate[0]";
398             ####
399             my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
400             my $overduetext="$duedate";
401             ($overduetext="<font color=red>$duedate</font>") if ($duedate lt $todaysdate);
402             ($duedate) || ($overduetext="<img src=/koha/images/blackdot.gif>");
403             my $borrowernumber=$riborrowernumber{$_};
404             my ($borrower) = getpatroninformation(\%env,$borrowernumber,0);
405             my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
406             print "<tr><td bgcolor=$color>$overduetext</td><td bgcolor=$color align=center><a href=/cgi-bin/koha/detail.pl?bib=$iteminformation->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$barcode</a></td><td bgcolor=$color>$iteminformation->{'title'}</td><td bgcolor=$color>$iteminformation->{'author'}</td><td bgcolor=$color align=center>$iteminformation->{'itemtype'}</td><td bgcolor=$color><img src=/koha/images/blackdot.gif><a href=/cgi-bin/koha/moremember.pl?bornum=$borrower->{'borrowernumber'} onClick=\"openWindow(this,'Member', 480, 640)\">$borrower->{'cardnumber'}</a> $borrower->{'firstname'} $borrower->{'surname'}</td></tr>\n";
407           } else {
408             last
409           }
410           $count++;
411         }
412         print "</table>\n";
413     } else {
414         print $barcodeentrytext;
415     }
416     print "</form>\n";
417     print "</td></tr></table>\n";
418 }
419
420 sub issues {
421     my ($noheader) = shift;
422     my $print=$query->param('print');
423     my $borrowernumber=$query->param('borrnumber');    
424     my $barcode=$query->param('barcode');
425     if ($barcode eq ''){
426       $print='yes';     
427     } elsif ($barcode eq ' '){
428       $query->param('barcode','');
429       $barcode='';
430     }
431     if ($print eq 'yes' && $borrowernumber ne ''){
432       my ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
433       $env{'todaysissues'}=1;
434       my ($borrowerissues) = currentissues(\%env, $borrower);
435       $env{'nottodaysissues'}=1;
436       $env{'todaysissues'}=0;
437       my ($borroweriss2)=currentissues(\%env, $borrower);
438       $env{'nottodaysissues'}=0;
439       my $i=0;
440       my @issues;
441       foreach (sort keys %$borrowerissues) {
442         $issues[$i]=$borrowerissues->{$_};
443         my $dd=$issues[$i]->{'date_due'};
444         #convert to nz style dates
445         #this should be set with some kinda config variable     
446         my @tempdate=split(/-/,$dd);
447         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
448         $i++;
449       }
450       foreach (sort keys %$borroweriss2) {
451         $issues[$i]=$borroweriss2->{$_};
452         my $dd=$issues[$i]->{'date_due'};
453         #convert to nz style dates
454         #this should be set with some kinda config variable     
455         my @tempdate=split(/-/,$dd);
456         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
457         $i++;
458       }
459       remoteprint(\%env,\@issues,$borrower);
460       $query->param('borrnumber','')
461     }
462     unless ($noheader) {
463       print <<EOF
464       <p align=right>
465       <FONT SIZE=2  face="arial, helvetica">
466       <a href=circulation.pl?borrnumber=$borrowernumber&module=issues&branch=$branch&printer=$printer&print>Next Borrower</a> ||
467       <a href=circulation.pl?module=returns&branch=$branch&printer=$printer>Returns</a></font><p>
468        
469         
470         </p>
471 EOF
472     }
473     if (my $borrnumber=$query->param('borrnumber')) {
474         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
475         my $year=$query->param('year');
476         my $month=$query->param('month');
477         my $day=$query->param('day');
478         if (my $barcode=$query->param('barcode')) {
479           # decode cuecat
480           chomp($barcode);
481           @fields = split(/\./,$barcode);
482           @results = map(decode($_), @fields[1..$#fields]);
483           if ($#results == 2)
484           {
485             $barcode=$results[2];
486           }
487           # end decode
488             my $invalidduedate=0;
489             $env{'datedue'}='';
490             if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
491                 $env{'datedue'}='';
492             } else {
493                 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
494                     print "Invalid Due Date Specified. Book was not issued.<p>\n";
495                     $invalidduedate=1;
496                 } else {
497                     if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
498                         print "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
499                         $invalidduedate=1;
500                     } elsif (($day>29) && ($month==2)) {
501                         print "Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
502                         $invalidduedate=1;
503                     } elsif (($month==2) && ($day>28) && (($year%4) && ((!($year%100) || ($year%400))))) {
504                         print "Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
505                         $invalidduedate=1;
506                     } else {
507                         $env{'datedue'}="$year-$month-$day";
508                     }
509                 }
510             }
511             my %responses;
512             foreach (sort $query->param) {
513                 if ($_ =~ /response-(\d*)/) {
514                     $responses{$1}=$query->param($_);
515                 }
516             }
517             if (my $qnumber=$query->param('questionnumber')) {
518                 $responses{$qnumber}=$query->param('answer');
519             }
520             unless ($invalidduedate) {
521                 my @time=localtime(time);
522                 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
523                 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message) = issuebook(\%env, $borrower, $barcode, \%responses,$date);
524                 unless ($iteminformation) {
525                     print << "EOF";
526                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
527                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Error</font></th></tr>
528                     <tr><td>
529                     <table border=0 cellpadding=5>
530                     <tr><td>
531                     $barcode is not a valid barcode.
532                     </td></tr>
533                     </table>
534 EOF
535                 }
536                 if ($rejected) {
537                     if ($rejected == -1) {
538                     } else {
539                         print << "EOF"
540                         <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
541                         <tr><th><font color=black size=6>Error Issuing Book</font></th></tr>
542                         <tr><td><font color=red size=6>$rejected</font></td></tr>
543                         </table>
544                         <br>
545 EOF
546                     }
547                 }
548                 my $responsesform='';
549                 foreach (keys %responses) {
550                     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
551                 }
552                 if ($question) {
553                     my $stickyduedate=$query->param('stickyduedate');
554                     print << "EOF";
555                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
556                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Issuing Question</b></font></td></tr>
557                     <tr><td>
558                     <table border=0 cellpadding=10>
559                     <tr><td>
560                     Attempting to issue $iteminformation->{'title'} by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
561                     <p>
562                     $question
563                     </td></tr>
564                     </table>
565                     </td></tr>
566
567                     <tr><td align=center>
568                     <table border=0>
569                     <tr><td>
570                     <form method=get>
571                     <input type=hidden name=module value=issues>
572                     <input type=hidden name=borrnumber value=$borrnumber>
573                     <input type=hidden name=barcode value=$barcode>
574                     <input type=hidden name=questionnumber value=$questionnumber>
575                     <input type=hidden name=day value=$day>
576                     <input type=hidden name=month value=$month>
577                     <input type=hidden name=year value=$year>
578                     <input type=hidden name=stickyduedate value=$stickyduedate>
579                     <input type=hidden name=branch value=$branch>
580                     <input type=hidden name=printer value=$printer>
581                     $responsesform
582                     <input type=hidden name=answer value=Y>
583                     <input type=submit value=Yes>
584                     </form>
585                     </td>
586                     <td>
587                     <form method=get>
588                     <input type=hidden name=module value=issues>
589                     <input type=hidden name=borrnumber value=$borrnumber>
590                     <input type=hidden name=barcode value=$barcode>
591                     <input type=hidden name=questionnumber value=$questionnumber>
592                     <input type=hidden name=day value=$day>
593                     <input type=hidden name=month value=$month>
594                     <input type=hidden name=year value=$year>
595                     <input type=hidden name=stickyduedate value=$stickyduedate>
596                     <input type=hidden name=branch value=$branch>
597                     <input type=hidden name=printer value=$printer>
598                     $responsesform
599                     <input type=hidden name=answer value=N>
600                     <input type=submit value=No>
601                     </form>
602                     </td>
603                     </tr>
604                     </table>
605                     </td></tr>
606                     </table>
607                     </td></tr>
608                     </table>
609 EOF
610                     return;
611                 }
612                 if ($message) {
613                     print << "EOF";
614                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
615                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Message</font></th></tr>
616                     <tr><td>$message</td></tr>
617                     </table>
618                     <p>
619 EOF
620                 }
621             }
622         }
623         my $issueid=$query->param('issueid');
624         ($issueid) || ($issueid=int(rand()*1000000000));
625         my $flag='';
626         my $flagtext='';
627         my $flaginfotext='';
628         foreach $flag (sort keys %$flags) {
629             if ($flags->{$flag}->{'noissues'}) {
630                 $flagtext.="<font color=red>$flag</font> ";
631             } else {
632                 $flagtext.="$flag ";
633             }
634             $flags->{$flag}->{'message'}=~s/\n/<br>/g;
635             if ($flags->{$flag}->{'noissues'}) {
636                 $flaginfotext.="<tr><td bgcolor=red valign=top><font color=black><b>$flag</b></font></td><td bgcolor=red><font color=black><b>$flags->{$flag}->{'message'}</b></font></td></tr>\n";
637             } else {
638                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
639             }
640         }
641         if ($flaginfotext) {
642             $flaginfotext="<table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd><tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black>Patron Flags</font></th></tr>$flaginfotext</table>\n";
643         }
644         $env{'nottodaysissues'}=1;
645         my ($borrowerissues) = currentissues(\%env, $borrower);
646         $env{'nottodaysissues'}=0;
647         $env{'todaysissues'}=1;
648         my ($today) = currentissues(\%env, $borrower);
649         $env{'todaysissues'}=0;
650         my $previssues='';
651         my @datearr = localtime(time());
652         my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
653         my $color='';
654         foreach (sort keys %$borrowerissues) {
655             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
656             my $bookissue=$borrowerissues->{$_};
657             my $bgcolor='';
658             my $datedue=$bookissue->{'date_due'};
659             my $dd=$bookissue->{'date_due'};
660             #convert to nz style dates
661             #this should be set with some kinda config variable
662             
663             my @tempdate=split(/-/,$dd);
664             $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
665             
666             #####
667             $datedue=~s/-//g;
668             if ($datedue < $todaysdate) {
669                 $dd="<font color=red>$dd</font>\n";
670             }
671             $previssues.="<tr><td bgcolor=$color align=center>$dd</td><td bgcolor=$color align=center><a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td><td bgcolor=$color>$bookissue->{'title'}</td><td bgcolor=$color>$bookissue->{'author'}</td><td bgcolor=$color align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>\n";
672         }
673         my $todaysissues='';
674         $color='';
675         foreach (sort keys %$today) {
676             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
677             my $bookissue=$today->{$_};
678             $todaysissues.="<tr><td bgcolor=$color align=center>$bookissue->{'date_due'}</td><td bgcolor=$color align=center><a href=/cgi-bin/koha/detail.pl?bib=$bookissue->{'biblionumber'}&type=intra onClick=\"openWindow(this, 'Item', 480, 640)\">$bookissue->{'barcode'}</a></td><td bgcolor=$color>$bookissue->{'title'}</td><td bgcolor=$color>$bookissue->{'author'}</td><td bgcolor=$color align=center>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>\n";
679         }
680         for ($i=1; $i<32; $i++) {
681             my $selected='';
682             if (($query->param('stickyduedate')) && ($day==$i)) {
683                 $selected='selected';
684             }
685             $dayoptions.="<option value=$i $selected>$i";
686         }
687         my $counter=1;
688         foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
689             my $selected='';
690             if (($query->param('stickyduedate')) && ($month==$counter)) {
691                 $selected='selected';
692             }
693             $monthoptions.="<option value=$counter $selected>$_";
694             $counter++;
695         }
696         for ($i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
697             my $selected='';
698             if (($query->param('stickyduedate')) && ($year==$i)) {
699                 $selected='selected';
700             }
701             $yearoptions.="<option value=$i $selected>$i";
702         }
703
704         my $selected='';
705         ($query->param('stickyduedate')) && ($selected='checked');
706         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
707         my ($patrontable, $flaginfotable) = patrontable($borrower);
708         print << "EOF";
709         $patrontable
710         $flaginfotable
711         <FONT SIZE=6><em>Issues</em></FONT> &nbsp; &nbsp;<br> <a href=circulation.pl?selectnewbranchprinter=1><b>Branch:</b></a> $branch,
712          <a href=circulation.pl?selectnewbranchprinter=1><b>Printer:</b></a> $printer<P>
713          <p>
714         <form method=get>
715     <table border=1 cellpadding=5>
716     <tr>
717         <td align=center valign=top>
718             <table border=0 cellspacing=0 cellpadding=5>
719                 <tr><th align=center background=$backgroundimage><font color=black><b>Enter Book Barcode</b></font></th></tr>
720                 <tr><td align=center>
721                 <table border=0>
722                 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></tr>
723                 <tr><td colspan=3 align=center>
724                 <table border=0 cellpadding=0 cellspacing=0>
725                 <tr><td>
726                 <select name=day><option value=0>Day$dayoptions</select>
727                 </td><td>
728                 <select name=month><option value=0>Month$monthoptions</select>
729                 </td><td>
730                 <select name=year><option value=0>Year$yearoptions</select>
731                 </td></tr>
732                 </table>
733                 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
734                 </td></tr>
735               </table>
736         <input type=hidden name=module value=issues>
737         <input type=hidden name=borrnumber value=$borrnumber>
738         <input type=hidden name=branch value=$branch>
739         <input type=hidden name=printer value=$printer>
740         
741 EOF
742 ;
743
744   if ($flags->{'CHARGES'}){
745             print "<input type=hidden name=charges value=yes>";
746   }
747     my $amountold=$flags->{'CHARGES'}->{'message'};
748   my @temp=split(/\$/,$amountold);
749   $amountold=$temp[1];
750   print "<input type=hidden name=oldamount value=$amountold>";
751 print <<EOF
752 </form>
753         </td>
754
755
756     </tr>
757     </table>
758     </table>
759     
760     <p clear=all>
761 <!-- issues tables-->
762         <table border=1 cellpadding=5 cellspacing=0 width=90%>
763         <td colspan=2 align=center>
764         <table border=0 cellpadding=5 cellspacing=0 width=100% > 
765             <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Issues Today</b></font></th></tr>
766             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
767             $todaysissues
768         </table>
769         </td>
770     </tr>
771
772     <tr>
773         <td colspan=2 align=center>
774         <table border=0 cellpadding=5 cellspacing=0 width=100%>
775             <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Previous Issues</b></font></th></tr>
776             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
777             $previssues
778         </table>
779         </td>
780     </tr>
781 </table>
782 </td></tr></table>
783 <p>
784 EOF
785     } else {
786         if (my $findborrower=$query->param('findborrower')) {
787             my ($borrowers, $flags) = findborrower(\%env, $findborrower);
788             my @borrowers=@$borrowers;
789             if ($#borrowers == -1) {
790                 $query->param('findborrower', '');
791                 print "No borrower matched '$findborrower'<p>\n";
792                 issues(1);
793                 return;
794             }
795             if ($#borrowers == 0) {
796                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
797                 $query->param('barcode',' ');
798                 issues(1);
799                 return;
800             } else {
801                 print "<form method=get>\n";
802                 print "<input type=hidden name=module value=issues>\n";
803                 print "<input type=hidden name=branch value=$branch>\n";
804                 print "<input type=hidden name=printer value=$printer>\n";
805                 print "<input type=hidden name=barcode value=\" \">\n";
806                 print "<table border=0 cellspacing=0 cellpadding=5 bgcolor=#dddddd>";
807                 print "<tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Select a borrower</b></font></th></tr>\n";
808                 print "<tr><td align=center>\n";
809                 print "<select name=borrnumber size=7>\n";
810                 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowers) {
811                     print "<option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})\n";
812                 }
813                 print "</select><br>";
814                 print "<input type=submit>\n";
815                 print "</td></tr></table>\n";
816                 print "</td></tr></table>\n";
817             }
818         } else {
819             print << "EOF";
820 <form method=get>
821 <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
822 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
823 <tr><td><input name=findborrower></td></tr>
824 </table>
825 <input type=hidden name=module value=issues>
826 <input type=hidden name=branch value=$branch>
827 <input type=hidden name=printer value=$printer>
828 <input type=hidden name=barcode value=" ">
829 </form>
830 </td></tr></table>
831 EOF
832         }
833     }
834 }
835
836
837 sub patrontable {
838     my ($borrower) = @_;
839     my $flags=$borrower->{'flags'};
840     my $flagtext='';
841     my $flaginfotable='';
842     my $flaginfotext='';
843     my $flag;
844     foreach $flag (sort keys %$flags) {
845         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
846         if ($flags->{$flag}->{'noissues'}) {
847             $flagtext.="<font color=red>$flag</font> ";
848         } else {
849             $flagtext.="$flag ";
850         }
851         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
852         if ($flags->{$flag}->{'noissues'}) {
853             if ($flag eq 'CHARGES') {
854                 $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";
855             } else {
856                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
857             }
858         } else {
859             if ($flag eq 'CHARGES') {
860                 $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";
861             } elsif ($flag eq 'WAITING') {
862                 my $itemswaiting='';
863                 my $items=$flags->{$flag}->{'itemlist'};
864                 foreach $item (@$items) {
865                     my ($iteminformation) = getiteminformation($env, $item->{'itemnumber'}, 0);
866                     $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";
867                 }
868                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
869             } elsif ($flag eq 'ODUES') {
870                 my $items=$flags->{$flag}->{'itemlist'};
871                 my $itemswaiting="<table border=0 cellspacing=0 cellpadding=2>\n";
872                 my $currentcolor=$color;
873                 {
874                     my $color=$currentcolor;
875                     foreach $item (@$items) {
876                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
877                         my ($iteminformation) = getiteminformation($env, $item->{'itemnumber'}, 0);
878                         $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";
879                     }
880                 }
881                 $itemswaiting.="</table>\n";
882                 if ($query->param('module') ne 'returns'){
883                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
884                 } else {
885                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n"; 
886                 }
887             } else {
888                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
889             }
890         }
891     }
892     ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
893     $flaginfotext.="</table>";
894     my $patrontable= << "EOF";
895     <table border=1
896     cellpadding=5 cellspacing=0 align=right>
897     <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
898     <tr><td colspan=2>
899     <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>
900 EOF
901     if ($query->param('module') ne 'returns'){ 
902        $patrontable.="$borrower->{'streetaddress'} $borrower->{'city'} Cat:
903        $borrower->{'categorycode'} ";
904      }
905     $patrontable.=<< "EOF";
906     
907     </td></tr>
908
909 EOF
910     return($patrontable, $flaginfotext);
911 }