Bug 10694: (follow-up) fix various issues
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2
3 #script to execute branch transfers of books
4
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25 use CGI;
26 use C4::Circulation;
27 use C4::Output;
28 use C4::Reserves;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Auth qw/:DEFAULT get_session/;
32 use C4::Branch; # GetBranches
33 use C4::Koha;
34 use C4::Members;
35
36 ###############################################
37 #  Getting state
38
39 my $query = new CGI;
40
41 if (!C4::Context->userenv){
42         my $sessionID = $query->cookie("CGISESSID");
43     my $session;
44         $session = get_session($sessionID) if $sessionID;
45         if (!$session or $session->param('branch') eq 'NO_LIBRARY_SET'){
46                 # no branch set we can't transfer
47                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
48                 exit;
49         }
50 }
51
52 #######################################################################################
53 # Make the page .....
54 my ($template, $user, $cookie) = get_template_and_user(
55     {
56         template_name   => "circ/branchtransfers.tmpl",
57         query           => $query,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { circulate => "circulate_remaining_permissions" },
61     }
62 );
63
64 my $branches = GetBranches;
65
66 my $messages;
67 my $found;
68 my $reserved;
69 my $waiting;
70 my $reqmessage;
71 my $cancelled;
72 my $setwaiting;
73
74 my $request        = $query->param('request')        || '';
75 my $borrowernumber = $query->param('borrowernumber') ||  0;
76 my $tobranchcd     = $query->param('tobranchcd')     || '';
77
78 my $ignoreRs = 0;
79 ############
80 # Deal with the requests....
81 if ( $request eq "KillWaiting" ) {
82     my $item = $query->param('itemnumber');
83     CancelReserve({
84         itemnumber     => $item,
85         borrowernumber => $borrowernumber
86     });
87     $cancelled   = 1;
88     $reqmessage  = 1;
89 }
90 elsif ( $request eq "SetWaiting" ) {
91     my $item = $query->param('itemnumber');
92     ModReserveAffect( $item, $borrowernumber );
93     $ignoreRs    = 1;
94     $setwaiting  = 1;
95     $reqmessage  = 1;
96 }
97 elsif ( $request eq 'KillReserved' ) {
98     my $biblio = $query->param('biblionumber');
99     CancelReserve({
100         biblionumber   => $biblio,
101         borrowernumber => $borrowernumber
102     });
103     $cancelled   = 1;
104     $reqmessage  = 1;
105 }
106
107 # collect the stack of books already transfered so they can printed...
108 my @trsfitemloop;
109 my %transfereditems;
110 my $transfered;
111 my $barcode = $query->param('barcode');
112 # strip whitespace
113 defined $barcode and $barcode =~ s/\s*//g;  # FIXME: barcodeInputFilter
114 # warn "barcode : $barcode";
115 if ($barcode) {
116
117     my $iteminformation;
118     ( $transfered, $messages, $iteminformation ) =
119       transferbook( $tobranchcd, $barcode, $ignoreRs );
120 #       use Data::Dumper;
121 #       warn "Transfered : $transfered / ".Dumper($messages);
122     $found = $messages->{'ResFound'};
123     if ($transfered) {
124         my %item;
125         my $frbranchcd =  C4::Context->userenv->{'branch'};
126 #         if ( not($found) ) {
127         $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
128         $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
129         $item{'title'}                 = $iteminformation->{'title'};
130         $item{'author'}                = $iteminformation->{'author'};
131         $item{'itemtype'}              = $iteminformation->{'itemtype'};
132         $item{'ccode'}                 = $iteminformation->{'ccode'};
133         $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
134         $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
135         $item{'frbrname'}              = $branches->{$frbranchcd}->{'branchname'};
136         $item{'tobrname'}              = $branches->{$tobranchcd}->{'branchname'};
137 #         }
138         $item{counter}  = 0;
139         $item{barcode}  = $barcode;
140         $item{frombrcd} = $frbranchcd;
141         $item{tobrcd}   = $tobranchcd;
142         push( @trsfitemloop, \%item );
143 #         warn Dumper(@trsfitemloop);
144     }
145 }
146
147 foreach ( $query->param ) {
148     (next) unless (/bc-(\d*)/);
149     my $counter = $1;
150     my %item;
151     my $bc    = $query->param("bc-$counter");
152     my $frbcd = $query->param("fb-$counter");
153     my $tobcd = $query->param("tb-$counter");
154     $counter++;
155     $item{counter}  = $counter;
156     $item{barcode}  = $bc;
157     $item{frombrcd} = $frbcd;
158     $item{tobrcd}   = $tobcd;
159     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
160     $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
161     $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
162     $item{'title'}                 = $iteminformation->{'title'};
163     $item{'author'}                = $iteminformation->{'author'};
164     $item{'itemtype'}              = $iteminformation->{'itemtype'};
165     $item{'ccode'}                 = $iteminformation->{'ccode'};
166     $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
167     $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
168     $item{'frbrname'}              = $branches->{$frbcd}->{'branchname'};
169     $item{'tobrname'}              = $branches->{$tobcd}->{'branchname'};
170     push( @trsfitemloop, \%item );
171 }
172
173 my $itemnumber;
174 my $biblionumber;
175
176 #####################
177
178 if ($found) {
179     my $res = $messages->{'ResFound'};
180     $itemnumber = $res->{'itemnumber'};
181
182     if ( $res->{'ResFound'} eq "Waiting" ) {
183         $waiting = 1;
184     }
185     elsif ( $res->{'ResFound'} eq "Reserved" ) {
186         $reserved  = 1;
187         $biblionumber = $res->{'biblionumber'};
188     }
189 }
190
191 #####################
192
193 # Used for branch transfer limits error messages.
194 my $codeTypeDescription = 'Collection Code';
195 my $codeType = C4::Context->preference("BranchTransferLimitsType");
196 if ( $codeType eq 'itemtype' ) {   
197     $codeTypeDescription = 'Item Type';
198 }
199
200 my @errmsgloop;
201 foreach my $code ( keys %$messages ) {
202     if ( $code ne 'WasTransfered' ) {
203         my %err;
204         if ( $code eq 'BadBarcode' ) {
205             $err{msg}        = $messages->{'BadBarcode'};
206             $err{errbadcode} = 1;
207         }
208         elsif ( $code eq "NotAllowed" ) {
209             warn "NotAllowed: $messages->{'NotAllowed'} to  " . $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
210             # Do we really want a error log message here? --atz
211             $err{errnotallowed} =  1;
212             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
213             $err{tbr}      = $branches->{ $tbr }->{'branchname'};
214             $err{code}     = $typecode;
215             $err{codeType} = $codeTypeDescription;
216         }
217         elsif ( $code eq 'IsPermanent' ) {
218             $err{errispermanent} = 1;
219             $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
220         }
221         elsif ( $code eq 'WasReturned' ) {
222             $err{errwasreturned} = 1;
223             $err{borrowernumber} = $messages->{'WasReturned'};
224             my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
225             $err{title}      = $borrower->{'title'};
226             $err{firstname}  = $borrower->{'firstname'};
227             $err{surname}    = $borrower->{'surname'};
228             $err{cardnumber} = $borrower->{'cardnumber'};
229         }
230         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
231         push( @errmsgloop, \%err );
232     }
233 }
234
235 # use Data::Dumper;
236 # warn "FINAL ============= ".Dumper(@trsfitemloop);
237 $template->param(
238     found                   => $found,
239     reserved                => $reserved,
240     waiting                 => $waiting,
241     borrowernumber          => $borrowernumber,
242     itemnumber              => $itemnumber,
243     barcode                 => $barcode,
244     biblionumber            => $biblionumber,
245     tobranchcd              => $tobranchcd,
246     reqmessage              => $reqmessage,
247     cancelled               => $cancelled,
248     setwaiting              => $setwaiting,
249     trsfitemloop            => \@trsfitemloop,
250     branchoptionloop        => GetBranchesLoop($tobranchcd),
251     errmsgloop              => \@errmsgloop,
252     CircAutocompl           => C4::Context->preference("CircAutocompl")
253 );
254 output_html_with_http_headers $query, $cookie, $template->output;
255