Bug 13321: Rename variables
[koha.git] / acqui / invoice.pl
index e38e02d..0672866 100755 (executable)
@@ -3,18 +3,18 @@
 # Copyright 2011 BibLibre SARL
 # 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
 
@@ -29,17 +29,21 @@ Invoice details
 use strict;
 use warnings;
 
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Output;
 use C4::Acquisition;
-use C4::Bookseller qw/GetBookSellerFromId/;
 use C4::Budgets;
 
+use Koha::Acquisition::Bookseller;
+use Koha::Acquisition::Currencies;
+use Koha::DateUtils;
+use Koha::Misc::Files;
+
 my $input = new CGI;
 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
     {
-        template_name   => 'acqui/invoice.tmpl',
+        template_name   => 'acqui/invoice.tt',
         query           => $input,
         type            => 'intranet',
         authnotrequired => 0,
@@ -51,6 +55,12 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
 my $invoiceid = $input->param('invoiceid');
 my $op        = $input->param('op');
 
+my $invoice_files;
+if ( C4::Context->preference('AcqEnableFiles') ) {
+    $invoice_files = Koha::Misc::Files->new(
+        tabletag => 'aqinvoices', recordid => $invoiceid );
+}
+
 if ( $op && $op eq 'close' ) {
     CloseInvoice($invoiceid);
     my $referer = $input->param('referer');
@@ -68,14 +78,14 @@ elsif ( $op && $op eq 'reopen' ) {
     }
 }
 elsif ( $op && $op eq 'mod' ) {
-    my $shipmentdate       = $input->param('shipmentdate');
-    my $billingdate        = $input->param('billingdate');
     my $shipmentcost       = $input->param('shipmentcost');
     my $shipment_budget_id = $input->param('shipment_budget_id');
+    my $invoicenumber      = $input->param('invoicenumber');
     ModInvoice(
         invoiceid             => $invoiceid,
-        shipmentdate          => C4::Dates->new($shipmentdate)->output("iso"),
-        billingdate           => C4::Dates->new($billingdate)->output("iso"),
+        invoicenumber         => $invoicenumber,
+        shipmentdate          => output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
+        billingdate           => output_pref( { str => scalar $input->param('billingdate'),  dateformat => 'iso', dateonly => 1 } ),
         shipmentcost          => $shipmentcost,
         shipmentcost_budgetid => $shipment_budget_id
     );
@@ -83,11 +93,16 @@ elsif ( $op && $op eq 'mod' ) {
         ReopenInvoice($invoiceid);
     } elsif ($input->param('close')) {
         CloseInvoice($invoiceid);
+    } elsif ($input->param('merge')) {
+        my @sources = $input->multi_param('merge');
+        MergeInvoices($invoiceid, \@sources);
+        defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
     }
     $template->param( modified => 1 );
 }
 elsif ( $op && $op eq 'delete' ) {
     DelInvoice($invoiceid);
+    defined($invoice_files) && $invoice_files->DelAllFiles();
     my $referer = $input->param('referer') || 'invoices.pl';
     if ($referer) {
         print $input->redirect($referer);
@@ -95,86 +110,40 @@ elsif ( $op && $op eq 'delete' ) {
     }
 }
 
-my $details     = GetInvoiceDetails($invoiceid);
-my $bookseller  = GetBookSellerFromId( $details->{booksellerid} );
+
+my $details = GetInvoiceDetails($invoiceid);
+my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $details->{booksellerid} });
 my @orders_loop = ();
-my $orders      = $details->{'orders'};
-my $qty_total;
-my @books_loop;
-my @book_foot_loop;
+my $orders = $details->{'orders'};
+my @foot_loop;
 my %foot;
 my $total_quantity = 0;
-my $total_rrp      = 0;
-my $total_est      = 0;
-
+my $total_tax_excluded = 0;
+my $total_tax_included = 0;
+my $total_tax_value = 0;
 foreach my $order (@$orders) {
-    my $line = get_infos( $order, $bookseller );
+    my $line = get_infos( $order, $bookseller);
 
+    $line->{total_tax_excluded} = Koha::Number::Price->new( $line->{unitprice_tax_excluded} * $line->{quantity} )->format;
+    $line->{total_tax_included} = Koha::Number::Price->new( $line->{unitprice_tax_included} * $line->{quantity} )->format;
+
+    $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
+    $foot{$$line{tax_rate}}{tax_value} += $$line{tax_value};
+    $total_tax_value += $$line{tax_value};
+    $foot{$$line{tax_rate}}{quantity}  += $$line{quantity};
     $total_quantity += $$line{quantity};
-    $total_rrp      += $order->{quantity} * $order->{rrp};
-    $total_est      += $order->{quantity} * $order->{'ecost'};
+    $foot{$$line{tax_rate}}{total_tax_excluded} += $$line{total_tax_excluded};
+    $total_tax_excluded += $$line{total_tax_excluded};
+    $foot{$$line{tax_rate}}{total_tax_included} += $$line{total_tax_included};
+    $total_tax_included += $$line{total_tax_included};
 
-    my %row = ( %$order, %$line );
-    push @orders_loop, \%row;
+    $line->{orderline} = $line->{parent_ordernumber};
+    push @orders_loop, $line;
 }
 
-my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
-my $discount =
-  $bookseller->{'discount'} ? ( $bookseller->{discount} / 100 ) : 0;
-my $total_est_gste;
-my $total_est_gsti;
-my $total_rrp_gsti;    # RRP Total, GST included
-my $total_rrp_gste;    # RRP Total, GST excluded
-my $gist_est;
-my $gist_rrp;
-if ($gist) {
-
-    # if we have GST
-    if ( $bookseller->{'listincgst'} ) {
-
-        # if prices already includes GST
-
-        # we know $total_rrp_gsti
-        $total_rrp_gsti = $total_rrp;
-
-        # and can reverse compute other values
-        $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
-
-        $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;
-        $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
-        $total_est_gsti = $total_est;
-    }
-    else {
-        # if prices does not include GST
-
-        # then we use the common way to compute other values
-        $total_rrp_gste = $total_rrp;
-        $gist_rrp       = $total_rrp_gste * $gist;
-        $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
-        $total_est_gste = $total_est;
-        $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
-    }
-    $gist_est = $gist_rrp - ( $gist_rrp * $discount );
-}
-else {
-    $total_rrp_gste = $total_rrp_gsti = $total_rrp;
-    $total_est_gste = $total_est_gsti = $total_est;
-    $gist_rrp       = $gist_est       = 0;
-}
-my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
+push @foot_loop, map {$_} values %foot;
 
 my $format = "%.2f";
-$template->param(
-    total_rrp_gste      => sprintf( $format, $total_rrp_gste ),
-    total_rrp_gsti      => sprintf( $format, $total_rrp_gsti ),
-    total_est_gste      => sprintf( $format, $total_est_gste ),
-    total_est_gsti      => sprintf( $format, $total_est_gsti ),
-    gist_rrp            => sprintf( $format, $gist_rrp ),
-    gist_est            => sprintf( $format, $gist_est ),
-    total_gsti_shipment => sprintf( $format, $total_gsti_shipment ),
-    gist                => sprintf( $format, $gist * 100 ),
-);
-
 my $budgets = GetBudgets();
 my @budgets_loop;
 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
@@ -193,34 +162,44 @@ $template->param(
     invoiceid        => $details->{'invoiceid'},
     invoicenumber    => $details->{'invoicenumber'},
     suppliername     => $details->{'suppliername'},
-    booksellerid       => $details->{'booksellerid'},
-    datereceived     => $details->{'datereceived'},
+    booksellerid     => $details->{'booksellerid'},
     shipmentdate     => $details->{'shipmentdate'},
     billingdate      => $details->{'billingdate'},
     invoiceclosedate => $details->{'closedate'},
-    shipmentcost     => sprintf( $format, $details->{'shipmentcost'} || 0 ),
+    shipmentcost     => $details->{'shipmentcost'},
     orders_loop      => \@orders_loop,
+    foot_loop        => \@foot_loop,
     total_quantity   => $total_quantity,
+    total_tax_excluded       => sprintf( $format, $total_tax_excluded ),
+    total_tax_included       => sprintf( $format, $total_tax_included ),
+    total_tax_value   => sprintf( $format, $total_tax_value ),
+    total_tax_excluded_shipment => sprintf( $format, $total_tax_excluded + $details->{shipmentcost}),
+    total_tax_included_shipment => sprintf( $format, $total_tax_included + $details->{shipmentcost}),
     invoiceincgst    => $bookseller->{invoiceincgst},
-    currency         => $bookseller->{listprice},
-    budgets_loop             => \@budgets_loop,
+    currency         => Koha::Acquisition::Currencies->get_active,
+    budgets_loop     => \@budgets_loop,
 );
 
+defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
+
+# FIXME
+# Fonction dupplicated from basket.pl
+# Code must to be exported. Where ??
 sub get_infos {
-    my $order      = shift;
+    my $order = shift;
     my $bookseller = shift;
-    my $qty        = $order->{'quantity'} || 0;
+    my $qty = $order->{'quantity'} || 0;
     if ( !defined $order->{quantityreceived} ) {
         $order->{quantityreceived} = 0;
     }
     my $budget = GetBudget( $order->{'budget_id'} );
 
-    my %line = %{$order};
+    my %line = %{ $order };
     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
     $line{budget_name}    = $budget->{budget_name};
-    $line{total}          = $qty * $order->{ecost};
 
     if ( $line{uncertainprice} ) {
+        $template->param( uncertainprices => 1 );
         $line{rrp} .= ' (Uncertain)';
     }
     if ( $line{'title'} ) {
@@ -229,9 +208,6 @@ sub get_infos {
         $line{'title'} .= " / $seriestitle" if $seriestitle;
         $line{'title'} .= " / $volume"      if $volume;
     }
-    else {
-        $line{'title'} = "Deleted bibliographic notice, can't find title.";
-    }
 
     return \%line;
 }