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