DATEV Export modular gekapselt.
[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 SL::DATEV;
28
29 use utf8;
30 use strict;
31
32 use SL::DBUtils;
33 use SL::DATEV::KNEFile;
34 use SL::Taxkeys;
35
36 use Data::Dumper;
37 use DateTime;
38 use Exporter qw(import);
39 use File::Path;
40 use List::Util qw(max sum);
41 use Time::HiRes qw(gettimeofday);
42
43 {
44   my $i = 0;
45   use constant {
46     DATEV_ET_BUCHUNGEN => $i++,
47     DATEV_ET_STAMM     => $i++,
48
49     DATEV_FORMAT_KNE   => $i++,
50     DATEV_FORMAT_OBE   => $i++,
51   };
52 }
53
54 my @export_constants = qw(DATEV_ET_BUCHUNGEN DATEV_ET_STAMM DATEV_FORMAT_KNE DATEV_FORMAT_OBE);
55 our @EXPORT_OK = (@export_constants);
56 our %EXPORT_TAGS = (CONSTANTS => [ @export_constants ]);
57
58
59 sub new {
60   my $class = shift;
61   my %data  = @_;
62
63   my $obj = bless {}, $class;
64
65   $obj->$_($data{$_}) for keys %data;
66
67   $obj;
68 }
69
70 sub exporttype {
71   my $self = shift;
72   $self->{exporttype} = $_[0] if @_;
73   return $self->{exporttype};
74 }
75
76 sub has_exporttype {
77   defined $_[0]->{exporttype};
78 }
79
80 sub format {
81   my $self = shift;
82   $self->{format} = $_[0] if @_;
83   return $self->{format};
84 }
85
86 sub has_format {
87   defined $_[0]->{format};
88 }
89
90 sub _get_export_path {
91   $main::lxdebug->enter_sub();
92
93   my ($a, $b) = gettimeofday();
94   my $path    = _get_path_for_download_token("${a}-${b}-${$}");
95
96   mkpath($path) unless (-d $path);
97
98   $main::lxdebug->leave_sub();
99
100   return $path;
101 }
102
103 sub _get_path_for_download_token {
104   $main::lxdebug->enter_sub();
105
106   my $token = shift || '';
107   my $path;
108
109   if ($token =~ m|^(\d+)-(\d+)-(\d+)$|) {
110     $path = $::lx_office_conf{paths}->{userspath} . "/datev-export-${1}-${2}-${3}/";
111   }
112
113   $main::lxdebug->leave_sub();
114
115   return $path;
116 }
117
118 sub _get_download_token_for_path {
119   $main::lxdebug->enter_sub();
120
121   my $path = shift;
122   my $token;
123
124   if ($path =~ m|.*datev-export-(\d+)-(\d+)-(\d+)/?$|) {
125     $token = "${1}-${2}-${3}";
126   }
127
128   $main::lxdebug->leave_sub();
129
130   return $token;
131 }
132
133 sub download_token {
134   my $self = shift;
135   $self->{download_token} = $_[0] if @_;
136   return $self->{download_token} ||= _get_download_token_for_path($self->export_path);
137 }
138
139 sub export_path {
140   my ($self) = @_;
141
142   return  $self->{export_path} ||= _get_path_for_download_token($self->{download_token}) || _get_export_path();
143 }
144
145 sub add_filenames {
146   my $self = shift;
147   push @{ $self->{filenames} ||= [] }, @_;
148 }
149
150 sub filenames {
151   return @{ $_[0]{filenames} || [] };
152 }
153
154 sub add_error {
155   my $self = shift;
156   push @{ $self->{errors} ||= [] }, @_;
157 }
158
159 sub errors {
160   return @{ $_[0]{errors} || [] };
161 }
162
163 sub add_net_gross_differences {
164   my $self = shift;
165   push @{ $self->{net_gross_differences} ||= [] }, @_;
166 }
167
168 sub net_gross_differences {
169   return @{ $_[0]{net_gross_differences} || [] };
170 }
171
172 sub sum_net_gross_differences {
173   return sum $_[0]->net_gross_differences;
174 }
175
176 sub from {
177  my $self = shift;
178
179  if (@_) {
180    $self->{from} = $_[0];
181  }
182
183  return $self->{from};
184 }
185
186 sub to {
187  my $self = shift;
188
189  if (@_) {
190    $self->{to} = $_[0];
191  }
192
193  return $self->{to};
194 }
195
196 sub accnofrom {
197  my $self = shift;
198
199  if (@_) {
200    $self->{accnofrom} = $_[0];
201  }
202
203  return $self->{accnofrom};
204 }
205
206 sub accnoto {
207  my $self = shift;
208
209  if (@_) {
210    $self->{accnoto} = $_[0];
211  }
212
213  return $self->{accnoto};
214 }
215
216
217 sub dbh {
218   my $self = shift;
219
220   if (@_) {
221     $self->{dbh} = $_[0];
222     $self->{provided_dbh} = 1;
223   }
224
225   $self->{dbh} ||= $::form->get_standard_dbh;
226 }
227
228 sub provided_dbh {
229   $_[0]{provided_dbh};
230 }
231
232 sub clean_temporary_directories {
233   $::lxdebug->enter_sub;
234
235   foreach my $path (glob($::lx_office_conf{paths}->{userspath} . "/datev-export-*")) {
236     next unless -d $path;
237
238     my $mtime = (stat($path))[9];
239     next if ((time() - $mtime) < 8 * 60 * 60);
240
241     rmtree $path;
242   }
243
244   $::lxdebug->leave_sub;
245 }
246
247 sub _fill {
248   $main::lxdebug->enter_sub();
249
250   my $text      = shift;
251   my $field_len = shift;
252   my $fill_char = shift;
253   my $alignment = shift || 'right';
254
255   my $text_len  = length $text;
256
257   if ($field_len < $text_len) {
258     $text = substr $text, 0, $field_len;
259
260   } elsif ($field_len > $text_len) {
261     my $filler = ($fill_char) x ($field_len - $text_len);
262     $text      = $alignment eq 'right' ? $filler . $text : $text . $filler;
263   }
264
265   $main::lxdebug->leave_sub();
266
267   return $text;
268 }
269
270 sub get_datev_stamm {
271   return $_[0]{stamm} ||= selectfirst_hashref_query($::form, $_[0]->dbh, 'SELECT * FROM datev');
272 }
273
274 sub save_datev_stamm {
275   my ($self, $data) = @_;
276
277   do_query($::form, $self->dbh, 'DELETE FROM datev');
278
279   my @columns = qw(beraternr beratername dfvkz mandantennr datentraegernr abrechnungsnr);
280
281   my $query = "INSERT INTO datev (" . join(', ', @columns) . ") VALUES (" . join(', ', ('?') x @columns) . ")";
282   do_query($::form, $self->dbh, $query, map { $data->{$_} } @columns);
283
284   $self->dbh->commit unless $self->provided_dbh;
285 }
286
287 sub export {
288   my ($self) = @_;
289   my $result;
290
291   die 'no format set!' unless $self->has_format;
292
293   if ($self->format == DATEV_FORMAT_KNE) {
294     $result = $self->kne_export;
295   } elsif ($self->format == DATEV_FORMAT_OBE) {
296     $result = $self->obe_export;
297   } else {
298     die 'unrecognized export format';
299   }
300
301   return $result;
302 }
303
304 sub kne_export {
305   my ($self) = @_;
306   my $result;
307
308   die 'no exporttype set!' unless $self->has_exporttype;
309
310   if ($self->exporttype == DATEV_ET_BUCHUNGEN) {
311     $result = $self->kne_buchungsexport;
312   } elsif ($self->exporttype == DATEV_ET_STAMM) {
313     $result = $self->kne_stammdatenexport;
314   } else {
315     die 'unrecognized exporttype';
316   }
317
318   return $result;
319 }
320
321 sub obe_export {
322   die 'not yet implemented';
323 }
324
325 sub fromto {
326   my ($self) = @_;
327
328   return unless $self->from && $self->to;
329
330   return "transdate >= '" . $self->from->to_lxoffice . "' and transdate <= '" . $self->to->to_lxoffice . "'";
331 }
332
333 sub _sign {
334   $_[0] <=> 0;
335 }
336
337 sub _get_transactions {
338   $main::lxdebug->enter_sub();
339   my $self     = shift;
340   my $fromto   =  shift;
341   my $progress_callback = shift || sub {};
342
343   my $form     =  $main::form;
344
345   my ($notsplitindex);
346
347   $fromto      =~ s/transdate/ac\.transdate/g;
348
349   my $taxkeys  = Taxkeys->new();
350   my $filter   = '';            # Useful for debugging purposes
351
352   my %all_taxchart_ids = selectall_as_map($form, $self->dbh, qq|SELECT DISTINCT chart_id, TRUE AS is_set FROM tax|, 'chart_id', 'is_set');
353
354   my $query    =
355     qq|SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey,
356          ar.invnumber, ar.duedate, ar.amount as umsatz,
357          ct.name,
358          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
359          ar.invoice
360        FROM acc_trans ac
361        LEFT JOIN ar          ON (ac.trans_id    = ar.id)
362        LEFT JOIN customer ct ON (ar.customer_id = ct.id)
363        LEFT JOIN chart c     ON (ac.chart_id    = c.id)
364        WHERE (ar.id IS NOT NULL)
365          AND $fromto
366          $filter
367
368        UNION ALL
369
370        SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,ap.id, ac.amount, ac.taxkey,
371          ap.invnumber, ap.duedate, ap.amount as umsatz,
372          ct.name,
373          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
374          ap.invoice
375        FROM acc_trans ac
376        LEFT JOIN ap        ON (ac.trans_id  = ap.id)
377        LEFT JOIN vendor ct ON (ap.vendor_id = ct.id)
378        LEFT JOIN chart c   ON (ac.chart_id  = c.id)
379        WHERE (ap.id IS NOT NULL)
380          AND $fromto
381          $filter
382
383        UNION ALL
384
385        SELECT ac.acc_trans_id, ac.transdate, ac.trans_id,gl.id, ac.amount, ac.taxkey,
386          gl.reference AS invnumber, gl.transdate AS duedate, ac.amount as umsatz,
387          gl.description AS name,
388          c.accno, c.taxkey_id as charttax, c.datevautomatik, c.id, c.link,
389          FALSE AS invoice
390        FROM acc_trans ac
391        LEFT JOIN gl      ON (ac.trans_id  = gl.id)
392        LEFT JOIN chart c ON (ac.chart_id  = c.id)
393        WHERE (gl.id IS NOT NULL)
394          AND $fromto
395          $filter
396
397        ORDER BY trans_id, acc_trans_id|;
398
399   my $sth = prepare_execute_query($form, $self->dbh, $query);
400   $self->{DATEV} = [];
401
402   my $counter = 0;
403   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
404     $counter++;
405     if (($counter % 500) == 0) {
406       $progress_callback->($counter);
407     }
408
409     my $trans    = [ $ref ];
410
411     my $count    = $ref->{amount};
412     my $firstrun = 1;
413     my $subcent  = abs($count) < 0.02;
414
415     while (abs($count) > 0.01 || $firstrun || ($subcent && abs($count) > 0.005)) {
416       my $ref2 = $sth->fetchrow_hashref("NAME_lc");
417       last unless ($ref2);
418
419       if ($ref2->{trans_id} != $trans->[0]->{trans_id}) {
420         $self->add_error("Unbalanced ledger! old trans_id " . $trans->[0]->{trans_id} . " new trans_id " . $ref2->{trans_id} . " count $count");
421         return;
422       }
423
424       push @{ $trans }, $ref2;
425
426       $count    += $ref2->{amount};
427       $firstrun  = 0;
428     }
429
430     foreach my $i (0 .. scalar(@{ $trans }) - 1) {
431       my $ref        = $trans->[$i];
432       my $prev_ref   = 0 < $i ? $trans->[$i - 1] : undef;
433       if (   $all_taxchart_ids{$ref->{id}}
434           && ($ref->{link} =~ m/(?:AP_tax|AR_tax)/)
435           && (   ($prev_ref && $prev_ref->{taxkey} && (_sign($ref->{amount}) == _sign($prev_ref->{amount})))
436               || $ref->{invoice})) {
437         $ref->{is_tax} = 1;
438       }
439
440       if (   !$ref->{invoice}   # we have a non-invoice booking (=gl)
441           &&  $ref->{is_tax}    # that has "is_tax" set
442           && !($prev_ref->{is_tax})  # previous line wasn't is_tax
443           &&  (_sign($ref->{amount}) == _sign($prev_ref->{amount}))) {  # and sign same as previous sign
444         $trans->[$i - 1]->{tax_amount} = $ref->{amount};
445       }
446     }
447
448     my %taxid_taxkeys = ();
449     my $absumsatz     = 0;
450     if (scalar(@{$trans}) <= 2) {
451       push @{ $self->{DATEV} }, $trans;
452       next;
453     }
454
455     for my $j (0 .. (scalar(@{$trans}) - 1)) {
456       # for gl-bookings no split is allowed and there is no AR/AP account, so we always use the maximum value as a reference
457       # for ap/ar bookings we can always search for AR/AP in link and use that
458       if ( ( not $trans->[$j]->{'invoice'} and abs($trans->[$j]->{'amount'}) > abs($absumsatz) )
459          or ($trans->[$j]->{'invoice'} and ($trans->[$j]->{'link'} eq 'AR' or $trans->[$j]->{'link'} eq 'AP'))) {
460         $absumsatz     = $trans->[$j]->{'amount'};
461         $notsplitindex = $j;
462       }
463     }
464
465     my $ml             = ($trans->[0]->{'umsatz'} > 0) ? 1 : -1;
466     my $rounding_error = 0;
467     my @taxed;
468
469     for my $j (0 .. (scalar(@{$trans}) - 1)) {
470       if (   ($j != $notsplitindex)
471           && !$trans->[$j]->{is_tax}
472           && (   $trans->[$j]->{'taxkey'} eq ""
473               || $trans->[$j]->{'taxkey'} eq "0"
474               || $trans->[$j]->{'taxkey'} eq "1"
475               || $trans->[$j]->{'taxkey'} eq "10"
476               || $trans->[$j]->{'taxkey'} eq "11")) {
477         my %new_trans = ();
478         map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
479
480         $absumsatz               += $trans->[$j]->{'amount'};
481         $new_trans{'amount'}      = $trans->[$j]->{'amount'} * (-1);
482         $new_trans{'umsatz'}      = abs($trans->[$j]->{'amount'}) * $ml;
483         $trans->[$j]->{'umsatz'}  = abs($trans->[$j]->{'amount'}) * $ml;
484
485         push @{ $self->{DATEV} }, [ \%new_trans, $trans->[$j] ];
486
487       } elsif (($j != $notsplitindex) && !$trans->[$j]->{is_tax}) {
488         my %tax_info = $taxkeys->get_full_tax_info('transdate' => $trans->[$j]->{transdate});
489
490         my %new_trans = ();
491         map { $new_trans{$_} = $trans->[$notsplitindex]->{$_}; } keys %{ $trans->[$notsplitindex] };
492
493         my $tax_rate              = $tax_info{taxkeys}->{ $trans->[$j]->{'taxkey'} }->{taxrate};
494         $new_trans{'net_amount'}  = $trans->[$j]->{'amount'} * -1;
495         $new_trans{'tax_rate'}    = 1 + $tax_rate;
496
497         if (!$trans->[$j]->{'invoice'}) {
498           $new_trans{'amount'}      = $form->round_amount(-1 * ($trans->[$j]->{amount} + $trans->[$j]->{tax_amount}), 2);
499           $new_trans{'umsatz'}      = abs($new_trans{'amount'}) * $ml;
500           $trans->[$j]->{'umsatz'}  = $new_trans{'umsatz'};
501           $absumsatz               += -1 * $new_trans{'amount'};
502
503         } else {
504           my $unrounded             = $trans->[$j]->{'amount'} * (1 + $tax_rate) * -1 + $rounding_error;
505           my $rounded               = $form->round_amount($unrounded, 2);
506
507           $rounding_error           = $unrounded - $rounded;
508           $new_trans{'amount'}      = $rounded;
509           $new_trans{'umsatz'}      = abs($rounded) * $ml;
510           $trans->[$j]->{'umsatz'}  = $new_trans{umsatz};
511           $absumsatz               -= $rounded;
512         }
513
514         push @{ $self->{DATEV} }, [ \%new_trans, $trans->[$j] ];
515         push @taxed, $self->{DATEV}->[-1];
516       }
517     }
518
519     my $idx        = 0;
520     my $correction = 0;
521     while ((abs($absumsatz) >= 0.01) && (abs($absumsatz) < 1.00)) {
522       if ($idx >= scalar @taxed) {
523         last if (!$correction);
524
525         $correction = 0;
526         $idx        = 0;
527       }
528
529       my $transaction = $taxed[$idx]->[0];
530
531       my $old_amount     = $transaction->{amount};
532       my $old_correction = $correction;
533       my @possible_diffs;
534
535       if (!$transaction->{diff}) {
536         @possible_diffs = (0.01, -0.01);
537       } else {
538         @possible_diffs = ($transaction->{diff});
539       }
540
541       foreach my $diff (@possible_diffs) {
542         my $net_amount = $form->round_amount(($transaction->{amount} + $diff) / $transaction->{tax_rate}, 2);
543         next if ($net_amount != $transaction->{net_amount});
544
545         $transaction->{diff}    = $diff;
546         $transaction->{amount} += $diff;
547         $transaction->{umsatz} += $diff;
548         $absumsatz             -= $diff;
549         $correction             = 1;
550
551         last;
552       }
553
554       $idx++;
555     }
556
557     $absumsatz = $form->round_amount($absumsatz, 2);
558     if (abs($absumsatz) >= (0.01 * (1 + scalar @taxed))) {
559       $self->add_error("Datev-Export fehlgeschlagen! Bei Transaktion $trans->[0]->{trans_id} ($absumsatz)");
560
561     } elsif (abs($absumsatz) >= 0.01) {
562       $self->add_net_gross_differences($absumsatz);
563     }
564   }
565
566   $sth->finish();
567
568   $::lxdebug->leave_sub;
569 }
570
571 sub make_kne_data_header {
572   $main::lxdebug->enter_sub();
573
574   my ($self, $form) = @_;
575   my ($primanota);
576
577   my $stamm = $self->get_datev_stamm;
578
579   my $jahr = $self->from ? $self->from->year : DateTime->today->year;
580
581   #Header
582   my $header  = "\x1D\x181";
583   $header    .= _fill($stamm->{datentraegernr}, 3, ' ', 'left');
584   $header    .= ($self->fromto) ? "11" : "13"; # Anwendungsnummer
585   $header    .= _fill($stamm->{dfvkz}, 2, '0');
586   $header    .= _fill($stamm->{beraternr}, 7, '0');
587   $header    .= _fill($stamm->{mandantennr}, 5, '0');
588   $header    .= _fill($stamm->{abrechnungsnr} . $jahr, 6, '0');
589
590   $header .= $self->from ? $self->from->strftime('%d%m%y') : '';
591   $header .= $self->to   ? $self->to->strftime('%d%m%y')   : '';
592
593   if ($self->fromto) {
594     $primanota = "001";
595     $header .= $primanota;
596   }
597
598   $header .= _fill($stamm->{passwort}, 4, '0');
599   $header .= " " x 16;       # Anwendungsinfo
600   $header .= " " x 16;       # Inputinfo
601   $header .= "\x79";
602
603   #Versionssatz
604   my $versionssatz  = $self->exporttype == DATEV_ET_BUCHUNGEN ? "\xB5" . "1," : "\xB6" . "1,";
605
606   my $query         = qq|SELECT accno FROM chart LIMIT 1|;
607   my $ref           = selectfirst_hashref_query($form, $self->dbh, $query);
608
609   $versionssatz    .= length $ref->{accno};
610   $versionssatz    .= ",";
611   $versionssatz    .= length $ref->{accno};
612   $versionssatz    .= ",SELF" . "\x1C\x79";
613
614   $header          .= $versionssatz;
615
616   $main::lxdebug->leave_sub();
617
618   return $header;
619 }
620
621 sub datetofour {
622   $main::lxdebug->enter_sub();
623
624   my ($date, $six) = @_;
625
626   my ($day, $month, $year) = split(/\./, $date);
627
628   if ($day =~ /^0/) {
629     $day = substr($day, 1, 1);
630   }
631   if (length($month) < 2) {
632     $month = "0" . $month;
633   }
634   if (length($year) > 2) {
635     $year = substr($year, -2, 2);
636   }
637
638   if ($six) {
639     $date = $day . $month . $year;
640   } else {
641     $date = $day . $month;
642   }
643
644   $main::lxdebug->leave_sub();
645
646   return $date;
647 }
648
649 sub trim_leading_zeroes {
650   my $str = shift;
651
652   $str =~ s/^0+//g;
653
654   return $str;
655 }
656
657 sub make_ed_versionset {
658   $main::lxdebug->enter_sub();
659
660   my ($self, $header, $filename, $blockcount) = @_;
661
662   my $versionset  = "V" . substr($filename, 2, 5);
663   $versionset    .= substr($header, 6, 22);
664
665   if ($self->fromto) {
666     $versionset .= "0000" . substr($header, 28, 19);
667   } else {
668     my $datum = " " x 16;
669     $versionset .= $datum . "001" . substr($header, 28, 4);
670   }
671
672   $versionset .= _fill($blockcount, 5, '0');
673   $versionset .= "001";
674   $versionset .= " 1";
675   $versionset .= substr($header, -12, 10) . "    ";
676   $versionset .= " " x 53;
677
678   $main::lxdebug->leave_sub();
679
680   return $versionset;
681 }
682
683 sub make_ev_header {
684   $main::lxdebug->enter_sub();
685
686   my ($self, $form, $fileno) = @_;
687
688   my $stamm = $self->get_datev_stamm;
689
690   my $ev_header  = _fill($stamm->{datentraegernr}, 3, ' ', 'left');
691   $ev_header    .= "   ";
692   $ev_header    .= _fill($stamm->{beraternr}, 7, ' ', 'left');
693   $ev_header    .= _fill($stamm->{beratername}, 9, ' ', 'left');
694   $ev_header    .= " ";
695   $ev_header    .= (_fill($fileno, 5, '0')) x 2;
696   $ev_header    .= " " x 95;
697
698   $main::lxdebug->leave_sub();
699
700   return $ev_header;
701 }
702
703 sub kne_buchungsexport {
704   $main::lxdebug->enter_sub();
705
706   my ($self) = @_;
707
708   my $form = $::form;
709
710   my @filenames;
711
712   my $filename    = "ED00000";
713   my $evfile      = "EV01";
714   my @ed_versionset;
715   my $fileno = 0;
716
717   my $fromto = $self->fromto;
718
719   $self->_get_transactions($fromto);
720
721   return if $self->errors;
722
723   my $counter = 0;
724
725   while (scalar(@{ $self->{DATEV} || [] })) {
726     my $umsatzsumme = 0;
727     $filename++;
728     my $ed_filename = $self->export_path . $filename;
729     push(@filenames, $filename);
730     my $header = $self->make_kne_data_header($form);
731
732     my $kne_file = SL::DATEV::KNEFile->new();
733     $kne_file->add_block($header);
734
735     while (scalar(@{ $self->{DATEV} }) > 0) {
736       my $transaction = shift @{ $self->{DATEV} };
737       my $trans_lines = scalar(@{$transaction});
738       $counter++;
739
740       my $umsatz         = 0;
741       my $gegenkonto     = "";
742       my $konto          = "";
743       my $belegfeld1     = "";
744       my $datum          = "";
745       my $waehrung       = "";
746       my $buchungstext   = "";
747       my $belegfeld2     = "";
748       my $datevautomatik = 0;
749       my $taxkey         = 0;
750       my $charttax       = 0;
751       my ($haben, $soll);
752       my $iconv          = $::locale->{iconv_utf8};
753       my %umlaute = ($iconv->convert('ä') => 'ae',
754                      $iconv->convert('ö') => 'oe',
755                      $iconv->convert('ü') => 'ue',
756                      $iconv->convert('Ä') => 'Ae',
757                      $iconv->convert('Ö') => 'Oe',
758                      $iconv->convert('Ü') => 'Ue',
759                      $iconv->convert('ß') => 'sz');
760       for (my $i = 0; $i < $trans_lines; $i++) {
761         if ($trans_lines == 2) {
762           if (abs($transaction->[$i]->{'amount'}) > abs($umsatz)) {
763             $umsatz = $transaction->[$i]->{'amount'};
764           }
765         } else {
766           if (abs($transaction->[$i]->{'umsatz'}) > abs($umsatz)) {
767             $umsatz = $transaction->[$i]->{'umsatz'};
768           }
769         }
770         if ($transaction->[$i]->{'datevautomatik'}) {
771           $datevautomatik = 1;
772         }
773         if ($transaction->[$i]->{'taxkey'}) {
774           $taxkey = $transaction->[$i]->{'taxkey'};
775         }
776         if ($transaction->[$i]->{'charttax'}) {
777           $charttax = $transaction->[$i]->{'charttax'};
778         }
779         if ($transaction->[$i]->{'amount'} > 0) {
780           $haben = $i;
781         } else {
782           $soll = $i;
783         }
784       }
785
786       # Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern
787       foreach my $umlaut (keys(%umlaute)) {
788         $transaction->[$haben]->{'invnumber'} =~ s/${umlaut}/${umlaute{$umlaut}}/g;
789         $transaction->[$haben]->{'name'}      =~ s/${umlaut}/${umlaute{$umlaut}}/g;
790       }
791
792       $transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
793       $transaction->[$haben]->{'name'}      =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
794
795       $transaction->[$haben]->{'invnumber'} =  substr($transaction->[$haben]->{'invnumber'}, 0, 12);
796       $transaction->[$haben]->{'name'}      =  substr($transaction->[$haben]->{'name'}, 0, 30);
797       $transaction->[$haben]->{'invnumber'} =~ s/\ *$//;
798       $transaction->[$haben]->{'name'}      =~ s/\ *$//;
799
800       if ($trans_lines >= 2) {
801
802         $gegenkonto = "a" . trim_leading_zeroes($transaction->[$haben]->{'accno'});
803         $konto      = "e" . trim_leading_zeroes($transaction->[$soll]->{'accno'});
804         if ($transaction->[$haben]->{'invnumber'} ne "") {
805           $belegfeld1 = "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
806         }
807         $datum = "d";
808         $datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
809         $waehrung = "\xB3" . "EUR" . "\x1C";
810         if ($transaction->[$haben]->{'name'} ne "") {
811           $buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
812         }
813         if ($transaction->[$haben]->{'duedate'} ne "") {
814           $belegfeld2 = "\xBE" . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
815         }
816       }
817
818       $umsatz       = $kne_file->format_amount(abs($umsatz), 0);
819       $umsatzsumme += $umsatz;
820       $kne_file->add_block("+" . $umsatz);
821
822       # Dies ist die einzige Stelle die datevautomatik auswertet. Was soll gesagt werden?
823       # Im Prinzip hat jeder acc_trans Eintrag einen Steuerschlüssel, außer, bei gewissen Fällen
824       # wie: Kreditorenbuchung mit negativen Vorzeichen, SEPA-Export oder Rechnungen die per
825       # Skript angelegt werden.
826       # Also falls ein Steuerschlüssel da ist und NICHT datevautomatik diesen Block hinzufügen.
827       # Oder aber datevautomatik ist WAHR, aber der Steuerschlüssel in der acc_trans weicht
828       # von dem in der Chart ab: Also wahrscheinlich Programmfehler (NULL übergeben, statt
829       # DATEV-Steuerschlüssel) oder der Steuerschlüssel des Kontos weicht WIRKLICH von dem Eintrag in der
830       # acc_trans ab. Gibt es für diesen Fall eine plausiblen Grund?
831       #
832       if (   ( $datevautomatik || $taxkey)
833           && (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) {
834 #         $kne_file->add_block("\x6C" . (!$datevautomatik ? $taxkey : "4"));
835         $kne_file->add_block("\x6C${taxkey}");
836       }
837
838       $kne_file->add_block($gegenkonto);
839       $kne_file->add_block($belegfeld1);
840       $kne_file->add_block($belegfeld2);
841       $kne_file->add_block($datum);
842       $kne_file->add_block($konto);
843       $kne_file->add_block($buchungstext);
844       $kne_file->add_block($waehrung . "\x79");
845     }
846
847     my $mandantenendsumme = "x" . $kne_file->format_amount($umsatzsumme / 100.0, 14) . "\x79\x7a";
848
849     $kne_file->add_block($mandantenendsumme);
850     $kne_file->flush();
851
852     open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
853     print(ED $kne_file->get_data());
854     close(ED);
855
856     $ed_versionset[$fileno] = $self->make_ed_versionset($header, $filename, $kne_file->get_block_count());
857     $fileno++;
858   }
859
860   #Make EV Verwaltungsdatei
861   my $ev_header = $self->make_ev_header($form, $fileno);
862   my $ev_filename = $self->export_path . $evfile;
863   push(@filenames, $evfile);
864   open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
865   print(EV $ev_header);
866
867   foreach my $file (@ed_versionset) {
868     print(EV $ed_versionset[$file]);
869   }
870   close(EV);
871   ###
872
873   $self->add_filenames(@filenames);
874
875   $main::lxdebug->leave_sub();
876
877   return { 'download_token' => $self->download_token, 'filenames' => \@filenames };
878 }
879
880 sub kne_stammdatenexport {
881   $main::lxdebug->enter_sub();
882
883   my ($self) = @_;
884   my $form = $::form;
885
886   $self->get_datev_stamm->{abrechnungsnr} = "99";
887
888   my @filenames;
889
890   my $filename    = "ED00000";
891   my $evfile      = "EV01";
892   my @ed_versionset;
893   my $fileno          = 1;
894   my $i               = 0;
895   my $blockcount      = 1;
896   my $remaining_bytes = 256;
897   my $total_bytes     = 256;
898   my $buchungssatz    = "";
899   $filename++;
900   my $ed_filename = $self->export_path . $filename;
901   push(@filenames, $filename);
902   open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
903   my $header = $self->make_kne_data_header($form);
904   $remaining_bytes -= length($header);
905
906   my $fuellzeichen;
907
908   my (@where, @values) = ((), ());
909   if ($self->accnofrom) {
910     push @where, 'c.accno >= ?';
911     push @values, $self->accnofrom;
912   }
913   if ($self->accnoto) {
914     push @where, 'c.accno <= ?';
915     push @values, $self->accnoto;
916   }
917
918   my $where_str = @where ? ' WHERE ' . join(' AND ', map { "($_)" } @where) : '';
919
920   my $query     = qq|SELECT c.accno, c.description
921                      FROM chart c
922                      $where_str
923                      ORDER BY c.accno|;
924
925   my $sth = $self->dbh->prepare($query);
926   $sth->execute(@values) || $form->dberror($query);
927
928   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
929     if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
930       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
931       $buchungssatz .= "\x00" x $fuellzeichen;
932       $blockcount++;
933       $total_bytes = ($blockcount) * 256;
934     }
935     $buchungssatz .= "t" . $ref->{'accno'};
936     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
937     $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
938     $ref->{'description'} = substr($ref->{'description'}, 0, 40);
939     $ref->{'description'} =~ s/\ *$//;
940
941     if (
942         ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
943         ) <= 6
944       ) {
945       $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
946       $buchungssatz .= "\x00" x $fuellzeichen;
947       $blockcount++;
948       $total_bytes = ($blockcount) * 256;
949     }
950     $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
951     $remaining_bytes = $total_bytes - length($buchungssatz . $header);
952   }
953
954   $sth->finish;
955   print(ED $header);
956   print(ED $buchungssatz);
957   $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
958   my $dateiende = "\x00" x $fuellzeichen;
959   print(ED "z");
960   print(ED $dateiende);
961   close(ED);
962
963   #Make EV Verwaltungsdatei
964   $ed_versionset[0] =
965     $self->make_ed_versionset($header, $filename, $blockcount);
966
967   my $ev_header = $self->make_ev_header($form, $fileno);
968   my $ev_filename = $self->export_path . $evfile;
969   push(@filenames, $evfile);
970   open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
971   print(EV $ev_header);
972
973   foreach my $file (@ed_versionset) {
974     print(EV $ed_versionset[$file]);
975   }
976   close(EV);
977
978   $self->add_filenames(@filenames);
979
980   $main::lxdebug->leave_sub();
981
982   return { 'download_token' => $self->download_token, 'filenames' => \@filenames };
983 }
984
985 sub DESTROY {
986   clean_temporary_directories();
987 }
988
989 1;
990
991 __END__
992
993 =encoding utf-8
994
995 =head1 NAME
996
997 SL::DATEV - Lx-Office DATEV Export module
998
999 =head1 SYNOPSIS
1000
1001   use SL::DATEV qw(:CONSTANTS);
1002
1003   my $datev = SL::DATEV->new(
1004     exporttype => DATEV_ET_BUCHUNGEN,
1005     format     => DATEV_FORMAT_KNE,
1006     from       => $startdate,
1007     to         => $enddate,
1008   );
1009
1010   my $datev = SL::DATEV->new(
1011     exporttype => DATEV_ET_STAMM,
1012     format     => DATEV_FORMAT_KNE,
1013     accnofrom  => $start_account_number,
1014     accnoto    => $end_account_number,
1015   );
1016
1017   # get or set datev stamm
1018   my $hashref = $datev->get_datev_stamm;
1019   $datev->save_datev_stamm($hashref);
1020
1021   # manually clean up temporary directories
1022   $datev->clean_temporary_directories;
1023
1024   # export
1025   $datev->export;
1026
1027   if ($datev->errors) {
1028     die join "\n", $datev->error;
1029   }
1030
1031   # get relevant data for saving the export:
1032   my $dl_token = $datev->download_token;
1033   my $path     = $datev->export_path;
1034   my @files    = $datev->filenames;
1035
1036   # retrieving an export at a later time
1037   my $datev = SL::DATEV->new(
1038     download_token => $dl_token_from_user,
1039   );
1040
1041   my $path     = $datev->export_path;
1042   my @files    = glob("$path/*");
1043
1044 =head1 DESCRIPTION
1045
1046 This module implements the DATEV export standard. For usage see above.
1047
1048 =head1 FUNCTIONS
1049
1050 =over 4
1051
1052 =item new PARAMS
1053
1054 Generic constructor. See section attributes for information about hat to pass.
1055
1056 =item get_datev_stamm
1057
1058 Loads DATEV Stammdaten and returns as hashref.
1059
1060 =item save_datev_stamm HASHREF
1061
1062 Saves DATEV Stammdaten from provided hashref.
1063
1064 =item exporttype
1065
1066 See L<CONSTANTS> for possible values
1067
1068 =item has_exporttype
1069
1070 Returns true if an exporttype has been set. Without exporttype most report functions won't work.
1071
1072 =item format
1073
1074 Specifies the designated format of the export. Currently only KNE export is implemented.
1075
1076 See L<CONSTANTS> for possible values
1077
1078 =item has_format
1079
1080 Returns true if a format has been set. Without format most report functions won't work.
1081
1082 =item download_token
1083
1084 Returns a download token for this DATEV object.
1085
1086 Note: If either a download_token or export_path were set at the creation these are infered, otherwise randomly generated.
1087
1088 =item export_path
1089
1090 Returns an export_path for this DATEV object.
1091
1092 Note: If either a download_token or export_path were set at the creation these are infered, otherwise randomly generated.
1093
1094 =item filenames
1095
1096 Returns a list of filenames generated by this DATEV object. This only works if th files were generated during it's lifetime, not if the object was created from a download_token.
1097
1098 =item net_gross_differences
1099
1100 If there were any net gross differences during calculation they will be collected here.
1101
1102 =item sum_net_gross_differences
1103
1104 Sum of all differences.
1105
1106 =item clean_temporary_directories
1107
1108 Forces a garbage collection on previous exports which will delete all exports that are older than 8 hours. It will be automatically called on destruction of the object, but is advised to be called manually before delivering results of an export to the user.
1109
1110 =item errors
1111
1112 Returns a list of errors that occured. If no errors occured, the export was a success.
1113
1114 =item export
1115
1116 Exports data. You have to have set L<exporttype> and L<format> or an error will
1117 occur. OBE exports are currently not implemented.
1118
1119 =back
1120
1121 =head1 ATTRIBUTES
1122
1123 This is a list of attributes set in either the C<new> or a method of the same name.
1124
1125 =over 4
1126
1127 =item dbh
1128
1129 Set a database handle to use in the process. This allows for an export to be
1130 done on a transaction in progress without committing first.
1131
1132 =item exporttype
1133
1134 See L<CONSTANTS> for possible values. This MUST be set before export is called.
1135
1136 =item format
1137
1138 See L<CONSTANTS> for possible values. This MUST be set before export is called.
1139
1140 =item download_token
1141
1142 Can be set on creation to retrieve a prior export for download.
1143
1144 =item from
1145
1146 =item to
1147
1148 Set boundary dates for the export. Currently thse MUST be set for the export to work.
1149
1150 =item accnofrom
1151
1152 =item accnoto
1153
1154 Set boundary account numbers for the export. Only useful for a stammdaten export.
1155
1156 =back
1157
1158 =head1 CONSTANTS
1159
1160 =head2 Supplied to L<exporttype>
1161
1162 =over 4
1163
1164 =item DATEV_ET_BUCHUNGEN
1165
1166 =item DATEV_ET_STAMM
1167
1168 =back
1169
1170 =head2 Supplied to L<format>.
1171
1172 =over 4
1173
1174 =item DATEV_FORMAT_KNE
1175
1176 =item DATEV_FORMAT_OBE
1177
1178 =back
1179
1180 =head1 ERROR HANDLING
1181
1182 This module will die in the following cases:
1183
1184 =over 4
1185
1186 =item *
1187
1188 No or unrecognized exporttype or format was provided for an export
1189
1190 =item *
1191
1192 OBE rxport was called, which is not yet implemented.
1193
1194 =item *
1195
1196 general I/O errors
1197
1198 =back
1199
1200 Errors that occur during th actual export will be collected in L<errors>. The following types can occur at the moment:
1201
1202 =over 4
1203
1204 =item *
1205
1206 C<Unbalanced Ledger!>. Exactly that, your ledger is unbalanced. Should never occur.
1207
1208 =item *
1209
1210 C<Datev-Export fehlgeschlagen! Bei Transaktion %d (%f).>  This error occurs if a
1211 transaction could not be reliably sorted out, or had rounding errors over the acceptable threshold.
1212
1213 =back
1214
1215 =head1 BUGS AND CAVEATS
1216
1217 =over 4
1218
1219 =item *
1220
1221 Handling of Vollvorlauf is currently not fully implemented. You must provide both from and to to get a working export.
1222
1223 =item *
1224
1225 OBE export is currently not implemented.
1226
1227 =back
1228
1229 =head1 TODO
1230
1231 - handling of export_path and download token is a bit dodgy, clean that up.
1232
1233 =head1 SEE ALSO
1234
1235 L<SL::DATEV::KNEFile>
1236
1237 =head1 AUTHORS
1238
1239 Philip Reetz E<lt>p.reetz@linet-services.deE<gt>,
1240
1241 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
1242
1243 Jan Büren E<lt>jan@lx-office-hosting.deE<gt>,
1244
1245 Geoffrey Richardson E<lt>information@lx-office-hosting.deE<gt>,
1246
1247 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
1248
1249 Stephan Köhler
1250
1251 =cut