3 #script to execute branch transfers of books
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use C4::Circulation qw( transferbook );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Reserves qw( ModReserve ModReserveAffect );
28 use C4::Auth qw( get_session get_template_and_user );
30 use Koha::BiblioFrameworks;
31 use Koha::AuthorisedValues;
36 ###############################################
41 if (!C4::Context->userenv){
42 my $sessionID = $query->cookie("CGISESSID");
44 $session = get_session($sessionID) if $sessionID;
46 # no branch set we can't transfer
47 print $query->redirect("/cgi-bin/koha/circ/set-library.pl");
52 #######################################################################################
54 my ($template, $user, $cookie, $flags ) = get_template_and_user(
56 template_name => "circ/branchtransfers.tt",
59 flagsrequired => { circulate => "circulate_remaining_permissions" },
63 # Check transfers is allowed from system preference
64 if ( C4::Context->preference("IndependentBranchesTransfers") && !C4::Context->IsSuperLibrarian() ) {
65 print $query->redirect("/cgi-bin/koha/errors/403.pl");
79 my $request = $query->param('request') || '';
80 my $borrowernumber = $query->param('borrowernumber') || 0;
81 my $tobranchcd = $query->param('tobranchcd') || '';
82 my $trigger = 'Manual';
86 # Deal with the requests....
87 if ( $request eq "KillWaiting" ) {
88 my $item = $query->param('itemnumber');
89 my $holds = Koha::Holds->search({
91 borrowernumber => $borrowernumber
93 if ( $holds->count ) {
99 elsif ( $request eq "SetTransit" ) {
100 my $item = $query->param('itemnumber');
101 my $reserve_id = $query->param('reserve_id');
102 ModReserveAffect( $item, $borrowernumber, 1, $reserve_id );
106 $trigger = 'Reserve';
108 elsif ( $request eq 'KillReserved' ) {
109 my $biblionumber = $query->param('biblionumber');
110 my $reserve_id = $query->param('reserve_id');
111 my $hold = Koha::Holds->find({ reserve_id => $reserve_id });
119 # collect the stack of books already transferred so they can printed...
122 my $barcode = $query->param('barcode');
123 # remove leading/trailing whitespace
124 defined $barcode and $barcode =~ s/^\s*|\s*$//g; # FIXME: barcodeInputFilter
125 # warn "barcode : $barcode";
128 ( $transferred, $messages ) =
130 from_branch => C4::Context->userenv->{'branch'},
131 to_branch => $tobranchcd,
133 ignore_reserves => $ignoreRs,
136 my $item = Koha::Items->find({ barcode => $barcode });
137 $found = $messages->{'ResFound'} unless $settransit;
140 my $frbranchcd = C4::Context->userenv->{'branch'};
141 $trsfitem{item} = $item;
142 $trsfitem{counter} = 0;
143 $trsfitem{frombrcd} = $frbranchcd;
144 $trsfitem{tobrcd} = $tobranchcd;
145 push( @trsfitemloop, \%trsfitem );
149 foreach ( $query->param ) {
150 (next) unless (/bc-(\d*)/);
153 my $bc = $query->param("bc-$counter");
154 my $frbcd = $query->param("fb-$counter");
155 my $tobcd = $query->param("tb-$counter");
157 $trsfitem{counter} = $counter;
158 $trsfitem{frombrcd} = $frbcd;
159 $trsfitem{tobrcd} = $tobcd;
160 my $item = Koha::Items->find({ barcode => $bc });
161 $trsfitem{item} = $item;
162 push( @trsfitemloop, \%trsfitem );
168 #####################
172 $hold = Koha::Holds->find(
173 { reserve_id => $found->{reserve_id} },
174 { prefetch => ['item','patron'] }
176 $itemnumber = $found->{'itemnumber'};
177 $borrowernumber = $found->{'borrowernumber'};
179 if ( $found->{'ResFound'} eq "Waiting" ) {
181 } elsif ( $found->{'ResFound'} eq "Transferred" ) {
182 $hold_transferred = 1;
183 } elsif ( $found->{'ResFound'} eq "Processing" ) {
185 } elsif ( $found->{'ResFound'} eq "Reserved" ) {
187 $biblionumber = $found->{'biblionumber'};
192 foreach my $code ( keys %$messages ) {
193 if ( $code ne 'WasTransfered' ) {
195 if ( $code eq 'BadBarcode' ) {
196 $err{msg} = $messages->{'BadBarcode'};
197 $err{errbadcode} = 1;
199 elsif ( $code eq "NotAllowed" ) {
200 warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
201 # Do we really want a error log message here? --atz
202 $err{errnotallowed} = 1;
203 my ( $tbr, $typecode ) = split( /::/, $messages->{'NotAllowed'} );
205 $err{code} = $typecode;
207 elsif ( $code eq 'WasReturned' ) {
208 $err{errwasreturned} = 1;
209 $err{borrowernumber} = $messages->{'WasReturned'};
210 my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
211 if ( $patron ) { # Just in case...
212 $err{patron} = $patron;
215 elsif ( $code eq 'DestinationEqualsHolding' ) {
216 $err{errdesteqholding} = 1;
218 push( @errmsgloop, \%err ) if (keys %err);
223 # warn "FINAL ============= ".Dumper(@trsfitemloop);
228 reserved => $reserved,
230 transferred => $hold_transferred,
231 processing => $hold_processed,
232 borrowernumber => $borrowernumber,
233 itemnumber => $itemnumber,
235 biblionumber => $biblionumber,
236 tobranchcd => $tobranchcd,
237 reqmessage => $reqmessage,
238 cancelled => $cancelled,
239 settransit => $settransit,
240 trsfitemloop => \@trsfitemloop,
241 errmsgloop => \@errmsgloop,
242 PatronAutoComplete => C4::Context->preference("PatronAutoComplete"),
245 # Checking if there is a Fast Cataloging Framework
246 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
248 output_html_with_http_headers $query, $cookie, $template->output;