CIRCULATION : the big rewrite...
[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 ); #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         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             $flaginfo{bornum} = $borrower->{borrowernumber};
327         }
328         elsif ( $flag eq 'WAITING' ) {
329             $flaginfo{msg}     = $flag;
330             $flaginfo{waiting} = 1;
331             my @waitingitemloop;
332             my $items = $flags->{$flag}->{'itemlist'};
333             foreach my $item (@$items) {
334                 my ($iteminformation) =
335                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
336                 my %waitingitem;
337                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
338                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
339                 $waitingitem{title}     = $iteminformation->{'title'};
340                 $waitingitem{brname}    =
341                   $branches->{ $iteminformation->{'holdingbranch'} }->{
342                   'branchname'};
343                 push ( @waitingitemloop, \%waitingitem );
344             }
345             $flaginfo{itemloop} = \@waitingitemloop;
346         }
347         elsif ( $flag eq 'ODUES' ) {
348             my $items = $flags->{$flag}->{'itemlist'};
349             my @itemloop;
350             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
351                 @$items )
352             {
353                 my ($iteminformation) =
354                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
355                 my %overdueitem;
356                 $overdueitem{duedate}   = $item->{'date_due'};
357                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
358                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
359                 $overdueitem{title}     = $iteminformation->{'title'};
360                 $overdueitem{brname}    =
361                   $branches->{ $iteminformation->{'holdingbranch'} }->{
362                   'branchname'};
363                 push ( @itemloop, \%overdueitem );
364             }
365             $flaginfo{itemloop} = \@itemloop;
366             $flaginfo{overdue}  = 1;
367         }
368         else {
369             $flaginfo{other} = 1;
370             $flaginfo{msg}   = $flags->{$flag}->{'message'};
371         }
372         push ( @flagloop, \%flaginfo );
373     }
374     $template->param(
375         flagset        => $flagset,
376         flagloop       => \@flagloop,
377         ribornum       => $borrower->{'borrowernumber'},
378         riborcnum      => $borrower->{'cardnumber'},
379         riborsurname   => $borrower->{'surname'},
380         ribortitle     => $borrower->{'title'},
381         riborfirstname => $borrower->{'firstname'}
382     );
383 }
384
385 my $color = '';
386
387 #set up so only the last 8 returned items display (make for faster loading pages)
388 my $count = 0;
389 my @riloop;
390 foreach ( sort { $a <=> $b } keys %returneditems ) {
391     my %ri;
392     if ( $count < 8 ) {
393         ( $color eq $linecolor1 ) 
394           ? ( $color = $linecolor2 )
395           : ( $color = $linecolor1 );
396         $ri{color} = $color;
397         my $barcode = $returneditems{$_};
398         my $duedate = $riduedate{$_};
399         my $overduetext;
400         my $borrowerinfo;
401         if ($duedate) {
402             my @tempdate = split ( /-/, $duedate );
403             $ri{year}=$tempdate[0];
404             $ri{month}=$tempdate[1];
405             $ri{day}=$tempdate[2];
406             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
407             my @datearr    = localtime( time() );
408             my $todaysdate =
409               $datearr[5] . '-'
410               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
411               . sprintf( "%0.2d", $datearr[3] );
412             $ri{duedate}=$duedate;
413             my ($borrower) =
414               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
415             $ri{bornum}       = $borrower->{'borrowernumber'};
416             $ri{borcnum}      = $borrower->{'cardnumber'};
417             $ri{borfirstname} = $borrower->{'firstname'};
418             $ri{borsurname}   = $borrower->{'surname'};
419             $ri{bortitle}     = $borrower->{'title'};
420         }
421         else {
422             $ri{bornum} = $riborrowernumber{$_};
423         }
424 #        my %ri;
425         my ($iteminformation) = getiteminformation( \%env, 0, $barcode );
426         $ri{color}            = $color;
427         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
428         $ri{itemtitle}        = $iteminformation->{'title'};
429         $ri{itemauthor}       = $iteminformation->{'author'};
430         $ri{itemtype}         = $iteminformation->{'itemtype'};
431         $ri{barcode}          = $barcode;
432     }
433     else {
434         last;
435     }
436     $count++;
437     push ( @riloop, \%ri );
438 }
439 $template->param( riloop => \@riloop );
440
441 $template->param(
442     genbrname  => $branches->{$branch}->{'branchname'},
443     genprname  => $printers->{$printer}->{'printername'},
444     branch     => $branch,
445     printer    => $printer,
446     errmsgloop => \@errmsgloop
447 );
448
449 # actually print the page!
450 output_html_with_http_headers $query, $cookie, $template->output;
451
452 sub cuecatbarcodedecode {
453     my ($barcode) = @_;
454     chomp($barcode);
455     my @fields = split ( /\./, $barcode );
456     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
457     if ( $#results == 2 ) {
458         return $results[2];
459     }
460     else {
461         return $barcode;
462     }
463 }
464
465 # Local Variables:
466 # tab-width: 4
467 # End: