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