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 barcodedecode );
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 $op = $query->param('op') || '';
80 my $borrowernumber = $query->param('borrowernumber') || 0;
81 my $tobranchcd = $query->param('tobranchcd') || '';
82 my $trigger = 'Manual';
86 # Deal with the requests....
87 if ( $op eq "cud-KillWaiting" ) {
88 my $item = $query->param('itemnumber');
89 my $holds = Koha::Holds->search({
91 borrowernumber => $borrowernumber
93 if ( $holds->count ) {
99 elsif ( $op eq "cud-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 ( $op eq 'cud-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 $barcode = barcodedecode($barcode) if $barcode;
125 if ($op eq 'cud-transfer' && $barcode) {
127 ( $transferred, $messages ) =
129 from_branch => C4::Context->userenv->{'branch'},
130 to_branch => $tobranchcd,
132 ignore_reserves => $ignoreRs,
135 my $item = Koha::Items->find({ barcode => $barcode });
136 $found = $messages->{'ResFound'} unless $settransit;
139 my $frbranchcd = C4::Context->userenv->{'branch'};
140 $trsfitem{item} = $item;
141 $trsfitem{counter} = 0;
142 $trsfitem{frombrcd} = $frbranchcd;
143 $trsfitem{tobrcd} = $tobranchcd;
144 push( @trsfitemloop, \%trsfitem );
148 foreach ( $query->param ) {
149 (next) unless (/bc-(\d*)/);
152 my $bc = $query->param("bc-$counter");
153 my $frbcd = $query->param("fb-$counter");
154 my $tobcd = $query->param("tb-$counter");
156 $trsfitem{counter} = $counter;
157 $trsfitem{frombrcd} = $frbcd;
158 $trsfitem{tobrcd} = $tobcd;
159 my $item = Koha::Items->find({ barcode => $bc });
160 $trsfitem{item} = $item;
161 push( @trsfitemloop, \%trsfitem );
167 #####################
171 $hold = Koha::Holds->find(
172 { reserve_id => $found->{reserve_id} },
173 { prefetch => ['item','patron'] }
175 $itemnumber = $found->{'itemnumber'};
176 $borrowernumber = $found->{'borrowernumber'};
178 if ( $found->{'ResFound'} eq "Waiting" ) {
180 } elsif ( $found->{'ResFound'} eq "Transferred" ) {
181 $hold_transferred = 1;
182 } elsif ( $found->{'ResFound'} eq "Processing" ) {
184 } elsif ( $found->{'ResFound'} eq "Reserved" ) {
186 $biblionumber = $found->{'biblionumber'};
191 foreach my $code ( keys %$messages ) {
192 if ( $code ne 'WasTransfered' ) {
194 if ( $code eq 'BadBarcode' ) {
195 $err{msg} = $messages->{'BadBarcode'};
196 $err{errbadcode} = 1;
198 elsif ( $code eq "NotAllowed" ) {
199 warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
200 # Do we really want a error log message here? --atz
201 $err{errnotallowed} = 1;
202 my ( $tbr, $typecode ) = split( /::/, $messages->{'NotAllowed'} );
204 $err{code} = $typecode;
206 elsif ( $code eq 'WasReturned' ) {
207 $err{errwasreturned} = 1;
208 $err{borrowernumber} = $messages->{'WasReturned'};
209 my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
210 if ( $patron ) { # Just in case...
211 $err{patron} = $patron;
214 elsif ( $code eq 'DestinationEqualsHolding' ) {
215 $err{errdesteqholding} = 1;
217 push( @errmsgloop, \%err ) if (keys %err);
222 # warn "FINAL ============= ".Dumper(@trsfitemloop);
227 reserved => $reserved,
229 transferred => $hold_transferred,
230 processing => $hold_processed,
231 borrowernumber => $borrowernumber,
232 itemnumber => $itemnumber,
234 biblionumber => $biblionumber,
235 tobranchcd => $tobranchcd,
236 reqmessage => $reqmessage,
237 cancelled => $cancelled,
238 settransit => $settransit,
239 trsfitemloop => \@trsfitemloop,
240 errmsgloop => \@errmsgloop,
241 PatronAutoComplete => C4::Context->preference("PatronAutoComplete"),
244 # Checking if there is a Fast Cataloging Framework
245 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
247 output_html_with_http_headers $query, $cookie, $template->output;