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.
31 use C4::Bookseller qw( GetBookSellerFromId);
34 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
37 use Date::Calc qw/Add_Delta_Days/;
45 This script display all informations about basket for the supplier given
46 on input arg. Moreover, it allows us to add a new order for this supplier from
47 an existing record, a suggestion or a new record.
59 the supplier this script have to display the basket.
68 my $basketno = $query->param('basketno');
69 my $booksellerid = $query->param('booksellerid');
71 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
73 template_name => "acqui/basket.tmpl",
77 flagsrequired => { acquisition => 'order_manage' },
82 my $basket = GetBasket($basketno);
84 # FIXME : what about the "discount" percentage?
85 # FIXME : the query->param('booksellerid') below is probably useless. The bookseller is always known from the basket
86 # if no booksellerid in parameter, get it from basket
87 # warn "=>".$basket->{booksellerid};
88 $booksellerid = $basket->{booksellerid} unless $booksellerid;
89 my ($bookseller) = GetBookSellerFromId($booksellerid);
90 my $op = $query->param('op');
95 my $confirm_pref= C4::Context->preference("BasketConfirmations") || '1';
96 $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
98 if ( $op eq 'delete_confirm' ) {
99 my $basketno = $query->param('basketno');
100 DelBasket($basketno);
101 $template->param( delete_confirmed => 1 );
102 } elsif ( !$bookseller ) {
103 $template->param( NO_BOOKSELLER => 1 );
104 } elsif ( $op eq 'del_basket') {
105 $template->param( delete_confirm => 1 );
106 if ( C4::Context->preference("IndependantBranches") ) {
107 my $userenv = C4::Context->userenv;
108 unless ( $userenv->{flags} == 1 ) {
109 my $validtest = ( $basket->{creationdate} eq '' )
110 || ( $userenv->{branch} eq $basket->{branch} )
111 || ( $userenv->{branch} eq '' )
112 || ( $basket->{branch} eq '' );
113 unless ($validtest) {
114 print $query->redirect("../mainpage.pl");
119 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
120 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
121 my $contract = &GetContract($basket->{contractnumber});
123 basketno => $basketno,
124 basketname => $basket->{'basketname'},
125 basketnote => $basket->{note},
126 basketbooksellernote => $basket->{booksellernote},
127 basketcontractno => $basket->{contractnumber},
128 basketcontractname => $contract->{contractname},
129 creationdate => $basket->{creationdate},
130 authorisedby => $basket->{authorisedby},
131 authorisedbyname => $basket->{authorisedbyname},
132 closedate => $basket->{closedate},
133 active => $bookseller->{'active'},
134 booksellerid => $bookseller->{'id'},
135 name => $bookseller->{'name'},
136 address1 => $bookseller->{'address1'},
137 address2 => $bookseller->{'address2'},
138 address3 => $bookseller->{'address3'},
139 address4 => $bookseller->{'address4'},
141 } elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
142 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
143 # check if we have to "close" a basket before building page
144 } elsif ($op eq 'export') {
145 print $query->header(
147 -attachment => 'basket' . $basket->{'basketno'} . '.csv',
149 print GetBasketAsCSV($query->param('basketno'), $query);
151 } elsif ($op eq 'close') {
152 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
154 my $basketno = $query->param('basketno');
155 my $booksellerid = $query->param('booksellerid');
156 $basketno =~ /^\d+$/ and CloseBasket($basketno);
157 # if requested, create basket group, close it and attach the basket
158 if ($query->param('createbasketgroup')) {
160 if(C4::Context->userenv and C4::Context->userenv->{'branch'}
161 and C4::Context->userenv->{'branch'} ne "NO_LIBRARY_SET") {
162 $branchcode = C4::Context->userenv->{'branch'};
164 my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
165 booksellerid => $booksellerid,
166 deliveryplace => $branchcode,
167 billingplace => $branchcode,
170 ModBasket( { basketno => $basketno,
171 basketgroupid => $basketgroupid } );
172 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
174 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?booksellerid=' . $booksellerid);
178 $template->param(confirm_close => "1",
179 booksellerid => $booksellerid,
180 basketno => $basket->{'basketno'},
181 basketname => $basket->{'basketname'},
182 basketgroupname => $basket->{'basketname'});
185 } elsif ($op eq 'reopen') {
187 $basket->{basketno} = $query->param('basketno');
188 $basket->{closedate} = undef;
190 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
192 # get librarian branch...
193 if ( C4::Context->preference("IndependantBranches") ) {
194 my $userenv = C4::Context->userenv;
195 unless ( $userenv->{flags} == 1 ) {
196 my $validtest = ( $basket->{creationdate} eq '' )
197 || ( $userenv->{branch} eq $basket->{branch} )
198 || ( $userenv->{branch} eq '' )
199 || ( $basket->{branch} eq '' );
200 unless ($validtest) {
201 print $query->redirect("../mainpage.pl");
206 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
208 my $member = GetMember(borrowernumber => $loggedinuser);
209 if ($basket->{closedate} && haspermission({ acquisition => 'group_manage'} )) {
210 $basketgroups = GetBasketgroups($basket->{booksellerid});
211 for my $bg ( @{$basketgroups} ) {
212 if ($basket->{basketgroupid} && $basket->{basketgroupid} == $bg->{id}){
216 my %emptygroup = ( id => undef,
218 if ( ! $basket->{basketgroupid} ) {
219 $emptygroup{default} = 1;
220 $emptygroup{nogroup} = 1;
222 unshift( @$basketgroups, \%emptygroup );
225 # if the basket is closed, calculate estimated delivery date
226 my $estimateddeliverydate;
227 if( $basket->{closedate} ) {
228 my ($year, $month, $day) = ($basket->{closedate} =~ /(\d+)-(\d+)-(\d+)/);
229 ($year, $month, $day) = Add_Delta_Days($year, $month, $day, $bookseller->{deliverytime});
230 $estimateddeliverydate = "$year-$month-$day";
233 # if new basket, pre-fill infos
234 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
235 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
238 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
239 $basket->{creationdate}, $basket->{authorisedby};
241 #to get active currency
242 my $cur = GetCurrency();
245 my @results = GetOrders( $basketno );
250 my $total_quantity = 0;
253 my $total_gstvalue = 0;
254 for my $order (@results) {
255 my $line = get_order_infos( $order, $bookseller);
256 if ( $line->{uncertainprice} ) {
257 $template->param( uncertainprices => 1 );
260 push @books_loop, $line;
262 $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
263 $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
264 $total_gstvalue += $$line{gstvalue};
265 $foot{$$line{gstgsti}}{quantity} += $$line{quantity};
266 $total_quantity += $$line{quantity};
267 $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
268 $total_gste += $$line{totalgste};
269 $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
270 $total_gsti += $$line{totalgsti};
273 push @book_foot_loop, map {$_} values %foot;
275 # Get cancelled orders
276 @results = GetCancelledOrders($basketno);
277 my @cancelledorders_loop;
278 for my $order (@results) {
279 my $line = get_order_infos( $order, $bookseller);
280 push @cancelledorders_loop, $line;
283 my $contract = &GetContract($basket->{contractnumber});
284 my @orders = GetOrders($basketno);
286 my $borrower= GetMember('borrowernumber' => $loggedinuser);
287 my $budgets = GetBudgetHierarchy;
289 foreach my $r (@{$budgets}) {
290 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
293 next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
299 my @cancelledorders = GetCancelledOrders($basketno);
300 foreach (@cancelledorders) {
301 $_->{'line_total'} = sprintf("%.2f", $_->{'ecost'} * $_->{'quantity'});
305 basketno => $basketno,
306 basketname => $basket->{'basketname'},
307 basketnote => $basket->{note},
308 basketbooksellernote => $basket->{booksellernote},
309 basketcontractno => $basket->{contractnumber},
310 basketcontractname => $contract->{contractname},
311 creationdate => $basket->{creationdate},
312 authorisedby => $basket->{authorisedby},
313 authorisedbyname => $basket->{authorisedbyname},
314 closedate => $basket->{closedate},
315 estimateddeliverydate=> $estimateddeliverydate,
316 active => $bookseller->{'active'},
317 booksellerid => $bookseller->{'id'},
318 name => $bookseller->{'name'},
319 books_loop => \@books_loop,
320 book_foot_loop => \@book_foot_loop,
321 cancelledorders_loop => \@cancelledorders,
322 total_quantity => $total_quantity,
323 total_gste => sprintf( "%.2f", $total_gste ),
324 total_gsti => sprintf( "%.2f", $total_gsti ),
325 total_gstvalue => sprintf( "%.2f", $total_gstvalue ),
326 currency => $cur->{'currency'},
327 listincgst => $bookseller->{listincgst},
328 basketgroups => $basketgroups,
329 grouped => $basket->{basketgroupid},
330 unclosable => @orders ? 0 : 1,
331 has_budgets => $has_budgets,
335 sub get_order_infos {
337 my $bookseller = shift;
338 my $qty = $order->{'quantity'} || 0;
339 if ( !defined $order->{quantityreceived} ) {
340 $order->{quantityreceived} = 0;
342 my $budget = GetBudget( $order->{'budget_id'} );
344 my %line = %{ $order };
345 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
346 $line{basketno} = $basketno;
347 $line{budget_name} = $budget->{budget_name};
348 $line{rrp} = ConvertCurrency( $order->{'currency'}, $line{rrp} ); # FIXME from comm
349 if ( $bookseller->{'listincgst'} ) {
350 $line{rrpgsti} = sprintf( "%.2f", $line{rrp} );
351 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
352 $line{rrpgste} = sprintf( "%.2f", $line{rrp} / ( 1 + ( $line{gstgsti} / 100 ) ) );
353 $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
354 $line{ecostgsti} = sprintf( "%.2f", $line{ecost} );
355 $line{ecostgste} = sprintf( "%.2f", $line{ecost} / ( 1 + ( $line{gstgsti} / 100 ) ) );
356 $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
357 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
358 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
360 $line{rrpgsti} = sprintf( "%.2f", $line{rrp} * ( 1 + ( $line{gstrate} ) ) );
361 $line{rrpgste} = sprintf( "%.2f", $line{rrp} );
362 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
363 $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
364 $line{ecostgsti} = sprintf( "%.2f", $line{ecost} * ( 1 + ( $line{gstrate} ) ) );
365 $line{ecostgste} = sprintf( "%.2f", $line{ecost} );
366 $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
367 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
368 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
371 if ( $line{uncertainprice} ) {
372 $line{rrpgste} .= ' (Uncertain)';
374 if ( $line{'title'} ) {
375 my $volume = $order->{'volume'};
376 my $seriestitle = $order->{'seriestitle'};
377 $line{'title'} .= " / $seriestitle" if $seriestitle;
378 $line{'title'} .= " / $volume" if $volume;
380 $line{'title'} = "Deleted bibliographic notice, can't find title.";
383 my $biblionumber = $order->{'biblionumber'};
384 my $countbiblio = CountBiblioInOrders($biblionumber);
385 my $ordernumber = $order->{'ordernumber'};
386 my @subscriptions = GetSubscriptionsId ($biblionumber);
387 my $itemcount = GetItemsCount($biblionumber);
388 my $holds = GetHolds ($biblionumber);
389 my @items = GetItemnumbersFromOrder( $ordernumber );
391 foreach my $item (@items){
392 my $nb = GetItemHolds($biblionumber, $item);
397 # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
398 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
399 $line{items} = ($itemcount) - (scalar @items);
400 $line{left_item} = 1 if $line{items} >= 1;
401 $line{left_biblio} = 1 if $countbiblio > 1;
402 $line{biblios} = $countbiblio - 1;
403 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
404 $line{subscriptions} = scalar @subscriptions;
405 ($holds >= 1) ? $line{left_holds} = 1 : $line{left_holds} = 0;
406 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
407 $line{holds} = $holds;
408 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
411 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
412 $line{suggestionid} = $$suggestion{suggestionid};
413 $line{surnamesuggestedby} = $$suggestion{surnamesuggestedby};
414 $line{firstnamesuggestedby} = $$suggestion{firstnamesuggestedby};
419 output_html_with_http_headers $query, $cookie, $template->output;