Continuing on my tests mission
[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::Context;
31 use C4::Auth;
32 use C4::Output;
33 use C4::Circulation;
34 use C4::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
107 if ( $query->param('resbarcode') ) {
108     my $item           = $query->param('itemnumber');
109     my $borrowernumber = $query->param('borrowernumber');
110     my $resbarcode     = $query->param('resbarcode');
111     my $diffBranchReturned = $query->param('diffBranch');
112     # set to waiting....
113     my $iteminfo   = GetBiblioFromItemNumber($item);
114     my $diffBranchSend;
115     
116 #     addin in ModReserveAffect the possibility to check if the document is expected in this library or not,
117 # if not we send a value in reserve waiting for not implementting waiting status
118         if ($diffBranchReturned) {
119         $diffBranchSend = $diffBranchReturned;
120         }
121         else {
122         $diffBranchSend = undef;
123         }
124                  
125     ModReserveAffect( $item, $borrowernumber,$diffBranchSend);
126 #   check if we have other reservs for this document, if we have a return send the message of transfer
127     my ( $messages, $nextreservinfo ) = GetOtherReserves($item);
128
129     my $branchname = GetBranchName( $messages->{'transfert'} );
130     my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
131     my $borcnum = $borr->{'cardnumber'};
132     my $name    =
133       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
134     my $slip = $query->param('resslip');
135
136
137     if ( $messages->{'transfert'} ) {
138         $template->param(
139             itemtitle      => $iteminfo->{'title'},
140             iteminfo       => $iteminfo->{'author'},
141             tobranchname   => $branchname,
142             name           => $name,
143             borrowernumber => $borrowernumber,
144             borcnum        => $borcnum,
145             borfirstname   => $borr->{'firstname'},
146             borsurname     => $borr->{'surname'},
147             diffbranch     => 1
148         );
149     }
150 }
151
152 my $borrower;
153 my $returned = 0;
154 my $messages;
155 my $issueinformation;
156 my $barcode = $query->param('barcode');
157
158 # actually return book and prepare item table.....
159 if ($barcode) {
160     # decode cuecat
161     $barcode = cuecatbarcodedecode($barcode);
162 #
163 # save the return
164 #
165     ( $returned, $messages, $issueinformation, $borrower ) =
166       AddReturn( $barcode, C4::Context->userenv->{'branch'} );
167     # get biblio description
168     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
169     $template->param(
170         title            => $biblio->{'title'},
171         homebranch       => $biblio->{'homebranch'},
172         author           => $biblio->{'author'},
173         itembarcode      => $biblio->{'barcode'},
174         itemtype         => $biblio->{'itemtype'},
175         ccode            => $biblio->{'ccode'},
176         itembiblionumber => $biblio->{'biblionumber'},    
177     );
178     if ($returned) {
179         $returneditems{0}    = $barcode;
180         $riborrowernumber{0} = $borrower->{'borrowernumber'};
181         $riduedate{0}        = $issueinformation->{'date_due'};
182         my %input;
183         $input{counter}        = 0;
184         $input{first}          = 1;
185         $input{barcode}        = $barcode;
186         $input{duedate}        = $riduedate{0};
187         $input{borrowernumber} = $riborrowernumber{0};
188         push( @inputloop, \%input );
189
190         # check if the branch is the same as homebranch
191         # if not, we want to put a message
192         if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
193             $template->param( homebranch => $biblio->{'homebranch'} );
194         }
195     }
196     elsif ( !$messages->{'BadBarcode'} ) {
197         my %input;
198         $input{counter} = 0;
199         $input{first}   = 1;
200         $input{barcode} = $barcode;
201         $input{duedate} = 0;
202
203         $returneditems{0} = $barcode;
204         $riduedate{0}     = 0;
205         if ( $messages->{'wthdrawn'} ) {
206             $input{withdrawn}      = 1;
207             $input{borrowernumber} = "Item Cancelled";
208             $riborrowernumber{0}   = 'Item Cancelled';
209         }
210         else {
211             $input{borrowernumber} = " ";
212             $riborrowernumber{0} = ' ';
213         }
214         push( @inputloop, \%input );
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     $template->param(
228         found          => 1,
229         transfer       => 1,
230     );
231
232 }
233
234 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
235
236 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
237         $template->param(
238         WrongTransfer  => 1,
239         TransferWaitingAt => $messages->{'WrongTransfer'},
240         WrongTransferItem => $messages->{'WrongTransferItem'},
241     );
242
243     my $reserve        = $messages->{'ResFound'};
244     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
245     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
246     my $name =
247       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
248         $template->param(
249             wname           => $name,
250             wborfirstname   => $borr->{'firstname'},
251             wborsurname     => $borr->{'surname'},
252             wbortitle       => $borr->{'title'},
253             wborphone       => $borr->{'phone'},
254             wboremail       => $borr->{'emailaddress'},
255             wborstraddress  => $borr->{'streetaddress'},
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->{'emailaddress'},
288             borstraddress  => $borr->{'streetaddress'},
289             borcity        => $borr->{'city'},
290             borzip         => $borr->{'zipcode'},
291             borrowernumber => $reserve->{'borrowernumber'},
292             borcnum        => $borr->{'cardnumber'},
293             debarred       => $borr->{'debarred'},
294             gonenoaddress  => $borr->{'gonenoaddress'},
295             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
296             itemnumber       => $reserve->{'itemnumber'},
297         );
298
299     }
300     if ( $reserve->{'ResFound'} eq "Reserved" ) {
301         my @da         = localtime( time() );
302         my $todaysdate =
303             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
304           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
305           . ( $da[5] + 1900 );
306
307         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
308             $template->param( intransit => 0 );
309         }
310         else {
311             $template->param( intransit => 1 );
312         }
313
314         $template->param(
315             found          => 1,
316             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
317             destbranchname =>
318               $branches->{ $reserve->{'branchcode'} }->{'branchname'},
319             destbranch     => $reserve->{'branchcode'},
320             transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
321             reserved => 1,
322             today            => $todaysdate,
323             itemnumber       => $reserve->{'itemnumber'},
324             borsurname       => $borr->{'surname'},
325             bortitle         => $borr->{'title'},
326             borfirstname     => $borr->{'firstname'},
327             borrowernumber   => $reserve->{'borrowernumber'},
328             borcnum          => $borr->{'cardnumber'},
329             borphone         => $borr->{'phone'},
330             borstraddress    => $borr->{'streetaddress'},
331             borsub           => $borr->{'suburb'},
332             borcity          => $borr->{'city'},
333             borzip           => $borr->{'zipcode'},
334             boremail         => $borr->{'emailaddress'},
335             debarred         => $borr->{'debarred'},
336             gonenoaddress    => $borr->{'gonenoaddress'},
337             barcode          => $barcode
338         );
339     }
340 }
341
342 # Error Messages
343 my @errmsgloop;
344 foreach my $code ( keys %$messages ) {
345
346     #    warn $code;
347     my %err;
348     my $exit_required_p = 0;
349     if ( $code eq 'BadBarcode' ) {
350         $err{badbarcode} = 1;
351         $err{msg}        = $messages->{'BadBarcode'};
352     }
353     elsif ( $code eq 'NotIssued' ) {
354         $err{notissued} = 1;
355         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
356     }
357     elsif ( $code eq 'WasLost' ) {
358         $err{waslost} = 1;
359     }
360     elsif ( $code eq 'ResFound' ) {
361         ;    # FIXME... anything to do here?
362     }
363     elsif ( $code eq 'WasReturned' ) {
364         ;    # FIXME... anything to do here?
365     }
366     elsif ( $code eq 'WasTransfered' ) {
367         ;    # FIXME... anything to do here?
368     }
369     elsif ( $code eq 'wthdrawn' ) {
370         $err{withdrawn} = 1;
371         $exit_required_p = 1;
372     }
373     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
374         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
375             $err{ispermanent} = 1;
376             $err{msg}         =
377               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
378         }
379     }
380     elsif ( $code eq 'WrongTransfer' ) {
381         ;    # FIXME... anything to do here?
382     }
383     elsif ( $code eq 'WrongTransferItem' ) {
384         ;    # FIXME... anything to do here?
385     }
386     else {
387         die "Unknown error code $code";    # XXX
388     }
389     if (%err) {
390         push( @errmsgloop, \%err );
391     }
392     last if $exit_required_p;
393 }
394 $template->param( errmsgloop => \@errmsgloop );
395
396 # patrontable ....
397 if ($borrower) {
398     my $flags = $borrower->{'flags'};
399     my @flagloop;
400     my $flagset;
401     foreach my $flag ( sort keys %$flags ) {
402         my %flaginfo;
403         unless ($flagset) { $flagset = 1; }
404         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
405         $flaginfo{flag}    = $flag;
406         if ( $flag eq 'CHARGES' ) {
407             $flaginfo{msg}            = $flag;
408             $flaginfo{charges}        = 1;
409             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
410         }
411         elsif ( $flag eq 'WAITING' ) {
412             $flaginfo{msg}     = $flag;
413             $flaginfo{waiting} = 1;
414             my @waitingitemloop;
415             my $items = $flags->{$flag}->{'itemlist'};
416             foreach my $item (@$items) {
417                 my $biblio =
418                   GetBiblioFromItemNumber( $item->{'itemnumber'});
419                 my %waitingitem;
420                 $waitingitem{biblionum} = $biblio->{'biblionumber'};
421                 $waitingitem{barcode}   = $biblio->{'barcode'};
422                 $waitingitem{title}     = $biblio->{'title'};
423                 $waitingitem{brname}    =
424                   $branches->{ $biblio->{'holdingbranch'} }
425                   ->{'branchname'};
426                 push( @waitingitemloop, \%waitingitem );
427             }
428             $flaginfo{itemloop} = \@waitingitemloop;
429         }
430         elsif ( $flag eq 'ODUES' ) {
431             my $items = $flags->{$flag}->{'itemlist'};
432             my @itemloop;
433             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
434                 @$items )
435             {
436                 my $biblio =
437                   GetBiblioFromItemNumber( $item->{'itemnumber'});
438                 my %overdueitem;
439                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
440                 $overdueitem{biblionum} = $biblio->{'biblionumber'};
441                 $overdueitem{barcode}   = $biblio->{'barcode'};
442                 $overdueitem{title}     = $biblio->{'title'};
443                 $overdueitem{brname}    =
444                   $branches->{ $biblio->{'holdingbranch'} }
445                   ->{'branchname'};
446                 push( @itemloop, \%overdueitem );
447             }
448             $flaginfo{itemloop} = \@itemloop;
449             $flaginfo{overdue}  = 1;
450         }
451         else {
452             $flaginfo{other} = 1;
453             $flaginfo{msg}   = $flags->{$flag}->{'message'};
454         }
455         push( @flagloop, \%flaginfo );
456     }
457     $template->param(
458         flagset          => $flagset,
459         flagloop         => \@flagloop,
460         riborrowernumber => $borrower->{'borrowernumber'},
461         riborcnum        => $borrower->{'cardnumber'},
462         riborsurname     => $borrower->{'surname'},
463         ribortitle       => $borrower->{'title'},
464         riborfirstname   => $borrower->{'firstname'}
465     );
466 }
467
468 #set up so only the last 8 returned items display (make for faster loading pages)
469 my $count = 0;
470 my @riloop;
471 foreach ( sort { $a <=> $b } keys %returneditems ) {
472     my %ri;
473     if ( $count < 8 ) {
474         my $barcode = $returneditems{$_};
475         my $duedate = $riduedate{$_};
476         my $overduetext;
477         my $borrowerinfo;
478         if ($duedate) {
479             my @tempdate = split( /-/, $duedate );
480             $ri{year}  = $tempdate[0];
481             $ri{month} = $tempdate[1];
482             $ri{day}   = $tempdate[2];
483             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
484             my @datearr    = localtime( time() );
485             my $todaysdate =
486                 $datearr[5] . '-'
487               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
488               . sprintf( "%0.2d", $datearr[3] );
489             $ri{duedate} = format_date($duedate);
490             my ($borrower) =
491               GetMemberDetails( $riborrowernumber{$_}, 0 );
492             $ri{borrowernumber} = $borrower->{'borrowernumber'};
493             $ri{borcnum}        = $borrower->{'cardnumber'};
494             $ri{borfirstname}   = $borrower->{'firstname'};
495             $ri{borsurname}     = $borrower->{'surname'};
496             $ri{bortitle}       = $borrower->{'title'};
497         }
498         else {
499             $ri{borrowernumber} = $riborrowernumber{$_};
500         }
501
502         #        my %ri;
503         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
504         $ri{itembiblionumber} = $biblio->{'biblionumber'};
505         $ri{itemtitle}        = $biblio->{'title'};
506         $ri{itemauthor}       = $biblio->{'author'};
507         $ri{itemtype}         = $biblio->{'itemtype'};
508         $ri{ccode}            = $biblio->{'ccode'};
509         $ri{barcode}          = $barcode;
510     }
511     else {
512         last;
513     }
514     $count++;
515     push( @riloop, \%ri );
516 }
517 $template->param( riloop => \@riloop );
518
519 $template->param(
520     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
521     genprname               => $printers->{$printer}->{'printername'},
522     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
523     printer                 => $printer,
524     errmsgloop              => \@errmsgloop,
525     intranetcolorstylesheet =>
526       C4::Context->preference("intranetcolorstylesheet"),
527     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
528     IntranetNav        => C4::Context->preference("IntranetNav"),
529 );
530
531 # actually print the page!
532 output_html_with_http_headers $query, $cookie, $template->output;