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