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