Der Datevexport wird nicht mehr in Dateien im datev/-Verzeichnis geschrieben, sondern...
[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   my $counter = 0;
260   @splits;
261   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
262     $count    = 0;
263     $firstrun = 1;
264     $counter++;
265     if (($counter % 500) == 0) {
266       print("$counter ");
267     }
268
269     $count += $ref->{amount};
270     push @{$i}, $ref;
271     while (abs($count) > 0.01 || $firstrun) {
272       $ref2 = $sth->fetchrow_hashref(NAME_lc);
273       last unless ($ref2);
274       $count += $ref2->{amount};
275       push @{$i}, $ref2;
276       $firstrun = 0;
277     }
278     $absumsatz = 0;
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'};
283           $notsplitindex = $j;
284         }
285       }
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")
295           ) {
296           my %blubb = {};
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] };
306           $g++;
307           } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) {
308           $absumsatz +=
309             ($i->[$j]->{'amount'} * (1 + $taxes{ $i->[$j]->{'taxkey'} }));
310           my %blubb = {};
311           map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; }
312               keys(%{ $i->[$notsplitindex] }));
313           $test = 1 + $taxes{ $i->[$j]->{'taxkey'} };
314           $blubb{'amount'} =
315             $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2);
316
317           $blubb{'umsatz'} =
318             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
319
320           $i->[$j]->{'umsatz'} =
321             abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml;
322
323           push @{ $splits[$g] }, \%blubb;
324           push @{ $splits[$g] }, $i->[$j];
325           push @{ $form->{DATEV} }, \@{ $splits[$g] };
326           $g++;
327           } else {
328           next;
329         }
330       }
331       if (abs($absumsatz) > 0.01) {
332         $form->error("Datev-Export fehlgeschlagen! Bei Transaktion $i->[0]->{trans_id}\n");
333       }
334     } else {
335       push @{ $form->{DATEV} }, \@{$i};
336     }
337     $i++;
338   }
339   $sth->finish;
340   $dbh->disconnect;
341
342   $main::lxdebug->leave_sub();
343 }
344
345 sub make_kne_data_header {
346   $main::lxdebug->enter_sub();
347
348   my ($myconfig, $form, $fromto) = @_;
349
350   # connect to database
351   my $dbh = $form->dbconnect($myconfig);
352
353   my @a = localtime;
354   $jahr = $a[5];
355
356   #Header
357   $anwendungsnr = ($fromto) ? "\x31\x31" : "\x31\x33";
358   while (length($form->{datentraegernr}) < 3) {
359     $form->{datentraegernr} = "\x30" . $form->{datentraegernr};
360   }
361
362   $header = "\x1D\x18\x31" . $form->{datentraegernr} . $anwendungsnr;
363
364   $dfvkz = $form->{dfvkz};
365   while (length($dfvkz) < 2) {
366     $dfvkz = "\x30" . $dfvkz;
367   }
368   $header .= $dfvkz;
369
370   $beraternr = $form->{beraternr};
371   while (length($beraternr) < 7) {
372     $beraternr = "\x30" . $beraternr;
373   }
374   $header .= $beraternr;
375
376   $mandantennr = $form->{mandantennr};
377   while (length($mandantennr) < 5) {
378     $mandantennr = "\x30" . $mandantennr;
379   }
380   $header .= $mandantennr;
381
382   $abrechnungsnr = $form->{abrechnungsnr} . $jahr;
383   while (length($abrechnungsnr) < 6) {
384     $abrechnungsnr = "\x30" . $abrechnungsnr;
385   }
386   $header .= $abrechnungsnr;
387
388   $fromto =~ s/transdate|>=|and|\'|<=//g;
389   my ($from, $to) = split /   /, $fromto;
390   $from =~ s/ //g;
391   $to   =~ s/ //g;
392
393   if ($from ne "") {
394     my ($fday, $fmonth, $fyear) = split(/\./, $from);
395     if (length($fmonth) < 2) {
396       $fmonth = "0" . $fmonth;
397     }
398     if (length($fday) < 2) {
399       $fday = "0" . $fday;
400     }
401     $from = $fday . $fmonth . substr($fyear, -2, 2);
402   } else {
403     $from = "";
404   }
405
406   $header .= $from;
407
408   if ($to ne "") {
409     my ($tday, $tmonth, $tyear) = split(/\./, $to);
410     if (length($tmonth) < 2) {
411       $tmonth = "0" . $tmonth;
412     }
413     if (length($tday) < 2) {
414       $tday = "0" . $tday;
415     }
416     $to = $tday . $tmonth . substr($tyear, -2, 2);
417   } else {
418     $to = "";
419   }
420   $header .= $to;
421   if ($fromto ne "") {
422     $primanota = "\x30\x30\x31";
423     $header .= $primanota;
424   }
425
426   $passwort = $form->{passwort};
427   while (length($passwort) < 4) {
428     $passwort = "\x30" . $passwort;
429   }
430   $header .= $passwort;
431
432   $anwendungsinfo = "\x20" x 16;
433   $header .= $anwendungsinfo;
434   $inputinfo = "\x20" x 16;
435   $header .= $inputinfo;
436
437   $header .= "\x79";
438
439   #Versionssatz
440   if ($form->{exporttype} == 0) {
441     $versionssatz = "\xB5" . "1,";
442   } else {
443     $versionssatz = "\xB6" . "1,";
444   }
445
446   $query = qq| select accno from chart limit 1|;
447   $sth   = $dbh->prepare($query);
448   $sth->execute || $form->dberror($query);
449   my $ref = $sth->fetchrow_hashref(NAME_lc);
450
451   $accnolength = $ref->{accno};
452   $sth->finish;
453
454   $versionssatz .= length($accnolength);
455   $versionssatz .= ",";
456   $versionssatz .= length($accnolength);
457   $versionssatz .= ",SELF" . "\x1C\x79";
458
459   $dbh->disconnect;
460
461   $header .= $versionssatz;
462
463   $main::lxdebug->leave_sub();
464
465   return $header;
466 }
467
468 sub datetofour {
469   $main::lxdebug->enter_sub();
470
471   my ($date, $six) = @_;
472
473   ($day, $month, $year) = split(/\./, $date);
474
475   if ($day =~ /^0/) {
476     $day = substr($day, 1, 1);
477   }
478   if (length($month) < 2) {
479     $month = "0" . $month;
480   }
481   if (length($year) > 2) {
482     $year = substr($year, -2, 2);
483   }
484
485   if ($six) {
486     $date = $day . $month . $year;
487   } else {
488     $date = $day . $month;
489   }
490
491   $main::lxdebug->leave_sub();
492
493   return $date;
494 }
495
496 sub formatumsatz {
497   $main::lxdebug->enter_sub();
498
499   my ($umsatz, $stellen) = @_;
500
501   $umsatz =~ s/-//;
502   ($vorkomma, $nachkomma) = split(/\./, $umsatz);
503   $umsatz = "";
504   if ($stellen > 0) {
505     for ($i = $stellen; $i >= $stellen + 2 - length($vorkomma); $i--) {
506       $umsatz .= "0";
507     }
508   }
509   for ($i = 3; $i > length($nachkomma); $i--) {
510     $nachkomma .= "0";
511   }
512   $umsatz = $vorkomma . substr($nachkomma, 0, 2);
513
514   $main::lxdebug->leave_sub();
515
516   return $umsatz;
517 }
518
519 sub make_ed_versionset {
520   $main::lxdebug->enter_sub();
521
522   my ($header, $filename, $blockcount, $fromto) = @_;
523
524   $versionset = "V" . substr($filename, 2, 5);
525   $versionset .= substr($header, 6, 22);
526   if ($fromto ne "") {
527     $versionset .= "0000" . substr($header, 28, 19);
528   } else {
529     $datum = "\x20" x 16;
530     $versionset .= $datum . "001" . substr($header, 28, 4);
531   }
532   while (length($blockcount) < 5) {
533     $blockcount = "0" . $blockcount;
534   }
535   $versionset .= $blockcount;
536   $versionset .= "001";
537   $versionset .= "\x20\x31";
538   $versionset .= substr($header, -12, 10) . "    ";
539   $versionset .= "\x20" x 53;
540
541   $main::lxdebug->leave_sub();
542
543   return $versionset;
544 }
545
546 sub make_ev_header {
547   $main::lxdebug->enter_sub();
548
549   my ($form, $fileno) = @_;
550   $datentraegernr = $form->{datentraegernr};
551   $beraternummer  = $form->{beraternr};
552   $beratername    = $form->{beratername};
553   $anzahl_dateien = $fileno;
554
555   while (length($datentraegernr) < 3) {
556     $datentraegernr .= " ";
557   }
558
559   while (length($beraternummer) < 7) {
560     $beraternummer .= " ";
561   }
562
563   while (length($beratername) < 9) {
564     $beratername .= " ";
565   }
566
567   while (length($anzahl_dateien) < 5) {
568     $anzahl_dateien = "0" . $anzahl_dateien;
569   }
570
571   $ev_header =
572     $datentraegernr . "\x20\x20\x20" . $beraternummer . $beratername . "\x20";
573   $ev_header .= $anzahl_dateien . $anzahl_dateien;
574   $ev_header .= "\x20" x 95;
575
576   $main::lxdebug->leave_sub();
577
578   return $ev_header;
579 }
580
581 sub kne_buchungsexport {
582   $main::lxdebug->enter_sub();
583
584   my ($myconfig, $form) = @_;
585
586   my @filenames;
587
588   my $export_path = $main::userspath . "/";
589   my $filename    = "ED00000";
590   my $evfile      = "EV01";
591   my @ed_versionsets;
592   my $fileno = 0;
593
594   $form->header;
595   print qq|
596   <html>
597   <body>Export in Bearbeitung<br>
598   Buchungss&auml;tze verarbeitet:
599 |;
600
601   $fromto =
602     &get_dates($form->{zeitraum}, $form->{monat},
603                $form->{quartal},  $form->{transdatefrom},
604                $form->{transdateto});
605   &get_transactions($myconfig, $form, $fromto);
606   my $counter = 0;
607   print qq|<br>2. Durchlauf:|;
608   while (scalar(@{ $form->{DATEV} })) {
609     my $blockcount      = 1;
610     my $remaining_bytes = 256;
611     my $total_bytes     = 256;
612     my $umsatzsumme     = 0;
613     my $buchungssatz    = "";
614     $filename++;
615     my $ed_filename = $export_path . $filename;
616     push(@filenames, $filename);
617     open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
618     $header = &make_kne_data_header($myconfig, $form, $fromto);
619     $remaining_bytes -= length($header);
620
621     while (scalar(@{ $form->{DATEV} }) > 0) {
622       $transaction = shift @{ $form->{DATEV} };
623       $trans_lines = scalar(@{$transaction});
624       $counter++;
625       if (($counter % 500) == 0) {
626         print("$counter ");
627       }
628
629       $umsatz         = 0;
630       $gegenkonto     = "";
631       $konto          = "";
632       $belegfeld1     = "";
633       $datum          = "";
634       $waehrung       = "";
635       $buchungstext   = "";
636       $belegfeld2     = "";
637       $datevautomatik = 0;
638       $taxkey         = 0;
639       $charttax       = 0;
640       %umlaute = ('ä' => 'ae',
641                   'ö' => 'oe',
642                   'ü' => 'ue',
643                   'Ä' => 'Ae',
644                   'Ö' => 'Oe',
645                   'Ãœ' => 'Ue',
646                   'ß' => 'sz');
647
648       for (my $i = 0; $i < $trans_lines; $i++) {
649         if ($trans_lines == 2) {
650           if (abs($transaction->[$i]->{'amount'}) > abs($umsatz)) {
651             $umsatz = $transaction->[$i]->{'amount'};
652           }
653         } else {
654           if (abs($transaction->[$i]->{'umsatz'}) > abs($umsatz)) {
655             $umsatz = $transaction->[$i]->{'umsatz'};
656           }
657         }
658         if ($transaction->[$i]->{'datevautomatik'}) {
659           $datevautomatik = 1;
660         }
661         if ($transaction->[$i]->{'taxkey'}) {
662           $taxkey = $transaction->[$i]->{'taxkey'};
663         }
664         if ($transaction->[$i]->{'charttax'}) {
665           $charttax = $transaction->[$i]->{'charttax'};
666         }
667         if (   ($transaction->[$i]->{'id'} eq $transaction->[$i]->{'chart_id'})
668             && ($trans_lines > 2)) {
669           undef($transaction->[$i]);
670             } elsif ($transaction->[$i]->{'amount'} > 0) {
671           $haben = $i;
672             } else {
673           $soll = $i;
674         }
675       }
676
677       $umsatzsumme += abs($umsatz);
678
679       # Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern
680       foreach $umlaut (keys(%umlaute)) {
681         $transaction->[$haben]->{'invnumber'} =~
682           s/${umlaut}/${umlaute{$umlaut}}/g;
683         $transaction->[$haben]->{'name'} =~ s/${umlaut}/${umlaute{$umlaut}}/g;
684       }
685
686       $transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
687       $transaction->[$haben]->{'name'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
688
689       $transaction->[$haben]->{'invnumber'} =
690         substr($transaction->[$haben]->{'invnumber'}, 0, 12);
691       $transaction->[$haben]->{'name'} =
692         substr($transaction->[$haben]->{'name'}, 0, 30);
693       $transaction->[$haben]->{'invnumber'} =~ s/\ *$//;
694       $transaction->[$haben]->{'name'}      =~ s/\ *$//;
695
696       if ($trans_lines >= 2) {
697
698         $gegenkonto = "a" . $transaction->[$haben]->{'accno'};
699         $konto      = "e" . $transaction->[$soll]->{'accno'};
700         if ($transaction->[$haben]->{'invnumber'} ne "") {
701           $belegfeld1 =
702             "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
703         }
704         $datum = "d";
705         $datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
706         $waehrung = "\xB3" . "EUR" . "\x1C";
707         if ($transaction->[$haben]->{'name'} ne "") {
708           $buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
709         }
710         if ($transaction->[$haben]->{'duedate'} ne "") {
711           $belegfeld2 = "\xBE"
712             . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
713         }
714       }
715
716       if (($remaining_bytes - length("+" . &formatumsatz($umsatz, 0))) <= 6) {
717         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
718         $buchungssatz .= "\x00" x $fuellzeichen;
719         $blockcount++;
720         $total_bytes = ($blockcount) * 256;
721       }
722       $umsatz = abs($umsatz);
723       $vorzeichen = ($umsatz > 0) ? "+" : "-";
724       $buchungssatz .= $vorzeichen . &formatumsatz($umsatz, 0);
725       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
726
727       if ( ($taxkey || $datevautomatik)
728         && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
729         if (($remaining_bytes - length("\x6C" . "11")) <= 6) {
730           $fuellzeichen =
731             ($blockcount * 256 - length($buchungssatz . $header));
732           $buchungssatz .= "\x00" x $fuellzeichen;
733           $blockcount++;
734           $total_bytes = ($blockcount) * 256;
735         }
736         if (!$datevautomatik) {
737           $buchungssatz .= "\x6C" . $taxkey;
738         } else {
739           $buchungssatz .= "\x6C" . "4";
740         }
741         $remaining_bytes = $total_bytes - length($buchungssatz . $header);
742       }
743
744       if (($remaining_bytes - length($gegenkonto)) <= 6) {
745         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
746         $buchungssatz .= "\x00" x $fuellzeichen;
747         $blockcount++;
748         $total_bytes = ($blockcount) * 256;
749       }
750       $buchungssatz .= $gegenkonto;
751       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
752
753       if (($remaining_bytes - length($belegfeld1)) <= 6) {
754         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
755         $buchungssatz .= "\x00" x $fuellzeichen;
756         $blockcount++;
757         $total_bytes = ($blockcount) * 256;
758       }
759       $buchungssatz .= $belegfeld1;
760       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
761
762       if (($remaining_bytes - length($belegfeld2)) <= 6) {
763         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
764         $buchungssatz .= "\x00" x $fuellzeichen;
765         $blockcount++;
766         $total_bytes = ($blockcount) * 256;
767       }
768       $buchungssatz .= $belegfeld2;
769       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
770
771       if (($remaining_bytes - length($datum)) <= 6) {
772         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
773         $buchungssatz .= "\x00" x $fuellzeichen;
774         $blockcount++;
775         $total_bytes = ($blockcount) * 256;
776       }
777       $buchungssatz .= $datum;
778       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
779
780       if (($remaining_bytes - length($konto)) <= 6) {
781         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
782         $buchungssatz .= "\x00" x $fuellzeichen;
783         $blockcount++;
784         $total_bytes = ($blockcount) * 256;
785       }
786       $buchungssatz .= $konto;
787       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
788
789       if (($remaining_bytes - length($buchungstext)) <= 6) {
790         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
791         $buchungssatz .= "\x00" x $fuellzeichen;
792         $blockcount++;
793         $total_bytes = ($blockcount) * 256;
794       }
795       $buchungssatz .= $buchungstext;
796       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
797
798       if (($remaining_bytes - (length($waehrung . "\x79"))) <= 6) {
799         $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
800         $buchungssatz .= "\x00" x $fuellzeichen;
801         $blockcount++;
802         $total_bytes = ($blockcount) * 256;
803       }
804       $buchungssatz .= $waehrung . "\x79";
805       $remaining_bytes = $total_bytes - length($buchungssatz . $header);
806
807     }
808
809     $mandantenendsumme =
810       "x" . &formatumsatz($umsatzsumme, 14) . "\x79" . "\x7a";
811     $fuellzeichen =
812       256 - (length($header . $buchungssatz . $mandantenendsumme) % 256);
813     $dateiende = "\x00" x $fuellzeichen;
814     print(ED $header);
815     print(ED $buchungssatz);
816     print(ED $mandantenendsumme);
817     print(ED $dateiende);
818     close(ED);
819
820     $ed_versionset[$fileno] =
821       &make_ed_versionset($header, $filename, $blockcount, $fromto);
822     $fileno++;
823   }
824
825   #Make EV Verwaltungsdatei
826   $ev_header = &make_ev_header($form, $fileno);
827   $ev_filename = $export_path . $evfile;
828   push(@filenames, $evfile);
829   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
830   print(EV $ev_header);
831
832   foreach $file (@ed_versionset) {
833     print(EV $ed_versionset[$file]);
834   }
835   close(EV);
836   print qq|<br>Done. <br>
837 |;
838   ###
839   $main::lxdebug->leave_sub();
840
841   return @filenames;
842 }
843
844 sub kne_stammdatenexport {
845   $main::lxdebug->enter_sub();
846
847   my ($myconfig, $form) = @_;
848   $form->{abrechnungsnr} = "99";
849
850   $form->header;
851   print qq|
852   <html>
853   <body>Export in Bearbeitung<br>
854 |;
855
856   my @filenames;
857
858   my $export_path = $main::userspath . "/";
859   my $filename    = "ED00000";
860   my $evfile      = "EV01";
861   my @ed_versionsets;
862   my $fileno          = 1;
863   my $i               = 0;
864   my $blockcount      = 1;
865   my $remaining_bytes = 256;
866   my $total_bytes     = 256;
867   my $buchungssatz    = "";
868   $filename++;
869   my $ed_filename = $export_path . $filename;
870   push(@filenames, $filename);
871   open(ED, "> $ed_filename") or die "can't open outputfile: $!\n";
872   $header = &make_kne_data_header($myconfig, $form, "");
873   $remaining_bytes -= length($header);
874
875   # connect to database
876   my $dbh = $form->dbconnect($myconfig);
877
878   $query =
879     qq|SELECT c.accno, c.description FROM chart c WHERE c.accno >=|
880     . $dbh->quote($form->{accnofrom}) . qq|
881            AND c.accno <= |
882     . $dbh->quote($form->{accnoto}) . qq| ORDER BY c.accno|;
883
884   $sth = $dbh->prepare($query);
885   $sth->execute || $form->dberror($query);
886
887   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
888     if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
889       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
890       $buchungssatz .= "\x00" x $fuellzeichen;
891       $blockcount++;
892       $total_bytes = ($blockcount) * 256;
893     }
894     $buchungssatz .= "t" . $ref->{'accno'};
895     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
896     $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
897     $ref->{'description'} = substr($ref->{'description'}, 0, 40);
898     $ref->{'description'} =~ s/\ *$//;
899
900     if (
901         ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
902         ) <= 6
903       ) {
904       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
905       $buchungssatz .= "\x00" x $fuellzeichen;
906       $blockcount++;
907       $total_bytes = ($blockcount) * 256;
908     }
909     $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
910     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
911   }
912
913   $sth->finish;
914   print(ED $header);
915   print(ED $buchungssatz);
916   $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
917   $dateiende = "\x00" x $fuellzeichen;
918   print(ED "z");
919   print(ED $dateiende);
920   close(ED);
921
922   #Make EV Verwaltungsdatei
923   $ed_versionset[0] =
924     &make_ed_versionset($header, $filename, $blockcount, $fromto);
925
926   $ev_header = &make_ev_header($form, $fileno);
927   $ev_filename = $export_path . $evfile;
928   push(@filenames, $evfile);
929   open(EV, "> $ev_filename") or die "can't open outputfile: EV01\n";
930   print(EV $ev_header);
931
932   foreach $file (@ed_versionset) {
933     print(EV $ed_versionset[$file]);
934   }
935   close(EV);
936
937   $dbh->disconnect;
938   ###
939
940   print qq|<br>Done. <br>
941 |;
942
943   $main::lxdebug->leave_sub();
944
945   return @filenames;
946 }
947
948 1;