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