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