Bug 22280: Add a unit test
[koha.git] / circ / branchtransfers.pl
blobd5b8e9e7263d995a1e6e3c837302c21082b2836f
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 or $session->param('branch') eq 'NO_LIBRARY_SET'){
49 # no branch set we can't transfer
50 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.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 authnotrequired => 0,
63 flagsrequired => { circulate => "circulate_remaining_permissions" },
67 # Check transfers is allowed from system preference
68 if ( C4::Context->preference("IndependentBranchesTransfers") && !C4::Context->IsSuperLibrarian() ) {
69 print $query->redirect("/cgi-bin/koha/errors/403.pl");
70 exit;
73 my $messages;
74 my $found;
75 my $reserved;
76 my $waiting;
77 my $reqmessage;
78 my $cancelled;
79 my $setwaiting;
81 my $request = $query->param('request') || '';
82 my $borrowernumber = $query->param('borrowernumber') || 0;
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 ) =
130 transferbook( $tobranchcd, $barcode, $ignoreRs );
131 my $item = Koha::Items->find({ barcode => $barcode });
132 $found = $messages->{'ResFound'};
133 if ($transferred) {
134 my %item;
135 my $biblio = $item->biblio;
136 my $frbranchcd = C4::Context->userenv->{'branch'};
137 $item{'biblionumber'} = $item->biblionumber;
138 $item{'itemnumber'} = $item->itemnumber;
139 $item{'title'} = $biblio->title;
140 $item{'author'} = $biblio->author;
141 $item{'itemtype'} = $biblio->biblioitem->itemtype;
142 $item{'ccode'} = $item->ccode;
143 $item{'itemcallnumber'} = $item->itemcallnumber;
144 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $item->location });
145 $item{'location'} = $av->count ? $av->next->lib : '';
146 $item{counter} = 0;
147 $item{barcode} = $barcode;
148 $item{frombrcd} = $frbranchcd;
149 $item{tobrcd} = $tobranchcd;
150 push( @trsfitemloop, \%item );
154 foreach ( $query->param ) {
155 (next) unless (/bc-(\d*)/);
156 my $counter = $1;
157 my %item;
158 my $bc = $query->param("bc-$counter");
159 my $frbcd = $query->param("fb-$counter");
160 my $tobcd = $query->param("tb-$counter");
161 $counter++;
162 $item{counter} = $counter;
163 $item{barcode} = $bc;
164 $item{frombrcd} = $frbcd;
165 $item{tobrcd} = $tobcd;
166 my $item = Koha::Items->find({ barcode => $bc });
167 my $biblio = $item->biblio;
168 $item{'biblionumber'} = $item->biblionumber;
169 $item{'itemnumber'} = $item->itemnumber;
170 $item{'title'} = $biblio->title;
171 $item{'author'} = $biblio->author;
172 $item{'itemtype'} = $biblio->biblioitem->itemtype;
173 $item{'ccode'} = $item->ccode;
174 $item{'itemcallnumber'} = $item->itemcallnumber;
175 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $item->location });
176 $item{'location'} = $av->count ? $av->next->lib : '';
177 push( @trsfitemloop, \%item );
180 my $itemnumber;
181 my $biblionumber;
183 #####################
185 if ($found) {
186 my $res = $messages->{'ResFound'};
187 $itemnumber = $res->{'itemnumber'};
189 if ( $res->{'ResFound'} eq "Waiting" ) {
190 $waiting = 1;
192 elsif ( $res->{'ResFound'} eq "Reserved" ) {
193 $reserved = 1;
194 $biblionumber = $res->{'biblionumber'};
198 my @errmsgloop;
199 foreach my $code ( keys %$messages ) {
200 if ( $code ne 'WasTransfered' ) {
201 my %err;
202 if ( $code eq 'BadBarcode' ) {
203 $err{msg} = $messages->{'BadBarcode'};
204 $err{errbadcode} = 1;
206 elsif ( $code eq "NotAllowed" ) {
207 warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
208 # Do we really want a error log message here? --atz
209 $err{errnotallowed} = 1;
210 my ( $tbr, $typecode ) = split( /::/, $messages->{'NotAllowed'} );
211 $err{tbr} = $tbr;
212 $err{code} = $typecode;
214 elsif ( $code eq 'WasReturned' ) {
215 $err{errwasreturned} = 1;
216 $err{borrowernumber} = $messages->{'WasReturned'};
217 my $patron = Koha::Patrons->find( $messages->{'WasReturned'} );
218 if ( $patron ) { # Just in case...
219 $err{patron} = $patron;
222 $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
223 push( @errmsgloop, \%err );
227 # use Data::Dumper;
228 # warn "FINAL ============= ".Dumper(@trsfitemloop);
230 $template->param(
231 found => $found,
232 reserved => $reserved,
233 waiting => $waiting,
234 borrowernumber => $borrowernumber,
235 itemnumber => $itemnumber,
236 barcode => $barcode,
237 biblionumber => $biblionumber,
238 tobranchcd => $tobranchcd,
239 reqmessage => $reqmessage,
240 cancelled => $cancelled,
241 setwaiting => $setwaiting,
242 trsfitemloop => \@trsfitemloop,
243 errmsgloop => \@errmsgloop,
244 CircAutocompl => C4::Context->preference("CircAutocompl"),
247 # Checking if there is a Fast Cataloging Framework
248 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
250 output_html_with_http_headers $query, $cookie, $template->output;