Bug 22417: Send message to /queue
[koha.git] / circ / branchtransfers.pl
blobf64d37fc76cd0db482566e120fda0d8eef1dea62
1 #!/usr/bin/perl
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>.
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Circulation;
26 use C4::Output;
27 use C4::Reserves;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Auth qw/:DEFAULT get_session/;
31 use C4::Koha;
32 use C4::Members;
33 use Koha::BiblioFrameworks;
34 use Koha::AuthorisedValues;
35 use Koha::Holds;
36 use Koha::Items;
37 use Koha::Patrons;
39 ###############################################
40 # Getting state
42 my $query = new CGI;
44 if (!C4::Context->userenv){
45 my $sessionID = $query->cookie("CGISESSID");
46 my $session;
47 $session = get_session($sessionID) if $sessionID;
48 if (!$session){
49 # no branch set we can't transfer
50 print $query->redirect("/cgi-bin/koha/circ/set-library.pl");
51 exit;
55 #######################################################################################
56 # Make the page .....
57 my ($template, $user, $cookie, $flags ) = get_template_and_user(
59 template_name => "circ/branchtransfers.tt",
60 query => $query,
61 type => "intranet",
62 flagsrequired => { circulate => "circulate_remaining_permissions" },
66 # Check transfers is allowed from system preference
67 if ( C4::Context->preference("IndependentBranchesTransfers") && !C4::Context->IsSuperLibrarian() ) {
68 print $query->redirect("/cgi-bin/koha/errors/403.pl");
69 exit;
72 my $messages;
73 my $found;
74 my $reserved;
75 my $waiting;
76 my $reqmessage;
77 my $cancelled;
78 my $setwaiting;
80 my $request = $query->param('request') || '';
81 my $borrowernumber = $query->param('borrowernumber') || 0;
82 my $frombranchcd = $query->param('frombranchcd') || C4::Context->userenv->{'branch'};
83 my $tobranchcd = $query->param('tobranchcd') || '';
85 my $ignoreRs = 0;
86 ############
87 # Deal with the requests....
88 if ( $request eq "KillWaiting" ) {
89 my $item = $query->param('itemnumber');
90 my $holds = Koha::Holds->search({
91 itemnumber => $item,
92 borrowernumber => $borrowernumber
93 });
94 if ( $holds->count ) {
95 $holds->next->cancel;
96 $cancelled = 1;
97 $reqmessage = 1;
98 } # FIXME else?
100 elsif ( $request eq "SetWaiting" ) {
101 my $item = $query->param('itemnumber');
102 ModReserveAffect( $item, $borrowernumber );
103 $ignoreRs = 1;
104 $setwaiting = 1;
105 $reqmessage = 1;
107 elsif ( $request eq 'KillReserved' ) {
108 my $biblionumber = $query->param('biblionumber');
109 my $holds = Koha::Holds->search({
110 biblionumber => $biblionumber,
111 borrowernumber => $borrowernumber
113 if ( $holds->count ) {
114 $holds->next->cancel;
115 $cancelled = 1;
116 $reqmessage = 1;
117 } # FIXME else?
120 # collect the stack of books already transferred so they can printed...
121 my @trsfitemloop;
122 my $transferred;
123 my $barcode = $query->param('barcode');
124 # remove leading/trailing whitespace
125 defined $barcode and $barcode =~ s/^\s*|\s*$//g; # FIXME: barcodeInputFilter
126 # warn "barcode : $barcode";
127 if ($barcode) {
129 ( $transferred, $messages ) =
131 transferbook({
132 from_branch => C4::Context->userenv->{'branch'},
133 to_branch => $tobranchcd,
134 barcode => $barcode,
135 ignore_reserves => $ignoreRs,
136 trigger => 'Manual'
138 my $item = Koha::Items->find({ barcode => $barcode });
139 $found = $messages->{'ResFound'};
140 if ($transferred) {
141 my %trsfitem;
142 my $frbranchcd = C4::Context->userenv->{'branch'};
143 $trsfitem{item} = $item;
144 $trsfitem{counter} = 0;
145 $trsfitem{frombrcd} = $frbranchcd;
146 $trsfitem{tobrcd} = $tobranchcd;
147 push( @trsfitemloop, \%trsfitem );
151 foreach ( $query->param ) {
152 (next) unless (/bc-(\d*)/);
153 my $counter = $1;
154 my %trsfitem;
155 my $bc = $query->param("bc-$counter");
156 my $frbcd = $query->param("fb-$counter");
157 my $tobcd = $query->param("tb-$counter");
158 $counter++;
159 $trsfitem{counter} = $counter;
160 $trsfitem{frombrcd} = $frbcd;
161 $trsfitem{tobrcd} = $tobcd;
162 my $item = Koha::Items->find({ barcode => $bc });
163 $trsfitem{item} = $item;
164 push( @trsfitemloop, \%trsfitem );
167 my $itemnumber;
168 my $biblionumber;
170 #####################
172 if ($found) {
173 my $res = $messages->{'ResFound'};
174 $itemnumber = $res->{'itemnumber'};
175 $borrowernumber = $res->{'borrowernumber'};
177 if ( $res->{'ResFound'} eq "Waiting" ) {
178 $waiting = 1;
180 elsif ( $res->{'ResFound'} eq "Reserved" ) {
181 $reserved = 1;
182 $biblionumber = $res->{'biblionumber'};
186 my @errmsgloop;
187 foreach my $code ( keys %$messages ) {
188 if ( $code ne 'WasTransfered' ) {
189 my %err;
190 if ( $code eq 'BadBarcode' ) {
191 $err{msg} = $messages->{'BadBarcode'};
192 $err{errbadcode} = 1;
194 elsif ( $code eq "NotAllowed" ) {
195 warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
196 # Do we really want a error log message here? --atz
197 $err{errnotallowed} = 1;
198 my ( $tbr, $typecode ) = split( /::/, $messages->{'NotAllowed'} );
199 $err{tbr} = $tbr;
200 $err{code} = $typecode;
202 elsif ( $code eq 'WasReturned' ) {
203 $err{errwasreturned} = 1;
204 $err{borrowernumber} = $messages->{'WasReturned'};
205 my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
206 if ( $patron ) { # Just in case...
207 $err{patron} = $patron;
210 $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
211 push( @errmsgloop, \%err );
215 # use Data::Dumper;
216 # warn "FINAL ============= ".Dumper(@trsfitemloop);
218 $template->param(
219 found => $found,
220 reserved => $reserved,
221 waiting => $waiting,
222 borrowernumber => $borrowernumber,
223 itemnumber => $itemnumber,
224 barcode => $barcode,
225 biblionumber => $biblionumber,
226 frombranchcd => $frombranchcd,
227 tobranchcd => $tobranchcd,
228 reqmessage => $reqmessage,
229 cancelled => $cancelled,
230 setwaiting => $setwaiting,
231 trsfitemloop => \@trsfitemloop,
232 errmsgloop => \@errmsgloop,
233 PatronAutoComplete => C4::Context->preference("PatronAutoComplete"),
236 # Checking if there is a Fast Cataloging Framework
237 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
239 output_html_with_http_headers $query, $cookie, $template->output;