HUGE COMMIT : code cleaning circulation.
[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::Reserves2;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31 use C4::Branch; # GetBranches
32 use C4::Koha;
33
34 ###############################################
35 # constants
36
37 my %env;
38 my $branches = GetBranches;
39 my $printers = GetPrinters( \%env );
40
41 ###############################################
42 #  Getting state
43
44 my $query = new CGI;
45
46 my $branch  = GetBranch( $query,  $branches );
47 my $printer = GetPrinter( $query, $printers );
48
49 my $genbrname = $branches->{$branch}->{'branchname'};
50 my $genprname = $printers->{$printer}->{'printername'};
51
52 my $messages;
53 my $found;
54 my $reserved;
55 my $waiting;
56 my $reqmessage;
57 my $cancelled;
58 my $setwaiting;
59 my $reqbrchname;
60 my $allmessages;
61
62 my $request        = $query->param('request');
63 my $borrowernumber = $query->param('borrowernumber');
64
65 my $tobranchcd = $query->param('tobranchcd');
66 my $frbranchcd = '';
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     $allmessages = 1;
77 }
78
79 my $ignoreRs = 0;
80 if ( $request eq "SetWaiting" ) {
81     my $item = $query->param('itemnumber');
82     $tobranchcd  = ReserveWaiting( $item, $borrowernumber );
83     $reqbrchname = $branches->{$tobranchcd}->{'branchname'};
84     $ignoreRs    = 1;
85     $setwaiting  = 1;
86     $reqmessage  = 1;
87     $allmessages = 1;
88 }
89 if ( $request eq 'KillReserved' ) {
90     my $biblio = $query->param('biblionumber');
91     CancelReserve( $biblio, 0, $borrowernumber );
92     $cancelled   = 1;
93     $reqmessage  = 1;
94     $allmessages = 1;
95 }
96
97 # set up the branchselect options....
98 my @branchoptionloop;
99 foreach my $br ( keys %$branches ) {
100
101     #(next) unless $branches->{$br}->{'CU'}; #FIXME disabled to fix bug 202
102     my %branch;
103     $branch{selected} = ( $br eq $tobranchcd );
104     $branch{code}     = $br;
105     $branch{name}     = $branches->{$br}->{'branchname'};
106     push( @branchoptionloop, \%branch );
107 }
108
109 # collect the stack of books already transfered so they can printed...
110 my @trsfitemloop;
111 my %transfereditems;
112 my %frbranchcds;
113 my %tobranchcds;
114 my $transfered;
115 my $barcode = $query->param('barcode');
116 if ($barcode) {
117
118     my $iteminformation;
119     ( $transfered, $messages, $iteminformation ) =
120       transferbook( $tobranchcd, $barcode, $ignoreRs );
121     $found = $messages->{'ResFound'};
122     if ($transfered) {
123         my %item;
124         my $frbranchcd = $iteminformation->{'frbranchcd'};
125         if ( not($found) ) {
126             $item{'biblionumber'} = $iteminformation->{'biblionumber'};
127             $item{'title'}        = $iteminformation->{'title'};
128             $item{'author'}       = $iteminformation->{'author'};
129             $item{'itemtype'}     = $iteminformation->{'itemtype'};
130             $item{'ccode'}        = $iteminformation->{'ccode'};
131             $item{'frbrname'}     = $branches->{$frbranchcd}->{'branchname'};
132             $item{'tobrname'}     = $branches->{$tobranchcd}->{'branchname'};
133         }
134         $item{counter}  = 0;
135         $item{barcode}  = $barcode;
136         $item{frombrcd} = $frbranchcd;
137         $item{tobrcd}   = $tobranchcd;
138 ##########
139         #Are these lines still useful ???
140         $transfereditems{0} = $barcode;
141         $frbranchcds{0}     = $frbranchcd;
142         $tobranchcds{0}     = $tobranchcd;
143 ##########
144         push( @trsfitemloop, \%item );
145     }
146 }
147
148 foreach ( $query->param ) {
149     (next) unless (/bc-(\d*)/);
150     my $counter = $1;
151     my %item;
152     my $bc    = $query->param("bc-$counter");
153     my $frbcd = $query->param("fb-$counter");
154     my $tobcd = $query->param("tb-$counter");
155     $counter++;
156     $item{counter}  = $counter;
157     $item{barcode}  = $bc;
158     $item{frombrcd} = $frbcd;
159     $item{tobrcd}   = $tobcd;
160     my ($iteminformation) = GetBiblioFromItemNumer( 0, $bc );
161     $item{'biblionumber'} = $iteminformation->{'biblionumber'};
162     $item{'title'}        = $iteminformation->{'title'};
163     $item{'author'}       = $iteminformation->{'author'};
164     $item{'itemtype'}     = $iteminformation->{'itemtype'};
165     $item{'ccode'}        = $iteminformation->{'ccode'};
166     $item{'frbrname'}     = $branches->{$frbcd}->{'branchname'};
167     $item{'tobrname'}     = $branches->{$tobcd}->{'branchname'};
168 ##########
169     #Are these lines still useful ???
170     $transfereditems{$counter} = $bc;
171     $frbranchcds{$counter}     = $frbcd;
172     $tobranchcds{$counter}     = $tobcd;
173 #########
174     push( @trsfitemloop, \%item );
175 }
176
177 my $title;
178 my $surname;
179 my $firstname;
180 my $borphone;
181 my $borstraddress;
182 my $borcity;
183 my $borzip;
184 my $boremail;
185 my $borcnum;
186 my $itemnumber;
187 my $biblionum;
188 my $branchname;
189 my $wastransferred;
190
191 #####################
192
193 if ($found) {
194     my $res = $messages->{'ResFound'};
195     $branchname = $branches->{ $res->{'branchcode'} }->{'branchname'};
196     my ($borr) = GetMemberDetails( $res->{'borrowernumber'}, 0 );
197     $title          = $borr->{'title'};
198     $surname        = $borr->{'surname'};
199     $firstname      = $borr->{'firstname'};
200     $borrowernumber = $borr->{'borrowernumber'};
201     $borphone       = $borr->{'phone'};
202     $borstraddress  = $borr->{'streetaddress'};
203     $borcity        = $borr->{'city'};
204     $borzip         = $borr->{'zipcode'};
205     $boremail       = $borr->{'emailadress'};
206
207     #Hopefully, borr->{borrowernumber}=res->{borrowernumber}
208     $borcnum    = $borr->{'cardnumber'};
209     $itemnumber = $res->{'itemnumber'};
210
211     if ( $res->{'ResFound'} eq "Waiting" ) {
212         $waiting = 1;
213     }
214     if ( $res->{'ResFound'} eq "Reserved" ) {
215         $reserved  = 1;
216         $biblionum = $res->{'biblionumber'};
217     }
218 }
219
220 #####################
221
222 my @errmsgloop;
223 foreach my $code ( keys %$messages ) {
224     my %err;
225
226     if ( $code eq 'BadBarcode' ) {
227         $err{msg}        = $messages->{'BadBarcode'};
228         $err{errbadcode} = 1;
229         $allmessages     = 1;
230     }
231
232     if ( $code eq 'IsPermanent' ) {
233         $err{errispermanent} = 1;
234         $err{msg} = $branches->{ $messages->{'IsPermanent'} }->{'branchname'};
235
236         # Here, msg contains the branchname
237         # Not so satisfied with this... But should work
238         $allmessages = 1;
239     }
240     $err{errdesteqholding} = ( $code eq 'DestinationEqualsHolding' );
241
242     if ( $code eq 'WasReturned' ) {
243         $err{errwasreturned} = 1;
244         $allmessages = 1;
245         my ($borrowerinfo) =
246           GetMemberDetails( $messages->{'WasReturned'}, 0 );
247         $title          = $borrowerinfo->{'title'};
248         $surname        = $borrowerinfo->{'surname'};
249         $firstname      = $borrowerinfo->{'firstname'};
250         $borrowernumber = $borrowerinfo->{'borrowernumber'};
251         $borcnum        = $borrowerinfo->{'cardnumber'};
252     }
253
254     #    if ($code eq 'WasTransfered'){
255     # Put code here if you want to notify the user that item was transfered...
256     #        $wastransferred = 1;
257     #    }
258     push( @errmsgloop, \%err );
259 }
260
261 #######################################################################################
262 # Make the page .....
263 my ( $template, $cookie );
264 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
265     {
266         template_name   => "circ/branchtransfers.tmpl",
267         query           => $query,
268         type            => "intranet",
269         authnotrequired => 0,
270         flagsrequired   => { circulate => 1 },
271     }
272 );
273 if ($allmessages) {
274     $template->param( allmessages => 1 );
275 }
276
277 $template->param(
278     genbrname               => $genbrname,
279     genprname               => $genprname,
280     branch                  => $branch,
281     printer                 => $printer,
282     found                   => $found,
283     reserved                => $reserved,
284     waiting                 => $waiting,
285     title                   => $title,
286     surname                 => $surname,
287     firstname               => $firstname,
288     borphone                => $borphone,
289     borstraddress           => $borstraddress,
290     borcity                 => $borcity,
291     borzip                  => $borzip,
292     boremail                => $boremail,
293     borrowernumber          => $borrowernumber,
294     borcnum                 => $borcnum,
295     branchname              => $branchname,
296     itemnumber              => $itemnumber,
297     barcode                 => $barcode,
298     biblionumber            => $biblionum,
299     tobranchcd              => $tobranchcd,
300     reqmessage              => $reqmessage,
301     cancelled               => $cancelled,
302     setwaiting              => $setwaiting,
303     wastransferred          => $wastransferred,
304     trsfitemloop            => \@trsfitemloop,
305     branchoptionloop        => \@branchoptionloop,
306     errmsgloop              => \@errmsgloop,
307     intranetcolorstylesheet =>
308       C4::Context->preference("intranetcolorstylesheet"),
309     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
310     IntranetNav        => C4::Context->preference("IntranetNav"),
311 );
312 output_html_with_http_headers $query, $cookie, $template->output;
313
314 sub name {
315     my ($borinfo) = @_;
316     return $borinfo->{'surname'} . " "
317       . $borinfo->{'title'} . " "
318       . $borinfo->{'firstname'};
319 }
320
321 # Local Variables:
322 # tab-width: 4
323 # End: