Bug 10120: Adding a syspref to control if overdue charges are updated on return
[koha.git] / acqui / parcel.pl
blob8e822832794af3f2341059e8fc359b9e309b5615
1 #!/usr/bin/perl
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
14 # version.
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.
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::Bookseller qw/ GetBookSellerFromId /;
64 use C4::Biblio;
65 use C4::Items;
66 use CGI;
67 use C4::Output;
68 use C4::Dates qw/format_date format_date_in_iso/;
69 use C4::Suggestions;
70 use JSON;
72 my $input=new CGI;
74 sub get_value_with_gst_params {
75 my $value = shift;
76 my $gstrate = shift;
77 my $bookseller = shift;
78 if ( $bookseller->{listincgst} ) {
79 if ( $bookseller->{invoiceincgst} ) {
80 return $value;
81 } else {
82 return $value / ( 1 + $gstrate );
84 } else {
85 if ( $bookseller->{invoiceincgst} ) {
86 return $value * ( 1 + $gstrate );
87 } else {
88 return $value;
93 sub get_gste {
94 my $value = shift;
95 my $gstrate = shift;
96 my $bookseller = shift;
97 return $bookseller->{invoiceincgst}
98 ? $value / ( 1 + $gstrate )
99 : $value;
102 sub get_gst {
103 my $value = shift;
104 my $gstrate = shift;
105 my $bookseller = shift;
106 return $bookseller->{invoiceincgst}
107 ? $value / ( 1 + $gstrate ) * $gstrate
108 : $value * ( 1 + $gstrate ) - $value;
111 my ($template, $loggedinuser, $cookie)
112 = get_template_and_user({template_name => "acqui/parcel.tmpl",
113 query => $input,
114 type => "intranet",
115 authnotrequired => 0,
116 flagsrequired => {acquisition => 'order_receive'},
117 debug => 1,
120 my $invoiceid = $input->param('invoiceid');
121 my $op = $input->param('op') // '';
123 if ($op eq 'cancelreceipt') {
124 my $ordernumber = $input->param('ordernumber');
125 my $parent_ordernumber = CancelReceipt($ordernumber);
126 unless($parent_ordernumber) {
127 $template->param(error_cancelling_receipt => 1);
131 my $invoice = GetInvoiceDetails($invoiceid);
132 my $booksellerid = $invoice->{booksellerid};
133 my $bookseller = GetBookSellerFromId($booksellerid);
134 my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
135 my $datereceived = C4::Dates->new();
136 my $code = $input->param('code');
137 my @rcv_err = $input->param('error');
138 my @rcv_err_barcode = $input->param('error_bc');
142 # If receiving error, report the error (coming from finishrecieve.pl(sic)).
143 if( scalar(@rcv_err) ) {
144 my $cnt=0;
145 my $error_loop;
146 for my $err (@rcv_err) {
147 push @$error_loop, { "error_$err" => 1 , barcode => $rcv_err_barcode[$cnt] };
148 $cnt++;
150 $template->param( receive_error => 1 ,
151 error_loop => $error_loop,
155 my $cfstr = "%.2f"; # currency format string -- could get this from currency table.
156 my @parcelitems = @{ $invoice->{orders} };
157 my $countlines = scalar @parcelitems;
158 my $totalprice = 0;
159 my $totalquantity = 0;
160 my $total;
161 my @loop_received = ();
162 my @book_foot_loop;
163 my %foot;
164 my $total_quantity = 0;
165 my $total_gste = 0;
166 my $total_gsti = 0;
168 for my $item ( @parcelitems ) {
169 $item->{unitprice} = get_value_with_gst_params( $item->{unitprice}, $item->{gstrate}, $bookseller );
170 $total = ( $item->{'unitprice'} ) * $item->{'quantityreceived'};
171 $item->{'unitprice'} += 0;
172 my %line;
173 %line = %{ $item };
174 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
175 $line{ecost} = sprintf( "%.2f", $ecost );
176 $line{invoice} = $invoice->{invoicenumber};
177 $line{total} = sprintf($cfstr, $total);
178 $line{booksellerid} = $invoice->{booksellerid};
179 $totalprice += $item->{'unitprice'};
180 $line{unitprice} = sprintf( $cfstr, $item->{'unitprice'} );
181 my $gste = get_gste( $line{total}, $line{gstrate}, $bookseller );
182 my $gst = get_gst( $line{total}, $line{gstrate}, $bookseller );
183 $foot{$line{gstrate}}{gstrate} = $line{gstrate};
184 $foot{$line{gstrate}}{value} += sprintf( "%.2f", $gst );
185 $total_quantity += $line{quantity};
186 $total_gste += $gste;
187 $total_gsti += $gste + $gst;
189 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
190 $line{suggestionid} = $suggestion->{suggestionid};
191 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
192 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
194 if ( $line{parent_ordernumber} != $line{ordernumber} ) {
195 if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
196 @parcelitems )
198 $line{cannot_cancel} = 1;
202 my $budget = GetBudget( $line{budget_id} );
203 $line{budget_name} = $budget->{'budget_name'};
205 push @loop_received, \%line;
206 $totalquantity += $item->{'quantityreceived'};
209 push @book_foot_loop, map { $_ } values %foot;
211 my @loop_orders = ();
212 if(!defined $invoice->{closedate}) {
213 my $pendingorders;
214 if($input->param('op') eq "search"){
215 my $search = $input->param('summaryfilter') || '';
216 my $ean = $input->param('eanfilter') || '';
217 my $basketno = $input->param('basketfilter') || '';
218 my $orderno = $input->param('orderfilter') || '';
219 my $grouped;
220 my $owner;
221 $pendingorders = GetPendingOrders($booksellerid,$grouped,$owner,$basketno,$orderno,$search,$ean);
222 }else{
223 $pendingorders = GetPendingOrders($booksellerid);
225 my $countpendings = scalar @$pendingorders;
227 for (my $i = 0 ; $i < $countpendings ; $i++) {
228 my %line;
229 %line = %{$pendingorders->[$i]};
231 my $ecost = get_value_with_gst_params( $line{ecost}, $line{gstrate}, $bookseller );
232 $line{unitprice} = get_value_with_gst_params( $line{unitprice}, $line{gstrate}, $bookseller );
233 $line{quantity} += 0;
234 $line{quantityreceived} += 0;
235 $line{unitprice}+=0;
236 $line{ecost} = sprintf( "%.2f", $ecost );
237 $line{ordertotal} = sprintf( "%.2f", $ecost * $line{quantity} );
238 $line{unitprice} = sprintf("%.2f",$line{unitprice});
239 $line{invoice} = $invoice;
240 $line{booksellerid} = $booksellerid;
244 my $biblionumber = $line{'biblionumber'};
245 my $countbiblio = CountBiblioInOrders($biblionumber);
246 my $ordernumber = $line{'ordernumber'};
247 my @subscriptions = GetSubscriptionsId ($biblionumber);
248 my $itemcount = GetItemsCount($biblionumber);
249 my $holds = GetHolds ($biblionumber);
250 my @items = GetItemnumbersFromOrder( $ordernumber );
251 my $itemholds;
252 foreach my $item (@items){
253 my $nb = GetItemHolds($biblionumber, $item);
254 if ($nb){
255 $itemholds += $nb;
259 my $suggestion = GetSuggestionInfoFromBiblionumber($line{biblionumber});
260 $line{suggestionid} = $suggestion->{suggestionid};
261 $line{surnamesuggestedby} = $suggestion->{surnamesuggestedby};
262 $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
264 # 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
265 $line{can_del_bib} = 1 if $countbiblio <= 1 && $itemcount == scalar @items && !(@subscriptions) && !($holds);
266 $line{items} = ($itemcount) - (scalar @items);
267 $line{left_item} = 1 if $line{items} >= 1;
268 $line{left_biblio} = 1 if $countbiblio > 1;
269 $line{biblios} = $countbiblio - 1;
270 $line{left_subscription} = 1 if scalar @subscriptions >= 1;
271 $line{subscriptions} = scalar @subscriptions;
272 $line{left_holds} = ($holds >= 1) ? 1 : 0;
273 $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
274 $line{holds} = $holds;
275 $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
277 my $budget = GetBudget( $line{budget_id} );
278 $line{budget_name} = $budget->{'budget_name'};
280 push @loop_orders, \%line;
283 $template->param(
284 loop_orders => \@loop_orders,
288 $template->param(
289 invoiceid => $invoice->{invoiceid},
290 invoice => $invoice->{invoicenumber},
291 invoiceclosedate => $invoice->{closedate},
292 datereceived => $datereceived->output('iso'),
293 invoicedatereceived => $datereceived->output('iso'),
294 formatteddatereceived => $datereceived->output(),
295 name => $bookseller->{'name'},
296 booksellerid => $bookseller->{id},
297 countreceived => $countlines,
298 loop_received => \@loop_received,
299 loop_orders => \@loop_orders,
300 book_foot_loop => \@book_foot_loop,
301 totalprice => sprintf($cfstr, $totalprice),
302 totalquantity => $totalquantity,
303 (uc(C4::Context->preference("marcflavour"))) => 1,
304 total_quantity => $total_quantity,
305 total_gste => sprintf( "%.2f", $total_gste ),
306 total_gsti => sprintf( "%.2f", $total_gsti ),
308 output_html_with_http_headers $input, $cookie, $template->output;