Fix for 1704, now if autoitemreturns is off you get a dialogue warning a
[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
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
157 my $dotransfer = $query->param('dotransfer');
158 if ($dotransfer){
159         # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
160         my $transferitem=$query->param('transferitem');
161         my $tobranch=$query->param('tobranch');
162         ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch); 
163 }
164
165 # actually return book and prepare item table.....
166 if ($barcode) {
167     # decode cuecat
168     $barcode = cuecatbarcodedecode($barcode);
169 #
170 # save the return
171 #
172     ( $returned, $messages, $issueinformation, $borrower ) =
173       AddReturn( $barcode, C4::Context->userenv->{'branch'} );
174     # get biblio description
175     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
176     $template->param(
177         title            => $biblio->{'title'},
178         homebranch       => $biblio->{'homebranch'},
179         author           => $biblio->{'author'},
180         itembarcode      => $biblio->{'barcode'},
181         itemtype         => $biblio->{'itemtype'},
182         ccode            => $biblio->{'ccode'},
183         itembiblionumber => $biblio->{'biblionumber'},    
184     );
185     if ($returned) {
186         $returneditems{0}    = $barcode;
187         $riborrowernumber{0} = $borrower->{'borrowernumber'};
188         $riduedate{0}        = $issueinformation->{'date_due'};
189         my %input;
190         $input{counter}        = 0;
191         $input{first}          = 1;
192         $input{barcode}        = $barcode;
193         $input{duedate}        = $riduedate{0};
194         $input{borrowernumber} = $riborrowernumber{0};
195         push( @inputloop, \%input );
196
197         # check if the branch is the same as homebranch
198         # if not, we want to put a message
199         if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
200             $template->param( homebranch => $biblio->{'homebranch'} );
201         }
202     }
203     elsif ( !$messages->{'BadBarcode'} ) {
204         my %input;
205         $input{counter} = 0;
206         $input{first}   = 1;
207         $input{barcode} = $barcode;
208         $input{duedate} = 0;
209
210         $returneditems{0} = $barcode;
211         $riduedate{0}     = 0;
212         if ( $messages->{'wthdrawn'} ) {
213             $input{withdrawn}      = 1;
214             $input{borrowernumber} = "Item Cancelled";
215             $riborrowernumber{0}   = 'Item Cancelled';
216         }
217         else {
218             $input{borrowernumber} = " ";
219             $riborrowernumber{0} = ' ';
220         }
221         push( @inputloop, \%input );
222     }
223 }
224 $template->param( inputloop => \@inputloop );
225
226 my $found    = 0;
227 my $waiting  = 0;
228 my $reserved = 0;
229
230 # new op dev : we check if the document must be returned to his homebranch directly,
231 #  if the document is transfered, we have warning message .
232
233 if ( $messages->{'WasTransfered'} ) {
234     $template->param(
235         found          => 1,
236         transfer       => 1,
237     );
238 }
239
240 if ( $messages->{'NeedsTransfer'} ){
241         $template->param(
242                 found          => 1,
243                 needstransfer  => 1,
244                 itemnumber => $issueinformation->{'itemnumber'}
245         );
246 }
247
248 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
249
250 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
251         $template->param(
252         WrongTransfer  => 1,
253         TransferWaitingAt => $messages->{'WrongTransfer'},
254         WrongTransferItem => $messages->{'WrongTransferItem'},
255     );
256
257     my $reserve        = $messages->{'ResFound'};
258     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
259     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
260     my $name =
261       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
262         $template->param(
263             wname           => $name,
264             wborfirstname   => $borr->{'firstname'},
265             wborsurname     => $borr->{'surname'},
266             wbortitle       => $borr->{'title'},
267             wborphone       => $borr->{'phone'},
268             wboremail       => $borr->{'email'},
269             wboraddress  => $borr->{'address'},
270             wboraddress2 => $borr->{'address2'},
271             wborcity        => $borr->{'city'},
272             wborzip         => $borr->{'zipcode'},
273             wborrowernumber => $reserve->{'borrowernumber'},
274             wborcnum        => $borr->{'cardnumber'},
275             wtransfertFrom    => C4::Context->userenv->{'branch'},
276         );
277 }
278
279
280 #
281 # reserve found and item arrived at the expected branch
282 #
283 if ( $messages->{'ResFound'}) {
284     my $reserve        = $messages->{'ResFound'};
285     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
286     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
287     if ( $reserve->{'ResFound'} eq "Waiting" ) {
288         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
289             $template->param( waiting => 1 );
290         }
291         else {
292             $template->param( waiting => 0 );
293         }
294
295         $template->param(
296             found          => 1,
297             name           => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
298             borfirstname   => $borr->{'firstname'},
299             borsurname     => $borr->{'surname'},
300             bortitle       => $borr->{'title'},
301             borphone       => $borr->{'phone'},
302             boremail       => $borr->{'email'},
303             boraddress  => $borr->{'address'},
304             boraddress2  => $borr->{'address2'},
305             borcity        => $borr->{'city'},
306             borzip         => $borr->{'zipcode'},
307             borrowernumber => $reserve->{'borrowernumber'},
308             borcnum        => $borr->{'cardnumber'},
309             debarred       => $borr->{'debarred'},
310             gonenoaddress  => $borr->{'gonenoaddress'},
311             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
312             itemnumber       => $reserve->{'itemnumber'},
313             barcode     => $barcode,
314         );
315
316     }
317     if ( $reserve->{'ResFound'} eq "Reserved" ) {
318         my @da         = localtime( time() );
319         my $todaysdate =
320             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
321           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
322           . ( $da[5] + 1900 );
323
324         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
325             $template->param( intransit => 0 );
326         }
327         else {
328             $template->param( intransit => 1 );
329         }
330
331         $template->param(
332             found          => 1,
333             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
334             destbranchname =>
335               $branches->{ $reserve->{'branchcode'} }->{'branchname'},
336             destbranch     => $reserve->{'branchcode'},
337             transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
338             reserved => 1,
339             resbarcode       => $barcode,
340             today            => $todaysdate,
341             itemnumber       => $reserve->{'itemnumber'},
342             borsurname       => $borr->{'surname'},
343             bortitle         => $borr->{'title'},
344             borfirstname     => $borr->{'firstname'},
345             borrowernumber   => $reserve->{'borrowernumber'},
346             borcnum          => $borr->{'cardnumber'},
347             borphone         => $borr->{'phone'},
348             boraddress    => $borr->{'address'},
349             boraddress2    => $borr->{'address2'},
350             borsub           => $borr->{'suburb'},
351             borcity          => $borr->{'city'},
352             borzip           => $borr->{'zipcode'},
353             boremail         => $borr->{'email'},
354             debarred         => $borr->{'debarred'},
355             gonenoaddress    => $borr->{'gonenoaddress'},
356             barcode          => $barcode
357         );
358     }
359 }
360
361 # Error Messages
362 my @errmsgloop;
363 foreach my $code ( keys %$messages ) {
364
365     #    warn $code;
366     my %err;
367     my $exit_required_p = 0;
368     if ( $code eq 'BadBarcode' ) {
369         $err{badbarcode} = 1;
370         $err{msg}        = $messages->{'BadBarcode'};
371     }
372     elsif ( $code eq 'NotIssued' ) {
373         $err{notissued} = 1;
374         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
375     }
376     elsif ( $code eq 'WasLost' ) {
377         $err{waslost} = 1;
378     }
379     elsif ( $code eq 'ResFound' ) {
380         ;    # FIXME... anything to do here?
381     }
382     elsif ( $code eq 'WasReturned' ) {
383         ;    # FIXME... anything to do here?
384     }
385     elsif ( $code eq 'WasTransfered' ) {
386         ;    # FIXME... anything to do here?
387     }
388     elsif ( $code eq 'wthdrawn' ) {
389         $err{withdrawn} = 1;
390         $exit_required_p = 1;
391     }
392     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
393         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
394             $err{ispermanent} = 1;
395             $err{msg}         =
396               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
397         }
398     }
399     elsif ( $code eq 'WrongTransfer' ) {
400         ;    # FIXME... anything to do here?
401     }
402     elsif ( $code eq 'WrongTransferItem' ) {
403         ;    # FIXME... anything to do here?
404     }
405         elsif ( $code eq 'NeedsTransfer' ) {
406         }
407                 
408     else {
409         die "Unknown error code $code";    # XXX
410     }
411     if (%err) {
412         push( @errmsgloop, \%err );
413     }
414     last if $exit_required_p;
415 }
416 $template->param( errmsgloop => \@errmsgloop );
417
418 # patrontable ....
419 if ($borrower) {
420     my $flags = $borrower->{'flags'};
421     my @flagloop;
422     my $flagset;
423     foreach my $flag ( sort keys %$flags ) {
424         my %flaginfo;
425         unless ($flagset) { $flagset = 1; }
426         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
427         $flaginfo{flag}    = $flag;
428         if ( $flag eq 'CHARGES' ) {
429             $flaginfo{msg}            = $flag;
430             $flaginfo{charges}        = 1;
431             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
432         }
433         elsif ( $flag eq 'WAITING' ) {
434             $flaginfo{msg}     = $flag;
435             $flaginfo{waiting} = 1;
436             my @waitingitemloop;
437             my $items = $flags->{$flag}->{'itemlist'};
438             foreach my $item (@$items) {
439                 my $biblio =
440                   GetBiblioFromItemNumber( $item->{'itemnumber'});
441                 my %waitingitem;
442                 $waitingitem{biblionum} = $biblio->{'biblionumber'};
443                 $waitingitem{barcode}   = $biblio->{'barcode'};
444                 $waitingitem{title}     = $biblio->{'title'};
445                 $waitingitem{brname}    =
446                   $branches->{ $biblio->{'holdingbranch'} }
447                   ->{'branchname'};
448                 push( @waitingitemloop, \%waitingitem );
449             }
450             $flaginfo{itemloop} = \@waitingitemloop;
451         }
452         elsif ( $flag eq 'ODUES' ) {
453             my $items = $flags->{$flag}->{'itemlist'};
454             my @itemloop;
455             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
456                 @$items )
457             {
458                 my $biblio =
459                   GetBiblioFromItemNumber( $item->{'itemnumber'});
460                 my %overdueitem;
461                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
462                 $overdueitem{biblionum} = $biblio->{'biblionumber'};
463                 $overdueitem{barcode}   = $biblio->{'barcode'};
464                 $overdueitem{title}     = $biblio->{'title'};
465                 $overdueitem{brname}    =
466                   $branches->{ $biblio->{'holdingbranch'} }
467                   ->{'branchname'};
468                 push( @itemloop, \%overdueitem );
469             }
470             $flaginfo{itemloop} = \@itemloop;
471             $flaginfo{overdue}  = 1;
472         }
473         else {
474             $flaginfo{other} = 1;
475             $flaginfo{msg}   = $flags->{$flag}->{'message'};
476         }
477         push( @flagloop, \%flaginfo );
478     }
479     $template->param(
480         flagset          => $flagset,
481         flagloop         => \@flagloop,
482         riborrowernumber => $borrower->{'borrowernumber'},
483         riborcnum        => $borrower->{'cardnumber'},
484         riborsurname     => $borrower->{'surname'},
485         ribortitle       => $borrower->{'title'},
486         riborfirstname   => $borrower->{'firstname'}
487     );
488 }
489
490 #set up so only the last 8 returned items display (make for faster loading pages)
491 my $count = 0;
492 my @riloop;
493 foreach ( sort { $a <=> $b } keys %returneditems ) {
494     my %ri;
495     if ( $count < 8 ) {
496         my $barcode = $returneditems{$_};
497         my $duedate = $riduedate{$_};
498         my $overduetext;
499         my $borrowerinfo;
500         if ($duedate) {
501             my @tempdate = split( /-/, $duedate );
502             $ri{year}  = $tempdate[0];
503             $ri{month} = $tempdate[1];
504             $ri{day}   = $tempdate[2];
505             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
506             my @datearr    = localtime( time() );
507             my $todaysdate =
508                 $datearr[5] . '-'
509               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
510               . sprintf( "%0.2d", $datearr[3] );
511             $ri{duedate} = format_date($duedate);
512             my ($borrower) =
513               GetMemberDetails( $riborrowernumber{$_}, 0 );
514             $ri{borrowernumber} = $borrower->{'borrowernumber'};
515             $ri{borcnum}        = $borrower->{'cardnumber'};
516             $ri{borfirstname}   = $borrower->{'firstname'};
517             $ri{borsurname}     = $borrower->{'surname'};
518             $ri{bortitle}       = $borrower->{'title'};
519         }
520         else {
521             $ri{borrowernumber} = $riborrowernumber{$_};
522         }
523
524         #        my %ri;
525         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
526         $ri{itembiblionumber} = $biblio->{'biblionumber'};
527         $ri{itemtitle}        = $biblio->{'title'};
528         $ri{itemauthor}       = $biblio->{'author'};
529         $ri{itemtype}         = $biblio->{'itemtype'};
530         $ri{ccode}            = $biblio->{'ccode'};
531         $ri{barcode}          = $barcode;
532     }
533     else {
534         last;
535     }
536     $count++;
537     push( @riloop, \%ri );
538 }
539 $template->param( riloop => \@riloop );
540
541 $template->param(
542     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
543     genprname               => $printers->{$printer}->{'printername'},
544     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
545     printer                 => $printer,
546     errmsgloop              => \@errmsgloop,
547 );
548
549 # actually print the page!
550 output_html_with_http_headers $query, $cookie, $template->output;