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