Bug 11100: make label for Topics facet easily CSS selectable
[koha.git] / acqui / basket.pl
blob681cd60dd304fb91d9525a06b9a38d0d52082934
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;
31 use C4::Bookseller qw( GetBookSellerFromId);
32 use C4::Debug;
33 use C4::Biblio;
34 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
35 use C4::Items;
36 use C4::Suggestions;
37 use Date::Calc qw/Add_Delta_Days/;
39 =head1 NAME
41 basket.pl
43 =head1 DESCRIPTION
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.
49 =head1 CGI PARAMETERS
51 =over 4
53 =item $basketno
55 The basket number.
57 =item booksellerid
59 the supplier this script have to display the basket.
61 =item order
63 =back
65 =cut
67 my $query = new CGI;
68 our $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",
74 query => $query,
75 type => "intranet",
76 authnotrequired => 0,
77 flagsrequired => { acquisition => 'order_manage' },
78 debug => 1,
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');
91 if (!defined $op) {
92 $op = q{};
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("IndependentBranches") ) {
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");
115 exit 1;
119 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
120 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
121 my $contract = &GetContract($basket->{contractnumber});
122 $template->param(
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 deliveryplace => $basket->{deliveryplace},
134 billingplace => $basket->{billingplace},
135 active => $bookseller->{'active'},
136 booksellerid => $bookseller->{'id'},
137 name => $bookseller->{'name'},
138 address1 => $bookseller->{'address1'},
139 address2 => $bookseller->{'address2'},
140 address3 => $bookseller->{'address3'},
141 address4 => $bookseller->{'address4'},
143 } elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
144 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
145 # check if we have to "close" a basket before building page
146 } elsif ($op eq 'export') {
147 print $query->header(
148 -type => 'text/csv',
149 -attachment => 'basket' . $basket->{'basketno'} . '.csv',
151 print GetBasketAsCSV($query->param('basketno'), $query);
152 exit;
153 } elsif ($op eq 'close') {
154 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
155 if ($confirm) {
156 my $basketno = $query->param('basketno');
157 my $booksellerid = $query->param('booksellerid');
158 $basketno =~ /^\d+$/ and CloseBasket($basketno);
159 # if requested, create basket group, close it and attach the basket
160 if ($query->param('createbasketgroup')) {
161 my $branchcode;
162 if(C4::Context->userenv and C4::Context->userenv->{'branch'}
163 and C4::Context->userenv->{'branch'} ne "NO_LIBRARY_SET") {
164 $branchcode = C4::Context->userenv->{'branch'};
166 my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
167 booksellerid => $booksellerid,
168 deliveryplace => $branchcode,
169 billingplace => $branchcode,
170 closed => 1,
172 ModBasket( { basketno => $basketno,
173 basketgroupid => $basketgroupid } );
174 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
175 } else {
176 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?booksellerid=' . $booksellerid);
178 exit;
179 } else {
180 $template->param(confirm_close => "1",
181 booksellerid => $booksellerid,
182 basketno => $basket->{'basketno'},
183 basketname => $basket->{'basketname'},
184 basketgroupname => $basket->{'basketname'});
187 } elsif ($op eq 'reopen') {
188 my $basket;
189 $basket->{basketno} = $query->param('basketno');
190 $basket->{closedate} = undef;
191 ModBasket($basket);
192 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
193 } else {
194 # get librarian branch...
195 if ( C4::Context->preference("IndependentBranches") ) {
196 my $userenv = C4::Context->userenv;
197 unless ( $userenv->{flags} == 1 ) {
198 my $validtest = ( $basket->{creationdate} eq '' )
199 || ( $userenv->{branch} eq $basket->{branch} )
200 || ( $userenv->{branch} eq '' )
201 || ( $basket->{branch} eq '' );
202 unless ($validtest) {
203 print $query->redirect("../mainpage.pl");
204 exit 1;
208 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
209 my ($basketgroup, $basketgroups);
210 my $staffuser = GetMember(borrowernumber => $loggedinuser);
211 if ($basket->{closedate} && haspermission($staffuser->{userid}, { acquisition => 'group_manage'} )) {
212 $basketgroups = GetBasketgroups($basket->{booksellerid});
213 for my $bg ( @{$basketgroups} ) {
214 if ($basket->{basketgroupid} && $basket->{basketgroupid} == $bg->{id}){
215 $bg->{default} = 1;
216 $basketgroup = $bg;
219 my %emptygroup = ( id => undef,
220 name => "No group");
221 if ( ! $basket->{basketgroupid} ) {
222 $emptygroup{default} = 1;
223 $emptygroup{nogroup} = 1;
225 unshift( @$basketgroups, \%emptygroup );
228 # if the basket is closed, calculate estimated delivery date
229 my $estimateddeliverydate;
230 if( $basket->{closedate} ) {
231 my ($year, $month, $day) = ($basket->{closedate} =~ /(\d+)-(\d+)-(\d+)/);
232 ($year, $month, $day) = Add_Delta_Days($year, $month, $day, $bookseller->{deliverytime});
233 $estimateddeliverydate = "$year-$month-$day";
236 # if new basket, pre-fill infos
237 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
238 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
239 $debug
240 and warn sprintf
241 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
242 $basket->{creationdate}, $basket->{authorisedby};
244 #to get active currency
245 my $cur = GetCurrency();
248 my @results = GetOrders( $basketno );
249 my @books_loop;
251 my @book_foot_loop;
252 my %foot;
253 my $total_quantity = 0;
254 my $total_gste = 0;
255 my $total_gsti = 0;
256 my $total_gstvalue = 0;
257 for my $order (@results) {
258 my $line = get_order_infos( $order, $bookseller);
259 if ( $line->{uncertainprice} ) {
260 $template->param( uncertainprices => 1 );
263 push @books_loop, $line;
265 $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
266 $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
267 $total_gstvalue += $$line{gstvalue};
268 $foot{$$line{gstgsti}}{quantity} += $$line{quantity};
269 $total_quantity += $$line{quantity};
270 $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
271 $total_gste += $$line{totalgste};
272 $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
273 $total_gsti += $$line{totalgsti};
276 push @book_foot_loop, map {$_} values %foot;
278 # Get cancelled orders
279 @results = GetCancelledOrders($basketno);
280 my @cancelledorders_loop;
281 for my $order (@results) {
282 my $line = get_order_infos( $order, $bookseller);
283 push @cancelledorders_loop, $line;
286 my $contract = &GetContract($basket->{contractnumber});
287 my @orders = GetOrders($basketno);
289 if ($basket->{basketgroupid}){
290 $basketgroup = GetBasketgroup($basket->{basketgroupid});
291 $basketgroup->{deliveryplacename} = C4::Branch::GetBranchName( $basketgroup->{deliveryplace} );
292 $basketgroup->{billingplacename} = C4::Branch::GetBranchName( $basketgroup->{billingplace} );
294 my $borrower= GetMember('borrowernumber' => $loggedinuser);
295 my $budgets = GetBudgetHierarchy;
296 my $has_budgets = 0;
297 foreach my $r (@{$budgets}) {
298 if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
299 next;
301 next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
303 $has_budgets = 1;
304 last;
307 $template->param(
308 basketno => $basketno,
309 basketname => $basket->{'basketname'},
310 basketnote => $basket->{note},
311 basketbooksellernote => $basket->{booksellernote},
312 basketcontractno => $basket->{contractnumber},
313 basketcontractname => $contract->{contractname},
314 creationdate => $basket->{creationdate},
315 authorisedby => $basket->{authorisedby},
316 authorisedbyname => $basket->{authorisedbyname},
317 closedate => $basket->{closedate},
318 estimateddeliverydate=> $estimateddeliverydate,
319 deliveryplace => C4::Branch::GetBranchName( $basket->{deliveryplace} ),
320 billingplace => C4::Branch::GetBranchName( $basket->{billingplace} ),
321 active => $bookseller->{'active'},
322 booksellerid => $bookseller->{'id'},
323 name => $bookseller->{'name'},
324 books_loop => \@books_loop,
325 book_foot_loop => \@book_foot_loop,
326 cancelledorders_loop => \@cancelledorders_loop,
327 total_quantity => $total_quantity,
328 total_gste => sprintf( "%.2f", $total_gste ),
329 total_gsti => sprintf( "%.2f", $total_gsti ),
330 total_gstvalue => sprintf( "%.2f", $total_gstvalue ),
331 currency => $cur->{'currency'},
332 listincgst => $bookseller->{listincgst},
333 basketgroups => $basketgroups,
334 basketgroup => $basketgroup,
335 grouped => $basket->{basketgroupid},
336 unclosable => @orders ? 0 : 1,
337 has_budgets => $has_budgets,
341 sub get_order_infos {
342 my $order = shift;
343 my $bookseller = shift;
344 my $qty = $order->{'quantity'} || 0;
345 if ( !defined $order->{quantityreceived} ) {
346 $order->{quantityreceived} = 0;
348 my $budget = GetBudget( $order->{'budget_id'} );
350 my %line = %{ $order };
351 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
352 $line{basketno} = $basketno;
353 $line{budget_name} = $budget->{budget_name};
354 $line{rrp} = ConvertCurrency( $order->{'currency'}, $line{rrp} ); # FIXME from comm
355 if ( $bookseller->{'listincgst'} ) {
356 $line{rrpgsti} = sprintf( "%.2f", $line{rrp} );
357 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
358 $line{rrpgste} = sprintf( "%.2f", $line{rrp} / ( 1 + ( $line{gstgsti} / 100 ) ) );
359 $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
360 $line{ecostgsti} = sprintf( "%.2f", $line{ecost} );
361 $line{ecostgste} = sprintf( "%.2f", $line{ecost} / ( 1 + ( $line{gstgsti} / 100 ) ) );
362 $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
363 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
364 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
365 } else {
366 $line{rrpgsti} = sprintf( "%.2f", $line{rrp} * ( 1 + ( $line{gstrate} ) ) );
367 $line{rrpgste} = sprintf( "%.2f", $line{rrp} );
368 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
369 $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
370 $line{ecostgsti} = sprintf( "%.2f", $line{ecost} * ( 1 + ( $line{gstrate} ) ) );
371 $line{ecostgste} = sprintf( "%.2f", $line{ecost} );
372 $line{gstvalue} = sprintf( "%.2f", ( $line{ecostgsti} - $line{ecostgste} ) * $line{quantity});
373 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{ecostgste} );
374 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{ecostgsti} );
377 if ( $line{uncertainprice} ) {
378 $line{rrpgste} .= ' (Uncertain)';
380 if ( $line{'title'} ) {
381 my $volume = $order->{'volume'};
382 my $seriestitle = $order->{'seriestitle'};
383 $line{'title'} .= " / $seriestitle" if $seriestitle;
384 $line{'title'} .= " / $volume" if $volume;
385 } else {
386 $line{'title'} = "Deleted bibliographic notice, can't find title.";
389 my $biblionumber = $order->{'biblionumber'};
390 my $countbiblio = CountBiblioInOrders($biblionumber);
391 my $ordernumber = $order->{'ordernumber'};
392 my @subscriptions = GetSubscriptionsId ($biblionumber);
393 my $itemcount = GetItemsCount($biblionumber);
394 my $holds = GetHolds ($biblionumber);
395 my @items = GetItemnumbersFromOrder( $ordernumber );
396 my $itemholds;
397 foreach my $item (@items){
398 my $nb = GetItemHolds($biblionumber, $item);
399 if ($nb){
400 $itemholds += $nb;
403 # 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
404 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
405 $line{items} = ($itemcount) - (scalar @items);
406 $line{left_item} = 1 if $line{items} >= 1;
407 $line{left_biblio} = 1 if $countbiblio > 1;
408 $line{biblios} = $countbiblio - 1;
409 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
410 $line{subscriptions} = scalar @subscriptions;
411 ($holds >= 1) ? $line{left_holds} = 1 : $line{left_holds} = 0;
412 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
413 $line{holds} = $holds;
414 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
417 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
418 $line{suggestionid} = $$suggestion{suggestionid};
419 $line{surnamesuggestedby} = $$suggestion{surnamesuggestedby};
420 $line{firstnamesuggestedby} = $$suggestion{firstnamesuggestedby};
422 foreach my $key (qw(transferred_from transferred_to)) {
423 if ($line{$key}) {
424 my $order = GetOrder($line{$key});
425 my $basket = GetBasket($order->{basketno});
426 my $bookseller = GetBookSellerFromId($basket->{booksellerid});
427 $line{$key} = {
428 order => $order,
429 basket => $basket,
430 bookseller => $bookseller,
431 timestamp => $line{$key . '_timestamp'},
436 return \%line;
439 output_html_with_http_headers $query, $cookie, $template->output;