dbbd28232d65a5f13cd297dcfc8017f81a8cd8ef
[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   if ($::form->{exporttype} == 0) {
66     export_bewegungsdaten();
67   } else {
68     export_stammdaten();
69   }
70   $::lxdebug->leave_sub;
71 }
72
73 sub export_bewegungsdaten {
74   $::lxdebug->enter_sub;
75   $::auth->assert('datev_export');
76
77   setup_datev_export2_action_bar();
78
79   $::form->header;
80   $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
81   $::form->{show_pk_option}  = SL::DATEV->new->check_vcnumbers_are_valid_pk_numbers;
82   print $::form->parse_html_template('datev/export_bewegungsdaten');
83
84   $::lxdebug->leave_sub;
85 }
86
87 sub export_stammdaten {
88   $::lxdebug->enter_sub;
89   $::auth->assert('datev_export');
90
91   setup_datev_export2_action_bar();
92
93   $::form->header;
94   print $::form->parse_html_template('datev/export_stammdaten');
95
96   $::lxdebug->leave_sub;
97 }
98
99 sub export3 {
100   $::lxdebug->enter_sub;
101   $::auth->assert('datev_export');
102
103   my %data = (
104     exporttype => $::form->{exporttype} ? DATEV_ET_STAMM : DATEV_ET_BUCHUNGEN,
105     format     => $::form->{kne}        ? DATEV_FORMAT_KNE : $::form->{csv} ? DATEV_FORMAT_CSV : die "unknown format",
106   );
107
108   if ($::form->{exporttype} == DATEV_ET_STAMM) {
109     $data{accnofrom}  = $::form->{accnofrom},
110     $data{accnoto}    = $::form->{accnoto},
111   } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
112     @data{qw(from to)} = _get_dates(
113       $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
114       $::form->{transdatefrom}, $::form->{transdateto},
115     );
116     $data{use_pk} = $::form->{use_pk};
117   } else {
118     die 'invalid exporttype';
119   }
120
121   my $datev = SL::DATEV->new(%data);
122
123   $datev->clean_temporary_directories;
124   $datev->save_datev_stamm($::form);
125
126   $datev->export;
127
128   if (!$datev->errors) {
129     setup_datev_export3_action_bar(download_token => $datev->download_token);
130
131     $::form->header;
132     print $::form->parse_html_template('datev/export3', { WARNINGS => $datev->warnings });
133   } else {
134     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
135   }
136
137   $::lxdebug->leave_sub;
138 }
139
140 sub download {
141   $main::lxdebug->enter_sub();
142
143   my $form     = $main::form;
144   my $locale   = $main::locale;
145
146   $::auth->assert('datev_export');
147
148   my $tmp_name = Common->tmpname();
149   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
150
151   my $cwd = getcwd();
152
153   my $datev = SL::DATEV->new(download_token => $form->{download_token});
154
155   my $path = $datev->export_path;
156   if (!$path) {
157     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
158   }
159
160   chdir($path) || die("chdir $path");
161
162   my @filenames = glob "*";
163
164   if (!@filenames) {
165     chdir($cwd);
166     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
167   }
168
169   my $zip = Archive::Zip->new();
170   map { $zip->addFile($_); } @filenames;
171   $zip->writeToFileNamed($tmp_name);
172
173   chdir($cwd);
174
175   open(IN, $tmp_name) || die("open $tmp_name");
176   $::locale->with_raw_io(\*STDOUT, sub {
177     print("Content-Type: application/zip\n");
178     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
179     while (<IN>) {
180       print($_);
181     }
182   });
183   close(IN);
184
185   unlink($tmp_name);
186
187   $main::lxdebug->leave_sub();
188 }
189
190 sub _get_dates {
191   $::lxdebug->enter_sub;
192
193   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
194   my ($fromdate, $todate);
195
196   if ($mode eq "monat") {
197     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
198     $todate   = $fromdate->clone->add(months => 1)->add(days => -1);
199   } elsif ($mode eq "quartal") {
200     die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
201     $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
202     $todate   = $fromdate->clone->add(months => 3)->add(days => -1);
203   } elsif ($mode eq "zeit") {
204     $fromdate = DateTime->from_lxoffice($transdatefrom);
205     $todate   = DateTime->from_lxoffice($transdateto);
206     die 'need from and to time' unless $fromdate && $todate;
207   } else {
208     die 'undefined interval mode';
209   }
210
211   $::lxdebug->leave_sub;
212
213   return ($fromdate, $todate);
214 }
215
216 sub setup_datev_export_action_bar {
217   my %params = @_;
218
219   for my $bar ($::request->layout->get('actionbar')) {
220     $bar->add(
221       action => [
222         t8('Continue'),
223         submit    => [ '#form', { action => 'export2' } ],
224         accesskey => 'enter',
225       ],
226     );
227   }
228 }
229
230 sub setup_datev_export2_action_bar {
231   my %params = @_;
232
233   for my $bar ($::request->layout->get('actionbar')) {
234     $bar->add(
235       action => [
236         t8('Export'),
237         submit    => [ '#form', { action => 'export3' } ],
238         accesskey => 'enter',
239       ],
240       action => [
241         t8('Back'),
242         call => [ 'kivi.history_back' ],
243       ],
244     );
245   }
246 }
247
248 sub setup_datev_export3_action_bar {
249   my %params = @_;
250
251   for my $bar ($::request->layout->get('actionbar')) {
252     $bar->add(
253       link => [
254         t8('Download'),
255         link => [ 'datev.pl?action=download&download_token=' . $::form->escape($params{download_token}) ],
256       ],
257       action => [
258         t8('Back'),
259         call => [ 'kivi.history_back' ],
260       ],
261     );
262   }
263 }