epic-ts
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # Datev export module
25 #
26 #======================================================================
27
28 use POSIX qw(strftime getcwd);
29 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
30
31 use SL::Common;
32 use SL::DATEV qw(:CONSTANTS);
33
34 use strict;
35
36 1;
37
38 # end of main
39
40 require "bin/mozilla/common.pl";
41
42 sub continue { call_sub($main::form->{"nextsub"}); }
43
44 sub export {
45   $::lxdebug->enter_sub;
46   $::auth->assert('datev_export');
47
48   my $stamm = SL::DATEV->new->get_datev_stamm;
49
50   $::form->header;
51   print $::form->parse_html_template('datev/export', $stamm);
52
53   $::lxdebug->leave_sub;
54 }
55
56 sub export2 {
57   $::lxdebug->enter_sub;
58   $::auth->assert('datev_export');
59
60   if ($::form->{exporttype} == 0) {
61     export_bewegungsdaten();
62   } else {
63     export_stammdaten();
64   }
65   $::lxdebug->leave_sub;
66 }
67
68 sub export_bewegungsdaten {
69   $::lxdebug->enter_sub;
70   $::auth->assert('datev_export');
71
72   $::form->header;
73   print $::form->parse_html_template('datev/export_bewegungsdaten');
74
75   $::lxdebug->leave_sub;
76 }
77
78 sub export_stammdaten {
79   $::lxdebug->enter_sub;
80   $::auth->assert('datev_export');
81
82   $::form->header;
83   print $::form->parse_html_template('datev/export_stammdaten');
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->{kne}        ? DATEV_FORMAT_KNE : DATEV_FORMAT_OBE,
95   );
96
97   if ($::form->{exporttype} == DATEV_ET_STAMM) {
98     $data{accnofrom}  = $::form->{accnofrom},
99     $data{accnoto}    = $::form->{accnoto},
100   } elsif ($::form->{exporttype} == DATEV_ET_BUCHUNGEN) {
101     @data{qw(from to)} = _get_dates(
102       $::form->{zeitraum}, $::form->{monat}, $::form->{quartal},
103       $::form->{transdatefrom}, $::form->{transdateto},
104     );
105   } else {
106     die 'invalid exporttype';
107   }
108
109   my $datev = SL::DATEV->new(%data);
110
111   $datev->clean_temporary_directories;
112   $datev->save_datev_stamm($::form);
113
114   $datev->export;
115
116   if (!$datev->errors) {
117     $::form->header;
118     print $::form->parse_html_template('datev/export3', { datev => $datev });
119   } else {
120     $::form->error("Export schlug fehl.\n" . join "\n", $datev->errors);
121   }
122
123   $::lxdebug->leave_sub;
124 }
125
126 sub download {
127   $main::lxdebug->enter_sub();
128
129   my $form     = $main::form;
130   my $locale   = $main::locale;
131
132   $::auth->assert('datev_export');
133
134   my $tmp_name = Common->tmpname();
135   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
136
137   my $cwd = getcwd();
138
139   my $datev = SL::DATEV->new(download_token => $form->{download_token});
140
141   my $path = $datev->export_path;
142   if (!$path) {
143     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
144   }
145
146   chdir($path) || die("chdir $path");
147
148   my @filenames = glob "*";
149
150   if (!@filenames) {
151     chdir($cwd);
152     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
153   }
154
155   my $zip = Archive::Zip->new();
156   map { $zip->addFile($_); } @filenames;
157   $zip->writeToFileNamed($tmp_name);
158
159   chdir($cwd);
160
161   open(IN, $tmp_name) || die("open $tmp_name");
162   $::locale->with_raw_io(\*STDOUT, sub {
163     print("Content-Type: application/zip\n");
164     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
165     while (<IN>) {
166       print($_);
167     }
168   });
169   close(IN);
170
171   unlink($tmp_name);
172
173   $main::lxdebug->leave_sub();
174 }
175
176 sub _get_dates {
177   $::lxdebug->enter_sub;
178
179   my ($mode, $month, $quarter, $transdatefrom, $transdateto) = @_;
180   my ($fromdate, $todate);
181
182   if ($mode eq "monat") {
183     $fromdate = DateTime->new(day => 1, month => $month, year => DateTime->today->year);
184     $todate   = $fromdate->clone->add(months => 1)->add(days => -1);
185   } elsif ($mode eq "quartal") {
186     die 'quarter out of of bounds' if $quarter < 1 || $quarter > 4;
187     $fromdate = DateTime->new(day => 1, month => (3 * $quarter - 2), year => DateTime->today->year);
188     $todate   = $fromdate->clone->add(months => 3)->add(days => -1);
189   } elsif ($mode eq "zeit") {
190     $fromdate = DateTime->from_lxoffice($transdatefrom);
191     $todate   = DateTime->from_lxoffice($transdateto);
192     die 'need from and to time' unless $fromdate && $todate;
193   } else {
194     die 'undefined interval mode';
195   }
196
197   $::lxdebug->leave_sub;
198
199   return ($fromdate, $todate);
200 }