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