From: Moritz Bunkus Date: Tue, 9 Nov 2010 13:47:57 +0000 (+0100) Subject: Refactoring; kleine Erweiterungen für Rechnungsberechnung X-Git-Tag: release-2.6.3~61^2~7^2~1^2~2^2~75 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=2766beaf2abefb4bd973a69bcd42f46185c7cb63;p=kivitendo-erp.git Refactoring; kleine Erweiterungen für Rechnungsberechnung --- diff --git a/SL/DB/Helper/PriceTaxCalculator.pm b/SL/DB/Helper/PriceTaxCalculator.pm index ac262f187..9706d0258 100644 --- a/SL/DB/Helper/PriceTaxCalculator.pm +++ b/SL/DB/Helper/PriceTaxCalculator.pm @@ -7,95 +7,131 @@ our @EXPORT = qw(calculate_prices_and_taxes); use Carp; use List::Util qw(sum); +use SL::DB::Default; use SL::DB::PriceFactor; use SL::DB::Unit; sub calculate_prices_and_taxes { - my ($self) = @_; + my ($self, %params) = @_; my $is_sales = $self->can('customer') && $self->customer; + my $is_invoice = (ref($self) =~ /Invoice/) || $params{invoice}; my %units_by_name = map { ( $_->name => $_ ) } @{ SL::DB::Manager::Unit->get_all }; my %price_factors_by_id = map { ( $_->id => $_ ) } @{ SL::DB::Manager::PriceFactor->get_all }; my %taxes_by_chart_id = (); + my %amounts_by_chart_id = (); + + my %data = ( lastcost_total => 0, + invoicediff => 0, + units_by_name => \%units_by_name, + price_factors_by_id => \%price_factors_by_id, + taxes_by_chart_id => \%taxes_by_chart_id, + amounts_by_chart_id => \%amounts_by_chart_id, + exchangerate => undef, + ); + + if (($self->curr || '') ne SL::DB::Default->get_default_currency) { + $data{exchangerate} = $::form->check_exchangerate(\%::myconfig, $self->curr, $self->transdate, $is_sales ? 'buy' : 'sell'); + $data{exchangerate} ||= $params{exchangerate}; + } + $data{exchangerate} ||= 1; $self->netamount( 0); $self->marge_total(0); - my $lastcost_total = 0; my $idx = 0; - foreach my $item ($self->items) { $idx++; + _calculate_item($self, $item, $idx, \%data); + } - my $part_unit = $units_by_name{ $item->part->unit }; - my $item_unit = $units_by_name{ $item->unit }; + my $tax_sum = sum map { _round($_, 2) } values %taxes_by_chart_id; - croak("Undefined unit " . $item->part->unit) if !$part_unit; - croak("Undefined unit " . $item->unit) if !$item_unit; + $self->amount( _round($self->netamount + $tax_sum, 2)); + $self->netamount( _round($self->netamount, 2)); + $self->marge_percent($self->netamount ? ($self->netamount - $data{lastcost_total}) * 100 / $self->netamount : 0); - $item->base_qty($item_unit->convert_to($item->qty, $part_unit)); + return $self unless wantarray; + return ( self => $self, + taxes => \%taxes_by_chart_id, + amounts => \%amounts_by_chart_id, + ); +} - my $num_dec = num_decimal_places($item->sellprice); - my $discount = round($item->sellprice * ($item->discount || 0), $num_dec); - my $sellprice = round($item->sellprice - $discount, $num_dec); +sub _calculate_item { + my ($self, $item, $idx, $data) = @_; - $item->price_factor( ! $item->price_factor_obj ? 1 : ($item->price_factor_obj->factor || 1)); - $item->marge_price_factor(! $item->part->price_factor ? 1 : ($item->part->price_factor->factor || 1)); - my $linetotal = round($sellprice * $item->qty / $item->price_factor, 2); + my $part_unit = $data->{units_by_name}->{ $item->part->unit }; + my $item_unit = $data->{units_by_name}->{ $item->unit }; - if (!$linetotal) { - $item->marge_total( 0); - $item->marge_percent(0); + croak("Undefined unit " . $item->part->unit) if !$part_unit; + croak("Undefined unit " . $item->unit) if !$item_unit; - } else { - my $lastcost = ! ($item->lastcost * 1) ? ($item->part->lastcost || 0) : $item->lastcost; + $item->base_qty($item_unit->convert_to($item->qty, $part_unit)); - $item->marge_total( $linetotal - $lastcost / $item->marge_price_factor); - $item->marge_percent($item->marge_total * 100 / $linetotal); + my $num_dec = _num_decimal_places($item->sellprice); + my $discount = _round($item->sellprice * ($item->discount || 0), $num_dec); + my $sellprice = _round($item->sellprice - $discount, $num_dec); - $self->marge_total( $self->marge_total + $item->marge_total); - $lastcost_total += $lastcost; - } + $item->price_factor( ! $item->price_factor_obj ? 1 : ($item->price_factor_obj->factor || 1)); + $item->marge_price_factor(! $item->part->price_factor ? 1 : ($item->part->price_factor->factor || 1)); + my $linetotal = _round($sellprice * $item->qty / $item->price_factor, 2) * $data->{exchangerate}; + $linetotal = _round($linetotal, 2); - my $taxkey = $item->part->get_taxkey(date => $self->transdate, is_sales => $is_sales, taxzone => $self->taxzone_id); - my $tax_rate = $taxkey->tax->rate; - my $tax_amount = undef; + $data->{invoicediff} += $sellprice * $item->qty * $data->{exchangerate} / $item->price_factor - $linetotal; - if ($self->taxincluded) { - $tax_amount = $linetotal * $tax_rate / ($tax_rate + 1); - $sellprice = $sellprice / ($tax_rate + 1); + if (!$linetotal) { + $item->marge_total( 0); + $item->marge_percent(0); - } else { - $tax_amount = $linetotal * $tax_rate; - } + } else { + my $lastcost = ! ($item->lastcost * 1) ? ($item->part->lastcost || 0) : $item->lastcost; + + $item->marge_total( $linetotal - $lastcost / $item->marge_price_factor); + $item->marge_percent($item->marge_total * 100 / $linetotal); - $taxes_by_chart_id{ $taxkey->chart_id } ||= 0; - $taxes_by_chart_id{ $taxkey->chart_id } += $tax_amount; + $self->marge_total( $self->marge_total + $item->marge_total); + $data->{lastcost_total} += $lastcost; + } + + my $taxkey = $item->part->get_taxkey(date => $self->transdate, is_sales => $data->{is_sales}, taxzone => $self->taxzone_id); + my $tax_rate = $taxkey->tax->rate; + my $tax_amount = undef; - $self->netamount($self->netamount + $sellprice * $item->qty / $item->price_factor); + if ($self->taxincluded) { + $tax_amount = $linetotal * $tax_rate / ($tax_rate + 1); + $sellprice = $sellprice / ($tax_rate + 1); - $::lxdebug->message(0, "CALCULATE! ${idx} i.qty " . $item->qty . " i.sellprice " . $item->sellprice . " sellprice $sellprice taxamount $tax_amount " . - "i.linetotal $linetotal netamount " . $self->netamount . " marge_total " . $item->marge_total . " marge_percent " . $item->marge_percent); + } else { + $tax_amount = $linetotal * $tax_rate; } - my $tax_sum = sum map { round($_, 2) } values %taxes_by_chart_id; + $data->{taxes_by_chart_id}->{ $taxkey->chart_id } ||= 0; + $data->{taxes_by_chart_id}->{ $taxkey->chart_id } += $tax_amount; - $self->amount( round($self->netamount + $tax_sum, 2)); - $self->netamount( round($self->netamount, 2)); - $self->marge_percent($self->netamount ? ($self->netamount - $lastcost_total) * 100 / $self->netamount : 0); + $self->netamount($self->netamount + $sellprice * $item->qty / $item->price_factor); - return $self unless wantarray; - return ( self => $self, - taxes => \%taxes_by_chart_id, - ); + my $chart = $item->part->get_chart(type => $data->{is_sales} ? 'income' : 'expense', taxzone => $self->taxzone_id); + $data->{amounts_by_chart_id}->{$chart->id} += $linetotal; + + if ($data->{is_invoice}) { + if ($item->part->is_assembly) { + # process_assembly()... + } else { + # cogs... + } + } + + $::lxdebug->message(0, "CALCULATE! ${idx} i.qty " . $item->qty . " i.sellprice " . $item->sellprice . " sellprice $sellprice taxamount $tax_amount " . + "i.linetotal $linetotal netamount " . $self->netamount . " marge_total " . $item->marge_total . " marge_percent " . $item->marge_percent); } -sub round { +sub _round { return $::form->round_amount(@_); } -sub num_decimal_places { +sub _num_decimal_places { return length( (split(/\./, '' . shift, 2))[1] || '' ); } @@ -163,6 +199,11 @@ The object itself. A hash reference with the calculated taxes. The keys are chart IDs, the values the calculated taxes. +=item C + +A hash reference with the calculated amounts. The keys are chart IDs, +the values the calculated amounts. + =back =back