Revision 3c147670
Von Kivitendo Admin vor mehr als 7 Jahren hinzugefügt
SL/DATEV.pm | ||
---|---|---|
841 | 841 |
$filename++; |
842 | 842 |
my $ed_filename = $self->export_path . $filename; |
843 | 843 |
push(@filenames, $filename); |
844 |
my $header = $self->make_kne_data_header($form); |
|
845 |
|
|
846 |
my $kne_file = SL::DATEV::KNEFile->new(); |
|
847 |
$kne_file->add_block($header); |
|
848 | 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 = (); |
|
849 | 848 |
while (scalar(@{ $self->{DATEV} }) > 0) { |
849 |
my %kne_data = (); |
|
850 | 850 |
my $transaction = shift @{ $self->{DATEV} }; |
851 | 851 |
my $trans_lines = scalar(@{$transaction}); |
852 | 852 |
$counter++; |
... | ... | |
864 | 864 |
my $charttax = 0; |
865 | 865 |
my $ustid =""; |
866 | 866 |
my ($haben, $soll); |
867 |
my $iconv = $::locale->{iconv_utf8}; |
|
868 |
my %umlaute = ($iconv->convert('ä') => 'ae', |
|
869 |
$iconv->convert('ö') => 'oe', |
|
870 |
$iconv->convert('ü') => 'ue', |
|
871 |
$iconv->convert('Ä') => 'Ae', |
|
872 |
$iconv->convert('Ö') => 'Oe', |
|
873 |
$iconv->convert('Ü') => 'Ue', |
|
874 |
$iconv->convert('ß') => 'sz'); |
|
875 | 867 |
for (my $i = 0; $i < $trans_lines; $i++) { |
876 | 868 |
if ($trans_lines == 2) { |
877 | 869 |
if (abs($transaction->[$i]->{'amount'}) > abs($umsatz)) { |
... | ... | |
897 | 889 |
$soll = $i; |
898 | 890 |
} |
899 | 891 |
} |
900 |
# Umwandlung von Umlauten und Sonderzeichen in erlaubte Zeichen bei Textfeldern |
|
901 |
foreach my $umlaut (keys(%umlaute)) { |
|
902 |
$transaction->[$haben]->{'invnumber'} =~ s/${umlaut}/${umlaute{$umlaut}}/g; |
|
903 |
$transaction->[$haben]->{'name'} =~ s/${umlaut}/${umlaute{$umlaut}}/g; |
|
904 |
} |
|
905 |
|
|
906 |
$transaction->[$haben]->{'invnumber'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g; |
|
907 |
$transaction->[$haben]->{'name'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g; |
|
908 |
|
|
909 |
$transaction->[$haben]->{'invnumber'} = substr($transaction->[$haben]->{'invnumber'}, 0, 12); |
|
910 |
$transaction->[$haben]->{'name'} = substr($transaction->[$haben]->{'name'}, 0, 30); |
|
911 |
$transaction->[$haben]->{'invnumber'} =~ s/\ *$//; |
|
912 |
$transaction->[$haben]->{'name'} =~ s/\ *$//; |
|
913 | 892 |
|
914 | 893 |
if ($trans_lines >= 2) { |
915 | 894 |
|
916 |
$gegenkonto = "a" . trim_leading_zeroes($transaction->[$haben]->{'accno'});
|
|
917 |
$konto = "e" . trim_leading_zeroes($transaction->[$soll]->{'accno'});
|
|
895 |
$kne_data{'gegenkonto'} = $transaction->[$haben]->{'accno'};
|
|
896 |
$kne_data{'konto'} = $transaction->[$soll]->{'accno'};
|
|
918 | 897 |
if ($transaction->[$haben]->{'invnumber'} ne "") { |
919 |
$belegfeld1 = "\xBD" . $transaction->[$haben]->{'invnumber'} . "\x1C";
|
|
898 |
$kne_data{belegfeld1} = $transaction->[$haben]->{'invnumber'};
|
|
920 | 899 |
} |
921 |
$datum = "d";
|
|
922 |
$datum .= &datetofour($transaction->[$haben]->{'transdate'}, 0);
|
|
923 |
$waehrung = "\xB3" . "EUR" . "\x1C"; |
|
900 |
$kne_data{datum} = $transaction->[$haben]->{'transdate'};
|
|
901 |
$kne_data{waehrung} = 'EUR';
|
|
902 |
|
|
924 | 903 |
if ($transaction->[$haben]->{'name'} ne "") { |
925 |
$buchungstext = "\x1E" . $transaction->[$haben]->{'name'} . "\x1C";
|
|
904 |
$kne_data{buchungstext} = $transaction->[$haben]->{'name'};
|
|
926 | 905 |
} |
927 | 906 |
if (($transaction->[$haben]->{'ustid'} // '') ne "") { |
928 |
$ustid = "\xBA" . $transaction->[$haben]->{'ustid'} . "\x1C";
|
|
907 |
$kne_data{ustid} = $transaction->[$haben]->{'ustid'};
|
|
929 | 908 |
} |
930 | 909 |
if (($transaction->[$haben]->{'duedate'} // '') ne "") { |
931 |
$belegfeld2 = "\xBE" . &datetofour($transaction->[$haben]->{'duedate'}, 1) . "\x1C";
|
|
910 |
$kne_data{belegfeld2} = $transaction->[$haben]->{'duedate'};
|
|
932 | 911 |
} |
933 | 912 |
} |
934 | 913 |
|
935 |
$umsatz = $kne_file->format_amount(abs($umsatz), 0); |
|
936 |
$umsatzsumme += $umsatz; |
|
937 |
$kne_file->add_block("+" . $umsatz); |
|
914 |
$kne_data{umsatz} = abs($umsatz); # sales invoices without tax have a different sign??? |
|
915 |
$umsatzsumme += $kne_data{umsatz}; #umsatz; # add the abs amount |
|
938 | 916 |
|
939 | 917 |
# Dies ist die einzige Stelle die datevautomatik auswertet. Was soll gesagt werden? |
940 | 918 |
# Im Prinzip hat jeder acc_trans Eintrag einen Steuerschlüssel, außer, bei gewissen Fällen |
... | ... | |
946 | 924 |
# DATEV-Steuerschlüssel) oder der Steuerschlüssel des Kontos weicht WIRKLICH von dem Eintrag in der |
947 | 925 |
# acc_trans ab. Gibt es für diesen Fall eine plausiblen Grund? |
948 | 926 |
# |
927 |
|
|
928 |
# only set buchungsschluessel if the following conditions are met: |
|
949 | 929 |
if ( ( $datevautomatik || $taxkey) |
950 | 930 |
&& (!$datevautomatik || ($datevautomatik && ($charttax ne $taxkey)))) { |
951 |
# $kne_file->add_block("\x6C" . (!$datevautomatik ? $taxkey : "4"));
|
|
952 |
$kne_file->add_block("\x6C${taxkey}");
|
|
931 |
# $kne_data{buchungsschluessel} = !$datevautomatik ? $taxkey : "4";
|
|
932 |
$kne_data{buchungsschluessel} = $taxkey;
|
|
953 | 933 |
} |
954 | 934 |
|
955 |
$kne_file->add_block($gegenkonto); |
|
956 |
$kne_file->add_block($belegfeld1); |
|
957 |
$kne_file->add_block($belegfeld2); |
|
958 |
$kne_file->add_block($datum); |
|
959 |
$kne_file->add_block($konto); |
|
960 |
$kne_file->add_block($buchungstext); |
|
961 |
$kne_file->add_block($ustid); |
|
962 |
$kne_file->add_block($waehrung . "\x79"); |
|
935 |
push(@kne_lines, \%kne_data); |
|
963 | 936 |
} |
964 | 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); |
|
965 | 997 |
my $mandantenendsumme = "x" . $kne_file->format_amount($umsatzsumme / 100.0, 14) . "\x79\x7a"; |
966 | 998 |
|
967 | 999 |
$kne_file->add_block($mandantenendsumme); |
Auch abrufbar als: Unified diff
DATEV KNE Export Refactoring
Anstatt die Werte aus der DB direkt zu transformieren und per add_block
direkt die KNE-Datei zu bauen werden jetzt alle Daten in einem Array aus
Hashrefs gesammelt und unformatiert zwischengespeichert.
Aus diesem Zwischenstand wird dann erst in einem Rutsch die KNE-Datei
generiert. An dieser Stelle könnte man aber auch direkt die Daten als
CSV abgreifen.