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