PriceTaxCalculator: für Items berechnete flüchtige Werte zurückgeben Teil 3
[kivitendo-erp.git] / t / db_helper / price_tax_calculator.t
index e8ee4ac..f7aa9ab 100644 (file)
@@ -7,6 +7,7 @@ use utf8;
 
 use Carp;
 use Data::Dumper;
+use List::MoreUtils qw(uniq);
 use Support::TestSetup;
 use Test::Exception;
 
@@ -17,8 +18,9 @@ use SL::DB::Employee;
 use SL::DB::Invoice;
 use SL::DB::Part;
 use SL::DB::Unit;
+use SL::DB::TaxZone;
 
-my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7);
+my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
 
 sub reset_state {
   my %params = @_;
@@ -35,12 +37,14 @@ sub reset_state {
   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
   $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(
     name        => 'Test Customer',
     currency_id => $currency_id,
+    taxzone_id  => $taxzone->id,
     %{ $params{customer} }
   )->save;
 
@@ -75,7 +79,7 @@ sub new_invoice {
     employee_id => $employee->id,
     salesman_id => $employee->id,
     gldate      => DateTime->today_local->to_kivitendo,
-    taxzone_id  => 0,
+    taxzone_id  => $taxzone->id,
     transdate   => DateTime->today_local->to_kivitendo,
     invoice     => 1,
     type        => 'invoice',
@@ -107,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
@@ -128,22 +134,30 @@ sub test_default_invoice_one_item_19_tax_not_included() {
   is($invoice->marge_percent,   17.4358974358974, "${title}: marge_percent");
 
   is_deeply(\%data, {
-    allocated                            => {},
-    amounts                              => {
-      $buchungsgruppe->income_accno_id_0 => {
-        amount                           => 5.85,
-        tax_id                           => $tax->id,
-        taxkey                           => 3,
+    allocated                                    => {},
+    amounts                                      => {
+      $buchungsgruppe->income_accno_id($taxzone) => {
+        amount                                   => 5.85,
+        tax_id                                   => $tax->id,
+        taxkey                                   => 3,
       },
     },
-    amounts_cogs                         => {},
-    assembly_items                       => [
+    amounts_cogs                                 => {},
+    assembly_items                               => [
       [],
     ],
-    exchangerate                         => 1,
-    taxes                                => {
-      $tax->chart_id                     => 1.11,
+    exchangerate                                 => 1,
+    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");
 }
 
@@ -157,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
@@ -192,28 +209,160 @@ sub test_default_invoice_two_items_19_7_tax_not_included() {
   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
 
   is_deeply(\%data, {
-    allocated                             => {},
-    amounts                               => {
-      $buchungsgruppe->income_accno_id_0  => {
-        amount                            => 5.85,
-        tax_id                            => $tax->id,
-        taxkey                            => 3,
+    allocated                                     => {},
+    amounts                                       => {
+      $buchungsgruppe->income_accno_id($taxzone)  => {
+        amount                                    => 5.85,
+        tax_id                                    => $tax->id,
+        taxkey                                    => 3,
       },
-      $buchungsgruppe7->income_accno_id_0 => {
-        amount                            => 11.66,
-        tax_id                            => $tax7->id,
-        taxkey                            => 2,
+      $buchungsgruppe7->income_accno_id($taxzone) => {
+        amount                                    => 11.66,
+        tax_id                                    => $tax7->id,
+        taxkey                                    => 2,
       },
     },
-    amounts_cogs                          => {},
-    assembly_items                        => [
+    amounts_cogs                                  => {},
+    assembly_items                                => [
       [], [],
     ],
-    exchangerate                          => 1,
-    taxes                                 => {
-      $tax->chart_id                      => 1.11,
-      $tax7->chart_id                     => 0.82,
+    exchangerate                                  => 1,
+    taxes                                         => {
+      $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");
+}
+
+sub test_default_invoice_three_items_sellprice_rounding_discount() {
+  reset_state();
+
+  my $item1   = new_item(qty => 1, sellprice => 5.55, discount => .05);
+  my $item2   = new_item(qty => 1, sellprice => 5.50, discount => .05);
+  my $item3   = new_item(qty => 1, sellprice => 5.00, discount => .05);
+  my $invoice = new_invoice(
+    taxincluded  => 0,
+    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
+  # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
+  # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
+  # ...
+
+  # item 1:
+  # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
+  # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
+  # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
+  # 19%(5.27) = 1.0013; rounded = 1.00
+  # total rounded = 6.27
+
+  # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
+  # line marge_total = 3.34
+  # line marge_percent = 63.3776091081594
+
+  # item 2:
+  # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
+  # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
+  # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
+  # 19%(5.22) = 0.9918; rounded = 0.99
+  # total rounded = 6.21
+
+  # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
+  # line marge_total = 5.22 - 1.93 = 3.29
+  # line marge_percent = 3.29/5.22 = 0.630268199233716
+
+  # item 3:
+  # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
+  # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
+  # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
+  # 19%(4.75) = 0.9025; rounded = 0.90
+  # total rounded = 5.65
+
+  # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
+  # line marge_total = 2.82
+  # line marge_percent = 59.3684210526316
+
+  my $title = 'default invoice, three items, sellprice, rounding, discount';
+  my %data  = $invoice->calculate_prices_and_taxes;
+
+  is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
+  is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
+  is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
+
+  is($item2->marge_total,        3.29,               "${title}: item2 marge_total");
+  is($item2->marge_percent,      63.0268199233716,  "${title}: item2 marge_percent");
+  is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
+
+  is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
+  is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
+  is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
+
+  is($invoice->netamount,        5.27 + 5.22 + 4.75, "${title}: netamount");
+
+  # 6.27 + 6.21 + 5.65 = 18.13
+  # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
+  #is($invoice->amount,           6.27 + 6.21 + 5.65, "${title}: amount");
+  is($invoice->amount,           18.14,              "${title}: amount");
+
+  is($invoice->marge_total,      3.34 + 3.29 + 2.82, "${title}: marge_total");
+  is($invoice->marge_percent,    62.007874015748,    "${title}: marge_percent");
+
+  is_deeply(\%data, {
+    allocated                                    => {},
+    amounts                                      => {
+      $buchungsgruppe->income_accno_id($taxzone) => {
+        amount                                   => 15.24,
+        tax_id                                   => $tax->id,
+        taxkey                                   => 3,
+      },
+    },
+    amounts_cogs                                 => {},
+    assembly_items                               => [
+      [], [], [],
+    ],
+    exchangerate                                 => 1,
+    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");
 }
 
@@ -221,5 +370,6 @@ 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();
 
 done_testing();