Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / SL / DATEV.pm
index 508ed95..bfb48d0 100644 (file)
@@ -58,7 +58,7 @@ sub get_path_for_download_token {
   my $path;
 
   if ($token =~ m|^(\d+)-(\d+)-(\d+)$|) {
-    $path = "${main::userspath}/datev-export-${1}-${2}-${3}";
+    $path = $::lx_office_conf{paths}->{userspath} . "/datev-export-${1}-${2}-${3}";
   }
 
   $main::lxdebug->leave_sub();
@@ -84,7 +84,7 @@ sub get_download_token_for_path {
 sub clean_temporary_directories {
   $main::lxdebug->enter_sub();
 
-  foreach my $path (glob "${main::userspath}/datev-export-*") {
+  foreach my $path (glob($::lx_office_conf{paths}->{userspath} . "/datev-export-*")) {
     next unless (-d $path);
 
     my $mtime = (stat($path))[9];
@@ -399,8 +399,9 @@ sub _get_transactions {
 
     my $count    = $ref->{amount};
     my $firstrun = 1;
+    my $subcent  = abs($count) < 0.02;
 
-    while (abs($count) > 0.01 || $firstrun) {
+    while (abs($count) > 0.01 || $firstrun || ($subcent && abs($count) > 0.005)) {
       my $ref2 = $sth->fetchrow_hashref("NAME_lc");
       last unless ($ref2);
 
@@ -425,10 +426,10 @@ sub _get_transactions {
         $ref->{is_tax} = 1;
       }
 
-      if (   !$ref->{invoice}
-          &&  $ref->{is_tax}
-          && !($prev_ref->{is_tax})
-          &&  (_sign($ref->{amount}) == _sign($prev_ref->{amount}))) {
+      if (   !$ref->{invoice}   # we have a non-invoice booking (=gl)
+          &&  $ref->{is_tax}    # that has "is_tax" set
+          && !($prev_ref->{is_tax})  # previous line wasn't is_tax
+          &&  (_sign($ref->{amount}) == _sign($prev_ref->{amount}))) {  # and sign same as previous sign
         $trans->[$i - 1]->{tax_amount} = $ref->{amount};
       }
     }
@@ -441,7 +442,10 @@ sub _get_transactions {
     }
 
     for my $j (0 .. (scalar(@{$trans}) - 1)) {
-      if (abs($trans->[$j]->{'amount'}) > abs($absumsatz)) {
+      # for gl-bookings no split is allowed and there is no AR/AP account, so we always use the maximum value as a reference
+      # for ap/ar bookings we can always search for AR/AP in link and use that
+      if ( ( not $trans->[$j]->{'invoice'} and abs($trans->[$j]->{'amount'}) > abs($absumsatz) )
+         or ($trans->[$j]->{'invoice'} and ($trans->[$j]->{'link'} eq 'AR' or $trans->[$j]->{'link'} eq 'AP'))) {
         $absumsatz     = $trans->[$j]->{'amount'};
         $notsplitindex = $j;
       }
@@ -847,6 +851,16 @@ sub kne_buchungsexport {
       $umsatzsumme += $umsatz;
       $kne_file->add_block("+" . $umsatz);
 
+      # Dies ist die einzige Stelle die datevautomatik auswertet. Was soll gesagt werden?
+      # Im Prinzip hat jeder acc_trans Eintrag einen Steuerschlüssel, außer, bei gewissen Fällen
+      # wie: Kreditorenbuchung mit negativen Vorzeichen, SEPA-Export oder Rechnungen die per
+      # Skript angelegt werden.
+      # Also falls ein Steuerschlüssel da ist und NICHT datevautomatik diesen Block hinzufügen.
+      # Oder aber datevautomatik ist WAHR, aber der Steuerschlüssel in der acc_trans weicht
+      # von dem in der Chart ab: Also wahrscheinlich Programmfehler (NULL übergeben, statt
+      # DATEV-Steuerschlüssel) oder der Steuerschlüssel des Kontos weicht WIRKLICH von dem Eintrag in der
+      # acc_trans ab. Gibt es für diesen Fall eine plausiblen Grund?
+      #
       if (   ( $datevautomatik || $taxkey)
           && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
 #         $kne_file->add_block("\x6C" . (!$datevautomatik ? $taxkey : "4"));
@@ -867,7 +881,7 @@ sub kne_buchungsexport {
     $kne_file->add_block($mandantenendsumme);
     $kne_file->flush();
 
-    open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
+    open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
     print(ED $kne_file->get_data());
     close(ED);
 
@@ -879,7 +893,7 @@ sub kne_buchungsexport {
   my $ev_header = &make_ev_header($form, $fileno);
   my $ev_filename = $export_path . $evfile;
   push(@filenames, $evfile);
-  open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
+  open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
   print(EV $ev_header);
 
   foreach my $file (@ed_versionset) {
@@ -921,7 +935,7 @@ sub kne_stammdatenexport {
   $filename++;
   my $ed_filename = $export_path . $filename;
   push(@filenames, $filename);
-  open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
+  open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
   my $header = &make_kne_data_header($myconfig, $form, "");
   $remaining_bytes -= length($header);
 
@@ -941,7 +955,7 @@ sub kne_stammdatenexport {
     push @values, $form->{accnoto};
   }
 
-  my $where_str = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (scalar @where);
+  my $where_str = @where ? ' WHERE ' . join(' AND ', map { "($_)" } @where) : '';
 
   my $query     = qq|SELECT c.accno, c.description
                      FROM chart c
@@ -993,7 +1007,7 @@ sub kne_stammdatenexport {
   my $ev_header = &make_ev_header($form, $fileno);
   my $ev_filename = $export_path . $evfile;
   push(@filenames, $evfile);
-  open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
+  open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
   print(EV $ev_header);
 
   foreach my $file (@ed_versionset) {