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 ) = 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'));
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')) {
159 my $basketgroupid = NewBasketgroup
( { name
=> $basket->{basketname
},
160 booksellerid
=> $booksellerid,
163 ModBasket
( { basketno
=> $basketno,
164 basketgroupid
=> $basketgroupid } );
165 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
167 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?booksellerid=' . $booksellerid);
171 $template->param(confirm_close
=> "1",
172 booksellerid
=> $booksellerid,
173 basketno
=> $basket->{'basketno'},
174 basketname
=> $basket->{'basketname'},
175 basketgroupname
=> $basket->{'basketname'});
178 } elsif ($op eq 'reopen') {
180 $basket->{basketno
} = $query->param('basketno');
181 $basket->{closedate
} = undef;
183 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
185 # get librarian branch...
186 if ( C4
::Context
->preference("IndependantBranches") ) {
187 my $userenv = C4
::Context
->userenv;
188 unless ( $userenv->{flags
} == 1 ) {
189 my $validtest = ( $basket->{creationdate
} eq '' )
190 || ( $userenv->{branch
} eq $basket->{branch
} )
191 || ( $userenv->{branch
} eq '' )
192 || ( $basket->{branch
} eq '' );
193 unless ($validtest) {
194 print $query->redirect("../mainpage.pl");
199 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
201 my $member = GetMember
(borrowernumber
=> $loggedinuser);
202 if ($basket->{closedate
} && haspermission
({ acquisition
=> 'group_manage'} )) {
203 $basketgroups = GetBasketgroups
($basket->{booksellerid
});
204 for my $bg ( @
{$basketgroups} ) {
205 if ($basket->{basketgroupid
} && $basket->{basketgroupid
} == $bg->{id
}){
209 my %emptygroup = ( id
=> undef,
211 if ( ! $basket->{basketgroupid
} ) {
212 $emptygroup{default} = 1;
213 $emptygroup{nogroup
} = 1;
215 unshift( @
$basketgroups, \
%emptygroup );
218 # if the basket is closed, calculate estimated delivery date
219 my $estimateddeliverydate;
220 if( $basket->{closedate
} ) {
221 my ($year, $month, $day) = ($basket->{closedate
} =~ /(\d+)-(\d+)-(\d+)/);
222 ($year, $month, $day) = Add_Delta_Days
($year, $month, $day, $bookseller->{deliverytime
});
223 $estimateddeliverydate = "$year-$month-$day";
226 # if new basket, pre-fill infos
227 $basket->{creationdate
} = "" unless ( $basket->{creationdate
} );
228 $basket->{authorisedby
} = $loggedinuser unless ( $basket->{authorisedby
} );
231 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
232 $basket->{creationdate
}, $basket->{authorisedby
};
234 #to get active currency
235 my $cur = GetCurrency
();
238 my @results = GetOrders
( $basketno );
240 my $gist = $bookseller->{gstrate
} // C4
::Context
->preference("gist") // 0;
241 $gist = 0 if $gist == 0.0000;
242 my $discount = $bookseller->{'discount'} / 100;
243 my $total_rrp = 0; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
244 my $total_rrp_gsti = 0; # RRP Total, GST included
245 my $total_rrp_gste = 0; # RRP Total, GST excluded
247 my $total_rrp_est = 0;
253 for my $order ( @results ) {
254 my $rrp = $order->{'listprice'} || 0;
255 my $qty = $order->{'quantity'} || 0;
256 if (!defined $order->{quantityreceived
}) {
257 $order->{quantityreceived
} = 0;
259 for ( qw(rrp ecost quantityreceived)) {
260 if (!defined $order->{$_}) {
265 my $budget = GetBudget
( $order->{'budget_id'} );
266 $rrp = ConvertCurrency
( $order->{'currency'}, $rrp );
268 $total_rrp += $qty * $order->{'rrp'};
269 my $line_total = $qty * $order->{'ecost'};
270 $total_rrp_est += $qty * $order->{'ecost'};
271 # FIXME: what about the "actual cost" field?
273 my %line = %{ $order };
274 my $biblionumber = $order->{'biblionumber'};
275 my $countbiblio = CountBiblioInOrders
($biblionumber);
276 my $ordernumber = $order->{'ordernumber'};
277 my @subscriptions = GetSubscriptionsId
($biblionumber);
278 my $itemcount = GetItemsCount
($biblionumber);
279 my $holds = GetHolds
($biblionumber);
280 my @items = GetItemnumbersFromOrder
( $ordernumber );
282 foreach my $item (@items){
283 my $nb = GetItemHolds
($biblionumber, $item);
288 # 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
289 $line{can_del_bib
} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
290 $line{items
} = ($itemcount) - (scalar @items);
291 $line{left_item
} = 1 if $line{items
} >= 1;
292 $line{left_biblio
} = 1 if $countbiblio > 1;
293 $line{biblios
} = $countbiblio - 1;
294 $line{left_subscription
} = 1 if scalar @subscriptions >= 1;
295 $line{subscriptions
} = scalar @subscriptions;
296 ($holds >= 1) ?
$line{left_holds
} = 1 : $line{left_holds
} = 0;
297 $line{left_holds_on_order
} = 1 if $line{left_holds
}==1 && ($line{items
} == 0 || $itemholds );
298 $line{holds
} = $holds;
299 $line{holds_on_order
} = $itemholds?
$itemholds:$holds if $line{left_holds_on_order
};
300 $line{order_received
} = ( $qty == $order->{'quantityreceived'} );
301 $line{basketno
} = $basketno;
302 $line{budget_name
} = $budget->{budget_name
};
303 $line{rrp
} = sprintf( "%.2f", $line{'rrp'} );
304 $line{ecost
} = sprintf( "%.2f", $line{'ecost'} );
305 $line{line_total
} = sprintf( "%.2f", $line_total );
306 if ($line{uncertainprice
}) {
307 $template->param( uncertainprices
=> 1 );
308 $line{rrp
} .= ' (Uncertain)';
311 my $volume = $order->{'volume'};
312 my $seriestitle = $order->{'seriestitle'};
313 $line{'title'} .= " / $seriestitle" if $seriestitle;
314 $line{'title'} .= " / $volume" if $volume;
316 $line{'title'} = "Deleted bibliographic notice, can't find title.";
319 $suggestion = GetSuggestionInfoFromBiblionumber
($line{biblionumber
});
320 $line{suggestionid
} = $suggestion->{suggestionid
};
321 $line{surnamesuggestedby
} = $suggestion->{surnamesuggestedby
};
322 $line{firstnamesuggestedby
} = $suggestion->{firstnamesuggestedby
};
324 push @books_loop, \
%line;
330 if ($gist){ # if we have GST
331 if ( $bookseller->{'listincgst'} ) { # if prices already includes GST
332 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
333 $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 ); # and can reverse compute other values
334 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
335 $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
336 $total_est_gsti = $total_rrp_est;
337 } else { # if prices does not include GST
338 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
339 $gist_rrp = $total_rrp_gste * $gist; #
340 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
341 $total_est_gste = $total_rrp_est;
342 $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
344 $gist_est = $gist_rrp - ( $gist_rrp * $discount );
346 $total_rrp_gsti = $total_rrp;
347 $total_est_gsti = $total_rrp_est;
350 my $contract = &GetContract
($basket->{contractnumber
});
351 my @orders = GetOrders
($basketno);
353 my $borrower= GetMember
('borrowernumber' => $loggedinuser);
354 my $budgets = GetBudgetHierarchy
(q{},$borrower->{branchcode
},$borrower->{borrowernumber
});
356 foreach my $r (@
{$budgets}) {
357 if (!defined $r->{budget_amount
} || $r->{budget_amount
} == 0) {
364 my @cancelledorders = GetCancelledOrders
($basketno);
365 foreach (@cancelledorders) {
366 $_->{'line_total'} = sprintf("%.2f", $_->{'ecost'} * $_->{'quantity'});
370 basketno
=> $basketno,
371 basketname
=> $basket->{'basketname'},
372 basketnote
=> $basket->{note
},
373 basketbooksellernote
=> $basket->{booksellernote
},
374 basketcontractno
=> $basket->{contractnumber
},
375 basketcontractname
=> $contract->{contractname
},
376 creationdate
=> $basket->{creationdate
},
377 authorisedby
=> $basket->{authorisedby
},
378 authorisedbyname
=> $basket->{authorisedbyname
},
379 closedate
=> $basket->{closedate
},
380 estimateddeliverydate
=> $estimateddeliverydate,
381 active
=> $bookseller->{'active'},
382 booksellerid
=> $bookseller->{'id'},
383 name
=> $bookseller->{'name'},
384 books_loop
=> \
@books_loop,
385 cancelledorders_loop
=> \
@cancelledorders,
386 gist_rate
=> sprintf( "%.2f", $gist * 100 ) . '%',
387 total_rrp_gste
=> sprintf( "%.2f", $total_rrp_gste ),
388 total_est_gste
=> sprintf( "%.2f", $total_est_gste ),
389 gist_est
=> sprintf( "%.2f", $gist_est ),
390 gist_rrp
=> sprintf( "%.2f", $gist_rrp ),
391 total_rrp_gsti
=> sprintf( "%.2f", $total_rrp_gsti ),
392 total_est_gsti
=> sprintf( "%.2f", $total_est_gsti ),
393 # currency => $bookseller->{'listprice'},
394 currency
=> $cur->{'currency'},
395 qty_total
=> $qty_total,
397 basketgroups
=> $basketgroups,
398 grouped
=> $basket->{basketgroupid
},
399 unclosable
=> @orders ?
0 : 1,
400 has_budgets
=> $has_budgets,
404 output_html_with_http_headers
$query, $cookie, $template->output;