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