Sub renamed according to the coding guidelines
[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 use C4::Context;
39
40 my $query = new CGI;
41
42 #getting the template
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {
45         template_name   => "circ/returns.tmpl",
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { circulate => 1 },
50     }
51 );
52
53 #####################
54 #Global vars
55 my %env;
56 my $headerbackgroundcolor = '#99cc33';
57 my $linecolor1            = '#ffffcc';
58 my $linecolor2            = 'white';
59
60 my $branches = getbranches('IS');
61 my $printers = getprinters( \%env );
62
63 my $branch  = C4::Context->userenv->{'branch'};
64 my $printer = C4::Context->userenv->{'branchprinter'};
65
66 #
67 # Some code to handle the error if there is no branch or printer setting.....
68 #
69
70 $env{'branchcode'} = $branch;
71 $env{'printer'}    = $printer;
72 $env{'queue'}      = $printer;
73
74 # Set up the item stack ....
75 my %returneditems;
76 my %riduedate;
77 my %riborrowernumber;
78 my @inputloop;
79 foreach ( $query->param ) {
80     (next) unless (/ri-(\d*)/);
81     my %input;
82     my $counter = $1;
83     (next) if ( $counter > 20 );
84     my $barcode        = $query->param("ri-$counter");
85     my $duedate        = $query->param("dd-$counter");
86     my $borrowernumber = $query->param("bn-$counter");
87     $counter++;
88
89     # decode cuecat
90     $barcode = cuecatbarcodedecode($barcode);
91
92     ######################
93     #Are these lines still useful ?
94     $returneditems{$counter}    = $barcode;
95     $riduedate{$counter}        = $duedate;
96     $riborrowernumber{$counter} = $borrowernumber;
97
98     #######################
99     $input{counter} = $counter;
100     $input{barcode} = $barcode;
101     $input{duedate} = $duedate;
102     $input{bornum}  = $borrowernumber;
103     push ( @inputloop, \%input );
104 }
105
106 ############
107 # Deal with the requests....
108 if ( $query->param('resbarcode') ) {
109     my $item       = $query->param('itemnumber');
110     my $borrnum    = $query->param('borrowernumber');
111     my $resbarcode = $query->param('resbarcode');
112
113     # set to waiting....
114     my $iteminfo = getiteminformation( \%env, $item );
115     my $tobranchcd = ReserveWaiting( $item, $borrnum );
116 #     if($iteminfo->{'holdingbranch'} ne $branch){
117 #       UpdateHoldingbranch($branch,$item);
118 #       }
119 #   check if we have other reservs for this document, if we have a return send the message of transfer
120     my ($messages,$nextreservinfo) = OtherReserves($item);
121     # my $branchname = $branches->{$tobranchcd}->{'branchname'}; - not sure if this line + $tobranchcd line do the same thing
122     # as the $messages{'transfert'} code
123     my $branchname = getbranchname($messages->{'transfert'});
124     my ($borr) = getpatroninformation( \%env, $nextreservinfo, 0 );
125     my $borcnum = $borr->{'cardnumber'};
126     my $name    =
127       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
128     my $slip = $query->param('resslip');
129     printreserve( \%env, $branchname, $borr, $iteminfo );
130 #   if ( $tobranchcd ne $branch ) { - not sure if line below is doing the same
131     if ( $messages->{'transfert'} ) {
132 #       add the transfer routine
133 #       C4::Circulation::Circ2::dotransfer($item,$iteminfo->{'holdingbranch'},$tobranchcd);
134         $template->param(
135             itemtitle  => $iteminfo->{'title'},
136             iteminfo   => $iteminfo->{'author'},
137             tobranchname => $branchname,
138             name       => $name,
139             bornum     => $borrnum,
140             borcnum    => $borcnum,
141             borfirstname  => $borr->{'firstname'},
142             borsurname    => $borr->{'surname'},
143             diffbranch => 1
144         );
145         set_transit($item);
146     }
147 }
148
149 my $iteminformation;
150 my $borrower;
151 my $returned = 0;
152 my $messages;
153 my $barcode = $query->param('barcode');
154
155 # actually return book and prepare item table.....
156 if ($barcode) {
157     # decode cuecat
158     $barcode = cuecatbarcodedecode($barcode);
159     ( $returned, $messages, $iteminformation, $borrower ) =
160       returnbook( $barcode, $branch );
161     if ($returned) {
162         $returneditems{0}    = $barcode;
163         $riborrowernumber{0} = $borrower->{'borrowernumber'};
164         $riduedate{0}        = $iteminformation->{'date_due'};
165         my %input;
166         $input{counter} = 0;
167         $input{first}   = 1;
168         $input{barcode} = $barcode;
169         $input{duedate} = $riduedate{0};
170         $input{bornum}  = $riborrowernumber{0};
171         push ( @inputloop, \%input );
172         # check if the branch is the same as homebranch
173         # if not, we want to put a message
174         if ($iteminformation->{'homebranch'} ne $branch){
175              $template->param( homebranch =>$iteminformation->{'homebranch'});
176         }
177     }
178     elsif ( !$messages->{'BadBarcode'} ) {
179         my %input;
180         $input{counter} = 0;
181         $input{first}   = 1;
182         $input{barcode} = $barcode;
183         $input{duedate} = 0;
184
185         $returneditems{0} = $barcode;
186         $riduedate{0}     = 0;
187         if ( $messages->{'wthdrawn'} ) {
188             $input{withdrawn} = 1;
189             $input{bornum}    = "Item Cancelled";
190             $riborrowernumber{0} = 'Item Cancelled';
191         }
192         else {
193             $input{bornum} = " ";
194             $riborrowernumber{0} = ' ';
195         }
196         push ( @inputloop, \%input );
197     }
198     $template->param(
199         returned  => $returned,
200         itemtitle => $iteminformation->{'title'},
201
202         #                                                                       itembc => $iteminformation->{'barcode'},
203         #                                                                       itemdatedue => $iteminformation->{'datedue'},
204         itemauthor => $iteminformation->{'author'}
205     );
206 }
207 $template->param( inputloop => \@inputloop );
208
209 my $found    = 0;
210 my $waiting  = 0;
211 my $reserved = 0;
212
213 # new op dev : we check if the document must be returned to his homebranch directly,
214 #  if the document is transfered, we have warning message .
215
216 if ( $messages->{'WasTransfered'} ) {
217 #     my $res        = $messages->{'ResFound'};
218 #     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
219 #     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
220 #     my $name =
221 #       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
222     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
223
224 #     if ( $res->{'ResFound'} eq "Waiting" ) {
225 #               if($branch eq $res->{'branchcode'}){
226 #                       $template->param(intransit => 0);
227 #               } else {
228 #                       $template->param(intransit => 1);
229 #               }
230         
231         $template->param(
232                 found         => 1,
233                 transfer     => 1,
234                 itemhomebranch => $branches->{$iteminfo->{'homebranch'} }->{'branchname'}
235         );
236
237     }
238
239 if ( $messages->{'ResFound'} ) {
240     my $res        = $messages->{'ResFound'};
241     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
242     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
243     my $name =
244       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
245     my ($iteminfo) = getiteminformation( \%env, 0, $barcode );
246
247     if ( $res->{'ResFound'} eq "Waiting" ) {
248                 if($branch eq $res->{'branchcode'}){
249                         $template->param(intransit => 0);
250                 } else {
251                         $template->param(intransit => 1);
252                 }
253         
254         $template->param(
255             found         => 1,
256             name          => $name,
257             borfirstname  => $borr->{'firstname'},
258             borsurname    => $borr->{'surname'},
259             bortitle      => $borr->{'title'},
260             borphone      => $borr->{'phone'},
261             boremail         => $borr->{'emailaddress'},
262             borstraddress => $borr->{'streetaddress'},
263             borcity       => $borr->{'city'},
264             borzip        => $borr->{'zipcode'},
265             bornum        => $res->{'borrowernumber'},
266             borcnum       => $borr->{'cardnumber'},
267                         debarred => $borr->{'debarred'},
268                         gonenoaddress => $borr->{'gonenoaddress'},
269                         currentbranch => $branches->{ $branch }->{'branchname'},
270             tobranchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
271             waiting     => 1,
272             itemnumber  => $res->{'itemnumber'},
273             itemtitle   => $iteminfo->{'title'},
274             itemauthor  => $iteminfo->{'author'},
275             itembarcode => $iteminfo->{'barcode'},
276             itemtype    => $iteminfo->{'itemtype'},
277             itembiblionumber => $iteminfo->{'biblionumber'}
278         );
279
280     }
281     if ( $res->{'ResFound'} eq "Reserved" ) {
282         my @da         = localtime( time() );
283         my $todaysdate =
284           sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
285           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
286           . ( $da[5] + 1900 );
287                   
288                  if($branch eq $res->{'branchcode'}){
289                         $template->param(intransit => 0);
290                 } else {
291                         $template->param(intransit => 1);
292                 }
293                   
294         $template->param(
295             found       => 1,
296                 currentbranch => $branches->{ $branch }->{'branchname'},
297             name        => $name,
298             branchname  => $branches->{ $res->{'branchcode'} }->{'branchname'},
299             reserved    => 1,
300             today       => $todaysdate,
301             itemnumber  => $res->{'itemnumber'},
302             itemtitle   => $iteminfo->{'title'},
303             itemauthor  => $iteminfo->{'author'},
304             itembarcode => $iteminfo->{'barcode'},
305             itemtype    => $iteminfo->{'itemtype'},
306             itembiblionumber => $iteminfo->{'biblionumber'},
307             borsurname       => $borr->{'surname'},
308             bortitle         => $borr->{'title'},
309             borfirstname     => $borr->{'firstname'},
310             bornum           => $res->{'borrowernumber'},
311             borcnum          => $borr->{'cardnumber'},
312             borphone         => $borr->{'phone'},
313             borstraddress    => $borr->{'streetaddress'},
314             borsub           => $borr->{'suburb'},
315             borcity          => $borr->{'city'},
316             borzip           => $borr->{'zipcode'},
317             boremail         => $borr->{'emailaddress'},
318                         debarred => $borr->{'debarred'},
319                         gonenoaddress => $borr->{'gonenoaddress'},
320             barcode          => $barcode
321         );
322     }
323 }
324
325 # Error Messages
326 my @errmsgloop;
327 foreach my $code ( keys %$messages ) {
328
329     #    warn $code;
330     my %err;
331     my $exit_required_p = 0;
332     if ( $code eq 'BadBarcode' ) {
333         $err{badbarcode} = 1;
334         $err{msg}        = $messages->{'BadBarcode'};
335     }
336     elsif ( $code eq 'NotIssued' ) {
337         $err{notissued} = 1;
338         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
339     }
340     elsif ( $code eq 'WasLost' ) {
341         $err{waslost} = 1;
342     }
343     elsif ( $code eq 'ResFound' ) {
344         ;    # FIXME... anything to do here?
345     }
346     elsif ( $code eq 'WasReturned' ) {
347         ;    # FIXME... anything to do here?
348     }
349     elsif ( $code eq 'WasTransfered' ) {
350         ;    # FIXME... anything to do here?
351     }
352     elsif ( $code eq 'wthdrawn' ) {
353         $err{withdrawn} = 1;
354         $exit_required_p = 1;
355     }
356     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
357         if ( $messages->{'IsPermanent'} ne $branch ) {
358             $err{ispermanent} = 1;
359             $err{msg}         =
360               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
361         }
362     }
363     else {
364         die "Unknown error code $code";    # XXX
365     }
366     if (%err) {
367         push ( @errmsgloop, \%err );
368     }
369     last if $exit_required_p;
370 }
371 $template->param( errmsgloop => \@errmsgloop );
372
373 # patrontable ....
374 if ($borrower) {
375     my $flags = $borrower->{'flags'};
376     my $color = '';
377     my @flagloop;
378     my $flagset;
379     foreach my $flag ( sort keys %$flags ) {
380         my %flaginfo;
381         ( $color eq $linecolor1 ) 
382           ? ( $color = $linecolor2 )
383           : ( $color = $linecolor1 );
384         unless ($flagset) { $flagset = 1; }
385         $flaginfo{color}   = $color;
386         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
387         $flaginfo{flag}    = $flag;
388         if ( $flag eq 'CHARGES' ) {
389             $flaginfo{msg}     = $flag;
390             $flaginfo{charges} = 1;
391             $flaginfo{bornum} = $borrower->{borrowernumber};
392         }
393         elsif ( $flag eq 'WAITING' ) {
394             $flaginfo{msg}     = $flag;
395             $flaginfo{waiting} = 1;
396             my @waitingitemloop;
397             my $items = $flags->{$flag}->{'itemlist'};
398             foreach my $item (@$items) {
399                 my ($iteminformation) =
400                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
401                 my %waitingitem;
402                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
403                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
404                 $waitingitem{title}     = $iteminformation->{'title'};
405                 $waitingitem{brname}    =
406                   $branches->{ $iteminformation->{'holdingbranch'} }->{
407                   'branchname'};
408                 push ( @waitingitemloop, \%waitingitem );
409             }
410             $flaginfo{itemloop} = \@waitingitemloop;
411         }
412         elsif ( $flag eq 'ODUES' ) {
413             my $items = $flags->{$flag}->{'itemlist'};
414             my @itemloop;
415             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
416                 @$items )
417             {
418                 my ($iteminformation) =
419                   getiteminformation( \%env, $item->{'itemnumber'}, 0 );
420                 my %overdueitem;
421                 $overdueitem{duedate}   = format_date($item->{'date_due'});
422                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
423                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
424                 $overdueitem{title}     = $iteminformation->{'title'};
425                 $overdueitem{brname}    =
426                   $branches->{ $iteminformation->{'holdingbranch'} }->{
427                   'branchname'};
428                 push ( @itemloop, \%overdueitem );
429             }
430             $flaginfo{itemloop} = \@itemloop;
431             $flaginfo{overdue}  = 1;
432         }
433         else {
434             $flaginfo{other} = 1;
435             $flaginfo{msg}   = $flags->{$flag}->{'message'};
436         }
437         push ( @flagloop, \%flaginfo );
438     }
439     $template->param(
440         flagset        => $flagset,
441         flagloop       => \@flagloop,
442         ribornum       => $borrower->{'borrowernumber'},
443         riborcnum      => $borrower->{'cardnumber'},
444         riborsurname   => $borrower->{'surname'},
445         ribortitle     => $borrower->{'title'},
446         riborfirstname => $borrower->{'firstname'}
447     );
448 }
449
450 my $color = '';
451
452 #set up so only the last 8 returned items display (make for faster loading pages)
453 my $count = 0;
454 my @riloop;
455 foreach ( sort { $a <=> $b } keys %returneditems ) {
456     my %ri;
457     if ( $count < 8 ) {
458         ( $color eq $linecolor1 ) 
459           ? ( $color = $linecolor2 )
460           : ( $color = $linecolor1 );
461         $ri{color} = $color;
462         my $barcode = $returneditems{$_};
463         my $duedate = $riduedate{$_};
464         my $overduetext;
465         my $borrowerinfo;
466         if ($duedate) {
467             my @tempdate = split ( /-/, $duedate );
468             $ri{year}=$tempdate[0];
469             $ri{month}=$tempdate[1];
470             $ri{day}=$tempdate[2];
471             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
472             my @datearr    = localtime( time() );
473             my $todaysdate =
474               $datearr[5] . '-'
475               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
476               . sprintf( "%0.2d", $datearr[3] );
477             $ri{duedate}=format_date($duedate);
478             my ($borrower) =
479               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
480             $ri{bornum}       = $borrower->{'borrowernumber'};
481             $ri{borcnum}      = $borrower->{'cardnumber'};
482             $ri{borfirstname} = $borrower->{'firstname'};
483             $ri{borsurname}   = $borrower->{'surname'};
484             $ri{bortitle}     = $borrower->{'title'};
485         }
486         else {
487             $ri{bornum} = $riborrowernumber{$_};
488         }
489 #        my %ri;
490         my ($iteminformation) = getiteminformation( \%env, 0, $barcode );
491         $ri{color}            = $color;
492         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
493         $ri{itemtitle}        = $iteminformation->{'title'};
494         $ri{itemauthor}       = $iteminformation->{'author'};
495         $ri{itemtype}         = $iteminformation->{'itemtype'};
496         $ri{barcode}          = $barcode;
497     }
498     else {
499         last;
500     }
501     $count++;
502     push ( @riloop, \%ri );
503 }
504 $template->param( riloop => \@riloop );
505
506 $template->param(
507     genbrname  => $branches->{$branch}->{'branchname'},
508     genprname  => $printers->{$printer}->{'printername'},
509     branchname     => $branches->{$branch}->{'branchname'},
510     printer    => $printer,
511     errmsgloop => \@errmsgloop,
512     intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
513                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
514                 IntranetNav => C4::Context->preference("IntranetNav"),
515 );
516
517 # actually print the page!
518 output_html_with_http_headers $query, $cookie, $template->output;
519
520 sub cuecatbarcodedecode {
521     my ($barcode) = @_;
522     chomp($barcode);
523     my @fields = split ( /\./, $barcode );
524     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
525     if ( $#results == 2 ) {
526         return $results[2];
527     }
528     else {
529         return $barcode;
530     }
531 }
532
533
534 sub set_transit {
535         my ($itemnumber) = @_;
536         my $dbh=C4::Context->dbh();
537         my $query = "UPDATE items SET holdingbranch='TRA' WHERE itemnumber = ?";
538         my $sth=$dbh->prepare($query);
539         $sth->execute($itemnumber);
540         $sth->finish();
541     }
542
543 sub set_transit {
544         my ($itemnumber) = @_;
545         my $dbh=C4::Context->dbh();
546         my $query = "UPDATE items SET holdingbranch='TRA' WHERE itemnumber = ?";
547         my $sth=$dbh->prepare($query);
548         $sth->execute($itemnumber);
549         $sth->finish();
550 }
551
552
553 # Local Variables:
554 # tab-width: 4
555 # End: