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');
68 my $sort = $query->param('order') || "aqorders.ordernumber";
72 foreach (split /\,/, $sort) {
76 # other possibly valid tables for later: aqbookfund biblio biblioitems
78 (/^\s*(biblioitems)\.(\w+)\s*$/ and $2 eq 'publishercode') or
79 (/^\s*(aqorders)\.(\w+)\s*$/ and $2 eq 'ordernumber' )
81 $sorthash{table} = $1;
82 $sorthash{field} = $2;
86 push @sort_loop, \%sorthash;
90 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
92 template_name => "acqui/basket.tmpl",
96 flagsrequired => { acquisition => 'order_manage' },
101 my $basket = GetBasket($basketno);
103 # FIXME : what about the "discount" percentage?
104 # FIXME : the query->param('supplierid') below is probably useless. The bookseller is always known from the basket
105 # if no booksellerid in parameter, get it from basket
106 # warn "=>".$basket->{booksellerid};
107 $booksellerid = $basket->{booksellerid} unless $booksellerid;
108 my ($bookseller) = GetBookSellerFromId($booksellerid);
109 my $op = $query->param('op');
111 if ( $op eq 'delete_confirm' ) {
112 my $basketno = $query->param('basketno');
113 DelBasket($basketno);
114 $template->param( delete_confirmed => 1 );
115 } elsif ( !$bookseller ) {
116 $template->param( NO_BOOKSELLER => 1 );
117 } elsif ( $op eq 'del_basket') {
118 $template->param( delete_confirm => 1 );
119 if ( C4::Context->preference("IndependantBranches") ) {
120 my $userenv = C4::Context->userenv;
121 unless ( $userenv->{flags} == 1 ) {
122 my $validtest = ( $basket->{creationdate} eq '' )
123 || ( $userenv->{branch} eq $basket->{branch} )
124 || ( $userenv->{branch} eq '' )
125 || ( $basket->{branch} eq '' );
126 unless ($validtest) {
127 print $query->redirect("../mainpage.pl");
132 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
133 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
134 my $contract = &GetContract($basket->{contractnumber});
135 my $count = scalar GetOrders( $basketno);
137 basketno => $basketno,
138 basketname => $basket->{'basketname'},
139 basketnote => $basket->{note},
140 basketbooksellernote => $basket->{booksellernote},
141 basketcontractno => $basket->{contractnumber},
142 basketcontractname => $contract->{contractname},
143 creationdate => format_date( $basket->{creationdate} ),
144 authorisedby => $basket->{authorisedby},
145 authorisedbyname => $basket->{authorisedbyname},
146 closedate => format_date( $basket->{closedate} ),
147 active => $bookseller->{'active'},
148 booksellerid => $bookseller->{'id'},
149 name => $bookseller->{'name'},
150 address1 => $bookseller->{'address1'},
151 address2 => $bookseller->{'address2'},
152 address3 => $bookseller->{'address3'},
153 address4 => $bookseller->{'address4'},
157 # get librarian branch...
158 if ( C4::Context->preference("IndependantBranches") ) {
159 my $userenv = C4::Context->userenv;
160 unless ( $userenv->{flags} == 1 ) {
161 my $validtest = ( $basket->{creationdate} eq '' )
162 || ( $userenv->{branch} eq $basket->{branch} )
163 || ( $userenv->{branch} eq '' )
164 || ( $basket->{branch} eq '' );
165 unless ($validtest) {
166 print $query->redirect("../mainpage.pl");
171 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
173 my $member = GetMember($loggedinuser, "borrowernumber");
174 if ($basket->{closedate} && haspermission({ flagsrequired => { acquisition => 'group_manage'} })) {
175 $basketgroups = GetBasketgroups($basket->{booksellerid});
176 for (my $i=0; $i < scalar(@$basketgroups); $i++) {
177 if (@$basketgroups[$i]->{closed}) {
178 splice(@$basketgroups, $i, 1);
180 } elsif ($basket->{basketgroupid} == @$basketgroups[$i]->{id}){
181 @$basketgroups[$i]->{default} = 1;
184 my %emptygroup = ( id => undef,
186 if ( ! $basket->{basketgroupid} ) {
187 $emptygroup{default} = 1;
189 unshift( @$basketgroups, \%emptygroup );
191 # if new basket, pre-fill infos
192 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
193 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
196 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
197 $basket->{creationdate}, $basket->{authorisedby};
199 my @results = GetOrders( $basketno, $sort );
200 my $count = scalar @results;
202 my $sub_total; # total of line totals
203 my $grand_total; # $subttotal + $gist
205 # my $line_total_est; # total of each line
206 my $sub_total_est; # total of line totals
207 my $sub_total_rrp; # total of line totals
208 my $grand_total_est; # $subttotal + $gist
213 for ( my $i = 0 ; $i < $count ; $i++ ) {
214 my $rrp = $results[$i]->{'listprice'};
215 my $qty = $results[$i]->{'quantity'};
217 my $budget = GetBudget( $results[$i]->{'budget_id'} );
218 $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
220 $sub_total_rrp += $qty * $results[$i]->{'rrp'};
221 my $line_total = $qty * $results[$i]->{'ecost'};
222 # FIXME: what about the "actual cost" field?
223 $sub_total += $line_total;
225 my %line = %{ $results[$i] };
226 ($i%2) and $line{toggle} = 1;
228 $line{order_received} = ( $qty eq $results[$i]->{'quantityreceived'} );
229 $line{basketno} = $basketno;
231 $line{budget_name} = $budget->{budget_name};
232 $line{rrp} = sprintf( "%.2f", $line{'rrp'} );
233 $line{ecost} = sprintf( "%.2f", $line{'ecost'} );
234 $line{line_total} = sprintf( "%.2f", $line_total );
236 if ($line{uncertainprice}) {
237 $template->param( unclosable => 1 );
238 for my $key (qw/ecost line_total rrp/) {
242 push @books_loop, \%line;
248 my $prefgist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
249 my $gist = $sub_total * $prefgist;
250 my $gist_rrp = $sub_total_rrp * $prefgist;
251 $grand_total = $sub_total_est = $sub_total;
252 $grand_total_est = $sub_total_est; # FIXME: Too many things that are ALL the SAME
254 if ($temp = $bookseller->{'listincgst'}) {
255 $template->param(listincgst => $temp);
258 $grand_total += $gist;
259 $grand_total_est += $sub_total_est * $prefgist; # same thing as += gist
261 if ($temp = $bookseller->{'discount'}) {
262 $template->param(discount => sprintf( "%.2f", $temp ));
264 my $contract = &GetContract($basket->{contractnumber});
266 basketno => $basketno,
267 basketname => $basket->{'basketname'},
268 basketnote => $basket->{note},
269 basketbooksellernote => $basket->{booksellernote},
270 basketcontractno => $basket->{contractnumber},
271 basketcontractname => $contract->{contractname},
272 creationdate => format_date( $basket->{creationdate} ),
273 authorisedby => $basket->{authorisedby},
274 authorisedbyname => $basket->{authorisedbyname},
275 closedate => format_date( $basket->{closedate} ),
276 active => $bookseller->{'active'},
277 booksellerid => $bookseller->{'id'},
278 name => $bookseller->{'name'},
279 address1 => $bookseller->{'address1'},
280 address2 => $bookseller->{'address2'},
281 address3 => $bookseller->{'address3'},
282 address4 => $bookseller->{'address4'},
283 entrydate => format_date( $results[0]->{'entrydate'} ),
284 books_loop => \@books_loop,
285 sort_loop => \@sort_loop,
287 gist => $gist ? sprintf( "%.2f", $gist ) : 0,
288 gist_rate => sprintf( "%.2f", $prefgist * 100 ) . '%',
289 gist_est => sprintf( "%.2f", $sub_total_est * $prefgist ),
290 gist_rrp => sprintf( "%.2f", $gist_rrp ),
291 sub_total => sprintf( "%.2f", $sub_total ),
292 grand_total => sprintf( "%.2f", $grand_total ),
293 sub_total_est => sprintf( "%.2f", $sub_total_est ),
294 grand_total_est => sprintf( "%.2f", $grand_total_est ),
295 sub_total_rrp => sprintf( "%.2f", $sub_total_rrp ),
296 grand_total_rrp => sprintf( "%.2f", $sub_total_rrp + $gist_rrp ),
297 currency => $bookseller->{'listprice'},
298 qty_total => $qty_total,
300 basketgroups => $basketgroups,
301 grouped => $basket->{basketgroupid},
304 output_html_with_http_headers $query, $cookie, $template->output;