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