BIG COMMIT : cleaning of Reserves.pm. See detail on koha-devel
[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::Auth;
31 use C4::Branch; # GetBranches
32 use C4::Koha;
33
34 ###############################################
35 #  Getting state
36
37 my $query = new CGI;
38
39 #######################################################################################
40 # Make the page .....
41 my ( $template, $cookie );
42 my $user;
43 ( $template, $user, $cookie ) = get_template_and_user(
44     {
45         template_name   => "circ/branchtransfers.tmpl",
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { circulate => 1 },
50     }
51 );
52
53 my $branches = GetBranches;
54 my $branch  = GetBranch( $query,  $branches );
55
56 my $messages;
57 my $found;
58 my $reserved;
59 my $waiting;
60 my $reqmessage;
61 my $cancelled;
62 my $setwaiting;
63
64 my $request        = $query->param('request');
65 my $borrowernumber = $query->param('borrowernumber');
66 my $tobranchcd     = $query->param('tobranchcd');
67
68 ############
69 # Deal with the requests....
70 if ( $request eq "KillWaiting" ) {
71     my $item = $query->param('itemnumber');
72
73     CancelReserve( 0, $item, $borrowernumber );
74     $cancelled   = 1;
75     $reqmessage  = 1;
76 }
77
78 my $ignoreRs = 0;
79 if ( $request eq "SetWaiting" ) {
80     my $item = $query->param('itemnumber');
81     ModReserveAffect( $item, $borrowernumber );
82     $ignoreRs    = 1;
83     $setwaiting  = 1;
84     $reqmessage  = 1;
85 }
86 if ( $request eq 'KillReserved' ) {
87     my $biblio = $query->param('biblionumber');
88     CancelReserve( $biblio, 0, $borrowernumber );
89     $cancelled   = 1;
90     $reqmessage  = 1;
91 }
92
93 # set up the branchselect options....
94 my @branchoptionloop;
95 foreach my $br ( keys %$branches ) {
96     my %branch;
97     $branch{selected} = ( $br eq $tobranchcd );
98     $branch{code}     = $br;
99     $branch{name}     = $branches->{$br}->{'branchname'};
100     push( @branchoptionloop, \%branch );
101 }
102
103 # collect the stack of books already transfered so they can printed...
104 my @trsfitemloop;
105 my %transfereditems;
106 my $transfered;
107 my $barcode = $query->param('barcode');
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     if ( $res->{'ResFound'} eq "Reserved" ) {
174         $reserved  = 1;
175         $biblionumber = $res->{'biblionumber'};
176     }
177 }
178
179 #####################
180
181 my @errmsgloop;
182 foreach my $code ( keys %$messages ) {
183     my %err;
184
185     if ( $code eq 'BadBarcode' ) {
186         $err{msg}        = $messages->{'BadBarcode'};
187         $err{errbadcode} = 1;
188     }
189
190     if ( $code eq 'IsPermanent' ) {
191         $err{errispermanent} = 1;
192         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
193     }
194     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
195
196     if ( $code eq 'WasReturned' ) {
197         $err{errwasreturned} = 1;
198     }
199     push( @errmsgloop, \%err );
200 }
201
202 # use Data::Dumper;
203 # warn "FINAL ============= ".Dumper(@trsfitemloop);
204 $template->param(
205     found                   => $found,
206     reserved                => $reserved,
207     waiting                 => $waiting,
208     borrowernumber          => $borrowernumber,
209     itemnumber              => $itemnumber,
210     barcode                 => $barcode,
211     biblionumber            => $biblionumber,
212     tobranchcd              => $tobranchcd,
213     reqmessage              => $reqmessage,
214     cancelled               => $cancelled,
215     setwaiting              => $setwaiting,
216     trsfitemloop            => \@trsfitemloop,
217     branchoptionloop        => \@branchoptionloop,
218     errmsgloop              => \@errmsgloop,
219 );
220 output_html_with_http_headers $query, $cookie, $template->output;
221
222 sub name {
223     my ($borinfo) = @_;
224     return $borinfo->{'surname'} . " "
225       . $borinfo->{'title'} . " "
226       . $borinfo->{'firstname'};
227 }
228
229 # Local Variables:
230 # tab-width: 4
231 # End: