Completely new. This file handles the issues, and sets branch and printer
[koha.git] / circ / circulationold.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
157 sub default {
158 print << "EOF";
159 <a href=cirulationold.pl?module=issues&branch=$branch&printer=$printer>Issues</a>
160 <a href=cirulationold.pl?module=returns&branch=$branch&printer=$printer>Returns</a>
161 EOF
162 }
163
164
165 sub decode {
166     ($encoded) = @_;
167     $seq = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-';
168     @s = map { index($seq,$_); } split(//,$encoded);
169     $l = ($#s+1) % 4;
170     if ($l)
171     {
172         if ($l == 1)
173         {
174             print "Error!";
175             return;
176         }
177         $l = 4-$l;
178         $#s += $l;
179     }
180     $r = '';
181     while ($#s >= 0)
182     {
183         $n = (($s[0] << 6 | $s[1]) << 6 | $s[2]) << 6 | $s[3];
184         $r .=chr(($n >> 16) ^ 67) .
185              chr(($n >> 8 & 255) ^ 67) .
186              chr(($n & 255) ^ 67);
187         @s = @s[4..$#s];
188     }
189     $r = substr($r,0,length($r)-$l);
190     return $r;
191 }
192
193 sub returns {
194     my %returneditems;
195     print "<FONT SIZE=6><em>Circulation: Returns</em></FONT><br>";
196     foreach ($query->param) {
197         (next) unless (/ri-(\d*)/);
198         my $counter=$1;
199         (next) if ($counter>20);
200         my $barcode=$query->param("ri-$counter");
201       # I'm not POSITIVE this decode belongs here - can't tell what causes it to run.
202       # decode cuecat
203       chomp($barcode);
204       @fields = split(/\./,$barcode);
205       @results = map(decode($_), @fields[1..$#fields]);
206       if ($#results == 2)
207       {
208         $barcode=$results[2];
209       }
210       # end decode
211         my $duedate=$query->param("dd-$counter");
212         my $borrowernumber=$query->param("bn-$counter");
213         $counter++;
214         $returneditems{$counter}=$barcode;
215         $riduedate{$counter}=$duedate;
216         $riborrowernumber{$counter}=$borrowernumber;
217         $ritext.="<input type=hidden name=ri-$counter value=$barcode>\n";
218         $ritext.="<input type=hidden name=dd-$counter value=$duedate>\n";
219         $ritext.="<input type=hidden name=bn-$counter value=$borrowernumber>\n";
220     }
221     if (my $barcode=$query->param('barcode')) {
222       # decode cuecat
223       chomp($barcode);
224       @fields = split(/\./,$barcode);
225       @results = map(decode($_), @fields[1..$#fields]);
226       if ($#results == 2)
227       {
228         $barcode=$results[2];
229       }
230       # end decode
231         $ritext.="<input type=hidden name=ri-0 value=$barcode>\n";
232         $returneditems{0}=$barcode;
233     }
234         
235     my $barcodeentrytext= << "EOF";
236     <form method=post action=/cgi-bin/koha/circ/cirulationold.pl>
237     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
238         <tr><td colspan=2 bgcolor=$headerbackgroundcolor align=center background=$backgroundimage><font color=black><b>Enter Book Barcode</b></font></td></tr>
239         <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
240     </table>
241     <input type=hidden name=module value=returns>
242     <input type=hidden name=branch value=$branch>
243     <input type=hidden name=printer value=$printer>
244     $ritext
245 EOF
246     if ((my $barcode=$query->param('barcode')) || (%returneditems)) {
247       # decode cuecat
248       chomp($barcode);
249       @fields = split(/\./,$barcode);
250       @results = map(decode($_), @fields[1..$#fields]);
251       if ($#results == 2)
252       {
253         $barcode=$results[2];
254       }
255       # end decode
256         my ($iteminformation, $borrower, $messages, $overduecharge) = returnbook(\%env, $barcode);
257         
258         (my $nosuchitem=1) unless ($iteminformation);
259         $riborrowernumber{0}=$borrower->{'borrowernumber'};
260         $riduedate{0}=$iteminformation->{'date_due'};
261         $barcodeentrytext.= "<input type=hidden name=dd-0 value=$iteminformation->{'date_due'}>\n";
262         $barcodeentrytext.= "<input type=hidden name=bn-0 value=$borrower->{'borrowernumber'}>\n";
263         my @datearr = localtime(time());
264         my $todaysdate = (1900+$datearr[5]).'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.
265           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
422     my ($noheader) = shift;
423     my $print=$query->param('print');
424     my $borrowernumber=$query->param('borrnumber');    
425     my $barcode=$query->param('barcode');
426     if ($barcode eq ''){
427       $print='yes';     
428     } elsif ($barcode eq ' '){
429       $query->param('barcode','');
430       $barcode='';
431     }
432     if ($print eq 'yes' && $borrowernumber ne ''){
433       my ($borrower, $flags) = getpatroninformation(\%env,$borrowernumber,0);
434       $env{'todaysissues'}=1;
435       my ($borrowerissues) = currentissues(\%env, $borrower);
436       $env{'nottodaysissues'}=1;
437       $env{'todaysissues'}=0;
438       my ($borroweriss2)=currentissues(\%env, $borrower);
439       $env{'nottodaysissues'}=0;
440       my $i=0;
441       my @issues;
442       foreach (sort keys %$borrowerissues) {
443         $issues[$i]=$borrowerissues->{$_};
444         my $dd=$issues[$i]->{'date_due'};
445         #convert to nz style dates
446         #this should be set with some kinda config variable     
447         my @tempdate=split(/-/,$dd);
448         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
449         $i++;
450       }
451       foreach (sort keys %$borroweriss2) {
452         $issues[$i]=$borroweriss2->{$_};
453         my $dd=$issues[$i]->{'date_due'};
454         #convert to nz style dates
455         #this should be set with some kinda config variable     
456         my @tempdate=split(/-/,$dd);
457         $issues[$i]->{'date_due'}="$tempdate[2]/$tempdate[1]/$tempdate[0]";
458         $i++;
459       }
460       remoteprint(\%env,\@issues,$borrower);
461       $query->param('borrnumber','')
462     }
463     unless ($noheader) {
464       print <<EOF
465       <p align=right>
466       <FONT SIZE=2  face="arial, helvetica">
467       <a href=circulationold.pl?borrnumber=$borrowernumber&module=issues&branch=$branch&printer=$printer&print>Next Borrower</a> ||
468       <a href=returns.pl>Returns</a> ||
469       <a href=branchtransfers.pl>Transfers</a></font><p>
470         
471         </p>
472 <FONT SIZE=6><em>Circulation: Issues</em></FONT><br>
473 EOF
474     }
475     if (my $borrnumber=$query->param('borrnumber')) {
476         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
477         my $year=$query->param('year');
478         my $month=$query->param('month');
479         my $day=$query->param('day');
480         if (my $barcode=$query->param('barcode')) {
481           # decode cuecat
482           chomp($barcode);
483           @fields = split(/\./,$barcode);
484           @results = map(decode($_), @fields[1..$#fields]);
485           if ($#results == 2)
486           {
487             $barcode=$results[2];
488           }
489           # end decode
490             my $invalidduedate=0;
491             $env{'datedue'}='';
492             if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
493                 $env{'datedue'}='';
494             } else {
495                 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
496                     print "Invalid Due Date Specified. Book was not issued.<p>\n";
497                     $invalidduedate=1;
498                 } else {
499                     if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
500                         print "Invalid Due Date Specified. Book was not issued. Only 30 days in $month month.<p>\n";
501                         $invalidduedate=1;
502                     } elsif (($day>29) && ($month==2)) {
503                         print "Invalid Due Date Specified. Book was not issued.  Never that many days in February!<p>\n";
504                         $invalidduedate=1;
505                     } elsif (($month==2) && ($day>28) && (($year%4) && ((!($year%100) || ($year%400))))) {
506                         print "Invalid Due Date Specified. Book was not issued.  $year is not a leap year.<p>\n";
507                         $invalidduedate=1;
508                     } else {
509                         $env{'datedue'}="$year-$month-$day";
510                     }
511                 }
512             }
513             my %responses;
514             foreach (sort $query->param) {
515                 if ($_ =~ /response-(\d*)/) {
516                     $responses{$1}=$query->param($_);
517                 }
518             }
519             if (my $qnumber=$query->param('questionnumber')) {
520                 $responses{$qnumber}=$query->param('answer');
521             }
522             unless ($invalidduedate) {
523                 my @time=localtime(time);
524                 my $date= (1900+$time[5])."-".($time[4]+1)."-".$time[3];
525                 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message) = issuebook(\%env, $borrower, $barcode, \%responses,$date);
526                 unless ($iteminformation) {
527                     print << "EOF";
528                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
529                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Error</font></th></tr>
530                     <tr><td>
531                     <table border=0 cellpadding=5>
532                     <tr><td>
533                     $barcode is not a valid barcode.
534                     </td></tr>
535                     </table>
536 EOF
537                 }
538                 if ($rejected) {
539                     if ($rejected == -1) {
540                     } else {
541                         print << "EOF"
542                         <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
543                         <tr><th><font color=black size=6>Error Issuing Book</font></th></tr>
544                         <tr><td><font color=red size=6>$rejected</font></td></tr>
545                         </table>
546                         <br>
547 EOF
548                     }
549                 }
550                 my $responsesform='';
551                 foreach (keys %responses) {
552                     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
553                 }
554                 if ($question) {
555                     my $stickyduedate=$query->param('stickyduedate');
556                     print << "EOF";
557                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
558                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Issuing Question</b></font></td></tr>
559                     <tr><td>
560                     <table border=0 cellpadding=10>
561                     <tr><td>
562                     Attempting to issue $iteminformation->{'title'} by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
563                     <p>
564                     $question
565                     </td></tr>
566                     </table>
567                     </td></tr>
568
569                     <tr><td align=center>
570                     <table border=0>
571                     <tr><td>
572                     <form method=get>
573                     <input type=hidden name=module value=issues>
574                     <input type=hidden name=borrnumber value=$borrnumber>
575                     <input type=hidden name=barcode value=$barcode>
576                     <input type=hidden name=questionnumber value=$questionnumber>
577                     <input type=hidden name=day value=$day>
578                     <input type=hidden name=month value=$month>
579                     <input type=hidden name=year value=$year>
580                     <input type=hidden name=stickyduedate value=$stickyduedate>
581                     <input type=hidden name=branch value=$branch>
582                     <input type=hidden name=printer value=$printer>
583                     $responsesform
584                     <input type=hidden name=answer value=Y>
585                     <input type=submit value=Yes>
586                     </form>
587                     </td>
588                     <td>
589                     <form method=get>
590                     <input type=hidden name=module value=issues>
591                     <input type=hidden name=borrnumber value=$borrnumber>
592                     <input type=hidden name=barcode value=$barcode>
593                     <input type=hidden name=questionnumber value=$questionnumber>
594                     <input type=hidden name=day value=$day>
595                     <input type=hidden name=month value=$month>
596                     <input type=hidden name=year value=$year>
597                     <input type=hidden name=stickyduedate value=$stickyduedate>
598                     <input type=hidden name=branch value=$branch>
599                     <input type=hidden name=printer value=$printer>
600                     $responsesform
601                     <input type=hidden name=answer value=N>
602                     <input type=submit value=No>
603                     </form>
604                     </td>
605                     </tr>
606                     </table>
607                     </td></tr>
608                     </table>
609                     </td></tr>
610                     </table>
611 EOF
612                     return;
613                 }
614                 if ($message) {
615                     print << "EOF";
616                     <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
617                     <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black>Message</font></th></tr>
618                     <tr><td>$message</td></tr>
619                     </table>
620                     <p>
621 EOF
622                 }
623             }
624         }
625         my $issueid=$query->param('issueid');
626         ($issueid) || ($issueid=int(rand()*1000000000));
627         my $flag='';
628         my $flagtext='';
629         my $flaginfotext='';
630         foreach $flag (sort keys %$flags) {
631             if ($flags->{$flag}->{'noissues'}) {
632                 $flagtext.="<font color=red>$flag</font> ";
633             } else {
634                 $flagtext.="$flag ";
635             }
636             $flags->{$flag}->{'message'}=~s/\n/<br>/g;
637             if ($flags->{$flag}->{'noissues'}) {
638                 $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";
639             } else {
640                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
641             }
642         }
643         if ($flaginfotext) {
644             $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";
645         }
646         $env{'nottodaysissues'}=1;
647         my ($borrowerissues) = currentissues(\%env, $borrower);
648         $env{'nottodaysissues'}=0;
649         $env{'todaysissues'}=1;
650         my ($today) = currentissues(\%env, $borrower);
651         $env{'todaysissues'}=0;
652         my $previssues='';
653         my @datearr = localtime(time());
654         my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
655         my $color='';
656         foreach (sort keys %$borrowerissues) {
657             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
658             my $bookissue=$borrowerissues->{$_};
659             my $bgcolor='';
660             my $datedue=$bookissue->{'date_due'};
661             my $dd=$bookissue->{'date_due'};
662             #convert to nz style dates
663             #this should be set with some kinda config variable
664             
665             my @tempdate=split(/-/,$dd);
666             $dd="$tempdate[2]/$tempdate[1]/$tempdate[0]";
667             
668             #####
669             $datedue=~s/-//g;
670             if ($datedue < $todaysdate) {
671                 $dd="<font color=red>$dd</font>\n";
672             }
673             $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";
674         }
675         my $todaysissues='';
676         $color='';
677         foreach (sort keys %$today) {
678             ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
679             my $bookissue=$today->{$_};
680             $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";
681         }
682         for ($i=1; $i<32; $i++) {
683             my $selected='';
684             if (($query->param('stickyduedate')) && ($day==$i)) {
685                 $selected='selected';
686             }
687             $dayoptions.="<option value=$i $selected>$i";
688         }
689         my $counter=1;
690         foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
691             my $selected='';
692             if (($query->param('stickyduedate')) && ($month==$counter)) {
693                 $selected='selected';
694             }
695             $monthoptions.="<option value=$counter $selected>$_";
696             $counter++;
697         }
698         for ($i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
699             my $selected='';
700             if (($query->param('stickyduedate')) && ($year==$i)) {
701                 $selected='selected';
702             }
703             $yearoptions.="<option value=$i $selected>$i";
704         }
705
706         my $selected='';
707         ($query->param('stickyduedate')) && ($selected='checked');
708         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
709         my ($patrontable, $flaginfotable) = patrontable($borrower);
710         print << "EOF";
711         $patrontable
712         $flaginfotable
713         &nbsp; &nbsp;<br> <a href=cirulationold.pl?selectnewbranchprinter=1><b>Branch:</b></a> $branch,
714          <a href=cirulationold.pl?selectnewbranchprinter=1><b>Printer:</b></a> $printer<P>
715          <p>
716         <form method=get>
717     <table border=1 cellpadding=5>
718     <tr>
719         <td align=center valign=top>
720             <table border=0 cellspacing=0 cellpadding=5>
721                 <tr><th align=center background=$backgroundimage><font color=black><b>Enter Book Barcode</b></font></th></tr>
722                 <tr><td align=center>
723                 <table border=0>
724                 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></tr>
725                 <tr><td colspan=3 align=center>
726                 <table border=0 cellpadding=0 cellspacing=0>
727                 <tr><td>
728                 <select name=day><option value=0>Day$dayoptions</select>
729                 </td><td>
730                 <select name=month><option value=0>Month$monthoptions</select>
731                 </td><td>
732                 <select name=year><option value=0>Year$yearoptions</select>
733                 </td></tr>
734                 </table>
735                 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
736                 </td></tr>
737               </table>
738         <input type=hidden name=module value=issues>
739         <input type=hidden name=borrnumber value=$borrnumber>
740         <input type=hidden name=branch value=$branch>
741         <input type=hidden name=printer value=$printer>
742         
743 EOF
744 ;
745
746   if ($flags->{'CHARGES'}){
747             print "<input type=hidden name=charges value=yes>";
748   }
749     my $amountold=$flags->{'CHARGES'}->{'message'};
750   my @temp=split(/\$/,$amountold);
751   $amountold=$temp[1];
752   print "<input type=hidden name=oldamount value=$amountold>";
753 print <<EOF
754 </form>
755         </td>
756
757
758     </tr>
759     </table>
760     </table>
761     
762     <p clear=all>
763 <!-- issues tables-->
764         <table border=1 cellpadding=5 cellspacing=0 width=90%>
765         <td colspan=2 align=center>
766         <table border=0 cellpadding=5 cellspacing=0 width=100% > 
767             <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Issues Today</b></font></th></tr>
768             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
769             $todaysissues
770         </table>
771         </td>
772     </tr>
773
774     <tr>
775         <td colspan=2 align=center>
776         <table border=0 cellpadding=5 cellspacing=0 width=100%>
777             <tr><th colspan=5 bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Previous Issues</b></font></th></tr>
778             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
779             $previssues
780         </table>
781         </td>
782     </tr>
783 </table>
784 </td></tr></table>
785 <p>
786 EOF
787     } else {
788         if (my $findborrower=$query->param('findborrower')) {
789             my ($borrowers, $flags) = findborrower(\%env, $findborrower);
790             my @borrowers=@$borrowers;
791             if ($#borrowers == -1) {
792                 $query->param('findborrower', '');
793                 print "No borrower matched '$findborrower'<p>\n";
794                 issues(1);
795                 return;
796             }
797             if ($#borrowers == 0) {
798                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
799                 $query->param('barcode',' ');
800                 issues(1);
801                 return;
802             } else {
803                 print "<form method=get>\n";
804                 print "<input type=hidden name=module value=issues>\n";
805                 print "<input type=hidden name=branch value=$branch>\n";
806                 print "<input type=hidden name=printer value=$printer>\n";
807                 print "<input type=hidden name=barcode value=\" \">\n";
808                 print "<table border=0 cellspacing=0 cellpadding=5 bgcolor=#dddddd>";
809                 print "<tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Select a borrower</b></font></th></tr>\n";
810                 print "<tr><td align=center>\n";
811                 print "<select name=borrnumber size=7>\n";
812                 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowers) {
813                     print "<option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})\n";
814                 }
815                 print "</select><br>";
816                 print "<input type=submit>\n";
817                 print "</td></tr></table>\n";
818                 print "</td></tr></table>\n";
819             }
820         } else {
821             print << "EOF";
822 <form method=get>
823 <table border=0 cellpadding=5 cellspacing=0 bgcolor=#dddddd>
824 <tr><th bgcolor=$headerbackgroundcolor background=$backgroundimage><font color=black><b>Enter borrower card number<br> or partial last name</b></font></td></tr>
825 <tr><td><input name=findborrower></td></tr>
826 </table>
827 <input type=hidden name=module value=issues>
828 <input type=hidden name=branch value=$branch>
829 <input type=hidden name=printer value=$printer>
830 <input type=hidden name=barcode value=" ">
831 </form>
832 </td></tr></table>
833 EOF
834         }
835     }
836 }
837
838
839 sub patrontable {
840     my ($borrower) = @_;
841     my $flags=$borrower->{'flags'};
842     my $flagtext='';
843     my $flaginfotable='';
844     my $flaginfotext='';
845     my $flag;
846     foreach $flag (sort keys %$flags) {
847         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
848         if ($flags->{$flag}->{'noissues'}) {
849             $flagtext.="<font color=red>$flag</font> ";
850         } else {
851             $flagtext.="$flag ";
852         }
853         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
854         if ($flags->{$flag}->{'noissues'}) {
855             if ($flag eq 'CHARGES') {
856                 $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";
857             } else {
858                 $flaginfotext.="<tr><td valign=top><font color=red>$flag</font></td><td bgcolor=$color>$flags->{$flag}->{'message'}</td></tr>\n";
859             }
860         } else {
861             if ($flag eq 'CHARGES') {
862                 $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";
863             } elsif ($flag eq 'WAITING') {
864                 my $itemswaiting='';
865                 my $items=$flags->{$flag}->{'itemlist'};
866                 foreach $item (@$items) {
867                     my ($iteminformation) = getiteminformation($env, $item->{'itemnumber'}, 0);
868                     $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";
869                 }
870                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$itemswaiting</td></tr>\n";
871             } elsif ($flag eq 'ODUES') {
872                 my $items=$flags->{$flag}->{'itemlist'};
873                 my $itemswaiting="<table border=0 cellspacing=0 cellpadding=2>\n";
874                 my $currentcolor=$color;
875                 {
876                     my $color=$currentcolor;
877                     foreach $item (@$items) {
878                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
879                         my ($iteminformation) = getiteminformation($env, $item->{'itemnumber'}, 0);
880                         $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";
881                     }               
882                 }
883                 $itemswaiting.="</table>\n";
884                 if ($query->param('module') ne 'returns'){
885                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}, See below</td></tr>\n";
886                 } else {
887                   $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n"; 
888                 }
889             } else {
890                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
891             }
892         }
893     }
894     ($flaginfotext) && ($flaginfotext="<tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><b>Flags</b></td></tr>$flaginfotext\n");
895     $flaginfotext.="</table>";
896     my $patrontable= << "EOF";
897     <table border=1
898     cellpadding=5 cellspacing=0 align=right>
899     <tr><td bgcolor=$headerbackgroundcolor background=$backgroundimage colspan=2><font color=black><b>Patron Information</b></font></td></tr>
900     <tr><td colspan=2>
901     <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>
902 EOF
903     if ($query->param('module') ne 'returns'){ 
904        $patrontable.="$borrower->{'streetaddress'} $borrower->{'city'} Cat:
905        $borrower->{'categorycode'} ";
906      }
907     $patrontable.=<< "EOF";
908     
909     </td></tr>
910
911 EOF
912     return($patrontable, $flaginfotext);
913 }