Clean up before final commits
[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 my $query = new CGI;
38
39 #getting the template
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name   => "circ/returns.tmpl",
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { circulate => 1 },
47     }
48 );
49
50 #####################
51 #Global vars
52 my %env;
53 my $headerbackgroundcolor = '#99cc33';
54 my $linecolor1            = '#ffffcc';
55 my $linecolor2            = 'white';
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 my $branch=C4::Context->preference("defaultBranch");
67 $env{'branchcode'} = $branch;
68 $env{'printer'}    = $printer;
69 $env{'queue'}      = $printer;
70
71 # Set up the item stack ....
72 my %returneditems;
73 my %riduedate;
74 my %riborrowernumber;
75 my @inputloop;
76 foreach ( $query->param ) {
77     (next) unless (/ri-(\d*)/);
78     my %input;
79     my $counter = $1;
80     (next) if ( $counter > 20 );
81     my $barcode        = $query->param("ri-$counter");
82     my $duedate        = $query->param("dd-$counter");
83     my $borrowernumber = $query->param("bn-$counter");
84     $counter++;
85
86     # decode cuecat
87     $barcode = cuecatbarcodedecode($barcode);
88
89     ######################
90     #Are these lines still useful ?
91     $returneditems{$counter}    = $barcode;
92     $riduedate{$counter}        = $duedate;
93     $riborrowernumber{$counter} = $borrowernumber;
94
95     #######################
96     $input{counter} = $counter;
97     $input{barcode} = $barcode;
98     $input{duedate} = $duedate;
99     $input{bornum}  = $borrowernumber;
100     push ( @inputloop, \%input );
101 }
102
103 ############
104 my $item;
105 # Deal with the requests....
106 if ( $query->param('resbarcode') ) {
107     $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 ); #removed by paul
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                 if ( $messages->{'NotIssued'} ) {
162                 my $dbh = C4::Context->dbh;
163                 my $sth=$dbh->prepare("select duetime from reserveissue where itemnumber=? and isnull(rettime)");
164                 $sth->execute($iteminformation->{'itemnumber'});
165                 my ($date_due) = $sth->fetchrow;
166                 
167                 $sth->finish;
168                         if ($date_due){
169 #                               $messages->{'ReserveIssued'} =$barcode;                 
170                         print $query->redirect("/cgi-bin/koha/circ/resreturns.pl?barcode=$barcode");
171                         }
172                 }
173         my %input;
174         $input{counter} = 0;
175         $input{first}   = 1;
176         $input{barcode} = $barcode;
177         $input{duedate} = 0;
178
179         $returneditems{0} = $barcode;
180         $riduedate{0}     = 0;
181         if ( $messages->{'wthdrawn'} ) {
182             $input{withdrawn} = 1;
183             $input{bornum}    = "Item Cancelled";
184             $riborrowernumber{0} = 'Item Cancelled';
185         }
186         else {
187             $input{bornum} = " ";
188             $riborrowernumber{0} = ' ';
189         }
190         push ( @inputloop, \%input );
191     }
192     $template->param(
193         returned  => $returned,
194         itemtitle => $iteminformation->{'title'},
195
196         #                                                                       itembc => $iteminformation->{'barcode'},
197         #                                                                       itemdatedue => $iteminformation->{'datedue'},
198         itemauthor => $iteminformation->{'author'}
199     );
200 }
201 $template->param( inputloop => \@inputloop );
202
203 my $found    = 0;
204 my $waiting  = 0;
205 my $reserved = 0;
206
207 if ( $messages->{'ResFound'} ) {
208     my $res        = $messages->{'ResFound'};
209     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
210     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
211     my $name =
212       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
213     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
214
215     if ( $res->{'ResFound'} eq "Waiting" ) {
216         $template->param(
217             found         => 1,
218             name          => $name,
219             borfirstname  => $borr->{'firstname'},
220             borsurname    => $borr->{'surname'},
221             bortitle      => $borr->{'title'},
222             borphone      => $borr->{'phone'},
223             borstraddress => $borr->{'streetaddress'},
224             borcity       => $borr->{'city'},
225             borzip        => $borr->{'zipcode'},
226             bornum        => $res->{'borrowernumber'},
227             borcnum       => $borr->{'cardnumber'},
228             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
229             waiting     => 1,
230             itemnumber  => $res->{'itemnumber'},
231             itemtitle   => $iteminfo->{'title'},
232             itemauthor  => $iteminfo->{'author'},
233             itembarcode => $iteminfo->{'barcode'},
234             itemtype    => $iteminfo->{'itemtype'},
235             itembiblionumber => $iteminfo->{'biblionumber'}
236         );
237
238     }
239     if ( $res->{'ResFound'} eq "Reserved" ) {
240         my @da         = localtime( time() );
241         my $todaysdate =
242           sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
243           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
244           . ( $da[5] + 1900 );
245         $template->param(
246             found       => 1,
247             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
248             reserved    => 1,
249             today       => $todaysdate,
250             itemnumber  => $res->{'itemnumber'},
251             itemtitle   => $iteminfo->{'title'},
252             itemauthor  => $iteminfo->{'author'},
253             itembarcode => $iteminfo->{'barcode'},
254             itemtype    => $iteminfo->{'itemtype'},
255             itembiblionumber => $iteminfo->{'biblionumber'},
256             borsurname       => $borr->{'surname'},
257             bortitle         => $borr->{'title'},
258             borfirstname     => $borr->{'firstname'},
259             bornum           => $res->{'borrowernumber'},
260             borcnum          => $borr->{'cardnumber'},
261             borphone         => $borr->{'phone'},
262             borstraddress    => $borr->{'streetaddress'},
263             borsub           => $borr->{'suburb'},
264             borcity          => $borr->{'city'},
265             borzip           => $borr->{'zipcode'},
266             boremail         => $borr->{'emailadress'},
267             barcode          => $barcode
268         );
269     }
270 }
271
272 # Error Messages
273 my @errmsgloop;
274 foreach my $code ( keys %$messages ) {
275
276     #    warn $code;
277     my %err;
278     my $exit_required_p = 0;
279     if ( $code eq 'BadBarcode' ) {
280         $err{badbarcode} = 1;
281         $err{msg}        = $messages->{'BadBarcode'};
282     }
283     elsif ( $code eq 'NotIssued' ) {
284         $err{notissued} = 1;
285         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
286     }
287     elsif ( $code eq 'WasLost' ) {
288         $err{waslost} = 1;
289     }
290     elsif ( $code eq 'ResFound' ) {
291         ;    # FIXME... anything to do here?
292     }
293     elsif ( $code eq 'WasReturned' ) {
294         ;    # FIXME... anything to do here?
295     }
296     elsif ( $code eq 'WasTransfered' ) {
297         ;    # FIXME... anything to do here?
298     }
299         elsif ( $code eq 'ReserveIssued' ) {
300         $err{reserveissued} = 1;
301     }
302     elsif ( $code eq 'wthdrawn' ) {
303         $err{withdrawn} = 1;
304         $exit_required_p = 1;
305     }
306     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
307         if ( $messages->{'IsPermanent'} ne $branch ) {
308             $err{ispermanent} = 1;
309             $err{msg}         =
310               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
311         }
312     }
313     else {
314         die "Unknown error code $code";    # XXX
315     }
316     if (%err) {
317         push ( @errmsgloop, \%err );
318     }
319     last if $exit_required_p;
320 }
321 $template->param( errmsgloop => \@errmsgloop );
322
323 # patrontable ....
324 if ($borrower) {
325     my $flags = $borrower->{'flags'};
326     my $color = '';
327     my @flagloop;
328     my $flagset;
329     foreach my $flag ( sort keys %$flags ) {
330         my %flaginfo;
331         ( $color eq $linecolor1 ) 
332           ? ( $color = $linecolor2 )
333           : ( $color = $linecolor1 );
334         unless ($flagset) { $flagset = 1; }
335         $flaginfo{color}   = $color;
336         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
337         $flaginfo{flag}    = $flag;
338         if ( $flag eq 'CHARGES' ) {
339             $flaginfo{msg}     = $flag;
340             $flaginfo{charges} = 1;
341             $flaginfo{bornum} = $borrower->{borrowernumber};
342         }
343         elsif ( $flag eq 'WAITING' ) {
344             $flaginfo{msg}     = $flag;
345             $flaginfo{waiting} = 1;
346             my @waitingitemloop;
347             my $items = $flags->{$flag}->{'itemlist'};
348             foreach my $item (@$items) {
349                 my ($iteminformation) =
350                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
351                 my %waitingitem;
352                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
353                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
354                 $waitingitem{title}     = $iteminformation->{'title'};
355                 $waitingitem{brname}    =
356                   $branches->{ $iteminformation->{'holdingbranch'} }->{
357                   'branchname'};
358                 push ( @waitingitemloop, \%waitingitem );
359             }
360             $flaginfo{itemloop} = \@waitingitemloop;
361         }
362         elsif ( $flag eq 'ODUES' ) {
363             my $items = $flags->{$flag}->{'itemlist'};
364             my @itemloop;
365             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
366                 @$items )
367             {
368                 my ($iteminformation) =
369                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
370                 my %overdueitem;
371                 $overdueitem{duedate}   = $item->{'date_due'};
372                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
373                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
374                 $overdueitem{title}     = $iteminformation->{'title'};
375                 $overdueitem{brname}    =
376                   $branches->{ $iteminformation->{'holdingbranch'} }->{
377                   'branchname'};
378                 push ( @itemloop, \%overdueitem );
379             }
380             $flaginfo{itemloop} = \@itemloop;
381             $flaginfo{overdue}  = 1;
382         }
383         else {
384             $flaginfo{other} = 1;
385             $flaginfo{msg}   = $flags->{$flag}->{'message'};
386         }
387         push ( @flagloop, \%flaginfo );
388     }
389     $template->param(
390         flagset        => $flagset,
391         flagloop       => \@flagloop,
392         ribornum       => $borrower->{'borrowernumber'},
393         riborcnum      => $borrower->{'cardnumber'},
394         riborsurname   => $borrower->{'surname'},
395         ribortitle     => $borrower->{'title'},
396         riborfirstname => $borrower->{'firstname'}
397     );
398 }
399
400 my $color = '';
401
402 #set up so only the last 8 returned items display (make for faster loading pages)
403 my $count = 0;
404 my @riloop;
405 foreach ( sort { $a <=> $b } keys %returneditems ) {
406     my %ri;
407     if ( $count < 8 ) {
408         ( $color eq $linecolor1 ) 
409           ? ( $color = $linecolor2 )
410           : ( $color = $linecolor1 );
411         $ri{color} = $color;
412         my $barcode = $returneditems{$_};
413         my $duedate = $riduedate{$_};
414         my $overduetext;
415         my $borrowerinfo;
416         if ($duedate) {
417             my @tempdate = split ( /-/, $duedate );
418             $ri{year}=$tempdate[0];
419             $ri{month}=$tempdate[1];
420             $ri{day}=$tempdate[2];
421             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
422             my @datearr    = localtime( time() );
423             my $todaysdate =
424               $datearr[5] . '-'
425               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
426               . sprintf( "%0.2d", $datearr[3] );
427             $ri{duedate}=$duedate;
428             my ($borrower) =
429               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
430             $ri{bornum}       = $borrower->{'borrowernumber'};
431             $ri{borcnum}      = $borrower->{'cardnumber'};
432             $ri{borfirstname} = $borrower->{'firstname'};
433             $ri{borsurname}   = $borrower->{'surname'};
434             $ri{bortitle}     = $borrower->{'title'};
435         }
436         else {
437             $ri{bornum} = $riborrowernumber{$_};
438         }
439 #        my %ri;
440         my ($iteminformation) = getiteminformation( \%env, 0, $barcode );
441         $ri{color}            = $color;
442         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
443         $ri{itemtitle}        = $iteminformation->{'title'};
444         $ri{itemauthor}       = $iteminformation->{'author'};
445         $ri{itemtype}         = $iteminformation->{'itemtype'};
446         $ri{barcode}          = $barcode;
447     }
448     else {
449         last;
450     }
451     $count++;
452     push ( @riloop, \%ri );
453 }
454 $template->param( riloop => \@riloop );
455
456 $template->param(
457     genbrname  => $branches->{$branch}->{'branchname'},
458     genprname  => $printers->{$printer}->{'printername'},
459     branch     => $branch,
460     printer    => $printer,
461     errmsgloop => \@errmsgloop
462 );
463
464 # actually print the page!
465 output_html_with_http_headers $query, $cookie, $template->output;
466
467 sub cuecatbarcodedecode {
468     my ($barcode) = @_;
469     chomp($barcode);
470     my @fields = split ( /\./, $barcode );
471     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
472     if ( $#results == 2 ) {
473         return $results[2];
474     }
475     else {
476         return $barcode;
477     }
478 }
479
480 # Local Variables:
481 # tab-width: 4
482 # End: