new fonctions for returns.pl :
[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  = C4::Context->userenv->{'branch'};
63 my $printer = C4::Context->userenv->{'branchprinter'};
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 #     if($iteminfo->{'holdingbranch'} ne $branch){
115 #       UpdateHoldingbranch($branch,$item);
116 #       }
117 #   check if we have other reservs for this document, if we have a return send the message of transfer
118     my ($messages,$nextreservinfo) = OtherReserves($item);
119     my $branchname = getbranchname($messages->{'transfert'});
120     my ($borr) = getpatroninformation( \%env, $nextreservinfo, 0 );
121     my $borcnum = $borr->{'cardnumber'};
122     my $name    =
123       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
124     my $slip = $query->param('resslip');
125 #    printslip( \%env, $slip ); #removed by paul
126
127     if ( $messages->{'transfert'} ) {
128 #       add the transfer routine
129 #       C4::Circulation::Circ2::dotransfer($item,$iteminfo->{'holdingbranch'},$tobranchcd);
130         $template->param(
131             itemtitle  => $iteminfo->{'title'},
132             iteminfo   => $iteminfo->{'author'},
133             branchname => $branchname,
134             name       => $name,
135             bornum     => $borrnum,
136             borcnum    => $borcnum,
137             borfirstname  => $borr->{'firstname'},
138             borsurname    => $borr->{'surname'},
139             diffbranch => 1
140         );
141     }
142 }
143
144 my $iteminformation;
145 my $borrower;
146 my $returned = 0;
147 my $messages;
148 my $barcode = $query->param('barcode');
149
150 # actually return book and prepare item table.....
151 if ($barcode) {
152     # decode cuecat
153     $barcode = cuecatbarcodedecode($barcode);
154     ( $returned, $messages, $iteminformation, $borrower ) =
155       returnbook( $barcode, $branch );
156     if ($returned) {
157         $returneditems{0}    = $barcode;
158         $riborrowernumber{0} = $borrower->{'borrowernumber'};
159         $riduedate{0}        = $iteminformation->{'date_due'};
160         my %input;
161         $input{counter} = 0;
162         $input{first}   = 1;
163         $input{barcode} = $barcode;
164         $input{duedate} = $riduedate{0};
165         $input{bornum}  = $riborrowernumber{0};
166         push ( @inputloop, \%input );
167     }
168     elsif ( !$messages->{'BadBarcode'} ) {
169         my %input;
170         $input{counter} = 0;
171         $input{first}   = 1;
172         $input{barcode} = $barcode;
173         $input{duedate} = 0;
174
175         $returneditems{0} = $barcode;
176         $riduedate{0}     = 0;
177         if ( $messages->{'wthdrawn'} ) {
178             $input{withdrawn} = 1;
179             $input{bornum}    = "Item Cancelled";
180             $riborrowernumber{0} = 'Item Cancelled';
181         }
182         else {
183             $input{bornum} = " ";
184             $riborrowernumber{0} = ' ';
185         }
186         push ( @inputloop, \%input );
187     }
188     $template->param(
189         returned  => $returned,
190         itemtitle => $iteminformation->{'title'},
191
192         #                                                                       itembc => $iteminformation->{'barcode'},
193         #                                                                       itemdatedue => $iteminformation->{'datedue'},
194         itemauthor => $iteminformation->{'author'}
195     );
196 }
197 $template->param( inputloop => \@inputloop );
198
199 my $found    = 0;
200 my $waiting  = 0;
201 my $reserved = 0;
202
203 # new op dev : we check if the document must be returned to his homebranch directly,
204 #  if the document is transfered, we have warning message .
205
206 if ( $messages->{'WasTransfered'} ) {
207 #     my $res        = $messages->{'ResFound'};
208 #     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
209 #     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
210 #     my $name =
211 #       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
212     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
213
214 #     if ( $res->{'ResFound'} eq "Waiting" ) {
215 #               if($branch eq $res->{'branchcode'}){
216 #                       $template->param(intransit => 0);
217 #               } else {
218 #                       $template->param(intransit => 1);
219 #               }
220         
221         $template->param(
222                 found         => 1,
223                 transfer     => 1,
224                 itemhomebranch => $branches->{$iteminfo->{'homebranch'} }->{'branchname'}
225         );
226
227     }
228
229 if ( $messages->{'ResFound'} ) {
230     my $res        = $messages->{'ResFound'};
231     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
232     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
233     my $name =
234       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
235     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
236
237     if ( $res->{'ResFound'} eq "Waiting" ) {
238                 if($branch eq $res->{'branchcode'}){
239                         $template->param(intransit => 0);
240                 } else {
241                         $template->param(intransit => 1);
242                 }
243         
244         $template->param(
245             found         => 1,
246             name          => $name,
247             borfirstname  => $borr->{'firstname'},
248             borsurname    => $borr->{'surname'},
249             bortitle      => $borr->{'title'},
250             borphone      => $borr->{'phone'},
251             boremail         => $borr->{'emailaddress'},
252             borstraddress => $borr->{'streetaddress'},
253             borcity       => $borr->{'city'},
254             borzip        => $borr->{'zipcode'},
255             bornum        => $res->{'borrowernumber'},
256             borcnum       => $borr->{'cardnumber'},
257                         debarred => $borr->{'debarred'},
258                         gonenoaddress => $borr->{'gonenoaddress'},
259                         currentbranch => $branches->{ $branch }->{'branchname'},
260             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
261             waiting     => 1,
262             itemnumber  => $res->{'itemnumber'},
263             itemtitle   => $iteminfo->{'title'},
264             itemauthor  => $iteminfo->{'author'},
265             itembarcode => $iteminfo->{'barcode'},
266             itemtype    => $iteminfo->{'itemtype'},
267             itembiblionumber => $iteminfo->{'biblionumber'}
268         );
269
270     }
271     if ( $res->{'ResFound'} eq "Reserved" ) {
272         my @da         = localtime( time() );
273         my $todaysdate =
274           sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
275           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
276           . ( $da[5] + 1900 );
277                   
278                  if($branch eq $res->{'branchcode'}){
279                         $template->param(intransit => 0);
280                 } else {
281                         $template->param(intransit => 1);
282                 }
283                   
284         $template->param(
285             found       => 1,
286                 currentbranch => $branches->{ $branch }->{'branchname'},
287             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
288             reserved    => 1,
289             today       => $todaysdate,
290             itemnumber  => $res->{'itemnumber'},
291             itemtitle   => $iteminfo->{'title'},
292             itemauthor  => $iteminfo->{'author'},
293             itembarcode => $iteminfo->{'barcode'},
294             itemtype    => $iteminfo->{'itemtype'},
295             itembiblionumber => $iteminfo->{'biblionumber'},
296             borsurname       => $borr->{'surname'},
297             bortitle         => $borr->{'title'},
298             borfirstname     => $borr->{'firstname'},
299             bornum           => $res->{'borrowernumber'},
300             borcnum          => $borr->{'cardnumber'},
301             borphone         => $borr->{'phone'},
302             borstraddress    => $borr->{'streetaddress'},
303             borsub           => $borr->{'suburb'},
304             borcity          => $borr->{'city'},
305             borzip           => $borr->{'zipcode'},
306             boremail         => $borr->{'emailaddress'},
307                         debarred => $borr->{'debarred'},
308                         gonenoaddress => $borr->{'gonenoaddress'},
309             barcode          => $barcode
310         );
311     }
312 }
313
314 # Error Messages
315 my @errmsgloop;
316 foreach my $code ( keys %$messages ) {
317
318     #    warn $code;
319     my %err;
320     my $exit_required_p = 0;
321     if ( $code eq 'BadBarcode' ) {
322         $err{badbarcode} = 1;
323         $err{msg}        = $messages->{'BadBarcode'};
324     }
325     elsif ( $code eq 'NotIssued' ) {
326         $err{notissued} = 1;
327         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
328     }
329     elsif ( $code eq 'WasLost' ) {
330         $err{waslost} = 1;
331     }
332     elsif ( $code eq 'ResFound' ) {
333         ;    # FIXME... anything to do here?
334     }
335     elsif ( $code eq 'WasReturned' ) {
336         ;    # FIXME... anything to do here?
337     }
338     elsif ( $code eq 'WasTransfered' ) {
339         ;    # FIXME... anything to do here?
340     }
341     elsif ( $code eq 'wthdrawn' ) {
342         $err{withdrawn} = 1;
343         $exit_required_p = 1;
344     }
345     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
346         if ( $messages->{'IsPermanent'} ne $branch ) {
347             $err{ispermanent} = 1;
348             $err{msg}         =
349               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
350         }
351     }
352     else {
353         die "Unknown error code $code";    # XXX
354     }
355     if (%err) {
356         push ( @errmsgloop, \%err );
357     }
358     last if $exit_required_p;
359 }
360 $template->param( errmsgloop => \@errmsgloop );
361
362 # patrontable ....
363 if ($borrower) {
364     my $flags = $borrower->{'flags'};
365     my $color = '';
366     my @flagloop;
367     my $flagset;
368     foreach my $flag ( sort keys %$flags ) {
369         my %flaginfo;
370         ( $color eq $linecolor1 ) 
371           ? ( $color = $linecolor2 )
372           : ( $color = $linecolor1 );
373         unless ($flagset) { $flagset = 1; }
374         $flaginfo{color}   = $color;
375         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
376         $flaginfo{flag}    = $flag;
377         if ( $flag eq 'CHARGES' ) {
378             $flaginfo{msg}     = $flag;
379             $flaginfo{charges} = 1;
380             $flaginfo{bornum} = $borrower->{borrowernumber};
381         }
382         elsif ( $flag eq 'WAITING' ) {
383             $flaginfo{msg}     = $flag;
384             $flaginfo{waiting} = 1;
385             my @waitingitemloop;
386             my $items = $flags->{$flag}->{'itemlist'};
387             foreach my $item (@$items) {
388                 my ($iteminformation) =
389                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
390                 my %waitingitem;
391                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
392                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
393                 $waitingitem{title}     = $iteminformation->{'title'};
394                 $waitingitem{brname}    =
395                   $branches->{ $iteminformation->{'holdingbranch'} }->{
396                   'branchname'};
397                 push ( @waitingitemloop, \%waitingitem );
398             }
399             $flaginfo{itemloop} = \@waitingitemloop;
400         }
401         elsif ( $flag eq 'ODUES' ) {
402             my $items = $flags->{$flag}->{'itemlist'};
403             my @itemloop;
404             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
405                 @$items )
406             {
407                 my ($iteminformation) =
408                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
409                 my %overdueitem;
410                 $overdueitem{duedate}   = format_date($item->{'date_due'});
411                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
412                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
413                 $overdueitem{title}     = $iteminformation->{'title'};
414                 $overdueitem{brname}    =
415                   $branches->{ $iteminformation->{'holdingbranch'} }->{
416                   'branchname'};
417                 push ( @itemloop, \%overdueitem );
418             }
419             $flaginfo{itemloop} = \@itemloop;
420             $flaginfo{overdue}  = 1;
421         }
422         else {
423             $flaginfo{other} = 1;
424             $flaginfo{msg}   = $flags->{$flag}->{'message'};
425         }
426         push ( @flagloop, \%flaginfo );
427     }
428     $template->param(
429         flagset        => $flagset,
430         flagloop       => \@flagloop,
431         ribornum       => $borrower->{'borrowernumber'},
432         riborcnum      => $borrower->{'cardnumber'},
433         riborsurname   => $borrower->{'surname'},
434         ribortitle     => $borrower->{'title'},
435         riborfirstname => $borrower->{'firstname'}
436     );
437 }
438
439 my $color = '';
440
441 #set up so only the last 8 returned items display (make for faster loading pages)
442 my $count = 0;
443 my @riloop;
444 foreach ( sort { $a <=> $b } keys %returneditems ) {
445     my %ri;
446     if ( $count < 8 ) {
447         ( $color eq $linecolor1 ) 
448           ? ( $color = $linecolor2 )
449           : ( $color = $linecolor1 );
450         $ri{color} = $color;
451         my $barcode = $returneditems{$_};
452         my $duedate = $riduedate{$_};
453         my $overduetext;
454         my $borrowerinfo;
455         if ($duedate) {
456             my @tempdate = split ( /-/, $duedate );
457             $ri{year}=$tempdate[0];
458             $ri{month}=$tempdate[1];
459             $ri{day}=$tempdate[2];
460             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
461             my @datearr    = localtime( time() );
462             my $todaysdate =
463               $datearr[5] . '-'
464               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
465               . sprintf( "%0.2d", $datearr[3] );
466             $ri{duedate}=format_date($duedate);
467             my ($borrower) =
468               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
469             $ri{bornum}       = $borrower->{'borrowernumber'};
470             $ri{borcnum}      = $borrower->{'cardnumber'};
471             $ri{borfirstname} = $borrower->{'firstname'};
472             $ri{borsurname}   = $borrower->{'surname'};
473             $ri{bortitle}     = $borrower->{'title'};
474         }
475         else {
476             $ri{bornum} = $riborrowernumber{$_};
477         }
478 #        my %ri;
479         my ($iteminformation) = getiteminformation( \%env, 0, $barcode );
480         $ri{color}            = $color;
481         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
482         $ri{itemtitle}        = $iteminformation->{'title'};
483         $ri{itemauthor}       = $iteminformation->{'author'};
484         $ri{itemtype}         = $iteminformation->{'itemtype'};
485         $ri{barcode}          = $barcode;
486     }
487     else {
488         last;
489     }
490     $count++;
491     push ( @riloop, \%ri );
492 }
493 $template->param( riloop => \@riloop );
494
495 $template->param(
496     genbrname  => $branches->{$branch}->{'branchname'},
497     genprname  => $printers->{$printer}->{'printername'},
498     branch     => $branch,
499     printer    => $printer,
500     errmsgloop => \@errmsgloop
501 );
502
503 # actually print the page!
504 output_html_with_http_headers $query, $cookie, $template->output;
505
506 sub cuecatbarcodedecode {
507     my ($barcode) = @_;
508     chomp($barcode);
509     my @fields = split ( /\./, $barcode );
510     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
511     if ( $#results == 2 ) {
512         return $results[2];
513     }
514     else {
515         return $barcode;
516     }
517 }
518
519 # Local Variables:
520 # tab-width: 4
521 # End: