1 #=====================================================================
6 # Email: p.reetz@linet-services.de
7 # Web: http://www.lx-office.org
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
26 #======================================================================
28 use POSIX qw(strftime getcwd);
29 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
32 use SL::DATEV qw(:CONSTANTS);
40 require "bin/mozilla/common.pl";
42 sub continue { call_sub($main::form->{"nextsub"}); }
45 $::lxdebug->enter_sub;
46 $::auth->assert('datev_export');
48 my $stamm = SL::DATEV->new->get_datev_stamm;
51 print $::form->parse_html_template('datev/export', $stamm);
53 $::lxdebug->leave_sub;
57 $::lxdebug->enter_sub;
58 $::auth->assert('datev_export');
60 if ($::form->{exporttype} == 0) {
61 export_bewegungsdaten();
65 $::lxdebug->leave_sub;
68 sub export_bewegungsdaten {
69 $::lxdebug->enter_sub;
70 $::auth->assert('datev_export');
73 print $::form->parse_html_template('datev/export_bewegungsdaten');
75 $::lxdebug->leave_sub;
78 sub export_stammdaten {
79 $::lxdebug->enter_sub;
80 $::auth->assert('datev_export');
83 print $::form->parse_html_template('datev/export_stammdaten');
85 $::lxdebug->leave_sub;
89 $::lxdebug->enter_sub;
90 $::auth->assert('datev_export');
93 exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
94 format => $::form->{kne} ? DATEV_FORMAT_KNE : DATEV_FORMAT_OBE,
97 if ($::form->{exporttype} == DATEV_ET_STAMM) {
98 $data{accnofrom} = $::form->{accnofrom},
99 $data{accnoto} = $::form->{accnoto},
100 } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
101 @data{qw(from to)} = _get_dates(
102 $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
103 $::form->{transdatefrom}, $::form->{transdateto},
106 die 'invalid exporttype';
109 my $datev = SL::DATEV->new(%data);
111 $datev->clean_temporary_directories;
112 $datev->save_datev_stamm($::form);
116 if (!$datev->errors) {
118 print $::form->parse_html_template('datev/export3', { datev => $datev });
120 $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
123 $::lxdebug->leave_sub;
127 $main::lxdebug->enter_sub();
129 my $form = $main::form;
130 my $locale = $main::locale;
132 $::auth->assert('datev_export');
134 my $tmp_name = Common->tmpname();
135 my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
139 my $datev = SL::DATEV->new(download_token => $form->{download_token});
141 my $path = $datev->export_path;
143 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
146 chdir($path) || die("chdir $path");
148 my @filenames = glob "*";
152 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
155 my $zip = Archive::Zip->new();
156 map { $zip->addFile($_); } @filenames;
157 $zip->writeToFileNamed($tmp_name);
161 open(IN, $tmp_name) || die("open $tmp_name");
162 $::locale->with_raw_io(\*STDOUT, sub {
163 print("Content-Type: application/zip\n");
164 print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
173 $main::lxdebug->leave_sub();
177 $::lxdebug->enter_sub;
179 my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
180 my ($fromdate, $todate);
182 if ($mode eq "monat") {
183 $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
184 $todate = $fromdate->clone->add(months => 1)->add(days => -1);
185 } elsif ($mode eq "quartal") {
186 die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
187 $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
188 $todate = $fromdate->clone->add(months => 3)->add(days => -1);
189 } elsif ($mode eq "zeit") {
190 $fromdate = DateTime->from_lxoffice($transdatefrom);
191 $todate = DateTime->from_lxoffice($transdateto);
192 die 'need from and to time' unless $fromdate && $todate;
194 die 'undefined interval mode';
197 $::lxdebug->leave_sub;
199 return ($fromdate, $todate);