Resynching Savannah
[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 C4::Koha;
36 use C4::Members;
37 use C4::Date;
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 my $todaysdate =get_today();
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 my $branch=C4::Context->preference("defaultBranch");
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 my $item;
106 # Deal with the requests....
107 if ( $query->param('resbarcode') ) {
108     $item       = $query->param('itemnumber');
109     my $borrnum    = $query->param('borrowernumber');
110     my $resbarcode = $query->param('resbarcode');
111
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    =
119       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
120     my $slip = $query->param('resslip');
121     printslip( \%env, $slip ); #removed by paul
122
123     if ( $tobranchcd ne $branch ) {
124         $template->param(
125             itemtitle  => $iteminfo->{'title'},
126             iteminfo   => $iteminfo->{'author'},
127             branchname => $branchname,
128             name       => $name,
129             bornum     => $borrnum,
130             borcnum    => $borcnum,
131             diffbranch => 1
132         );
133     }
134 }
135
136 my $iteminformation;
137 my $borrower;
138 my $returned = 0;
139 my $messages;
140 my $barcode = $query->param('barcode');
141
142 # actually return book and prepare item table.....
143 if ($barcode) {
144
145     # decode cuecat
146     $barcode = cuecatbarcodedecode($barcode);
147     ( $returned, $messages, $iteminformation, $borrower ) =
148       returnbook( $barcode, $branch );
149     if ($returned) {
150         $returneditems{0}    = $barcode;
151         $riborrowernumber{0} = $borrower->{'borrowernumber'};
152         $riduedate{0}        = $iteminformation->{'date_due'};
153         my %input;
154         $input{counter} = 0;
155         $input{first}   = 1;
156         $input{barcode} = $barcode;
157         $input{duedate} = $riduedate{0};
158         $input{bornum}  = $riborrowernumber{0};
159         push ( @inputloop, \%input );
160     }
161     elsif ( !$messages->{'BadBarcode'} ) {
162                 if ( $messages->{'NotIssued'} ) {
163                 my $dbh = C4::Context->dbh;
164                 my $sth=$dbh->prepare("select duetime from reserveissue where itemnumber=? and isnull(rettime)");
165                 $sth->execute($iteminformation->{'itemnumber'});
166                 my ($date_due) = $sth->fetchrow;
167                 
168                 $sth->finish;
169                         if ($date_due){
170 #                               $messages->{'ReserveIssued'} =$barcode;                 
171                         print $query->redirect("/cgi-bin/koha/circ/resreturns.pl?barcode=$barcode");
172                         }
173                 }
174         my %input;
175         $input{counter} = 0;
176         $input{first}   = 1;
177         $input{barcode} = $barcode;
178         $input{duedate} = 0;
179
180         $returneditems{0} = $barcode;
181         $riduedate{0}     = 0;
182         if ( $messages->{'wthdrawn'} ) {
183             $input{withdrawn} = 1;
184             $input{bornum}    = "Item Cancelled";
185             $riborrowernumber{0} = 'Item Cancelled';
186         }
187         else {
188             $input{bornum} = " ";
189             $riborrowernumber{0} = ' ';
190         }
191         push ( @inputloop, \%input );
192     }
193     $template->param(
194         returned  => $returned,
195         itemtitle => $iteminformation->{'title'},
196
197         #                                                                       itembc => $iteminformation->{'barcode'},
198         #                                                                       itemdatedue => $iteminformation->{'datedue'},
199         itemauthor => $iteminformation->{'author'}
200     );
201 }
202 $template->param( inputloop => \@inputloop );
203
204 my $found    = 0;
205 my $waiting  = 0;
206 my $reserved = 0;
207
208 if ( $messages->{'ResFound'} ) {
209     my $res        = $messages->{'ResFound'};
210     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
211     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
212     my $name =
213       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
214     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
215
216     if ( $res->{'ResFound'} eq "Waiting" ) {
217         $template->param(
218             found         => 1,
219             name          => $name,
220             borfirstname  => $borr->{'firstname'},
221             borsurname    => $borr->{'surname'},
222             bortitle      => $borr->{'title'},
223             borphone      => $borr->{'phone'},
224             borstraddress => $borr->{'streetaddress'},
225             borcity       => $borr->{'city'},
226             borzip        => $borr->{'zipcode'},
227             bornum        => $res->{'borrowernumber'},
228             borcnum       => $borr->{'cardnumber'},
229             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
230             waiting     => 1,
231             itemnumber  => $res->{'itemnumber'},
232             itemtitle   => $iteminfo->{'title'},
233             itemauthor  => $iteminfo->{'author'},
234             itembarcode => $iteminfo->{'barcode'},
235             itemtype    => $iteminfo->{'itemtype'},
236             itembiblionumber => $iteminfo->{'biblionumber'}
237         );
238
239     }
240     if ( $res->{'ResFound'} eq "Reserved" ) {
241       
242         $template->param(
243             found       => 1,
244             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
245             reserved    => 1,
246             today       =>format_date( $todaysdate),
247             itemnumber  => $res->{'itemnumber'},
248             itemtitle   => $iteminfo->{'title'},
249             itemauthor  => $iteminfo->{'author'},
250             itembarcode => $iteminfo->{'barcode'},
251             itemtype    => $iteminfo->{'itemtype'},
252             itembiblionumber => $iteminfo->{'biblionumber'},
253             borsurname       => $borr->{'surname'},
254             bortitle         => $borr->{'title'},
255             borfirstname     => $borr->{'firstname'},
256             bornum           => $res->{'borrowernumber'},
257             borcnum          => $borr->{'cardnumber'},
258             borphone         => $borr->{'phone'},
259             borstraddress    => $borr->{'streetaddress'},
260             borsub           => $borr->{'suburb'},
261             borcity          => $borr->{'city'},
262             borzip           => $borr->{'zipcode'},
263             boremail         => $borr->{'emailadress'},
264             barcode          => $barcode
265         );
266     }
267 }
268
269 # Error Messages
270 my @errmsgloop;
271 foreach my $code ( keys %$messages ) {
272
273     #    warn $code;
274     my %err;
275     my $exit_required_p = 0;
276     if ( $code eq 'BadBarcode' ) {
277         $err{badbarcode} = 1;
278         $err{msg}        = $messages->{'BadBarcode'};
279     }
280     elsif ( $code eq 'NotIssued' ) {
281         $err{notissued} = 1;
282         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
283     }
284     elsif ( $code eq 'WasLost' ) {
285         $err{waslost} = 1;
286     }
287     elsif ( $code eq 'ResFound' ) {
288         ;    # FIXME... anything to do here?
289     }
290     elsif ( $code eq 'WasReturned' ) {
291         ;    # FIXME... anything to do here?
292     }
293     elsif ( $code eq 'WasTransfered' ) {
294         ;    # FIXME... anything to do here?
295     }
296         elsif ( $code eq 'ReserveIssued' ) {
297         $err{reserveissued} = 1;
298     }
299     elsif ( $code eq 'wthdrawn' ) {
300         $err{withdrawn} = 1;
301         $exit_required_p = 1;
302     }
303     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
304         if ( $messages->{'IsPermanent'} ne $branch ) {
305             $err{ispermanent} = 1;
306             $err{msg}         =
307               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
308         }
309     }
310     else {
311         die "Unknown error code $code";    # XXX
312     }
313     if (%err) {
314         push ( @errmsgloop, \%err );
315     }
316     last if $exit_required_p;
317 }
318 $template->param( errmsgloop => \@errmsgloop );
319
320 # patrontable ....
321 if ($borrower) {
322     my $flags = $borrower->{'flags'};
323     my $color = '';
324     my @flagloop;
325     my $flagset;
326     foreach my $flag ( sort keys %$flags ) {
327         my %flaginfo;
328         ( $color eq $linecolor1 ) 
329           ? ( $color = $linecolor2 )
330           : ( $color = $linecolor1 );
331         unless ($flagset) { $flagset = 1; }
332         $flaginfo{color}   = $color;
333         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
334         $flaginfo{flag}    = $flag;
335         if ( $flag eq 'CHARGES' ) {
336             $flaginfo{msg}     = $flag;
337             $flaginfo{charges} = 1;
338             $flaginfo{bornum} = $borrower->{borrowernumber};
339         }
340         elsif ( $flag eq 'WAITING' ) {
341             $flaginfo{msg}     = $flag;
342             $flaginfo{waiting} = 1;
343             my @waitingitemloop;
344             my $items = $flags->{$flag}->{'itemlist'};
345             foreach my $item (@$items) {
346                 my ($iteminformation) =
347                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
348                 my %waitingitem;
349                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
350                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
351                 $waitingitem{title}     = $iteminformation->{'title'};
352                 $waitingitem{brname}    =
353                   $branches->{ $iteminformation->{'holdingbranch'} }->{
354                   'branchname'};
355                 push ( @waitingitemloop, \%waitingitem );
356             }
357             $flaginfo{itemloop} = \@waitingitemloop;
358         }
359         elsif ( $flag eq 'ODUES' ) {
360             my $items = $flags->{$flag}->{'itemlist'};
361             my @itemloop;
362             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
363                 @$items )
364             {
365                 my ($iteminformation) =
366                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
367                 my %overdueitem;
368                 $overdueitem{duedate}   = $item->{'date_due'};
369                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
370                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
371                 $overdueitem{title}     = $iteminformation->{'title'};
372                 $overdueitem{brname}    =
373                   $branches->{ $iteminformation->{'holdingbranch'} }->{
374                   'branchname'};
375                 push ( @itemloop, \%overdueitem );
376             }
377             $flaginfo{itemloop} = \@itemloop;
378             $flaginfo{overdue}  = 1;
379         }
380         else {
381             $flaginfo{other} = 1;
382             $flaginfo{msg}   = $flags->{$flag}->{'message'};
383         }
384         push ( @flagloop, \%flaginfo );
385     }
386     $template->param(
387         flagset        => $flagset,
388         flagloop       => \@flagloop,
389         ribornum       => $borrower->{'borrowernumber'},
390         riborcnum      => $borrower->{'cardnumber'},
391         riborsurname   => $borrower->{'surname'},
392         ribortitle     => $borrower->{'title'},
393         riborfirstname => $borrower->{'firstname'}
394     );
395 }
396
397 my $color = '';
398
399 #set up so only the last 8 returned items display (make for faster loading pages)
400 my $count = 0;
401 my @riloop;
402 foreach ( sort { $a <=> $b } keys %returneditems ) {
403     my %ri;
404     if ( $count < 8 ) {
405         ( $color eq $linecolor1 ) 
406           ? ( $color = $linecolor2 )
407           : ( $color = $linecolor1 );
408         $ri{color} = $color;
409         my $barcode = $returneditems{$_};
410         my $duedate = $riduedate{$_};
411         my $overduetext;
412         my $borrowerinfo;
413         if ($duedate) {
414            
415            
416             $ri{duedate}=format_date($duedate);
417             my ($borrower) =
418               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
419             $ri{bornum}       = $borrower->{'borrowernumber'};
420             $ri{borcnum}      = $borrower->{'cardnumber'};
421             $ri{borfirstname} = $borrower->{'firstname'};
422             $ri{borsurname}   = $borrower->{'surname'};
423             $ri{bortitle}     = $borrower->{'title'};
424         }
425         else {
426             $ri{bornum} = $riborrowernumber{$_};
427         }
428 #        my %ri;
429         my ($iteminformation) = getiteminformation( \%env, 0, $barcode );
430         $ri{color}            = $color;
431         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
432         $ri{itemtitle}        = $iteminformation->{'title'};
433         $ri{itemauthor}       = $iteminformation->{'author'};
434         $ri{itemtype}         = $iteminformation->{'itemtype'};
435         $ri{barcode}          = $barcode;
436     }
437     else {
438         last;
439     }
440     $count++;
441     push ( @riloop, \%ri );
442 }
443 $template->param( riloop => \@riloop );
444
445 $template->param(
446     genbrname  => $branches->{$branch}->{'branchname'},
447     genprname  => $printers->{$printer}->{'printername'},
448     branch     => $branch,
449     printer    => $printer,
450     errmsgloop => \@errmsgloop
451 );
452
453 # actually print the page!
454 output_html_with_http_headers $query, $cookie, $template->output;
455
456 sub cuecatbarcodedecode {
457     my ($barcode) = @_;
458     chomp($barcode);
459     my @fields = split ( /\./, $barcode );
460     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
461     if ( $#results == 2 ) {
462         return $results[2];
463     }
464     else {
465         return $barcode;
466     }
467 }
468
469 # Local Variables:
470 # tab-width: 4
471 # End: