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