kivitendo/bin/mozilla/datev.pl @ 4cb374f9
d319704a | Moritz Bunkus | #=====================================================================
|
||
008c2e15 | Moritz Bunkus | # kivitendo ERP
|
||
d319704a | Moritz Bunkus | # Copyright (c) 2004
|
||
#
|
||||
# Author: Philip Reetz
|
||||
# Email: p.reetz@linet-services.de
|
||||
# Web: http://www.lx-office.org
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
f7b15d43 | Christian Wittmer | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
# MA 02110-1335, USA.
|
||||
d319704a | Moritz Bunkus | #======================================================================
|
||
#
|
||||
# Datev export module
|
||||
#
|
||||
#======================================================================
|
||||
39f3d12c | Moritz Bunkus | use POSIX qw(strftime getcwd);
|
||
use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
|
||||
use SL::Common;
|
||||
631b4c04 | Sven Schöling | use SL::DATEV qw(:CONSTANTS);
|
||
15cc7149 | Moritz Bunkus | use SL::Locale::String qw(t8);
|
||
c0f873c1 | Geoffrey Richardson | use SL::DB::Department;
|
||
d319704a | Moritz Bunkus | |||
27309ff1 | Sven Schöling | use strict;
|
||
d319704a | Moritz Bunkus | 1;
|
||
# end of main
|
||||
40782548 | Moritz Bunkus | require "bin/mozilla/common.pl";
|
||
27309ff1 | Sven Schöling | sub continue { call_sub($main::form->{"nextsub"}); }
|
||
d319704a | Moritz Bunkus | |||
sub export {
|
||||
6fc9c2bb | Sven Schöling | $::lxdebug->enter_sub;
|
||
$::auth->assert('datev_export');
|
||||
b65a230d | Sven Schöling | |||
631b4c04 | Sven Schöling | my $stamm = SL::DATEV->new->get_datev_stamm;
|
||
15cc7149 | Moritz Bunkus | setup_datev_export_action_bar();
|
||
6fc9c2bb | Sven Schöling | $::form->header;
|
||
631b4c04 | Sven Schöling | print $::form->parse_html_template('datev/export', $stamm);
|
||
0576299f | Moritz Bunkus | |||
6fc9c2bb | Sven Schöling | $::lxdebug->leave_sub;
|
||
d319704a | Moritz Bunkus | }
|
||
sub export2 {
|
||||
6fc9c2bb | Sven Schöling | $::lxdebug->enter_sub;
|
||
$::auth->assert('datev_export');
|
||||
8c7e4493 | Moritz Bunkus | |||
c255a38e | Jan Büren | export_bewegungsdaten();
|
||
6fc9c2bb | Sven Schöling | $::lxdebug->leave_sub;
|
||
d319704a | Moritz Bunkus | }
|
||
sub export_bewegungsdaten {
|
||||
6fc9c2bb | Sven Schöling | $::lxdebug->enter_sub;
|
||
$::auth->assert('datev_export');
|
||||
27309ff1 | Sven Schöling | |||
15cc7149 | Moritz Bunkus | setup_datev_export2_action_bar();
|
||
6fc9c2bb | Sven Schöling | $::form->header;
|
||
2b37ad3a | Jan Büren | $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
|
||
69b298a0 | Geoffrey Richardson | $::form->{show_pk_option} = SL::DATEV->new->check_vcnumbers_are_valid_pk_numbers;
|
||
254ef1b2 | Jan Büren | |||
# check if we have mismatching number length domains
|
||||
SL::DATEV->new->check_valid_length_of_accounts;
|
||||
6fc9c2bb | Sven Schöling | print $::form->parse_html_template('datev/export_bewegungsdaten');
|
||
d319704a | Moritz Bunkus | |||
6fc9c2bb | Sven Schöling | $::lxdebug->leave_sub;
|
||
d319704a | Moritz Bunkus | }
|
||
sub export3 {
|
||||
631b4c04 | Sven Schöling | $::lxdebug->enter_sub;
|
||
$::auth->assert('datev_export');
|
||||
d319704a | Moritz Bunkus | |||
631b4c04 | Sven Schöling | my %data = (
|
||
exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
|
||||
5b5fc3ca | Jan Büren | format => $::form->{exportformat} eq 'kne' ? DATEV_FORMAT_KNE : DATEV_FORMAT_CSV,
|
||
631b4c04 | Sven Schöling | );
|
||
c255a38e | Jan Büren | @data{qw(from to)} = _get_dates(
|
||
$::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
|
||||
$::form->{transdatefrom}, $::form->{transdateto},
|
||||
);
|
||||
$data{use_pk} = $::form->{use_pk};
|
||||
$data{locked} = $::form->{locked};
|
||||
$data{imported} = $::form->{imported};
|
||||
39f3d12c | Moritz Bunkus | |||
631b4c04 | Sven Schöling | my $datev = SL::DATEV->new(%data);
|
||
f8138d17 | Moritz Bunkus | |||
631b4c04 | Sven Schöling | $datev->clean_temporary_directories;
|
||
$datev->save_datev_stamm($::form);
|
||||
f8138d17 | Moritz Bunkus | |||
631b4c04 | Sven Schöling | $datev->export;
|
||
a3501388 | Moritz Bunkus | |||
631b4c04 | Sven Schöling | if (!$datev->errors) {
|
||
15cc7149 | Moritz Bunkus | setup_datev_export3_action_bar(download_token => $datev->download_token);
|
||
631b4c04 | Sven Schöling | $::form->header;
|
||
9e99dce5 | Jan Büren | print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
|
||
d319704a | Moritz Bunkus | } else {
|
||
631b4c04 | Sven Schöling | $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
|
||
d319704a | Moritz Bunkus | }
|
||
39f3d12c | Moritz Bunkus | |||
631b4c04 | Sven Schöling | $::lxdebug->leave_sub;
|
||
39f3d12c | Moritz Bunkus | }
|
||
sub download {
|
||||
27309ff1 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
my $locale = $main::locale;
|
||||
39f3d12c | Moritz Bunkus | |||
631b4c04 | Sven Schöling | $::auth->assert('datev_export');
|
||
8c7e4493 | Moritz Bunkus | |||
39f3d12c | Moritz Bunkus | my $tmp_name = Common->tmpname();
|
||
65867e61 | Sven Schöling | my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
|
||
39f3d12c | Moritz Bunkus | |||
my $cwd = getcwd();
|
||||
631b4c04 | Sven Schöling | my $datev = SL::DATEV->new(download_token => $form->{download_token});
|
||
my $path = $datev->export_path;
|
||||
f8138d17 | Moritz Bunkus | if (!$path) {
|
||
$form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
|
||||
}
|
||||
chdir($path) || die("chdir $path");
|
||||
my @filenames = glob "*";
|
||||
if (!@filenames) {
|
||||
chdir($cwd);
|
||||
$form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
|
||||
}
|
||||
39f3d12c | Moritz Bunkus | |||
my $zip = Archive::Zip->new();
|
||||
f8138d17 | Moritz Bunkus | map { $zip->addFile($_); } @filenames;
|
||
39f3d12c | Moritz Bunkus | $zip->writeToFileNamed($tmp_name);
|
||
f8138d17 | Moritz Bunkus | |||
39f3d12c | Moritz Bunkus | chdir($cwd);
|
||
open(IN, $tmp_name) || die("open $tmp_name");
|
||||
e652a0c2 | Moritz Bunkus | $::locale->with_raw_io(\*STDOUT, sub {
|
||
print("Content-Type: application/zip\n");
|
||||
print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
|
||||
while (<IN>) {
|
||||
print($_);
|
||||
}
|
||||
});
|
||||
39f3d12c | Moritz Bunkus | close(IN);
|
||
unlink($tmp_name);
|
||||
27309ff1 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
d319704a | Moritz Bunkus | }
|
||
631b4c04 | Sven Schöling | |||
sub _get_dates {
|
||||
$::lxdebug->enter_sub;
|
||||
my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
|
||||
my ($fromdate, $todate);
|
||||
if ($mode eq "monat") {
|
||||
$fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
|
||||
2636820b | Jan Büren | # december export is usually in january/february
|
||
$fromdate = $fromdate->subtract(years => 1) if ($month == 12);
|
||||
631b4c04 | Sven Schöling | $todate = $fromdate->clone->add(months => 1)->add(days => -1);
|
||
} elsif ($mode eq "quartal") {
|
||||
die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
|
||||
$fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
|
||||
$todate = $fromdate->clone->add(months => 3)->add(days => -1);
|
||||
} elsif ($mode eq "zeit") {
|
||||
$fromdate = DateTime->from_lxoffice($transdatefrom);
|
||||
$todate = DateTime->from_lxoffice($transdateto);
|
||||
die 'need from and to time' unless $fromdate && $todate;
|
||||
} else {
|
||||
die 'undefined interval mode';
|
||||
}
|
||||
$::lxdebug->leave_sub;
|
||||
return ($fromdate, $todate);
|
||||
}
|
||||
15cc7149 | Moritz Bunkus | |||
sub setup_datev_export_action_bar {
|
||||
my %params = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Continue'),
|
||||
submit => [ '#form', { action => 'export2' } ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
sub setup_datev_export2_action_bar {
|
||||
my %params = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Export'),
|
||||
submit => [ '#form', { action => 'export3' } ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
action => [
|
||||
t8('Back'),
|
||||
call => [ 'kivi.history_back' ],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
sub setup_datev_export3_action_bar {
|
||||
my %params = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
link => [
|
||||
t8('Download'),
|
||||
link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
|
||||
],
|
||||
action => [
|
||||
t8('Back'),
|
||||
call => [ 'kivi.history_back' ],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|