bug 3765: move road type after address
[koha.git] / acqui / basket.pl
blob492417abb7bcafe96fb9f9762a25c95ef5bddbba
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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA 02111-1307 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;
32 use C4::Bookseller;
33 use C4::Dates qw/format_date/;
34 use C4::Debug;
36 use C4::Members qw/GetMember/; #needed for permissions checking for changing basketgroup of a basket
37 =head1 NAME
39 basket.pl
41 =head1 DESCRIPTION
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.
47 =head1 CGI PARAMETERS
49 =over 4
51 =item $basketno
53 The basket number.
55 =item supplierid
57 the supplier this script have to display the basket.
59 =item order
61 =back
63 =cut
65 my $query = new CGI;
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",
72 query => $query,
73 type => "intranet",
74 authnotrequired => 0,
75 flagsrequired => { acquisition => 'order_manage' },
76 debug => 1,
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');
92 DelBasket($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");
107 exit 1;
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);
115 $template->param(
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'},
133 count => $count,
135 } elsif ($op eq 'attachbasket' && $template->{'param_map'}->{'CAN_user_acquisition_group_manage'} == 1) {
136 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
137 # check if we have to "close" a basket before building page
138 } elsif ($op eq 'close') {
139 my $confirm = $query->param('confirm');
140 if ($confirm) {
141 my $basketno = $query->param('basketno');
142 my $booksellerid = $query->param('booksellerid');
143 $basketno =~ /^\d+$/ and CloseBasket($basketno);
144 # if requested, create basket group, close it and attach the basket
145 if ($query->param('createbasketgroup')) {
146 my $basketgroupid = NewBasketgroup( { name => $basket->{basketname},
147 booksellerid => $booksellerid,
148 closed => 1,
150 ModBasket( { basketno => $basketno,
151 basketgroupid => $basketgroupid } );
152 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid);
153 } else {
154 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno='.$basketno.'&op=attachbasket&booksellerid=' . $booksellerid);
156 exit;
157 } else {
158 $template->param(confirm_close => "1",
159 booksellerid => $booksellerid,
160 basketno => $basket->{'basketno'},
161 basketname => $basket->{'basketname'},
162 basketgroupname => $basket->{'basketname'});
165 } elsif ($query->param('op') eq 'reopen') {
166 my $basket;
167 $basket->{basketno} = $query->param('basketno');
168 $basket->{closedate} = undef;
169 ModBasket($basket);
170 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
171 } else {
172 # get librarian branch...
173 if ( C4::Context->preference("IndependantBranches") ) {
174 my $userenv = C4::Context->userenv;
175 unless ( $userenv->{flags} == 1 ) {
176 my $validtest = ( $basket->{creationdate} eq '' )
177 || ( $userenv->{branch} eq $basket->{branch} )
178 || ( $userenv->{branch} eq '' )
179 || ( $basket->{branch} eq '' );
180 unless ($validtest) {
181 print $query->redirect("../mainpage.pl");
182 exit 1;
186 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
187 my $basketgroups;
188 my $member = GetMember($loggedinuser, "borrowernumber");
189 if ($basket->{closedate} && haspermission({ flagsrequired => { acquisition => 'group_manage'} })) {
190 $basketgroups = GetBasketgroups($basket->{booksellerid});
191 for (my $i=0; $i < scalar(@$basketgroups); $i++) {
192 if ($basket->{basketgroupid} == @$basketgroups[$i]->{id}){
193 @$basketgroups[$i]->{default} = 1;
196 my %emptygroup = ( id => undef,
197 name => "No group");
198 if ( ! $basket->{basketgroupid} ) {
199 $emptygroup{default} = 1;
201 unshift( @$basketgroups, \%emptygroup );
203 # if new basket, pre-fill infos
204 $basket->{creationdate} = "" unless ( $basket->{creationdate} );
205 $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
206 $debug
207 and warn sprintf
208 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
209 $basket->{creationdate}, $basket->{authorisedby};
211 my @results = GetOrders( $basketno );
212 my $count = scalar @results;
214 my $gist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
215 my $discount = $bookseller->{'discount'} / 100;
216 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
217 my $total_rrp_gsti; # RRP Total, GST included
218 my $total_rrp_gste; # RRP Total, GST excluded
219 my $gist_rrp;
221 my $qty_total;
222 my @books_loop;
224 for ( my $i = 0 ; $i < $count ; $i++ ) {
225 my $rrp = $results[$i]->{'listprice'};
226 my $qty = $results[$i]->{'quantity'} || 0;
228 my $budget = GetBudget( $results[$i]->{'budget_id'} );
229 $rrp = ConvertCurrency( $results[$i]->{'currency'}, $rrp );
231 $total_rrp += $qty * $results[$i]->{'rrp'};
232 my $line_total = $qty * $results[$i]->{'ecost'};
233 # FIXME: what about the "actual cost" field?
234 $qty_total += $qty;
235 my %line = %{ $results[$i] };
236 ($i%2) and $line{toggle} = 1;
238 $line{order_received} = ( $qty eq $results[$i]->{'quantityreceived'} );
239 $line{basketno} = $basketno;
240 $line{i} = $i;
241 $line{budget_name} = $budget->{budget_name};
242 $line{rrp} = sprintf( "%.2f", $line{'rrp'} );
243 $line{ecost} = sprintf( "%.2f", $line{'ecost'} );
244 $line{line_total} = sprintf( "%.2f", $line_total );
245 $line{odd} = $i % 2;
246 if ($line{uncertainprice}) {
247 $template->param( unclosable => 1 );
248 for my $key (qw/ecost line_total rrp/) {
249 $line{$key} .= '??';
252 if ($line{'title'}){
253 my $volume = $results[$i]->{'volume'};
254 my $seriestitle = $results[$i]->{'seriestitle'};
255 $line{'title'} .= " / $seriestitle" if $seriestitle;
256 $line{'title'} .= " / $volume" if $volume;
257 } else {
258 $line{'title'} = "Deleted bibliographic notice, can't find title.";
260 push @books_loop, \%line;
263 if ($bookseller->{'listincgst'}) { # if prices already includes GST
264 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
265 $total_rrp_gste = $total_rrp_gsti / ($gist + 1); # and can reverse compute other values
266 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
267 } else { # if prices does not include GST
268 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
269 $gist_rrp = $total_rrp_gste * $gist; #
270 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
272 # These vars are estimated totals and GST, taking in account the booksellet discount
273 my $total_est_gsti = $total_rrp_gsti - ($total_rrp_gsti * $discount);
274 my $gist_est = $gist_rrp - ($gist_rrp * $discount);
275 my $total_est_gste = $total_rrp_gste - ($total_rrp_gste * $discount);
277 my $contract = &GetContract($basket->{contractnumber});
278 $template->param(
279 basketno => $basketno,
280 basketname => $basket->{'basketname'},
281 basketnote => $basket->{note},
282 basketbooksellernote => $basket->{booksellernote},
283 basketcontractno => $basket->{contractnumber},
284 basketcontractname => $contract->{contractname},
285 creationdate => C4::Dates->new($basket->{creationdate},'iso')->output,
286 authorisedby => $basket->{authorisedby},
287 authorisedbyname => $basket->{authorisedbyname},
288 closedate => C4::Dates->new($basket->{closedate},'iso')->output,
289 active => $bookseller->{'active'},
290 booksellerid => $bookseller->{'id'},
291 name => $bookseller->{'name'},
292 entrydate => C4::Dates->new($results[0]->{'entrydate'},'iso')->output,
293 books_loop => \@books_loop,
294 count => $count,
295 gist_rate => sprintf( "%.2f", $gist * 100 ) . '%',
296 total_rrp_gste => sprintf( "%.2f", $total_rrp_gste ),
297 total_est_gste => sprintf( "%.2f", $total_est_gste ),
298 gist_est => sprintf( "%.2f", $gist_est ),
299 gist_rrp => sprintf( "%.2f", $gist_rrp ),
300 total_rrp_gsti => sprintf( "%.2f", $total_rrp_gsti ),
301 total_est_gsti => sprintf( "%.2f", $total_est_gsti ),
302 currency => $bookseller->{'listprice'},
303 qty_total => $qty_total,
304 GST => $gist,
305 basketgroups => $basketgroups,
306 grouped => $basket->{basketgroupid},
310 output_html_with_http_headers $query, $cookie, $template->output;