Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / bin / mozilla / datev.pl
1 #=====================================================================
2 # kivitendo ERP
3 # Copyright (c) 2004
4 #
5 #  Author: Philip Reetz
6 #   Email: p.reetz@linet-services.de
7 #     Web: http://www.lx-office.org
8 #
9 #
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.
14 #
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,
22 # MA 02110-1335, USA.
23 #======================================================================
24 #
25 # Datev export module
26 #
27 #======================================================================
28
29 use POSIX qw(strftime getcwd);
30 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
31
32 use SL::Common;
33 use SL::DATEV qw(:CONSTANTS);
34 use SL::Locale::String qw(t8);
35 use SL::DB::Department;
36
37 use strict;
38
39 1;
40
41 # end of main
42
43 require "bin/mozilla/common.pl";
44
45 sub continue { call_sub($main::form->{"nextsub"}); }
46
47 sub export {
48   $::lxdebug->enter_sub;
49   $::auth->assert('datev_export');
50
51   my $stamm = SL::DATEV->new->get_datev_stamm;
52
53   setup_datev_export_action_bar();
54
55   $::form->header;
56   print $::form->parse_html_template('datev/export', $stamm);
57
58   $::lxdebug->leave_sub;
59 }
60
61 sub export2 {
62   $::lxdebug->enter_sub;
63   $::auth->assert('datev_export');
64
65   export_bewegungsdaten();
66
67   $::lxdebug->leave_sub;
68 }
69
70 sub export_bewegungsdaten {
71   $::lxdebug->enter_sub;
72   $::auth->assert('datev_export');
73
74   setup_datev_export2_action_bar();
75
76   $::form->header;
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
80   # check if we have mismatching number length domains
81   SL::DATEV->new->check_valid_length_of_accounts;
82
83   print $::form->parse_html_template('datev/export_bewegungsdaten');
84
85   $::lxdebug->leave_sub;
86 }
87
88 sub export3 {
89   $::lxdebug->enter_sub;
90   $::auth->assert('datev_export');
91
92   my %data = (
93     exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
94     format     => $::form->{exportformat} eq 'kne' ? DATEV_FORMAT_KNE :  DATEV_FORMAT_CSV,
95   );
96
97   @data{qw(from to)} = _get_dates(
98     $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
99     $::form->{transdatefrom}, $::form->{transdateto},
100   );
101   $data{use_pk} = $::form->{use_pk};
102   $data{locked} = $::form->{locked};
103   $data{imported} = $::form->{imported};
104
105   my $datev = SL::DATEV->new(%data);
106
107   $datev->clean_temporary_directories;
108   $datev->save_datev_stamm($::form);
109
110   $datev->export;
111
112   if (!$datev->errors) {
113     setup_datev_export3_action_bar(download_token => $datev->download_token);
114
115     $::form->header;
116     print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
117   } else {
118     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
119   }
120
121   $::lxdebug->leave_sub;
122 }
123
124 sub download {
125   $main::lxdebug->enter_sub();
126
127   my $form     = $main::form;
128   my $locale   = $main::locale;
129
130   $::auth->assert('datev_export');
131
132   my $tmp_name = Common->tmpname();
133   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
134
135   my $cwd = getcwd();
136
137   my $datev = SL::DATEV->new(download_token => $form->{download_token});
138
139   my $path = $datev->export_path;
140   if (!$path) {
141     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
142   }
143
144   chdir($path) || die("chdir $path");
145
146   my @filenames = glob "*";
147
148   if (!@filenames) {
149     chdir($cwd);
150     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
151   }
152
153   my $zip = Archive::Zip->new();
154   map { $zip->addFile($_); } @filenames;
155   $zip->writeToFileNamed($tmp_name);
156
157   chdir($cwd);
158
159   open(IN, $tmp_name) || die("open $tmp_name");
160   $::locale->with_raw_io(\*STDOUT, sub {
161     print("Content-Type: application/zip\n");
162     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
163     while (<IN>) {
164       print($_);
165     }
166   });
167   close(IN);
168
169   unlink($tmp_name);
170
171   $main::lxdebug->leave_sub();
172 }
173
174 sub _get_dates {
175   $::lxdebug->enter_sub;
176
177   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
178   my ($fromdate, $todate);
179
180   if ($mode eq "monat") {
181     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
182     # december export is usually in january/february
183     $fromdate = $fromdate->subtract(years => 1) if ($month == 12);
184
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;
194   } else {
195     die 'undefined interval mode';
196   }
197
198   $::lxdebug->leave_sub;
199
200   return ($fromdate, $todate);
201 }
202
203 sub setup_datev_export_action_bar {
204   my %params = @_;
205
206   for my $bar ($::request->layout->get('actionbar')) {
207     $bar->add(
208       action => [
209         t8('Continue'),
210         submit    => [ '#form', { action => 'export2' } ],
211         accesskey => 'enter',
212       ],
213     );
214   }
215 }
216
217 sub setup_datev_export2_action_bar {
218   my %params = @_;
219
220   for my $bar ($::request->layout->get('actionbar')) {
221     $bar->add(
222       action => [
223         t8('Export'),
224         submit    => [ '#form', { action => 'export3' } ],
225         accesskey => 'enter',
226       ],
227       action => [
228         t8('Back'),
229         call => [ 'kivi.history_back' ],
230       ],
231     );
232   }
233 }
234
235 sub setup_datev_export3_action_bar {
236   my %params = @_;
237
238   for my $bar ($::request->layout->get('actionbar')) {
239     $bar->add(
240       link => [
241         t8('Download'),
242         link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
243       ],
244       action => [
245         t8('Back'),
246         call => [ 'kivi.history_back' ],
247       ],
248     );
249   }
250 }