Bugfix for #499
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2 # WARNING: This file contains mixed-sized tabs! (some 4-character, some 8)
3 # WARNING: Currently, 4-character tabs seem to be dominant
4 # WARNING: But there are still lots of 8-character tabs
5
6 #written 11/3/2002 by Finlay
7 #script to execute returns of books
8
9
10 # Copyright 2000-2002 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
14 # Koha is free software; you can redistribute it and/or modify it under the
15 # terms of the GNU General Public License as published by the Free Software
16 # Foundation; either version 2 of the License, or (at your option) any later
17 # version.
18 #
19 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
20 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License along with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA  02111-1307 USA
26
27 use strict;
28 use CGI;
29 use C4::Circulation::Circ2;
30 use C4::Search;
31 use C4::Output;
32 use C4::Print;
33 use C4::Reserves2;
34 use C4::Auth;
35 use C4::Interface::CGI::Output;
36 use HTML::Template;
37 use C4::Koha;
38
39 my $query=new CGI;
40 #getting the template
41 my ($template, $borrowernumber, $cookie)
42     = get_template_and_user({template_name => "circ/returns.tmpl",
43                                                         query => $query,
44                             type => "intranet",
45                             authnotrequired => 0,
46                             flagsrequired => {circulate => 1},
47                          });
48
49 #####################
50 #Global vars
51 my %env;
52 my $headerbackgroundcolor='#99cc33';
53 my $linecolor1='#ffffcc';
54 my $linecolor2='white';
55
56 my $branches = getbranches();
57 my $printers = getprinters(\%env);
58
59 my $branch = getbranch($query, $branches);
60 my $printer = getprinter($query, $printers);
61
62 #
63 # Some code to handle the error if there is no branch or printer setting.....
64 #
65
66
67 $env{'branchcode'}=$branch;
68 $env{'printer'}=$printer;
69 $env{'queue'}=$printer;
70
71
72 # Set up the item stack ....
73 my %returneditems;
74 my %riduedate;
75 my %riborrowernumber;
76 my @inputloop;
77 foreach ($query->param) {
78     (next) unless (/ri-(\d*)/);
79         my %input;
80     my $counter=$1;
81     (next) if ($counter>20);
82     my $barcode=$query->param("ri-$counter");
83     my $duedate=$query->param("dd-$counter");
84     my $borrowernumber=$query->param("bn-$counter");
85     $counter++;
86     # decode cuecat
87     $barcode = cuecatbarcodedecode($barcode);
88         ######################
89         #Are these lines still useful ?
90     $returneditems{$counter}=$barcode;
91     $riduedate{$counter}=$duedate;
92     $riborrowernumber{$counter}=$borrowernumber;
93         #######################
94         $input{counter}=$counter;
95         $input{barcode}=$barcode;
96         $input{duedate}=$duedate;
97         $input{bornum}=$borrowernumber;
98         push (@inputloop, \%input);
99 }
100
101 ############
102 # Deal with the requests....
103 if ($query->param('resbarcode')) {
104     my $item = $query->param('itemnumber');
105     my $borrnum = $query->param('borrowernumber');
106     my $resbarcode = $query->param('resbarcode');
107 # set to waiting....
108     my $iteminfo = getiteminformation(\%env, $item);
109     my $tobranchcd = ReserveWaiting($item, $borrnum);
110     my $branchname = $branches->{$tobranchcd}->{'branchname'};
111     my ($borr) = getpatroninformation(\%env, $borrnum, 0);
112         my $borcnum=$borr->{'cardnumber'};
113     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
114     my $slip = $query->param('resslip');
115     printslip(\%env, $slip);
116     if ($tobranchcd ne $branch) {
117                 $template->param(       itemtitle => $iteminfo->{'title'},
118                                                                                 iteminfo => $iteminfo->{'author'},
119                                                                                 branchname => $branchname,
120                                                                                 name => $name,
121                                                                                 bornum => $borrnum,
122                                                                                 borcnum => $borcnum,
123                                                                                 diffbranch => 1);
124     }
125 }
126
127
128 my $iteminformation;
129 my $borrower;
130 my $returned = 0;
131 my $messages;
132 my $barcode = $query->param('barcode');
133 # actually return book and prepare item table.....
134 if ($barcode) {
135     # decode cuecat
136     $barcode = cuecatbarcodedecode($barcode);
137     ($returned, $messages, $iteminformation, $borrower) = returnbook($barcode, $branch);
138     if ($returned) {
139                 $returneditems{0} = $barcode;
140                 $riborrowernumber{0} = $borrower->{'borrowernumber'};
141                 $riduedate{0} = $iteminformation->{'date_due'};
142                 my %input;
143                 $input{counter}=0;
144                 $input{first}=1;
145                 $input{barcode}=$barcode;
146                 $input{duedate}=$riduedate{0};
147                 $input{bornum}=$riborrowernumber{0};
148                 push (@inputloop, \%input);
149     } elsif (! $messages->{'BadBarcode'}) {
150                 my %input;
151                 $input{counter}=0;
152                 $input{first}=1;
153                 $input{barcode}=$barcode;
154                 $input{duedate}=0;
155
156                 $returneditems{0} = $barcode;
157                 $riduedate{0} = 0;
158                 if ($messages->{'wthdrawn'}) {
159                         $input{withdrawn}=1;
160                         $input{bornum}="Item Cancelled";
161                         $riborrowernumber{0} = 'Item Cancelled';
162                 } else {
163                         $input{bornum}=" ";
164                         $riborrowernumber{0} = ' ';
165                 }
166                 push (@inputloop, \%input);
167         }
168         $template->param(       returned => $returned,
169                                                                         itemtitle => $iteminformation->{'title'},
170 #                                                                       itembc => $iteminformation->{'barcode'},
171 #                                                                       itemdatedue => $iteminformation->{'datedue'},
172                                                                         itemauthor => $iteminformation->{'author'});
173 }
174 $template->param(inputloop => \@inputloop);
175
176
177 my $found=0;
178 my $waiting=0;
179 my $reserved=0;
180
181 if ($messages->{'ResFound'}) {
182     my $res = $messages->{'ResFound'};
183     my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
184     my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
185     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
186     my ($iteminfo) = getiteminformation(\%env, 0, $barcode);
187
188     if ($res->{'ResFound'} eq "Waiting") {
189                 $template->param(       found => 1,
190                                                                                 name => $name,
191                                                                                 bornum => $res->{'borrowernumber'},
192                                                                                 borcnum => $borr->{'cardnumber'},
193                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
194                                                                                 waiting => 1,
195                                                                                 itemtitle => $iteminfo->{'title'},
196                                                                                 itemauthor => $iteminfo->{'author'});
197
198     }
199         if ($res->{'ResFound'} eq "Reserved") {
200                 my @da = localtime(time());
201                 my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
202                 $template->param(       found => 1,
203                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
204                                                                                 reserved => 1,
205                                                                                 today => $todaysdate,
206                                                                                 itemnum => $res->{'itemnumber'},
207                                                                                 itemtitle => $iteminfo->{'title'},
208                                                                                 itemauthor => $iteminfo->{'author'},
209                                                                                 itembarcode => $iteminfo->{'barcode'},
210                                                                                 itemtype => $iteminfo->{'itemtype'},
211                                                                                 itembiblionumber => $iteminfo->{'biblionumber'},
212                                                                                 borsurname => $borr->{'surname'},
213                                                                                 bortitle => $borr->{'title'},
214                                                                                 borfirstname => $borr->{'firstname'},
215                                                                                 bornum => $res->{'borrowernumber'},
216                                                                                 borcnum => $borr->{'cardnumber'},
217                                                                                 borphone => $borr->{'phone'},
218                                                                                 borstraddress => $borr->{'streetaddress'},
219                                                                                 borsub => $borr->{'suburb'},
220                                                                                 bortown => $borr->{'town'},
221                                                                                 boremail => $borr->{'emailadress'},
222                                                                                 barcode => $barcode
223                                                                                 );
224     }
225 }
226
227 # Error Messages
228 my @errmsgloop;
229 foreach my $code (keys %$messages) {
230 #    warn $code;
231         my %err;
232         my $exit_required_p = 0;
233     if ($code eq 'BadBarcode'){
234                 $err{badbarcode}=1;
235                 $err{msg}= $messages->{'BadBarcode'};
236     } elsif ($code eq 'NotIssued'){
237                 $err{notissued}=1;
238                 $err{msg}= $branches->{$messages->{'IsPermanent'}}->{'branchname'};
239     } elsif ($code eq 'WasLost'){
240                 $err{waslost}=1;
241     } elsif ($code eq 'ResFound'){
242                 ;       # FIXME... anything to do here?
243     } elsif ($code eq 'WasReturned'){
244                 ;       # FIXME... anything to do here?
245     } elsif ($code eq 'WasTransfered'){
246                 ;       # FIXME... anything to do here?
247     } elsif ($code eq 'wthdrawn'){
248                 $err{withdrawn}=1;
249                 $exit_required_p = 1;
250     } elsif (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
251                 if ($messages->{'IsPermanent'} ne $branch) {
252                                 $err{ispermanent}=1;
253                                 $err{msg}=$branches->{$messages->{'IsPermanent'}}->{'branchname'} ;
254                 }
255     } else {
256                 die "Unknown error code $code"; # XXX
257     }
258         push (@errmsgloop, \%err);
259 last if $exit_required_p;
260 }
261 $template->param(errmsgloop => \@errmsgloop);
262
263 # patrontable ....
264 if ($borrower) {
265     my $flags = $borrower->{'flags'};
266     my $color = '';
267         my @flagloop;
268         my $flagset;
269     foreach my $flag (sort keys %$flags) {
270                 my %flaginfo;
271                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
272                 unless($flagset) { $flagset=1; }
273                 $flaginfo{color}=$color;
274                 $flaginfo{redfont} =($flags->{$flag}->{'noissues'});
275                 $flaginfo{flag}=$flag;
276                 if ($flag eq 'CHARGES') {
277                         $flaginfo{msg}=$flag;
278                         $flaginfo{charges}=1;
279                 } elsif ($flag eq 'WAITING') {
280                         $flaginfo{msg}=$flag;
281                         $flaginfo{waiting}=1;
282                         my @waitingitemloop;
283                         my $items = $flags->{$flag}->{'itemlist'};
284                         foreach my $item (@$items) {
285                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
286                                 my %waitingitem;
287                                 $waitingitem{biblionum}=$iteminformation->{'biblionumber'};
288                                 $waitingitem{barcode}=$iteminformation->{'barcode'};
289                                 $waitingitem{title}=$iteminformation->{'title'};
290                                 $waitingitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
291                                 push(@waitingitemloop, \%waitingitem);
292                         }
293                         $flaginfo{itemloop}=\@waitingitemloop;
294                 } elsif ($flag eq 'ODUES') {
295                         my $items = $flags->{$flag}->{'itemlist'};
296                         my @itemloop;
297                         foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
298                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
299                                 my %overdueitem;
300                                 $overdueitem{duedate}=$item->{'date_due'};
301                                 $overdueitem{biblionum}=$iteminformation->{'biblionumber'};
302                                 $overdueitem{barcode}=$iteminformation->{'barcode'};
303                                 $overdueitem{title}=$iteminformation->{'title'};
304                                 $overdueitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
305                                 push(@itemloop, \%overdueitem);
306                         }
307                         $flaginfo{itemloop}=\@itemloop;
308                         $flaginfo{overdue}=1;
309                         } else {
310                         $flaginfo{other}=1;
311                         $flaginfo{msg}=$flags->{$flag}->{'message'};
312                 }
313                 push(@flagloop, \%flaginfo);
314     }
315         $template->param(       flagset => $flagset,
316                                                                         flagloop => \@flagloop,
317                                                                         ribornum => $borrower->{'borrowernumber'},
318                                                                         riborcnum => $borrower->{'cardnumber'},
319                                                                         riborsurname => $borrower->{'surname'},
320                                                                         ribortitle => $borrower->{'title'},
321                                                                         riborfirstname => $borrower->{'firstname'}
322                                                                         );
323 }
324
325 my $color='';
326 #set up so only the lat 8 returned items display (make for faster loading pages)
327 my $count=0;
328 my @riloop;
329 foreach (sort {$a <=> $b} keys %returneditems) {
330         my %ri;
331     if ($count < 8) {
332                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
333                 $ri{color}=$color;
334                 my $barcode = $returneditems{$_};
335                 my $duedate = $riduedate{$_};
336                 my $overduetext;
337         my $borrowerinfo;
338                 if ($duedate) {
339                 my @tempdate = split ( /-/ , $duedate ) ;
340             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
341             my @datearr = localtime(time());
342             my $todaysdate = $datearr[5].'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
343                 my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
344                         $ri{bornum}=$borrower->{'borrowernumber'};
345                         $ri{borcnum}=$borrower->{'cardnumber'};
346                         $ri{borfirstname}=$borrower->{'firstname'};
347                         $ri{borsurname}=$borrower->{'surname'};
348                         $ri{bortitle}=$borrower->{'title'};
349         } else {
350                         $ri{bornum}=$riborrowernumber{$_};
351                 }
352                 my %ri;
353                 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
354                 $ri{color}=$color;
355                 $ri{itembiblionumber}=$iteminformation->{'biblionumber'};
356                 $ri{itemtitle}=$iteminformation->{'title'};
357                 $ri{itemauthor}=$iteminformation->{'author'};
358                 $ri{itemtype}=$iteminformation->{'itemtype'};
359                 $ri{barcode}=$barcode;
360         } else {
361                 last;
362     }
363     $count++;
364         push(@riloop,\%ri);
365 }
366 $template->param(riloop => \@riloop);
367
368 $template->param(       genbrname => $branches->{$branch}->{'branchname'},
369                         genprname => $printers->{$printer}->{'printername'},
370                         branch => $branch,
371                         printer => $printer,
372                         errmsgloop => \@errmsgloop
373                 );
374
375 # actually print the page!
376 output_html_with_http_headers $query, $cookie, $template->output;
377
378 sub cuecatbarcodedecode {
379     my ($barcode) = @_;
380     chomp($barcode);
381     my @fields = split(/\./,$barcode);
382     my @results = map(decode($_), @fields[1..$#fields]);
383     if ($#results == 2){
384         return $results[2];
385     } else {
386         return $barcode;
387     }}
388
389 # Local Variables:
390 # tab-width: 4
391 # End: