- my $form = $::form;
-
- my @filenames;
-
- my $filename = "ED00001";
- my $evfile = "EV01";
- my @ed_versionset;
- my $fileno = 1;
- my $ed_filename = $self->export_path . $filename;
-
- my $fromto = $self->fromto;
-
- $self->generate_datev_data(from_to => $self->fromto); # fetches data from db, transforms data and fills $self->{DATEV}
- return if $self->errors;
-
- my @datev_lines = @{ $self->generate_datev_lines };
-
-
- my $umsatzsumme = sum map { $_->{umsatz} } @datev_lines;
-
- # prepare kne file, everything gets stored in ED00001
- my $header = $self->make_kne_data_header($form);
- my $kne_file = SL::DATEV::KNEFile->new();
- $kne_file->add_block($header);
-
- my $iconv = $::locale->{iconv_utf8};
- my %umlaute = ($iconv->convert('ä') => 'ae',
- $iconv->convert('ö') => 'oe',
- $iconv->convert('ü') => 'ue',
- $iconv->convert('Ä') => 'Ae',
- $iconv->convert('Ö') => 'Oe',
- $iconv->convert('Ü') => 'Ue',
- $iconv->convert('ß') => 'sz');
-
- # add the data from @datev_lines to the kne_file, formatting as needed
- foreach my $kne ( @datev_lines ) {
- $kne_file->add_block("+" . $kne_file->format_amount(abs($kne->{umsatz}), 0));
-
- # only add buchungsschluessel if it was previously defined
- $kne_file->add_block("\x6C" . $kne->{buchungsschluessel}) if defined $kne->{buchungsschluessel};
-
- # ($kne->{gegenkonto}) = $kne->{gegenkonto} =~ /^(\d+)/;
- $kne_file->add_block("a" . trim_leading_zeroes($kne->{gegenkonto}));
-
- if ( $kne->{belegfeld1} ) {
- my $invnumber = $kne->{belegfeld1};
- foreach my $umlaut (keys(%umlaute)) {
- $invnumber =~ s/${umlaut}/${umlaute{$umlaut}}/g;
- }
- $invnumber =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
- $invnumber = substr($invnumber, 0, 12);
- $invnumber =~ s/\ *$//;
- $kne_file->add_block("\xBD" . $invnumber . "\x1C");
- }
-
- $kne_file->add_block("\xBE" . &datetofour($kne->{belegfeld2},1) . "\x1C");
-
- $kne_file->add_block("d" . &datetofour($kne->{datum},0));
-
- # ($kne->{konto}) = $kne->{konto} =~ /^(\d+)/;
- $kne_file->add_block("e" . trim_leading_zeroes($kne->{konto}));
-
- my $name = $kne->{buchungstext};
- foreach my $umlaut (keys(%umlaute)) {
- $name =~ s/${umlaut}/${umlaute{$umlaut}}/g;
- }
- $name =~ s/[^0-9A-Za-z\$\%\&\*\+\-\ \/]//g;
- $name = substr($name, 0, 30);
- $name =~ s/\ *$//;
- $kne_file->add_block("\x1E" . $name . "\x1C");
-
- $kne_file->add_block("\xBA" . $kne->{'ustid'} . "\x1C") if $kne->{'ustid'};
-
- $kne_file->add_block("\xB3" . $kne->{'waehrung'} . "\x1C" . "\x79");
- };
-
- $umsatzsumme = $kne_file->format_amount(abs($umsatzsumme), 0);
- my $mandantenendsumme = "x" . $kne_file->format_amount($umsatzsumme / 100.0, 14) . "\x79\x7a";
-
- $kne_file->add_block($mandantenendsumme);
- $kne_file->flush();
-
- open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
- print(ED $kne_file->get_data());
- close(ED);
-
- $ed_versionset[$fileno] = $self->make_ed_versionset($header, $filename, $kne_file->get_block_count());
-
- #Make EV Verwaltungsdatei
- my $ev_header = $self->make_ev_header($form, $fileno);
- my $ev_filename = $self->export_path . $evfile;
- push(@filenames, $evfile);
- open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
- print(EV $ev_header);
-
- foreach my $file (@ed_versionset) {
- print(EV $file);
- }
- close(EV);
- ###
-
- $self->add_filenames(@filenames);
-
- $main::lxdebug->leave_sub();
-
- return { 'download_token' => $self->download_token, 'filenames' => \@filenames };
-}
-
-sub kne_stammdatenexport {
- $main::lxdebug->enter_sub();
-
- my ($self) = @_;
- my $form = $::form;
-
- $self->get_datev_stamm->{abrechnungsnr} = "99";
-
- my @filenames;
-
- my $filename = "ED00000";
- my $evfile = "EV01";
- my @ed_versionset;
- my $fileno = 1;
- my $i = 0;
- my $blockcount = 1;
- my $remaining_bytes = 256;
- my $total_bytes = 256;
- my $buchungssatz = "";
- $filename++;
- my $ed_filename = $self->export_path . $filename;
- push(@filenames, $filename);
- open(ED, ">", $ed_filename) or die "can't open outputfile: $!\n";
- my $header = $self->make_kne_data_header($form);
- $remaining_bytes -= length($header);
-
- my $fuellzeichen;
-
- my (@where, @values) = ((), ());
- if ($self->accnofrom) {
- push @where, 'c.accno >= ?';
- push @values, $self->accnofrom;
- }
- if ($self->accnoto) {
- push @where, 'c.accno <= ?';
- push @values, $self->accnoto;
- }
-
- my $where_str = @where ? ' WHERE ' . join(' AND ', map { "($_)" } @where) : '';
-
- my $query = qq|SELECT c.accno, c.description
- FROM chart c
- $where_str
- ORDER BY c.accno|;
-
- my $sth = $self->dbh->prepare($query);
- $sth->execute(@values) || $form->dberror($query);
-
- while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- if (($remaining_bytes - length("t" . $ref->{'accno'})) <= 6) {
- $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
- $buchungssatz .= "\x00" x $fuellzeichen;
- $blockcount++;
- $total_bytes = ($blockcount) * 256;
- }
- $buchungssatz .= "t" . $ref->{'accno'};
- $remaining_bytes = $total_bytes - length($buchungssatz . $header);
- $ref->{'description'} =~ s/[^0-9A-Za-z\$\%\&\*\+\-\/]//g;
- $ref->{'description'} = substr($ref->{'description'}, 0, 40);
- $ref->{'description'} =~ s/\ *$//;
-
- if (
- ($remaining_bytes - length("\x1E" . $ref->{'description'} . "\x1C\x79")
- ) <= 6
- ) {
- $fuellzeichen = ($blockcount * 256 - length($buchungssatz . $header));
- $buchungssatz .= "\x00" x $fuellzeichen;
- $blockcount++;
- $total_bytes = ($blockcount) * 256;
- }
- $buchungssatz .= "\x1E" . $ref->{'description'} . "\x1C\x79";
- $remaining_bytes = $total_bytes - length($buchungssatz . $header);
- }
-
- $sth->finish;
- print(ED $header);
- print(ED $buchungssatz);
- $fuellzeichen = 256 - (length($header . $buchungssatz . "z") % 256);
- my $dateiende = "\x00" x $fuellzeichen;
- print(ED "z");
- print(ED $dateiende);
- close(ED);
-
- #Make EV Verwaltungsdatei
- $ed_versionset[0] =
- $self->make_ed_versionset($header, $filename, $blockcount);
-
- my $ev_header = $self->make_ev_header($form, $fileno);
- my $ev_filename = $self->export_path . $evfile;
- push(@filenames, $evfile);
- open(EV, ">", $ev_filename) or die "can't open outputfile: EV01\n";
- print(EV $ev_header);
-
- foreach my $file (@ed_versionset) {
- print(EV $ed_versionset[$file]);
- }
- close(EV);
-
- $self->add_filenames(@filenames);
-
- $main::lxdebug->leave_sub();
-
- return { 'download_token' => $self->download_token, 'filenames' => \@filenames };