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