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