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