Bug 13321: Fix the prices calculation method
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to receive orders
4
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
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.
15 #
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.
20 #
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>.
23
24 =head1 NAME
25
26 parcel.pl
27
28 =head1 DESCRIPTION
29
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.
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 =item booksellerid
38
39 To know the supplier this script has to show orders.
40
41 =item code
42
43 is the bookseller invoice number.
44
45
46 =item gst
47
48
49 =item datereceived
50
51 To filter the results list on this given date.
52
53 =back
54
55 =cut
56
57 use strict;
58 use warnings;
59
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/;
69
70 use Koha::Acquisition::Bookseller;
71 use Koha::DateUtils;
72
73 use JSON;
74
75 my $input=new CGI;
76 my $sticky_filters = $input->param('sticky_filters') || 0;
77
78 my ($template, $loggedinuser, $cookie)
79     = get_template_and_user({template_name => "acqui/parcel.tt",
80                  query => $input,
81                  type => "intranet",
82                  authnotrequired => 0,
83                  flagsrequired => {acquisition => 'order_receive'},
84                  debug => 1,
85 });
86
87 my $op = $input->param('op') // '';
88
89 # process cancellation first so that list of
90 # orders to display is calculated after
91 if ($op eq 'cancelreceipt') {
92     my $ordernumber = $input->param('ordernumber');
93     my $parent_ordernumber = CancelReceipt($ordernumber);
94     unless($parent_ordernumber) {
95         $template->param(error_cancelling_receipt => 1);
96     }
97 }
98
99 my $invoiceid = $input->param('invoiceid');
100 my $invoice;
101 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
102
103 unless( $invoiceid and $invoice->{invoiceid} ) {
104     $template->param(
105         error_invoice_not_known => 1,
106         no_orders_to_display    => 1
107     );
108     output_html_with_http_headers $input, $cookie, $template->output;
109     exit;
110 }
111
112 my $booksellerid = $invoice->{booksellerid};
113 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
114 my $gst = $bookseller->{tax_rate} // C4::Context->preference("gist") // 0;
115
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;
123
124 my $subtotal_for_funds;
125 for my $order ( @orders ) {
126     $order->{'unitprice'} += 0;
127
128     if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
129         $order->{ecost}     = $order->{ecost_tax_excluded};
130         $order->{unitprice} = $order->{unitprice_tax_excluded};
131     }
132     elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
133         $order->{ecost}     = $order->{ecost_tax_included};
134         $order->{unitprice} = $order->{unitprice_tax_included};
135     } else {
136         $order->{ecost} = $order->{ecost_tax_excluded};
137         $order->{unitprice} = $order->{unitprice_tax_excluded};
138     }
139     $order->{total} = $order->{unitprice} * $order->{quantity};
140
141     my %line = %{ $order };
142     $line{invoice} = $invoice->{invoicenumber};
143     $line{holds} = 0;
144     my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} );
145     for my $itemnumber ( @itemnumbers ) {
146         my $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber });
147         $line{holds} += scalar( @$holds );
148     }
149     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
150     $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
151     $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
152     $total_tax_excluded += $line{unitprice_tax_excluded} * $line{quantity};
153     $total_tax_included += $line{unitprice_tax_included} * $line{quantity};
154
155     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
156     $line{suggestionid}         = $suggestion->{suggestionid};
157     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
158     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
159
160     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
161         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
162             @orders
163             )
164         {
165             $line{cannot_cancel} = 1;
166         }
167     }
168
169     my $budget_name = GetBudgetName( $line{budget_id} );
170     $line{budget_name} = $budget_name;
171
172     $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
173     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
174
175     push @loop_received, \%line;
176 }
177 push @book_foot_loop, map { $_ } values %foot;
178
179 my @loop_orders = ();
180 unless( defined $invoice->{closedate} ) {
181     my $pendingorders;
182     if ( $op eq "search" or $sticky_filters ) {
183         my ( $search, $ean, $basketname, $orderno, $basketgroupname );
184         if ( $sticky_filters ) {
185             $search = $input->cookie("filter_parcel_summary");
186             $ean = $input->cookie("filter_parcel_ean");
187             $basketname = $input->cookie("filter_parcel_basketname");
188             $orderno = $input->cookie("filter_parcel_orderno");
189             $basketgroupname = $input->cookie("filter_parcel_basketgroupname");
190         } else {
191             $search   = $input->param('summaryfilter') || '';
192             $ean      = $input->param('eanfilter') || '';
193             $basketname = $input->param('basketfilter') || '';
194             $orderno  = $input->param('orderfilter') || '';
195             $basketgroupname = $input->param('basketgroupnamefilter') || '';
196         }
197         $pendingorders = SearchOrders({
198             booksellerid => $booksellerid,
199             basketname => $basketname,
200             ordernumber => $orderno,
201             search => $search,
202             ean => $ean,
203             basketgroupname => $basketgroupname,
204             pending => 1,
205             ordered => 1,
206         });
207         $template->param(
208             summaryfilter => $search,
209             eanfilter => $ean,
210             basketfilter => $basketname,
211             orderfilter => $orderno,
212             basketgroupnamefilter => $basketgroupname,
213         );
214     }else{
215         $pendingorders = SearchOrders({
216             booksellerid => $booksellerid,
217             ordered => 1
218         });
219     }
220     my $countpendings = scalar @$pendingorders;
221
222     for (my $i = 0 ; $i < $countpendings ; $i++) {
223         my $order = $pendingorders->[$i];
224
225         if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
226             $order->{ecost} = $order->{ecost_tax_excluded};
227         } elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
228             $order->{ecost} = $order->{ecost_tax_included};
229         } else {
230             $order->{ecost} = $order->{ecost_tax_excluded};
231         }
232         $order->{total} = $order->{ecost} * $order->{quantity};
233
234         my %line = %$order;
235
236         $line{invoice} = $invoice;
237         $line{booksellerid} = $booksellerid;
238
239         my $biblionumber = $line{'biblionumber'};
240         my $countbiblio = CountBiblioInOrders($biblionumber);
241         my $ordernumber = $line{'ordernumber'};
242         my @subscriptions = GetSubscriptionsId ($biblionumber);
243         my $itemcount = GetItemsCount($biblionumber);
244         my $holds  = GetHolds ($biblionumber);
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;
251             }
252         }
253
254         my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
255         $line{suggestionid}         = $suggestion->{suggestionid};
256         $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
257         $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
258
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);
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 >= 1) ? 1 : 0;
268         $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
269         $line{holds}                = $holds;
270         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
271
272         my $budget_name = GetBudgetName( $line{budget_id} );
273         $line{budget_name} = $budget_name;
274
275         push @loop_orders, \%line;
276     }
277
278     $template->param(
279         loop_orders  => \@loop_orders,
280     );
281 }
282
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,
298 );
299 output_html_with_http_headers $input, $cookie, $template->output;