Bug 6958 : Fix for leading spaces being stripped on translation
[koha.git] / acqui / basket.pl
blob70fb39cf7d6eedaac47ef6b35c172144ae4e847e
1 #!/usr/bin/perl
3 #script to show display basket of orders
5 # Copyright 2000 - 2004 Katipo
6 # Copyright 2008 - 2009 BibLibre SARL
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use strict;
24 use warnings;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
28 use CGI;
29 use C4::Acquisition;
30 use C4::Budgets;
32 use C4::Bookseller qw( GetBookSellerFromId);
33 use C4::Dates qw/format_date/;
34 use C4::Debug;
36 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
37 =head1 NAME
39 basket.pl
41 =head1 DESCRIPTION
43 This script display all informations about basket for the supplier given
44 on input arg. Moreover, it allows us to add a new order for this supplier from
45 an existing record, a suggestion or a new record.
47 =head1 CGI PARAMETERS
49 =over 4
51 =item $basketno
53 The basket number.
55 =item supplierid
57 the supplier this script have to display the basket.
59 =item order
61 =back
63 =cut
65 my $query = new CGI;
66 my $basketno = $query->param('basketno');
67 my $booksellerid = $query->param('supplierid');
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
71 template_name => "acqui/basket.tmpl",
72 query => $query,
73 type => "intranet",
74 authnotrequired => 0,
75 flagsrequired => { acquisition => 'order_manage' },
76 debug => 1,
80 my $basket = GetBasket($basketno);
82 # FIXME : what about the "discount" percentage?
83 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
84 # if no booksellerid in parameter, get it from basket
85 # warn "=>".$basket->{booksellerid};
86 $booksellerid = $basket->{booksellerid} unless $booksellerid;
87 my ($bookseller) = GetBookSellerFromId($booksellerid);
88 my $op = $query->param('op');
89 if (!defined $op) {
90 $op = q{};
93 my $confirm_pref= C4::Context->preference("BasketConfirmations") || '1';
94 $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
96 if ( $op eq 'delete_confirm' ) {
97 my $basketno = $query->param('basketno');
98 DelBasket($basketno);
99 $template->param( delete_confirmed => 1 );
100 } elsif ( !$bookseller ) {
101 $template->param( NO_BOOKSELLER => 1 );
102 } elsif ( $op eq 'del_basket') {
103 $template->param( delete_confirm => 1 );
104 if ( C4::Context->preference("IndependantBranches") ) {
105 my $userenv = C4::Context->userenv;
106 unless ( $userenv->{flags} == 1 ) {
107 my $validtest = ( $basket->{creationdate} eq '' )
108 || ( $userenv->{branch} eq $basket->{branch} )
109 || ( $userenv->{branch} eq '' )
110 || ( $basket->{branch} eq '' );
111 unless ($validtest) {
112 print $query->redirect("../mainpage.pl");
113 exit 1;
117 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
118 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
119 my $contract = &GetContract($basket->{contractnumber});
120 $template->param(
121 basketno => $basketno,
122 basketname => $basket->{'basketname'},
123 basketnote => $basket->{note},
124 basketbooksellernote => $basket->{booksellernote},
125 basketcontractno => $basket->{contractnumber},
126 basketcontractname => $contract->{contractname},
127 creationdate => format_date( $basket->{creationdate} ),
128 authorisedby => $basket->{authorisedby},
129 authorisedbyname => $basket->{authorisedbyname},
130 closedate => format_date( $basket->{closedate} ),
131 active => $bookseller->{'active'},
132 booksellerid => $bookseller->{'id'},
133 name => $bookseller->{'name'},
134 address1 => $bookseller->{'address1'},
135 address2 => $bookseller->{'address2'},
136 address3 => $bookseller->{'address3'},
137 address4 => $bookseller->{'address4'},
139 } elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
140 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
141 # check if we have to "close" a basket before building page
142 } elsif ($op eq 'export') {
143 print $query->header(
144 -type => 'text/csv',
145 -attachment => 'basket' . $basket->{'basketno'} . '.csv',
147 print GetBasketAsCSV($query->param('basketno'));
148 exit;
149 } elsif ($op eq 'close') {
150 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
151 if ($confirm) {
152 my $basketno = $query->param('basketno');
153 my $booksellerid = $query->param('booksellerid');
154 $basketno =~ /^\d+$/ and CloseBasket($basketno);
155 # if requested, create basket group, close it and attach the basket
156 if ($query->param('createbasketgroup')) {
157 my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
158 booksellerid => $booksellerid,
159 closed => 1,
161 ModBasket( { basketno => $basketno,
162 basketgroupid => $basketgroupid } );
163 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
164 } else {
165 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid=' . $booksellerid);
167 exit;
168 } else {
169 $template->param(confirm_close => "1",
170 booksellerid => $booksellerid,
171 basketno => $basket->{'basketno'},
172 basketname => $basket->{'basketname'},
173 basketgroupname => $basket->{'basketname'});
176 } elsif ($op eq 'reopen') {
177 my $basket;
178 $basket->{basketno} = $query->param('basketno');
179 $basket->{closedate} = undef;
180 ModBasket($basket);
181 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
182 } else {
183 # get librarian branch...
184 if ( C4::Context->preference("IndependantBranches") ) {
185 my $userenv = C4::Context->userenv;
186 unless ( $userenv->{flags} == 1 ) {
187 my $validtest = ( $basket->{creationdate} eq '' )
188 || ( $userenv->{branch} eq $basket->{branch} )
189 || ( $userenv->{branch} eq '' )
190 || ( $basket->{branch} eq '' );
191 unless ($validtest) {
192 print $query->redirect("../mainpage.pl");
193 exit 1;
197 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
198 my $basketgroups;
199 my $member = GetMember(borrowernumber => $loggedinuser);
200 if ($basket->{closedate} && haspermission({ acquisition => 'group_manage'} )) {
201 $basketgroups = GetBasketgroups($basket->{booksellerid});
202 for my $bg ( @{$basketgroups} ) {
203 if ($basket->{basketgroupid} && $basket->{basketgroupid} == $bg->{id}){
204 $bg->{default} = 1;
207 my %emptygroup = ( id => undef,
208 name => "No group");
209 if ( ! $basket->{basketgroupid} ) {
210 $emptygroup{default} = 1;
211 $emptygroup{nogroup} = 1;
213 unshift( @$basketgroups, \%emptygroup );
215 # if new basket, pre-fill infos
216 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
217 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
218 $debug
219 and warn sprintf
220 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
221 $basket->{creationdate}, $basket->{authorisedby};
223 #to get active currency
224 my $cur = GetCurrency();
227 my @results = GetOrders( $basketno );
229 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
230 my $discount = $bookseller->{'discount'} / 100;
231 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
232 my $total_rrp_gsti; # RRP Total, GST included
233 my $total_rrp_gste; # RRP Total, GST excluded
234 my $gist_rrp;
236 my $qty_total;
237 my @books_loop;
239 for my $order ( @results ) {
240 my $rrp = $order->{'listprice'} || 0;
241 my $qty = $order->{'quantity'} || 0;
242 if (!defined $order->{quantityreceived}) {
243 $order->{quantityreceived} = 0;
245 for ( qw(rrp ecost quantityreceived)) {
246 if (!defined $order->{$_}) {
247 $order->{$_} = 0;
251 my $budget = GetBudget( $order->{'budget_id'} );
252 $rrp = ConvertCurrency( $order->{'currency'}, $rrp );
254 $total_rrp += $qty * $order->{'rrp'};
255 my $line_total = $qty * $order->{'ecost'};
256 # FIXME: what about the "actual cost" field?
257 $qty_total += $qty;
258 my %line = %{ $order };
260 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
261 $line{basketno} = $basketno;
262 $line{budget_name} = $budget->{budget_name};
263 $line{rrp} = sprintf( "%.2f", $line{'rrp'} );
264 $line{ecost} = sprintf( "%.2f", $line{'ecost'} );
265 $line{line_total} = sprintf( "%.2f", $line_total );
266 if ($line{uncertainprice}) {
267 $template->param( uncertainprices => 1 );
268 $line{rrp} .= ' (Uncertain)';
270 if ($line{'title'}){
271 my $volume = $order->{'volume'};
272 my $seriestitle = $order->{'seriestitle'};
273 $line{'title'} .= " / $seriestitle" if $seriestitle;
274 $line{'title'} .= " / $volume" if $volume;
275 } else {
276 $line{'title'} = "Deleted bibliographic notice, can't find title.";
278 push @books_loop, \%line;
281 if ($bookseller->{'listincgst'}) { # if prices already includes GST
282 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
283 $total_rrp_gste = $total_rrp_gsti / ($gist + 1); # and can reverse compute other values
284 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
285 } else { # if prices does not include GST
286 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
287 $gist_rrp = $total_rrp_gste * $gist; #
288 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
290 # These vars are estimated totals and GST, taking in account the booksellet discount
291 my $total_est_gsti = $total_rrp_gsti - ($total_rrp_gsti * $discount);
292 my $gist_est = $gist_rrp - ($gist_rrp * $discount);
293 my $total_est_gste = $total_rrp_gste - ($total_rrp_gste * $discount);
295 my $contract = &GetContract($basket->{contractnumber});
296 my @orders = GetOrders($basketno);
298 my $borrower= GetMember('borrowernumber' => $loggedinuser);
299 my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
300 my $has_budgets = 0;
301 foreach my $r (@{$budgets}) {
302 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
303 next;
305 $has_budgets = 1;
306 last;
309 $template->param(
310 basketno => $basketno,
311 basketname => $basket->{'basketname'},
312 basketnote => $basket->{note},
313 basketbooksellernote => $basket->{booksellernote},
314 basketcontractno => $basket->{contractnumber},
315 basketcontractname => $contract->{contractname},
316 creationdate => C4::Dates->new($basket->{creationdate},'iso')->output,
317 authorisedby => $basket->{authorisedby},
318 authorisedbyname => $basket->{authorisedbyname},
319 closedate => C4::Dates->new($basket->{closedate},'iso')->output,
320 active => $bookseller->{'active'},
321 booksellerid => $bookseller->{'id'},
322 name => $bookseller->{'name'},
323 entrydate => C4::Dates->new($results[0]->{'entrydate'},'iso')->output,
324 books_loop => \@books_loop,
325 gist_rate => sprintf( "%.2f", $gist * 100 ) . '%',
326 total_rrp_gste => sprintf( "%.2f", $total_rrp_gste ),
327 total_est_gste => sprintf( "%.2f", $total_est_gste ),
328 gist_est => sprintf( "%.2f", $gist_est ),
329 gist_rrp => sprintf( "%.2f", $gist_rrp ),
330 total_rrp_gsti => sprintf( "%.2f", $total_rrp_gsti ),
331 total_est_gsti => sprintf( "%.2f", $total_est_gsti ),
332 # currency => $bookseller->{'listprice'},
333 currency => $cur->{'currency'},
334 qty_total => $qty_total,
335 GST => $gist,
336 basketgroups => $basketgroups,
337 grouped => $basket->{basketgroupid},
338 unclosable => @orders ? 0 : 1,
339 has_budgets => $has_budgets,
343 output_html_with_http_headers $query, $cookie, $template->output;