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