added roundings for periodic invoices
authorRolf Fluehmann <rolf.fluehmann@revamp-it.ch>
Thu, 30 Jun 2016 16:18:30 +0000 (18:18 +0200)
committerRolf Fluehmann <rolf.fluehmann@revamp-it.ch>
Thu, 30 Jun 2016 16:42:42 +0000 (18:42 +0200)
SL/DB/Helper/PriceTaxCalculator.pm
SL/DB/Invoice.pm

index 44c6511..6afb45a 100644 (file)
@@ -57,7 +57,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 {
@@ -185,10 +185,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);
 }
 
index 9aa17ad..99c2e11 100644 (file)
@@ -260,6 +260,8 @@ sub post {
     $self->_post_add_acctrans({ $params{ar_id} => $self->amount * -1 });
 
     $self->_post_update_allocated($data{allocated});
+
+    $self->_post_book_rounding($data{rounding});
   };
 
   if ($self->db->in_transaction) {
@@ -296,6 +298,26 @@ sub _post_add_acctrans {
   }
 }
 
+sub _post_book_rounding {
+  my ($self, $rounding) = @_;
+
+  my $tax_id = SL::DB::Manager::Tax->find_by(taxkey => 0)->id;
+  my $rnd_accno = $rounding == 0 ? 0
+                : $rounding > 0  ? SL::DB::Default->get->rndgain_accno_id
+                :                  SL::DB::Default->get->rndloss_accno_id
+  ;
+  if ($rnd_accno != 0) {
+    SL::DB::AccTransaction->new(trans_id   => $self->id,
+                                chart_id   => $rnd_accno,
+                                amount     => $rounding,
+                                tax_id     => $tax_id,
+                                taxkey     => 0,
+                                project_id => $self->globalproject_id,
+                                transdate  => $self->transdate,
+                                chart_link => $rnd_accno)->save;
+  }
+}
+
 sub add_ar_amount_row {
   my ($self, %params ) = @_;