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