From abd56be11a58421b1d9401e812a83d28832b939a Mon Sep 17 00:00:00 2001 From: Rolf Fluehmann Date: Thu, 30 Jun 2016 18:18:30 +0200 Subject: [PATCH] added roundings for periodic invoices --- SL/DB/Helper/PriceTaxCalculator.pm | 9 ++++++--- SL/DB/Invoice.pm | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/SL/DB/Helper/PriceTaxCalculator.pm b/SL/DB/Helper/PriceTaxCalculator.pm index 44c651196..6afb45a4f 100644 --- a/SL/DB/Helper/PriceTaxCalculator.pm +++ b/SL/DB/Helper/PriceTaxCalculator.pm @@ -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); } diff --git a/SL/DB/Invoice.pm b/SL/DB/Invoice.pm index 9aa17adec..99c2e11ad 100644 --- a/SL/DB/Invoice.pm +++ b/SL/DB/Invoice.pm @@ -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 ) = @_; -- 2.20.1