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
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
35 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
38 use Date::Calc qw/Add_Delta_Days/;
46 This script display all informations about basket for the supplier given
47 on input arg. Moreover, it allows us to add a new order for this supplier from
48 an existing record, a suggestion or a new record.
60 the supplier this script have to display the basket.
69 our $basketno = $query->param('basketno');
70 my $booksellerid = $query->param('booksellerid');
71 my $duplinbatch = $query->param('duplinbatch');
73 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
75 template_name => "acqui/basket.tt",
79 flagsrequired => { acquisition => 'order_manage' },
84 my $basket = GetBasket($basketno);
85 $booksellerid = $basket->{booksellerid} unless $booksellerid;
86 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
88 unless (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
90 cannot_manage_basket => 1,
91 basketno => $basketno,
92 basketname => $basket->{basketname},
93 booksellerid => $booksellerid,
94 name => $bookseller->{name}
96 output_html_with_http_headers $query, $cookie, $template->output;
100 # FIXME : what about the "discount" percentage?
101 # FIXME : the query->param('booksellerid') below is probably useless. The bookseller is always known from the basket
102 # if no booksellerid in parameter, get it from basket
103 # warn "=>".$basket->{booksellerid};
104 my $op = $query->param('op');
109 my $confirm_pref= C4::Context->preference("BasketConfirmations") || '1';
110 $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
112 if ( $op eq 'delete_confirm' ) {
113 my $basketno = $query->param('basketno');
114 my $delbiblio = $query->param('delbiblio');
115 my @orders = GetOrders($basketno);
116 #Delete all orders included in that basket, and all items received.
117 foreach my $myorder (@orders){
118 DelOrder($myorder->{biblionumber},$myorder->{ordernumber});
120 # if $delbiblio = 1, delete the records if possible
121 if ((defined $delbiblio)and ($delbiblio ==1)){
122 my @cannotdelbiblios ;
123 foreach my $myorder (@orders){
124 my $biblionumber = $myorder->{'biblionumber'};
125 my $countbiblio = CountBiblioInOrders($biblionumber);
126 my $ordernumber = $myorder->{'ordernumber'};
127 my $subscriptions = scalar GetSubscriptionsId ($biblionumber);
128 my $itemcount = GetItemsCount($biblionumber);
130 if ($countbiblio == 0 && $itemcount == 0 && $subscriptions == 0) {
131 $error = DelBiblio($myorder->{biblionumber}) }
133 push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
134 title=> $myorder->{'title'},
135 author=> $myorder->{'author'},
136 countbiblio=> $countbiblio,
137 itemcount=>$itemcount,
138 subscriptions=>$subscriptions};
141 push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
142 title=> $myorder->{'title'},
143 author=> $myorder->{'author'},
144 othererror=> $error};
147 $template->param( cannotdelbiblios => \@cannotdelbiblios );
150 DelBasket($basketno,);
151 $template->param( delete_confirmed => 1 );
152 } elsif ( !$bookseller ) {
153 $template->param( NO_BOOKSELLER => 1 );
154 } elsif ($op eq 'export') {
155 print $query->header(
157 -attachment => 'basket' . $basket->{'basketno'} . '.csv',
159 print GetBasketAsCSV($query->param('basketno'), $query);
161 } elsif ($op eq 'close') {
162 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
164 my $basketno = $query->param('basketno');
165 my $booksellerid = $query->param('booksellerid');
166 $basketno =~ /^\d+$/ and CloseBasket($basketno);
167 # if requested, create basket group, close it and attach the basket
168 if ($query->param('createbasketgroup')) {
170 if(C4::Context->userenv and C4::Context->userenv->{'branch'}
171 and C4::Context->userenv->{'branch'} ne "NO_LIBRARY_SET") {
172 $branchcode = C4::Context->userenv->{'branch'};
174 my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
175 booksellerid => $booksellerid,
176 deliveryplace => $branchcode,
177 billingplace => $branchcode,
180 ModBasket( { basketno => $basketno,
181 basketgroupid => $basketgroupid } );
182 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
184 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?booksellerid=' . $booksellerid);
189 confirm_close => "1",
190 booksellerid => $booksellerid,
191 basketno => $basket->{'basketno'},
192 basketname => $basket->{'basketname'},
193 basketgroupname => $basket->{'basketname'},
196 } elsif ($op eq 'reopen') {
197 ReopenBasket($query->param('basketno'));
198 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
199 } elsif ( $op eq 'mod_users' ) {
200 my $basketusers_ids = $query->param('users_ids');
201 my @basketusers = split( /:/, $basketusers_ids );
202 ModBasketUsers($basketno, @basketusers);
203 print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
205 } elsif ( $op eq 'mod_branch' ) {
206 my $branch = $query->param('branch');
207 $branch = undef if(defined $branch and $branch eq '');
209 basketno => $basket->{basketno},
212 print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
216 # get librarian branch...
217 if ( C4::Context->preference("IndependentBranches") ) {
218 my $userenv = C4::Context->userenv;
219 unless ( C4::Context->IsSuperLibrarian() ) {
220 my $validtest = ( $basket->{creationdate} eq '' )
221 || ( $userenv->{branch} eq $basket->{branch} )
222 || ( $userenv->{branch} eq '' )
223 || ( $basket->{branch} eq '' );
224 unless ($validtest) {
225 print $query->redirect("../mainpage.pl");
229 if (!defined $basket->{branch} or $basket->{branch} eq $userenv->{branch}) {
230 push @branches_loop, {
231 branchcode => $userenv->{branch},
232 branchname => $userenv->{branchname},
238 my $branches = C4::Branch::GetBranches;
239 my @branchcodes = sort {
240 $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}
242 foreach my $branch (@branchcodes) {
244 if (defined $basket->{branch}) {
245 $selected = 1 if $branch eq $basket->{branch};
247 $selected = 1 if $branch eq C4::Context->userenv->{branch};
249 push @branches_loop, {
250 branchcode => $branch,
251 branchname => $branches->{$branch}->{branchname},
252 selected => $selected
257 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
258 my ($basketgroup, $basketgroups);
259 my $staffuser = GetMember(borrowernumber => $loggedinuser);
260 if ($basket->{closedate} && haspermission($staffuser->{userid}, { acquisition => 'group_manage'} )) {
261 $basketgroups = GetBasketgroups($basket->{booksellerid});
262 for my $bg ( @{$basketgroups} ) {
263 if ($basket->{basketgroupid} && $basket->{basketgroupid} == $bg->{id}){
270 # if the basket is closed, calculate estimated delivery date
271 my $estimateddeliverydate;
272 if( $basket->{closedate} ) {
273 my ($year, $month, $day) = ($basket->{closedate} =~ /(\d+)-(\d+)-(\d+)/);
274 ($year, $month, $day) = Add_Delta_Days($year, $month, $day, $bookseller->{deliverytime});
275 $estimateddeliverydate = sprintf( "%04d-%02d-%02d", $year, $month, $day );
278 # if new basket, pre-fill infos
279 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
280 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
283 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
284 $basket->{creationdate}, $basket->{authorisedby};
286 my @basketusers_ids = GetBasketUsers($basketno);
288 foreach my $basketuser_id (@basketusers_ids) {
289 my $basketuser = GetMember(borrowernumber => $basketuser_id);
290 push @basketusers, $basketuser if $basketuser;
293 my $active_currency = Koha::Acquisition::Currencies->get_active;
295 my @orders = GetOrders( $basketno );
300 my $total_quantity = 0;
303 my $total_gstvalue = 0;
304 for my $order (@orders) {
305 $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $booksellerid, ordering => 1 });
306 my $line = get_order_infos( $order, $bookseller);
307 if ( $line->{uncertainprice} ) {
308 $template->param( uncertainprices => 1 );
311 push @books_loop, $line;
313 $foot{$$line{gstrate}}{gstrate} = $$line{gstrate};
314 $foot{$$line{gstrate}}{gstvalue} += $$line{gstvalue};
315 $total_gstvalue += $$line{gstvalue};
316 $foot{$$line{gstrate}}{quantity} += $$line{quantity};
317 $total_quantity += $$line{quantity};
318 $foot{$$line{gstrate}}{totalgste} += $$line{totalgste};
319 $total_gste += $$line{totalgste};
320 $foot{$$line{gstrate}}{totalgsti} += $$line{totalgsti};
321 $total_gsti += $$line{totalgsti};
324 push @book_foot_loop, map {$_} values %foot;
326 # Get cancelled orders
327 my @cancelledorders = GetOrders($basketno, { cancelled => 1 });
328 my @cancelledorders_loop;
329 for my $order (@cancelledorders) {
330 $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $booksellerid, ordering => 1 });
331 my $line = get_order_infos( $order, $bookseller);
332 push @cancelledorders_loop, $line;
335 my $contract = GetContract({
336 contractnumber => $basket->{contractnumber}
339 if ($basket->{basketgroupid}){
340 $basketgroup = GetBasketgroup($basket->{basketgroupid});
341 $basketgroup->{deliveryplacename} = C4::Branch::GetBranchName( $basketgroup->{deliveryplace} );
342 $basketgroup->{billingplacename} = C4::Branch::GetBranchName( $basketgroup->{billingplace} );
344 my $borrower= GetMember('borrowernumber' => $loggedinuser);
345 my $budgets = GetBudgetHierarchy;
347 foreach my $r (@{$budgets}) {
348 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
351 next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
358 basketno => $basketno,
359 basketname => $basket->{'basketname'},
360 basketbranchname => C4::Branch::GetBranchName($basket->{branch}),
361 basketnote => $basket->{note},
362 basketbooksellernote => $basket->{booksellernote},
363 basketcontractno => $basket->{contractnumber},
364 basketcontractname => $contract->{contractname},
365 branches_loop => \@branches_loop,
366 creationdate => $basket->{creationdate},
367 authorisedby => $basket->{authorisedby},
368 authorisedbyname => $basket->{authorisedbyname},
369 users_ids => join(':', @basketusers_ids),
370 users => \@basketusers,
371 closedate => $basket->{closedate},
372 estimateddeliverydate=> $estimateddeliverydate,
373 deliveryplace => C4::Branch::GetBranchName( $basket->{deliveryplace} ),
374 billingplace => C4::Branch::GetBranchName( $basket->{billingplace} ),
375 active => $bookseller->{'active'},
376 booksellerid => $bookseller->{'id'},
377 name => $bookseller->{'name'},
378 books_loop => \@books_loop,
379 book_foot_loop => \@book_foot_loop,
380 cancelledorders_loop => \@cancelledorders_loop,
381 total_quantity => $total_quantity,
382 total_gste => sprintf( "%.2f", $total_gste ),
383 total_gsti => sprintf( "%.2f", $total_gsti ),
384 total_gstvalue => sprintf( "%.2f", $total_gstvalue ),
385 currency => $active_currency->currency,
386 listincgst => $bookseller->{listincgst},
387 basketgroups => $basketgroups,
388 basketgroup => $basketgroup,
389 grouped => $basket->{basketgroupid},
390 unclosable => @orders ? 0 : 1,
391 has_budgets => $has_budgets,
392 duplinbatch => $duplinbatch,
396 sub get_order_infos {
398 my $bookseller = shift;
399 my $qty = $order->{'quantity'} || 0;
400 if ( !defined $order->{quantityreceived} ) {
401 $order->{quantityreceived} = 0;
403 my $budget = GetBudget( $order->{'budget_id'} );
405 my %line = %{ $order };
406 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
407 $line{basketno} = $basketno;
408 $line{budget_name} = $budget->{budget_name};
410 if ( $line{uncertainprice} ) {
411 $line{rrpgste} .= ' (Uncertain)';
413 if ( $line{'title'} ) {
414 my $volume = $order->{'volume'};
415 my $seriestitle = $order->{'seriestitle'};
416 $line{'title'} .= " / $seriestitle" if $seriestitle;
417 $line{'title'} .= " / $volume" if $volume;
420 my $biblionumber = $order->{'biblionumber'};
421 my $countbiblio = CountBiblioInOrders($biblionumber);
422 my $ordernumber = $order->{'ordernumber'};
423 my @subscriptions = GetSubscriptionsId ($biblionumber);
424 my $itemcount = GetItemsCount($biblionumber);
425 my $holds = GetHolds ($biblionumber);
426 my @items = GetItemnumbersFromOrder( $ordernumber );
428 foreach my $item (@items){
429 my $nb = GetItemHolds($biblionumber, $item);
434 # 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
435 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
436 $line{items} = ($itemcount) - (scalar @items);
437 $line{left_item} = 1 if $line{items} >= 1;
438 $line{left_biblio} = 1 if $countbiblio > 1;
439 $line{biblios} = $countbiblio - 1;
440 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
441 $line{subscriptions} = scalar @subscriptions;
442 ($holds >= 1) ? $line{left_holds} = 1 : $line{left_holds} = 0;
443 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
444 $line{holds} = $holds;
445 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
448 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
449 $line{suggestionid} = $$suggestion{suggestionid};
450 $line{surnamesuggestedby} = $$suggestion{surnamesuggestedby};
451 $line{firstnamesuggestedby} = $$suggestion{firstnamesuggestedby};
453 foreach my $key (qw(transferred_from transferred_to)) {
455 my $order = GetOrder($line{$key});
456 my $basket = GetBasket($order->{basketno});
457 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
461 bookseller => $bookseller,
462 timestamp => $line{$key . '_timestamp'},
470 output_html_with_http_headers $query, $cookie, $template->output;