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.
32 use C4
::Bookseller
qw( GetBookSellerFromId);
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 my $confirm_pref= C4
::Context
->preference("BasketConfirmations") || '1';
94 $template->param( skip_confirm_reopen
=> 1) if $confirm_pref eq '2';
96 if ( $op eq 'delete_confirm' ) {
97 my $basketno = $query->param('basketno');
99 $template->param( delete_confirmed
=> 1 );
100 } elsif ( !$bookseller ) {
101 $template->param( NO_BOOKSELLER
=> 1 );
102 } elsif ( $op eq 'del_basket') {
103 $template->param( delete_confirm
=> 1 );
104 if ( C4
::Context
->preference("IndependantBranches") ) {
105 my $userenv = C4
::Context
->userenv;
106 unless ( $userenv->{flags
} == 1 ) {
107 my $validtest = ( $basket->{creationdate
} eq '' )
108 || ( $userenv->{branch
} eq $basket->{branch
} )
109 || ( $userenv->{branch
} eq '' )
110 || ( $basket->{branch
} eq '' );
111 unless ($validtest) {
112 print $query->redirect("../mainpage.pl");
117 $basket->{creationdate
} = "" unless ( $basket->{creationdate
} );
118 $basket->{authorisedby
} = $loggedinuser unless ( $basket->{authorisedby
} );
119 my $contract = &GetContract
($basket->{contractnumber
});
121 basketno
=> $basketno,
122 basketname
=> $basket->{'basketname'},
123 basketnote
=> $basket->{note
},
124 basketbooksellernote
=> $basket->{booksellernote
},
125 basketcontractno
=> $basket->{contractnumber
},
126 basketcontractname
=> $contract->{contractname
},
127 creationdate
=> format_date
( $basket->{creationdate
} ),
128 authorisedby
=> $basket->{authorisedby
},
129 authorisedbyname
=> $basket->{authorisedbyname
},
130 closedate
=> format_date
( $basket->{closedate
} ),
131 active
=> $bookseller->{'active'},
132 booksellerid
=> $bookseller->{'id'},
133 name
=> $bookseller->{'name'},
134 address1
=> $bookseller->{'address1'},
135 address2
=> $bookseller->{'address2'},
136 address3
=> $bookseller->{'address3'},
137 address4
=> $bookseller->{'address4'},
139 } elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
140 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
141 # check if we have to "close" a basket before building page
142 } elsif ($op eq 'export') {
143 print $query->header(
145 -attachment
=> 'basket' . $basket->{'basketno'} . '.csv',
147 print GetBasketAsCSV
($query->param('basketno'));
149 } elsif ($op eq 'close') {
150 my $confirm = $query->param('confirm') || $confirm_pref eq '2';
152 my $basketno = $query->param('basketno');
153 my $booksellerid = $query->param('booksellerid');
154 $basketno =~ /^\d+$/ and CloseBasket
($basketno);
155 # if requested, create basket group, close it and attach the basket
156 if ($query->param('createbasketgroup')) {
157 my $basketgroupid = NewBasketgroup
( { name
=> $basket->{basketname
},
158 booksellerid
=> $booksellerid,
161 ModBasket
( { basketno
=> $basketno,
162 basketgroupid
=> $basketgroupid } );
163 print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid='.$booksellerid.'&closed=1');
165 print $query->redirect('/cgi-bin/koha/acqui/booksellers.pl?supplierid=' . $booksellerid);
169 $template->param(confirm_close
=> "1",
170 booksellerid
=> $booksellerid,
171 basketno
=> $basket->{'basketno'},
172 basketname
=> $basket->{'basketname'},
173 basketgroupname
=> $basket->{'basketname'});
176 } elsif ($op eq 'reopen') {
178 $basket->{basketno
} = $query->param('basketno');
179 $basket->{closedate
} = undef;
181 print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
183 # get librarian branch...
184 if ( C4
::Context
->preference("IndependantBranches") ) {
185 my $userenv = C4
::Context
->userenv;
186 unless ( $userenv->{flags
} == 1 ) {
187 my $validtest = ( $basket->{creationdate
} eq '' )
188 || ( $userenv->{branch
} eq $basket->{branch
} )
189 || ( $userenv->{branch
} eq '' )
190 || ( $basket->{branch
} eq '' );
191 unless ($validtest) {
192 print $query->redirect("../mainpage.pl");
197 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
199 my $member = GetMember
(borrowernumber
=> $loggedinuser);
200 if ($basket->{closedate
} && haspermission
({ acquisition
=> 'group_manage'} )) {
201 $basketgroups = GetBasketgroups
($basket->{booksellerid
});
202 for my $bg ( @
{$basketgroups} ) {
203 if ($basket->{basketgroupid
} && $basket->{basketgroupid
} == $bg->{id
}){
207 my %emptygroup = ( id
=> undef,
209 if ( ! $basket->{basketgroupid
} ) {
210 $emptygroup{default} = 1;
211 $emptygroup{nogroup
} = 1;
213 unshift( @
$basketgroups, \
%emptygroup );
215 # if new basket, pre-fill infos
216 $basket->{creationdate
} = "" unless ( $basket->{creationdate
} );
217 $basket->{authorisedby
} = $loggedinuser unless ( $basket->{authorisedby
} );
220 "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
221 $basket->{creationdate
}, $basket->{authorisedby
};
223 #to get active currency
224 my $cur = GetCurrency
();
227 my @results = GetOrders
( $basketno );
229 my $gist = $bookseller->{gstrate
} // C4
::Context
->preference("gist") // 0;
230 my $discount = $bookseller->{'discount'} / 100;
231 my $total_rrp; # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
232 my $total_rrp_gsti; # RRP Total, GST included
233 my $total_rrp_gste; # RRP Total, GST excluded
239 for my $order ( @results ) {
240 my $rrp = $order->{'listprice'} || 0;
241 my $qty = $order->{'quantity'} || 0;
242 if (!defined $order->{quantityreceived
}) {
243 $order->{quantityreceived
} = 0;
245 for ( qw(rrp ecost quantityreceived)) {
246 if (!defined $order->{$_}) {
251 my $budget = GetBudget
( $order->{'budget_id'} );
252 $rrp = ConvertCurrency
( $order->{'currency'}, $rrp );
254 $total_rrp += $qty * $order->{'rrp'};
255 my $line_total = $qty * $order->{'ecost'};
256 # FIXME: what about the "actual cost" field?
258 my %line = %{ $order };
260 $line{order_received
} = ( $qty == $order->{'quantityreceived'} );
261 $line{basketno
} = $basketno;
262 $line{budget_name
} = $budget->{budget_name
};
263 $line{rrp
} = sprintf( "%.2f", $line{'rrp'} );
264 $line{ecost
} = sprintf( "%.2f", $line{'ecost'} );
265 $line{line_total
} = sprintf( "%.2f", $line_total );
266 if ($line{uncertainprice
}) {
267 $template->param( uncertainprices
=> 1 );
268 $line{rrp
} .= ' (Uncertain)';
271 my $volume = $order->{'volume'};
272 my $seriestitle = $order->{'seriestitle'};
273 $line{'title'} .= " / $seriestitle" if $seriestitle;
274 $line{'title'} .= " / $volume" if $volume;
276 $line{'title'} = "Deleted bibliographic notice, can't find title.";
278 push @books_loop, \
%line;
281 if ($bookseller->{'listincgst'}) { # if prices already includes GST
282 $total_rrp_gsti = $total_rrp; # we know $total_rrp_gsti
283 $total_rrp_gste = $total_rrp_gsti / ($gist + 1); # and can reverse compute other values
284 $gist_rrp = $total_rrp_gsti - $total_rrp_gste; #
285 } else { # if prices does not include GST
286 $total_rrp_gste = $total_rrp; # then we use the common way to compute other values
287 $gist_rrp = $total_rrp_gste * $gist; #
288 $total_rrp_gsti = $total_rrp_gste + $gist_rrp; #
290 # These vars are estimated totals and GST, taking in account the booksellet discount
291 my $total_est_gsti = $total_rrp_gsti - ($total_rrp_gsti * $discount);
292 my $gist_est = $gist_rrp - ($gist_rrp * $discount);
293 my $total_est_gste = $total_rrp_gste - ($total_rrp_gste * $discount);
295 my $contract = &GetContract
($basket->{contractnumber
});
296 my @orders = GetOrders
($basketno);
298 my $borrower= GetMember
('borrowernumber' => $loggedinuser);
299 my $budgets = GetBudgetHierarchy
(q{},$borrower->{branchcode
},$borrower->{borrowernumber
});
301 foreach my $r (@
{$budgets}) {
302 if (!defined $r->{budget_amount
} || $r->{budget_amount
} == 0) {
310 basketno
=> $basketno,
311 basketname
=> $basket->{'basketname'},
312 basketnote
=> $basket->{note
},
313 basketbooksellernote
=> $basket->{booksellernote
},
314 basketcontractno
=> $basket->{contractnumber
},
315 basketcontractname
=> $contract->{contractname
},
316 creationdate
=> C4
::Dates
->new($basket->{creationdate
},'iso')->output,
317 authorisedby
=> $basket->{authorisedby
},
318 authorisedbyname
=> $basket->{authorisedbyname
},
319 closedate
=> C4
::Dates
->new($basket->{closedate
},'iso')->output,
320 active
=> $bookseller->{'active'},
321 booksellerid
=> $bookseller->{'id'},
322 name
=> $bookseller->{'name'},
323 entrydate
=> C4
::Dates
->new($results[0]->{'entrydate'},'iso')->output,
324 books_loop
=> \
@books_loop,
325 gist_rate
=> sprintf( "%.2f", $gist * 100 ) . '%',
326 total_rrp_gste
=> sprintf( "%.2f", $total_rrp_gste ),
327 total_est_gste
=> sprintf( "%.2f", $total_est_gste ),
328 gist_est
=> sprintf( "%.2f", $gist_est ),
329 gist_rrp
=> sprintf( "%.2f", $gist_rrp ),
330 total_rrp_gsti
=> sprintf( "%.2f", $total_rrp_gsti ),
331 total_est_gsti
=> sprintf( "%.2f", $total_est_gsti ),
332 # currency => $bookseller->{'listprice'},
333 currency
=> $cur->{'currency'},
334 qty_total
=> $qty_total,
336 basketgroups
=> $basketgroups,
337 grouped
=> $basket->{basketgroupid
},
338 unclosable
=> @orders ?
0 : 1,
339 has_budgets
=> $has_budgets,
343 output_html_with_http_headers
$query, $cookie, $template->output;