perltidy finishreceive.pl and two comments added
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2 # WARNING: This file uses 4-character tabs!
3
4 #written 11/3/2002 by Finlay
5 #script to execute branch transfers of books
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
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 = get_session($sessionID);
44         if ($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 #######################################################################################
53 # Make the page .....
54 my ( $template, $cookie );
55 my $user;
56 ( $template, $user, $cookie ) = get_template_and_user(
57     {
58         template_name   => "circ/branchtransfers.tmpl",
59         query           => $query,
60         type            => "intranet",
61         authnotrequired => 0,
62         flagsrequired   => { circulate => "circulate_remaining_permissions" },
63     }
64 );
65
66 my $branches = GetBranches;
67 my $branch  = GetBranch( $query,  $branches );
68
69 my $messages;
70 my $found;
71 my $reserved;
72 my $waiting;
73 my $reqmessage;
74 my $cancelled;
75 my $setwaiting;
76
77 my $request        = $query->param('request');
78 my $borrowernumber = $query->param('borrowernumber');
79 my $tobranchcd     = $query->param('tobranchcd');
80
81 ############
82 # Deal with the requests....
83 if ( $request eq "KillWaiting" ) {
84     my $item = $query->param('itemnumber');
85
86     CancelReserve( 0, $item, $borrowernumber );
87     $cancelled   = 1;
88     $reqmessage  = 1;
89 }
90
91 my $ignoreRs = 0;
92 if ( $request eq "SetWaiting" ) {
93     my $item = $query->param('itemnumber');
94     ModReserveAffect( $item, $borrowernumber );
95     $ignoreRs    = 1;
96     $setwaiting  = 1;
97     $reqmessage  = 1;
98 }
99 if ( $request eq 'KillReserved' ) {
100     my $biblio = $query->param('biblionumber');
101     CancelReserve( $biblio, 0, $borrowernumber );
102     $cancelled   = 1;
103     $reqmessage  = 1;
104 }
105
106 # set up the branchselect options....
107 my @branchoptionloop;
108 foreach my $br ( keys %$branches ) {
109     my %branch;
110     $branch{selected} = ( $br eq $tobranchcd );
111     $branch{code}     = $br;
112     $branch{name}     = $branches->{$br}->{'branchname'};
113     push( @branchoptionloop, \%branch );
114 }
115
116 # collect the stack of books already transfered so they can printed...
117 my @trsfitemloop;
118 my %transfereditems;
119 my $transfered;
120 my $barcode = $query->param('barcode');
121 # strip whitespace
122 $barcode =~ s/\s*//g;
123 # warn "barcode : $barcode";
124 if ($barcode) {
125
126     my $iteminformation;
127     ( $transfered, $messages, $iteminformation ) =
128       transferbook( $tobranchcd, $barcode, $ignoreRs );
129 #       use Data::Dumper;
130 #       warn "Transfered : $transfered / ".Dumper($messages);
131     $found = $messages->{'ResFound'};
132     if ($transfered) {
133         my %item;
134         my $frbranchcd =  C4::Context->userenv->{'branch'};
135 #         if ( not($found) ) {
136         $item{'biblionumber'} = $iteminformation->{'biblionumber'};
137         $item{'title'}        = $iteminformation->{'title'};
138         $item{'author'}       = $iteminformation->{'author'};
139         $item{'itemtype'}     = $iteminformation->{'itemtype'};
140         $item{'ccode'}        = $iteminformation->{'ccode'};
141         $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
142         $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
143 #         }
144         $item{counter}  = 0;
145         $item{barcode}  = $barcode;
146         $item{frombrcd} = $frbranchcd;
147         $item{tobrcd}   = $tobranchcd;
148         push( @trsfitemloop, \%item );
149 #         warn Dumper(@trsfitemloop);
150     }
151 }
152
153 foreach ( $query->param ) {
154     (next) unless (/bc-(\d*)/);
155     my $counter = $1;
156     my %item;
157     my $bc    = $query->param("bc-$counter");
158     my $frbcd = $query->param("fb-$counter");
159     my $tobcd = $query->param("tb-$counter");
160     $counter++;
161     $item{counter}  = $counter;
162     $item{barcode}  = $bc;
163     $item{frombrcd} = $frbcd;
164     $item{tobrcd}   = $tobcd;
165     my ($iteminformation) = GetBiblioFromItemNumber( GetItemnumberFromBarcode($bc) );
166     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
167     $item{'title'}        = $iteminformation->{'title'};
168     $item{'author'}       = $iteminformation->{'author'};
169     $item{'itemtype'}     = $iteminformation->{'itemtype'};
170     $item{'ccode'}        = $iteminformation->{'ccode'};
171     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
172     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
173     push( @trsfitemloop, \%item );
174 }
175
176 my $itemnumber;
177 my $biblionumber;
178
179 #####################
180
181 if ($found) {
182     my $res = $messages->{'ResFound'};
183     $itemnumber = $res->{'itemnumber'};
184
185     if ( $res->{'ResFound'} eq "Waiting" ) {
186         $waiting = 1;
187     }
188     if ( $res->{'ResFound'} eq "Reserved" ) {
189         $reserved  = 1;
190         $biblionumber = $res->{'biblionumber'};
191     }
192 }
193
194 #####################
195
196 # Used for branch transfer limits error messages.
197 my $codeTypeDescription = 'Collection Code';
198 my $codeType = C4::Context->preference("BranchTransferLimitsType");
199 if ( $codeType eq 'itemtype' ) {   
200   $codeTypeDescription = 'Item Type';
201 }
202
203
204 my @errmsgloop;
205 foreach my $code ( keys %$messages ) {
206     my %err;
207
208     if ( $code eq 'BadBarcode' ) {
209         $err{msg}        = $messages->{'BadBarcode'};
210         $err{errbadcode} = 1;
211     }
212
213     if ( $code eq "NotAllowed" ) {
214     warn $messages->{'NotAllowed'};
215     warn  $branches->{ $messages->{'NotAllowed'} }->{'branchname'};
216         $err{errnotallowed} =  1;
217         my ( $tbr, $typecode ) = split( /::/,  $messages->{'NotAllowed'} );
218         $err{tbr} = $branches->{ $tbr }->{'branchname'};
219         $err{code} = $typecode;
220         $err{codeType} = $codeTypeDescription; 
221     }
222     
223     if ( $code eq 'IsPermanent' ) {
224         $err{errispermanent} = 1;
225         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
226     }
227     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
228
229     if ( $code eq 'WasReturned' ) {
230         $err{errwasreturned} = 1;
231                 $err{borrowernumber}=$messages->{'WasReturned'};
232                 my $borrower = GetMember($messages->{'WasReturned'},'borrowernumber');
233                 $err{title}=$borrower->{'title'};
234                 $err{firstname}=$borrower->{'firstname'};
235                 $err{surname}=$borrower->{'surname'};
236                 $err{cardnumber} =$borrower->{'cardnumber'};
237     }
238     push( @errmsgloop, \%err );
239 }
240
241 # use Data::Dumper;
242 # warn "FINAL ============= ".Dumper(@trsfitemloop);
243 $template->param(
244     found                   => $found,
245     reserved                => $reserved,
246     waiting                 => $waiting,
247     borrowernumber          => $borrowernumber,
248     itemnumber              => $itemnumber,
249     barcode                 => $barcode,
250     biblionumber            => $biblionumber,
251     tobranchcd              => $tobranchcd,
252     reqmessage              => $reqmessage,
253     cancelled               => $cancelled,
254     setwaiting              => $setwaiting,
255     trsfitemloop            => \@trsfitemloop,
256     branchoptionloop        => \@branchoptionloop,
257     errmsgloop              => \@errmsgloop,
258     CircAutocompl           => C4::Context->preference("CircAutocompl")
259 );
260 output_html_with_http_headers $query, $cookie, $template->output;
261
262 sub name {
263     my ($borinfo) = @_;
264     return $borinfo->{'surname'} . " "
265       . $borinfo->{'title'} . " "
266       . $borinfo->{'firstname'};
267 }
268
269 # Local Variables:
270 # tab-width: 4
271 # End: