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