From: Moritz Bunkus Date: Thu, 18 Dec 2014 12:04:05 +0000 (+0100) Subject: PriceTaxCalculator: für Items berechnete flüchtige Werte zurückgeben Teil 3 X-Git-Tag: release-3.2.0beta~183 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=1d34002dbf0a3ee90237f4373229a45ed890cd7f;p=kivitendo-erp.git PriceTaxCalculator: für Items berechnete flüchtige Werte zurückgeben Teil 3 Die items können nicht als Hash-Referenz gespeichert werden, weil das kaputt geht, wenn die Items noch nicht gespeichert wurden und damit noch keine ID besitzen. Daher Umstellung auf Array-Speicherung. Außerdem Anpassung des Testcases. --- diff --git a/SL/DB/Helper/PriceTaxCalculator.pm b/SL/DB/Helper/PriceTaxCalculator.pm index 4cf79e4a2..9048736da 100644 --- a/SL/DB/Helper/PriceTaxCalculator.pm +++ b/SL/DB/Helper/PriceTaxCalculator.pm @@ -37,7 +37,7 @@ sub calculate_prices_and_taxes { exchangerate => undef, is_sales => $self->can('customer') && $self->customer, is_invoice => (ref($self) =~ /Invoice/) || $params{invoice}, - items => { }, + items => [ ], ); _get_exchangerate($self, \%data, %params); @@ -149,12 +149,12 @@ sub _calculate_item { $data->{last_incex_chart_id} = $chart->id if $data->{is_sales}; - $data->{items}->{ $item->id } = { + push @{ $data->{items} }, { linetotal => $linetotal, linetotal_cost => $linetotal_cost, sellprice => $sellprice, tax_amount => $tax_amount, - taxkey => $taxkey, + taxkey_id => $taxkey->id, }; _dbg("CALCULATE! ${idx} i.qty " . $item->qty . " i.sellprice " . $item->sellprice . " sellprice $sellprice num_dec $num_dec taxamount $tax_amount " . @@ -351,12 +351,14 @@ The exchangerate used for the calculation. =item C -A hashref. For each line item this hashref contains an entry with -additional values that have been calculated for that item but that -aren't stored in the item object itself. These include C, -C, C, C and C. +An array reference. For each line item this array contains a hash ref +entry with additional values that have been calculated for that item +but that aren't stored in the item object itself. These include +C, C, C, C and +C. -The items are hashed by their IDs. +The items are stored in the same order the items are stored in the +object that L has been called on. =back diff --git a/t/db_helper/price_tax_calculator.t b/t/db_helper/price_tax_calculator.t index 8ec8d1c90..f7aa9ab0a 100644 --- a/t/db_helper/price_tax_calculator.t +++ b/t/db_helper/price_tax_calculator.t @@ -7,6 +7,7 @@ use utf8; use Carp; use Data::Dumper; +use List::MoreUtils qw(uniq); use Support::TestSetup; use Test::Exception; @@ -110,6 +111,8 @@ sub test_default_invoice_one_item_19_tax_not_included() { invoiceitems => [ $item ], ); + my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); + # sellprice 2.34 * qty 2.5 = 5.85 # 19%(5.85) = 1.1115; rounded = 1.11 # total rounded = 6.96 @@ -147,6 +150,14 @@ sub test_default_invoice_one_item_19_tax_not_included() { taxes => { $tax->chart_id => 1.11, }, + items => [ + { linetotal => 5.85, + linetotal_cost => 4.83, + sellprice => 2.34, + tax_amount => 1.1115, + taxkey_id => $taxkey->id, + }, + ], }, "${title}: calculated data"); } @@ -160,6 +171,9 @@ sub test_default_invoice_two_items_19_7_tax_not_included() { invoiceitems => [ $item1, $item2 ], ); + my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); + my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id); + # item 1: # sellprice 2.34 * qty 2.5 = 5.85 # 19%(5.85) = 1.1115; rounded = 1.11 @@ -217,6 +231,20 @@ sub test_default_invoice_two_items_19_7_tax_not_included() { $tax->chart_id => 1.11, $tax7->chart_id => 0.82, }, + items => [ + { linetotal => 5.85, + linetotal_cost => 4.83, + sellprice => 2.34, + tax_amount => 1.1115, + taxkey_id => $taxkey1->id, + }, + { linetotal => 11.66, + linetotal_cost => 6.57, + sellprice => 9.714, + tax_amount => 0.8162, + taxkey_id => $taxkey2->id, + }, + ], }, "${title}: calculated data"); } @@ -231,6 +259,8 @@ sub test_default_invoice_three_items_sellprice_rounding_discount() { invoiceitems => [ $item1, $item2, $item3 ], ); + my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3); + # this is how price_tax_calculator is implemented. It differs from # the way sales_order / invoice - forms are calculating: # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27 @@ -313,6 +343,26 @@ sub test_default_invoice_three_items_sellprice_rounding_discount() { taxes => { $tax->chart_id => 2.9, }, + items => [ + { linetotal => 5.27, + linetotal_cost => 1.93, + sellprice => 5.27, + tax_amount => 1.0013, + taxkey_id => $taxkeys{$item1->parts_id}->id, + }, + { linetotal => 5.22, + linetotal_cost => 1.93, + sellprice => 5.22, + tax_amount => 0.9918, + taxkey_id => $taxkeys{$item2->parts_id}->id, + }, + { linetotal => 4.75, + linetotal_cost => 1.93, + sellprice => 4.75, + tax_amount => 0.9025, + taxkey_id => $taxkeys{$item3->parts_id}->id, + } + ], }, "${title}: calculated data"); }