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 @parcelitems = @{ $invoice->{orders} };
153 my $countlines = scalar @parcelitems;
155 my $totalquantity = 0;
157 my @loop_received = ();
160 my $total_quantity = 0;
164 for my $item ( @parcelitems ) {
165 $item->{unitprice} = get_value_with_gst_params( $item->{unitprice}, $item->{gstrate}, $bookseller );
166 $total = ( $item->{'unitprice'} ) * $item->{'quantityreceived'};
167 $item->{'unitprice'} += 0;
168 my %line = %{ $item };
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};
174 my ($count) = &GetReservesFromBiblionumber($line{biblionumber},undef,$item->{itemnumber});
175 $line{holds} = $count;
176 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
177 $totalprice += $item->{'unitprice'};
178 $line{unitprice} = sprintf( $cfstr, $item->{'unitprice'} );
179 my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
180 my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
181 $foot{$line{gstrate}}{gstrate} = $line{gstrate};
182 $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
183 $total_quantity += $line{quantity};
184 $total_gste += $gste;
185 $total_gsti += $gste + $gst;
187 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
188 $line{suggestionid} = $suggestion->{suggestionid};
189 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
190 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
192 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
193 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
197 $line{cannot_cancel} = 1;
201 my $budget = GetBudget( $line{budget_id} );
202 $line{budget_name} = $budget->{'budget_name'};
204 push @loop_received, \%line;
205 $totalquantity += $item->{'quantityreceived'};
208 push @book_foot_loop, map { $_ } values %foot;
210 my @loop_orders = ();
211 unless( defined $invoice->{closedate} ) {
214 my $search = $input->param('summaryfilter') || '';
215 my $ean = $input->param('eanfilter') || '';
216 my $basketname = $input->param('basketfilter') || '';
217 my $orderno = $input->param('orderfilter') || '';
218 my $basketgroupname = $input->param('basketgroupnamefilter') || '';
219 $pendingorders = SearchOrders({
220 booksellerid => $booksellerid,
221 basketname => $basketname,
222 ordernumber => $orderno,
225 basketgroupname => $basketgroupname,
229 summaryfilter => $search,
231 basketfilter => $basketname,
232 orderfilter => $orderno,
233 basketgroupnamefilter => $basketgroupname,
236 $pendingorders = SearchOrders({
237 booksellerid => $booksellerid,
241 my $countpendings = scalar @$pendingorders;
243 for (my $i = 0 ; $i < $countpendings ; $i++) {
245 %line = %{$pendingorders->[$i]};
247 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
248 $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
249 $line{quantity} += 0;
250 $line{quantityreceived} += 0;
252 $line{ecost} = sprintf( "%.2f", $ecost );
253 $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
254 $line{unitprice} = sprintf("%.2f",$line{unitprice});
255 $line{invoice} = $invoice;
256 $line{booksellerid} = $booksellerid;
260 my $biblionumber = $line{'biblionumber'};
261 my $countbiblio = CountBiblioInOrders($biblionumber);
262 my $ordernumber = $line{'ordernumber'};
263 my @subscriptions = GetSubscriptionsId ($biblionumber);
264 my $itemcount = GetItemsCount($biblionumber);
265 my $holds = GetHolds ($biblionumber);
266 my @items = GetItemnumbersFromOrder( $ordernumber );
268 foreach my $item (@items){
269 my $nb = GetItemHolds($biblionumber, $item);
275 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
276 $line{suggestionid} = $suggestion->{suggestionid};
277 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
278 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
280 # 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
281 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
282 $line{items} = ($itemcount) - (scalar @items);
283 $line{left_item} = 1 if $line{items} >= 1;
284 $line{left_biblio} = 1 if $countbiblio > 1;
285 $line{biblios} = $countbiblio - 1;
286 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
287 $line{subscriptions} = scalar @subscriptions;
288 $line{left_holds} = ($holds >= 1) ? 1 : 0;
289 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
290 $line{holds} = $holds;
291 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
293 my $budget = GetBudget( $line{budget_id} );
294 $line{budget_name} = $budget->{'budget_name'};
296 push @loop_orders, \%line;
300 loop_orders => \@loop_orders,
305 invoiceid => $invoice->{invoiceid},
306 invoice => $invoice->{invoicenumber},
307 invoiceclosedate => $invoice->{closedate},
308 datereceived => $datereceived->output('iso'),
309 invoicedatereceived => $datereceived->output('iso'),
310 formatteddatereceived => $datereceived->output(),
311 name => $bookseller->{'name'},
312 booksellerid => $bookseller->{id},
313 countreceived => $countlines,
314 loop_received => \@loop_received,
315 loop_orders => \@loop_orders,
316 book_foot_loop => \@book_foot_loop,
317 totalprice => sprintf($cfstr, $totalprice),
318 totalquantity => $totalquantity,
319 (uc(C4::Context->preference("marcflavour"))) => 1,
320 total_quantity => $total_quantity,
321 total_gste => sprintf( "%.2f", $total_gste ),
322 total_gsti => sprintf( "%.2f", $total_gsti ),
324 output_html_with_http_headers $input, $cookie, $template->output;