Verbrauchsbericht: Lager->Berichte->Lagerentnahme
[kivitendo-erp.git] / SL / DATEV.pm
index c1c56c5..be3ec95 100644 (file)
@@ -31,6 +31,7 @@ use strict;
 
 use SL::DBUtils;
 use SL::DATEV::KNEFile;
+use SL::DB;
 
 use Data::Dumper;
 use DateTime;
@@ -233,7 +234,7 @@ sub dbh {
     $self->{provided_dbh} = 1;
   }
 
-  $self->{dbh} ||= $::form->get_standard_dbh;
+  $self->{dbh} ||= SL::DB->client->dbh;
 }
 
 sub provided_dbh {
@@ -285,14 +286,15 @@ sub get_datev_stamm {
 sub save_datev_stamm {
   my ($self, $data) = @_;
 
-  do_query($::form, $self->dbh, 'DELETE FROM datev');
+  SL::DB->client->with_transaction(sub {
+    do_query($::form, $self->dbh, 'DELETE FROM datev');
 
-  my @columns = qw(beraternr beratername dfvkz mandantennr datentraegernr abrechnungsnr);
+    my @columns = qw(beraternr beratername dfvkz mandantennr datentraegernr abrechnungsnr);
 
-  my $query = "INSERT INTO datev (" . join(', ', @columns) . ") VALUES (" . join(', ', ('?') x @columns) . ")";
-  do_query($::form, $self->dbh, $query, map { $data->{$_} } @columns);
-
-  $self->dbh->commit unless $self->provided_dbh;
+    my $query = "INSERT INTO datev (" . join(', ', @columns) . ") VALUES (" . join(', ', ('?') x @columns) . ")";
+    do_query($::form, $self->dbh, $query, map { $data->{$_} } @columns);
+    1;
+  }) or do { die SL::DB->client->error };
 }
 
 sub export {
@@ -377,7 +379,8 @@ sub _get_transactions {
          ct.name, ct.ustid,
          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, ac.chart_link AS link,
          ar.invoice,
-         t.rate AS taxrate
+         t.rate AS taxrate,
+         'ar' as table
        FROM acc_trans ac
        LEFT JOIN ar          ON (ac.trans_id    = ar.id)
        LEFT JOIN customer ct ON (ar.customer_id = ct.id)
@@ -395,7 +398,8 @@ sub _get_transactions {
          ct.name,ct.ustid,
          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, ac.chart_link AS link,
          ap.invoice,
-         t.rate AS taxrate
+         t.rate AS taxrate,
+         'ap' as table
        FROM acc_trans ac
        LEFT JOIN ap        ON (ac.trans_id  = ap.id)
        LEFT JOIN vendor ct ON (ap.vendor_id = ct.id)
@@ -413,7 +417,8 @@ sub _get_transactions {
          gl.description AS name, NULL as ustid,
          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, ac.chart_link AS link,
          FALSE AS invoice,
-         t.rate AS taxrate
+         t.rate AS taxrate,
+         'gl' as table
        FROM acc_trans ac
        LEFT JOIN gl      ON (ac.trans_id  = gl.id)
        LEFT JOIN chart c ON (ac.chart_id  = c.id)
@@ -539,7 +544,9 @@ sub _get_transactions {
       # Problem: we can't distinguish between AR and AP and normal invoices via boolean "invoice"
       # for AR and AP transaction exit the loop as soon as an AR or AP account is found
       # there must be only one AR or AP chart in the booking
-      if ( $trans->[$j]->{'link'} eq 'AR' or $trans->[$j]->{'link'} eq 'AP') {
+      # since it is possible to do this kind of things with GL too, make sure those don't get aborted in case someone
+      # manually pays an invoice in GL.
+      if ($trans->[$j]->{table} ne 'gl' and ($trans->[$j]->{'link'} eq 'AR' or $trans->[$j]->{'link'} eq 'AP')) {
         $notsplitindex = $j;   # position in booking with highest amount
         $absumsatz     = $trans->[$j]->{'amount'};
         last;
@@ -958,7 +965,7 @@ sub kne_buchungsexport {
   print(EV $ev_header);
 
   foreach my $file (@ed_versionset) {
-    print(EV $ed_versionset[$file]);
+    print(EV $file);
   }
   close(EV);
   ###
@@ -1232,6 +1239,10 @@ This is a list of attributes set in either the C<new> or a method of the same na
 Set a database handle to use in the process. This allows for an export to be
 done on a transaction in progress without committing first.
 
+Note: If you don't want this code to commit, simply providing a dbh is not
+enough enymore. You'll have to wrap the call into a transaction yourself, so
+that the internal transaction does not commit.
+
 =item exporttype
 
 See L<CONSTANTS> for possible values. This MUST be set before export is called.