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