Update .htaccess for Apache 2.4 to avoid mod_access_compat dependency
[kivitendo-erp.git] / SL / DB / Helper / PriceTaxCalculator.pm
index 44c6511..9a27184 100644 (file)
@@ -40,7 +40,15 @@ sub calculate_prices_and_taxes {
                items               => [ ],
              );
 
-  _get_exchangerate($self, \%data, %params);
+  # set exchangerate in $data>{exchangerate}
+  if ( ref($self) eq 'SL::DB::Order' ) {
+    # orders store amount in the order currency
+    $data{exchangerate} = 1;
+  } else {
+    # invoices store amount in the default currency
+    _get_exchangerate($self, \%data, %params);
+    # $data{exchangerate} = $self->exchangerate; # untested alternative for setting exchangerate
+  };
 
   $self->netamount(  0);
   $self->marge_total(0);
@@ -57,7 +65,7 @@ sub calculate_prices_and_taxes {
 
   return $self unless wantarray;
 
-  return map { ($_ => $data{$_}) } qw(taxes amounts amounts_cogs allocated exchangerate assembly_items items);
+  return map { ($_ => $data{$_}) } qw(taxes amounts amounts_cogs allocated exchangerate assembly_items items rounding);
 }
 
 sub _get_exchangerate {
@@ -104,7 +112,7 @@ sub _calculate_item {
     $item->marge_percent(0);
 
   } else {
-    my $lastcost       = ! ($item->lastcost * 1) ? ($part->lastcost || 0) : $item->lastcost;
+    my $lastcost       = !(($item->lastcost // 0) * 1) ? ($part->lastcost || 0) : $item->lastcost;
     $linetotal_cost    = _round($lastcost * $item->qty / $item->marge_price_factor, 2);
 
     $item->marge_total(  $linetotal - $linetotal_cost);
@@ -185,10 +193,13 @@ sub _calculate_amounts {
   _dbg("Sna " . $self->netamount . " idiff " . $data->{invoicediff} . " tdiff ${tax_diff}");
 
   my $tax              = sum values %{ $data->{taxes} };
-  $data->{arap_amount} = $netamount + $tax;
+  $amount              = $netamount + $tax;
+  my $grossamount      = _round($amount, 2, 1);
+  $data->{rounding}    = _round($grossamount - $amount, 2);
+  $data->{arap_amount} = $grossamount;
 
   $self->netamount(    $netamount);
-  $self->amount(       $netamount + $tax);
+  $self->amount(       $grossamount);
   $self->marge_percent($self->netamount ? ($self->netamount - $data->{lastcost_total}) * 100 / $self->netamount : 0);
 }