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