Bug 13321: Rename variables
[koha.git] / acqui / parcel.pl
index ae4daad..ffe41d8 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 
-#script to recieve orders
+#script to receive orders
 
 
 # Copyright 2000-2002 Katipo Communications
@@ -8,18 +8,18 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -64,11 +64,11 @@ use C4::Biblio;
 use C4::Items;
 use CGI qw ( -utf8 );
 use C4::Output;
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Suggestions;
 use C4::Reserves qw/GetReservesFromBiblionumber/;
 
 use Koha::Acquisition::Bookseller;
+use Koha::DateUtils;
 
 use JSON;
 
@@ -111,28 +111,30 @@ unless( $invoiceid and $invoice->{invoiceid} ) {
 
 my $booksellerid = $invoice->{booksellerid};
 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
-my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
-my $datereceived = C4::Dates->new();
+my $gst = $bookseller->{tax_rate} // C4::Context->preference("gist") // 0;
 
 my @orders        = @{ $invoice->{orders} };
 my $countlines    = scalar @orders;
 my @loop_received = ();
 my @book_foot_loop;
 my %foot;
-my $total_gste = 0;
-my $total_gsti = 0;
+my $total_tax_excluded = 0;
+my $total_tax_included = 0;
 
+my $subtotal_for_funds;
 for my $order ( @orders ) {
-    $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
     $order->{'unitprice'} += 0;
 
     if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
-        $order->{ecost}     = $order->{ecostgste};
-        $order->{unitprice} = $order->{unitpricegste};
+        $order->{ecost}     = $order->{ecost_tax_excluded};
+        $order->{unitprice} = $order->{unitprice_tax_excluded};
     }
     elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
-        $order->{ecost}     = $order->{ecostgsti};
-        $order->{unitprice} = $order->{unitpricegsti};
+        $order->{ecost}     = $order->{ecost_tax_included};
+        $order->{unitprice} = $order->{unitprice_tax_included};
+    } else {
+        $order->{ecost} = $order->{ecost_tax_excluded};
+        $order->{unitprice} = $order->{unitprice_tax_excluded};
     }
     $order->{total} = $order->{unitprice} * $order->{quantity};
 
@@ -145,10 +147,10 @@ for my $order ( @orders ) {
         $line{holds} += scalar( @$holds );
     }
     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
-    $foot{$line{gstrate}}{gstrate} = $line{gstrate};
-    $foot{$line{gstrate}}{gstvalue} += $line{gstvalue};
-    $total_gste += $line{totalgste};
-    $total_gsti += $line{totalgsti};
+    $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
+    $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
+    $total_tax_excluded += Koha::Number::Price->new( $line{ecost_tax_excluded} * $line{quantity} )->format;
+    $total_tax_included += Koha::Number::Price->new( $line{ecost_tax_included} * $line{quantity} )->format;
 
     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
     $line{suggestionid}         = $suggestion->{suggestionid};
@@ -164,8 +166,11 @@ for my $order ( @orders ) {
         }
     }
 
-    my $budget = GetBudget( $line{budget_id} );
-    $line{budget_name} = $budget->{'budget_name'};
+    my $budget_name = GetBudgetName( $line{budget_id} );
+    $line{budget_name} = $budget_name;
+
+    $subtotal_for_funds->{ $line{budget_name} }{ecost} += $order->{ecost} * $order->{quantity};
+    $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
 
     push @loop_received, \%line;
 }
@@ -216,12 +221,13 @@ unless( defined $invoice->{closedate} ) {
 
     for (my $i = 0 ; $i < $countpendings ; $i++) {
         my $order = $pendingorders->[$i];
-        $order = C4::Acquisition::populate_order_with_prices({ order => $order, booksellerid => $bookseller->{id}, receiving => 1, ordering => 1 });
 
         if ( $bookseller->{listincgst} and not $bookseller->{invoiceincgst} ) {
-            $order->{ecost} = $order->{ecostgste};
+            $order->{ecost} = $order->{ecost_tax_excluded};
         } elsif ( not $bookseller->{listinct} and $bookseller->{invoiceincgst} ) {
-            $order->{ecost} = $order->{ecostgsti};
+            $order->{ecost} = $order->{ecost_tax_included};
+        } else {
+            $order->{ecost} = $order->{ecost_tax_excluded};
         }
         $order->{total} = $order->{ecost} * $order->{quantity};
 
@@ -263,8 +269,8 @@ unless( defined $invoice->{closedate} ) {
         $line{holds}                = $holds;
         $line{holds_on_order}       = $itemholds?$itemholds:$holds if $line{left_holds_on_order};
 
-        my $budget = GetBudget( $line{budget_id} );
-        $line{budget_name} = $budget->{'budget_name'};
+        my $budget_name = GetBudgetName( $line{budget_id} );
+        $line{budget_name} = $budget_name;
 
         push @loop_orders, \%line;
     }
@@ -278,17 +284,16 @@ $template->param(
     invoiceid             => $invoice->{invoiceid},
     invoice               => $invoice->{invoicenumber},
     invoiceclosedate      => $invoice->{closedate},
-    datereceived          => $datereceived->output('iso'),
-    invoicedatereceived   => $datereceived->output('iso'),
-    formatteddatereceived => $datereceived->output(),
+    datereceived          => dt_from_string,
     name                  => $bookseller->{'name'},
     booksellerid          => $bookseller->{id},
     loop_received         => \@loop_received,
     loop_orders           => \@loop_orders,
     book_foot_loop        => \@book_foot_loop,
     (uc(C4::Context->preference("marcflavour"))) => 1,
-    total_gste           => $total_gste,
-    total_gsti           => $total_gsti,
+    total_tax_excluded    => $total_tax_excluded,
+    total_tax_included    => $total_tax_included,
+    subtotal_for_funds    => $subtotal_for_funds,
     sticky_filters       => $sticky_filters,
 );
 output_html_with_http_headers $input, $cookie, $template->output;