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/) {
243 my $volume = $results[$i]->{'volume'};
244 my $seriestitle = $results[$i]->{'seriestitle'};
245 $line{'title'} .= " / $seriestitle" if $seriestitle;
246 $line{'title'} .= " / $volume" if $volume;
248 $line{'title'} = "Deleted bibliographic notice, can't find title.";
250 push @books_loop, \%line;
256 my $prefgist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
257 my $gist = $sub_total * $prefgist;
258 my $gist_rrp = $sub_total_rrp * $prefgist;
259 $grand_total = $sub_total_est = $sub_total;
260 $grand_total_est = $sub_total_est; # FIXME: Too many things that are ALL the SAME
262 if ($temp = $bookseller->{'listincgst'}) {
263 $template->param(listincgst => $temp);
266 $grand_total += $gist;
267 $grand_total_est += $sub_total_est * $prefgist; # same thing as += gist
269 if ($temp = $bookseller->{'discount'}) {
270 $template->param(discount => sprintf( "%.2f", $temp ));
272 my $contract = &GetContract($basket->{contractnumber});
274 basketno => $basketno,
275 basketname => $basket->{'basketname'},
276 basketnote => $basket->{note},
277 basketbooksellernote => $basket->{booksellernote},
278 basketcontractno => $basket->{contractnumber},
279 basketcontractname => $contract->{contractname},
280 creationdate => format_date( $basket->{creationdate} ),
281 authorisedby => $basket->{authorisedby},
282 authorisedbyname => $basket->{authorisedbyname},
283 closedate => format_date( $basket->{closedate} ),
284 active => $bookseller->{'active'},
285 booksellerid => $bookseller->{'id'},
286 name => $bookseller->{'name'},
287 address1 => $bookseller->{'address1'},
288 address2 => $bookseller->{'address2'},
289 address3 => $bookseller->{'address3'},
290 address4 => $bookseller->{'address4'},
291 entrydate => format_date( $results[0]->{'entrydate'} ),
292 books_loop => \@books_loop,
293 sort_loop => \@sort_loop,
295 gist => $gist ? sprintf( "%.2f", $gist ) : 0,
296 gist_rate => sprintf( "%.2f", $prefgist * 100 ) . '%',
297 gist_est => sprintf( "%.2f", $sub_total_est * $prefgist ),
298 gist_rrp => sprintf( "%.2f", $gist_rrp ),
299 sub_total => sprintf( "%.2f", $sub_total ),
300 grand_total => sprintf( "%.2f", $grand_total ),
301 sub_total_est => sprintf( "%.2f", $sub_total_est ),
302 grand_total_est => sprintf( "%.2f", $grand_total_est ),
303 sub_total_rrp => sprintf( "%.2f", $sub_total_rrp ),
304 grand_total_rrp => sprintf( "%.2f", $sub_total_rrp + $gist_rrp ),
305 currency => $bookseller->{'listprice'},
306 qty_total => $qty_total,
308 basketgroups => $basketgroups,
309 grouped => $basket->{basketgroupid},
312 output_html_with_http_headers $query, $cookie, $template->output;