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