Further breaking up of the TmplTokenizer module.
[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                                                                                 itemnumber => $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     if (%err){
259         push (@errmsgloop, \%err);
260         }
261 last if $exit_required_p;
262 }
263 $template->param(errmsgloop => \@errmsgloop);
264
265 # patrontable ....
266 if ($borrower) {
267     my $flags = $borrower->{'flags'};
268     my $color = '';
269         my @flagloop;
270         my $flagset;
271     foreach my $flag (sort keys %$flags) {
272                 my %flaginfo;
273                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
274                 unless($flagset) { $flagset=1; }
275                 $flaginfo{color}=$color;
276                 $flaginfo{redfont} =($flags->{$flag}->{'noissues'});
277                 $flaginfo{flag}=$flag;
278                 if ($flag eq 'CHARGES') {
279                         $flaginfo{msg}=$flag;
280                         $flaginfo{charges}=1;
281                 } elsif ($flag eq 'WAITING') {
282                         $flaginfo{msg}=$flag;
283                         $flaginfo{waiting}=1;
284                         my @waitingitemloop;
285                         my $items = $flags->{$flag}->{'itemlist'};
286                         foreach my $item (@$items) {
287                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
288                                 my %waitingitem;
289                                 $waitingitem{biblionum}=$iteminformation->{'biblionumber'};
290                                 $waitingitem{barcode}=$iteminformation->{'barcode'};
291                                 $waitingitem{title}=$iteminformation->{'title'};
292                                 $waitingitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
293                                 push(@waitingitemloop, \%waitingitem);
294                         }
295                         $flaginfo{itemloop}=\@waitingitemloop;
296                 } elsif ($flag eq 'ODUES') {
297                         my $items = $flags->{$flag}->{'itemlist'};
298                         my @itemloop;
299                         foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
300                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
301                                 my %overdueitem;
302                                 $overdueitem{duedate}=$item->{'date_due'};
303                                 $overdueitem{biblionum}=$iteminformation->{'biblionumber'};
304                                 $overdueitem{barcode}=$iteminformation->{'barcode'};
305                                 $overdueitem{title}=$iteminformation->{'title'};
306                                 $overdueitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
307                                 push(@itemloop, \%overdueitem);
308                         }
309                         $flaginfo{itemloop}=\@itemloop;
310                         $flaginfo{overdue}=1;
311                         } else {
312                         $flaginfo{other}=1;
313                         $flaginfo{msg}=$flags->{$flag}->{'message'};
314                 }
315                 push(@flagloop, \%flaginfo);
316     }
317         $template->param(       flagset => $flagset,
318                                                                         flagloop => \@flagloop,
319                                                                         ribornum => $borrower->{'borrowernumber'},
320                                                                         riborcnum => $borrower->{'cardnumber'},
321                                                                         riborsurname => $borrower->{'surname'},
322                                                                         ribortitle => $borrower->{'title'},
323                                                                         riborfirstname => $borrower->{'firstname'}
324                                                                         );
325 }
326
327 my $color='';
328 #set up so only the lat 8 returned items display (make for faster loading pages)
329 my $count=0;
330 my @riloop;
331 foreach (sort {$a <=> $b} keys %returneditems) {
332         my %ri;
333     if ($count < 8) {
334                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
335                 $ri{color}=$color;
336                 my $barcode = $returneditems{$_};
337                 my $duedate = $riduedate{$_};
338                 my $overduetext;
339         my $borrowerinfo;
340                 if ($duedate) {
341                 my @tempdate = split ( /-/ , $duedate ) ;
342             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
343             my @datearr = localtime(time());
344             my $todaysdate = $datearr[5].'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
345                 my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
346                         $ri{bornum}=$borrower->{'borrowernumber'};
347                         $ri{borcnum}=$borrower->{'cardnumber'};
348                         $ri{borfirstname}=$borrower->{'firstname'};
349                         $ri{borsurname}=$borrower->{'surname'};
350                         $ri{bortitle}=$borrower->{'title'};
351         } else {
352                         $ri{bornum}=$riborrowernumber{$_};
353                 }
354                 my %ri;
355                 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
356                 $ri{color}=$color;
357                 $ri{itembiblionumber}=$iteminformation->{'biblionumber'};
358                 $ri{itemtitle}=$iteminformation->{'title'};
359                 $ri{itemauthor}=$iteminformation->{'author'};
360                 $ri{itemtype}=$iteminformation->{'itemtype'};
361                 $ri{barcode}=$barcode;
362         } else {
363                 last;
364     }
365     $count++;
366         push(@riloop,\%ri);
367 }
368 $template->param(riloop => \@riloop);
369
370 $template->param(       genbrname => $branches->{$branch}->{'branchname'},
371                         genprname => $printers->{$printer}->{'printername'},
372                         branch => $branch,
373                         printer => $printer,
374                         errmsgloop => \@errmsgloop
375                 );
376
377 # actually print the page!
378 output_html_with_http_headers $query, $cookie, $template->output;
379
380 sub cuecatbarcodedecode {
381     my ($barcode) = @_;
382     chomp($barcode);
383     my @fields = split(/\./,$barcode);
384     my @results = map(decode($_), @fields[1..$#fields]);
385     if ($#results == 2){
386         return $results[2];
387     } else {
388         return $barcode;
389     }}
390
391 # Local Variables:
392 # tab-width: 4
393 # End: