Make sure flagsrequired is *circulate* not parameters
[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
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 $circbackgroundcolor='#ffffcc';
54 my $circbackgroundcolor='white';
55 my $linecolor1='#ffffcc';
56 my $linecolor2='white';
57 my $backgroundimage="/images/background-mem.gif";
58
59 my $branches = getbranches();
60 my $printers = getprinters(\%env);
61
62 my $branch = getbranch($query, $branches);
63 my $printer = getprinter($query, $printers);
64
65 #
66 # Some code to handle the error if there is no branch or printer setting.....
67 #
68
69
70 $env{'branchcode'}=$branch;
71 $env{'printer'}=$printer;
72 $env{'queue'}=$printer;
73
74
75 # Set up the item stack ....
76 my %returneditems;
77 my %riduedate;
78 my %riborrowernumber;
79 my @inputloop;
80 foreach ($query->param) {
81     (next) unless (/ri-(\d*)/);
82         my %input;
83     my $counter=$1;
84     (next) if ($counter>20);
85     my $barcode=$query->param("ri-$counter");
86     my $duedate=$query->param("dd-$counter");
87     my $borrowernumber=$query->param("bn-$counter");
88     $counter++;
89     # decode cuecat
90     $barcode = cuecatbarcodedecode($barcode);
91         ######################
92         #Are these lines still useful ?
93     $returneditems{$counter}=$barcode;
94     $riduedate{$counter}=$duedate;
95     $riborrowernumber{$counter}=$borrowernumber;
96         #######################
97         $input{counter}=$counter;
98         $input{barcode}=$barcode;
99         $input{duedate}=$duedate;
100         $input{bornum}=$borrowernumber;
101         push (@inputloop, \%input);
102 }
103
104 ############
105 # Deal with the requests....
106 if ($query->param('resbarcode')) {
107     my $item = $query->param('itemnumber');
108     my $borrnum = $query->param('borrowernumber');
109     my $resbarcode = $query->param('resbarcode');
110 # set to waiting....
111     my $iteminfo = getiteminformation(\%env, $item);
112     my $tobranchcd = ReserveWaiting($item, $borrnum);
113     my $branchname = $branches->{$tobranchcd}->{'branchname'};
114     my ($borr) = getpatroninformation(\%env, $borrnum, 0);
115         my $borcnum=$borr->{'cardnumber'};
116     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
117     my $slip = $query->param('resslip');
118     printslip(\%env, $slip);
119     if ($tobranchcd ne $branch) {
120                 $template->param(       itemtitle => $iteminfo->{'title'},
121                                                                                 iteminfo => $iteminfo->{'author'},
122                                                                                 branchname => $branchname,
123                                                                                 name => $name,
124                                                                                 bornum => $borrnum,
125                                                                                 borcnum => $borcnum,
126                                                                                 diffbranch => 1);
127     }
128 }
129
130
131 my $iteminformation;
132 my $borrower;
133 my $returned = 0;
134 my $messages;
135 my $barcode = $query->param('barcode');
136 # actually return book and prepare item table.....
137 if ($barcode) {
138     # decode cuecat
139     $barcode = cuecatbarcodedecode($barcode);
140     ($returned, $messages, $iteminformation, $borrower) = returnbook($barcode, $branch);
141     if ($returned) {
142                 $returneditems{0} = $barcode;
143                 $riborrowernumber{0} = $borrower->{'borrowernumber'};
144                 $riduedate{0} = $iteminformation->{'date_due'};
145                 my %input;
146                 $input{counter}=0;
147                 $input{first}=1;
148                 $input{barcode}=$barcode;
149                 $input{duedate}=$riduedate{0};
150                 $input{bornum}=$riborrowernumber{0};
151                 push (@inputloop, \%input);
152     } elsif (! $messages->{'BadBarcode'}) {
153                 my %input;
154                 $input{counter}=0;
155                 $input{first}=1;
156                 $input{barcode}=$barcode;
157                 $input{duedate}=0;
158
159                 $returneditems{0} = $barcode;
160                 $riduedate{0} = 0;
161                 if ($messages->{'wthdrawn'}) {
162                         $input{withdrawn}=1;
163                         $input{bornum}="Item Cancelled";
164                         $riborrowernumber{0} = 'Item Cancelled';
165                 } else {
166                         $input{bornum}=" ";
167                         $riborrowernumber{0} = ' ';
168                 }
169                 push (@inputloop, \%input);
170         }
171         $template->param(       returned => $returned,
172                                                                         itemtitle => $iteminformation->{'title'},
173 #                                                                       itembc => $iteminformation->{'barcode'},
174 #                                                                       itemdatedue => $iteminformation->{'datedue'},
175                                                                         itemauthor => $iteminformation->{'author'});
176 }
177 $template->param(inputloop => \@inputloop);
178
179
180 my $found=0;
181 my $waiting=0;
182 my $reserved=0;
183
184 if ($messages->{'ResFound'}) {
185     my $res = $messages->{'ResFound'};
186     my $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
187     my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
188     my $name = $borr->{'surname'}." ".$borr->{'title'}." ".$borr->{'firstname'};
189     my ($iteminfo) = getiteminformation(\%env, 0, $barcode);
190
191     if ($res->{'ResFound'} eq "Waiting") {
192                 $template->param(       found => 1,
193                                                                                 name => $name,
194                                                                                 bornum => $res->{'borrowernumber'},
195                                                                                 borcnum => $borr->{'cardnumber'},
196                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
197                                                                                 waiting => 1,
198                                                                                 itemtitle => $iteminfo->{'title'},
199                                                                                 itemauthor => $iteminfo->{'author'});
200
201     }
202         if ($res->{'ResFound'} eq "Reserved") {
203                 my @da = localtime(time());
204                 my $todaysdate = sprintf ("%0.2d", ($da[3]+1))."/".sprintf ("%0.2d", ($da[4]+1))."/".($da[5]+1900);
205                 $template->param(       found => 1,
206                                                                                 branchname => $branches->{$res->{'branchcode'}}->{'branchname'},
207                                                                                 reserved => 1,
208                                                                                 today => $todaysdate,
209                                                                                 itemnum => $res->{'itemnumber'},
210                                                                                 itemtitle => $iteminfo->{'title'},
211                                                                                 itemauthor => $iteminfo->{'author'},
212                                                                                 itembarcode => $iteminfo->{'barcode'},
213                                                                                 itemtype => $iteminfo->{'itemtype'},
214                                                                                 itembiblionumber => $iteminfo->{'biblionumber'},
215                                                                                 borsurname => $borr->{'surname'},
216                                                                                 bortitle => $borr->{'title'},
217                                                                                 borfirstname => $borr->{'firstname'},
218                                                                                 bornum => $res->{'borrowernumber'},
219                                                                                 borcnum => $borr->{'cardnumber'},
220                                                                                 borphone => $borr->{'phone'},
221                                                                                 borstraddress => $borr->{'streetaddress'},
222                                                                                 borsub => $borr->{'suburb'},
223                                                                                 bortown => $borr->{'town'},
224                                                                                 boremail => $borr->{'emailadress'},
225                                                                                 barcode => $barcode
226                                                                                 );
227     }
228 }
229
230 # Error Messages
231 my @errmsgloop;
232 foreach my $code (keys %$messages) {
233 #    warn $code;
234         my %err;
235         my $exit_required_p = 0;
236     if ($code eq 'BadBarcode'){
237                 $err{badbarcode}=1;
238                 $err{msg}= $messages->{'BadBarcode'};
239     } elsif ($code eq 'NotIssued'){
240                 $err{notissued}=1;
241                 $err{msg}= $branches->{$messages->{'IsPermanent'}}->{'branchname'};
242     } elsif ($code eq 'WasLost'){
243                 $err{waslost}=1;
244     } elsif ($code eq 'WasReturned'){
245                 ;       # FIXME... anything to do here?
246     } elsif ($code eq 'WasTransfered'){
247                 ;       # FIXME... anything to do here?
248     } elsif ($code eq 'wthdrawn'){
249                 $err{withdrawn}=1;
250                 $exit_required_p = 1;
251     } elsif (($code eq 'IsPermanent') && (not $messages->{'ResFound'})) {
252                 if ($messages->{'IsPermanent'} ne $branch) {
253                                 $err{ispermanent}=1;
254                                 $err{msg}=$branches->{$messages->{'IsPermanent'}}->{'branchname'} ;
255                 }
256     } else {
257                 die "Unknown error code $code"; # XXX
258     }
259         push (@errmsgloop, \%err);
260 last if $exit_required_p;
261 }
262 $template->param(errmsgloop => \@errmsgloop);
263
264 # patrontable ....
265 if ($borrower) {
266     my $flags = $borrower->{'flags'};
267     my $color = '';
268         my @flagloop;
269         my $flagset;
270     foreach my $flag (sort keys %$flags) {
271                 my %flaginfo;
272                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
273                 unless($flagset) { $flagset=1; }
274                 $flaginfo{color}=$color;
275                 $flaginfo{redfont} =($flags->{$flag}->{'noissues'});
276                 $flaginfo{flag}=$flag;
277                 if ($flag eq 'CHARGES') {
278                         $flaginfo{msg}=$flag;
279                         $flaginfo{charges}=1;
280                 } elsif ($flag eq 'WAITING') {
281                         $flaginfo{msg}=$flag;
282                         $flaginfo{waiting}=1;
283                         my @waitingitemloop;
284                         my $items = $flags->{$flag}->{'itemlist'};
285                         foreach my $item (@$items) {
286                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
287                                 my %waitingitem;
288                                 $waitingitem{biblionum}=$iteminformation->{'biblionumber'};
289                                 $waitingitem{barcode}=$iteminformation->{'barcode'};
290                                 $waitingitem{title}=$iteminformation->{'title'};
291                                 $waitingitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
292                                 push(@waitingitemloop, \%waitingitem);
293                         }
294                         $flaginfo{itemloop}=\@waitingitemloop;
295                 } elsif ($flag eq 'ODUES') {
296                         my $items = $flags->{$flag}->{'itemlist'};
297                         my @itemloop;
298                         foreach my $item (sort {$a->{'date_due'} cmp $b->{'date_due'}} @$items) {
299                                 my ($iteminformation) = getiteminformation(\%env, $item->{'itemnumber'}, 0);
300                                 my %overdueitem;
301                                 $overdueitem{duedate}=$item->{'date_due'};
302                                 $overdueitem{biblionum}=$iteminformation->{'biblionumber'};
303                                 $overdueitem{barcode}=$iteminformation->{'barcode'};
304                                 $overdueitem{title}=$iteminformation->{'title'};
305                                 $overdueitem{brname}=$branches->{$iteminformation->{'holdingbranch'}}->{'branchname'};
306                                 push(@itemloop, \%overdueitem);
307                         }
308                         $flaginfo{itemloop}=\@itemloop;
309                         $flaginfo{overdue}=1;
310                         } else {
311                         $flaginfo{other}=1;
312                         $flaginfo{msg}=$flags->{$flag}->{'message'};
313                 }
314                 push(@flagloop, \%flaginfo);
315     }
316         $template->param(       flagset => $flagset,
317                                                                         flagloop => \@flagloop,
318                                                                         ribornum => $borrower->{'borrowernumber'},
319                                                                         riborcnum => $borrower->{'cardnumber'},
320                                                                         riborsurname => $borrower->{'surname'},
321                                                                         ribortitle => $borrower->{'title'},
322                                                                         riborfirstname => $borrower->{'firstname'}
323                                                                         );
324 }
325
326 my $color='';
327 #set up so only the lat 8 returned items display (make for faster loading pages)
328 my $count=0;
329 my @riloop;
330 foreach (sort {$a <=> $b} keys %returneditems) {
331         my %ri;
332     if ($count < 8) {
333                 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
334                 $ri{color}=$color;
335                 my $barcode = $returneditems{$_};
336                 my $duedate = $riduedate{$_};
337                 my $overduetext;
338         my $borrowerinfo;
339                 if ($duedate) {
340                 my @tempdate = split ( /-/ , $duedate ) ;
341             my $duedatenz = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
342             my @datearr = localtime(time());
343             my $todaysdate = $datearr[5].'-'.sprintf ("%0.2d", ($datearr[4]+1)).'-'.sprintf ("%0.2d", $datearr[3]);
344                 my ($borrower) = getpatroninformation(\%env, $riborrowernumber{$_}, 0);
345                         $ri{bornum}=$borrower->{'borrowernumber'};
346                         $ri{borcnum}=$borrower->{'cardnumber'};
347                         $ri{borfirstname}=$borrower->{'firstname'};
348                         $ri{borsurname}=$borrower->{'surname'};
349                         $ri{bortitle}=$borrower->{'title'};
350         } else {
351                         $ri{bornum}=$riborrowernumber{$_};
352                 }
353                 my %ri;
354                 my ($iteminformation) = getiteminformation(\%env, 0, $barcode);
355                 $ri{color}=$color;
356                 $ri{itembiblionumber}=$iteminformation->{'biblionumber'};
357                 $ri{itemtitle}=$iteminformation->{'title'};
358                 $ri{itemauthor}=$iteminformation->{'author'};
359                 $ri{itemtype}=$iteminformation->{'itemtype'};
360                 $ri{barcode}=$barcode;
361         } else {
362                 last;
363     }
364     $count++;
365         push(@riloop,\%ri);
366 }
367 $template->param(riloop => \@riloop);
368
369 $template->param(       genbrname => $branches->{$branch}->{'branchname'},
370                         genprname => $printers->{$printer}->{'printername'},
371                         branch => $branch,
372                         printer => $printer,
373                         hdrbckgdcolor => $headerbackgroundcolor,
374                         bckgdimg => $backgroundimage,
375                         errmsgloop => \@errmsgloop
376                 );
377
378 # actually print the page!
379 output_html_with_http_headers $query, $cookie, $template->output;
380
381 sub cuecatbarcodedecode {
382     my ($barcode) = @_;
383     chomp($barcode);
384     my @fields = split(/\./,$barcode);
385     my @results = map(decode($_), @fields[1..$#fields]);
386     if ($#results == 2){
387         return $results[2];
388     } else {
389         return $barcode;
390     }}
391
392 # Local Variables:
393 # tab-width: 4
394 # End: