Bug 17736: [Follow-up] Rename to current_holds
[koha.git] / acqui / parcel.pl
blob44af9704aec15306864b96c6108ab5e15205a138
1 #!/usr/bin/perl
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>.
24 =head1 NAME
26 parcel.pl
28 =head1 DESCRIPTION
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.
33 =head1 CGI PARAMETERS
35 =over 4
37 =item booksellerid
39 To know the supplier this script has to show orders.
41 =item code
43 is the bookseller invoice number.
46 =item gst
49 =item datereceived
51 To filter the results list on this given date.
53 =back
55 =cut
57 use strict;
58 use warnings;
60 use C4::Auth;
61 use C4::Acquisition;
62 use C4::Budgets;
63 use C4::Biblio;
64 use C4::Items;
65 use CGI qw ( -utf8 );
66 use C4::Output;
67 use C4::Suggestions;
69 use Koha::Acquisition::Bookseller;
70 use Koha::Biblios;
71 use Koha::DateUtils;
72 use Koha::Biblios;
74 use JSON;
76 my $input=new CGI;
77 my $sticky_filters = $input->param('sticky_filters') || 0;
79 my ($template, $loggedinuser, $cookie)
80 = get_template_and_user({template_name => "acqui/parcel.tt",
81 query => $input,
82 type => "intranet",
83 authnotrequired => 0,
84 flagsrequired => {acquisition => 'order_receive'},
85 debug => 1,
86 });
88 my $op = $input->param('op') // '';
90 # process cancellation first so that list of
91 # orders to display is calculated after
92 if ($op eq 'cancelreceipt') {
93 my $ordernumber = $input->param('ordernumber');
94 my $parent_ordernumber = CancelReceipt($ordernumber);
95 unless($parent_ordernumber) {
96 $template->param(error_cancelling_receipt => 1);
100 my $invoiceid = $input->param('invoiceid');
101 my $invoice;
102 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
104 unless( $invoiceid and $invoice->{invoiceid} ) {
105 $template->param(
106 error_invoice_not_known => 1,
107 no_orders_to_display => 1
109 output_html_with_http_headers $input, $cookie, $template->output;
110 exit;
113 my $booksellerid = $invoice->{booksellerid};
114 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
116 my @orders = @{ $invoice->{orders} };
117 my $countlines = scalar @orders;
118 my @loop_received = ();
119 my @book_foot_loop;
120 my %foot;
121 my $total_tax_excluded = 0;
122 my $total_tax_included = 0;
124 my $subtotal_for_funds;
125 for my $order ( @orders ) {
126 $order->{'unitprice'} += 0;
128 if ( $bookseller->invoiceincgst ) {
129 $order->{ecost} = $order->{ecost_tax_included};
130 $order->{unitprice} = $order->{unitprice_tax_included};
132 else {
133 $order->{ecost} = $order->{ecost_tax_excluded};
134 $order->{unitprice} = $order->{unitprice_tax_excluded};
137 $order->{total} = $order->{unitprice} * $order->{quantity};
139 my %line = %{ $order };
140 $line{invoice} = $invoice->{invoicenumber};
141 $line{holds} = 0;
142 my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
143 my $biblio = Koha::Biblios->find( $order->{ordernumber} );
144 $line{holds} = $biblio->current_holds->search(
146 itemnumber => { -in => \@itemnumbers },
148 )->count;
149 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
151 $line{tax_value} = $line{tax_value_on_receiving};
152 $line{tax_rate} = $line{tax_rate_on_receiving};
153 $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
154 $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
155 $total_tax_excluded += $line{unitprice_tax_excluded} * $line{quantity};
156 $total_tax_included += $line{unitprice_tax_included} * $line{quantity};
158 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
159 $line{suggestionid} = $suggestion->{suggestionid};
160 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
161 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
163 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
164 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
165 @orders
168 $line{cannot_cancel} = 1;
172 my $budget_name = GetBudgetName( $line{budget_id} );
173 $line{budget_name} = $budget_name;
175 $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
176 $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
178 push @loop_received, \%line;
180 push @book_foot_loop, map { $_ } values %foot;
182 my @loop_orders = ();
183 unless( defined $invoice->{closedate} ) {
184 my $pendingorders;
185 if ( $op eq "search" or $sticky_filters ) {
186 my ( $search, $ean, $basketname, $orderno, $basketgroupname );
187 if ( $sticky_filters ) {
188 $search = $input->cookie("filter_parcel_summary");
189 $ean = $input->cookie("filter_parcel_ean");
190 $basketname = $input->cookie("filter_parcel_basketname");
191 $orderno = $input->cookie("filter_parcel_orderno");
192 $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
193 } else {
194 $search = $input->param('summaryfilter') || '';
195 $ean = $input->param('eanfilter') || '';
196 $basketname = $input->param('basketfilter') || '';
197 $orderno = $input->param('orderfilter') || '';
198 $basketgroupname = $input->param('basketgroupnamefilter') || '';
200 $pendingorders = SearchOrders({
201 booksellerid => $booksellerid,
202 basketname => $basketname,
203 ordernumber => $orderno,
204 search => $search,
205 ean => $ean,
206 basketgroupname => $basketgroupname,
207 pending => 1,
208 ordered => 1,
210 $template->param(
211 summaryfilter => $search,
212 eanfilter => $ean,
213 basketfilter => $basketname,
214 orderfilter => $orderno,
215 basketgroupnamefilter => $basketgroupname,
217 }else{
218 $pendingorders = SearchOrders({
219 booksellerid => $booksellerid,
220 ordered => 1
223 my $countpendings = scalar @$pendingorders;
225 for (my $i = 0 ; $i < $countpendings ; $i++) {
226 my $order = $pendingorders->[$i];
228 if ( $bookseller->invoiceincgst ) {
229 $order->{ecost} = $order->{ecost_tax_included};
230 } else {
231 $order->{ecost} = $order->{ecost_tax_excluded};
233 $order->{total} = $order->{ecost} * $order->{quantity};
235 my %line = %$order;
237 $line{invoice} = $invoice;
238 $line{booksellerid} = $booksellerid;
240 my $biblionumber = $line{'biblionumber'};
241 my $biblio = Koha::Biblios->find( $biblionumber );
242 my $countbiblio = CountBiblioInOrders($biblionumber);
243 my $ordernumber = $line{'ordernumber'};
244 my @subscriptions = GetSubscriptionsId ($biblionumber);
245 my $itemcount = $biblio->items->count;
246 my $holds_count = $biblio->holds->count;
247 my @items = GetItemnumbersFromOrder( $ordernumber );
248 my $itemholds;
249 foreach my $item (@items){
250 my $nb = GetItemHolds($biblionumber, $item);
251 if ($nb){
252 $itemholds += $nb;
256 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
257 $line{suggestionid} = $suggestion->{suggestionid};
258 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
259 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
261 # 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
262 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds_count);
263 $line{items} = ($itemcount) - (scalar @items);
264 $line{left_item} = 1 if $line{items} >= 1;
265 $line{left_biblio} = 1 if $countbiblio > 1;
266 $line{biblios} = $countbiblio - 1;
267 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
268 $line{subscriptions} = scalar @subscriptions;
269 $line{left_holds} = ($holds_count >= 1) ? 1 : 0;
270 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
271 $line{holds} = $holds_count;
272 $line{holds_on_order} = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order};
274 my $budget_name = GetBudgetName( $line{budget_id} );
275 $line{budget_name} = $budget_name;
277 push @loop_orders, \%line;
280 $template->param(
281 loop_orders => \@loop_orders,
285 $template->param(
286 invoiceid => $invoice->{invoiceid},
287 invoice => $invoice->{invoicenumber},
288 invoiceclosedate => $invoice->{closedate},
289 datereceived => dt_from_string,
290 name => $bookseller->name,
291 booksellerid => $bookseller->id,
292 loop_received => \@loop_received,
293 loop_orders => \@loop_orders,
294 book_foot_loop => \@book_foot_loop,
295 (uc(C4::Context->preference("marcflavour"))) => 1,
296 total_tax_excluded => $total_tax_excluded,
297 total_tax_included => $total_tax_included,
298 subtotal_for_funds => $subtotal_for_funds,
299 sticky_filters => $sticky_filters,
301 output_html_with_http_headers $input, $cookie, $template->output;