created old_issues and old_reserves tables
[koha_fer] / 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::Items;
39 use C4::Members;
40 use C4::Branch; # GetBranchName
41 use C4::Koha;   # FIXME : is it still useful ?
42
43 my $query = new CGI;
44
45 if (!C4::Context->userenv){
46         my $sessionID = $query->cookie("CGISESSID");
47         my $session = get_session($sessionID);
48         if ($session->param('branch') eq 'NO_LIBRARY_SET'){
49                 # no branch set we can't return
50                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
51                 exit;
52         }
53
54
55 #getting the template
56 my ( $template, $librarian, $cookie ) = get_template_and_user(
57     {
58         template_name   => "circ/returns.tmpl",
59         query           => $query,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { circulate => 1 },
63     }
64 );
65
66 #####################
67 #Global vars
68 my $branches = GetBranches();
69 my $printers = GetPrinters();
70
71 #my $branch  = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
72 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
73 my $overduecharges = (C4::Context->preference('finesMode') && C4::Context->preference('finesMode') ne 'off');
74 #
75 # Some code to handle the error if there is no branch or printer setting.....
76 #
77
78 # Set up the item stack ....
79 my %returneditems;
80 my %riduedate;
81 my %riborrowernumber;
82 my @inputloop;
83 foreach ( $query->param ) {
84     (next) unless (/ri-(\d*)/);
85     my %input;
86     my $counter = $1;
87     (next) if ( $counter > 20 );
88     my $barcode        = $query->param("ri-$counter");
89     my $duedate        = $query->param("dd-$counter");
90     my $borrowernumber = $query->param("bn-$counter");
91     $counter++;
92
93     # decode barcode
94     $barcode = barcodedecode($barcode) if(C4::Context->preference('itemBarcodeInputFilter'));
95
96     ######################
97     #Are these lines still useful ?
98     $returneditems{$counter}    = $barcode;
99     $riduedate{$counter}        = $duedate;
100     $riborrowernumber{$counter} = $borrowernumber;
101
102     #######################
103     $input{counter}        = $counter;
104     $input{barcode}        = $barcode;
105     $input{duedate}        = $duedate;
106     $input{borrowernumber} = $borrowernumber;
107     push( @inputloop, \%input );
108 }
109
110 ############
111 # Deal with the requests....
112
113 if ($query->param('WT-itemNumber')){
114         updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
115 }
116
117 if ( $query->param('resbarcode') ) {
118     my $item           = $query->param('itemnumber');
119     my $borrowernumber = $query->param('borrowernumber');
120     my $resbarcode     = $query->param('resbarcode');
121     my $diffBranchReturned = $query->param('diffBranch');
122     # set to waiting....
123     my $iteminfo   = GetBiblioFromItemNumber($item);
124     my $diffBranchSend;
125     
126 #     addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
127 # if not we send a value in reserve waiting for not implementting waiting status
128     if ($diffBranchReturned) {
129         $diffBranchSend = $diffBranchReturned;
130     }
131     else {
132         $diffBranchSend = undef;
133     }
134     ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
135 #   check if we have other reservs for this document, if we have a return send the message of transfer
136     my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
137
138     my $branchname = GetBranchName( $messages->{'transfert'} );
139     my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
140     my $borcnum = $borr->{'cardnumber'};
141     my $name    =
142       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
143     my $slip = $query->param('resslip');
144
145
146     if ( $messages->{'transfert'} ) {
147         $template->param(
148             itemtitle      => $iteminfo->{'title'},
149                         itembiblionumber => $iteminfo->{'biblionumber'},
150             iteminfo       => $iteminfo->{'author'},
151             tobranchname   => $branchname,
152             name           => $name,
153             borrowernumber => $borrowernumber,
154             borcnum        => $borcnum,
155             borfirstname   => $borr->{'firstname'},
156             borsurname     => $borr->{'surname'},
157             diffbranch     => 1
158         );
159     }
160 }
161
162 my $borrower;
163 my $returned = 0;
164 my $messages;
165 my $issueinformation;
166 my $barcode = $query->param('barcode');
167 # strip whitespace
168 # $barcode =~ s/\s*//g; - use barcodedecode for this; whitespace is not invalid.
169 my $exemptfine = $query->param('exemptfine');
170
171 my $dotransfer = $query->param('dotransfer');
172 if ($dotransfer){
173         # An item has been returned to a branch other than the homebranch, and the librarian has choosen to initiate a transfer
174         my $transferitem=$query->param('transferitem');
175         my $tobranch=$query->param('tobranch');
176         ModItemTransfer($transferitem, C4::Context->userenv->{'branch'}, $tobranch); 
177 }
178
179 # actually return book and prepare item table.....
180 if ($barcode) {
181     $barcode = barcodedecode($barcode)  if(C4::Context->preference('itemBarcodeInputFilter'));
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;