Bug 17318: Unit tests
[koha.git] / circ / branchtransfers.pl
blob864db53188e74e069ee26c530cd41f3401373f4b
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 strict;
24 use warnings;
25 use CGI qw ( -utf8 );
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::Koha;
33 use C4::Members;
35 use Koha::AuthorisedValues;
37 ###############################################
38 # Getting state
40 my $query = new CGI;
42 if (!C4::Context->userenv){
43 my $sessionID = $query->cookie("CGISESSID");
44 my $session;
45 $session = get_session($sessionID) if $sessionID;
46 if (!$session or $session->param('branch') eq 'NO_LIBRARY_SET'){
47 # no branch set we can't transfer
48 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
49 exit;
53 #######################################################################################
54 # Make the page .....
55 my ($template, $user, $cookie) = get_template_and_user(
57 template_name => "circ/branchtransfers.tt",
58 query => $query,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => { circulate => "circulate_remaining_permissions" },
65 my $messages;
66 my $found;
67 my $reserved;
68 my $waiting;
69 my $reqmessage;
70 my $cancelled;
71 my $setwaiting;
73 my $request = $query->param('request') || '';
74 my $borrowernumber = $query->param('borrowernumber') || 0;
75 my $tobranchcd = $query->param('tobranchcd') || '';
77 my $ignoreRs = 0;
78 ############
79 # Deal with the requests....
80 if ( $request eq "KillWaiting" ) {
81 my $item = $query->param('itemnumber');
82 CancelReserve({
83 itemnumber => $item,
84 borrowernumber => $borrowernumber
85 });
86 $cancelled = 1;
87 $reqmessage = 1;
89 elsif ( $request eq "SetWaiting" ) {
90 my $item = $query->param('itemnumber');
91 ModReserveAffect( $item, $borrowernumber );
92 $ignoreRs = 1;
93 $setwaiting = 1;
94 $reqmessage = 1;
96 elsif ( $request eq 'KillReserved' ) {
97 my $biblio = $query->param('biblionumber');
98 CancelReserve({
99 biblionumber => $biblio,
100 borrowernumber => $borrowernumber
102 $cancelled = 1;
103 $reqmessage = 1;
106 # collect the stack of books already transfered so they can printed...
107 my @trsfitemloop;
108 my $transfered;
109 my $barcode = $query->param('barcode');
110 # remove leading/trailing whitespace
111 defined $barcode and $barcode =~ s/^\s*|\s*$//g; # FIXME: barcodeInputFilter
112 # warn "barcode : $barcode";
113 if ($barcode) {
115 my $iteminformation;
116 ( $transfered, $messages, $iteminformation ) =
117 transferbook( $tobranchcd, $barcode, $ignoreRs );
118 # use Data::Dumper;
119 # warn "Transfered : $transfered / ".Dumper($messages);
120 $found = $messages->{'ResFound'};
121 if ($transfered) {
122 my %item;
123 my $frbranchcd = C4::Context->userenv->{'branch'};
124 # if ( not($found) ) {
125 $item{'biblionumber'} = $iteminformation->{'biblionumber'};
126 $item{'itemnumber'} = $iteminformation->{'itemnumber'};
127 $item{'title'} = $iteminformation->{'title'};
128 $item{'author'} = $iteminformation->{'author'};
129 $item{'itemtype'} = $iteminformation->{'itemtype'};
130 $item{'ccode'} = $iteminformation->{'ccode'};
131 $item{'itemcallnumber'} = $iteminformation->{'itemcallnumber'};
132 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $iteminformation->{location} });
133 $item{'location'} = $av->count ? $av->next->lib : '';
135 $item{counter} = 0;
136 $item{barcode} = $barcode;
137 $item{frombrcd} = $frbranchcd;
138 $item{tobrcd} = $tobranchcd;
139 push( @trsfitemloop, \%item );
140 # warn Dumper(@trsfitemloop);
144 foreach ( $query->param ) {
145 (next) unless (/bc-(\d*)/);
146 my $counter = $1;
147 my %item;
148 my $bc = $query->param("bc-$counter");
149 my $frbcd = $query->param("fb-$counter");
150 my $tobcd = $query->param("tb-$counter");
151 $counter++;
152 $item{counter} = $counter;
153 $item{barcode} = $bc;
154 $item{frombrcd} = $frbcd;
155 $item{tobrcd} = $tobcd;
156 my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
157 $item{'biblionumber'} = $iteminformation->{'biblionumber'};
158 $item{'itemnumber'} = $iteminformation->{'itemnumber'};
159 $item{'title'} = $iteminformation->{'title'};
160 $item{'author'} = $iteminformation->{'author'};
161 $item{'itemtype'} = $iteminformation->{'itemtype'};
162 $item{'ccode'} = $iteminformation->{'ccode'};
163 $item{'itemcallnumber'} = $iteminformation->{'itemcallnumber'};
164 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $iteminformation->{location} });
165 $item{'location'} = $av->count ? $av->next->lib : '';
166 push( @trsfitemloop, \%item );
169 my $itemnumber;
170 my $biblionumber;
172 #####################
174 if ($found) {
175 my $res = $messages->{'ResFound'};
176 $itemnumber = $res->{'itemnumber'};
178 if ( $res->{'ResFound'} eq "Waiting" ) {
179 $waiting = 1;
181 elsif ( $res->{'ResFound'} eq "Reserved" ) {
182 $reserved = 1;
183 $biblionumber = $res->{'biblionumber'};
187 my @errmsgloop;
188 foreach my $code ( keys %$messages ) {
189 if ( $code ne 'WasTransfered' ) {
190 my %err;
191 if ( $code eq 'BadBarcode' ) {
192 $err{msg} = $messages->{'BadBarcode'};
193 $err{errbadcode} = 1;
195 elsif ( $code eq "NotAllowed" ) {
196 warn "NotAllowed: $messages->{'NotAllowed'} to branchcode " . $messages->{'NotAllowed'};
197 # Do we really want a error log message here? --atz
198 $err{errnotallowed} = 1;
199 my ( $tbr, $typecode ) = split( /::/, $messages->{'NotAllowed'} );
200 $err{tbr} = $tbr;
201 $err{code} = $typecode;
203 elsif ( $code eq 'IsPermanent' ) {
204 $err{errispermanent} = 1;
205 $err{msg} = $messages->{'IsPermanent'};
207 elsif ( $code eq 'WasReturned' ) {
208 $err{errwasreturned} = 1;
209 $err{borrowernumber} = $messages->{'WasReturned'};
210 my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
211 $err{title} = $borrower->{'title'};
212 $err{firstname} = $borrower->{'firstname'};
213 $err{surname} = $borrower->{'surname'};
214 $err{cardnumber} = $borrower->{'cardnumber'};
216 $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
217 push( @errmsgloop, \%err );
221 # use Data::Dumper;
222 # warn "FINAL ============= ".Dumper(@trsfitemloop);
223 $template->param(
224 found => $found,
225 reserved => $reserved,
226 waiting => $waiting,
227 borrowernumber => $borrowernumber,
228 itemnumber => $itemnumber,
229 barcode => $barcode,
230 biblionumber => $biblionumber,
231 tobranchcd => $tobranchcd,
232 reqmessage => $reqmessage,
233 cancelled => $cancelled,
234 setwaiting => $setwaiting,
235 trsfitemloop => \@trsfitemloop,
236 errmsgloop => \@errmsgloop,
237 CircAutocompl => C4::Context->preference("CircAutocompl")
239 output_html_with_http_headers $query, $cookie, $template->output;