Adding in Coded values conforming to the Z39.77-2006 Holdings Statements for Bibliogr...
[koha.git] / circ / returns.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 returns.pl
21
22 script to execute returns of books
23
24 written 11/3/2002 by Finlay
25
26 =cut
27
28 use strict;
29 use CGI;
30 use C4::Circulation::Circ2;
31 use C4::Date;
32 use C4::Output;
33 use C4::Print;
34 use C4::Reserves2;
35 use C4::Auth;
36 use C4::Interface::CGI::Output;
37 use C4::Branch; # GetBranchName
38 use C4::Koha;   # FIXME : is it still useful ?
39 use C4::Context;
40
41 my $query = new CGI;
42
43 #getting the template
44 my ( $template, $librarian, $cookie ) = get_template_and_user(
45     {
46         template_name   => "circ/returns.tmpl",
47         query           => $query,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { circulate => 1 },
51     }
52 );
53
54 #####################
55 #Global vars
56 my %env;
57
58 my $branches = GetBranches();
59 my $printers = GetPrinters( \%env );
60
61 #my $branch  = C4::Context->userenv?C4::Context->userenv->{'branch'}:"";
62 my $printer = C4::Context->userenv?C4::Context->userenv->{'branchprinter'}:"";
63
64 #
65 # Some code to handle the error if there is no branch or printer setting.....
66 #
67
68 #$env{'branchcode'} = $branch;
69 $env{'printer'}    = $printer;
70 $env{'queue'}      = $printer;
71
72
73 # Set up the item stack ....
74 my %returneditems;
75 my %riduedate;
76 my %riborrowernumber;
77 my @inputloop;
78 foreach ( $query->param ) {
79     (next) unless (/ri-(\d*)/);
80     my %input;
81     my $counter = $1;
82     (next) if ( $counter > 20 );
83     my $barcode        = $query->param("ri-$counter");
84     my $duedate        = $query->param("dd-$counter");
85     my $borrowernumber = $query->param("bn-$counter");
86     $counter++;
87
88     # decode cuecat
89     $barcode = cuecatbarcodedecode($barcode);
90
91     ######################
92     #Are these lines still useful ?
93     $returneditems{$counter}    = $barcode;
94     $riduedate{$counter}        = $duedate;
95     $riborrowernumber{$counter} = $borrowernumber;
96
97     #######################
98     $input{counter}        = $counter;
99     $input{barcode}        = $barcode;
100     $input{duedate}        = $duedate;
101     $input{borrowernumber} = $borrowernumber;
102     push( @inputloop, \%input );
103 }
104
105 ############
106 # Deal with the requests....
107
108 if ($query->param('WT-itemNumber')){
109 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
110 }
111
112
113 if ( $query->param('resbarcode') ) {
114     my $item           = $query->param('itemnumber');
115     my $borrowernumber = $query->param('borrowernumber');
116     my $resbarcode     = $query->param('resbarcode');
117     my $diffBranchReturned = $query->param('diffBranch');
118     # set to waiting....
119     my $iteminfo   = getiteminformation($item);
120     my $diffBranchSend;
121     
122 #     addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
123 # if not we send a value in reserve waiting for not implementting waiting status
124         if ($diffBranchReturned) {
125         $diffBranchSend = $diffBranchReturned;
126         }
127         else {
128         $diffBranchSend = undef;
129         }
130                  
131     my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
132 #   check if we have other reservs for this document, if we have a return send the message of transfer
133     my ( $messages, $nextreservinfo ) = OtherReserves($item);
134
135     my $branchname = GetBranchName( $messages->{'transfert'} );
136     my ($borr) = getpatroninformation( \%env, $nextreservinfo, 0 );
137     my $borcnum = $borr->{'cardnumber'};
138     my $name    =
139       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
140     my $slip = $query->param('resslip');
141
142
143     if ( $messages->{'transfert'} ) {
144         $template->param(
145             itemtitle      => $iteminfo->{'title'},
146             iteminfo       => $iteminfo->{'author'},
147             tobranchname   => $branchname,
148             name           => $name,
149             borrowernumber => $borrowernumber,
150             borcnum        => $borcnum,
151             borfirstname   => $borr->{'firstname'},
152             borsurname     => $borr->{'surname'},
153             diffbranch     => 1
154         );
155     }
156 }
157
158 my $iteminformation;
159 my $borrower;
160 my $returned = 0;
161 my $messages;
162 my $barcode = $query->param('barcode');
163
164 # actually return book and prepare item table.....
165 if ($barcode) {
166     # decode cuecat
167     $barcode = cuecatbarcodedecode($barcode);
168     ( $returned, $messages, $iteminformation, $borrower ) =
169       returnbook( $barcode, C4::Context->userenv->{'branch'} );
170     if ($returned) {
171         $returneditems{0}    = $barcode;
172         $riborrowernumber{0} = $borrower->{'borrowernumber'};
173         $riduedate{0}        = $iteminformation->{'date_due'};
174         my %input;
175         $input{counter}        = 0;
176         $input{first}          = 1;
177         $input{barcode}        = $barcode;
178         $input{duedate}        = $riduedate{0};
179         $input{borrowernumber} = $riborrowernumber{0};
180         push( @inputloop, \%input );
181
182         # check if the branch is the same as homebranch
183         # if not, we want to put a message
184         if ( $iteminformation->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
185             $template->param( homebranch => $iteminformation->{'homebranch'} );
186         }
187     }
188     elsif ( !$messages->{'BadBarcode'} ) {
189         my %input;
190         $input{counter} = 0;
191         $input{first}   = 1;
192         $input{barcode} = $barcode;
193         $input{duedate} = 0;
194
195         $returneditems{0} = $barcode;
196         $riduedate{0}     = 0;
197         if ( $messages->{'wthdrawn'} ) {
198             $input{withdrawn}      = 1;
199             $input{borrowernumber} = "Item Cancelled";
200             $riborrowernumber{0}   = 'Item Cancelled';
201         }
202         else {
203             $input{borrowernumber} = " ";
204             $riborrowernumber{0} = ' ';
205         }
206         push( @inputloop, \%input );
207     }
208     $template->param(
209         returned  => $returned,
210         itemtitle => $iteminformation->{'title'},
211
212         #                                                                       itembc => $iteminformation->{'barcode'},
213         #                                                                       itemdatedue => $iteminformation->{'datedue'},
214         itemauthor => $iteminformation->{'author'}
215     );
216 }
217 $template->param( inputloop => \@inputloop );
218
219 my $found    = 0;
220 my $waiting  = 0;
221 my $reserved = 0;
222
223 # new op dev : we check if the document must be returned to his homebranch directly,
224 #  if the document is transfered, we have warning message .
225
226 if ( $messages->{'WasTransfered'} ) {
227
228     my ($iteminfo) = getiteminformation( 0, $barcode );
229
230     $template->param(
231         found          => 1,
232         transfer       => 1,
233         itemhomebranch =>
234           $branches->{ $iteminfo->{'homebranch'} }->{'branchname'}
235     );
236
237 }
238
239 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
240
241 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
242         $template->param(
243         WrongTransfer  => 1,
244         TransferWaitingAt => $messages->{'WrongTransfer'},
245         WrongTransferItem => $messages->{'WrongTransferItem'},
246     );
247
248     my $res        = $messages->{'ResFound'};
249     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
250     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
251     my $name =
252       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
253     my ($iteminfo) = getiteminformation( 0, $barcode );
254         
255         $template->param(
256             wname           => $name,
257             wborfirstname   => $borr->{'firstname'},
258             wborsurname     => $borr->{'surname'},
259             wbortitle       => $borr->{'title'},
260             wborphone       => $borr->{'phone'},
261             wboremail       => $borr->{'emailaddress'},
262             wborstraddress  => $borr->{'streetaddress'},
263             wborcity        => $borr->{'city'},
264             wborzip         => $borr->{'zipcode'},
265             wborrowernumber => $res->{'borrowernumber'},
266             wborcnum        => $borr->{'cardnumber'},
267             witemtitle        => $iteminfo->{'title'},
268             witemauthor       => $iteminfo->{'author'},
269             witembarcode      => $iteminfo->{'barcode'},
270             witemtype         => $iteminfo->{'itemtype'},
271             wccode            => $iteminfo->{'ccode'},
272             witembiblionumber => $iteminfo->{'biblionumber'},
273             wtransfertFrom    => C4::Context->userenv->{'branch'},
274         );
275 }
276
277
278 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
279     my $res        = $messages->{'ResFound'};
280     my $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
281     my ($borr) = getpatroninformation( \%env, $res->{'borrowernumber'}, 0 );
282     my $name =
283       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
284     my ($iteminfo) = getiteminformation( 0, $barcode );
285
286     if ( $res->{'ResFound'} eq "Waiting" ) {
287         if ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ) {
288             $template->param( waiting => 1 );
289         }
290         else {
291             $template->param( waiting => 0 );
292         }
293
294         $template->param(
295             found          => 1,
296             name           => $name,
297             borfirstname   => $borr->{'firstname'},
298             borsurname     => $borr->{'surname'},
299             bortitle       => $borr->{'title'},
300             borphone       => $borr->{'phone'},
301             boremail       => $borr->{'emailaddress'},
302             borstraddress  => $borr->{'streetaddress'},
303             borcity        => $borr->{'city'},
304             borzip         => $borr->{'zipcode'},
305             borrowernumber => $res->{'borrowernumber'},
306             borcnum        => $borr->{'cardnumber'},
307             debarred       => $borr->{'debarred'},
308             gonenoaddress  => $borr->{'gonenoaddress'},
309             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
310             itemnumber       => $res->{'itemnumber'},
311             itemtitle        => $iteminfo->{'title'},
312             itemauthor       => $iteminfo->{'author'},
313             itembarcode      => $iteminfo->{'barcode'},
314             itemtype         => $iteminfo->{'itemtype'},
315             ccode            => $iteminfo->{'ccode'},
316             itembiblionumber => $iteminfo->{'biblionumber'}
317         );
318
319     }
320     if ( $res->{'ResFound'} eq "Reserved" ) {
321         my @da         = localtime( time() );
322         my $todaysdate =
323             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
324           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
325           . ( $da[5] + 1900 );
326
327         if ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ) {
328             $template->param( intransit => 0 );
329         }
330         else {
331             $template->param( intransit => 1 );
332         }
333
334         $template->param(
335             found          => 1,
336             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
337             name           => $name,
338             destbranchname =>
339               $branches->{ $res->{'branchcode'} }->{'branchname'},
340             destbranch     => $res->{'branchcode'},
341             transfertodo => ( C4::Context->userenv->{'branch'} eq $res->{'branchcode'} ? 0 : 1 ),
342             reserved => 1,
343             today            => $todaysdate,
344             itemnumber       => $res->{'itemnumber'},
345             itemtitle        => $iteminfo->{'title'},
346             itemauthor       => $iteminfo->{'author'},
347             itembarcode      => $iteminfo->{'barcode'},
348             itemtype         => $iteminfo->{'itemtype'},
349             ccode            => $iteminfo->{'ccode'},
350             itembiblionumber => $iteminfo->{'biblionumber'},
351             borsurname       => $borr->{'surname'},
352             bortitle         => $borr->{'title'},
353             borfirstname     => $borr->{'firstname'},
354             borrowernumber   => $res->{'borrowernumber'},
355             borcnum          => $borr->{'cardnumber'},
356             borphone         => $borr->{'phone'},
357             borstraddress    => $borr->{'streetaddress'},
358             borsub           => $borr->{'suburb'},
359             borcity          => $borr->{'city'},
360             borzip           => $borr->{'zipcode'},
361             boremail         => $borr->{'emailaddress'},
362             debarred         => $borr->{'debarred'},
363             gonenoaddress    => $borr->{'gonenoaddress'},
364             barcode          => $barcode
365         );
366     }
367 }
368
369 # Error Messages
370 my @errmsgloop;
371 foreach my $code ( keys %$messages ) {
372
373     #    warn $code;
374     my %err;
375     my $exit_required_p = 0;
376     if ( $code eq 'BadBarcode' ) {
377         $err{badbarcode} = 1;
378         $err{msg}        = $messages->{'BadBarcode'};
379     }
380     elsif ( $code eq 'NotIssued' ) {
381         $err{notissued} = 1;
382         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
383     }
384     elsif ( $code eq 'WasLost' ) {
385         $err{waslost} = 1;
386     }
387     elsif ( $code eq 'ResFound' ) {
388         ;    # FIXME... anything to do here?
389     }
390     elsif ( $code eq 'WasReturned' ) {
391         ;    # FIXME... anything to do here?
392     }
393     elsif ( $code eq 'WasTransfered' ) {
394         ;    # FIXME... anything to do here?
395     }
396     elsif ( $code eq 'wthdrawn' ) {
397         $err{withdrawn} = 1;
398         $exit_required_p = 1;
399     }
400     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
401         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
402             $err{ispermanent} = 1;
403             $err{msg}         =
404               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
405         }
406     }
407     elsif ( $code eq 'WrongTransfer' ) {
408         ;    # FIXME... anything to do here?
409     }
410     elsif ( $code eq 'WrongTransferItem' ) {
411         ;    # FIXME... anything to do here?
412     }
413     else {
414         die "Unknown error code $code";    # XXX
415     }
416     if (%err) {
417         push( @errmsgloop, \%err );
418     }
419     last if $exit_required_p;
420 }
421 $template->param( errmsgloop => \@errmsgloop );
422
423 # patrontable ....
424 if ($borrower) {
425     my $flags = $borrower->{'flags'};
426     my @flagloop;
427     my $flagset;
428     foreach my $flag ( sort keys %$flags ) {
429         my %flaginfo;
430         unless ($flagset) { $flagset = 1; }
431         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
432         $flaginfo{flag}    = $flag;
433         if ( $flag eq 'CHARGES' ) {
434             $flaginfo{msg}            = $flag;
435             $flaginfo{charges}        = 1;
436             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
437         }
438         elsif ( $flag eq 'WAITING' ) {
439             $flaginfo{msg}     = $flag;
440             $flaginfo{waiting} = 1;
441             my @waitingitemloop;
442             my $items = $flags->{$flag}->{'itemlist'};
443             foreach my $item (@$items) {
444                 my ($iteminformation) =
445                   getiteminformation( $item->{'itemnumber'}, 0 );
446                 my %waitingitem;
447                 $waitingitem{biblionum} = $iteminformation->{'biblionumber'};
448                 $waitingitem{barcode}   = $iteminformation->{'barcode'};
449                 $waitingitem{title}     = $iteminformation->{'title'};
450                 $waitingitem{brname}    =
451                   $branches->{ $iteminformation->{'holdingbranch'} }
452                   ->{'branchname'};
453                 push( @waitingitemloop, \%waitingitem );
454             }
455             $flaginfo{itemloop} = \@waitingitemloop;
456         }
457         elsif ( $flag eq 'ODUES' ) {
458             my $items = $flags->{$flag}->{'itemlist'};
459             my @itemloop;
460             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
461                 @$items )
462             {
463                 my ($iteminformation) =
464                   getiteminformation( $item->{'itemnumber'}, 0 );
465                 my %overdueitem;
466                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
467                 $overdueitem{biblionum} = $iteminformation->{'biblionumber'};
468                 $overdueitem{barcode}   = $iteminformation->{'barcode'};
469                 $overdueitem{title}     = $iteminformation->{'title'};
470                 $overdueitem{brname}    =
471                   $branches->{ $iteminformation->{'holdingbranch'} }
472                   ->{'branchname'};
473                 push( @itemloop, \%overdueitem );
474             }
475             $flaginfo{itemloop} = \@itemloop;
476             $flaginfo{overdue}  = 1;
477         }
478         else {
479             $flaginfo{other} = 1;
480             $flaginfo{msg}   = $flags->{$flag}->{'message'};
481         }
482         push( @flagloop, \%flaginfo );
483     }
484     $template->param(
485         flagset          => $flagset,
486         flagloop         => \@flagloop,
487         riborrowernumber => $borrower->{'borrowernumber'},
488         riborcnum        => $borrower->{'cardnumber'},
489         riborsurname     => $borrower->{'surname'},
490         ribortitle       => $borrower->{'title'},
491         riborfirstname   => $borrower->{'firstname'}
492     );
493 }
494
495 #set up so only the last 8 returned items display (make for faster loading pages)
496 my $count = 0;
497 my @riloop;
498 foreach ( sort { $a <=> $b } keys %returneditems ) {
499     my %ri;
500     if ( $count < 8 ) {
501         my $barcode = $returneditems{$_};
502         my $duedate = $riduedate{$_};
503         my $overduetext;
504         my $borrowerinfo;
505         if ($duedate) {
506             my @tempdate = split( /-/, $duedate );
507             $ri{year}  = $tempdate[0];
508             $ri{month} = $tempdate[1];
509             $ri{day}   = $tempdate[2];
510             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
511             my @datearr    = localtime( time() );
512             my $todaysdate =
513                 $datearr[5] . '-'
514               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
515               . sprintf( "%0.2d", $datearr[3] );
516             $ri{duedate} = format_date($duedate);
517             my ($borrower) =
518               getpatroninformation( \%env, $riborrowernumber{$_}, 0 );
519             $ri{borrowernumber} = $borrower->{'borrowernumber'};
520             $ri{borcnum}        = $borrower->{'cardnumber'};
521             $ri{borfirstname}   = $borrower->{'firstname'};
522             $ri{borsurname}     = $borrower->{'surname'};
523             $ri{bortitle}       = $borrower->{'title'};
524         }
525         else {
526             $ri{borrowernumber} = $riborrowernumber{$_};
527         }
528
529         #        my %ri;
530         my ($iteminformation) = getiteminformation( 0, $barcode );
531         $ri{itembiblionumber} = $iteminformation->{'biblionumber'};
532         $ri{itemtitle}        = $iteminformation->{'title'};
533         $ri{itemauthor}       = $iteminformation->{'author'};
534         $ri{itemtype}         = $iteminformation->{'itemtype'};
535         $ri{ccode}            = $iteminformation->{'ccode'};
536         $ri{barcode}          = $barcode;
537     }
538     else {
539         last;
540     }
541     $count++;
542     push( @riloop, \%ri );
543 }
544 $template->param( riloop => \@riloop );
545
546 $template->param(
547     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
548     genprname               => $printers->{$printer}->{'printername'},
549     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
550     printer                 => $printer,
551     errmsgloop              => \@errmsgloop,
552     intranetcolorstylesheet =>
553       C4::Context->preference("intranetcolorstylesheet"),
554     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
555     IntranetNav        => C4::Context->preference("IntranetNav"),
556 );
557
558 # actually print the page!
559 output_html_with_http_headers $query, $cookie, $template->output;
560
561 sub cuecatbarcodedecode {
562     my ($barcode) = @_;
563     chomp($barcode);
564     my @fields = split( /\./, $barcode );
565     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
566     if ( $#results == 2 ) {
567         return $results[2];
568     }
569     else {
570         return $barcode;
571     }
572 }