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