]> wagnertech.de Git - kivitendo-erp.git/commitdiff
GLTransaction:: add_chart_booking um get_active_taxkey erweitert
authorJan Büren <jan@kivitendo.de>
Mon, 14 Dec 2020 08:31:32 +0000 (09:31 +0100)
committerJan Büren <jan@kivitendo.de>
Mon, 14 Dec 2020 08:31:32 +0000 (09:31 +0100)
Der Aufruf von add_chart_booking muss sich nicht mehr
darum kümmern welcher Steuerschlüssel gesetzt sein muss, wenn
er einfach nur den vorkonfigurierten Steuerschlüssel setzen will.
Dementsprechend ist param{tax_id} kein Pflichtparameter mehr.
POD ergänzt und mit Testfällen abgesichert (19% / 16% Fall).

SL/DB/GLTransaction.pm
t/gl/gl.t

index 25d4de7e3f7fd44f8d4ee8300bcea53180cd84be..50b29c0ff893be81c2b6a1dee1fe8c24bcc03175 100644 (file)
@@ -129,8 +129,15 @@ sub add_chart_booking {
   croak t8('You cannot use a negative amount with debit/credit!') if $amount < 0;
 
   require SL::DB::Tax;
-  my $tax = SL::DB::Manager::Tax->find_by(id => $params{tax_id})
-    // croak "Can't find tax with id " . $params{tax_id};
+
+  my $ct        = $chart->get_active_taxkey($self->deliverydate // $self->transdate);
+  my $chart_tax = ref $ct eq 'SL::DB::TaxKey' ? $ct->tax : undef;
+
+  my $tax = defined($params{tax_id})        ? SL::DB::Manager::Tax->find_by(id => $params{tax_id}) # 1. user param
+          : ref $chart_tax eq 'SL::DB::Tax' ? $chart_tax                                           # automatic tax
+          : SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.00);                              # no tax
+
+  die "No valid tax found. User input:" . $params{tax_id} unless ref $tax eq 'SL::DB::Tax';
 
   if ( $tax and $tax->rate != 0 ) {
     ($netamount, $taxamount) = Form->calculate_tax($amount, $tax->rate, $self->taxincluded, $dec);
@@ -283,6 +290,9 @@ Example of posting a GL transaction from scratch:
 Adds an acc_trans entry to an existing GL transaction, depending on the tax it
 will also automatically create the tax entry. The GL transaction already needs
 to have certain values, e.g. transdate, taxincluded, ...
+Tax can be either set via the param tax_id or it will be set automatically
+depending on the chart configuration. If not set and no configuration is found
+no tax entry will be created (taxkey 0).
 
 Mandatory params are
 
@@ -290,8 +300,6 @@ Mandatory params are
 
 =item * chart as an RDBO object
 
-=item * tax_id
-
 =item * either debit OR credit (positive values)
 
 =back
index 8cc0f6afd9cb5621fe5c803cab1532e86f3a7ab3..ab08ee599ccb66bc652a9efe0fe17ddedf3a00e6 100644 (file)
--- a/t/gl/gl.t
+++ b/t/gl/gl.t
@@ -1,5 +1,5 @@
 use strict;
-use Test::More tests => 4;
+use Test::More tests => 8;
 
 use lib 't';
 use Support::TestSetup;
@@ -257,6 +257,124 @@ is_deeply(&get_account_balances,
           "chart balances ok"
          );
 
+note "testing automatic tax 19%";
+
+my $gl_transaction_7 = SL::DB::GLTransaction->new(
+  reference   => 'betriebsbedarf tax not included',
+  description => 'bar',
+  taxincluded => 0,
+  transdate   => DateTime->new(year => 2019, month => 12, day => 30),
+);
+
+$gl_transaction_7->add_chart_booking(%{$_}) foreach (
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+    },
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+    },
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+      tax_id => $tax_0->id,
+    },
+    {
+      chart  => $cash,
+      credit => 338,
+    },
+);
+$gl_transaction_7->post;
+
+is(SL::DB::Manager::GLTransaction->get_all_count(), 9, "gl transactions created ok");
+is_deeply(&get_account_balances,
+          [
+            {
+              'accno' => '1000',
+              'sum' => '1328.14000'
+            },
+            {
+              'accno' => '1200',
+              'sum' => '-100.00000'
+            },
+            {
+              'accno' => '1571',
+              'sum' => '-14.00000'
+            },
+            {
+              'accno' => '1576',
+              'sum' => '-114.02000'
+            },
+            {
+              'accno' => '4980',
+              'sum' => '-1100.12000'
+            }
+          ],
+          "chart balances ok"
+         );
+
+note "testing automatic tax 16%";
+
+my $gl_transaction_8 = SL::DB::GLTransaction->new(
+  reference   => 'betriebsbedarf tax not included',
+  description => 'bar',
+  taxincluded => 0,
+  transdate   => DateTime->new(year => 2020, month => 12, day => 31),
+);
+
+$gl_transaction_8->add_chart_booking(%{$_}) foreach (
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+    },
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+    },
+    {
+      chart  => $betriebsbedarf,
+      debit  => 100,
+      tax_id => $tax_0->id,
+    },
+    {
+      chart  => $cash,
+      credit => 332,
+    },
+);
+$gl_transaction_8->post;
+
+is(SL::DB::Manager::GLTransaction->get_all_count(), 10, "gl transactions created ok");
+is_deeply(&get_account_balances,
+          [
+            {
+              'accno' => '1000',
+              'sum' => '1660.14000'
+            },
+            {
+              'accno' => '1200',
+              'sum' => '-100.00000'
+            },
+            {
+              'accno' => '1571',
+              'sum' => '-14.00000'
+            },
+            {
+              'accno' => '1575',
+              'sum' => '-32.00000'
+            },
+            {
+              'accno' => '1576',
+              'sum' => '-114.02000'
+            },
+            {
+              'accno' => '4980',
+              'sum' => '-1400.12000'
+            }
+          ],
+          "chart balances ok"
+         );
+
 done_testing;
 clear_up();