Bug 17767: (followup) Rename test file
[koha.git] / acqui / parcel.pl
blob32498ea1a35b536ee73c451444079f6cfe340d63
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;
68 use C4::Reserves qw/GetReservesFromBiblionumber/;
70 use Koha::Acquisition::Bookseller;
71 use Koha::Biblios;
72 use Koha::DateUtils;
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::Bookseller->fetch({ id => $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 for my $itemnumber ( @itemnumbers ) {
144 my $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber });
145 $line{holds} += scalar( @$holds );
147 $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
149 $line{tax_value} = $line{tax_value_on_receiving};
150 $line{tax_rate} = $line{tax_rate_on_receiving};
151 $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
152 $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
153 $total_tax_excluded += $line{unitprice_tax_excluded} * $line{quantity};
154 $total_tax_included += $line{unitprice_tax_included} * $line{quantity};
156 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
157 $line{suggestionid} = $suggestion->{suggestionid};
158 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
159 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
161 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
162 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
163 @orders
166 $line{cannot_cancel} = 1;
170 my $budget_name = GetBudgetName( $line{budget_id} );
171 $line{budget_name} = $budget_name;
173 $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
174 $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
176 push @loop_received, \%line;
178 push @book_foot_loop, map { $_ } values %foot;
180 my @loop_orders = ();
181 unless( defined $invoice->{closedate} ) {
182 my $pendingorders;
183 if ( $op eq "search" or $sticky_filters ) {
184 my ( $search, $ean, $basketname, $orderno, $basketgroupname );
185 if ( $sticky_filters ) {
186 $search = $input->cookie("filter_parcel_summary");
187 $ean = $input->cookie("filter_parcel_ean");
188 $basketname = $input->cookie("filter_parcel_basketname");
189 $orderno = $input->cookie("filter_parcel_orderno");
190 $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
191 } else {
192 $search = $input->param('summaryfilter') || '';
193 $ean = $input->param('eanfilter') || '';
194 $basketname = $input->param('basketfilter') || '';
195 $orderno = $input->param('orderfilter') || '';
196 $basketgroupname = $input->param('basketgroupnamefilter') || '';
198 $pendingorders = SearchOrders({
199 booksellerid => $booksellerid,
200 basketname => $basketname,
201 ordernumber => $orderno,
202 search => $search,
203 ean => $ean,
204 basketgroupname => $basketgroupname,
205 pending => 1,
206 ordered => 1,
208 $template->param(
209 summaryfilter => $search,
210 eanfilter => $ean,
211 basketfilter => $basketname,
212 orderfilter => $orderno,
213 basketgroupnamefilter => $basketgroupname,
215 }else{
216 $pendingorders = SearchOrders({
217 booksellerid => $booksellerid,
218 ordered => 1
221 my $countpendings = scalar @$pendingorders;
223 for (my $i = 0 ; $i < $countpendings ; $i++) {
224 my $order = $pendingorders->[$i];
226 if ( $bookseller->{invoiceincgst} ) {
227 $order->{ecost} = $order->{ecost_tax_included};
228 } else {
229 $order->{ecost} = $order->{ecost_tax_excluded};
231 $order->{total} = $order->{ecost} * $order->{quantity};
233 my %line = %$order;
235 $line{invoice} = $invoice;
236 $line{booksellerid} = $booksellerid;
238 my $biblionumber = $line{'biblionumber'};
239 my $biblio = Koha::Biblios->find( $biblionumber );
240 my $countbiblio = CountBiblioInOrders($biblionumber);
241 my $ordernumber = $line{'ordernumber'};
242 my @subscriptions = GetSubscriptionsId ($biblionumber);
243 my $itemcount = GetItemsCount($biblionumber);
244 my $holds_count = $biblio->holds->count;
245 my @items = GetItemnumbersFromOrder( $ordernumber );
246 my $itemholds;
247 foreach my $item (@items){
248 my $nb = GetItemHolds($biblionumber, $item);
249 if ($nb){
250 $itemholds += $nb;
254 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
255 $line{suggestionid} = $suggestion->{suggestionid};
256 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
257 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
259 # 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
260 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds_count);
261 $line{items} = ($itemcount) - (scalar @items);
262 $line{left_item} = 1 if $line{items} >= 1;
263 $line{left_biblio} = 1 if $countbiblio > 1;
264 $line{biblios} = $countbiblio - 1;
265 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
266 $line{subscriptions} = scalar @subscriptions;
267 $line{left_holds} = ($holds_count >= 1) ? 1 : 0;
268 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
269 $line{holds} = $holds_count;
270 $line{holds_on_order} = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order};
272 my $budget_name = GetBudgetName( $line{budget_id} );
273 $line{budget_name} = $budget_name;
275 push @loop_orders, \%line;
278 $template->param(
279 loop_orders => \@loop_orders,
283 $template->param(
284 invoiceid => $invoice->{invoiceid},
285 invoice => $invoice->{invoicenumber},
286 invoiceclosedate => $invoice->{closedate},
287 datereceived => dt_from_string,
288 name => $bookseller->{'name'},
289 booksellerid => $bookseller->{id},
290 loop_received => \@loop_received,
291 loop_orders => \@loop_orders,
292 book_foot_loop => \@book_foot_loop,
293 (uc(C4::Context->preference("marcflavour"))) => 1,
294 total_tax_excluded => $total_tax_excluded,
295 total_tax_included => $total_tax_included,
296 subtotal_for_funds => $subtotal_for_funds,
297 sticky_filters => $sticky_filters,
299 output_html_with_http_headers $input, $cookie, $template->output;