Merge remote branch 'kc/new/bug_4912' into kcmaster
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25 use CGI;
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.tmpl",
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( 0, $item, $borrowernumber );
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( $biblio, 0, $borrowernumber );
97     $cancelled   = 1;
98     $reqmessage  = 1;
99 }
100
101 # collect the stack of books already transfered so they can printed...
102 my @trsfitemloop;
103 my %transfereditems;
104 my $transfered;
105 my $barcode = $query->param('barcode');
106 # strip whitespace
107 defined $barcode and $barcode =~ s/\s*//g;  # FIXME: barcodeInputFilter
108 # warn "barcode : $barcode";
109 if ($barcode) {
110
111     my $iteminformation;
112     ( $transfered, $messages, $iteminformation ) =
113       transferbook( $tobranchcd, $barcode, $ignoreRs );
114 #       use Data::Dumper;
115 #       warn "Transfered : $transfered / ".Dumper($messages);
116     $found = $messages->{'ResFound'};
117     if ($transfered) {
118         my %item;
119         my $frbranchcd =  C4::Context->userenv->{'branch'};
120 #         if ( not($found) ) {
121         $item{'biblionumber'} = $iteminformation->{'biblionumber'};
122         $item{'title'}        = $iteminformation->{'title'};
123         $item{'author'}       = $iteminformation->{'author'};
124         $item{'itemtype'}     = $iteminformation->{'itemtype'};
125         $item{'ccode'}        = $iteminformation->{'ccode'};
126         $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
127         $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
128 #         }
129         $item{counter}  = 0;
130         $item{barcode}  = $barcode;
131         $item{frombrcd} = $frbranchcd;
132         $item{tobrcd}   = $tobranchcd;
133         push( @trsfitemloop, \%item );
134 #         warn Dumper(@trsfitemloop);
135     }
136 }
137
138 foreach ( $query->param ) {
139     (next) unless (/bc-(\d*)/);
140     my $counter = $1;
141     my %item;
142     my $bc    = $query->param("bc-$counter");
143     my $frbcd = $query->param("fb-$counter");
144     my $tobcd = $query->param("tb-$counter");
145     $counter++;
146     $item{counter}  = $counter;
147     $item{barcode}  = $bc;
148     $item{frombrcd} = $frbcd;
149     $item{tobrcd}   = $tobcd;
150     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
151     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
152     $item{'title'}        = $iteminformation->{'title'};
153     $item{'author'}       = $iteminformation->{'author'};
154     $item{'itemtype'}     = $iteminformation->{'itemtype'};
155     $item{'ccode'}        = $iteminformation->{'ccode'};
156     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
157     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
158     push( @trsfitemloop, \%item );
159 }
160
161 my $itemnumber;
162 my $biblionumber;
163
164 #####################
165
166 if ($found) {
167     my $res = $messages->{'ResFound'};
168     $itemnumber = $res->{'itemnumber'};
169
170     if ( $res->{'ResFound'} eq "Waiting" ) {
171         $waiting = 1;
172     }
173     elsif ( $res->{'ResFound'} eq "Reserved" ) {
174         $reserved  = 1;
175         $biblionumber = $res->{'biblionumber'};
176     }
177 }
178
179 #####################
180
181 # Used for branch transfer limits error messages.
182 my $codeTypeDescription = 'Collection Code';
183 my $codeType = C4::Context->preference("BranchTransferLimitsType");
184 if ( $codeType eq 'itemtype' ) {   
185     $codeTypeDescription = 'Item Type';
186 }
187
188 my @errmsgloop;
189 foreach my $code ( keys %$messages ) {
190     my %err;
191     if ( $code eq 'BadBarcode' ) {
192         $err{msg}        = $messages->{'BadBarcode'};
193         $err{errbadcode} = 1;
194     }
195     elsif ( $code eq "NotAllowed" ) {
196         warn "NotAllowed: $messages->{'NotAllowed'} to  " . $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
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}      = $branches->{ $tbr }->{'branchname'};
201         $err{code}     = $typecode;
202         $err{codeType} = $codeTypeDescription; 
203     }
204     elsif ( $code eq 'IsPermanent' ) {
205         $err{errispermanent} = 1;
206         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
207     }
208     elsif ( $code eq 'WasReturned' ) {
209         $err{errwasreturned} = 1;
210                 $err{borrowernumber} = $messages->{'WasReturned'};
211                 my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
212                 $err{title}      = $borrower->{'title'};
213                 $err{firstname}  = $borrower->{'firstname'};
214                 $err{surname}    = $borrower->{'surname'};
215                 $err{cardnumber} = $borrower->{'cardnumber'};
216     }
217     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
218     push( @errmsgloop, \%err );
219 }
220
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     branchoptionloop        => GetBranchesLoop($tobranchcd),
237     errmsgloop              => \@errmsgloop,
238     CircAutocompl           => C4::Context->preference("CircAutocompl")
239 );
240 output_html_with_http_headers $query, $cookie, $template->output;
241