From ea711360381d49e8e5341b1096da6de3d09cfade Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 5 Sep 2008 10:33:22 +0000 Subject: [PATCH] Kosmetik; Variablen lokal deklarieren. --- SL/DATEV.pm | 152 +++++++++++++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 73 deletions(-) diff --git a/SL/DATEV.pm b/SL/DATEV.pm index fb11d580d..1484b1dcc 100644 --- a/SL/DATEV.pm +++ b/SL/DATEV.pm @@ -216,16 +216,21 @@ sub get_dates { return $fromto; } -sub get_transactions { +sub _get_transactions { $main::lxdebug->enter_sub(); - my $dbh = $form->get_standard_dbh($myconfig); + my $fromto = shift; - $fromto =~ s/transdate/ac\.transdate/g; + my $myconfig = \%main::myconfig; + my $form = $main::form; - my %taxes = selectall_as_map($form, $dbh, qq|SELECT id, rate FROM tax|, 'id', 'rate'); + my $dbh = $form->get_standard_dbh($myconfig); - my $query = + $fromto =~ s/transdate/ac\.transdate/g; + + my %taxes = selectall_as_map($form, $dbh, qq|SELECT id, rate FROM tax|, 'id', 'rate'); + + my $query = qq|SELECT ac.oid, ac.transdate, ac.trans_id,ar.id, ac.amount, ac.taxkey, ar.invnumber, ar.duedate, ar.amount as umsatz, ct.name, @@ -268,91 +273,92 @@ sub get_transactions { ORDER BY trans_id, oid|; - my $sth = prepare_execute_query($form, $dbh, $query); + my $sth = prepare_execute_query($form, $dbh, $query); - $i = 0; - $g = 0; + my $g = 0; my $counter = 0; my @splits; while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - $count = 0; - $firstrun = 1; $counter++; if (($counter % 500) == 0) { print("$counter "); } - $count += $ref->{amount}; - push @{$i}, $ref; + my $i = [ $ref ]; + + my $count = $ref->{amount}; + my $firstrun = 1; + while (abs($count) > 0.01 || $firstrun) { - $ref2 = $sth->fetchrow_hashref(NAME_lc); + my $ref2 = $sth->fetchrow_hashref(NAME_lc); last unless ($ref2); - $count += $ref2->{amount}; + push @{$i}, $ref2; - $firstrun = 0; + + $count += $ref2->{amount}; + $firstrun = 0; } + my %taxid_taxkeys = (); - $absumsatz = 0; - if (scalar(@{$i}) > 2) { - for my $j (0 .. (scalar(@{$i}) - 1)) { - if (abs($i->[$j]->{'amount'}) > abs($absumsatz)) { - $absumsatz = $i->[$j]->{'amount'}; - $notsplitindex = $j; - } - if (($i->[$j]->{'taxtaxkey'}) && ($i->[$j]->{'taxid'})) { - $taxid_taxkeys{$i->[$j]->{'taxtaxkey'}} = $i->[$j]->{'taxid'}; - } - } - $ml = ($i->[0]->{'umsatz'} > 0) ? 1 : -1; - for my $j (0 .. (scalar(@{$i}) - 1)) { - if ( ($j != $notsplitindex) - && ($i->[$j]->{'chart_id'} eq "") - && ( $i->[$j]->{'taxkey'} eq "" - || $i->[$j]->{'taxkey'} eq "0" - || $i->[$j]->{'taxkey'} eq "1" - || $i->[$j]->{'taxkey'} eq "10" - || $i->[$j]->{'taxkey'} eq "11")) { - my %blubb = {}; - map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; } keys(%{ $i->[$notsplitindex] })); - - $absumsatz += $i->[$j]->{'amount'}; - $blubb{'amount'} = $i->[$j]->{'amount'} * (-1); - $blubb{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml; - $i->[$j]->{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml; - - push @{ $splits[$g] }, \%blubb; #$i->[$notsplitindex]; - push @{ $splits[$g] }, $i->[$j]; - push @{ $form->{DATEV} }, \@{ $splits[$g] }; - - $g++; - - } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) { - $absumsatz += ($i->[$j]->{'amount'} * (1 + $taxes{ $taxid_taxkeys{$i->[$j]->{'taxkey'}} })); - - my %blubb = {}; - map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; } keys(%{ $i->[$notsplitindex] })); - - $test = 1 + $taxes{ $taxid_taxkeys{$i->[$j]->{'taxkey'}} }; - $blubb{'amount'} = $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2); - $blubb{'umsatz'} = abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml; - $i->[$j]->{'umsatz'} = abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml; - - push @{ $splits[$g] }, \%blubb; - push @{ $splits[$g] }, $i->[$j]; - push @{ $form->{DATEV} }, \@{ $splits[$g] }; - $g++; + my $absumsatz = 0; + if (scalar(@{$i}) <= 2) { + push @{ $form->{DATEV} }, \@{$i}; + next; + } - } else { - next; - } + for my $j (0 .. (scalar(@{$i}) - 1)) { + if (abs($i->[$j]->{'amount'}) > abs($absumsatz)) { + $absumsatz = $i->[$j]->{'amount'}; + $notsplitindex = $j; } - if (abs($absumsatz) > 0.01) { - $form->error("Datev-Export fehlgeschlagen! Bei Transaktion $i->[0]->{trans_id} $absumsatz\n"); + if (($i->[$j]->{'taxtaxkey'}) && ($i->[$j]->{'taxid'})) { + $taxid_taxkeys{$i->[$j]->{'taxtaxkey'}} = $i->[$j]->{'taxid'}; } - } else { - push @{ $form->{DATEV} }, \@{$i}; } - $i++; + $ml = ($i->[0]->{'umsatz'} > 0) ? 1 : -1; + for my $j (0 .. (scalar(@{$i}) - 1)) { + if ( ($j != $notsplitindex) + && ($i->[$j]->{'chart_id'} eq "") + && ( $i->[$j]->{'taxkey'} eq "" + || $i->[$j]->{'taxkey'} eq "0" + || $i->[$j]->{'taxkey'} eq "1" + || $i->[$j]->{'taxkey'} eq "10" + || $i->[$j]->{'taxkey'} eq "11")) { + my %blubb = {}; + map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; } keys(%{ $i->[$notsplitindex] })); + + $absumsatz += $i->[$j]->{'amount'}; + $blubb{'amount'} = $i->[$j]->{'amount'} * (-1); + $blubb{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml; + $i->[$j]->{'umsatz'} = abs($i->[$j]->{'amount'}) * $ml; + + push @{ $splits[$g] }, \%blubb; #$i->[$notsplitindex]; + push @{ $splits[$g] }, $i->[$j]; + push @{ $form->{DATEV} }, \@{ $splits[$g] }; + + $g++; + + } elsif (($j != $notsplitindex) && ($i->[$j]->{'chart_id'} eq "")) { + $absumsatz += ($i->[$j]->{'amount'} * (1 + $taxes{ $taxid_taxkeys{$i->[$j]->{'taxkey'}} })); + + my %blubb = {}; + map({ $blubb{$_} = $i->[$notsplitindex]->{$_}; } keys(%{ $i->[$notsplitindex] })); + + $test = 1 + $taxes{ $taxid_taxkeys{$i->[$j]->{'taxkey'}} }; + $blubb{'amount'} = $form->round_amount(($i->[$j]->{'amount'} * $test * -1), 2); + $blubb{'umsatz'} = abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml; + $i->[$j]->{'umsatz'} = abs($form->round_amount(($i->[$j]->{'amount'} * $test), 2)) * $ml; + + push @{ $splits[$g] }, \%blubb; + push @{ $splits[$g] }, $i->[$j]; + push @{ $form->{DATEV} }, \@{ $splits[$g] }; + $g++; + } + } + + if (abs($absumsatz) > 0.01) { + $form->error("Datev-Export fehlgeschlagen! Bei Transaktion $i->[0]->{trans_id} $absumsatz\n"); + } } $sth->finish; $dbh->disconnect; @@ -620,7 +626,7 @@ sub kne_buchungsexport { &get_dates($form->{zeitraum}, $form->{monat}, $form->{quartal}, $form->{transdatefrom}, $form->{transdateto}); - &get_transactions($myconfig, $form, $fromto); + _get_transactions($fromto); my $counter = 0; print qq|
2. Durchlauf:|; while (scalar(@{ $form->{DATEV} })) { -- 2.20.1