1 #=====================================================================
 
   6 #   Email: p.reetz@linet-services.de
 
   7 #     Web: http://www.lx-office.org
 
  10 # This program is free software; you can redistribute it and/or modify
 
  11 # it under the terms of the GNU General Public License as published by
 
  12 # the Free Software Foundation; either version 2 of the License, or
 
  13 # (at your option) any later version.
 
  15 # This program is distributed in the hope that it will be useful,
 
  16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  18 # GNU General Public License for more details.
 
  19 # You should have received a copy of the GNU General Public License
 
  20 # along with this program; if not, write to the Free Software
 
  21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  22 #======================================================================
 
  25 #======================================================================
 
  33 use SL::DATEV::KNEFile;
 
  38 use List::Util qw(max);
 
  39 use Time::HiRes qw(gettimeofday);
 
  41 sub _get_export_path {
 
  42   $main::lxdebug->enter_sub();
 
  44   my ($a, $b) = gettimeofday();
 
  45   my $path    = get_path_for_download_token("${a}-${b}-${$}");
 
  47   mkpath($path) unless (-d $path);
 
  49   $main::lxdebug->leave_sub();
 
  54 sub get_path_for_download_token {
 
  55   $main::lxdebug->enter_sub();
 
  60   if ($token =~ m|^(\d+)-(\d+)-(\d+)$|) {
 
  61     $path = $::lx_office_conf{paths}->{userspath} . "/datev-export-${1}-${2}-${3}";
 
  64   $main::lxdebug->leave_sub();
 
  69 sub get_download_token_for_path {
 
  70   $main::lxdebug->enter_sub();
 
  75   if ($path =~ m|.*datev-export-(\d+)-(\d+)-(\d+)/?$|) {
 
  76     $token = "${1}-${2}-${3}";
 
  79   $main::lxdebug->leave_sub();
 
  84 sub clean_temporary_directories {
 
  85   $main::lxdebug->enter_sub();
 
  87   foreach my $path (glob($::lx_office_conf{paths}->{userspath} . "/datev-export-*")) {
 
  88     next unless (-d $path);
 
  90     my $mtime = (stat($path))[9];
 
  91     next if ((time() - $mtime) < 8 * 60 * 60);
 
  96   $main::lxdebug->leave_sub();
 
 100   $main::lxdebug->enter_sub();
 
 103   my $field_len = shift;
 
 104   my $fill_char = shift;
 
 105   my $alignment = shift || 'right';
 
 107   my $text_len  = length $text;
 
 109   if ($field_len < $text_len) {
 
 110     $text = substr $text, 0, $field_len;
 
 112   } elsif ($field_len > $text_len) {
 
 113     my $filler = ($fill_char) x ($field_len - $text_len);
 
 114     $text      = $alignment eq 'right' ? $filler . $text : $text . $filler;
 
 117   $main::lxdebug->leave_sub();
 
 122 sub get_datev_stamm {
 
 123   $main::lxdebug->enter_sub();
 
 125   my ($self, $myconfig, $form) = @_;
 
 127   # connect to database
 
 128   my $dbh = $form->dbconnect($myconfig);
 
 130   my $query = qq|SELECT * FROM datev|;
 
 131   my $sth   = $dbh->prepare($query);
 
 132   $sth->execute || $form->dberror($query);
 
 134   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 136   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 140   $main::lxdebug->leave_sub();
 
 143 sub save_datev_stamm {
 
 144   $main::lxdebug->enter_sub();
 
 146   my ($self, $myconfig, $form) = @_;
 
 148   # connect to database
 
 149   my $dbh = $form->dbconnect_noauto($myconfig);
 
 151   my $query = qq|DELETE FROM datev|;
 
 152   $dbh->do($query) || $form->dberror($query);
 
 154   $query = qq|INSERT INTO datev
 
 155               (beraternr, beratername, dfvkz, mandantennr, datentraegernr, abrechnungsnr) VALUES
 
 157     . $dbh->quote($form->{beraternr}) . qq|,|
 
 158     . $dbh->quote($form->{beratername}) . qq|,|
 
 159     . $dbh->quote($form->{dfvkz}) . qq|,
 
 161     . $dbh->quote($form->{mandantennr}) . qq|,|
 
 162     . $dbh->quote($form->{datentraegernr}) . qq|,|
 
 163     . $dbh->quote($form->{abrechnungsnr}) . qq|)|;
 
 164   my $sth = $dbh->prepare($query);
 
 165   $sth->execute || $form->dberror($query);
 
 170   $main::lxdebug->leave_sub();
 
 174   $main::lxdebug->enter_sub();
 
 176   my ($self, $myconfig, $form) = @_;
 
 179   if ($form->{exporttype} == 0) {
 
 180     $result = kne_buchungsexport($myconfig, $form);
 
 182     $result = kne_stammdatenexport($myconfig, $form);
 
 185   $main::lxdebug->leave_sub();
 
 191   $main::lxdebug->enter_sub();
 
 193   my ($self, $myconfig, $form) = @_;
 
 195   # connect to database
 
 196   my $dbh = $form->dbconnect_noauto($myconfig);
 
 199   $main::lxdebug->leave_sub();
 
 203   $main::lxdebug->enter_sub();
 
 205   my ($zeitraum, $monat, $quartal, $transdatefrom, $transdateto) = @_;
 
 206   my ($fromto, $jahr, $leap);
 
 208   my $form = $main::form;
 
 210   $fromto = "transdate >= ";
 
 215   if ($zeitraum eq "monat") {
 
 217       $monat eq "1" && do {
 
 218         $form->{fromdate} = "1.1.$jahr";
 
 219         $form->{todate}   = "31.1.$jahr";
 
 222       $monat eq "2" && do {
 
 223         $form->{fromdate} = "1.2.$jahr";
 
 225         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 226         $leap = ($jahr % 4 == 0) ? "29" : "28";
 
 227         $form->{todate} = "$leap.2.$jahr";
 
 230       $monat eq "3" && do {
 
 231         $form->{fromdate} = "1.3.$jahr";
 
 232         $form->{todate}   = "31.3.$jahr";
 
 235       $monat eq "4" && do {
 
 236         $form->{fromdate} = "1.4.$jahr";
 
 237         $form->{todate}   = "30.4.$jahr";
 
 240       $monat eq "5" && do {
 
 241         $form->{fromdate} = "1.5.$jahr";
 
 242         $form->{todate}   = "31.5.$jahr";
 
 245       $monat eq "6" && do {
 
 246         $form->{fromdate} = "1.6.$jahr";
 
 247         $form->{todate}   = "30.6.$jahr";
 
 250       $monat eq "7" && do {
 
 251         $form->{fromdate} = "1.7.$jahr";
 
 252         $form->{todate}   = "31.7.$jahr";
 
 255       $monat eq "8" && do {
 
 256         $form->{fromdate} = "1.8.$jahr";
 
 257         $form->{todate}   = "31.8.$jahr";
 
 260       $monat eq "9" && do {
 
 261         $form->{fromdate} = "1.9.$jahr";
 
 262         $form->{todate}   = "30.9.$jahr";
 
 265       $monat eq "10" && do {
 
 266         $form->{fromdate} = "1.10.$jahr";
 
 267         $form->{todate}   = "31.10.$jahr";
 
 270       $monat eq "11" && do {
 
 271         $form->{fromdate} = "1.11.$jahr";
 
 272         $form->{todate}   = "30.11.$jahr";
 
 275       $monat eq "12" && do {
 
 276         $form->{fromdate} = "1.12.$jahr";
 
 277         $form->{todate}   = "31.12.$jahr";
 
 282       "'" . $form->{fromdate} . "' and transdate <= '" . $form->{todate} . "'";
 
 285   elsif ($zeitraum eq "quartal") {
 
 288         "'01.01." . $jahr . "' and transdate <= '31.03." . $jahr . "'";
 
 289     } elsif ($quartal == 2) {
 
 291         "'01.04." . $jahr . "' and transdate <= '30.06." . $jahr . "'";
 
 292     } elsif ($quartal == 3) {
 
 294         "'01.07." . $jahr . "' and transdate <= '30.09." . $jahr . "'";
 
 295     } elsif ($quartal == 4) {
 
 297         "'01.10." . $jahr . "' and transdate <= '31.12." . $jahr . "'";
 
 301   elsif ($zeitraum eq "zeit") {
 
 302     $fromto            .= "'" . $transdatefrom . "' and transdate <= '" . $transdateto . "'";
 
 303     my ($yy, $mm, $dd)  = $main::locale->parse_date(\%main::myconfig, $transdatefrom);
 
 307   $main::lxdebug->leave_sub();
 
 309   return ($fromto, $jahr);
 
 315   return $value < 0 ? -1
 
 320 sub _get_transactions {
 
 321   $main::lxdebug->enter_sub();
 
 325   my $myconfig =  \%main::myconfig;
 
 326   my $form     =  $main::form;
 
 328   my $dbh      =  $form->get_standard_dbh($myconfig);
 
 333   $form->{net_gross_differences}     = [];
 
 334   $form->{sum_net_gross_differences} = 0;
 
 336   $fromto      =~ s/transdate/ac\.transdate/g;
 
 338   my $taxkeys  = Taxkeys->new();
 
 339   my $filter   = '';            # Useful for debugging purposes
 
 341   my %all_taxchart_ids = selectall_as_map($form, $dbh, qq|SELECT DISTINCT chart_id, TRUE AS is_set FROM tax|, 'chart_id', 'is_set');
 
 344     qq|SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey,
 
 345          ar.invnumber, ar.duedate, ar.amount as umsatz,
 
 347          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
 
 350        LEFT JOIN ar          ON (ac.trans_id    = ar.id)
 
 351        LEFT JOIN customer ct ON (ar.customer_id = ct.id)
 
 352        LEFT JOIN chart c     ON (ac.chart_id    = c.id)
 
 353        WHERE (ar.id IS NOT NULL)
 
 359        SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,ap.id, ac.amount, ac.taxkey,
 
 360          ap.invnumber, ap.duedate, ap.amount as umsatz,
 
 362          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
 
 365        LEFT JOIN ap        ON (ac.trans_id  = ap.id)
 
 366        LEFT JOIN vendor ct ON (ap.vendor_id = ct.id)
 
 367        LEFT JOIN chart c   ON (ac.chart_id  = c.id)
 
 368        WHERE (ap.id IS NOT NULL)
 
 374        SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,gl.id, ac.amount, ac.taxkey,
 
 375          gl.reference AS invnumber, gl.transdate AS duedate, ac.amount as umsatz,
 
 376          gl.description AS name,
 
 377          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
 
 380        LEFT JOIN gl      ON (ac.trans_id  = gl.id)
 
 381        LEFT JOIN chart c ON (ac.chart_id  = c.id)
 
 382        WHERE (gl.id IS NOT NULL)
 
 386        ORDER BY trans_id, acc_trans_id|;
 
 388   my $sth = prepare_execute_query($form, $dbh, $query);
 
 392   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 394     if (($counter % 500) == 0) {
 
 398     my $trans    = [ $ref ];
 
 400     my $count    = $ref->{amount};
 
 402     my $subcent  = abs($count) < 0.02;
 
 404     while (abs($count) > 0.01 || $firstrun || ($subcent && abs($count) > 0.001)) {
 
 405       my $ref2 = $sth->fetchrow_hashref("NAME_lc");
 
 408       if ($ref2->{trans_id} != $trans->[0]->{trans_id}) {
 
 409         $form->error("Unbalanced ledger! old trans_id " . $trans->[0]->{trans_id} . " new trans_id " . $ref2->{trans_id} . " count $count");
 
 413       push @{ $trans }, $ref2;
 
 415       $count    += $ref2->{amount};
 
 419     foreach my $i (0 .. scalar(@{ $trans }) - 1) {
 
 420       my $ref        = $trans->[$i];
 
 421       my $prev_ref   = 0 < $i ? $trans->[$i - 1] : undef;
 
 422       if (   $all_taxchart_ids{$ref->{id}}
 
 423           && ($ref->{link} =~ m/(?:AP_tax|AR_tax)/)
 
 424           && (   ($prev_ref && $prev_ref->{taxkey} && (_sign($ref->{amount}) == _sign($prev_ref->{amount})))
 
 425               || $ref->{invoice})) {
 
 429       if (   !$ref->{invoice}
 
 431           && !($prev_ref->{is_tax})
 
 432           &&  (_sign($ref->{amount}) == _sign($prev_ref->{amount}))) {
 
 433         $trans->[$i - 1]->{tax_amount} = $ref->{amount};
 
 437     my %taxid_taxkeys = ();
 
 439     if (scalar(@{$trans}) <= 2) {
 
 440       push @{ $form->{DATEV} }, $trans;
 
 444     for my $j (0 .. (scalar(@{$trans}) - 1)) {
 
 445       if (abs($trans->[$j]->{'amount'}) > abs($absumsatz)) {
 
 446         $absumsatz     = $trans->[$j]->{'amount'};
 
 451     my $ml             = ($trans->[0]->{'umsatz'} > 0) ? 1 : -1;
 
 452     my $rounding_error = 0;
 
 455     for my $j (0 .. (scalar(@{$trans}) - 1)) {
 
 456       if (   ($j != $notsplitindex)
 
 457           && !$trans->[$j]->{is_tax}
 
 458           && (   $trans->[$j]->{'taxkey'} eq ""
 
 459               || $trans->[$j]->{'taxkey'} eq "0"
 
 460               || $trans->[$j]->{'taxkey'} eq "1"
 
 461               || $trans->[$j]->{'taxkey'} eq "10"
 
 462               || $trans->[$j]->{'taxkey'} eq "11")) {
 
 464         map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
 
 466         $absumsatz               += $trans->[$j]->{'amount'};
 
 467         $new_trans{'amount'}      = $trans->[$j]->{'amount'} * (-1);
 
 468         $new_trans{'umsatz'}      = abs($trans->[$j]->{'amount'}) * $ml;
 
 469         $trans->[$j]->{'umsatz'}  = abs($trans->[$j]->{'amount'}) * $ml;
 
 471         push @{ $form->{DATEV} }, [ \%new_trans, $trans->[$j] ];
 
 473       } elsif (($j != $notsplitindex) && !$trans->[$j]->{is_tax}) {
 
 474         my %tax_info = $taxkeys->get_full_tax_info('transdate' => $trans->[$j]->{transdate});
 
 477         map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
 
 479         my $tax_rate              = $tax_info{taxkeys}->{ $trans->[$j]->{'taxkey'} }->{taxrate};
 
 480         $new_trans{'net_amount'}  = $trans->[$j]->{'amount'} * -1;
 
 481         $new_trans{'tax_rate'}    = 1 + $tax_rate;
 
 483         if (!$trans->[$j]->{'invoice'}) {
 
 484           $new_trans{'amount'}      = $form->round_amount(-1 * ($trans->[$j]->{amount} + $trans->[$j]->{tax_amount}), 2);
 
 485           $new_trans{'umsatz'}      = abs($new_trans{'amount'}) * $ml;
 
 486           $trans->[$j]->{'umsatz'}  = $new_trans{'umsatz'};
 
 487           $absumsatz               += -1 * $new_trans{'amount'};
 
 490           my $unrounded             = $trans->[$j]->{'amount'} * (1 + $tax_rate) * -1 + $rounding_error;
 
 491           my $rounded               = $form->round_amount($unrounded, 2);
 
 493           $rounding_error           = $unrounded - $rounded;
 
 494           $new_trans{'amount'}      = $rounded;
 
 495           $new_trans{'umsatz'}      = abs($rounded) * $ml;
 
 496           $trans->[$j]->{'umsatz'}  = $new_trans{umsatz};
 
 497           $absumsatz               -= $rounded;
 
 500         push @{ $form->{DATEV} }, [ \%new_trans, $trans->[$j] ];
 
 501         push @taxed, $form->{DATEV}->[-1];
 
 507     while ((abs($absumsatz) >= 0.01) && (abs($absumsatz) < 1.00)) {
 
 508       if ($idx >= scalar @taxed) {
 
 509         last if (!$correction);
 
 515       my $transaction = $taxed[$idx]->[0];
 
 517       my $old_amount     = $transaction->{amount};
 
 518       my $old_correction = $correction;
 
 521       if (!$transaction->{diff}) {
 
 522         @possible_diffs = (0.01, -0.01);
 
 524         @possible_diffs = ($transaction->{diff});
 
 527       foreach my $diff (@possible_diffs) {
 
 528         my $net_amount = $form->round_amount(($transaction->{amount} + $diff) / $transaction->{tax_rate}, 2);
 
 529         next if ($net_amount != $transaction->{net_amount});
 
 531         $transaction->{diff}    = $diff;
 
 532         $transaction->{amount} += $diff;
 
 533         $transaction->{umsatz} += $diff;
 
 543     $absumsatz = $form->round_amount($absumsatz, 2);
 
 544     if (abs($absumsatz) >= (0.01 * (1 + scalar @taxed))) {
 
 545       push @errors, "Datev-Export fehlgeschlagen! Bei Transaktion $trans->[0]->{trans_id} ($absumsatz)\n";
 
 547     } elsif (abs($absumsatz) >= 0.01) {
 
 548       push @{ $form->{net_gross_differences} }, $absumsatz;
 
 549       $form->{sum_net_gross_differences} += $absumsatz;
 
 555   $form->error(join("<br>\n", @errors)) if (@errors);
 
 557   $main::lxdebug->leave_sub();
 
 560 sub make_kne_data_header {
 
 561   $main::lxdebug->enter_sub();
 
 563   my ($myconfig, $form, $fromto, $start_jahr) = @_;
 
 566   my $jahr = $start_jahr;
 
 573   my $header  = "\x1D\x181";
 
 574   $header    .= _fill($form->{datentraegernr}, 3, ' ', 'left');
 
 575   $header    .= ($fromto) ? "11" : "13"; # Anwendungsnummer
 
 576   $header    .= _fill($form->{dfvkz}, 2, '0');
 
 577   $header    .= _fill($form->{beraternr}, 7, '0');
 
 578   $header    .= _fill($form->{mandantennr}, 5, '0');
 
 579   $header    .= _fill($form->{abrechnungsnr} . $jahr, 6, '0');
 
 581   $fromto         =~ s/transdate|>=|and|\'|<=//g;
 
 582   my ($from, $to) =  split /   /, $fromto;
 
 587     my ($fday, $fmonth, $fyear) = split(/\./, $from);
 
 588     if (length($fmonth) < 2) {
 
 589       $fmonth = "0" . $fmonth;
 
 591     if (length($fday) < 2) {
 
 594     $from = $fday . $fmonth . substr($fyear, -2, 2);
 
 602     my ($tday, $tmonth, $tyear) = split(/\./, $to);
 
 603     if (length($tmonth) < 2) {
 
 604       $tmonth = "0" . $tmonth;
 
 606     if (length($tday) < 2) {
 
 609     $to = $tday . $tmonth . substr($tyear, -2, 2);
 
 617     $header .= $primanota;
 
 620   $header .= _fill($form->{passwort}, 4, '0');
 
 621   $header .= " " x 16;       # Anwendungsinfo
 
 622   $header .= " " x 16;       # Inputinfo
 
 626   my $versionssatz  = $form->{exporttype} == 0 ? "\xB5" . "1," : "\xB6" . "1,";
 
 628   my $dbh           = $form->get_standard_dbh($myconfig);
 
 629   my $query         = qq|SELECT accno FROM chart LIMIT 1|;
 
 630   my $ref           = selectfirst_hashref_query($form, $dbh, $query);
 
 632   $versionssatz    .= length $ref->{accno};
 
 633   $versionssatz    .= ",";
 
 634   $versionssatz    .= length $ref->{accno};
 
 635   $versionssatz    .= ",SELF" . "\x1C\x79";
 
 637   $header          .= $versionssatz;
 
 639   $main::lxdebug->leave_sub();
 
 645   $main::lxdebug->enter_sub();
 
 647   my ($date, $six) = @_;
 
 649   my ($day, $month, $year) = split(/\./, $date);
 
 652     $day = substr($day, 1, 1);
 
 654   if (length($month) < 2) {
 
 655     $month = "0" . $month;
 
 657   if (length($year) > 2) {
 
 658     $year = substr($year, -2, 2);
 
 662     $date = $day . $month . $year;
 
 664     $date = $day . $month;
 
 667   $main::lxdebug->leave_sub();
 
 672 sub trim_leading_zeroes {
 
 680 sub make_ed_versionset {
 
 681   $main::lxdebug->enter_sub();
 
 683   my ($header, $filename, $blockcount, $fromto) = @_;
 
 685   my $versionset  = "V" . substr($filename, 2, 5);
 
 686   $versionset    .= substr($header, 6, 22);
 
 689     $versionset .= "0000" . substr($header, 28, 19);
 
 691     my $datum = " " x 16;
 
 692     $versionset .= $datum . "001" . substr($header, 28, 4);
 
 695   $versionset .= _fill($blockcount, 5, '0');
 
 696   $versionset .= "001";
 
 698   $versionset .= substr($header, -12, 10) . "    ";
 
 699   $versionset .= " " x 53;
 
 701   $main::lxdebug->leave_sub();
 
 707   $main::lxdebug->enter_sub();
 
 709   my ($form, $fileno) = @_;
 
 711   my $ev_header  = _fill($form->{datentraegernr}, 3, ' ', 'left');
 
 713   $ev_header    .= _fill($form->{beraternr}, 7, ' ', 'left');
 
 714   $ev_header    .= _fill($form->{beratername}, 9, ' ', 'left');
 
 716   $ev_header    .= (_fill($fileno, 5, '0')) x 2;
 
 717   $ev_header    .= " " x 95;
 
 719   $main::lxdebug->leave_sub();
 
 724 sub kne_buchungsexport {
 
 725   $main::lxdebug->enter_sub();
 
 727   my ($myconfig, $form) = @_;
 
 731   my $export_path = _get_export_path() . "/";
 
 732   my $filename    = "ED00000";
 
 740   <body>Export in Bearbeitung<br>
 
 741   Buchungssätze verarbeitet:
 
 744   my ($fromto, $start_jahr) =
 
 745     &get_dates($form->{zeitraum}, $form->{monat},
 
 746                $form->{quartal},  $form->{transdatefrom},
 
 747                $form->{transdateto});
 
 748   _get_transactions($fromto);
 
 750   print qq|<br>2. Durchlauf:|;
 
 751   while (scalar(@{ $form->{DATEV} })) {
 
 754     my $ed_filename = $export_path . $filename;
 
 755     push(@filenames, $filename);
 
 756     my $header = &make_kne_data_header($myconfig, $form, $fromto, $start_jahr);
 
 758     my $kne_file = SL::DATEV::KNEFile->new();
 
 759     $kne_file->add_block($header);
 
 761     while (scalar(@{ $form->{DATEV} }) > 0) {
 
 762       my $transaction = shift @{ $form->{DATEV} };
 
 763       my $trans_lines = scalar(@{$transaction});
 
 765       if (($counter % 500) == 0) {
 
 775       my $buchungstext   = "";
 
 777       my $datevautomatik = 0;
 
 781       my $iconv          = $::locale->{iconv_utf8};
 
 782       my %umlaute = ($iconv->convert('ä') => 'ae',
 
 783                      $iconv->convert('ö') => 'oe',
 
 784                      $iconv->convert('ü') => 'ue',
 
 785                      $iconv->convert('Ä') => 'Ae',
 
 786                      $iconv->convert('Ö') => 'Oe',
 
 787                      $iconv->convert('Ü') => 'Ue',
 
 788                      $iconv->convert('ß') => 'sz');
 
 789       for (my $i = 0; $i < $trans_lines; $i++) {
 
 790         if ($trans_lines == 2) {
 
 791           if (abs($transaction->[$i]->{'amount'}) > abs($umsatz)) {
 
 792             $umsatz = $transaction->[$i]->{'amount'};
 
 795           if (abs($transaction->[$i]->{'umsatz'}) > abs($umsatz)) {
 
 796             $umsatz = $transaction->[$i]->{'umsatz'};
 
 799         if ($transaction->[$i]->{'datevautomatik'}) {
 
 802         if ($transaction->[$i]->{'taxkey'}) {
 
 803           $taxkey = $transaction->[$i]->{'taxkey'};
 
 805         if ($transaction->[$i]->{'charttax'}) {
 
 806           $charttax = $transaction->[$i]->{'charttax'};
 
 808         if ($transaction->[$i]->{'amount'} > 0) {
 
 815       # Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern
 
 816       foreach my $umlaut (keys(%umlaute)) {
 
 817         $transaction->[$haben]->{'invnumber'} =~ s/${umlaut}/${umlaute{$umlaut}}/g;
 
 818         $transaction->[$haben]->{'name'}      =~ s/${umlaut}/${umlaute{$umlaut}}/g;
 
 821       $transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
 
 822       $transaction->[$haben]->{'name'}      =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
 
 824       $transaction->[$haben]->{'invnumber'} =  substr($transaction->[$haben]->{'invnumber'}, 0, 12);
 
 825       $transaction->[$haben]->{'name'}      =  substr($transaction->[$haben]->{'name'}, 0, 30);
 
 826       $transaction->[$haben]->{'invnumber'} =~ s/\ *$//;
 
 827       $transaction->[$haben]->{'name'}      =~ s/\ *$//;
 
 829       if ($trans_lines >= 2) {
 
 831         $gegenkonto = "a" . trim_leading_zeroes($transaction->[$haben]->{'accno'});
 
 832         $konto      = "e" . trim_leading_zeroes($transaction->[$soll]->{'accno'});
 
 833         if ($transaction->[$haben]->{'invnumber'} ne "") {
 
 834           $belegfeld1 = "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
 
 837         $datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
 
 838         $waehrung = "\xB3" . "EUR" . "\x1C";
 
 839         if ($transaction->[$haben]->{'name'} ne "") {
 
 840           $buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
 
 842         if ($transaction->[$haben]->{'duedate'} ne "") {
 
 843           $belegfeld2 = "\xBE" . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
 
 847       $umsatz       = $kne_file->format_amount(abs($umsatz), 0);
 
 848       $umsatzsumme += $umsatz;
 
 849       $kne_file->add_block("+" . $umsatz);
 
 851       if (   ( $datevautomatik || $taxkey)
 
 852           && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
 
 853 #         $kne_file->add_block("\x6C" . (!$datevautomatik ? $taxkey : "4"));
 
 854         $kne_file->add_block("\x6C${taxkey}");
 
 857       $kne_file->add_block($gegenkonto);
 
 858       $kne_file->add_block($belegfeld1);
 
 859       $kne_file->add_block($belegfeld2);
 
 860       $kne_file->add_block($datum);
 
 861       $kne_file->add_block($konto);
 
 862       $kne_file->add_block($buchungstext);
 
 863       $kne_file->add_block($waehrung . "\x79");
 
 866     my $mandantenendsumme = "x" . $kne_file->format_amount($umsatzsumme / 100.0, 14) . "\x79\x7a";
 
 868     $kne_file->add_block($mandantenendsumme);
 
 871     open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
 
 872     print(ED $kne_file->get_data());
 
 875     $ed_versionset[$fileno] = &make_ed_versionset($header, $filename, $kne_file->get_block_count(), $fromto);
 
 879   #Make EV Verwaltungsdatei
 
 880   my $ev_header = &make_ev_header($form, $fileno);
 
 881   my $ev_filename = $export_path . $evfile;
 
 882   push(@filenames, $evfile);
 
 883   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
 
 884   print(EV $ev_header);
 
 886   foreach my $file (@ed_versionset) {
 
 887     print(EV $ed_versionset[$file]);
 
 890   print qq|<br>Done. <br>
 
 893   $main::lxdebug->leave_sub();
 
 895   return { 'download_token' => get_download_token_for_path($export_path), 'filenames' => \@filenames };
 
 898 sub kne_stammdatenexport {
 
 899   $main::lxdebug->enter_sub();
 
 901   my ($myconfig, $form) = @_;
 
 902   $form->{abrechnungsnr} = "99";
 
 907   <body>Export in Bearbeitung<br>
 
 912   my $export_path = _get_export_path() . "/";
 
 913   my $filename    = "ED00000";
 
 919   my $remaining_bytes = 256;
 
 920   my $total_bytes     = 256;
 
 921   my $buchungssatz    = "";
 
 923   my $ed_filename = $export_path . $filename;
 
 924   push(@filenames, $filename);
 
 925   open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
 
 926   my $header = &make_kne_data_header($myconfig, $form, "");
 
 927   $remaining_bytes -= length($header);
 
 932   # connect to database
 
 933   my $dbh = $form->dbconnect($myconfig);
 
 935   my (@where, @values) = ((), ());
 
 936   if ($form->{accnofrom}) {
 
 937     push @where, 'c.accno >= ?';
 
 938     push @values, $form->{accnofrom};
 
 940   if ($form->{accnoto}) {
 
 941     push @where, 'c.accno <= ?';
 
 942     push @values, $form->{accnoto};
 
 945   my $where_str = ' WHERE ' . join(' AND ', map { "($_)" } @where) if (scalar @where);
 
 947   my $query     = qq|SELECT c.accno, c.description
 
 952   my $sth = $dbh->prepare($query);
 
 953   $sth->execute(@values) || $form->dberror($query);
 
 955   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 956     if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
 
 957       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 958       $buchungssatz .= "\x00" x $fuellzeichen;
 
 960       $total_bytes = ($blockcount) * 256;
 
 962     $buchungssatz .= "t" . $ref->{'accno'};
 
 963     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 964     $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
 
 965     $ref->{'description'} = substr($ref->{'description'}, 0, 40);
 
 966     $ref->{'description'} =~ s/\ *$//;
 
 969         ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
 
 972       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 973       $buchungssatz .= "\x00" x $fuellzeichen;
 
 975       $total_bytes = ($blockcount) * 256;
 
 977     $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
 
 978     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 983   print(ED $buchungssatz);
 
 984   $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
 
 985   my $dateiende = "\x00" x $fuellzeichen;
 
 987   print(ED $dateiende);
 
 990   #Make EV Verwaltungsdatei
 
 992     &make_ed_versionset($header, $filename, $blockcount, $fromto);
 
 994   my $ev_header = &make_ev_header($form, $fileno);
 
 995   my $ev_filename = $export_path . $evfile;
 
 996   push(@filenames, $evfile);
 
 997   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
 
 998   print(EV $ev_header);
 
1000   foreach my $file (@ed_versionset) {
 
1001     print(EV $ed_versionset[$file]);
 
1008   print qq|<br>Done. <br>
 
1011   $main::lxdebug->leave_sub();
 
1013   return { 'download_token' => get_download_token_for_path($export_path), 'filenames' => \@filenames };