Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
[koha.git] / circ / branchtransfers.pl
1 #!/usr/bin/perl
2
3 #written 11/3/2002 by Finlay
4 #script to execute branch transfers of books
5
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::Circ2;
27 use C4::Output;
28 use C4::Reserves2;
29 use C4::Auth;
30 use HTML::Template;
31
32 ###############################################
33 # constants
34
35 my %env;
36 my $headerbackgroundcolor='#99cc33';
37 my $circbackgroundcolor='#ffffcc';
38 my $circbackgroundcolor='white';
39 my $linecolor1='#ffffcc';
40 my $linecolor2='white';
41 my $backgroundimage="/images/background-mem.gif";
42
43 my $branches = getbranches();
44 my $printers = getprinters(\%env);
45
46
47 ###############################################
48 #  Getting state
49
50 my $query=new CGI;
51 my ($loggedinuser, $sessioncookie, $sessionID) = checkauth($query);
52
53
54 my $branch = $query->param("branch");
55 my $printer = $query->param("printer");
56
57 ($branch) || ($branch=$query->cookie('branch')) ;
58 ($printer) || ($printer=$query->cookie('printer')) ;
59
60 ($branches->{$branch}) || ($branch=(keys %$branches)[0]);
61 ($printers->{$printer}) || ($printer=(keys %$printers)[0]);
62
63 my $genbrname = $branches->{$branch}->{'branchname'} ;
64 my $genprname = $printers->{$printer}->{'printername'};
65
66 my $messages;
67 my $found;
68 my $reserved;
69 my $waiting;
70 my $reqmessage;
71 my $cancelled;
72 my $setwaiting;
73 my $reqbrchname;
74
75 my $request=$query->param('request');
76 my $borrnum = $query->param('borrowernumber');
77
78 my $tobranchcd=$query->param('tobranchcd');
79 my $frbranchcd='';
80
81 ############
82 # Deal with the requests....
83 if ($request eq "KillWaiting") {
84     my $item = $query->param('itemnumber');
85     CancelReserve(0, $item, $borrnum);
86         $cancelled = 1;
87         $reqmessage =1;
88 }
89
90 my $ignoreRs = 0;
91 if ($request eq "SetWaiting") {
92     my $item = $query->param('itemnumber');
93     $tobranchcd = ReserveWaiting($item, $borrnum);
94         $reqbrchname = $branches->{$tobranchcd}->{'branchname'};
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, $borrnum);
102         $cancelled = 1;
103         $reqmessage =1;
104 }
105
106
107
108 # set up the branchselect options....
109 my @branchoptionloop;
110 foreach my $br (keys %$branches) {
111     (next) unless $branches->{$br}->{'CU'};
112     my %branch;
113     $branch{selected}=($br eq $tobranchcd);
114         $branch{code}=$br;
115         $branch{name}=$branches->{$br}->{'branchname'};
116         push (@branchoptionloop, \%branch);
117 }
118
119
120 # collect the stack of books already transfered so they can printed...
121 my @trsfitemloop;
122 my %transfereditems;
123 my %frbranchcds;
124 my %tobranchcds;
125 my $color=$linecolor2;
126
127 my $barcode = $query->param('barcode');
128 if ($barcode) {
129         my $transfered;
130         my $iteminformation;
131         ($transfered, $messages, $iteminformation)
132                         = transferbook($tobranchcd, $barcode, $ignoreRs);
133         $found = $messages->{'ResFound'};
134         if ($transfered) {
135                 my %item;
136                 my $frbranchcd = $iteminformation->{'holdingbranch'};
137                 if (not ($found)) {
138                         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
139                         $item{'color'}=$color;
140                         $item{'biblionumber'}=$iteminformation->{'biblionumber'};
141                         $item{'title'}=$iteminformation->{'title'};
142                         $item{'author'}=$iteminformation->{'author'};
143                         $item{'itemtype'}=$iteminformation->{'itemtype'};
144                         $item{'frbrname'}=$branches->{$frbranchcd}->{'branchname'};
145                         $item{'tobrname'}=$branches->{$tobranchcd}->{'branchname'};
146                 }
147                 $item{counter}=0;
148                 $item{barcode}=$barcode;
149                 $item{frombrcd}=$frbranchcd;
150                 $item{tobrcd}=$tobranchcd;
151 ##########
152 #Are these lines still useful ???
153                 $transfereditems{0}=$barcode;
154                 $frbranchcds{0}=$frbranchcd;
155                 $tobranchcds{0}=$tobranchcd;
156 ##########
157                 push (@trsfitemloop, \%item);
158         }
159 }
160
161 foreach ($query->param){
162         (next) unless (/bc-(\d*)/);
163         my $counter=$1;
164         my %item;
165         my $bc=$query->param("bc-$counter");
166         my $frbcd=$query->param("fb-$counter");
167         my $tobcd=$query->param("tb-$counter");
168         $counter++;
169         $item{counter}=$counter;
170         $item{barcode}=$bc;
171         $item{frombrcd}=$frbcd;
172         $item{tobrcd}=$tobcd;
173         my ($iteminformation) = getiteminformation(\%env, 0, $bc);
174         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
175         $item{'color'}=$color;
176         $item{'biblionumber'}=$iteminformation->{'biblionumber'};
177         $item{'title'}=$iteminformation->{'title'};
178         $item{'author'}=$iteminformation->{'author'};
179         $item{'itemtype'}=$iteminformation->{'itemtype'};
180         $item{'frbrname'}=$branches->{$frbcd}->{'branchname'};
181         $item{'tobrname'}=$branches->{$tobcd}->{'branchname'};
182 ##########
183 #Are these lines still useful ???
184         $transfereditems{$counter}=$bc;
185         $frbranchcds{$counter}=$frbcd;
186         $tobranchcds{$counter}=$tobcd;
187 #########
188         push (@trsfitemloop, \%item);
189 }
190
191
192 my $name;
193 my $bornum;
194 my $borcnum;
195 my $itemnumber;
196 my $biblionum;
197 my $branchname;
198
199
200 #####################
201
202 if ($found) {
203     my $res = $messages->{'ResFound'};
204         $branchname = $branches->{$res->{'branchcode'}}->{'branchname'};
205         my ($borr) = getpatroninformation(\%env, $res->{'borrowernumber'}, 0);
206         $name = name($borr);
207         $bornum = $borr->{'borrowernumber'}; #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     $err{errbadcode} = ($code eq 'BadBarcode');
226         if ($code eq 'BadBarcode') {
227                 $err{msg}=$messages->{'BadBarcode'};
228         }
229
230     $err{errispermanent} = ($code eq 'IsPermanent');
231     if ($code eq 'IsPermanent'){
232                 $err{msg} = $branches->{$messages->{'IsPermanent'}}->{'branchname'};
233                 # Here, msg contains the branchname
234                 # Not so satisfied with this... But should work
235     }
236     $err{errdesteqholding} = ($code eq 'DestinationEqualsHolding');
237
238         $err{errwasreturned} = ($code eq 'WasReturned');
239         if ($code eq 'WasReturned') {
240                 my ($borrowerinfo) = getpatroninformation(\%env, $messages->{'WasReturned'}, 0);
241                 $name =name($borrowerinfo);
242                 $bornum =$borrowerinfo->{'borrowernumber'};
243                 $borcnum =$borrowerinfo->{'cardnumber'};
244     }
245     if ($code eq 'WasTransfered'){
246 # Put code here if you want to notify the user that item was transfered...
247     }
248         push (@errmsgloop, \%err);
249 }
250
251
252 #######################################################################################
253 # Make the page .....
254 my ($template, $borrowernumber, $cookie)
255     = get_template_and_user({template_name => "circ/branchtransfers.tmpl",
256                                                         query => $query,
257                             type => "intranet",
258                             authnotrequired => 0,
259                             flagsrequired => {parameters => 1},
260                          });
261 $template->param(       genbrname => $genbrname,
262                                                                 genprname => $genprname,
263                                                                 branch => $branch,
264                                                                 printer => $printer,
265                                                                 found => $found,
266                                                                 hdrbckgdcolor => $headerbackgroundcolor,
267                                                                 bckgdimg => $backgroundimage,
268                                                                 reserved => $reserved,
269                                                                 waiting => $waiting,
270                                                                 name => $name,
271                                                                 bornum => $bornum,
272                                                                 borcnum => $borcnum,
273                                                                 branchname => $branchname,
274                                                                 itemnumber => $itemnumber,
275                                                                 barcode => $barcode,
276                                                                 biblionumber => $biblionum,
277                                                                 tobranchcd => $tobranchcd,
278                                                                 reqmessage => $reqmessage,
279                                                                 cancelled => $cancelled,
280                                                                 setwaiting => $setwaiting,
281                                                                 trsfitemloop => \@trsfitemloop,
282                                                                 branchoptionloop => \@branchoptionloop,
283                                                                 errmsgloop => \@errmsgloop
284                                                         );
285 output_html_with_http_headers $query, $sessioncookie, $template->output;
286
287
288 sub name {
289         my ($borinfo) = @_;
290         return $borinfo->{'surname'}." ".$borinfo->{'title'}." ".$borinfo->{'firstname'};
291 }