3 #script to recieve orders
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 This script shows all orders receipt or pending for a given supplier.
31 It allows to write an order as 'received' when he arrives.
39 To know the supplier this script has to show orders.
43 is the bookseller invoice number.
51 To filter the results list on this given date.
63 use C4::Bookseller qw/ GetBookSellerFromId /;
68 use C4::Dates qw/format_date format_date_in_iso/;
70 use C4::Reserves qw/GetReservesFromBiblionumber/;
74 my $sticky_filters = $input->param('sticky_filters') || 0;
76 sub get_value_with_gst_params {
79 my $bookseller = shift;
80 if ( $bookseller->{listincgst} ) {
81 if ( $bookseller->{invoiceincgst} ) {
84 return $value / ( 1 + $gstrate );
87 if ( $bookseller->{invoiceincgst} ) {
88 return $value * ( 1 + $gstrate );
98 my $bookseller = shift;
99 return $bookseller->{invoiceincgst}
100 ? $value / ( 1 + $gstrate )
107 my $bookseller = shift;
108 return $bookseller->{invoiceincgst}
109 ? $value / ( 1 + $gstrate ) * $gstrate
110 : $value * ( 1 + $gstrate ) - $value;
113 my ($template, $loggedinuser, $cookie)
114 = get_template_and_user({template_name => "acqui/parcel.tmpl",
117 authnotrequired => 0,
118 flagsrequired => {acquisition => 'order_receive'},
122 my $op = $input->param('op') // '';
124 # process cancellation first so that list of
125 # orders to display is calculated after
126 if ($op eq 'cancelreceipt') {
127 my $ordernumber = $input->param('ordernumber');
128 my $parent_ordernumber = CancelReceipt($ordernumber);
129 unless($parent_ordernumber) {
130 $template->param(error_cancelling_receipt => 1);
134 my $invoiceid = $input->param('invoiceid');
136 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
138 unless( $invoiceid and $invoice->{invoiceid} ) {
140 error_invoice_not_known => 1,
141 no_orders_to_display => 1
143 output_html_with_http_headers $input, $cookie, $template->output;
147 my $booksellerid = $invoice->{booksellerid};
148 my $bookseller = GetBookSellerFromId($booksellerid);
149 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
150 my $datereceived = C4::Dates->new();
152 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
153 my @orders = @{ $invoice->{orders} };
154 my $countlines = scalar @orders;
156 my $totalquantity = 0;
158 my @loop_received = ();
161 my $total_quantity = 0;
165 for my $order ( @orders ) {
166 $order->{unitprice} = get_value_with_gst_params( $order->{unitprice}, $order->{gstrate}, $bookseller );
167 $total = ( $order->{unitprice} ) * $order->{quantityreceived};
168 $order->{'unitprice'} += 0;
169 my %line = %{ $order };
170 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
171 $line{ecost} = sprintf( "%.2f", $ecost );
172 $line{invoice} = $invoice->{invoicenumber};
173 $line{total} = sprintf($cfstr, $total);
174 $line{booksellerid} = $invoice->{booksellerid};
176 my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
177 for my $itemnumber ( @itemnumbers ) {
178 my ( $count ) = &GetReservesFromBiblionumber($line{biblionumber}, undef, $itemnumber);
179 $line{holds} += $count;
181 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
182 $totalprice += $order->{unitprice};
183 $line{unitprice} = sprintf( $cfstr, $order->{unitprice} );
184 my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
185 my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
186 $foot{$line{gstrate}}{gstrate} = $line{gstrate};
187 $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
188 $total_quantity += $line{quantity};
189 $total_gste += $gste;
190 $total_gsti += $gste + $gst;
192 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
193 $line{suggestionid} = $suggestion->{suggestionid};
194 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
195 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
197 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
198 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
202 $line{cannot_cancel} = 1;
206 my $budget = GetBudget( $line{budget_id} );
207 $line{budget_name} = $budget->{'budget_name'};
209 push @loop_received, \%line;
210 $totalquantity += $order->{quantityreceived};
213 push @book_foot_loop, map { $_ } values %foot;
215 my @loop_orders = ();
216 unless( defined $invoice->{closedate} ) {
218 if ( $op eq "search" or $sticky_filters ) {
219 my ( $search, $ean, $basketname, $orderno, $basketgroupname );
220 if ( $sticky_filters ) {
221 $search = $input->cookie("filter_parcel_summary");
222 $ean = $input->cookie("filter_parcel_ean");
223 $basketname = $input->cookie("filter_parcel_basketname");
224 $orderno = $input->cookie("filter_parcel_orderno");
225 $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
227 $search = $input->param('summaryfilter') || '';
228 $ean = $input->param('eanfilter') || '';
229 $basketname = $input->param('basketfilter') || '';
230 $orderno = $input->param('orderfilter') || '';
231 $basketgroupname = $input->param('basketgroupnamefilter') || '';
233 $pendingorders = SearchOrders({
234 booksellerid => $booksellerid,
235 basketname => $basketname,
236 ordernumber => $orderno,
239 basketgroupname => $basketgroupname,
243 summaryfilter => $search,
245 basketfilter => $basketname,
246 orderfilter => $orderno,
247 basketgroupnamefilter => $basketgroupname,
250 $pendingorders = SearchOrders({
251 booksellerid => $booksellerid,
255 my $countpendings = scalar @$pendingorders;
257 for (my $i = 0 ; $i < $countpendings ; $i++) {
259 %line = %{$pendingorders->[$i]};
261 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
262 $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
263 $line{quantity} += 0;
264 $line{quantityreceived} += 0;
266 $line{ecost} = sprintf( "%.2f", $ecost );
267 $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
268 $line{unitprice} = sprintf("%.2f",$line{unitprice});
269 $line{invoice} = $invoice;
270 $line{booksellerid} = $booksellerid;
274 my $biblionumber = $line{'biblionumber'};
275 my $countbiblio = CountBiblioInOrders($biblionumber);
276 my $ordernumber = $line{'ordernumber'};
277 my @subscriptions = GetSubscriptionsId ($biblionumber);
278 my $itemcount = GetItemsCount($biblionumber);
279 my $holds = GetHolds ($biblionumber);
280 my @items = GetItemnumbersFromOrder( $ordernumber );
282 foreach my $item (@items){
283 my $nb = GetItemHolds($biblionumber, $item);
289 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
290 $line{suggestionid} = $suggestion->{suggestionid};
291 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
292 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
294 # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
295 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
296 $line{items} = ($itemcount) - (scalar @items);
297 $line{left_item} = 1 if $line{items} >= 1;
298 $line{left_biblio} = 1 if $countbiblio > 1;
299 $line{biblios} = $countbiblio - 1;
300 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
301 $line{subscriptions} = scalar @subscriptions;
302 $line{left_holds} = ($holds >= 1) ? 1 : 0;
303 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
304 $line{holds} = $holds;
305 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
307 my $budget = GetBudget( $line{budget_id} );
308 $line{budget_name} = $budget->{'budget_name'};
310 push @loop_orders, \%line;
314 loop_orders => \@loop_orders,
319 invoiceid => $invoice->{invoiceid},
320 invoice => $invoice->{invoicenumber},
321 invoiceclosedate => $invoice->{closedate},
322 datereceived => $datereceived->output('iso'),
323 invoicedatereceived => $datereceived->output('iso'),
324 formatteddatereceived => $datereceived->output(),
325 name => $bookseller->{'name'},
326 booksellerid => $bookseller->{id},
327 countreceived => $countlines,
328 loop_received => \@loop_received,
329 loop_orders => \@loop_orders,
330 book_foot_loop => \@book_foot_loop,
331 totalprice => sprintf($cfstr, $totalprice),
332 totalquantity => $totalquantity,
333 (uc(C4::Context->preference("marcflavour"))) => 1,
334 total_quantity => $total_quantity,
335 total_gste => sprintf( "%.2f", $total_gste ),
336 total_gsti => sprintf( "%.2f", $total_gsti ),
337 sticky_filters => $sticky_filters,
339 output_html_with_http_headers $input, $cookie, $template->output;