circulation cleaning continued: bufixing
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 returns.pl
21
22 script to execute returns of books
23
24 written 11/3/2002 by Finlay
25
26 =cut
27
28 use strict;
29 use CGI;
30 use C4::Circulation;
31 use C4::Date;
32 use C4::Output;
33 use C4::Print;
34 use C4::Reserves2;
35 use C4::Auth;
36 use C4::Biblio;
37 use C4::Members;
38 use C4::Interface::CGI::Output;
39 use C4::Branch; # GetBranchName
40 use C4::Koha;   # FIXME : is it still useful ?
41 use C4::Context;
42
43 my $query = new CGI;
44
45 #getting the template
46 my ( $template, $librarian, $cookie ) = get_template_and_user(
47     {
48         template_name   => "circ/returns.tmpl",
49         query           => $query,
50         type            => "intranet",
51         authnotrequired => 0,
52         flagsrequired   => { circulate => 1 },
53     }
54 );
55
56 #####################
57 #Global vars
58 my %env;
59
60 my $branches = GetBranches();
61 my $printers = GetPrinters( \%env );
62
63 #my $branch  = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
64 my $printer = C4::Context->userenv?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
75 # Set up the item stack ....
76 my %returneditems;
77 my %riduedate;
78 my %riborrowernumber;
79 my @inputloop;
80 foreach ( $query->param ) {
81     (next) unless (/ri-(\d*)/);
82     my %input;
83     my $counter = $1;
84     (next) if ( $counter > 20 );
85     my $barcode        = $query->param("ri-$counter");
86     my $duedate        = $query->param("dd-$counter");
87     my $borrowernumber = $query->param("bn-$counter");
88     $counter++;
89
90     # decode cuecat
91     $barcode = cuecatbarcodedecode($barcode);
92
93     ######################
94     #Are these lines still useful ?
95     $returneditems{$counter}    = $barcode;
96     $riduedate{$counter}        = $duedate;
97     $riborrowernumber{$counter} = $borrowernumber;
98
99     #######################
100     $input{counter}        = $counter;
101     $input{barcode}        = $barcode;
102     $input{duedate}        = $duedate;
103     $input{borrowernumber} = $borrowernumber;
104     push( @inputloop, \%input );
105 }
106
107 ############
108 # Deal with the requests....
109
110 if ($query->param('WT-itemNumber')){
111 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
112 }
113
114
115 if ( $query->param('resbarcode') ) {
116     my $item           = $query->param('itemnumber');
117     my $borrowernumber = $query->param('borrowernumber');
118     my $resbarcode     = $query->param('resbarcode');
119     my $diffBranchReturned = $query->param('diffBranch');
120     # set to waiting....
121     my $iteminfo   = GetBiblioFromItemNumber($item);
122     my $diffBranchSend;
123     
124 #     addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
125 # if not we send a value in reserve waiting for not implementting waiting status
126         if ($diffBranchReturned) {
127         $diffBranchSend = $diffBranchReturned;
128         }
129         else {
130         $diffBranchSend = undef;
131         }
132                  
133     my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
134 #   check if we have other reservs for this document, if we have a return send the message of transfer
135     my ( $messages, $nextreservinfo ) = OtherReserves($item);
136
137     my $branchname = GetBranchName( $messages->{'transfert'} );
138     my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
139     my $borcnum = $borr->{'cardnumber'};
140     my $name    =
141       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
142     my $slip = $query->param('resslip');
143
144
145     if ( $messages->{'transfert'} ) {
146         $template->param(
147             itemtitle      => $iteminfo->{'title'},
148             iteminfo       => $iteminfo->{'author'},
149             tobranchname   => $branchname,
150             name           => $name,
151             borrowernumber => $borrowernumber,
152             borcnum        => $borcnum,
153             borfirstname   => $borr->{'firstname'},
154             borsurname     => $borr->{'surname'},
155             diffbranch     => 1
156         );
157     }
158 }
159
160 my $borrower;
161 my $returned = 0;
162 my $messages;
163 my $issueinformation;
164 my $barcode = $query->param('barcode');
165
166 # actually return book and prepare item table.....
167 if ($barcode) {
168     # decode cuecat
169     $barcode = cuecatbarcodedecode($barcode);
170 #
171 # save the return
172 #
173     ( $returned, $messages, $issueinformation, $borrower ) =
174       AddReturn( $barcode, C4::Context->userenv->{'branch'} );
175     # get biblio description
176     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
177     $template->param(
178         title            => $biblio->{'title'},
179         homebranch       => $biblio->{'homebranch'},
180         author           => $biblio->{'author'},
181         itembarcode      => $biblio->{'barcode'},
182         itemtype         => $biblio->{'itemtype'},
183         ccode            => $biblio->{'ccode'},
184         itembiblionumber => $biblio->{'biblionumber'},    
185     );
186     if ($returned) {
187         $returneditems{0}    = $barcode;
188         $riborrowernumber{0} = $borrower->{'borrowernumber'};
189         $riduedate{0}        = $issueinformation->{'date_due'};
190         my %input;
191         $input{counter}        = 0;
192         $input{first}          = 1;
193         $input{barcode}        = $barcode;
194         $input{duedate}        = $riduedate{0};
195         $input{borrowernumber} = $riborrowernumber{0};
196         push( @inputloop, \%input );
197
198         # check if the branch is the same as homebranch
199         # if not, we want to put a message
200         if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
201             $template->param( homebranch => $biblio->{'homebranch'} );
202         }
203     }
204     elsif ( !$messages->{'BadBarcode'} ) {
205         my %input;
206         $input{counter} = 0;
207         $input{first}   = 1;
208         $input{barcode} = $barcode;
209         $input{duedate} = 0;
210
211         $returneditems{0} = $barcode;
212         $riduedate{0}     = 0;
213         if ( $messages->{'wthdrawn'} ) {
214             $input{withdrawn}      = 1;
215             $input{borrowernumber} = "Item Cancelled";
216             $riborrowernumber{0}   = 'Item Cancelled';
217         }
218         else {
219             $input{borrowernumber} = " ";
220             $riborrowernumber{0} = ' ';
221         }
222         push( @inputloop, \%input );
223     }
224 }
225 $template->param( inputloop => \@inputloop );
226
227 my $found    = 0;
228 my $waiting  = 0;
229 my $reserved = 0;
230
231 # new op dev : we check if the document must be returned to his homebranch directly,
232 #  if the document is transfered, we have warning message .
233
234 if ( $messages->{'WasTransfered'} ) {
235     $template->param(
236         found          => 1,
237         transfer       => 1,
238     );
239
240 }
241
242 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
243
244 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
245         $template->param(
246         WrongTransfer  => 1,
247         TransferWaitingAt => $messages->{'WrongTransfer'},
248         WrongTransferItem => $messages->{'WrongTransferItem'},
249     );
250
251     my $reserve        = $messages->{'ResFound'};
252     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
253     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
254     my $name =
255       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
256         $template->param(
257             wname           => $name,
258             wborfirstname   => $borr->{'firstname'},
259             wborsurname     => $borr->{'surname'},
260             wbortitle       => $borr->{'title'},
261             wborphone       => $borr->{'phone'},
262             wboremail       => $borr->{'emailaddress'},
263             wborstraddress  => $borr->{'streetaddress'},
264             wborcity        => $borr->{'city'},
265             wborzip         => $borr->{'zipcode'},
266             wborrowernumber => $reserve->{'borrowernumber'},
267             wborcnum        => $borr->{'cardnumber'},
268             wtransfertFrom    => C4::Context->userenv->{'branch'},
269         );
270 }
271
272
273 #
274 # reserve found and item arrived at the expected branch
275 #
276 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
277     my $reserve        = $messages->{'ResFound'};
278     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
279     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
280     if ( $reserve->{'ResFound'} eq "Waiting" ) {
281         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
282             $template->param( waiting => 1 );
283         }
284         else {
285             $template->param( waiting => 0 );
286         }
287
288         $template->param(
289             found          => 1,
290             name           => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
291             borfirstname   => $borr->{'firstname'},
292             borsurname     => $borr->{'surname'},
293             bortitle       => $borr->{'title'},
294             borphone       => $borr->{'phone'},
295             boremail       => $borr->{'emailaddress'},
296             borstraddress  => $borr->{'streetaddress'},
297             borcity        => $borr->{'city'},
298             borzip         => $borr->{'zipcode'},
299             borrowernumber => $reserve->{'borrowernumber'},
300             borcnum        => $borr->{'cardnumber'},
301             debarred       => $borr->{'debarred'},
302             gonenoaddress  => $borr->{'gonenoaddress'},
303             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
304             itemnumber       => $reserve->{'itemnumber'},
305         );
306
307     }
308     if ( $reserve->{'ResFound'} eq "Reserved" ) {
309         my @da         = localtime( time() );
310         my $todaysdate =
311             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
312           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
313           . ( $da[5] + 1900 );
314
315         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
316             $template->param( intransit => 0 );
317         }
318         else {
319             $template->param( intransit => 1 );
320         }
321
322         $template->param(
323             found          => 1,
324             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
325             destbranchname =>
326               $branches->{ $reserve->{'branchcode'} }->{'branchname'},
327             destbranch     => $reserve->{'branchcode'},
328             transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
329             reserved => 1,
330             today            => $todaysdate,
331             itemnumber       => $reserve->{'itemnumber'},
332             borsurname       => $borr->{'surname'},
333             bortitle         => $borr->{'title'},
334             borfirstname     => $borr->{'firstname'},
335             borrowernumber   => $reserve->{'borrowernumber'},
336             borcnum          => $borr->{'cardnumber'},
337             borphone         => $borr->{'phone'},
338             borstraddress    => $borr->{'streetaddress'},
339             borsub           => $borr->{'suburb'},
340             borcity          => $borr->{'city'},
341             borzip           => $borr->{'zipcode'},
342             boremail         => $borr->{'emailaddress'},
343             debarred         => $borr->{'debarred'},
344             gonenoaddress    => $borr->{'gonenoaddress'},
345             barcode          => $barcode
346         );
347     }
348 }
349
350 # Error Messages
351 my @errmsgloop;
352 foreach my $code ( keys %$messages ) {
353
354     #    warn $code;
355     my %err;
356     my $exit_required_p = 0;
357     if ( $code eq 'BadBarcode' ) {
358         $err{badbarcode} = 1;
359         $err{msg}        = $messages->{'BadBarcode'};
360     }
361     elsif ( $code eq 'NotIssued' ) {
362         $err{notissued} = 1;
363         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
364     }
365     elsif ( $code eq 'WasLost' ) {
366         $err{waslost} = 1;
367     }
368     elsif ( $code eq 'ResFound' ) {
369         ;    # FIXME... anything to do here?
370     }
371     elsif ( $code eq 'WasReturned' ) {
372         ;    # FIXME... anything to do here?
373     }
374     elsif ( $code eq 'WasTransfered' ) {
375         ;    # FIXME... anything to do here?
376     }
377     elsif ( $code eq 'wthdrawn' ) {
378         $err{withdrawn} = 1;
379         $exit_required_p = 1;
380     }
381     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
382         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
383             $err{ispermanent} = 1;
384             $err{msg}         =
385               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
386         }
387     }
388     elsif ( $code eq 'WrongTransfer' ) {
389         ;    # FIXME... anything to do here?
390     }
391     elsif ( $code eq 'WrongTransferItem' ) {
392         ;    # FIXME... anything to do here?
393     }
394     else {
395         die "Unknown error code $code";    # XXX
396     }
397     if (%err) {
398         push( @errmsgloop, \%err );
399     }
400     last if $exit_required_p;
401 }
402 $template->param( errmsgloop => \@errmsgloop );
403
404 # patrontable ....
405 if ($borrower) {
406     my $flags = $borrower->{'flags'};
407     my @flagloop;
408     my $flagset;
409     foreach my $flag ( sort keys %$flags ) {
410         my %flaginfo;
411         unless ($flagset) { $flagset = 1; }
412         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
413         $flaginfo{flag}    = $flag;
414         if ( $flag eq 'CHARGES' ) {
415             $flaginfo{msg}            = $flag;
416             $flaginfo{charges}        = 1;
417             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
418         }
419         elsif ( $flag eq 'WAITING' ) {
420             $flaginfo{msg}     = $flag;
421             $flaginfo{waiting} = 1;
422             my @waitingitemloop;
423             my $items = $flags->{$flag}->{'itemlist'};
424             foreach my $item (@$items) {
425                 my $biblio =
426                   GetBiblioFromItemNumber( $item->{'itemnumber'});
427                 my %waitingitem;
428                 $waitingitem{biblionum} = $biblio->{'biblionumber'};
429                 $waitingitem{barcode}   = $biblio->{'barcode'};
430                 $waitingitem{title}     = $biblio->{'title'};
431                 $waitingitem{brname}    =
432                   $branches->{ $biblio->{'holdingbranch'} }
433                   ->{'branchname'};
434                 push( @waitingitemloop, \%waitingitem );
435             }
436             $flaginfo{itemloop} = \@waitingitemloop;
437         }
438         elsif ( $flag eq 'ODUES' ) {
439             my $items = $flags->{$flag}->{'itemlist'};
440             my @itemloop;
441             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
442                 @$items )
443             {
444                 my $biblio =
445                   GetBiblioFromItemNumber( $item->{'itemnumber'});
446                 my %overdueitem;
447                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
448                 $overdueitem{biblionum} = $biblio->{'biblionumber'};
449                 $overdueitem{barcode}   = $biblio->{'barcode'};
450                 $overdueitem{title}     = $biblio->{'title'};
451                 $overdueitem{brname}    =
452                   $branches->{ $biblio->{'holdingbranch'} }
453                   ->{'branchname'};
454                 push( @itemloop, \%overdueitem );
455             }
456             $flaginfo{itemloop} = \@itemloop;
457             $flaginfo{overdue}  = 1;
458         }
459         else {
460             $flaginfo{other} = 1;
461             $flaginfo{msg}   = $flags->{$flag}->{'message'};
462         }
463         push( @flagloop, \%flaginfo );
464     }
465     $template->param(
466         flagset          => $flagset,
467         flagloop         => \@flagloop,
468         riborrowernumber => $borrower->{'borrowernumber'},
469         riborcnum        => $borrower->{'cardnumber'},
470         riborsurname     => $borrower->{'surname'},
471         ribortitle       => $borrower->{'title'},
472         riborfirstname   => $borrower->{'firstname'}
473     );
474 }
475
476 #set up so only the last 8 returned items display (make for faster loading pages)
477 my $count = 0;
478 my @riloop;
479 foreach ( sort { $a <=> $b } keys %returneditems ) {
480     my %ri;
481     if ( $count < 8 ) {
482         my $barcode = $returneditems{$_};
483         my $duedate = $riduedate{$_};
484         my $overduetext;
485         my $borrowerinfo;
486         if ($duedate) {
487             my @tempdate = split( /-/, $duedate );
488             $ri{year}  = $tempdate[0];
489             $ri{month} = $tempdate[1];
490             $ri{day}   = $tempdate[2];
491             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
492             my @datearr    = localtime( time() );
493             my $todaysdate =
494                 $datearr[5] . '-'
495               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
496               . sprintf( "%0.2d", $datearr[3] );
497             $ri{duedate} = format_date($duedate);
498             my ($borrower) =
499               GetMemberDetails( $riborrowernumber{$_}, 0 );
500             $ri{borrowernumber} = $borrower->{'borrowernumber'};
501             $ri{borcnum}        = $borrower->{'cardnumber'};
502             $ri{borfirstname}   = $borrower->{'firstname'};
503             $ri{borsurname}     = $borrower->{'surname'};
504             $ri{bortitle}       = $borrower->{'title'};
505         }
506         else {
507             $ri{borrowernumber} = $riborrowernumber{$_};
508         }
509
510         #        my %ri;
511         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
512         $ri{itembiblionumber} = $biblio->{'biblionumber'};
513         $ri{itemtitle}        = $biblio->{'title'};
514         $ri{itemauthor}       = $biblio->{'author'};
515         $ri{itemtype}         = $biblio->{'itemtype'};
516         $ri{ccode}            = $biblio->{'ccode'};
517         $ri{barcode}          = $barcode;
518     }
519     else {
520         last;
521     }
522     $count++;
523     push( @riloop, \%ri );
524 }
525 $template->param( riloop => \@riloop );
526
527 $template->param(
528     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
529     genprname               => $printers->{$printer}->{'printername'},
530     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
531     printer                 => $printer,
532     errmsgloop              => \@errmsgloop,
533     intranetcolorstylesheet =>
534       C4::Context->preference("intranetcolorstylesheet"),
535     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
536     IntranetNav        => C4::Context->preference("IntranetNav"),
537 );
538
539 # actually print the page!
540 output_html_with_http_headers $query, $cookie, $template->output;
541
542 sub cuecatbarcodedecode {
543     my ($barcode) = @_;
544     chomp($barcode);
545     my @fields = split( /\./, $barcode );
546     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
547     if ( $#results == 2 ) {
548         return $results[2];
549     }
550     else {
551         return $barcode;
552     }
553 }