tax_id in acc_trans
[kivitendo-erp.git] / SL / GL.pm
index e1cb7bc..80c5fda 100644 (file)
--- a/SL/GL.pm
+++ b/SL/GL.pm
@@ -39,6 +39,7 @@
 package GL;
 
 use Data::Dumper;
+use SL::DATEV qw(:CONSTANTS);
 use SL::DBUtils;
 
 use strict;
@@ -158,11 +159,11 @@ sub post_transaction {
     if ($amount != 0) {
       $query =
         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
-                                  source, memo, project_id, taxkey, ob_transaction, cb_transaction)
+                                  source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id)
            VALUES (?, (SELECT id FROM chart WHERE accno = ?),
-                   ?, ?, ?, ?, ?, ?, ?, ?)|;
+                   ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
       @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
-                 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f');
+                 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}));
       do_query($form, $dbh, $query, @values);
     }
 
@@ -170,12 +171,12 @@ sub post_transaction {
       # add taxentry
       $query =
         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
-                                  source, memo, project_id, taxkey)
+                                  source, memo, project_id, taxkey, tax_id)
            VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
-                   ?, ?, ?, ?, ?, ?)|;
+                   ?, ?, ?, ?, ?, ?, ?)|;
       @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
                  $tax, conv_date($form->{transdate}), $form->{"source_$i"},
-                 $form->{"memo_$i"}, $project_id, $taxkey);
+                 $form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}));
       do_query($form, $dbh, $query, @values);
     }
   }
@@ -184,6 +185,27 @@ sub post_transaction {
     do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
   }
 
+  # safety check datev export
+  if ($::instance_conf->get_datev_check_on_gl_transaction) {
+    my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
+    $transdate  ||= DateTime->today;
+
+    my $datev = SL::DATEV->new(
+      exporttype => DATEV_ET_BUCHUNGEN,
+      format     => DATEV_FORMAT_KNE,
+      dbh        => $dbh,
+      from       => $transdate,
+      to         => $transdate,
+    );
+
+    $datev->export;
+
+    if ($datev->errors) {
+      $dbh->rollback;
+      die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
+    }
+  }
+
   # commit and redirect
   my $rc = $dbh->commit;
   $dbh->disconnect;
@@ -720,7 +742,7 @@ sub storno {
   $storno_row->{storno}    = 't';
   $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
 
-  delete @$storno_row{qw(itime mtime)};
+  delete @$storno_row{qw(itime mtime gldate)};
 
   $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
   do_query($form, $dbh, $query, (values %$storno_row));
@@ -733,7 +755,7 @@ sub storno {
   my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
 
   for my $row (@$rowref) {
-    delete @$row{qw(itime mtime acc_trans_id)};
+    delete @$row{qw(itime mtime acc_trans_id gldate)};
     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
     $row->{trans_id}   = $new_id;
     $row->{amount}    *= -1;