Refined Returns module.
[srvgit] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 use CGI qw/:standard/;
4 use C4::Circulation::Circ2;
5 use C4::Output;
6 use DBI;
7
8
9 my %env;
10 my $query=new CGI;
11 print $query->header;
12 print startpage();
13 print startmenu('circulation');
14 my $branches=getbranches(\%env);
15 my $printers=getprinters(\%env);
16 my $branch=$query->param('branch');
17 my $printer=$query->param('printer');
18 $env{'branchcode'}=$branch;
19 $env{'printer'}=$printer;
20
21 my $branchname='';
22 my $printername='';
23 if ($branch && $printer) {
24     $branchname=$branches->{$branch}->{'branchname'};
25     $printername=$printers->{$printer}->{'printername'};
26 }
27 print << "EOF";
28 <center>
29 <p>
30 <table border=0 width=100%>
31 <tr>
32 <td width=5%></td>
33 <td align=right width=30%><table border=1 bgcolor=black width=100%><tr><th><font color=white>$branchname</font></th></tr></table></td>
34 <td align=center width=20%>
35 <a href=circulation.pl?module=issues&branch=$branch&printer=$printer><img src=/images/issues.gif border=0 width=60></a>
36 <a href=circulation.pl?module=returns&branch=$branch&printer=$printer><img src=/images/returns.gif border=0 width=60></a>
37 </td><td align=left width=30%>
38 <table border=1 bgcolor=black width=100%><tr><th><font color=white>$printername</font></th></tr></table>
39 </td>
40 <td width=5%></td>
41 </tr>
42 </table>
43 <br>
44 EOF
45
46
47
48 if ($printer && $branch) {
49
50     SWITCH: {
51         if ($query->param('module') eq 'issues') { issues(); last SWITCH; }
52         if ($query->param('module') eq 'returns') { returns(); last SWITCH; }
53         issues();
54     }
55 } else {
56     my $branchcount=0;
57     my $printercount=0;
58     my $branchoptions;
59     my $printeroptions;
60     foreach (keys %$branches) {
61         (next) unless ($_);
62         $branchcount++;
63         $branchoptions.="<option value=$_>$branches->{$_}->{'branchname'}\n";
64     }
65     foreach (keys %$printers) {
66         (next) unless ($_);
67         $printercount++;
68         $printeroptions.="<option value=$_>$printers->{$_}->{'printername'}\n";
69     }
70     my ($printerform, $branchform);
71     if ($printercount>1) {
72         $printerform=<<"EOF";
73 <table border=1>
74 <tr><th bgcolor=black><font color=white>Choose a Printer</font></td></tr>
75 <tr><td>
76 <select name=printer>
77 $printeroptions
78 </select>
79 </td></tr>
80 </table>
81 EOF
82     } else {
83         my ($printer) = keys %$printers;
84         $printerform=<<"EOF";
85         <input type=hidden name=printer value=$printer>
86 EOF
87     }
88
89     if ($branchcount>1) {
90         $branchform=<<"EOF";
91 <table border=1>
92 <tr><th bgcolor=black><font color=white>Choose a Branch</font></td></tr>
93 <tr><td>
94 <select name=branch>
95 $branchoptions
96 </select>
97 </td></tr>
98 </table>
99 EOF
100     }
101     print << "EOF";
102     Select a printer and a branch
103     <form method=get>
104     <table border=0>
105     <tr><td>
106     $branchform
107     </td><td>
108     $printerform
109     </td></tr>
110     </table>
111     <input type=submit>
112     </form>
113 EOF
114 }
115
116
117 print endmenu('circulation');
118 print endpage();
119 sub default {
120 print << "EOF";
121 <a href=circulation.pl?module=issues&branch=$branch&printer=$printer>Issues</a>
122 <a href=circulation.pl?module=returns&branch=$branch&printer=$printer>Returns</a>
123 EOF
124 }
125
126
127 sub returns {
128     print << "EOF";
129     <form method=get>
130     <table border=3 bgcolor=#dddddd>
131         <tr><td colspan=2 bgcolor=black align=center><font color=white><b><font size=+1>Returns</font><br>Enter Book Barcode</b></font></td></tr>
132         <tr><td>Item Barcode:</td><td><input name=barcode size=10></td></tr>
133     </table>
134     <input type=hidden name=module value=returns>
135     <input type=hidden name=branch value=$branch>
136     <input type=hidden name=printer value=$printer>
137     </form>
138 EOF
139     if (my $barcode=$query->param('barcode')) {
140         print "Returning $barcode<br>\n";
141         my ($iteminformation, $borrower, $messages, $overduecharge) = returnbook(\%env, $barcode);
142         my $itemtable=<<"EOF";
143 <table border=1 bgcolor=#dddddd>
144 <tr><th bgcolor=black><font color=white>Item Information</font></th></tr>
145 <tr><td>
146 Title: $iteminformation->{'title'}<br>
147 Author: $iteminformation->{'author'}<br>
148 Barcode: $iteminformation->{'barcode'}
149 </td></tr>
150 </table>
151 EOF
152         if ($borrower) {
153             my ($patrontable, $flaginfotext) = patrontable($borrower);
154             print "<table border=0><tr><td valign=top align=center>$patrontable</td><td valign=top align=center>$flaginfotext</td></tr><tr><td colspan=2 align=center>$itemtable</td></tr></table><br>\n";
155         } else {
156             print "Not loaned out.\n";
157         }
158     }
159 }
160
161 sub issues {
162     if (my $borrnumber=$query->param('borrnumber')) {
163         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
164         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
165         my $year=$query->param('year');
166         my $month=$query->param('month');
167         my $day=$query->param('day');
168         if (my $barcode=$query->param('barcode')) {
169             my $invalidduedate=0;
170             $env{'datedue'}='';
171             if (($year eq 0) && ($month eq 0) && ($year eq 0)) {
172                 $env{'datedue'}='';
173             } else {
174                 if (($year eq 0) || ($month eq 0) || ($year eq 0)) {
175                     print "Invalid Due Date Specified. Book was not issued.<p>\n";
176                     $invalidduedate=1;
177                 } else {
178                     if (($day>30) && (($month==4) || ($month==6) || ($month==9) || ($month==11))) {
179                         print "Invalid Due Date Specified. Book was not issued.<p>\n";
180                         $invalidduedate=1;
181                     } elsif (($day>29) && ($month==2)) {
182                         print "Invalid Due Date Specified. Book was not issued.<p>\n";
183                         $invalidduedate=1;
184                     } elsif (($day>28) && (($year%4) && ((!($year%100) || ($year%400))))) {
185                         print "Invalid Due Date Specified. Book was not issued.<p>\n";
186                         $invalidduedate=1;
187                     } else {
188                         $env{'datedue'}="$year-$month-$day";
189                     }
190                 }
191             }
192             my %responses;
193             foreach (sort $query->param) {
194                 if ($_ =~ /response-(\d*)/) {
195                     $responses{$1}=$query->param($_);
196                 }
197             }
198             if (my $qnumber=$query->param('questionnumber')) {
199                 $responses{$qnumber}=$query->param('answer');
200             }
201             unless ($invalidduedate) {
202                 my ($iteminformation, $duedate, $rejected, $question, $questionnumber, $defaultanswer, $message) = issuebook(\%env, $borrower, $barcode, \%responses);
203                 if ($rejected) {
204                     if ($rejected == -1) {
205                     } else {
206                         print << "EOF"
207                         <table border=1 bgcolor=#dddddd>
208                         <tr><th bgcolor=black><font color=white>Error Issuing Book</font></th></tr>
209                         <tr><td><font color=red>$rejected</font></td></tr>
210                         </table>
211                         <br>
212 EOF
213                     }
214                 }
215                 my $responsesform='';
216                 foreach (keys %responses) {
217                     $responsesform.="<input type=hidden name=response-$_ value=$responses{$_}>\n";
218                 }
219                 if ($question) {
220                     my $stickyduedate=$query->param('stickyduedate');
221                     print << "EOF";
222                     <table border=1 bgcolor=#dddddd>
223                     <tr><th bgcolor=black><font color=white><b>Issuing Question</b></font></td></tr>
224                     <tr><td>
225                     <table border=0 cellpadding=10>
226                     <tr><td>
227                     Attempting to issue $iteminformation->{'title'} by $iteminformation->{'author'} to $borrower->{'firstname'} $borrower->{'surname'}.
228                     <p>
229                     $question
230                     </td></tr>
231                     </table>
232                     </td></tr>
233
234                     <tr><td align=center>
235                     <table border=0>
236                     <tr><td>
237                     <form method=get>
238                     <input type=hidden name=module value=issues>
239                     <input type=hidden name=borrnumber value=$borrnumber>
240                     <input type=hidden name=barcode value=$barcode>
241                     <input type=hidden name=questionnumber value=$questionnumber>
242                     <input type=hidden name=day value=$day>
243                     <input type=hidden name=month value=$month>
244                     <input type=hidden name=year value=$year>
245                     <input type=hidden name=stickyduedate value=$stickyduedate>
246                     <input type=hidden name=branch value=$branch>
247                     <input type=hidden name=printer value=$printer>
248                     $responsesform
249                     <input type=hidden name=answer value=Y>
250                     <input type=submit value=Yes>
251                     </form>
252                     </td>
253                     <td>
254                     <form method=get>
255                     <input type=hidden name=module value=issues>
256                     <input type=hidden name=borrnumber value=$borrnumber>
257                     <input type=hidden name=barcode value=$barcode>
258                     <input type=hidden name=questionnumber value=$questionnumber>
259                     <input type=hidden name=day value=$day>
260                     <input type=hidden name=month value=$month>
261                     <input type=hidden name=year value=$year>
262                     <input type=hidden name=stickyduedate value=$stickyduedate>
263                     <input type=hidden name=branch value=$branch>
264                     <input type=hidden name=printer value=$printer>
265                     $responsesform
266                     <input type=hidden name=answer value=N>
267                     <input type=submit value=No>
268                     </form>
269                     </td>
270                     </tr>
271                     </table>
272                     </td></tr>
273                     </table>
274 EOF
275                     return;
276                 }
277                 if ($message) {
278                     print << "EOF";
279                     <table border=1 bgcolor=#dddddd>
280                     <tr><th bgcolor=black><font color=white>Message</font></th></tr>
281                     <tr><td>$message</td></tr>
282                     </table>
283                     <p>
284 EOF
285                 }
286             }
287         }
288         my $issueid=$query->param('issueid');
289         ($issueid) || ($issueid=int(rand()*1000000000));
290         my $flag='';
291         my $flagtext='';
292         my $flaginfotext='';
293         foreach $flag (sort keys %$flags) {
294             if ($flags->{$flag}->{'noissues'}) {
295                 $flagtext.="<font color=red>$flag</font> ";
296             } else {
297                 $flagtext.="$flag ";
298             }
299             $flags->{$flag}->{'message'}=~s/\n/<br>/g;
300             if ($flags->{$flag}->{'noissues'}) {
301                 $flaginfotext.="<tr><td bgcolor=red valign=top><font color=white><b>$flag</b></font></td><td bgcolor=red><font color=white><b>$flags->{$flag}->{'message'}</b></font></td></tr>\n";
302             } else {
303                 $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
304             }
305         }
306         if ($flaginfotext) {
307             $flaginfotext="<table border=1 width=70% bgcolor=#dddddd><tr><th bgcolor=black colspan=2><font color=white>Patron Flags</font></th></tr>$flaginfotext</table>\n";
308         }
309         $env{'nottodaysissues'}=1;
310         my ($borrowerissues) = currentissues(\%env, $borrower);
311         $env{'nottodaysissues'}=0;
312         $env{'todaysissues'}=1;
313         my ($today) = currentissues(\%env, $borrower);
314         $env{'todaysissues'}=0;
315         my $previssues='';
316         my @datearr = localtime(time());
317         my $todaysdate = (1900+$datearr[5]).sprintf ("%0.2d", ($datearr[4]+1)).sprintf ("%0.2d", $datearr[3]);
318         foreach (sort keys %$borrowerissues) {
319             my $bookissue=$borrowerissues->{$_};
320             my $bgcolor='';
321             my $datedue=$bookissue->{'date_due'};
322             $datedue=~s/-//g;
323             if ($datedue < $todaysdate) {
324                 $bgcolor="bgcolor=red";
325             }
326             $previssues.="<tr $bgcolor><td>$bookissue->{'date_due'}</td><td>$bookissue->{'barcode'}</td><td>$bookissue->{'title'}</td><td>$bookissue->{'author'}</td><td>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>\n";
327         }
328         my $todaysissues='';
329         foreach (sort keys %$today) {
330             my $bookissue=$today->{$_};
331             $todaysissues.="<tr><td>$bookissue->{'date_due'}</td><td>$bookissue->{'barcode'}</td><td>$bookissue->{'title'}</td><td>$bookissue->{'author'}</td><td>$bookissue->{'dewey'} $bookissue->{'subclass'}</td></tr>\n";
332         }
333         for ($i=1; $i<32; $i++) {
334             my $selected='';
335             if (($query->param('stickyduedate')) && ($day==$i)) {
336                 $selected='selected';
337             }
338             $dayoptions.="<option value=$i $selected>$i";
339         }
340         my $counter=1;
341         foreach (('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) {
342             my $selected='';
343             if (($query->param('stickyduedate')) && ($month==$counter)) {
344                 $selected='selected';
345             }
346             $monthoptions.="<option value=$counter $selected>$_";
347             $counter++;
348         }
349         for ($i=$datearr[5]+1900; $i<$datearr[5]+1905; $i++) {
350             my $selected='';
351             if (($query->param('stickyduedate')) && ($year==$i)) {
352                 $selected='selected';
353             }
354             $yearoptions.="<option value=$i $selected>$i";
355         }
356
357         my $selected='';
358         ($query->param('stickyduedate')) && ($selected='checked');
359         my ($borrower, $flags) = getpatroninformation(\%env,$borrnumber,0);
360         my ($patrontable, $flaginfotable) = patrontable($borrower);
361         print << "EOF";
362         <form method=get>
363     <table border=0 cellpadding=5 width=90%>
364     <tr>
365         <td align=center>
366             <table border=1 bgcolor=#dddddd width=100%>
367                 <tr><td align=center bgcolor=black><font color=white><b>Enter Book Barcode</b></font></td></tr>
368                 <tr><td align=center>
369                 <table border=0 bgcolor=#dddddd>
370                 <tr><td>Item Barcode:</td><td><input name=barcode size=10></td><td><input type=submit value=Issue></tr>
371                 <tr><td colspan=3 align=center>
372                 <select name=day><option value=0>Day$dayoptions</select><select name=month><option value=0>Month$monthoptions</select><select name=year><option value=0>Year$yearoptions</select>
373                 <br>
374                 <input type=checkbox name=stickyduedate $selected> Sticky Due Date
375                 </td></tr>
376                 </table>
377                 </td></tr>
378             </table>
379         <input type=hidden name=module value=issues>
380         <input type=hidden name=borrnumber value=$borrnumber>
381         <input type=hidden name=branch value=$branch>
382         <input type=hidden name=printer value=$printer>
383         </form>
384         </td>
385         <td align=right valign=top>
386         $patrontable
387         <br>
388         $flaginfotable
389         </td>
390     </tr>
391     <tr>
392         <td colspan=2 align=center>
393         <table border=1 width=100% bgcolor=#dddddd>
394             <tr><th colspan=5 bgcolor=black><font color=white><b>Issues Today</b></font></th></tr>
395             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
396             $todaysissues
397         </table>
398         </td>
399     </tr>
400     <tr>
401         <td colspan=2 align=center>
402         <table border=1 width=100% bgcolor=#dddddd>
403             <tr><th colspan=5 bgcolor=black><font color=white><b>Previous Issues</b></font></th></tr>
404             <tr><th>Due Date</th><th>Bar Code</th><th>Title</th><th>Author</th><th>Class</th></tr>
405             $previssues
406         </table>
407         </td>
408     </tr>
409 </table>
410 <p>
411 EOF
412     } else {
413         if (my $findborrower=$query->param('findborrower')) {
414             my ($borrowers, $flags) = findborrower(\%env, $findborrower);
415             my @borrowers=@$borrowers;
416             if ($#borrowers == -1) {
417                 $query->param('findborrower', '');
418                 print "No borrower matched '$findborrower'<p>\n";
419                 issues();
420                 return;
421             }
422             if ($#borrowers == 0) {
423                 $query->param('borrnumber', $borrowers[0]->{'borrowernumber'});
424                 issues();
425                 return;
426             } else {
427                 print "<form method=get>\n";
428                 print "<input type=hidden name=module value=issues>\n";
429                 print "<input type=hidden name=branch value=$branch>\n";
430                 print "<input type=hidden name=printer value=$printer>\n";
431                 print "<table border=1 cellpadding=5 bgcolor=#dddddd>";
432                 print "<tr><th bgcolor=black><font color=white><b><font size=+1>Issues</font><br>Select a borrower</b></font></th></tr>\n";
433                 print "<tr><td align=center>\n";
434                 print "<select name=borrnumber size=7>\n";
435                 foreach (sort {$a->{'surname'}.$a->{'firstname'} cmp $b->{'surname'}.$b->{'firstname'}} @$borrowers) {
436                     print "<option value=$_->{'borrowernumber'}>$_->{'surname'}, $_->{'firstname'} ($_->{'cardnumber'})\n";
437                 }
438                 print "</select><br>";
439                 print "<input type=submit>\n";
440                 print "</td></tr></table>\n";
441             }
442         } else {
443             print << "EOF";
444 <form method=get>
445 <table border=1 bgcolor=#dddddd>
446 <tr><th bgcolor=black><font color=white><b><font size=+1>Issues</font><br>Enter borrower card number<br> or partial last name</b></font></td></tr>
447 <tr><td><input name=findborrower></td></tr>
448 </table>
449 <input type=hidden name=module value=issues>
450 <input type=hidden name=branch value=$branch>
451 <input type=hidden name=printer value=$printer>
452 </form>
453 EOF
454         }
455     }
456 }
457
458
459 sub patrontable {
460     my ($borrower) = @_;
461     my $flags=$borrower->{'flags'};
462     my $flagtext='';
463     my $flaginfotable='';
464     my $flaginfotext='';
465     my $flag;
466     foreach $flag (sort keys %$flags) {
467         if ($flags->{$flag}->{'noissues'}) {
468             $flagtext.="<font color=red>$flag</font> ";
469         } else {
470             $flagtext.="$flag ";
471         }
472         $flags->{$flag}->{'message'}=~s/\n/<br>/g;
473         if ($flags->{$flag}->{'noissues'}) {
474             $flaginfotext.="<tr><td bgcolor=red valign=top><font color=white><b>$flag</b></font></td><td bgcolor=red><font color=white><b>$flags->{$flag}->{'message'}</b></font></td></tr>\n";
475         } else {
476             $flaginfotext.="<tr><td valign=top>$flag</td><td>$flags->{$flag}->{'message'}</td></tr>\n";
477         }
478     }
479     ($flaginfotext) && ($flaginfotext="<table border=1 bgcolor=#dddddd><tr><th bgcolor=black colspan=2><font color=white>Patron Flags</font></th></tr>$flaginfotext</table>\n");
480     my $patrontable= << "EOF";
481     <table border=1 bgcolor=#dddddd>
482     <tr><th bgcolor=black><font color=white><b>Patron Information</b></font></td></tr>
483     <tr><td>
484     $borrower->{'cardnumber'} $borrower->{'surname'}, $borrower->{'title'} $borrower->{'firstname'}<br>
485     $borrower->{'streetaddress'} $borrower->{'city'}<br>
486     $borrower->{'categorycode'} $flagtext
487     </td></tr>
488     </table>
489 EOF
490     return($patrontable, $flaginfotext);
491 }