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