Fixed bug 202, the branch menu-building code has spurious checks for CU and
[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
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use CGI;
27 use C4::Circulation::Circ2;
28 use C4::Output;
29 use C4::Reserves2;
30 use C4::Auth;
31 use C4::Interface::CGI::Output;
32 use HTML::Template;
33
34 ###############################################
35 # constants
36
37 my %env;
38 my $headerbackgroundcolor='#99cc33';
39 my $circbackgroundcolor='#ffffcc';
40 my $circbackgroundcolor='white';
41 my $linecolor1='#ffffcc';
42 my $linecolor2='white';
43 my $backgroundimage="/images/background-mem.gif";
44
45 my $branches = getbranches();
46 my $printers = getprinters(\%env);
47
48
49 ###############################################
50 #  Getting state
51
52 my $query=new CGI;
53
54
55 my $branch = getbranch($query, $branches);
56 my $printer = getprinter($query, $printers);
57
58 my $genbrname = $branches->{$branch}->{'branchname'} ;
59 my $genprname = $printers->{$printer}->{'printername'};
60
61 my $messages;
62 my $found;
63 my $reserved;
64 my $waiting;
65 my $reqmessage;
66 my $cancelled;
67 my $setwaiting;
68 my $reqbrchname;
69
70 my $request=$query->param('request');
71 my $borrnum = $query->param('borrowernumber');
72
73 my $tobranchcd=$query->param('tobranchcd');
74 my $frbranchcd='';
75
76 ############
77 # Deal with the requests....
78 if ($request eq "KillWaiting") {
79     my $item = $query->param('itemnumber');
80     CancelReserve(0, $item, $borrnum);
81         $cancelled = 1;
82         $reqmessage =1;
83 }
84
85 my $ignoreRs = 0;
86 if ($request eq "SetWaiting") {
87     my $item = $query->param('itemnumber');
88     $tobranchcd = ReserveWaiting($item, $borrnum);
89         $reqbrchname = $branches->{$tobranchcd}->{'branchname'};
90     $ignoreRs = 1;
91         $setwaiting = 1;
92         $reqmessage =1;
93 }
94 if ($request eq 'KillReserved'){
95     my $biblio = $query->param('biblionumber');
96     CancelReserve($biblio, 0, $borrnum);
97         $cancelled = 1;
98         $reqmessage =1;
99 }
100
101
102
103 # set up the branchselect options....
104 my @branchoptionloop;
105 foreach my $br (keys %$branches) {
106     #(next) unless $branches->{$br}->{'CU'}; #FIXME disabled to fix bug 202
107     my %branch;
108     $branch{selected}=($br eq $tobranchcd);
109         $branch{code}=$br;
110         $branch{name}=$branches->{$br}->{'branchname'};
111         push (@branchoptionloop, \%branch);
112 }
113
114
115 # collect the stack of books already transfered so they can printed...
116 my @trsfitemloop;
117 my %transfereditems;
118 my %frbranchcds;
119 my %tobranchcds;
120 my $color=$linecolor2;
121
122 my $barcode = $query->param('barcode');
123 if ($barcode) {
124         my $transfered;
125         my $iteminformation;
126         ($transfered, $messages, $iteminformation)
127                         = transferbook($tobranchcd, $barcode, $ignoreRs);
128         $found = $messages->{'ResFound'};
129         if ($transfered) {
130                 my %item;
131                 my $frbranchcd = $iteminformation->{'holdingbranch'};
132                 if (not ($found)) {
133                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
134                         $item{'color'}=$color;
135                         $item{'biblionumber'}=$iteminformation->{'biblionumber'};
136                         $item{'title'}=$iteminformation->{'title'};
137                         $item{'author'}=$iteminformation->{'author'};
138                         $item{'itemtype'}=$iteminformation->{'itemtype'};
139                         $item{'frbrname'}=$branches->{$frbranchcd}->{'branchname'};
140                         $item{'tobrname'}=$branches->{$tobranchcd}->{'branchname'};
141                 }
142                 $item{counter}=0;
143                 $item{barcode}=$barcode;
144                 $item{frombrcd}=$frbranchcd;
145                 $item{tobrcd}=$tobranchcd;
146 ##########
147 #Are these lines still useful ???
148                 $transfereditems{0}=$barcode;
149                 $frbranchcds{0}=$frbranchcd;
150                 $tobranchcds{0}=$tobranchcd;
151 ##########
152                 push (@trsfitemloop, \%item);
153         }
154 }
155
156 foreach ($query->param){
157         (next) unless (/bc-(\d*)/);
158         my $counter=$1;
159         my %item;
160         my $bc=$query->param("bc-$counter");
161         my $frbcd=$query->param("fb-$counter");
162         my $tobcd=$query->param("tb-$counter");
163         $counter++;
164         $item{counter}=$counter;
165         $item{barcode}=$bc;
166         $item{frombrcd}=$frbcd;
167         $item{tobrcd}=$tobcd;
168         my ($iteminformation) = getiteminformation(\%env, 0, $bc);
169         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
170         $item{'color'}=$color;
171         $item{'biblionumber'}=$iteminformation->{'biblionumber'};
172         $item{'title'}=$iteminformation->{'title'};
173         $item{'author'}=$iteminformation->{'author'};
174         $item{'itemtype'}=$iteminformation->{'itemtype'};
175         $item{'frbrname'}=$branches->{$frbcd}->{'branchname'};
176         $item{'tobrname'}=$branches->{$tobcd}->{'branchname'};
177 ##########
178 #Are these lines still useful ???
179         $transfereditems{$counter}=$bc;
180         $frbranchcds{$counter}=$frbcd;
181         $tobranchcds{$counter}=$tobcd;
182 #########
183         push (@trsfitemloop, \%item);
184 }
185
186
187 my $name;
188 my $bornum;
189 my $borcnum;
190 my $itemnumber;
191 my $biblionum;
192 my $branchname;
193
194
195 #####################
196
197 if ($found) {
198     my $res = $messages->{'ResFound'};
199         $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
200         my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
201         $name = name($borr);
202         $bornum = $borr->{'borrowernumber'}; #Hopefully, borr->{borrowernumber}=res->{borrowernumber}
203         $borcnum = $borr->{'cardnumber'};
204         $itemnumber = $res->{'itemnumber'};
205
206         if ($res->{'ResFound'} eq "Waiting") {
207                 $waiting = 1;
208         }
209         if ($res->{'ResFound'} eq "Reserved") {
210                 $reserved = 1;
211                 $biblionum = $res->{'biblionumber'};
212         }
213 }
214
215 #####################
216
217 my @errmsgloop;
218 foreach my $code (keys %$messages) {
219         my %err;
220     $err{errbadcode} = ($code eq 'BadBarcode');
221         if ($code eq 'BadBarcode') {
222                 $err{msg}=$messages->{'BadBarcode'};
223         }
224
225     $err{errispermanent} = ($code eq 'IsPermanent');
226     if ($code eq 'IsPermanent'){
227                 $err{msg} = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
228                 # Here, msg contains the branchname
229                 # Not so satisfied with this... But should work
230     }
231     $err{errdesteqholding} = ($code eq 'DestinationEqualsHolding');
232
233         $err{errwasreturned} = ($code eq 'WasReturned');
234         if ($code eq 'WasReturned') {
235                 my ($borrowerinfo) = getpatroninformation(\%env, $messages->{'WasReturned'}, 0);
236                 $name =name($borrowerinfo);
237                 $bornum =$borrowerinfo->{'borrowernumber'};
238                 $borcnum =$borrowerinfo->{'cardnumber'};
239     }
240     if ($code eq 'WasTransfered'){
241 # Put code here if you want to notify the user that item was transfered...
242     }
243         push (@errmsgloop, \%err);
244 }
245
246
247 #######################################################################################
248 # Make the page .....
249 my ($template, $borrowernumber, $cookie)
250     = get_template_and_user({template_name => "circ/branchtransfers.tmpl",
251                                                         query => $query,
252                             type => "intranet",
253                             authnotrequired => 0,
254                             flagsrequired => {parameters => 1},
255                          });
256 $template->param(       genbrname => $genbrname,
257                                                                 genprname => $genprname,
258                                                                 branch => $branch,
259                                                                 printer => $printer,
260                                                                 found => $found,
261                                                                 hdrbckgdcolor => $headerbackgroundcolor,
262                                                                 bckgdimg => $backgroundimage,
263                                                                 reserved => $reserved,
264                                                                 waiting => $waiting,
265                                                                 name => $name,
266                                                                 bornum => $bornum,
267                                                                 borcnum => $borcnum,
268                                                                 branchname => $branchname,
269                                                                 itemnumber => $itemnumber,
270                                                                 barcode => $barcode,
271                                                                 biblionumber => $biblionum,
272                                                                 tobranchcd => $tobranchcd,
273                                                                 reqmessage => $reqmessage,
274                                                                 cancelled => $cancelled,
275                                                                 setwaiting => $setwaiting,
276                                                                 trsfitemloop => \@trsfitemloop,
277                                                                 branchoptionloop => \@branchoptionloop,
278                                                                 errmsgloop => \@errmsgloop
279                                                         );
280 output_html_with_http_headers $query, $cookie, $template->output;
281
282
283 sub name {
284         my ($borinfo) = @_;
285         return $borinfo->{'surname'}." ".$borinfo->{'title'}." ".$borinfo->{'firstname'};
286 }
287
288 # Local Variables:
289 # tab-width: 4
290 # End: