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