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);
34 use SL::Locale::String qw(t8);
35 use SL::DB::Department;
43 require "bin/mozilla/common.pl";
45 sub continue { call_sub($main::form->{"nextsub"}); }
48 $::lxdebug->enter_sub;
49 $::auth->assert('datev_export');
51 my $stamm = SL::DATEV->new->get_datev_stamm;
53 setup_datev_export_action_bar();
54 $::form->{title} = t8('DATEV - Export Assistent');
56 print $::form->parse_html_template('datev/export', $stamm);
58 $::lxdebug->leave_sub;
62 $::lxdebug->enter_sub;
63 $::auth->assert('datev_export');
65 export_bewegungsdaten();
67 $::lxdebug->leave_sub;
70 sub export_bewegungsdaten {
71 $::lxdebug->enter_sub;
72 $::auth->assert('datev_export');
74 setup_datev_export2_action_bar();
77 $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
78 $::form->{show_pk_option} = SL::DATEV->new->check_vcnumbers_are_valid_pk_numbers;
79 $::form->{show_documents_option} = SL::DATEV->new->check_document_export;
81 # check if we have mismatching number length domains
82 SL::DATEV->new->check_valid_length_of_accounts;
84 print $::form->parse_html_template('datev/export_bewegungsdaten');
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->{exportformat} eq 'kne' ? DATEV_FORMAT_KNE : DATEV_FORMAT_CSV,
98 @data{qw(from to)} = _get_dates(
99 $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
100 $::form->{transdatefrom}, $::form->{transdateto},
102 $data{use_pk} = $::form->{use_pk};
103 $data{locked} = $::form->{locked};
104 $data{imported} = $::form->{imported};
105 $data{documents} = $::form->{documents};
107 if ($data{documents} && !SL::DATEV->new->check_all_bookings_have_documents(from => $data{from}, to => $data{to})) {
108 $::form->error(t8("Cannot export with documents because some transactions don't have a PDF document attached."));
110 my $datev = SL::DATEV->new(%data);
112 $datev->clean_temporary_directories;
113 $datev->save_datev_stamm($::form);
117 if (!$datev->errors) {
118 setup_datev_export3_action_bar(download_token => $datev->download_token);
121 print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
123 $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
126 $::lxdebug->leave_sub;
130 $main::lxdebug->enter_sub();
132 my $form = $main::form;
133 my $locale = $main::locale;
135 $::auth->assert('datev_export');
137 my $tmp_name = Common->tmpname();
138 my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
142 my $datev = SL::DATEV->new(download_token => $form->{download_token});
144 my $path = $datev->export_path;
146 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
149 chdir($path) || die("chdir $path");
151 my @filenames = glob "*";
155 $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
158 my $zip = Archive::Zip->new();
159 map { $zip->addFile($_); } @filenames;
160 $zip->writeToFileNamed($tmp_name);
164 open(IN, $tmp_name) || die("open $tmp_name");
165 $::locale->with_raw_io(\*STDOUT, sub {
166 print("Content-Type: application/zip\n");
167 print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
176 $main::lxdebug->leave_sub();
180 $::lxdebug->enter_sub;
182 my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
183 my ($fromdate, $todate);
185 if ($mode eq "monat") {
186 $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
187 # december export is usually in january/february
188 $fromdate = $fromdate->subtract(years => 1) if ($month == 12);
190 $todate = $fromdate->clone->add(months => 1)->add(days => -1);
191 } elsif ($mode eq "quartal") {
192 die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
193 $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
194 $todate = $fromdate->clone->add(months => 3)->add(days => -1);
195 } elsif ($mode eq "zeit") {
196 $fromdate = DateTime->from_lxoffice($transdatefrom);
197 $todate = DateTime->from_lxoffice($transdateto);
198 die 'need from and to time' unless $fromdate && $todate;
200 die 'undefined interval mode';
203 $::lxdebug->leave_sub;
205 return ($fromdate, $todate);
208 sub setup_datev_export_action_bar {
211 for my $bar ($::request->layout->get('actionbar')) {
215 submit => [ '#form', { action => 'export2' } ],
216 accesskey => 'enter',
222 sub setup_datev_export2_action_bar {
225 for my $bar ($::request->layout->get('actionbar')) {
229 submit => [ '#form', { action => 'export3' } ],
230 accesskey => 'enter',
234 call => [ 'kivi.history_back' ],
240 sub setup_datev_export3_action_bar {
243 for my $bar ($::request->layout->get('actionbar')) {
247 link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
251 call => [ 'kivi.history_back' ],