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
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.
32 use C4::Bookseller qw( GetBookSellerFromId);
33 use C4::Dates qw/format_date/;
36 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
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.
57 the supplier this script have to display the basket.
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",
75 flagsrequired => { acquisition => 'order_manage' },
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');
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');
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");
117 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
118 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
119 my $contract = &GetContract($basket->{contractnumber});
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(
145 -attachment => 'basket' . $basket->{'basketno'} . '.csv',
147 print GetBasketAsCSV($query->param('basketno'));
149 } elsif ($op eq 'close') {
150 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
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,
161 ModBasket( { basketno => $basketno,
162 basketgroupid => $basketgroupid } );
163 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
165 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid=' . $booksellerid);
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') {
178 $basket->{basketno} = $query->param('basketno');
179 $basket->{closedate} = undef;
181 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
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");
197 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of 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}){
207 my %emptygroup = ( id => undef,
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} );
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 $gist = 0 if $gist == 0.0000;
231 my $discount = $bookseller->{'discount'} / 100;
232 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
233 my $total_rrp_gsti; # RRP Total, GST included
234 my $total_rrp_gste; # RRP Total, GST excluded
241 for my $order ( @results ) {
242 my $rrp = $order->{'listprice'} || 0;
243 my $qty = $order->{'quantity'} || 0;
244 if (!defined $order->{quantityreceived}) {
245 $order->{quantityreceived} = 0;
247 for ( qw(rrp ecost quantityreceived)) {
248 if (!defined $order->{$_}) {
253 my $budget = GetBudget( $order->{'budget_id'} );
254 $rrp = ConvertCurrency( $order->{'currency'}, $rrp );
256 $total_rrp += $qty * $order->{'rrp'};
257 my $line_total = $qty * $order->{'ecost'};
258 $total_rrp_est += $qty * $order->{'ecost'};
259 # FIXME: what about the "actual cost" field?
261 my %line = %{ $order };
263 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
264 $line{basketno} = $basketno;
265 $line{budget_name} = $budget->{budget_name};
266 $line{rrp} = sprintf( "%.2f", $line{'rrp'} );
267 $line{ecost} = sprintf( "%.2f", $line{'ecost'} );
268 $line{line_total} = sprintf( "%.2f", $line_total );
269 if ($line{uncertainprice}) {
270 $template->param( uncertainprices => 1 );
271 $line{rrp} .= ' (Uncertain)';
274 my $volume = $order->{'volume'};
275 my $seriestitle = $order->{'seriestitle'};
276 $line{'title'} .= " / $seriestitle" if $seriestitle;
277 $line{'title'} .= " / $volume" if $volume;
279 $line{'title'} = "Deleted bibliographic notice, can't find title.";
281 push @books_loop, \%line;
287 if ($gist){ # if we have GST
288 if ( $bookseller->{'listincgst'} ) { # if prices already includes GST
289 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
290 $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 ); # and can reverse compute other values
291 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
292 $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
293 $total_est_gsti = $total_rrp_est;
294 } else { # if prices does not include GST
295 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
296 $gist_rrp = $total_rrp_gste * $gist; #
297 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
298 $total_est_gste = $total_rrp_est;
299 $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
301 $gist_est = $gist_rrp - ( $gist_rrp * $discount );
303 $total_rrp_gsti = $total_rrp;
304 $total_est_gsti = $total_rrp_est;
307 my $contract = &GetContract($basket->{contractnumber});
308 my @orders = GetOrders($basketno);
310 my $borrower= GetMember('borrowernumber' => $loggedinuser);
311 my $budgets = GetBudgetHierarchy(q{},$borrower->{branchcode},$borrower->{borrowernumber});
313 foreach my $r (@{$budgets}) {
314 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
322 basketno => $basketno,
323 basketname => $basket->{'basketname'},
324 basketnote => $basket->{note},
325 basketbooksellernote => $basket->{booksellernote},
326 basketcontractno => $basket->{contractnumber},
327 basketcontractname => $contract->{contractname},
328 creationdate => C4::Dates->new($basket->{creationdate},'iso')->output,
329 authorisedby => $basket->{authorisedby},
330 authorisedbyname => $basket->{authorisedbyname},
331 closedate => C4::Dates->new($basket->{closedate},'iso')->output,
332 active => $bookseller->{'active'},
333 booksellerid => $bookseller->{'id'},
334 name => $bookseller->{'name'},
335 entrydate => C4::Dates->new($results[0]->{'entrydate'},'iso')->output,
336 books_loop => \@books_loop,
337 gist_rate => sprintf( "%.2f", $gist * 100 ) . '%',
338 total_rrp_gste => sprintf( "%.2f", $total_rrp_gste ),
339 total_est_gste => sprintf( "%.2f", $total_est_gste ),
340 gist_est => sprintf( "%.2f", $gist_est ),
341 gist_rrp => sprintf( "%.2f", $gist_rrp ),
342 total_rrp_gsti => sprintf( "%.2f", $total_rrp_gsti ),
343 total_est_gsti => sprintf( "%.2f", $total_est_gsti ),
344 # currency => $bookseller->{'listprice'},
345 currency => $cur->{'currency'},
346 qty_total => $qty_total,
348 basketgroups => $basketgroups,
349 grouped => $basket->{basketgroupid},
350 unclosable => @orders ? 0 : 1,
351 has_budgets => $has_budgets,
355 output_html_with_http_headers $query, $cookie, $template->output;