]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DATEV.pm
DATEV-Export nicht beim ersten Fehler abbrechen, sondern eine Liste aller Fehler...
[mfinanz.git] / SL / DATEV.pm
index 28fc018e60c075d88367dc4b324a5898b962f671..384ee3f9865f1ff822a4a90ba0ae37f6801277cc 100644 (file)
@@ -26,6 +26,8 @@
 
 package DATEV;
 
+use SL::DBUtils;
+
 use Data::Dumper;
 
 sub get_datev_stamm {
@@ -214,134 +216,151 @@ sub get_dates {
   return $fromto;
 }
 
-sub get_transactions {
+sub _get_transactions {
   $main::lxdebug->enter_sub();
 
-  my ($myconfig, $form, $fromto) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  $fromto =~ s/transdate/ac\.transdate/g;
-
-  $query = qq|SELECT id, taxkey, rate FROM tax|;
-  $sth   = $dbh->prepare($query);
-  $sth->execute || $form->dberror($query);
-
-  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    $taxes{ $ref->{id} } = $ref->{rate};
-  }
-
-  $sth->finish();
-
-  $query =
-    qq|SELECT ac.oid, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey, ar.invnumber, ar.duedate, ar.amount as umsatz,
-              ct.name, c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey FROM acc_trans ac,ar ar, customer ct,
-              chart c LEFT JOIN tax t ON
-                 (t.chart_id=c.id)WHERE $fromto AND ac.trans_id=ar.id AND ac.trans_id=ar.id
-              AND ar.customer_id=ct.id AND ac.chart_id=c.id
-              UNION ALL
-              SELECT ac.oid, ac.transdate, ac.trans_id,ap.id, ac.amount, ac.taxkey, ap.invnumber, ap.duedate, ap.amount as umsatz,
-              ct.name, c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey FROM acc_trans ac, ap ap, vendor ct, chart c LEFT JOIN tax t ON
-                 (t.chart_id=c.id)
-              WHERE $fromto AND ac.trans_id=ap.id AND ap.vendor_id=ct.id AND ac.chart_id=c.id
-              UNION ALL
-              SELECT ac.oid, ac.transdate, ac.trans_id,gl.id, ac.amount, ac.taxkey, gl.reference AS invnumber, gl.transdate AS duedate, ac.amount as umsatz,
-              gl.description AS name, c.accno,  c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey FROM acc_trans ac, gl gl,
-              chart c LEFT JOIN tax t ON
-                 (t.chart_id=c.id) WHERE $fromto AND ac.trans_id=gl.id AND ac.chart_id=c.id
-              ORDER BY trans_id, oid|;
-
-  $sth = $dbh->prepare($query);
-  $sth->execute || $form->dberror($query);
-  $i = 0;
-  $g = 0;
+  my $fromto   =  shift;
+
+  my $myconfig =  \%main::myconfig;
+  my $form     =  $main::form;
+
+  my $dbh      =  $form->get_standard_dbh($myconfig);
+
+  my @errors   = ();
+
+  $fromto      =~ s/transdate/ac\.transdate/g;
+
+  my %taxes    =  selectall_as_map($form, $dbh, qq|SELECT id, rate FROM tax|, 'id', 'rate');
+
+  my $query    =
+    qq|SELECT ac.oid, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey,
+         ar.invnumber, ar.duedate, ar.amount as umsatz,
+         ct.name,
+         c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id,
+         t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey
+       FROM acc_trans ac,ar ar, customer ct, chart c
+       LEFT JOIN tax t ON (t.chart_id = c.id)
+       WHERE $fromto
+         AND (ac.trans_id = ar.id)
+         AND (ac.trans_id = ar.id)
+         AND (ar.customer_id = ct.id)
+         AND (ac.chart_id = c.id)
+
+       UNION ALL
+
+       SELECT ac.oid, ac.transdate, ac.trans_id,ap.id, ac.amount, ac.taxkey,
+         ap.invnumber, ap.duedate, ap.amount as umsatz,
+         ct.name,
+         c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id,
+         t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey
+       FROM acc_trans ac, ap ap, vendor ct, chart c
+       LEFT JOIN tax t ON (t.chart_id = c.id)
+       WHERE $fromto
+         AND (ac.trans_id = ap.id)
+         AND (ap.vendor_id = ct.id)
+         AND (ac.chart_id = c.id)
+
+       UNION ALL
+
+       SELECT ac.oid, ac.transdate, ac.trans_id,gl.id, ac.amount, ac.taxkey,
+         gl.reference AS invnumber, gl.transdate AS duedate, ac.amount as umsatz,
+         gl.description AS name,
+         c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id,
+         t.chart_id, t.rate, t.id AS taxid, t.taxkey AS taxtaxkey
+       FROM acc_trans ac, gl gl, chart c
+       LEFT JOIN tax t ON (t.chart_id = c.id)
+       WHERE $fromto
+         AND (ac.trans_id = gl.id)
+         AND (ac.chart_id = c.id)
+
+       ORDER BY trans_id, oid|;
+
+  my $sth     = prepare_execute_query($form, $dbh, $query);
+
+  my @splits;
   my $counter = 0;
-  @splits;
   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
-    $count    = 0;
-    $firstrun = 1;
     $counter++;
     if (($counter % 500) == 0) {
       print("$counter ");
     }
 
-    $count += $ref->{amount};
-    push @{$i}, $ref;
+    my $trans    = [ $ref ];
+
+    my $count    = $ref->{amount};
+    my $firstrun = 1;
+
     while (abs($count) > 0.01 || $firstrun) {
-      $ref2 = $sth->fetchrow_hashref(NAME_lc);
+      my $ref2 = $sth->fetchrow_hashref(NAME_lc);
       last unless ($ref2);
-      $count += $ref2->{amount};
-      push @{$i}, $ref2;
-      $firstrun = 0;
+
+      push @{ $trans }, $ref2;
+
+      $count    += $ref2->{amount};
+      $firstrun  = 0;
     }
+
     my %taxid_taxkeys = ();
-    $absumsatz = 0;
-    if (scalar(@{$i}) > 2) {
-      for my $j (0 .. (scalar(@{$i}) - 1)) {
-        if (abs($i->[$j]->{'amount'}) > abs($absumsatz)) {
-          $absumsatz     = $i->[$j]->{'amount'};
-          $notsplitindex = $j;
-        }
-        if (($i->[$j]->{'taxtaxkey'}) && ($i->[$j]->{'taxid'})) {
-          $taxid_taxkeys{$i->[$j]->{'taxtaxkey'}}     = $i->[$j]->{'taxid'};
-        }
+    my $absumsatz     = 0;
+    if (scalar(@{$trans}) <= 2) {
+      push @{ $form->{DATEV} }, $trans;
+      next;
+    }
+
+    for my $j (0 .. (scalar(@{$trans}) - 1)) {
+      if (abs($trans->[$j]->{'amount'}) > abs($absumsatz)) {
+        $absumsatz     = $trans->[$j]->{'amount'};
+        $notsplitindex = $j;
       }
-      $ml = ($i->[0]->{'umsatz'} > 0) ? 1 : -1;
-      for my $j (0 .. (scalar(@{$i}) - 1)) {
-        if (   ($j != $notsplitindex)
-            && ($i->[$j]->{'chart_id'}  eq "")
-            && (   $i->[$j]->{'taxkey'} eq ""
-                || $i->[$j]->{'taxkey'} eq "0"
-                || $i->[$j]->{'taxkey'} eq "1"
-                || $i->[$j]->{'taxkey'} eq "10"
-                || $i->[$j]->{'taxkey'} eq "11")
-          ) {
-          my %blubb = {};
-          map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
-              keys(%{ $i->[$notsplitindex] }));
-          $absumsatz += $i->[$j]->{'amount'};
-          $blubb{'amount'}     = $i->[$j]->{'amount'} * (-1);
-          $blubb{'umsatz'}     = abs($i->[$j]->{'amount'}) * $ml;
-          $i->[$j]->{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml;
-          push @{ $splits[$g] }, \%blubb;    #$i->[$notsplitindex];
-          push @{ $splits[$g] }, $i->[$j];
-          push @{ $form->{DATEV} }, \@{ $splits[$g] };
-          $g++;
-          } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) {
-          $absumsatz +=
-            ($i->[$j]->{'amount'} * (1 + $taxes{ $taxid_taxkeys{$i->[$j]->{'taxkey'}} }));
-          my %blubb = {};
-          map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
-              keys(%{ $i->[$notsplitindex] }));
-          $test = 1 + $taxes{  $taxid_taxkeys{$i->[$j]->{'taxkey'}} };
-          $blubb{'amount'} =
-            $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2);
-
-          $blubb{'umsatz'} =
-            abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
-
-          $i->[$j]->{'umsatz'} =
-            abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
-
-          push @{ $splits[$g] }, \%blubb;
-          push @{ $splits[$g] }, $i->[$j];
-          push @{ $form->{DATEV} }, \@{ $splits[$g] };
-          $g++;
-          } else {
-          next;
-        }
+      if (($trans->[$j]->{'taxtaxkey'}) && ($trans->[$j]->{'taxid'})) {
+        $taxid_taxkeys{$trans->[$j]->{'taxtaxkey'}}     = $trans->[$j]->{'taxid'};
       }
-      if (abs($absumsatz) > 0.01) {
-        $form->error("Datev-Export fehlgeschlagen! Bei Transaktion $i->[0]->{trans_id} $absumsatz\n");
+    }
+
+    my $ml = ($trans->[0]->{'umsatz'} > 0) ? 1 : -1;
+    for my $j (0 .. (scalar(@{$trans}) - 1)) {
+      if (   ($j != $notsplitindex)
+          && ($trans->[$j]->{'chart_id'}  eq "")
+          && (   $trans->[$j]->{'taxkey'} eq ""
+              || $trans->[$j]->{'taxkey'} eq "0"
+              || $trans->[$j]->{'taxkey'} eq "1"
+              || $trans->[$j]->{'taxkey'} eq "10"
+              || $trans->[$j]->{'taxkey'} eq "11")) {
+        my %new_trans = ();
+        map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
+
+        $absumsatz               += $trans->[$j]->{'amount'};
+        $new_trans{'amount'}      = $trans->[$j]->{'amount'} * (-1);
+        $new_trans{'umsatz'}      = abs($trans->[$j]->{'amount'}) * $ml;
+        $trans->[$j]->{'umsatz'}  = abs($trans->[$j]->{'amount'}) * $ml;
+
+        push @splits, [ \%new_trans, $trans->[$j] ];
+        push @{ $form->{DATEV} }, $splits[-1];
+
+      } elsif (($j != $notsplitindex) && ($trans->[$j]->{'chart_id'} eq "")) {
+        $absumsatz += ($trans->[$j]->{'amount'} * (1 + $taxes{ $taxid_taxkeys{$trans->[$j]->{'taxkey'}} }));
+
+        my %new_trans = ();
+        map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
+
+        my $tax_rate             = 1 + $taxes{ $taxid_taxkeys{$trans->[$j]->{'taxkey'}} };
+        $new_trans{'amount'}     = $form->round_amount(($trans->[$j]->{'amount'} * $tax_rate * -1), 2);
+        $new_trans{'umsatz'}     = abs($form->round_amount(($trans->[$j]->{'amount'} * $tax_rate), 2)) * $ml;
+        $trans->[$j]->{'umsatz'} = abs($form->round_amount(($trans->[$j]->{'amount'} * $tax_rate), 2)) * $ml;
+
+        push @splits, [ \%new_trans, $trans->[$j] ];
+        push @{ $form->{DATEV} }, $splits[-1];
       }
-    } else {
-      push @{ $form->{DATEV} }, \@{$i};
     }
-    $i++;
+
+    if (abs($absumsatz) > 0.01) {
+      push @errors, "Datev-Export fehlgeschlagen! Bei Transaktion $trans->[0]->{trans_id} ($absumsatz)\n";
+    }
   }
-  $sth->finish;
-  $dbh->disconnect;
+
+  $sth->finish();
+
+  $form->error(join("<br>\n", @errors)) if (@errors);
 
   $main::lxdebug->leave_sub();
 }
@@ -606,7 +625,7 @@ sub kne_buchungsexport {
     &get_dates($form->{zeitraum}, $form->{monat},
                $form->{quartal},  $form->{transdatefrom},
                $form->{transdateto});
-  &get_transactions($myconfig, $form, $fromto);
+  _get_transactions($fromto);
   my $counter = 0;
   print qq|<br>2. Durchlauf:|;
   while (scalar(@{ $form->{DATEV} })) {