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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA 02111-1307 USA
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');
90 if ( $op eq 'delete_confirm' ) {
91 my $basketno = $query->param('basketno');
93 $template->param( delete_confirmed => 1 );
94 } elsif ( !$bookseller ) {
95 $template->param( NO_BOOKSELLER => 1 );
96 } elsif ( $op eq 'del_basket') {
97 $template->param( delete_confirm => 1 );
98 if ( C4::Context->preference("IndependantBranches") ) {
99 my $userenv = C4::Context->userenv;
100 unless ( $userenv->{flags} == 1 ) {
101 my $validtest = ( $basket->{creationdate} eq '' )
102 || ( $userenv->{branch} eq $basket->{branch} )
103 || ( $userenv->{branch} eq '' )
104 || ( $basket->{branch} eq '' );
105 unless ($validtest) {
106 print $query->redirect("../mainpage.pl");
111 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
112 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
113 my $contract = &GetContract($basket->{contractnumber});
114 my $count = scalar GetOrders( $basketno);
116 basketno => $basketno,
117 basketname => $basket->{'basketname'},
118 basketnote => $basket->{note},
119 basketbooksellernote => $basket->{booksellernote},
120 basketcontractno => $basket->{contractnumber},
121 basketcontractname => $contract->{contractname},
122 creationdate => format_date( $basket->{creationdate} ),
123 authorisedby => $basket->{authorisedby},
124 authorisedbyname => $basket->{authorisedbyname},
125 closedate => format_date( $basket->{closedate} ),
126 active => $bookseller->{'active'},
127 booksellerid => $bookseller->{'id'},
128 name => $bookseller->{'name'},
129 address1 => $bookseller->{'address1'},
130 address2 => $bookseller->{'address2'},
131 address3 => $bookseller->{'address3'},
132 address4 => $bookseller->{'address4'},
135 } elsif ($op eq 'attachbasket' && $template->{'param_map'}->{'CAN_user_acquisition_group_manage'} == 1) {
136 my $basketgroups = GetBasketgroups($basket->{booksellerid});
137 for (my $i=0; $i < scalar(@$basketgroups); $i++) {
138 if (@$basketgroups[$i]->{closed}) {
139 splice(@$basketgroups, $i, 1);
141 } elsif ($basket->{basketgroupid} == @$basketgroups[$i]->{id}){
142 @$basketgroups[$i]->{default} = 1;
146 basketgroups => $basketgroups,
147 booksellerid => $booksellerid,
148 basketno => $basket->{'basketno'},
149 basketname => $basket->{'basketname'},
150 name => $bookseller->{'name'},
153 # check if we have to "close" a basket before building page
154 } elsif ($op eq 'close') {
155 my $basket = $query->param('basketno');
156 $basket =~ /^\d+$/ and CloseBasket($basket);
157 if ($template->{'param_map'}->{'CAN_user_acquisition_group_manage'} == 1){
158 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket.'&op=attachbasket');
161 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid='.$booksellerid);
164 } elsif ($query->param('op') eq 'reopen') {
166 $basket->{basketno} = $query->param('basketno');
167 $basket->{closedate} = undef;
169 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
171 # get librarian branch...
172 if ( C4::Context->preference("IndependantBranches") ) {
173 my $userenv = C4::Context->userenv;
174 unless ( $userenv->{flags} == 1 ) {
175 my $validtest = ( $basket->{creationdate} eq '' )
176 || ( $userenv->{branch} eq $basket->{branch} )
177 || ( $userenv->{branch} eq '' )
178 || ( $basket->{branch} eq '' );
179 unless ($validtest) {
180 print $query->redirect("../mainpage.pl");
185 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
187 my $member = GetMember($loggedinuser, "borrowernumber");
188 if ($basket->{closedate} && haspermission({ flagsrequired => { acquisition => 'group_manage'} })) {
189 $basketgroups = GetBasketgroups($basket->{booksellerid});
190 for (my $i=0; $i < scalar(@$basketgroups); $i++) {
191 if (@$basketgroups[$i]->{closed}) {
192 splice(@$basketgroups, $i, 1);
194 } elsif ($basket->{basketgroupid} == @$basketgroups[$i]->{id}){
195 @$basketgroups[$i]->{default} = 1;
198 my %emptygroup = ( id => undef,
200 if ( ! $basket->{basketgroupid} ) {
201 $emptygroup{default} = 1;
203 unshift( @$basketgroups, \%emptygroup );
205 # if new basket, pre-fill infos
206 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
207 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
210 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
211 $basket->{creationdate}, $basket->{authorisedby};
213 my @results = GetOrders( $basketno );
214 my $count = scalar @results;
216 my $gist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
217 my $discount = $bookseller->{'discount'} / 100;
218 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
219 my $total_rrp_gsti; # RRP Total, GST included
220 my $total_rrp_gste; # RRP Total, GST excluded
226 for ( my $i = 0 ; $i < $count ; $i++ ) {
227 my $rrp = $results[$i]->{'listprice'};
228 my $qty = $results[$i]->{'quantity'};
230 my $budget = GetBudget( $results[$i]->{'budget_id'} );
231 $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
233 $total_rrp += $qty * $results[$i]->{'rrp'};
234 my $line_total = $qty * $results[$i]->{'ecost'};
235 # FIXME: what about the "actual cost" field?
237 my %line = %{ $results[$i] };
238 ($i%2) and $line{toggle} = 1;
240 $line{order_received} = ( $qty eq $results[$i]->{'quantityreceived'} );
241 $line{basketno} = $basketno;
243 $line{budget_name} = $budget->{budget_name};
244 $line{rrp} = sprintf( "%.2f", $line{'rrp'} );
245 $line{ecost} = sprintf( "%.2f", $line{'ecost'} );
246 $line{line_total} = sprintf( "%.2f", $line_total );
248 if ($line{uncertainprice}) {
249 $template->param( unclosable => 1 );
250 for my $key (qw/ecost line_total rrp/) {
255 my $volume = $results[$i]->{'volume'};
256 my $seriestitle = $results[$i]->{'seriestitle'};
257 $line{'title'} .= " / $seriestitle" if $seriestitle;
258 $line{'title'} .= " / $volume" if $volume;
260 $line{'title'} = "Deleted bibliographic notice, can't find title.";
262 push @books_loop, \%line;
265 if ($bookseller->{'listincgst'}) { # if prices already includes GST
266 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
267 $total_rrp_gste = $total_rrp_gsti / ($gist + 1); # and can reverse compute other values
268 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
269 } else { # if prices does not include GST
270 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
271 $gist_rrp = $total_rrp_gste * $gist; #
272 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
274 # These vars are estimated totals and GST, taking in account the booksellet discount
275 my $total_est_gsti = $total_rrp_gsti - ($total_rrp_gsti * $discount);
276 my $gist_est = $gist_rrp - ($gist_rrp * $discount);
277 my $total_est_gste = $total_rrp_gste - ($total_rrp_gste * $discount);
279 my $contract = &GetContract($basket->{contractnumber});
281 basketno => $basketno,
282 basketname => $basket->{'basketname'},
283 basketnote => $basket->{note},
284 basketbooksellernote => $basket->{booksellernote},
285 basketcontractno => $basket->{contractnumber},
286 basketcontractname => $contract->{contractname},
287 creationdate => format_date( $basket->{creationdate} ),
288 authorisedby => $basket->{authorisedby},
289 authorisedbyname => $basket->{authorisedbyname},
290 closedate => format_date( $basket->{closedate} ),
291 active => $bookseller->{'active'},
292 booksellerid => $bookseller->{'id'},
293 name => $bookseller->{'name'},
294 address1 => $bookseller->{'address1'},
295 address2 => $bookseller->{'address2'},
296 address3 => $bookseller->{'address3'},
297 address4 => $bookseller->{'address4'},
298 entrydate => format_date( $results[0]->{'entrydate'} ),
299 books_loop => \@books_loop,
301 gist_rate => sprintf( "%.2f", $gist * 100 ) . '%',
302 total_rrp_gste => sprintf( "%.2f", $total_rrp_gste ),
303 total_est_gste => sprintf( "%.2f", $total_est_gste ),
304 gist_est => sprintf( "%.2f", $gist_est ),
305 gist_rrp => sprintf( "%.2f", $gist_rrp ),
306 total_rrp_gsti => sprintf( "%.2f", $total_rrp_gsti ),
307 total_est_gsti => sprintf( "%.2f", $total_est_gsti ),
308 currency => $bookseller->{'listprice'},
309 qty_total => $qty_total,
311 basketgroups => $basketgroups,
312 grouped => $basket->{basketgroupid},
315 output_html_with_http_headers $query, $cookie, $template->output;