Merge remote-tracking branch 'origin/new/bug_5327'
[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{'itemnumber'}            = $iteminformation->{'itemnumber'};
123         $item{'title'}                 = $iteminformation->{'title'};
124         $item{'author'}                = $iteminformation->{'author'};
125         $item{'itemtype'}              = $iteminformation->{'itemtype'};
126         $item{'ccode'}                 = $iteminformation->{'ccode'};
127         $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
128         $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
129         $item{'frbrname'}              = $branches->{$frbranchcd}->{'branchname'};
130         $item{'tobrname'}              = $branches->{$tobranchcd}->{'branchname'};
131 #         }
132         $item{counter}  = 0;
133         $item{barcode}  = $barcode;
134         $item{frombrcd} = $frbranchcd;
135         $item{tobrcd}   = $tobranchcd;
136         push( @trsfitemloop, \%item );
137 #         warn Dumper(@trsfitemloop);
138     }
139 }
140
141 foreach ( $query->param ) {
142     (next) unless (/bc-(\d*)/);
143     my $counter = $1;
144     my %item;
145     my $bc    = $query->param("bc-$counter");
146     my $frbcd = $query->param("fb-$counter");
147     my $tobcd = $query->param("tb-$counter");
148     $counter++;
149     $item{counter}  = $counter;
150     $item{barcode}  = $bc;
151     $item{frombrcd} = $frbcd;
152     $item{tobrcd}   = $tobcd;
153     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
154     $item{'biblionumber'}          = $iteminformation->{'biblionumber'};
155     $item{'itemnumber'}            = $iteminformation->{'itemnumber'};
156     $item{'title'}                 = $iteminformation->{'title'};
157     $item{'author'}                = $iteminformation->{'author'};
158     $item{'itemtype'}              = $iteminformation->{'itemtype'};
159     $item{'ccode'}                 = $iteminformation->{'ccode'};
160     $item{'itemcallnumber'}        = $iteminformation->{'itemcallnumber'};
161     $item{'location'}              = GetKohaAuthorisedValueLib("LOC",$iteminformation->{'location'});
162     $item{'frbrname'}              = $branches->{$frbcd}->{'branchname'};
163     $item{'tobrname'}              = $branches->{$tobcd}->{'branchname'};
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 #####################
186
187 # Used for branch transfer limits error messages.
188 my $codeTypeDescription = 'Collection Code';
189 my $codeType = C4::Context->preference("BranchTransferLimitsType");
190 if ( $codeType eq 'itemtype' ) {   
191     $codeTypeDescription = 'Item Type';
192 }
193
194 my @errmsgloop;
195 foreach my $code ( keys %$messages ) {
196     if ( $code ne 'WasTransfered' ) {
197         my %err;
198         if ( $code eq 'BadBarcode' ) {
199             $err{msg}        = $messages->{'BadBarcode'};
200             $err{errbadcode} = 1;
201         }
202         elsif ( $code eq "NotAllowed" ) {
203             warn "NotAllowed: $messages->{'NotAllowed'} to  " . $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
204             # Do we really want a error log message here? --atz
205             $err{errnotallowed} =  1;
206             my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
207             $err{tbr}      = $branches->{ $tbr }->{'branchname'};
208             $err{code}     = $typecode;
209             $err{codeType} = $codeTypeDescription;
210         }
211         elsif ( $code eq 'IsPermanent' ) {
212             $err{errispermanent} = 1;
213             $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
214         }
215         elsif ( $code eq 'WasReturned' ) {
216             $err{errwasreturned} = 1;
217             $err{borrowernumber} = $messages->{'WasReturned'};
218             my $borrower = GetMember('borrowernumber'=>$messages->{'WasReturned'});
219             $err{title}      = $borrower->{'title'};
220             $err{firstname}  = $borrower->{'firstname'};
221             $err{surname}    = $borrower->{'surname'};
222             $err{cardnumber} = $borrower->{'cardnumber'};
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     branchoptionloop        => GetBranchesLoop($tobranchcd),
245     errmsgloop              => \@errmsgloop,
246     CircAutocompl           => C4::Context->preference("CircAutocompl")
247 );
248 output_html_with_http_headers $query, $cookie, $template->output;
249