Adding variable declarations so that 'waiting' screen shows same information 'reserve...
[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                                                                                 borfirstname => $borr->{'firstname'},
192                                                                                 borsurname => $borr->{'surname'},
193                                                                                 bortitle => $borr->{'title'},
194                                                                                 borphone => $borr->{'phone'},
195                                                                                 borstraddress => $borr->{'streetaddress'},
196                                                                                 borcity => $borr->{'city'},
197                                                                                 borzip => $borr->{'zipcode'},
198                                                                                 bornum => $res->{'borrowernumber'},
199                                                                                 borcnum => $borr->{'cardnumber'},
200                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
201                                                                                 waiting => 1,
202                                                                                 itemtitle => $iteminfo->{'title'},
203                                                                                 itemauthor => $iteminfo->{'author'});
204
205     }
206         if ($res->{'ResFound'} eq "Reserved") {
207                 my @da = localtime(time());
208                 my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
209                 $template->param(       found => 1,
210                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
211                                                                                 reserved => 1,
212                                                                                 today => $todaysdate,
213                                                                                 itemnumber => $res->{'itemnumber'},
214                                                                                 itemtitle => $iteminfo->{'title'},
215                                                                                 itemauthor => $iteminfo->{'author'},
216                                                                                 itembarcode => $iteminfo->{'barcode'},
217                                                                                 itemtype => $iteminfo->{'itemtype'},
218                                                                                 itembiblionumber => $iteminfo->{'biblionumber'},
219                                                                                 borsurname => $borr->{'surname'},
220                                                                                 bortitle => $borr->{'title'},
221                                                                                 borfirstname => $borr->{'firstname'},
222                                                                                 bornum => $res->{'borrowernumber'},
223                                                                                 borcnum => $borr->{'cardnumber'},
224                                                                                 borphone => $borr->{'phone'},
225                                                                                 borstraddress => $borr->{'streetaddress'},
226                                                                                 borsub => $borr->{'suburb'},
227                                                                                 borcity => $borr->{'city'},
228                                                                                 borzip => $borr->{'zipcode'},
229                                                                                 boremail => $borr->{'emailadress'},
230                                                                                 barcode => $barcode
231                                                                                 );
232     }
233 }
234
235 # Error Messages
236 my @errmsgloop;
237 foreach my $code (keys %$messages) {
238 #    warn $code;
239         my %err;
240         my $exit_required_p = 0;
241     if ($code eq 'BadBarcode'){
242                 $err{badbarcode}=1;
243                 $err{msg}= $messages->{'BadBarcode'};
244     } elsif ($code eq 'NotIssued'){
245                 $err{notissued}=1;
246                 $err{msg}= $branches->{$messages->{'IsPermanent'}}->{'branchname'};
247     } elsif ($code eq 'WasLost'){
248                 $err{waslost}=1;
249     } elsif ($code eq 'ResFound'){
250                 ;       # FIXME... anything to do here?
251     } elsif ($code eq 'WasReturned'){
252                 ;       # FIXME... anything to do here?
253     } elsif ($code eq 'WasTransfered'){
254                 ;       # FIXME... anything to do here?
255     } elsif ($code eq 'wthdrawn'){
256                 $err{withdrawn}=1;
257                 $exit_required_p = 1;
258     } elsif (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
259                 if ($messages->{'IsPermanent'} ne $branch) {
260                                 $err{ispermanent}=1;
261                                 $err{msg}=$branches->{$messages->{'IsPermanent'}}->{'branchname'} ;
262                 }
263     } else {
264                 die "Unknown error code $code"; # XXX
265     }
266     if (%err){
267         push (@errmsgloop, \%err);
268         }
269 last if $exit_required_p;
270 }
271 $template->param(errmsgloop => \@errmsgloop);
272
273 # patrontable ....
274 if ($borrower) {
275     my $flags = $borrower->{'flags'};
276     my $color = '';
277         my @flagloop;
278         my $flagset;
279     foreach my $flag (sort keys %$flags) {
280                 my %flaginfo;
281                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
282                 unless($flagset) { $flagset=1; }
283                 $flaginfo{color}=$color;
284                 $flaginfo{redfont} =($flags->{$flag}->{'noissues'});
285                 $flaginfo{flag}=$flag;
286                 if ($flag eq 'CHARGES') {
287                         $flaginfo{msg}=$flag;
288                         $flaginfo{charges}=1;
289                 } elsif ($flag eq 'WAITING') {
290                         $flaginfo{msg}=$flag;
291                         $flaginfo{waiting}=1;
292                         my @waitingitemloop;
293                         my $items = $flags->{$flag}->{'itemlist'};
294                         foreach my $item (@$items) {
295                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
296                                 my %waitingitem;
297                                 $waitingitem{biblionum}=$iteminformation->{'biblionumber'};
298                                 $waitingitem{barcode}=$iteminformation->{'barcode'};
299                                 $waitingitem{title}=$iteminformation->{'title'};
300                                 $waitingitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
301                                 push(@waitingitemloop, \%waitingitem);
302                         }
303                         $flaginfo{itemloop}=\@waitingitemloop;
304                 } elsif ($flag eq 'ODUES') {
305                         my $items = $flags->{$flag}->{'itemlist'};
306                         my @itemloop;
307                         foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
308                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
309                                 my %overdueitem;
310                                 $overdueitem{duedate}=$item->{'date_due'};
311                                 $overdueitem{biblionum}=$iteminformation->{'biblionumber'};
312                                 $overdueitem{barcode}=$iteminformation->{'barcode'};
313                                 $overdueitem{title}=$iteminformation->{'title'};
314                                 $overdueitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
315                                 push(@itemloop, \%overdueitem);
316                         }
317                         $flaginfo{itemloop}=\@itemloop;
318                         $flaginfo{overdue}=1;
319                         } else {
320                         $flaginfo{other}=1;
321                         $flaginfo{msg}=$flags->{$flag}->{'message'};
322                 }
323                 push(@flagloop, \%flaginfo);
324     }
325         $template->param(       flagset => $flagset,
326                                                                         flagloop => \@flagloop,
327                                                                         ribornum => $borrower->{'borrowernumber'},
328                                                                         riborcnum => $borrower->{'cardnumber'},
329                                                                         riborsurname => $borrower->{'surname'},
330                                                                         ribortitle => $borrower->{'title'},
331                                                                         riborfirstname => $borrower->{'firstname'}
332                                                                         );
333 }
334
335 my $color='';
336 #set up so only the lat 8 returned items display (make for faster loading pages)
337 my $count=0;
338 my @riloop;
339 foreach (sort {$a <=> $b} keys %returneditems) {
340         my %ri;
341     if ($count < 8) {
342                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
343                 $ri{color}=$color;
344                 my $barcode = $returneditems{$_};
345                 my $duedate = $riduedate{$_};
346                 my $overduetext;
347         my $borrowerinfo;
348                 if ($duedate) {
349                 my @tempdate = split ( /-/ , $duedate ) ;
350             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
351             my @datearr = localtime(time());
352             my $todaysdate = $datearr[5].'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
353                 my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
354                         $ri{bornum}=$borrower->{'borrowernumber'};
355                         $ri{borcnum}=$borrower->{'cardnumber'};
356                         $ri{borfirstname}=$borrower->{'firstname'};
357                         $ri{borsurname}=$borrower->{'surname'};
358                         $ri{bortitle}=$borrower->{'title'};
359         } else {
360                         $ri{bornum}=$riborrowernumber{$_};
361                 }
362                 my %ri;
363                 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
364                 $ri{color}=$color;
365                 $ri{itembiblionumber}=$iteminformation->{'biblionumber'};
366                 $ri{itemtitle}=$iteminformation->{'title'};
367                 $ri{itemauthor}=$iteminformation->{'author'};
368                 $ri{itemtype}=$iteminformation->{'itemtype'};
369                 $ri{barcode}=$barcode;
370         } else {
371                 last;
372     }
373     $count++;
374         push(@riloop,\%ri);
375 }
376 $template->param(riloop => \@riloop);
377
378 $template->param(       genbrname => $branches->{$branch}->{'branchname'},
379                         genprname => $printers->{$printer}->{'printername'},
380                         branch => $branch,
381                         printer => $printer,
382                         errmsgloop => \@errmsgloop
383                 );
384
385 # actually print the page!
386 output_html_with_http_headers $query, $cookie, $template->output;
387
388 sub cuecatbarcodedecode {
389     my ($barcode) = @_;
390     chomp($barcode);
391     my @fields = split(/\./,$barcode);
392     my @results = map(decode($_), @fields[1..$#fields]);
393     if ($#results == 2){
394         return $results[2];
395     } else {
396         return $barcode;
397     }}
398
399 # Local Variables:
400 # tab-width: 4
401 # End: