X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=t%2Fdb_helper%2Fprice_tax_calculator.t;h=c8884018e596815ebbe05abe51b9e6639e2b1913;hb=c092c963921436b418f42ed73bece53878a61bf0;hp=4bf9ab8635ed306c6df434450910ec50422d639c;hpb=3f9f158f2b1adbb6ad47906d16776331d7cf9993;p=kivitendo-erp.git diff --git a/t/db_helper/price_tax_calculator.t b/t/db_helper/price_tax_calculator.t index 4bf9ab863..c8884018e 100644 --- a/t/db_helper/price_tax_calculator.t +++ b/t/db_helper/price_tax_calculator.t @@ -7,27 +7,39 @@ use utf8; use Carp; use Data::Dumper; +use List::MoreUtils qw(uniq); use Support::TestSetup; use Test::Exception; +use SL::Dev::ALL qw(:ALL); use SL::DB::Buchungsgruppe; use SL::DB::Currency; use SL::DB::Customer; +use SL::DB::DeliveryOrder; use SL::DB::Employee; use SL::DB::Invoice; +use SL::DB::Order; use SL::DB::Part; use SL::DB::Unit; +use SL::DB::TaxZone; -my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone); +my ($customer, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone); + +sub clear_up { + SL::DB::Manager::Order->delete_all(all => 1); + SL::DB::Manager::DeliveryOrder->delete_all(all => 1); + SL::DB::Manager::InvoiceItem->delete_all(all => 1); + SL::DB::Manager::Invoice->delete_all(all => 1); + SL::DB::Manager::Part->delete_all(all => 1); + SL::DB::Manager::Customer->delete_all(all => 1); +}; sub reset_state { my %params = @_; $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax); - SL::DB::Manager::Invoice->delete_all(all => 1); - SL::DB::Manager::Part->delete_all(all => 1); - SL::DB::Manager::Customer->delete_all(all => 1); + clear_up(); $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group"; $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%"; @@ -37,16 +49,14 @@ sub reset_state { $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%"; $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone"; - $currency_id = $::instance_conf->get_currency_id; - - $customer = SL::DB::Customer->new( + $customer = new_customer( name => 'Test Customer', - currency_id => $currency_id, + taxzone_id => $taxzone->id, %{ $params{customer} } )->save; @parts = (); - push @parts, SL::DB::Part->new( + push @parts, new_part( partnumber => 'T4254', description => 'Fourty-two fifty-four', lastcost => 1.93, @@ -56,7 +66,7 @@ sub reset_state { %{ $params{part1} } )->save; - push @parts, SL::DB::Part->new( + push @parts, new_part( partnumber => 'T0815', description => 'Zero EIGHT fifteeN @ 7%', lastcost => 5.473, @@ -65,21 +75,24 @@ sub reset_state { unit => $unit->name, %{ $params{part2} } )->save; + + push @parts, new_part( + partnumber => 'T888', + description => 'Triple 8', + lastcost => 0, + sellprice => 0.6, + buchungsgruppen_id => $buchungsgruppe->id, + unit => $unit->name, + %{ $params{part3} } + )->save; + } sub new_invoice { my %params = @_; - return SL::DB::Invoice->new( - customer_id => $customer->id, - currency_id => $currency_id, - employee_id => $employee->id, - salesman_id => $employee->id, - gldate => DateTime->today_local->to_kivitendo, + return create_sales_invoice( taxzone_id => $taxzone->id, - transdate => DateTime->today_local->to_kivitendo, - invoice => 1, - type => 'invoice', %params, ); } @@ -89,12 +102,8 @@ sub new_item { my $part = delete($params{part}) || $parts[0]; - return SL::DB::InvoiceItem->new( - parts_id => $part->id, - lastcost => $part->lastcost, - sellprice => $part->sellprice, - description => $part->description, - unit => $part->unit, + return create_invoice_item( + part => $part, %params, ); } @@ -102,12 +111,14 @@ sub new_item { sub test_default_invoice_one_item_19_tax_not_included() { reset_state(); - my $item = new_item(qty => 2.5); + my $item = new_item(qty => 2.5); my $invoice = new_invoice( taxincluded => 0, 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 @@ -145,6 +156,15 @@ 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, + }, + ], + rounding => 0, }, "${title}: calculated data"); } @@ -158,6 +178,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 @@ -215,6 +238,21 @@ 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, + }, + ], + rounding => 0, }, "${title}: calculated data"); } @@ -229,6 +267,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 @@ -311,6 +351,91 @@ 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, + } + ], + rounding => 0, + }, "${title}: calculated data"); +} + +sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() { + reset_state(); + + my $item = new_item(qty => 6, part => $parts[2], discount => 0.03); + my $invoice = new_invoice( + taxincluded => 0, + invoiceitems => [ $item ], + ); + + my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item); + + # PTC and ar form calculate linetotal differently: + # 6 parts for 0.60 with 3% discount + # + # ar form: + # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49 + # total = 3.49 + 0.66 = 4.15 + # + # PTC: + # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02 + # sellprice = sellprice 0.60 - discount 0.02 = 0.58 + # linetotal = sellprice 0.58 * qty 6 = 3.48 + # 19%(3.48) = 0.6612; rounded = 0.66 + # total rounded = 3.48 + 0.66 = 4.14 + + my $title = 'default invoice, one item, sellprice, rounding, discount'; + my %data = $invoice->calculate_prices_and_taxes; + + is($invoice->netamount, 3.48, "${title}: netamount"); + + is($invoice->amount, 4.14, "${title}: amount"); + + is($invoice->marge_total, 3.48, "${title}: marge_total"); + is($invoice->marge_percent, 100, "${title}: marge_percent"); + + is_deeply(\%data, { + allocated => {}, + amounts => { + $buchungsgruppe->income_accno_id($taxzone) => { + amount => 3.48, + tax_id => $tax->id, + taxkey => 3, + }, + }, + amounts_cogs => {}, + assembly_items => [ + [], + ], + exchangerate => 1, + taxes => { + $tax->chart_id => 0.66, + }, + items => [ + { linetotal => 3.48, + linetotal_cost => 0, + sellprice => 0.58, + tax_amount => 0.6612, + taxkey_id => $taxkeys{$item->parts_id}->id, + }, + ], + rounding => 0, }, "${title}: calculated data"); } @@ -319,5 +444,7 @@ Support::TestSetup::login(); test_default_invoice_one_item_19_tax_not_included(); test_default_invoice_two_items_19_7_tax_not_included(); test_default_invoice_three_items_sellprice_rounding_discount(); +test_default_invoice_one_item_19_tax_not_included_rounding_discount(); +clear_up(); done_testing();