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.
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');
93 if ( $op eq 'delete_confirm' ) {
94 my $basketno = $query->param('basketno');
96 $template->param( delete_confirmed
=> 1 );
97 } elsif ( !$bookseller ) {
98 $template->param( NO_BOOKSELLER
=> 1 );
99 } elsif ( $op eq 'del_basket') {
100 $template->param( delete_confirm
=> 1 );
101 if ( C4
::Context
->preference("IndependantBranches") ) {
102 my $userenv = C4
::Context
->userenv;
103 unless ( $userenv->{flags
} == 1 ) {
104 my $validtest = ( $basket->{creationdate
} eq '' )
105 || ( $userenv->{branch
} eq $basket->{branch
} )
106 || ( $userenv->{branch
} eq '' )
107 || ( $basket->{branch
} eq '' );
108 unless ($validtest) {
109 print $query->redirect("../mainpage.pl");
114 $basket->{creationdate
} = "" unless ( $basket->{creationdate
} );
115 $basket->{authorisedby
} = $loggedinuser unless ( $basket->{authorisedby
} );
116 my $contract = &GetContract
($basket->{contractnumber
});
118 basketno
=> $basketno,
119 basketname
=> $basket->{'basketname'},
120 basketnote
=> $basket->{note
},
121 basketbooksellernote
=> $basket->{booksellernote
},
122 basketcontractno
=> $basket->{contractnumber
},
123 basketcontractname
=> $contract->{contractname
},
124 creationdate
=> format_date
( $basket->{creationdate
} ),
125 authorisedby
=> $basket->{authorisedby
},
126 authorisedbyname
=> $basket->{authorisedbyname
},
127 closedate
=> format_date
( $basket->{closedate
} ),
128 active
=> $bookseller->{'active'},
129 booksellerid
=> $bookseller->{'id'},
130 name
=> $bookseller->{'name'},
131 address1
=> $bookseller->{'address1'},
132 address2
=> $bookseller->{'address2'},
133 address3
=> $bookseller->{'address3'},
134 address4
=> $bookseller->{'address4'},
136 } elsif ($op eq 'attachbasket' && $template->{'param_map'}->{'CAN_user_acquisition_group_manage'} == 1) {
137 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
138 # check if we have to "close" a basket before building page
139 } elsif ($op eq 'export') {
140 print $query->header(
142 -attachment
=> 'basket' . $basket->{'basketno'} . '.csv',
144 print GetBasketAsCSV
($query->param('basketno'));
146 } elsif ($op eq 'close') {
147 my $confirm = $query->param('confirm');
149 my $basketno = $query->param('basketno');
150 my $booksellerid = $query->param('booksellerid');
151 $basketno =~ /^\d+$/ and CloseBasket
($basketno);
152 # if requested, create basket group, close it and attach the basket
153 if ($query->param('createbasketgroup')) {
154 my $basketgroupid = NewBasketgroup
( { name
=> $basket->{basketname
},
155 booksellerid
=> $booksellerid,
158 ModBasket
( { basketno
=> $basketno,
159 basketgroupid
=> $basketgroupid } );
160 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
162 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid=' . $booksellerid);
166 $template->param(confirm_close
=> "1",
167 booksellerid
=> $booksellerid,
168 basketno
=> $basket->{'basketno'},
169 basketname
=> $basket->{'basketname'},
170 basketgroupname
=> $basket->{'basketname'});
173 } elsif ($op eq 'reopen') {
175 $basket->{basketno
} = $query->param('basketno');
176 $basket->{closedate
} = undef;
178 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
180 # get librarian branch...
181 if ( C4
::Context
->preference("IndependantBranches") ) {
182 my $userenv = C4
::Context
->userenv;
183 unless ( $userenv->{flags
} == 1 ) {
184 my $validtest = ( $basket->{creationdate
} eq '' )
185 || ( $userenv->{branch
} eq $basket->{branch
} )
186 || ( $userenv->{branch
} eq '' )
187 || ( $basket->{branch
} eq '' );
188 unless ($validtest) {
189 print $query->redirect("../mainpage.pl");
194 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
196 my $member = GetMember
(borrowernumber
=> $loggedinuser);
197 if ($basket->{closedate
} && haspermission
({ flagsrequired
=> { acquisition
=> 'group_manage'} })) {
198 $basketgroups = GetBasketgroups
($basket->{booksellerid
});
199 for my $bg ( @
{$basketgroups} ) {
200 if ($basket->{basketgroupid
} == $bg->{id
}){
204 my %emptygroup = ( id
=> undef,
206 if ( ! $basket->{basketgroupid
} ) {
207 $emptygroup{default} = 1;
208 $emptygroup{nogroup
} = 1;
210 unshift( @
$basketgroups, \
%emptygroup );
212 # if new basket, pre-fill infos
213 $basket->{creationdate
} = "" unless ( $basket->{creationdate
} );
214 $basket->{authorisedby
} = $loggedinuser unless ( $basket->{authorisedby
} );
217 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
218 $basket->{creationdate
}, $basket->{authorisedby
};
220 my @results = GetOrders
( $basketno );
222 my $gist = $bookseller->{gstrate
} // C4
::Context
->preference("gist") // 0;
223 my $discount = $bookseller->{'discount'} / 100;
224 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
225 my $total_rrp_gsti; # RRP Total, GST included
226 my $total_rrp_gste; # RRP Total, GST excluded
232 for my $order ( @results ) {
233 my $rrp = $order->{'listprice'} || 0;
234 my $qty = $order->{'quantity'} || 0;
235 if (!defined $order->{quantityreceived
}) {
236 $order->{quantityreceived
} = 0;
238 for ( qw(rrp ecost quantityreceived)) {
239 if (!defined $order->{$_}) {
244 my $budget = GetBudget
( $order->{'budget_id'} );
245 $rrp = ConvertCurrency
( $order->{'currency'}, $rrp );
247 $total_rrp += $qty * $order->{'rrp'};
248 my $line_total = $qty * $order->{'ecost'};
249 # FIXME: what about the "actual cost" field?
251 my %line = %{ $order };
253 $line{order_received
} = ( $qty == $order->{'quantityreceived'} );
254 $line{basketno
} = $basketno;
255 $line{budget_name
} = $budget->{budget_name
};
256 $line{rrp
} = sprintf( "%.2f", $line{'rrp'} );
257 $line{ecost
} = sprintf( "%.2f", $line{'ecost'} );
258 $line{line_total
} = sprintf( "%.2f", $line_total );
259 if ($line{uncertainprice
}) {
260 $template->param( uncertainprices
=> 1 );
261 $line{rrp
} .= ' (Uncertain)';
264 my $volume = $order->{'volume'};
265 my $seriestitle = $order->{'seriestitle'};
266 $line{'title'} .= " / $seriestitle" if $seriestitle;
267 $line{'title'} .= " / $volume" if $volume;
269 $line{'title'} = "Deleted bibliographic notice, can't find title.";
271 push @books_loop, \
%line;
274 if ($bookseller->{'listincgst'}) { # if prices already includes GST
275 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
276 $total_rrp_gste = $total_rrp_gsti / ($gist + 1); # and can reverse compute other values
277 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
278 } else { # if prices does not include GST
279 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
280 $gist_rrp = $total_rrp_gste * $gist; #
281 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
283 # These vars are estimated totals and GST, taking in account the booksellet discount
284 my $total_est_gsti = $total_rrp_gsti - ($total_rrp_gsti * $discount);
285 my $gist_est = $gist_rrp - ($gist_rrp * $discount);
286 my $total_est_gste = $total_rrp_gste - ($total_rrp_gste * $discount);
288 my $contract = &GetContract
($basket->{contractnumber
});
289 my @orders = GetOrders
($basketno);
291 basketno
=> $basketno,
292 basketname
=> $basket->{'basketname'},
293 basketnote
=> $basket->{note
},
294 basketbooksellernote
=> $basket->{booksellernote
},
295 basketcontractno
=> $basket->{contractnumber
},
296 basketcontractname
=> $contract->{contractname
},
297 creationdate
=> C4
::Dates
->new($basket->{creationdate
},'iso')->output,
298 authorisedby
=> $basket->{authorisedby
},
299 authorisedbyname
=> $basket->{authorisedbyname
},
300 closedate
=> C4
::Dates
->new($basket->{closedate
},'iso')->output,
301 active
=> $bookseller->{'active'},
302 booksellerid
=> $bookseller->{'id'},
303 name
=> $bookseller->{'name'},
304 entrydate
=> C4
::Dates
->new($results[0]->{'entrydate'},'iso')->output,
305 books_loop
=> \
@books_loop,
306 gist_rate
=> sprintf( "%.2f", $gist * 100 ) . '%',
307 total_rrp_gste
=> sprintf( "%.2f", $total_rrp_gste ),
308 total_est_gste
=> sprintf( "%.2f", $total_est_gste ),
309 gist_est
=> sprintf( "%.2f", $gist_est ),
310 gist_rrp
=> sprintf( "%.2f", $gist_rrp ),
311 total_rrp_gsti
=> sprintf( "%.2f", $total_rrp_gsti ),
312 total_est_gsti
=> sprintf( "%.2f", $total_est_gsti ),
313 currency
=> $bookseller->{'listprice'},
314 qty_total
=> $qty_total,
316 basketgroups
=> $basketgroups,
317 grouped
=> $basket->{basketgroupid
},
318 unclosable
=> @orders ?
0 : 1,
322 output_html_with_http_headers
$query, $cookie, $template->output;