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 #======================================================================
 
  32   $main::lxdebug->enter_sub();
 
  34   my ($self, $myconfig, $form) = @_;
 
  37   my $dbh = $form->dbconnect($myconfig);
 
  39   $query = qq|SELECT * FROM datev|;
 
  40   $sth   = $dbh->prepare($query);
 
  41   $sth->execute || $form->dberror($query);
 
  43   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  45   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  49   $main::lxdebug->leave_sub();
 
  52 sub save_datev_stamm {
 
  53   $main::lxdebug->enter_sub();
 
  55   my ($self, $myconfig, $form) = @_;
 
  58   my $dbh = $form->dbconnect_noauto($myconfig);
 
  60   $query = qq|DELETE FROM datev|;
 
  61   $dbh->do($query) || $form->dberror($query);
 
  63   $query = qq|INSERT INTO datev
 
  64               (beraternr, beratername, dfvkz, mandantennr, datentraegernr, abrechnungsnr) VALUES
 
  66     . $dbh->quote($form->{beraternr}) . qq|,|
 
  67     . $dbh->quote($form->{beratername}) . qq|,|
 
  68     . $dbh->quote($form->{dfvkz}) . qq|,
 
  70     . $dbh->quote($form->{mandantennr}) . qq|,|
 
  71     . $dbh->quote($form->{datentraegernr}) . qq|,|
 
  72     . $dbh->quote($form->{abrechnungsnr}) . qq|)|;
 
  73   $sth = $dbh->prepare($query);
 
  74   $sth->execute || $form->dberror($query);
 
  79   $main::lxdebug->leave_sub();
 
  83   $main::lxdebug->enter_sub();
 
  85   my ($self, $myconfig, $form) = @_;
 
  88   if ($form->{exporttype} == 0) {
 
  89     $rc = &kne_buchungsexport($myconfig, $form);
 
  91     $rc = &kne_stammdatenexport($myconfig, $form);
 
  94   $main::lxdebug->leave_sub();
 
 100   $main::lxdebug->enter_sub();
 
 102   my ($self, $myconfig, $form) = @_;
 
 104   # connect to database
 
 105   my $dbh = $form->dbconnect_noauto($myconfig);
 
 108   $main::lxdebug->leave_sub();
 
 112   $main::lxdebug->enter_sub();
 
 114   my ($zeitraum, $monat, $quartal, $transdatefrom, $transdateto) = @_;
 
 116   $fromto = "transdate >= ";
 
 121   if ($zeitraum eq "monat") {
 
 123       $monat eq "1" && do {
 
 124         $form->{fromdate} = "1.1.$jahr";
 
 125         $form->{todate}   = "31.1.$jahr";
 
 128       $monat eq "2" && do {
 
 129         $form->{fromdate} = "1.2.$jahr";
 
 131         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 132         $leap = ($jahr % 4 == 0) ? "29" : "28";
 
 133         $form->{todate} = "$leap.2.$jahr";
 
 136       $monat eq "3" && do {
 
 137         $form->{fromdate} = "1.3.$jahr";
 
 138         $form->{todate}   = "31.3.$jahr";
 
 141       $monat eq "4" && do {
 
 142         $form->{fromdate} = "1.4.$jahr";
 
 143         $form->{todate}   = "30.4.$jahr";
 
 146       $monat eq "5" && do {
 
 147         $form->{fromdate} = "1.5.$jahr";
 
 148         $form->{todate}   = "31.5.$jahr";
 
 151       $monat eq "6" && do {
 
 152         $form->{fromdate} = "1.6.$jahr";
 
 153         $form->{todate}   = "30.6.$jahr";
 
 156       $monat eq "7" && do {
 
 157         $form->{fromdate} = "1.7.$jahr";
 
 158         $form->{todate}   = "31.7.$jahr";
 
 161       $monat eq "8" && do {
 
 162         $form->{fromdate} = "1.8.$jahr";
 
 163         $form->{todate}   = "31.8.$jahr";
 
 166       $monat eq "9" && do {
 
 167         $form->{fromdate} = "1.9.$jahr";
 
 168         $form->{todate}   = "30.9.$jahr";
 
 171       $monat eq "10" && do {
 
 172         $form->{fromdate} = "1.10.$jahr";
 
 173         $form->{todate}   = "31.10.$jahr";
 
 176       $monat eq "11" && do {
 
 177         $form->{fromdate} = "1.11.$jahr";
 
 178         $form->{todate}   = "30.11.$jahr";
 
 181       $monat eq "12" && do {
 
 182         $form->{fromdate} = "1.12.$jahr";
 
 183         $form->{todate}   = "31.12.$jahr";
 
 188       "'" . $form->{fromdate} . "' and transdate <= '" . $form->{todate} . "'";
 
 191   elsif ($zeitraum eq "quartal") {
 
 194         "'01.01." . $jahr . "' and transdate <= '31.03." . $jahr . "'";
 
 195     } elsif ($quartal == 2) {
 
 197         "'01.04." . $jahr . "' and transdate <= '30.06." . $jahr . "'";
 
 198     } elsif ($quartal == 3) {
 
 200         "'01.07." . $jahr . "' and transdate <= '30.09." . $jahr . "'";
 
 201     } elsif ($quartal == 4) {
 
 203         "'01.10." . $jahr . "' and transdate <= '31.12." . $jahr . "'";
 
 207   elsif ($zeitraum eq "zeit") {
 
 209       "'" . $transdatefrom . "' and transdate <= '" . $transdateto . "'";
 
 212   $main::lxdebug->leave_sub();
 
 217 sub get_transactions {
 
 218   $main::lxdebug->enter_sub();
 
 220   my ($myconfig, $form, $fromto) = @_;
 
 222   # connect to database
 
 223   my $dbh = $form->dbconnect($myconfig);
 
 225   $fromto =~ s/transdate/ac\.transdate/g;
 
 227   $query = qq|SELECT taxkey, rate FROM tax|;
 
 228   $sth   = $dbh->prepare($query);
 
 229   $sth->execute || $form->dberror($query);
 
 231   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 232     $taxes{ $ref->{taxkey} } = $ref->{rate};
 
 238     qq|SELECT ac.oid, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey, ar.invnumber, ar.duedate, ar.amount as umsatz,
 
 239               ct.name, c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate FROM acc_trans ac,ar ar, customer ct,
 
 240               chart c LEFT JOIN tax t ON
 
 241                  (t.chart_id=c.id)WHERE $fromto AND ac.trans_id=ar.id AND ac.trans_id=ar.id
 
 242               AND ar.customer_id=ct.id AND ac.chart_id=c.id
 
 244               SELECT ac.oid, ac.transdate, ac.trans_id,ap.id, ac.amount, ac.taxkey, ap.invnumber, ap.duedate, ap.amount as umsatz,
 
 245               ct.name, c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate FROM acc_trans ac, ap ap, vendor ct, chart c LEFT JOIN tax t ON
 
 247               WHERE $fromto AND ac.trans_id=ap.id AND ap.vendor_id=ct.id AND ac.chart_id=c.id
 
 249               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,
 
 250               gl.description AS name, c.accno,  c.taxkey_id as charttax, c.datevautomatik, c.id, t.chart_id, t.rate FROM acc_trans ac, gl gl,
 
 251               chart c LEFT JOIN tax t ON
 
 252                  (t.chart_id=c.id) WHERE $fromto AND ac.trans_id=gl.id AND ac.chart_id=c.id
 
 253               ORDER BY trans_id, oid|;
 
 255   $sth = $dbh->prepare($query);
 
 256   $sth->execute || $form->dberror($query);
 
 261   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 265     if (($counter % 500) == 0) {
 
 269     $count += $ref->{amount};
 
 271     while (abs($count) > 0.01 || $firstrun) {
 
 272       $ref2 = $sth->fetchrow_hashref(NAME_lc);
 
 274       $count += $ref2->{amount};
 
 279     if (scalar(@{$i}) > 2) {
 
 280       for my $j (0 .. (scalar(@{$i}) - 1)) {
 
 281         if (abs($i->[$j]->{'amount'}) > abs($absumsatz)) {
 
 282           $absumsatz     = $i->[$j]->{'amount'};
 
 286       $ml = ($i->[0]->{'umsatz'} > 0) ? 1 : -1;
 
 287       for my $j (0 .. (scalar(@{$i}) - 1)) {
 
 288         if (   ($j != $notsplitindex)
 
 289             && ($i->[$j]->{'chart_id'}  eq "")
 
 290             && (   $i->[$j]->{'taxkey'} eq ""
 
 291                 || $i->[$j]->{'taxkey'} eq "0"
 
 292                 || $i->[$j]->{'taxkey'} eq "1"
 
 293                 || $i->[$j]->{'taxkey'} eq "10"
 
 294                 || $i->[$j]->{'taxkey'} eq "11")
 
 297           map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
 
 298               keys(%{ $i->[$notsplitindex] }));
 
 299           $absumsatz += $i->[$j]->{'amount'};
 
 300           $blubb{'amount'}     = $i->[$j]->{'amount'} * (-1);
 
 301           $blubb{'umsatz'}     = abs($i->[$j]->{'amount'}) * $ml;
 
 302           $i->[$j]->{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml;
 
 303           push @{ $splits[$g] }, \%blubb;    #$i->[$notsplitindex];
 
 304           push @{ $splits[$g] }, $i->[$j];
 
 305           push @{ $form->{DATEV} }, \@{ $splits[$g] };
 
 307           } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) {
 
 309             ($i->[$j]->{'amount'} * (1 + $taxes{ $i->[$j]->{'taxkey'} }));
 
 311           map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
 
 312               keys(%{ $i->[$notsplitindex] }));
 
 313           $test = 1 + $taxes{ $i->[$j]->{'taxkey'} };
 
 315             $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2);
 
 317           #print(STDERR $test, " Taxrate\n\n");
 
 319             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
 
 321           $i->[$j]->{'umsatz'} =
 
 322             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
 
 324           #print(STDERR $i->[$j]->{'umsatz'}, " Steuer Umsatz\n");
 
 325           #print(STDERR $i->[$j]->{'amount'}, " Steuer Betrag\n");
 
 326           #print(STDERR $blubb{'umsatz'}, " Umsatz NOTSPLIT\n");
 
 327           push @{ $splits[$g] }, \%blubb;
 
 328           push @{ $splits[$g] }, $i->[$j];
 
 329           push @{ $form->{DATEV} }, \@{ $splits[$g] };
 
 335       if (abs($absumsatz) > 0.01) {
 
 336         print(STDERR $absumsatz, "ABSAUMSATZ\n");
 
 337         $form->error("Datev-Export fehlgeschlagen!");
 
 340       push @{ $form->{DATEV} }, \@{$i};
 
 347   $main::lxdebug->leave_sub();
 
 350 sub make_kne_data_header {
 
 351   $main::lxdebug->enter_sub();
 
 353   my ($myconfig, $form, $fromto) = @_;
 
 355   # connect to database
 
 356   my $dbh = $form->dbconnect($myconfig);
 
 362   $anwendungsnr = ($fromto) ? "\x31\x31" : "\x31\x33";
 
 363   while (length($form->{datentraegernr}) < 3) {
 
 364     $form->{datentraegernr} = "\x30" . $form->{datentraegernr};
 
 367   $header = "\x1D\x18\x31" . $form->{datentraegernr} . $anwendungsnr;
 
 369   $dfvkz = $form->{dfvkz};
 
 370   while (length($dfvkz) < 2) {
 
 371     $dfvkz = "\x30" . $dfvkz;
 
 375   $beraternr = $form->{beraternr};
 
 376   while (length($beraternr) < 7) {
 
 377     $beraternr = "\x30" . $beraternr;
 
 379   $header .= $beraternr;
 
 381   $mandantennr = $form->{mandantennr};
 
 382   while (length($mandantennr) < 5) {
 
 383     $mandantennr = "\x30" . $mandantennr;
 
 385   $header .= $mandantennr;
 
 387   $abrechnungsnr = $form->{abrechnungsnr} . $jahr;
 
 388   while (length($abrechnungsnr) < 6) {
 
 389     $abrechnungsnr = "\x30" . $abrechnungsnr;
 
 391   $header .= $abrechnungsnr;
 
 393   $fromto =~ s/transdate|>=|and|\'|<=//g;
 
 394   my ($from, $to) = split /   /, $fromto;
 
 399     my ($fday, $fmonth, $fyear) = split /\./, $from;
 
 400     if (length($fmonth) < 2) {
 
 401       $fmonth = "0" . $fmonth;
 
 403     if (length($fday) < 2) {
 
 406     $from = $fday . $fmonth . substr($fyear, -2, 2);
 
 414     my ($tday, $tmonth, $tyear) = split /\./, $to;
 
 415     if (length($tmonth) < 2) {
 
 416       $tmonth = "0" . $tmonth;
 
 418     if (length($tday) < 2) {
 
 421     $to = $tday . $tmonth . substr($tyear, -2, 2);
 
 427     $primanota = "\x30\x30\x31";
 
 428     $header .= $primanota;
 
 431   $passwort = $form->{passwort};
 
 432   while (length($passwort) < 4) {
 
 433     $passwort = "\x30" . $passwort;
 
 435   $header .= $passwort;
 
 437   $anwendungsinfo = "\x20" x 16;
 
 438   $header .= $anwendungsinfo;
 
 439   $inputinfo = "\x20" x 16;
 
 440   $header .= $inputinfo;
 
 445   if ($form->{exporttype} == 0) {
 
 446     $versionssatz = "\xB5" . "1,";
 
 448     $versionssatz = "\xB6" . "1,";
 
 451   $query = qq| select accno from chart limit 1|;
 
 452   $sth   = $dbh->prepare($query);
 
 453   $sth->execute || $form->dberror($query);
 
 454   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 456   $accnolength = $ref->{accno};
 
 459   $versionssatz .= length($accnolength);
 
 460   $versionssatz .= ",";
 
 461   $versionssatz .= length($accnolength);
 
 462   $versionssatz .= ",SELF" . "\x1C\x79";
 
 466   $header .= $versionssatz;
 
 468   $main::lxdebug->leave_sub();
 
 474   $main::lxdebug->enter_sub();
 
 476   my ($date, $six) = @_;
 
 478   ($day, $month, $year) = split /\./, $date;
 
 481     $day = substr($day, 1, 1);
 
 483   if (length($month) < 2) {
 
 484     $month = "0" . $month;
 
 486   if (length($year) > 2) {
 
 487     $year = substr($year, -2, 2);
 
 491     $date = $day . $month . $year;
 
 493     $date = $day . $month;
 
 496   $main::lxdebug->leave_sub();
 
 502   $main::lxdebug->enter_sub();
 
 504   my ($umsatz, $stellen) = @_;
 
 507   ($vorkomma, $nachkomma) = split /\./, $umsatz;
 
 510     for ($i = $stellen; $i >= $stellen + 2 - length($vorkomma); $i--) {
 
 514   for ($i = 3; $i > length($nachkomma); $i--) {
 
 517   $umsatz = $vorkomma . substr($nachkomma, 0, 2);
 
 519   $main::lxdebug->leave_sub();
 
 524 sub make_ed_versionset {
 
 525   $main::lxdebug->enter_sub();
 
 527   my ($header, $filename, $blockcount, $fromto) = @_;
 
 529   $versionset = "V" . substr($filename, 2, 5);
 
 530   $versionset .= substr($header, 6, 22);
 
 532     $versionset .= "0000" . substr($header, 28, 19);
 
 534     $datum = "\x20" x 16;
 
 535     $versionset .= $datum . "001" . substr($header, 28, 4);
 
 537   while (length($blockcount) < 5) {
 
 538     $blockcount = "0" . $blockcount;
 
 540   $versionset .= $blockcount;
 
 541   $versionset .= "001";
 
 542   $versionset .= "\x20\x31";
 
 543   $versionset .= substr($header, -12, 10) . "    ";
 
 544   $versionset .= "\x20" x 53;
 
 546   $main::lxdebug->leave_sub();
 
 552   $main::lxdebug->enter_sub();
 
 554   my ($form, $fileno) = @_;
 
 555   $datentraegernr = $form->{datentraegernr};
 
 556   $beraternummer  = $form->{beraternr};
 
 557   $beratername    = $form->{beratername};
 
 558   $anzahl_dateien = $fileno;
 
 560   while (length($datentraegernr) < 3) {
 
 561     $datentraegernr .= " ";
 
 564   while (length($beraternummer) < 7) {
 
 565     $beraternummer .= " ";
 
 568   while (length($beratername) < 9) {
 
 572   while (length($anzahl_dateien) < 5) {
 
 573     $anzahl_dateien = "0" . $anzahl_dateien;
 
 577     $datentraegernr . "\x20\x20\x20" . $beraternummer . $beratername . "\x20";
 
 578   $ev_header .= $anzahl_dateien . $anzahl_dateien;
 
 579   $ev_header .= "\x20" x 95;
 
 581   $main::lxdebug->leave_sub();
 
 586 sub kne_buchungsexport {
 
 587   $main::lxdebug->enter_sub();
 
 589   my ($myconfig, $form) = @_;
 
 591   my $export_path = "datev/";
 
 592   my $filename    = "ED00000";
 
 600   <body>Export in Bearbeitung<br>
 
 601   Buchungssätze verarbeitet:
 
 605     &get_dates($form->{zeitraum}, $form->{monat},
 
 606                $form->{quartal},  $form->{transdatefrom},
 
 607                $form->{transdateto});
 
 608   &get_transactions($myconfig, $form, $fromto);
 
 610   print qq|<br>2. Durchlauf:|;
 
 611   while (scalar(@{ $form->{DATEV} })) {
 
 613     my $remaining_bytes = 256;
 
 614     my $total_bytes     = 256;
 
 616     my $buchungssatz    = "";
 
 618     my $ed_filename = $export_path . $filename;
 
 619     open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
 
 620     $header = &make_kne_data_header($myconfig, $form, $fromto);
 
 621     $remaining_bytes -= length($header);
 
 623     while (scalar(@{ $form->{DATEV} }) > 0) {
 
 624       $transaction    = shift @{ $form->{DATEV} };
 
 625       $trans_lines    = scalar(@{$transaction});
 
 627       if (($counter % 500) == 0) {
 
 642       %umlaute = ('ä' => 'ae',
 
 650       for (my $i = 0; $i < $trans_lines; $i++) {
 
 651         if (abs($transaction->[$i]->{'umsatz'}) > abs($umsatz)) {
 
 652           $umsatz = $transaction->[$i]->{'umsatz'};
 
 654         if ($transaction->[$i]->{'datevautomatik'}) {
 
 657         if ($transaction->[$i]->{'taxkey'}) {
 
 658           $taxkey = $transaction->[$i]->{'taxkey'};
 
 660         if ($transaction->[$i]->{'charttax'}) {
 
 661           $charttax = $transaction->[$i]->{'charttax'};
 
 663         if (   ($transaction->[$i]->{'id'} eq $transaction->[$i]->{'chart_id'})
 
 664             && ($trans_lines > 2)) {
 
 665           undef($transaction->[$i]);
 
 666             } elsif ($transaction->[$i]->{'amount'} > 0) {
 
 673       $umsatzsumme += abs($umsatz);
 
 675       # Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern
 
 676       foreach $umlaut (keys(%umlaute)) {
 
 677         $transaction->[$haben]->{'invnumber'} =~
 
 678           s/${umlaut}/${umlaute{$umlaut}}/g;
 
 679         $transaction->[$haben]->{'name'} =~ s/${umlaut}/${umlaute{$umlaut}}/g;
 
 682       $transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
 
 683       $transaction->[$haben]->{'name'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
 
 685       $transaction->[$haben]->{'invnumber'} =
 
 686         substr($transaction->[$haben]->{'invnumber'}, 0, 12);
 
 687       $transaction->[$haben]->{'name'} =
 
 688         substr($transaction->[$haben]->{'name'}, 0, 30);
 
 689       $transaction->[$haben]->{'invnumber'} =~ s/\ *$//;
 
 690       $transaction->[$haben]->{'name'}      =~ s/\ *$//;
 
 692       if ($trans_lines >= 2) {
 
 694         $gegenkonto = "a" . $transaction->[$haben]->{'accno'};
 
 695         $konto      = "e" . $transaction->[$soll]->{'accno'};
 
 696         if ($transaction->[$haben]->{'invnumber'} ne "") {
 
 698             "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
 
 701         $datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
 
 702         $waehrung = "\xB3" . "EUR" . "\x1C";
 
 703         if ($transaction->[$haben]->{'name'} ne "") {
 
 704           $buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
 
 706         if ($transaction->[$haben]->{'duedate'} ne "") {
 
 708             . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
 
 712       if (($remaining_bytes - length("+" . &formatumsatz($umsatz, 0))) <= 6) {
 
 713         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 714         $buchungssatz .= "\x00" x $fuellzeichen;
 
 716         $total_bytes = ($blockcount) * 256;
 
 718       $umsatz = abs($umsatz);
 
 719       $vorzeichen = ($umsatz > 0) ? "+" : "-";
 
 720       $buchungssatz .= $vorzeichen . &formatumsatz($umsatz, 0);
 
 721       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 723       if ( ($taxkey || $datevautomatik)
 
 724         && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
 
 725         if (($remaining_bytes - length("\x6C" . "11")) <= 6) {
 
 727             ($blockcount * 256 - length($buchungssatz . $header));
 
 728           $buchungssatz .= "\x00" x $fuellzeichen;
 
 730           $total_bytes = ($blockcount) * 256;
 
 732         if (!$datevautomatik) {
 
 733           $buchungssatz .= "\x6C" . $taxkey;
 
 735           $buchungssatz .= "\x6C" . "4";
 
 737         $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 740       if (($remaining_bytes - length($gegenkonto)) <= 6) {
 
 741         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 742         $buchungssatz .= "\x00" x $fuellzeichen;
 
 744         $total_bytes = ($blockcount) * 256;
 
 746       $buchungssatz .= $gegenkonto;
 
 747       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 749       if (($remaining_bytes - length($belegfeld1)) <= 6) {
 
 750         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 751         $buchungssatz .= "\x00" x $fuellzeichen;
 
 753         $total_bytes = ($blockcount) * 256;
 
 755       $buchungssatz .= $belegfeld1;
 
 756       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 758       if (($remaining_bytes - length($belegfeld2)) <= 6) {
 
 759         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 760         $buchungssatz .= "\x00" x $fuellzeichen;
 
 762         $total_bytes = ($blockcount) * 256;
 
 764       $buchungssatz .= $belegfeld2;
 
 765       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 767       if (($remaining_bytes - length($datum)) <= 6) {
 
 768         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 769         $buchungssatz .= "\x00" x $fuellzeichen;
 
 771         $total_bytes = ($blockcount) * 256;
 
 773       $buchungssatz .= $datum;
 
 774       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 776       if (($remaining_bytes - length($konto)) <= 6) {
 
 777         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 778         $buchungssatz .= "\x00" x $fuellzeichen;
 
 780         $total_bytes = ($blockcount) * 256;
 
 782       $buchungssatz .= $konto;
 
 783       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 785       if (($remaining_bytes - length($buchungstext)) <= 6) {
 
 786         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 787         $buchungssatz .= "\x00" x $fuellzeichen;
 
 789         $total_bytes = ($blockcount) * 256;
 
 791       $buchungssatz .= $buchungstext;
 
 792       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 794       if (($remaining_bytes - (length($waehrung . "\x79"))) <= 6) {
 
 795         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 796         $buchungssatz .= "\x00" x $fuellzeichen;
 
 798         $total_bytes = ($blockcount) * 256;
 
 800       $buchungssatz .= $waehrung . "\x79";
 
 801       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 806       "x" . &formatumsatz($umsatzsumme, 14) . "\x79" . "\x7a";
 
 808       256 - (length($header . $buchungssatz . $mandantenendsumme) % 256);
 
 809     $dateiende = "\x00" x $fuellzeichen;
 
 811     print(ED $buchungssatz);
 
 812     print(ED $mandantenendsumme);
 
 813     print(ED $dateiende);
 
 816     $ed_versionset[$fileno] =
 
 817       &make_ed_versionset($header, $filename, $blockcount, $fromto);
 
 821   #Make EV Verwaltungsdatei
 
 822   $ev_header = &make_ev_header($form, $fileno);
 
 823   $ev_filename = $export_path . $evfile;
 
 824   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
 
 825   print(EV $ev_header);
 
 827   foreach $file (@ed_versionset) {
 
 828     print(EV $ed_versionset[$file]);
 
 831 print qq|<br>Done. <br></body>
 
 835   $main::lxdebug->leave_sub();
 
 838 sub kne_stammdatenexport {
 
 839   $main::lxdebug->enter_sub();
 
 841   my ($myconfig, $form) = @_;
 
 842   $form->{abrechnungsnr} = "99";
 
 844   my $export_path = "datev/";
 
 845   my $filename    = "ED00000";
 
 851   my $remaining_bytes = 256;
 
 852   my $total_bytes     = 256;
 
 853   my $buchungssatz    = "";
 
 855   my $ed_filename = $export_path . $filename;
 
 856   open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
 
 857   $header = &make_kne_data_header($myconfig, $form, "");
 
 858   $remaining_bytes -= length($header);
 
 860   # connect to database
 
 861   my $dbh = $form->dbconnect($myconfig);
 
 864     qq|SELECT c.accno, c.description FROM chart c WHERE c.accno >=|
 
 865     . $dbh->quote($form->{accnofrom}) . qq|
 
 867     . $dbh->quote($form->{accnoto})
 
 868     . qq| ORDER BY c.accno|;
 
 870   $sth = $dbh->prepare($query);
 
 871   $sth->execute || $form->dberror($query);
 
 873   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 874     if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
 
 875       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 876       $buchungssatz .= "\x00" x $fuellzeichen;
 
 878       $total_bytes = ($blockcount) * 256;
 
 880     $buchungssatz .= "t" . $ref->{'accno'};
 
 881     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 882     $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
 
 883     $ref->{'description'} = substr($ref->{'description'}, 0, 40);
 
 884     $ref->{'description'} =~ s/\ *$//;
 
 887         ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
 
 890       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
 
 891       $buchungssatz .= "\x00" x $fuellzeichen;
 
 893       $total_bytes = ($blockcount) * 256;
 
 895     $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
 
 896     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
 
 901   print(ED $buchungssatz);
 
 902   $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
 
 903   $dateiende = "\x00" x $fuellzeichen;
 
 905   print(ED $dateiende);
 
 908   #Make EV Verwaltungsdatei
 
 910     &make_ed_versionset($header, $filename, $blockcount, $fromto);
 
 912   $ev_header = &make_ev_header($form, $fileno);
 
 913   $ev_filename = $export_path . $evfile;
 
 914   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
 
 915   print(EV $ev_header);
 
 917   foreach $file (@ed_versionset) {
 
 918     print(EV $ed_versionset[$file]);
 
 925   $main::lxdebug->leave_sub();