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