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