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/;
75 sub get_value_with_gst_params {
78 my $bookseller = shift;
79 if ( $bookseller->{listincgst} ) {
80 if ( $bookseller->{invoiceincgst} ) {
83 return $value / ( 1 + $gstrate );
86 if ( $bookseller->{invoiceincgst} ) {
87 return $value * ( 1 + $gstrate );
97 my $bookseller = shift;
98 return $bookseller->{invoiceincgst}
99 ? $value / ( 1 + $gstrate )
106 my $bookseller = shift;
107 return $bookseller->{invoiceincgst}
108 ? $value / ( 1 + $gstrate ) * $gstrate
109 : $value * ( 1 + $gstrate ) - $value;
112 my ($template, $loggedinuser, $cookie)
113 = get_template_and_user({template_name => "acqui/parcel.tmpl",
116 authnotrequired => 0,
117 flagsrequired => {acquisition => 'order_receive'},
121 my $op = $input->param('op') // '';
123 # process cancellation first so that list of
124 # orders to display is calculated after
125 if ($op eq 'cancelreceipt') {
126 my $ordernumber = $input->param('ordernumber');
127 my $parent_ordernumber = CancelReceipt($ordernumber);
128 unless($parent_ordernumber) {
129 $template->param(error_cancelling_receipt => 1);
133 my $invoiceid = $input->param('invoiceid');
135 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
137 unless( $invoiceid and $invoice->{invoiceid} ) {
139 error_invoice_not_known => 1,
140 no_orders_to_display => 1
142 output_html_with_http_headers $input, $cookie, $template->output;
146 my $booksellerid = $invoice->{booksellerid};
147 my $bookseller = GetBookSellerFromId($booksellerid);
148 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
149 my $datereceived = C4::Dates->new();
151 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
152 my @orders = @{ $invoice->{orders} };
153 my $countlines = scalar @orders;
155 my $totalquantity = 0;
157 my @loop_received = ();
160 my $total_quantity = 0;
164 for my $order ( @orders ) {
165 $order->{unitprice} = get_value_with_gst_params( $order->{unitprice}, $order->{gstrate}, $bookseller );
166 $total = ( $order->{unitprice} ) * $order->{quantityreceived};
167 $order->{'unitprice'} += 0;
168 my %line = %{ $order };
169 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
170 $line{ecost} = sprintf( "%.2f", $ecost );
171 $line{invoice} = $invoice->{invoicenumber};
172 $line{total} = sprintf($cfstr, $total);
173 $line{booksellerid} = $invoice->{booksellerid};
175 my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
176 for my $itemnumber ( @itemnumbers ) {
177 my ( $count ) = &GetReservesFromBiblionumber($line{biblionumber}, undef, $itemnumber);
178 $line{holds} += $count;
180 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
181 $totalprice += $order->{unitprice};
182 $line{unitprice} = sprintf( $cfstr, $order->{unitprice} );
183 my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
184 my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
185 $foot{$line{gstrate}}{gstrate} = $line{gstrate};
186 $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
187 $total_quantity += $line{quantity};
188 $total_gste += $gste;
189 $total_gsti += $gste + $gst;
191 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
192 $line{suggestionid} = $suggestion->{suggestionid};
193 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
194 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
196 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
197 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
201 $line{cannot_cancel} = 1;
205 my $budget = GetBudget( $line{budget_id} );
206 $line{budget_name} = $budget->{'budget_name'};
208 push @loop_received, \%line;
209 $totalquantity += $order->{quantityreceived};
212 push @book_foot_loop, map { $_ } values %foot;
214 my @loop_orders = ();
215 unless( defined $invoice->{closedate} ) {
218 my $search = $input->param('summaryfilter') || '';
219 my $ean = $input->param('eanfilter') || '';
220 my $basketname = $input->param('basketfilter') || '';
221 my $orderno = $input->param('orderfilter') || '';
222 my $basketgroupname = $input->param('basketgroupnamefilter') || '';
223 $pendingorders = SearchOrders({
224 booksellerid => $booksellerid,
225 basketname => $basketname,
226 ordernumber => $orderno,
229 basketgroupname => $basketgroupname,
233 summaryfilter => $search,
235 basketfilter => $basketname,
236 orderfilter => $orderno,
237 basketgroupnamefilter => $basketgroupname,
240 $pendingorders = SearchOrders({
241 booksellerid => $booksellerid,
245 my $countpendings = scalar @$pendingorders;
247 for (my $i = 0 ; $i < $countpendings ; $i++) {
249 %line = %{$pendingorders->[$i]};
251 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
252 $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
253 $line{quantity} += 0;
254 $line{quantityreceived} += 0;
256 $line{ecost} = sprintf( "%.2f", $ecost );
257 $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
258 $line{unitprice} = sprintf("%.2f",$line{unitprice});
259 $line{invoice} = $invoice;
260 $line{booksellerid} = $booksellerid;
264 my $biblionumber = $line{'biblionumber'};
265 my $countbiblio = CountBiblioInOrders($biblionumber);
266 my $ordernumber = $line{'ordernumber'};
267 my @subscriptions = GetSubscriptionsId ($biblionumber);
268 my $itemcount = GetItemsCount($biblionumber);
269 my $holds = GetHolds ($biblionumber);
270 my @items = GetItemnumbersFromOrder( $ordernumber );
272 foreach my $item (@items){
273 my $nb = GetItemHolds($biblionumber, $item);
279 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
280 $line{suggestionid} = $suggestion->{suggestionid};
281 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
282 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
284 # 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
285 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
286 $line{items} = ($itemcount) - (scalar @items);
287 $line{left_item} = 1 if $line{items} >= 1;
288 $line{left_biblio} = 1 if $countbiblio > 1;
289 $line{biblios} = $countbiblio - 1;
290 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
291 $line{subscriptions} = scalar @subscriptions;
292 $line{left_holds} = ($holds >= 1) ? 1 : 0;
293 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
294 $line{holds} = $holds;
295 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
297 my $budget = GetBudget( $line{budget_id} );
298 $line{budget_name} = $budget->{'budget_name'};
300 push @loop_orders, \%line;
304 loop_orders => \@loop_orders,
309 invoiceid => $invoice->{invoiceid},
310 invoice => $invoice->{invoicenumber},
311 invoiceclosedate => $invoice->{closedate},
312 datereceived => $datereceived->output('iso'),
313 invoicedatereceived => $datereceived->output('iso'),
314 formatteddatereceived => $datereceived->output(),
315 name => $bookseller->{'name'},
316 booksellerid => $bookseller->{id},
317 countreceived => $countlines,
318 loop_received => \@loop_received,
319 loop_orders => \@loop_orders,
320 book_foot_loop => \@book_foot_loop,
321 totalprice => sprintf($cfstr, $totalprice),
322 totalquantity => $totalquantity,
323 (uc(C4::Context->preference("marcflavour"))) => 1,
324 total_quantity => $total_quantity,
325 total_gste => sprintf( "%.2f", $total_gste ),
326 total_gsti => sprintf( "%.2f", $total_gsti ),
328 output_html_with_http_headers $input, $cookie, $template->output;