Bug 15006: [QA Follow-up] Satisfy qa tools with one tab less
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2
3 #script to execute branch transfers of books
4
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
19 #
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>.
22
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::Branch; # GetBranches
33 use C4::Koha;
34 use C4::Members;
35
36 ###############################################
37 #  Getting state
38
39 my $query = new CGI;
40
41 if (!C4::Context->userenv){
42         my $sessionID = $query->cookie("CGISESSID");
43     my $session;
44         $session = get_session($sessionID) if $sessionID;
45         if (!$session or $session->param('branch') eq 'NO_LIBRARY_SET'){
46                 # no branch set we can't transfer
47                 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
48                 exit;
49         }
50 }
51
52 #######################################################################################
53 # Make the page .....
54 my ($template, $user, $cookie) = get_template_and_user(
55     {
56         template_name   => "circ/branchtransfers.tt",
57         query           => $query,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { circulate => "circulate_remaining_permissions" },
61     }
62 );
63
64 my $branches = GetBranches;
65
66 my $messages;
67 my $found;
68 my $reserved;
69 my $waiting;
70 my $reqmessage;
71 my $cancelled;
72 my $setwaiting;
73
74 my $request        = $query->param('request')        || '';
75 my $borrowernumber = $query->param('borrowernumber') ||  0;
76 my $tobranchcd     = $query->param('tobranchcd')     || '';
77
78 my $ignoreRs = 0;
79 ############
80 # Deal with the requests....
81 if ( $request eq "KillWaiting" ) {
82     my $item = $query->param('itemnumber');
83     CancelReserve({
84         itemnumber     => $item,
85         borrowernumber => $borrowernumber
86     });
87     $cancelled   = 1;
88     $reqmessage  = 1;
89 }
90 elsif ( $request eq "SetWaiting" ) {
91     my $item = $query->param('itemnumber');
92     ModReserveAffect( $item, $borrowernumber );
93     $ignoreRs    = 1;
94     $setwaiting  = 1;
95     $reqmessage  = 1;
96 }
97 elsif ( $request eq 'KillReserved' ) {
98     my $biblio = $query->param('biblionumber');
99     CancelReserve({
100         biblionumber   => $biblio,
101         borrowernumber => $borrowernumber
102     });
103     $cancelled   = 1;
104     $reqmessage  = 1;
105 }
106
107 # collect the stack of books already transfered so they can printed...
108 my @trsfitemloop;
109 my $transfered;
110 my $barcode = $query->param('barcode');
111 # remove leading/trailing whitespace
112 defined $barcode and $barcode =~ s/^\s*|\s*$//g;  # FIXME: barcodeInputFilter
113 # warn "barcode : $barcode";
114 if ($barcode) {
115
116     my $iteminformation;
117     ( $transfered, $messages, $iteminformation ) =
118       transferbook( $tobranchcd, $barcode, $ignoreRs );
119 #       use Data::Dumper;
120 #       warn "Transfered : $transfered / ".Dumper($messages);
121     $found = $messages->{'ResFound'};
122     if ($transfered) {
123         my %item;
124         my $frbranchcd =  C4::Context->userenv->{'branch'};
125 #         if ( not($found) ) {
126         $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
127         $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
128         $item{'title'}                 = $iteminformation->{'title'};
129         $item{'author'}                = $iteminformation->{'author'};
130         $item{'itemtype'}              = $iteminformation->{'itemtype'};
131         $item{'ccode'}                 = $iteminformation->{'ccode'};
132         $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
133         $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
134         $item{'frbrname'}              = $branches->{$frbranchcd}->{'branchname'};
135         $item{'tobrname'}              = $branches->{$tobranchcd}->{'branchname'};
136 #         }
137         $item{counter}  = 0;
138         $item{barcode}  = $barcode;
139         $item{frombrcd} = $frbranchcd;
140         $item{tobrcd}   = $tobranchcd;
141         push( @trsfitemloop, \%item );
142 #         warn Dumper(@trsfitemloop);
143     }
144 }
145
146 foreach ( $query->param ) {
147     (next) unless (/bc-(\d*)/);
148     my $counter = $1;
149     my %item;
150     my $bc    = $query->param("bc-$counter");
151     my $frbcd = $query->param("fb-$counter");
152     my $tobcd = $query->param("tb-$counter");
153     $counter++;
154     $item{counter}  = $counter;
155     $item{barcode}  = $bc;
156     $item{frombrcd} = $frbcd;
157     $item{tobrcd}   = $tobcd;
158     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
159     $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
160     $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
161     $item{'title'}                 = $iteminformation->{'title'};
162     $item{'author'}                = $iteminformation->{'author'};
163     $item{'itemtype'}              = $iteminformation->{'itemtype'};
164     $item{'ccode'}                 = $iteminformation->{'ccode'};
165     $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
166     $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
167     $item{'frbrname'}              = $branches->{$frbcd}->{'branchname'};
168     $item{'tobrname'}              = $branches->{$tobcd}->{'branchname'};
169     push( @trsfitemloop, \%item );
170 }
171
172 my $itemnumber;
173 my $biblionumber;
174
175 #####################
176
177 if ($found) {
178     my $res = $messages->{'ResFound'};
179     $itemnumber = $res->{'itemnumber'};
180
181     if ( $res->{'ResFound'} eq "Waiting" ) {
182         $waiting = 1;
183     }
184     elsif ( $res->{'ResFound'} eq "Reserved" ) {
185         $reserved  = 1;
186         $biblionumber = $res->{'biblionumber'};
187     }
188 }
189
190 my @errmsgloop;
191 foreach my $code ( keys %$messages ) {
192     if ( $code ne 'WasTransfered' ) {
193         my %err;
194         if ( $code eq 'BadBarcode' ) {
195             $err{msg}        = $messages->{'BadBarcode'};
196             $err{errbadcode} = 1;
197         }
198         elsif ( $code eq "NotAllowed" ) {
199             warn "NotAllowed: $messages->{'NotAllowed'} to  " . $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
200             # Do we really want a error log message here? --atz
201             $err{errnotallowed} =  1;
202             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
203             $err{tbr}      = $branches->{ $tbr }->{'branchname'};
204             $err{code}     = $typecode;
205         }
206         elsif ( $code eq 'IsPermanent' ) {
207             $err{errispermanent} = 1;
208             $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
209         }
210         elsif ( $code eq 'WasReturned' ) {
211             $err{errwasreturned} = 1;
212             $err{borrowernumber} = $messages->{'WasReturned'};
213             my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
214             $err{title}      = $borrower->{'title'};
215             $err{firstname}  = $borrower->{'firstname'};
216             $err{surname}    = $borrower->{'surname'};
217             $err{cardnumber} = $borrower->{'cardnumber'};
218         }
219         $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
220         push( @errmsgloop, \%err );
221     }
222 }
223
224 # use Data::Dumper;
225 # warn "FINAL ============= ".Dumper(@trsfitemloop);
226 $template->param(
227     found                   => $found,
228     reserved                => $reserved,
229     waiting                 => $waiting,
230     borrowernumber          => $borrowernumber,
231     itemnumber              => $itemnumber,
232     barcode                 => $barcode,
233     biblionumber            => $biblionumber,
234     tobranchcd              => $tobranchcd,
235     reqmessage              => $reqmessage,
236     cancelled               => $cancelled,
237     setwaiting              => $setwaiting,
238     trsfitemloop            => \@trsfitemloop,
239     branchoptionloop        => GetBranchesLoop($tobranchcd),
240     errmsgloop              => \@errmsgloop,
241     CircAutocompl           => C4::Context->preference("CircAutocompl")
242 );
243 output_html_with_http_headers $query, $cookie, $template->output;
244