renaming currenttransfers to transferstoreceive
[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;
31 use C4::Date;
32 use C4::Output;
33 use C4::Print;
34 use C4::Reserves;
35 use C4::Auth;
36 use C4::Biblio;
37 use C4::Members;
38 use C4::Interface::CGI::Output;
39 use C4::Branch; # GetBranchName
40 use C4::Koha;   # FIXME : is it still useful ?
41 use C4::Context;
42
43 my $query = new CGI;
44
45 #getting the template
46 my ( $template, $librarian, $cookie ) = get_template_and_user(
47     {
48         template_name   => "circ/returns.tmpl",
49         query           => $query,
50         type            => "intranet",
51         authnotrequired => 0,
52         flagsrequired   => { circulate => 1 },
53     }
54 );
55
56 #####################
57 #Global vars
58 my $branches = GetBranches();
59 my $printers = GetPrinters();
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 # Set up the item stack ....
69 my %returneditems;
70 my %riduedate;
71 my %riborrowernumber;
72 my @inputloop;
73 foreach ( $query->param ) {
74     (next) unless (/ri-(\d*)/);
75     my %input;
76     my $counter = $1;
77     (next) if ( $counter > 20 );
78     my $barcode        = $query->param("ri-$counter");
79     my $duedate        = $query->param("dd-$counter");
80     my $borrowernumber = $query->param("bn-$counter");
81     $counter++;
82
83     # decode cuecat
84     $barcode = cuecatbarcodedecode($barcode);
85
86     ######################
87     #Are these lines still useful ?
88     $returneditems{$counter}    = $barcode;
89     $riduedate{$counter}        = $duedate;
90     $riborrowernumber{$counter} = $borrowernumber;
91
92     #######################
93     $input{counter}        = $counter;
94     $input{barcode}        = $barcode;
95     $input{duedate}        = $duedate;
96     $input{borrowernumber} = $borrowernumber;
97     push( @inputloop, \%input );
98 }
99
100 ############
101 # Deal with the requests....
102
103 if ($query->param('WT-itemNumber')){
104 updateWrongTransfer ($query->param('WT-itemNumber'),$query->param('WT-waitingAt'),$query->param('WT-From'));
105 }
106
107
108 if ( $query->param('resbarcode') ) {
109     my $item           = $query->param('itemnumber');
110     my $borrowernumber = $query->param('borrowernumber');
111     my $resbarcode     = $query->param('resbarcode');
112     my $diffBranchReturned = $query->param('diffBranch');
113     # set to waiting....
114     my $iteminfo   = GetBiblioFromItemNumber($item);
115     my $diffBranchSend;
116     
117 #     addin in ReserveWaiting the possibility to check if the document is expected in this library or not,
118 # if not we send a value in reserve waiting for not implementting waiting status
119         if ($diffBranchReturned) {
120         $diffBranchSend = $diffBranchReturned;
121         }
122         else {
123         $diffBranchSend = undef;
124         }
125                  
126     my $tobranchcd = ReserveWaiting( $item, $borrowernumber,$diffBranchSend);
127 #   check if we have other reservs for this document, if we have a return send the message of transfer
128     my ( $messages, $nextreservinfo ) = OtherReserves($item);
129
130     my $branchname = GetBranchName( $messages->{'transfert'} );
131     my ($borr) = GetMemberDetails( $nextreservinfo, 0 );
132     my $borcnum = $borr->{'cardnumber'};
133     my $name    =
134       $borr->{'surname'} . ", " . $borr->{'title'} . " " . $borr->{'firstname'};
135     my $slip = $query->param('resslip');
136
137
138     if ( $messages->{'transfert'} ) {
139         $template->param(
140             itemtitle      => $iteminfo->{'title'},
141             iteminfo       => $iteminfo->{'author'},
142             tobranchname   => $branchname,
143             name           => $name,
144             borrowernumber => $borrowernumber,
145             borcnum        => $borcnum,
146             borfirstname   => $borr->{'firstname'},
147             borsurname     => $borr->{'surname'},
148             diffbranch     => 1
149         );
150     }
151 }
152
153 my $borrower;
154 my $returned = 0;
155 my $messages;
156 my $issueinformation;
157 my $barcode = $query->param('barcode');
158
159 # actually return book and prepare item table.....
160 if ($barcode) {
161     # decode cuecat
162     $barcode = cuecatbarcodedecode($barcode);
163 #
164 # save the return
165 #
166     ( $returned, $messages, $issueinformation, $borrower ) =
167       AddReturn( $barcode, C4::Context->userenv->{'branch'} );
168     # get biblio description
169     my $biblio = GetBiblioFromItemNumber($issueinformation->{'itemnumber'});
170     $template->param(
171         title            => $biblio->{'title'},
172         homebranch       => $biblio->{'homebranch'},
173         author           => $biblio->{'author'},
174         itembarcode      => $biblio->{'barcode'},
175         itemtype         => $biblio->{'itemtype'},
176         ccode            => $biblio->{'ccode'},
177         itembiblionumber => $biblio->{'biblionumber'},    
178     );
179     if ($returned) {
180         $returneditems{0}    = $barcode;
181         $riborrowernumber{0} = $borrower->{'borrowernumber'};
182         $riduedate{0}        = $issueinformation->{'date_due'};
183         my %input;
184         $input{counter}        = 0;
185         $input{first}          = 1;
186         $input{barcode}        = $barcode;
187         $input{duedate}        = $riduedate{0};
188         $input{borrowernumber} = $riborrowernumber{0};
189         push( @inputloop, \%input );
190
191         # check if the branch is the same as homebranch
192         # if not, we want to put a message
193         if ( $biblio->{'homebranch'} ne C4::Context->userenv->{'branch'} ) {
194             $template->param( homebranch => $biblio->{'homebranch'} );
195         }
196     }
197     elsif ( !$messages->{'BadBarcode'} ) {
198         my %input;
199         $input{counter} = 0;
200         $input{first}   = 1;
201         $input{barcode} = $barcode;
202         $input{duedate} = 0;
203
204         $returneditems{0} = $barcode;
205         $riduedate{0}     = 0;
206         if ( $messages->{'wthdrawn'} ) {
207             $input{withdrawn}      = 1;
208             $input{borrowernumber} = "Item Cancelled";
209             $riborrowernumber{0}   = 'Item Cancelled';
210         }
211         else {
212             $input{borrowernumber} = " ";
213             $riborrowernumber{0} = ' ';
214         }
215         push( @inputloop, \%input );
216     }
217 }
218 $template->param( inputloop => \@inputloop );
219
220 my $found    = 0;
221 my $waiting  = 0;
222 my $reserved = 0;
223
224 # new op dev : we check if the document must be returned to his homebranch directly,
225 #  if the document is transfered, we have warning message .
226
227 if ( $messages->{'WasTransfered'} ) {
228     $template->param(
229         found          => 1,
230         transfer       => 1,
231     );
232
233 }
234
235 # adding a case of wrong transfert, if the document wasn't transfered in the good library (according to branchtransfer (tobranch) BDD)
236
237 if ( $messages->{'WrongTransfer'} and not $messages->{'WasTransfered'}) {
238         $template->param(
239         WrongTransfer  => 1,
240         TransferWaitingAt => $messages->{'WrongTransfer'},
241         WrongTransferItem => $messages->{'WrongTransferItem'},
242     );
243
244     my $reserve        = $messages->{'ResFound'};
245     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
246     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
247     my $name =
248       $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'};
249         $template->param(
250             wname           => $name,
251             wborfirstname   => $borr->{'firstname'},
252             wborsurname     => $borr->{'surname'},
253             wbortitle       => $borr->{'title'},
254             wborphone       => $borr->{'phone'},
255             wboremail       => $borr->{'emailaddress'},
256             wborstraddress  => $borr->{'streetaddress'},
257             wborcity        => $borr->{'city'},
258             wborzip         => $borr->{'zipcode'},
259             wborrowernumber => $reserve->{'borrowernumber'},
260             wborcnum        => $borr->{'cardnumber'},
261             wtransfertFrom    => C4::Context->userenv->{'branch'},
262         );
263 }
264
265
266 #
267 # reserve found and item arrived at the expected branch
268 #
269 if ( $messages->{'ResFound'} and not $messages->{'WrongTransfer'}) {
270     my $reserve        = $messages->{'ResFound'};
271     my $branchname = $branches->{ $reserve->{'branchcode'} }->{'branchname'};
272     my ($borr) = GetMemberDetails( $reserve->{'borrowernumber'}, 0 );
273     if ( $reserve->{'ResFound'} eq "Waiting" ) {
274         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
275             $template->param( waiting => 1 );
276         }
277         else {
278             $template->param( waiting => 0 );
279         }
280
281         $template->param(
282             found          => 1,
283             name           => $borr->{'surname'} . " " . $borr->{'title'} . " " . $borr->{'firstname'},
284             borfirstname   => $borr->{'firstname'},
285             borsurname     => $borr->{'surname'},
286             bortitle       => $borr->{'title'},
287             borphone       => $borr->{'phone'},
288             boremail       => $borr->{'emailaddress'},
289             borstraddress  => $borr->{'streetaddress'},
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         );
299
300     }
301     if ( $reserve->{'ResFound'} eq "Reserved" ) {
302         my @da         = localtime( time() );
303         my $todaysdate =
304             sprintf( "%0.2d", ( $da[3] + 1 ) ) . "/"
305           . sprintf( "%0.2d", ( $da[4] + 1 ) ) . "/"
306           . ( $da[5] + 1900 );
307
308         if ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ) {
309             $template->param( intransit => 0 );
310         }
311         else {
312             $template->param( intransit => 1 );
313         }
314
315         $template->param(
316             found          => 1,
317             currentbranch  => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
318             destbranchname =>
319               $branches->{ $reserve->{'branchcode'} }->{'branchname'},
320             destbranch     => $reserve->{'branchcode'},
321             transfertodo => ( C4::Context->userenv->{'branch'} eq $reserve->{'branchcode'} ? 0 : 1 ),
322             reserved => 1,
323             today            => $todaysdate,
324             itemnumber       => $reserve->{'itemnumber'},
325             borsurname       => $borr->{'surname'},
326             bortitle         => $borr->{'title'},
327             borfirstname     => $borr->{'firstname'},
328             borrowernumber   => $reserve->{'borrowernumber'},
329             borcnum          => $borr->{'cardnumber'},
330             borphone         => $borr->{'phone'},
331             borstraddress    => $borr->{'streetaddress'},
332             borsub           => $borr->{'suburb'},
333             borcity          => $borr->{'city'},
334             borzip           => $borr->{'zipcode'},
335             boremail         => $borr->{'emailaddress'},
336             debarred         => $borr->{'debarred'},
337             gonenoaddress    => $borr->{'gonenoaddress'},
338             barcode          => $barcode
339         );
340     }
341 }
342
343 # Error Messages
344 my @errmsgloop;
345 foreach my $code ( keys %$messages ) {
346
347     #    warn $code;
348     my %err;
349     my $exit_required_p = 0;
350     if ( $code eq 'BadBarcode' ) {
351         $err{badbarcode} = 1;
352         $err{msg}        = $messages->{'BadBarcode'};
353     }
354     elsif ( $code eq 'NotIssued' ) {
355         $err{notissued} = 1;
356         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
357     }
358     elsif ( $code eq 'WasLost' ) {
359         $err{waslost} = 1;
360     }
361     elsif ( $code eq 'ResFound' ) {
362         ;    # FIXME... anything to do here?
363     }
364     elsif ( $code eq 'WasReturned' ) {
365         ;    # FIXME... anything to do here?
366     }
367     elsif ( $code eq 'WasTransfered' ) {
368         ;    # FIXME... anything to do here?
369     }
370     elsif ( $code eq 'wthdrawn' ) {
371         $err{withdrawn} = 1;
372         $exit_required_p = 1;
373     }
374     elsif ( ( $code eq 'IsPermanent' ) && ( not $messages->{'ResFound'} ) ) {
375         if ( $messages->{'IsPermanent'} ne C4::Context->userenv->{'branch'} ) {
376             $err{ispermanent} = 1;
377             $err{msg}         =
378               $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
379         }
380     }
381     elsif ( $code eq 'WrongTransfer' ) {
382         ;    # FIXME... anything to do here?
383     }
384     elsif ( $code eq 'WrongTransferItem' ) {
385         ;    # FIXME... anything to do here?
386     }
387     else {
388         die "Unknown error code $code";    # XXX
389     }
390     if (%err) {
391         push( @errmsgloop, \%err );
392     }
393     last if $exit_required_p;
394 }
395 $template->param( errmsgloop => \@errmsgloop );
396
397 # patrontable ....
398 if ($borrower) {
399     my $flags = $borrower->{'flags'};
400     my @flagloop;
401     my $flagset;
402     foreach my $flag ( sort keys %$flags ) {
403         my %flaginfo;
404         unless ($flagset) { $flagset = 1; }
405         $flaginfo{redfont} = ( $flags->{$flag}->{'noissues'} );
406         $flaginfo{flag}    = $flag;
407         if ( $flag eq 'CHARGES' ) {
408             $flaginfo{msg}            = $flag;
409             $flaginfo{charges}        = 1;
410             $flaginfo{borrowernumber} = $borrower->{borrowernumber};
411         }
412         elsif ( $flag eq 'WAITING' ) {
413             $flaginfo{msg}     = $flag;
414             $flaginfo{waiting} = 1;
415             my @waitingitemloop;
416             my $items = $flags->{$flag}->{'itemlist'};
417             foreach my $item (@$items) {
418                 my $biblio =
419                   GetBiblioFromItemNumber( $item->{'itemnumber'});
420                 my %waitingitem;
421                 $waitingitem{biblionum} = $biblio->{'biblionumber'};
422                 $waitingitem{barcode}   = $biblio->{'barcode'};
423                 $waitingitem{title}     = $biblio->{'title'};
424                 $waitingitem{brname}    =
425                   $branches->{ $biblio->{'holdingbranch'} }
426                   ->{'branchname'};
427                 push( @waitingitemloop, \%waitingitem );
428             }
429             $flaginfo{itemloop} = \@waitingitemloop;
430         }
431         elsif ( $flag eq 'ODUES' ) {
432             my $items = $flags->{$flag}->{'itemlist'};
433             my @itemloop;
434             foreach my $item ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
435                 @$items )
436             {
437                 my $biblio =
438                   GetBiblioFromItemNumber( $item->{'itemnumber'});
439                 my %overdueitem;
440                 $overdueitem{duedate}   = format_date( $item->{'date_due'} );
441                 $overdueitem{biblionum} = $biblio->{'biblionumber'};
442                 $overdueitem{barcode}   = $biblio->{'barcode'};
443                 $overdueitem{title}     = $biblio->{'title'};
444                 $overdueitem{brname}    =
445                   $branches->{ $biblio->{'holdingbranch'} }
446                   ->{'branchname'};
447                 push( @itemloop, \%overdueitem );
448             }
449             $flaginfo{itemloop} = \@itemloop;
450             $flaginfo{overdue}  = 1;
451         }
452         else {
453             $flaginfo{other} = 1;
454             $flaginfo{msg}   = $flags->{$flag}->{'message'};
455         }
456         push( @flagloop, \%flaginfo );
457     }
458     $template->param(
459         flagset          => $flagset,
460         flagloop         => \@flagloop,
461         riborrowernumber => $borrower->{'borrowernumber'},
462         riborcnum        => $borrower->{'cardnumber'},
463         riborsurname     => $borrower->{'surname'},
464         ribortitle       => $borrower->{'title'},
465         riborfirstname   => $borrower->{'firstname'}
466     );
467 }
468
469 #set up so only the last 8 returned items display (make for faster loading pages)
470 my $count = 0;
471 my @riloop;
472 foreach ( sort { $a <=> $b } keys %returneditems ) {
473     my %ri;
474     if ( $count < 8 ) {
475         my $barcode = $returneditems{$_};
476         my $duedate = $riduedate{$_};
477         my $overduetext;
478         my $borrowerinfo;
479         if ($duedate) {
480             my @tempdate = split( /-/, $duedate );
481             $ri{year}  = $tempdate[0];
482             $ri{month} = $tempdate[1];
483             $ri{day}   = $tempdate[2];
484             my $duedatenz  = "$tempdate[2]/$tempdate[1]/$tempdate[0]";
485             my @datearr    = localtime( time() );
486             my $todaysdate =
487                 $datearr[5] . '-'
488               . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
489               . sprintf( "%0.2d", $datearr[3] );
490             $ri{duedate} = format_date($duedate);
491             my ($borrower) =
492               GetMemberDetails( $riborrowernumber{$_}, 0 );
493             $ri{borrowernumber} = $borrower->{'borrowernumber'};
494             $ri{borcnum}        = $borrower->{'cardnumber'};
495             $ri{borfirstname}   = $borrower->{'firstname'};
496             $ri{borsurname}     = $borrower->{'surname'};
497             $ri{bortitle}       = $borrower->{'title'};
498         }
499         else {
500             $ri{borrowernumber} = $riborrowernumber{$_};
501         }
502
503         #        my %ri;
504         my $biblio = GetBiblioFromItemNumber(GetItemnumberFromBarcode($barcode));
505         $ri{itembiblionumber} = $biblio->{'biblionumber'};
506         $ri{itemtitle}        = $biblio->{'title'};
507         $ri{itemauthor}       = $biblio->{'author'};
508         $ri{itemtype}         = $biblio->{'itemtype'};
509         $ri{ccode}            = $biblio->{'ccode'};
510         $ri{barcode}          = $barcode;
511     }
512     else {
513         last;
514     }
515     $count++;
516     push( @riloop, \%ri );
517 }
518 $template->param( riloop => \@riloop );
519
520 $template->param(
521     genbrname               => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
522     genprname               => $printers->{$printer}->{'printername'},
523     branchname              => $branches->{C4::Context->userenv->{'branch'}}->{'branchname'},
524     printer                 => $printer,
525     errmsgloop              => \@errmsgloop,
526     intranetcolorstylesheet =>
527       C4::Context->preference("intranetcolorstylesheet"),
528     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
529     IntranetNav        => C4::Context->preference("IntranetNav"),
530 );
531
532 # actually print the page!
533 output_html_with_http_headers $query, $cookie, $template->output;