Kopie von stable, da die Aufraeumarbeiten jetzt abgeschlossen sind.
[kivitendo-erp.git] / SL / DATEV.pm
1 #=====================================================================
2 # Lx-Office ERP
3 # Copyright (c) 2004
4 #
5 #  Author: Philip Reetz
6 #   Email: p.reetz@linet-services.de
7 #     Web: http://www.lx-office.org
8 #
9 #
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.
14 #
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 #======================================================================
23 #
24 # Datev export module
25 #======================================================================
26
27 package DATEV;
28
29 use Data::Dumper;
30
31 sub get_datev_stamm {
32   $main::lxdebug->enter_sub();
33
34   my ($self, $myconfig, $form) = @_;
35
36   # connect to database
37   my $dbh = $form->dbconnect($myconfig);
38
39   $query = qq|SELECT * FROM datev|;
40   $sth   = $dbh->prepare($query);
41   $sth->execute || $form->dberror($query);
42
43   my $ref = $sth->fetchrow_hashref(NAME_lc);
44
45   map { $form->{$_} = $ref->{$_} } keys %$ref;
46
47   $sth->finish;
48   $dbh->disconnect;
49   $main::lxdebug->leave_sub();
50 }
51
52 sub save_datev_stamm {
53   $main::lxdebug->enter_sub();
54
55   my ($self, $myconfig, $form) = @_;
56
57   # connect to database
58   my $dbh = $form->dbconnect_noauto($myconfig);
59
60   $query = qq|DELETE FROM datev|;
61   $dbh->do($query) || $form->dberror($query);
62
63   $query = qq|INSERT INTO datev
64               (beraternr, beratername, dfvkz, mandantennr, datentraegernr, abrechnungsnr) VALUES
65               (|
66     . $dbh->quote($form->{beraternr}) . qq|,|
67     . $dbh->quote($form->{beratername}) . qq|,|
68     . $dbh->quote($form->{dfvkz}) . qq|,
69               |
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);
75   $sth->finish;
76
77   $dbh->commit;
78   $dbh->disconnect;
79   $main::lxdebug->leave_sub();
80 }
81
82 sub kne_export {
83   $main::lxdebug->enter_sub();
84
85   my ($self, $myconfig, $form) = @_;
86   my $rc;
87
88   if ($form->{exporttype} == 0) {
89     $rc = &kne_buchungsexport($myconfig, $form);
90   } else {
91     $rc = &kne_stammdatenexport($myconfig, $form);
92   }
93
94   $main::lxdebug->leave_sub();
95
96   return $rc;
97 }
98
99 sub obe_export {
100   $main::lxdebug->enter_sub();
101
102   my ($self, $myconfig, $form) = @_;
103
104   # connect to database
105   my $dbh = $form->dbconnect_noauto($myconfig);
106   $dbh->commit;
107   $dbh->disconnect;
108   $main::lxdebug->leave_sub();
109 }
110
111 sub get_dates {
112   $main::lxdebug->enter_sub();
113
114   my ($zeitraum, $monat, $quartal, $transdatefrom, $transdateto) = @_;
115
116   $fromto = "transdate >= ";
117
118   my @a = localtime;
119   $a[5] += 1900;
120   $jahr = $a[5];
121   if ($zeitraum eq "monat") {
122   SWITCH: {
123       $monat eq "1" && do {
124         $form->{fromdate} = "1.1.$jahr";
125         $form->{todate}   = "31.1.$jahr";
126         last SWITCH;
127       };
128       $monat eq "2" && do {
129         $form->{fromdate} = "1.2.$jahr";
130
131         #this works from 1901 to 2099, 1900 and 2100 fail.
132         $leap = ($jahr % 4 == 0) ? "29" : "28";
133         $form->{todate} = "$leap.2.$jahr";
134         last SWITCH;
135       };
136       $monat eq "3" && do {
137         $form->{fromdate} = "1.3.$jahr";
138         $form->{todate}   = "31.3.$jahr";
139         last SWITCH;
140       };
141       $monat eq "4" && do {
142         $form->{fromdate} = "1.4.$jahr";
143         $form->{todate}   = "30.4.$jahr";
144         last SWITCH;
145       };
146       $monat eq "5" && do {
147         $form->{fromdate} = "1.5.$jahr";
148         $form->{todate}   = "31.5.$jahr";
149         last SWITCH;
150       };
151       $monat eq "6" && do {
152         $form->{fromdate} = "1.6.$jahr";
153         $form->{todate}   = "30.6.$jahr";
154         last SWITCH;
155       };
156       $monat eq "7" && do {
157         $form->{fromdate} = "1.7.$jahr";
158         $form->{todate}   = "31.7.$jahr";
159         last SWITCH;
160       };
161       $monat eq "8" && do {
162         $form->{fromdate} = "1.8.$jahr";
163         $form->{todate}   = "31.8.$jahr";
164         last SWITCH;
165       };
166       $monat eq "9" && do {
167         $form->{fromdate} = "1.9.$jahr";
168         $form->{todate}   = "30.9.$jahr";
169         last SWITCH;
170       };
171       $monat eq "10" && do {
172         $form->{fromdate} = "1.10.$jahr";
173         $form->{todate}   = "31.10.$jahr";
174         last SWITCH;
175       };
176       $monat eq "11" && do {
177         $form->{fromdate} = "1.11.$jahr";
178         $form->{todate}   = "30.11.$jahr";
179         last SWITCH;
180       };
181       $monat eq "12" && do {
182         $form->{fromdate} = "1.12.$jahr";
183         $form->{todate}   = "31.12.$jahr";
184         last SWITCH;
185       };
186     }
187     $fromto .=
188       "'" . $form->{fromdate} . "' and transdate <= '" . $form->{todate} . "'";
189   }
190
191   elsif ($zeitraum eq "quartal") {
192     if ($quartal == 1) {
193       $fromto .=
194         "'01.01." . $jahr . "' and transdate <= '31.03." . $jahr . "'";
195     } elsif ($quartal == 2) {
196       $fromto .=
197         "'01.04." . $jahr . "' and transdate <= '30.06." . $jahr . "'";
198     } elsif ($quartal == 3) {
199       $fromto .=
200         "'01.07." . $jahr . "' and transdate <= '30.09." . $jahr . "'";
201     } elsif ($quartal == 4) {
202       $fromto .=
203         "'01.10." . $jahr . "' and transdate <= '31.12." . $jahr . "'";
204     }
205   }
206
207   elsif ($zeitraum eq "zeit") {
208     $fromto .=
209       "'" . $transdatefrom . "' and transdate <= '" . $transdateto . "'";
210   }
211
212   $main::lxdebug->leave_sub();
213
214   return $fromto;
215 }
216
217 sub get_transactions {
218   $main::lxdebug->enter_sub();
219
220   my ($myconfig, $form, $fromto) = @_;
221
222   # connect to database
223   my $dbh = $form->dbconnect($myconfig);
224
225   $fromto =~ s/transdate/ac\.transdate/g;
226
227   $query = qq|SELECT taxkey, rate FROM tax|;
228   $sth   = $dbh->prepare($query);
229   $sth->execute || $form->dberror($query);
230
231   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
232     $taxes{ $ref->{taxkey} } = $ref->{rate};
233   }
234
235   $sth->finish();
236
237   $query =
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
243               UNION ALL
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
246                  (t.chart_id=c.id)
247               WHERE $fromto AND ac.trans_id=ap.id AND ap.vendor_id=ct.id AND ac.chart_id=c.id
248               UNION ALL
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|;
254
255   $sth = $dbh->prepare($query);
256   $sth->execute || $form->dberror($query);
257   $i = 0;
258   $g = 0;
259   @splits;
260   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
261     $count    = 0;
262     $firstrun = 1;
263     $count += $ref->{amount};
264     push @{$i}, $ref;
265     while (abs($count) > 0.01 || $firstrun) {
266       $ref2 = $sth->fetchrow_hashref(NAME_lc);
267       last unless ($ref2);
268       $count += $ref2->{amount};
269       push @{$i}, $ref2;
270       $firstrun = 0;
271     }
272     $absumsatz = 0;
273     if (scalar(@{$i}) > 2) {
274       for my $j (0 .. (scalar(@{$i}) - 1)) {
275         if (abs($i->[$j]->{'amount'}) > abs($absumsatz)) {
276           $absumsatz     = $i->[$j]->{'amount'};
277           $notsplitindex = $j;
278         }
279       }
280       $ml = ($i->[0]->{'umsatz'} > 0) ? 1 : -1;
281       for my $j (0 .. (scalar(@{$i}) - 1)) {
282         if (   ($j != $notsplitindex)
283             && ($i->[$j]->{'chart_id'}  eq "")
284             && (   $i->[$j]->{'taxkey'} eq ""
285                 || $i->[$j]->{'taxkey'} eq "0"
286                 || $i->[$j]->{'taxkey'} eq "1"
287                 || $i->[$j]->{'taxkey'} eq "10"
288                 || $i->[$j]->{'taxkey'} eq "11")
289           ) {
290           my %blubb = {};
291           map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
292               keys(%{ $i->[$notsplitindex] }));
293           $absumsatz += $i->[$j]->{'amount'};
294           $blubb{'amount'}     = $i->[$j]->{'amount'} * (-1);
295           $blubb{'umsatz'}     = abs($i->[$j]->{'amount'}) * $ml;
296           $i->[$j]->{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml;
297           push @{ $splits[$g] }, \%blubb;    #$i->[$notsplitindex];
298           push @{ $splits[$g] }, $i->[$j];
299           push @{ $form->{DATEV} }, \@{ $splits[$g] };
300           $g++;
301           } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) {
302           $absumsatz +=
303             ($i->[$j]->{'amount'} * (1 + $taxes{ $i->[$j]->{'taxkey'} }));
304           my %blubb = {};
305           map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
306               keys(%{ $i->[$notsplitindex] }));
307           $test = 1 + $taxes{ $i->[$j]->{'taxkey'} };
308           $blubb{'amount'} =
309             $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2);
310
311           #print(STDERR $test, " Taxrate\n\n");
312           $blubb{'umsatz'} =
313             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
314
315           $i->[$j]->{'umsatz'} =
316             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
317
318           #print(STDERR $i->[$j]->{'umsatz'}, " Steuer Umsatz\n");
319           #print(STDERR $i->[$j]->{'amount'}, " Steuer Betrag\n");
320           #print(STDERR $blubb{'umsatz'}, " Umsatz NOTSPLIT\n");
321           push @{ $splits[$g] }, \%blubb;
322           push @{ $splits[$g] }, $i->[$j];
323           push @{ $form->{DATEV} }, \@{ $splits[$g] };
324           $g++;
325           } else {
326           next;
327         }
328       }
329       if (abs($absumsatz) > 0.01) {
330         print(STDERR $absumsatz, "ABSAUMSATZ\n");
331         $form->error("Datev-Export fehlgeschlagen!");
332       }
333     } else {
334       push @{ $form->{DATEV} }, \@{$i};
335     }
336     $i++;
337   }
338   $sth->finish;
339   $dbh->disconnect;
340
341   $main::lxdebug->leave_sub();
342 }
343
344 sub make_kne_data_header {
345   $main::lxdebug->enter_sub();
346
347   my ($myconfig, $form, $fromto) = @_;
348
349   # connect to database
350   my $dbh = $form->dbconnect($myconfig);
351
352   my @a = localtime;
353   $jahr = $a[5];
354
355   #Header
356   $anwendungsnr = ($fromto) ? "\x31\x31" : "\x31\x33";
357   while (length($form->{datentraegernr}) < 3) {
358     $form->{datentraegernr} = "\x30" . $form->{datentraegernr};
359   }
360
361   $header = "\x1D\x18\x31" . $form->{datentraegernr} . $anwendungsnr;
362
363   $dfvkz = $form->{dfvkz};
364   while (length($dfvkz) < 2) {
365     $dfvkz = "\x30" . $dfvkz;
366   }
367   $header .= $dfvkz;
368
369   $beraternr = $form->{beraternr};
370   while (length($beraternr) < 7) {
371     $beraternr = "\x30" . $beraternr;
372   }
373   $header .= $beraternr;
374
375   $mandantennr = $form->{mandantennr};
376   while (length($mandantennr) < 5) {
377     $mandantennr = "\x30" . $mandantennr;
378   }
379   $header .= $mandantennr;
380
381   $abrechnungsnr = $form->{abrechnungsnr} . $jahr;
382   while (length($abrechnungsnr) < 6) {
383     $abrechnungsnr = "\x30" . $abrechnungsnr;
384   }
385   $header .= $abrechnungsnr;
386
387   $fromto =~ s/transdate|>=|and|\'|<=//g;
388   my ($from, $to) = split /   /, $fromto;
389   $from =~ s/ //g;
390   $to   =~ s/ //g;
391
392   if ($from ne "") {
393     my ($fday, $fmonth, $fyear) = split /\./, $from;
394     if (length($fmonth) < 2) {
395       $fmonth = "0" . $fmonth;
396     }
397     if (length($fday) < 2) {
398       $fday = "0" . $fday;
399     }
400     $from = $fday . $fmonth . substr($fyear, -2, 2);
401   } else {
402     $from = "";
403   }
404
405   $header .= $from;
406
407   if ($to ne "") {
408     my ($tday, $tmonth, $tyear) = split /\./, $to;
409     if (length($tmonth) < 2) {
410       $tmonth = "0" . $tmonth;
411     }
412     if (length($tday) < 2) {
413       $tday = "0" . $tday;
414     }
415     $to = $tday . $tmonth . substr($tyear, -2, 2);
416   } else {
417     $to = "";
418   }
419   $header .= $to;
420   if ($fromto ne "") {
421     $primanota = "\x30\x30\x31";
422     $header .= $primanota;
423   }
424
425   $passwort = $form->{passwort};
426   while (length($passwort) < 4) {
427     $passwort = "\x30" . $passwort;
428   }
429   $header .= $passwort;
430
431   $anwendungsinfo = "\x20" x 16;
432   $header .= $anwendungsinfo;
433   $inputinfo = "\x20" x 16;
434   $header .= $inputinfo;
435
436   $header .= "\x79";
437
438   #Versionssatz
439   if ($form->{exporttype} == 0) {
440     $versionssatz = "\xB5" . "1,";
441   } else {
442     $versionssatz = "\xB6" . "1,";
443   }
444
445   $query = qq| select accno from chart limit 1|;
446   $sth   = $dbh->prepare($query);
447   $sth->execute || $form->dberror($query);
448   my $ref = $sth->fetchrow_hashref(NAME_lc);
449
450   $accnolength = $ref->{accno};
451   $sth->finish;
452
453   $versionssatz .= length($accnolength);
454   $versionssatz .= ",";
455   $versionssatz .= length($accnolength);
456   $versionssatz .= ",SELF" . "\x1C\x79";
457
458   $dbh->disconnect;
459
460   $header .= $versionssatz;
461
462   $main::lxdebug->leave_sub();
463
464   return $header;
465 }
466
467 sub datetofour {
468   $main::lxdebug->enter_sub();
469
470   my ($date, $six) = @_;
471
472   ($day, $month, $year) = split /\./, $date;
473
474   if ($day =~ /^0/) {
475     $day = substr($day, 1, 1);
476   }
477   if (length($month) < 2) {
478     $month = "0" . $month;
479   }
480   if (length($year) > 2) {
481     $year = substr($year, -2, 2);
482   }
483
484   if ($six) {
485     $date = $day . $month . $year;
486   } else {
487     $date = $day . $month;
488   }
489
490   $main::lxdebug->leave_sub();
491
492   return $date;
493 }
494
495 sub formatumsatz {
496   $main::lxdebug->enter_sub();
497
498   my ($umsatz, $stellen) = @_;
499
500   $umsatz =~ s/-//;
501   ($vorkomma, $nachkomma) = split /\./, $umsatz;
502   $umsatz = "";
503   if ($stellen > 0) {
504     for ($i = $stellen; $i >= $stellen + 2 - length($vorkomma); $i--) {
505       $umsatz .= "0";
506     }
507   }
508   for ($i = 3; $i > length($nachkomma); $i--) {
509     $nachkomma .= "0";
510   }
511   $umsatz = $vorkomma . substr($nachkomma, 0, 2);
512
513   $main::lxdebug->leave_sub();
514
515   return $umsatz;
516 }
517
518 sub make_ed_versionset {
519   $main::lxdebug->enter_sub();
520
521   my ($header, $filename, $blockcount, $fromto) = @_;
522
523   $versionset = "V" . substr($filename, 2, 5);
524   $versionset .= substr($header, 6, 22);
525   if ($fromto ne "") {
526     $versionset .= "0000" . substr($header, 28, 19);
527   } else {
528     $datum = "\x20" x 16;
529     $versionset .= $datum . "001" . substr($header, 28, 4);
530   }
531   while (length($blockcount) < 5) {
532     $blockcount = "0" . $blockcount;
533   }
534   $versionset .= $blockcount;
535   $versionset .= "001";
536   $versionset .= "\x20\x31";
537   $versionset .= substr($header, -12, 10) . "    ";
538   $versionset .= "\x20" x 53;
539
540   $main::lxdebug->leave_sub();
541
542   return $versionset;
543 }
544
545 sub make_ev_header {
546   $main::lxdebug->enter_sub();
547
548   my ($form, $fileno) = @_;
549   $datentraegernr = $form->{datentraegernr};
550   $beraternummer  = $form->{beraternr};
551   $beratername    = $form->{beratername};
552   $anzahl_dateien = $fileno;
553
554   while (length($datentraegernr) < 3) {
555     $datentraegernr .= " ";
556   }
557
558   while (length($beraternummer) < 7) {
559     $beraternummer .= " ";
560   }
561
562   while (length($beratername) < 9) {
563     $beratername .= " ";
564   }
565
566   while (length($anzahl_dateien) < 5) {
567     $anzahl_dateien = "0" . $anzahl_dateien;
568   }
569
570   $ev_header =
571     $datentraegernr . "\x20\x20\x20" . $beraternummer . $beratername . "\x20";
572   $ev_header .= $anzahl_dateien . $anzahl_dateien;
573   $ev_header .= "\x20" x 95;
574
575   $main::lxdebug->leave_sub();
576
577   return $ev_header;
578 }
579
580 sub kne_buchungsexport {
581   $main::lxdebug->enter_sub();
582
583   my ($myconfig, $form) = @_;
584
585   my $export_path = "datev/";
586   my $filename    = "ED00000";
587   my $evfile      = "EV01";
588   my @ed_versionsets;
589   my $fileno = 0;
590
591   $fromto =
592     &get_dates($form->{zeitraum}, $form->{monat},
593                $form->{quartal},  $form->{transdatefrom},
594                $form->{transdateto});
595   &get_transactions($myconfig, $form, $fromto);
596
597   while (scalar(@{ $form->{DATEV} })) {
598     my $blockcount      = 1;
599     my $remaining_bytes = 256;
600     my $total_bytes     = 256;
601     my $umsatzsumme     = 0;
602     my $buchungssatz    = "";
603     $filename++;
604     my $ed_filename = $export_path . $filename;
605     open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
606     $header = &make_kne_data_header($myconfig, $form, $fromto);
607     $remaining_bytes -= length($header);
608
609     while (scalar(@{ $form->{DATEV} }) > 0) {
610       $transaction    = shift @{ $form->{DATEV} };
611       $trans_lines    = scalar(@{$transaction});
612       $umsatz         = 0;
613       $gegenkonto     = "";
614       $konto          = "";
615       $belegfeld1     = "";
616       $datum          = "";
617       $waehrung       = "";
618       $buchungstext   = "";
619       $belegfeld2     = "";
620       $datevautomatik = 0;
621       $taxkey         = 0;
622       $charttax       = 0;
623       %umlaute = ('ä' => 'ae',
624                   'ö' => 'oe',
625                   'ü' => 'ue',
626                   'Ä' => 'Ae',
627                   'Ö' => 'Oe',
628                   'Ãœ' => 'Ue',
629                   'ß' => 'sz');
630
631       for (my $i = 0; $i < $trans_lines; $i++) {
632         if (abs($transaction->[$i]->{'umsatz'}) > abs($umsatz)) {
633           $umsatz = $transaction->[$i]->{'umsatz'};
634         }
635         if ($transaction->[$i]->{'datevautomatik'}) {
636           $datevautomatik = 1;
637         }
638         if ($transaction->[$i]->{'taxkey'}) {
639           $taxkey = $transaction->[$i]->{'taxkey'};
640         }
641         if ($transaction->[$i]->{'charttax'}) {
642           $charttax = $transaction->[$i]->{'charttax'};
643         }
644         if (   ($transaction->[$i]->{'id'} eq $transaction->[$i]->{'chart_id'})
645             && ($trans_lines > 2)) {
646           undef($transaction->[$i]);
647             } elsif ($transaction->[$i]->{'amount'} > 0) {
648           $haben = $i;
649             } else {
650           $soll = $i;
651         }
652       }
653
654       $umsatzsumme += $umsatz;
655
656       # Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern
657       foreach $umlaut (keys(%umlaute)) {
658         $transaction->[$haben]->{'invnumber'} =~
659           s/${umlaut}/${umlaute{$umlaut}}/g;
660         $transaction->[$haben]->{'name'} =~ s/${umlaut}/${umlaute{$umlaut}}/g;
661       }
662
663       $transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
664       $transaction->[$haben]->{'name'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
665
666       $transaction->[$haben]->{'invnumber'} =
667         substr($transaction->[$haben]->{'invnumber'}, 0, 12);
668       $transaction->[$haben]->{'name'} =
669         substr($transaction->[$haben]->{'name'}, 0, 30);
670       $transaction->[$haben]->{'invnumber'} =~ s/\ *$//;
671       $transaction->[$haben]->{'name'}      =~ s/\ *$//;
672
673       if ($trans_lines >= 2) {
674
675         $gegenkonto = "a" . $transaction->[$haben]->{'accno'};
676         $konto      = "e" . $transaction->[$soll]->{'accno'};
677         if ($transaction->[$haben]->{'invnumber'} ne "") {
678           $belegfeld1 =
679             "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
680         }
681         $datum = "d";
682         $datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
683         $waehrung = "\xB3" . "EUR" . "\x1C";
684         if ($transaction->[$haben]->{'name'} ne "") {
685           $buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
686         }
687         if ($transaction->[$haben]->{'duedate'} ne "") {
688           $belegfeld2 = "\xBE"
689             . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
690         }
691       }
692
693       if (($remaining_bytes - length("+" . &formatumsatz($umsatz, 0))) <= 6) {
694         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
695         $buchungssatz .= "\x00" x $fuellzeichen;
696         $blockcount++;
697         $total_bytes = ($blockcount) * 256;
698       }
699       $vorzeichen = ($umsatz > 0) ? "+" : "-";
700       $buchungssatz .= $vorzeichen . &formatumsatz($umsatz, 0);
701       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
702
703       if ( ($taxkey || $datevautomatik)
704         && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
705         if (($remaining_bytes - length("\x6C" . "11")) <= 6) {
706           $fuellzeichen =
707             ($blockcount * 256 - length($buchungssatz . $header));
708           $buchungssatz .= "\x00" x $fuellzeichen;
709           $blockcount++;
710           $total_bytes = ($blockcount) * 256;
711         }
712         if (!$datevautomatik) {
713           $buchungssatz .= "\x6C" . $taxkey;
714         } else {
715           $buchungssatz .= "\x6C" . "4";
716         }
717         $remaining_bytes = $total_bytes - length($buchungssatz . $header);
718       }
719
720       if (($remaining_bytes - length($gegenkonto)) <= 6) {
721         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
722         $buchungssatz .= "\x00" x $fuellzeichen;
723         $blockcount++;
724         $total_bytes = ($blockcount) * 256;
725       }
726       $buchungssatz .= $gegenkonto;
727       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
728
729       if (($remaining_bytes - length($belegfeld1)) <= 6) {
730         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
731         $buchungssatz .= "\x00" x $fuellzeichen;
732         $blockcount++;
733         $total_bytes = ($blockcount) * 256;
734       }
735       $buchungssatz .= $belegfeld1;
736       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
737
738       if (($remaining_bytes - length($belegfeld2)) <= 6) {
739         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
740         $buchungssatz .= "\x00" x $fuellzeichen;
741         $blockcount++;
742         $total_bytes = ($blockcount) * 256;
743       }
744       $buchungssatz .= $belegfeld2;
745       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
746
747       if (($remaining_bytes - length($datum)) <= 6) {
748         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
749         $buchungssatz .= "\x00" x $fuellzeichen;
750         $blockcount++;
751         $total_bytes = ($blockcount) * 256;
752       }
753       $buchungssatz .= $datum;
754       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
755
756       if (($remaining_bytes - length($konto)) <= 6) {
757         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
758         $buchungssatz .= "\x00" x $fuellzeichen;
759         $blockcount++;
760         $total_bytes = ($blockcount) * 256;
761       }
762       $buchungssatz .= $konto;
763       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
764
765       if (($remaining_bytes - length($buchungstext)) <= 6) {
766         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
767         $buchungssatz .= "\x00" x $fuellzeichen;
768         $blockcount++;
769         $total_bytes = ($blockcount) * 256;
770       }
771       $buchungssatz .= $buchungstext;
772       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
773
774       if (($remaining_bytes - (length($waehrung . "\x79"))) <= 6) {
775         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
776         $buchungssatz .= "\x00" x $fuellzeichen;
777         $blockcount++;
778         $total_bytes = ($blockcount) * 256;
779       }
780       $buchungssatz .= $waehrung . "\x79";
781       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
782
783     }
784
785     $mandantenendsumme =
786       "x" . &formatumsatz($umsatzsumme, 14) . "\x79" . "\x7a";
787     $fuellzeichen =
788       256 - (length($header . $buchungssatz . $mandantenendsumme) % 256);
789     $dateiende = "\x00" x $fuellzeichen;
790     print(ED $header);
791     print(ED $buchungssatz);
792     print(ED $mandantenendsumme);
793     print(ED $dateiende);
794     close(ED);
795
796     $ed_versionset[$fileno] =
797       &make_ed_versionset($header, $filename, $blockcount, $fromto);
798     $fileno++;
799   }
800
801   #Make EV Verwaltungsdatei
802   $ev_header = &make_ev_header($form, $fileno);
803   $ev_filename = $export_path . $evfile;
804   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
805   print(EV $ev_header);
806
807   foreach $file (@ed_versionset) {
808     print(EV $ed_versionset[$file]);
809   }
810   close(EV);
811   ###
812   $main::lxdebug->leave_sub();
813 }
814
815 sub kne_stammdatenexport {
816   $main::lxdebug->enter_sub();
817
818   my ($myconfig, $form) = @_;
819   $form->{abrechnungsnr} = "99";
820
821   my $export_path = "datev/";
822   my $filename    = "ED00000";
823   my $evfile      = "EV01";
824   my @ed_versionsets;
825   my $fileno          = 1;
826   my $i               = 0;
827   my $blockcount      = 1;
828   my $remaining_bytes = 256;
829   my $total_bytes     = 256;
830   my $buchungssatz    = "";
831   $filename++;
832   my $ed_filename = $export_path . $filename;
833   open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
834   $header = &make_kne_data_header($myconfig, $form, "");
835   $remaining_bytes -= length($header);
836
837   # connect to database
838   my $dbh = $form->dbconnect($myconfig);
839
840   $query =
841     qq|SELECT c.accno, c.description FROM chart c WHERE c.accno >=|
842     . $dbh->quote($form->{accnofrom}) . qq|
843            AND c.accno <= |
844     . $dbh->quote($form->{accnoto})
845     . qq| ORDER BY c.accno|;
846
847   $sth = $dbh->prepare($query);
848   $sth->execute || $form->dberror($query);
849
850   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
851     if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
852       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
853       $buchungssatz .= "\x00" x $fuellzeichen;
854       $blockcount++;
855       $total_bytes = ($blockcount) * 256;
856     }
857     $buchungssatz .= "t" . $ref->{'accno'};
858     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
859     $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
860     $ref->{'description'} = substr($ref->{'description'}, 0, 40);
861     $ref->{'description'} =~ s/\ *$//;
862
863     if (
864         ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
865         ) <= 6
866       ) {
867       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
868       $buchungssatz .= "\x00" x $fuellzeichen;
869       $blockcount++;
870       $total_bytes = ($blockcount) * 256;
871     }
872     $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
873     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
874   }
875
876   $sth->finish;
877   print(ED $header);
878   print(ED $buchungssatz);
879   $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
880   $dateiende = "\x00" x $fuellzeichen;
881   print(ED "z");
882   print(ED $dateiende);
883   close(ED);
884
885   #Make EV Verwaltungsdatei
886   $ed_versionset[0] =
887     &make_ed_versionset($header, $filename, $blockcount, $fromto);
888
889   $ev_header = &make_ev_header($form, $fileno);
890   $ev_filename = $export_path . $evfile;
891   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
892   print(EV $ev_header);
893
894   foreach $file (@ed_versionset) {
895     print(EV $ed_versionset[$file]);
896   }
897   close(EV);
898
899   $dbh->disconnect;
900   ###
901
902   $main::lxdebug->leave_sub();
903 }
904
905 1;