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