3 #script to receive 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
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
30 This script shows all orders receipt or pending for a given supplier.
31 It allows to write an order as 'received' when it 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.
68 use Koha::Acquisition::Baskets;
69 use Koha::Acquisition::Bookseller;
70 use Koha::Acquisition::Orders;
78 my $sticky_filters = $input->param('sticky_filters') || 0;
80 my ($template, $loggedinuser, $cookie)
81 = get_template_and_user({template_name => "acqui/parcel.tt",
85 flagsrequired => {acquisition => 'order_receive'},
89 my $op = $input->param('op') // '';
91 # process cancellation first so that list of
92 # orders to display is calculated after
93 if ($op eq 'cancelreceipt') {
94 my $ordernumber = $input->param('ordernumber');
95 my $parent_ordernumber = CancelReceipt($ordernumber);
96 unless($parent_ordernumber) {
97 $template->param(error_cancelling_receipt => 1);
101 my $invoiceid = $input->param('invoiceid');
103 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
105 unless( $invoiceid and $invoice->{invoiceid} ) {
107 error_invoice_not_known => 1,
108 no_orders_to_display => 1
110 output_html_with_http_headers $input, $cookie, $template->output;
114 my $booksellerid = $invoice->{booksellerid};
115 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
117 my @orders = @{ $invoice->{orders} };
118 my $countlines = scalar @orders;
119 my @loop_received = ();
122 my $total_tax_excluded = 0;
123 my $total_tax_included = 0;
125 my $subtotal_for_funds;
126 for my $order ( @orders ) {
127 $order->{'unitprice'} += 0;
128 $order->{'booksellerid'} = $booksellerid; # required for basket group links
130 my $order_object = Koha::Acquisition::Orders->find($order->{ordernumber});
131 if ( $bookseller->invoiceincgst ) {
132 $order->{ecost} = $order->{ecost_tax_included};
133 $order->{unitprice} = $order->{unitprice_tax_included};
136 $order->{ecost} = $order->{ecost_tax_excluded};
137 $order->{unitprice} = $order->{unitprice_tax_excluded};
140 $order->{total} = get_rounded_price($order->{unitprice}) * $order->{quantity};
142 my %line = %{ $order };
143 $line{invoice} = $invoice->{invoicenumber};
144 my @itemnumbers = $order_object->items->get_column('itemnumber');
145 my $biblio = Koha::Biblios->find( $line{biblionumber} );
146 $line{total_holds} = $biblio ? $biblio->holds->count : 0;
147 $line{item_holds} = $biblio ? $biblio->current_holds->search(
149 itemnumber => { -in => \@itemnumbers },
152 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
154 $line{tax_value} = $line{tax_value_on_receiving};
155 $line{tax_rate} = $line{tax_rate_on_receiving};
156 $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
157 $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
158 $total_tax_excluded += get_rounded_price($line{unitprice_tax_excluded}) * $line{quantity};
159 $total_tax_included += get_rounded_price($line{unitprice_tax_included}) * $line{quantity};
161 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
162 $line{suggestionid} = $suggestion->{suggestionid};
163 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
164 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
166 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
167 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
171 $line{cannot_cancel} = 1;
175 my $budget_name = GetBudgetName( $line{budget_id} );
176 $line{budget_name} = $budget_name;
178 $subtotal_for_funds->{ $line{budget_name} }{ecost} += get_rounded_price($order->{ecost}) * $order->{quantity};
179 $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
181 push @loop_received, \%line;
183 push @book_foot_loop, map { $_ } values %foot;
185 my @loop_orders = ();
186 unless( defined $invoice->{closedate} ) {
188 if ( $op eq "search" or $sticky_filters ) {
189 my ( $search, $ean, $basketname, $orderno, $basketgroupname );
190 if ( $sticky_filters ) {
191 $search = $input->cookie("filter_parcel_summary");
192 $ean = $input->cookie("filter_parcel_ean");
193 $basketname = $input->cookie("filter_parcel_basketname");
194 $orderno = $input->cookie("filter_parcel_orderno");
195 $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
197 $search = $input->param('summaryfilter') || '';
198 $ean = $input->param('eanfilter') || '';
199 $basketname = $input->param('basketfilter') || '';
200 $orderno = $input->param('orderfilter') || '';
201 $basketgroupname = $input->param('basketgroupnamefilter') || '';
203 $pendingorders = SearchOrders({
204 booksellerid => $booksellerid,
205 basketname => $basketname,
206 ordernumber => $orderno,
209 basketgroupname => $basketgroupname,
214 summaryfilter => $search,
216 basketfilter => $basketname,
217 orderfilter => $orderno,
218 basketgroupnamefilter => $basketgroupname,
221 $pendingorders = SearchOrders({
222 booksellerid => $booksellerid,
226 my $countpendings = scalar @$pendingorders;
228 for (my $i = 0 ; $i < $countpendings ; $i++) {
229 my $order = $pendingorders->[$i];
231 if ( $bookseller->invoiceincgst ) {
232 $order->{ecost} = $order->{ecost_tax_included};
234 $order->{ecost} = $order->{ecost_tax_excluded};
236 $order->{total} = get_rounded_price($order->{ecost}) * $order->{quantity};
240 $line{invoice} = $invoice;
241 $line{booksellerid} = $booksellerid;
243 my $biblionumber = $line{'biblionumber'};
244 my $biblio = Koha::Biblios->find( $biblionumber );
246 use Data::Dump qw(dump);
247 warn "FATAL ERROR: biblionumber $biblionumber not found in line: ",dump( \%line );
249 my $countbiblio = CountBiblioInOrders($biblionumber);
250 my $ordernumber = $line{'ordernumber'};
251 my $order_object = Koha::Acquisition::Orders->find($ordernumber);
252 my $cnt_subscriptions = $biblio ? $biblio->subscriptions->count: 0;
253 my $itemcount = $biblio ? $biblio->items->count : 0;
254 my $holds_count = $biblio ? $biblio->holds->count : 0;
255 my @itemnumbers = $order_object->items->get_column('itemnumber');
256 my $itemholds = $biblio ? $biblio->holds->search({ itemnumber => { -in => \@itemnumbers } })->count : 0;
258 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
259 $line{suggestionid} = $suggestion->{suggestionid};
260 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
261 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
263 # 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
264 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @itemnumbers && !($cnt_subscriptions) && !($holds_count);
265 $line{items} = ($itemcount) - (scalar @itemnumbers);
266 $line{left_item} = 1 if $line{items} >= 1;
267 $line{left_biblio} = 1 if $countbiblio > 1;
268 $line{biblios} = $countbiblio - 1;
269 $line{left_subscription} = 1 if $cnt_subscriptions;
270 $line{subscriptions} = $cnt_subscriptions;
271 $line{left_holds} = ($holds_count >= 1) ? 1 : 0;
272 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
273 $line{holds} = $holds_count;
274 $line{holds_on_order} = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order};
275 $line{basket} = Koha::Acquisition::Baskets->find( $line{basketno} );
277 my $budget_name = GetBudgetName( $line{budget_id} );
278 $line{budget_name} = $budget_name;
280 push @loop_orders, \%line;
284 loop_orders => \@loop_orders,
289 invoiceid => $invoice->{invoiceid},
290 invoice => $invoice->{invoicenumber},
291 invoiceclosedate => $invoice->{closedate},
292 datereceived => dt_from_string,
293 name => $bookseller->name,
294 booksellerid => $bookseller->id,
295 loop_received => \@loop_received,
296 loop_orders => \@loop_orders,
297 book_foot_loop => \@book_foot_loop,
298 (uc(C4::Context->preference("marcflavour"))) => 1,
299 total_tax_excluded => $total_tax_excluded,
300 total_tax_included => $total_tax_included,
301 subtotal_for_funds => $subtotal_for_funds,
302 sticky_filters => $sticky_filters,
304 output_html_with_http_headers $input, $cookie, $template->output;