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., 51 Franklin Street, Fifth Floor, Boston,
23 #======================================================================
27 #======================================================================
29 use POSIX qw(strftime getcwd);
30 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
33 use SL::DATEV qw(:CONSTANTS);
41 require "bin/mozilla/common.pl";
43 sub continue { call_sub($main::form->{"nextsub"}); }
46 $::lxdebug->enter_sub;
47 $::auth->assert('datev_export');
49 my $stamm = SL::DATEV->new->get_datev_stamm;
52 print $::form->parse_html_template('datev/export', $stamm);
54 $::lxdebug->leave_sub;
58 $::lxdebug->enter_sub;
59 $::auth->assert('datev_export');
61 if ($::form->{exporttype} == 0) {
62 export_bewegungsdaten();
66 $::lxdebug->leave_sub;
69 sub export_bewegungsdaten {
70 $::lxdebug->enter_sub;
71 $::auth->assert('datev_export');
74 print $::form->parse_html_template('datev/export_bewegungsdaten');
76 $::lxdebug->leave_sub;
79 sub export_stammdaten {
80 $::lxdebug->enter_sub;
81 $::auth->assert('datev_export');
84 print $::form->parse_html_template('datev/export_stammdaten');
86 $::lxdebug->leave_sub;
90 $::lxdebug->enter_sub;
91 $::auth->assert('datev_export');
94 exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
95 format => $::form->{kne} ? DATEV_FORMAT_KNE : DATEV_FORMAT_OBE,
98 if ($::form->{exporttype} == DATEV_ET_STAMM) {
99 $data{accnofrom} = $::form->{accnofrom},
100 $data{accnoto} = $::form->{accnoto},
101 } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
102 @data{qw(from to)} = _get_dates(
103 $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
104 $::form->{transdatefrom}, $::form->{transdateto},
107 die 'invalid exporttype';
110 my $datev = SL::DATEV->new(%data);
112 $datev->clean_temporary_directories;
113 $datev->save_datev_stamm($::form);
117 if (!$datev->errors) {
119 print $::form->parse_html_template('datev/export3', { datev => $datev });
121 $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
124 $::lxdebug->leave_sub;
128 $main::lxdebug->enter_sub();
130 my $form = $main::form;
131 my $locale = $main::locale;
133 $::auth->assert('datev_export');
135 my $tmp_name = Common->tmpname();
136 my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
140 my $datev = SL::DATEV->new(download_token => $form->{download_token});
142 my $path = $datev->export_path;
144 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
147 chdir($path) || die("chdir $path");
149 my @filenames = glob "*";
153 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
156 my $zip = Archive::Zip->new();
157 map { $zip->addFile($_); } @filenames;
158 $zip->writeToFileNamed($tmp_name);
162 open(IN, $tmp_name) || die("open $tmp_name");
163 $::locale->with_raw_io(\*STDOUT, sub {
164 print("Content-Type: application/zip\n");
165 print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
174 $main::lxdebug->leave_sub();
178 $::lxdebug->enter_sub;
180 my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
181 my ($fromdate, $todate);
183 if ($mode eq "monat") {
184 $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
185 $todate = $fromdate->clone->add(months => 1)->add(days => -1);
186 } elsif ($mode eq "quartal") {
187 die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
188 $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
189 $todate = $fromdate->clone->add(months => 3)->add(days => -1);
190 } elsif ($mode eq "zeit") {
191 $fromdate = DateTime->from_lxoffice($transdatefrom);
192 $todate = DateTime->from_lxoffice($transdateto);
193 die 'need from and to time' unless $fromdate && $todate;
195 die 'undefined interval mode';
198 $::lxdebug->leave_sub;
200 return ($fromdate, $todate);