kivitendo rebranding
[kivitendo-erp.git] / bin / mozilla / datev.pl
1 #=====================================================================
2 # Lx-Office 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;
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   DATEV->get_datev_stamm(\%::myconfig, $::form);
49   $::form->header;
50   print $::form->parse_html_template('datev/export');
51
52   $::lxdebug->leave_sub;
53 }
54
55 sub export2 {
56   $::lxdebug->enter_sub;
57   $::auth->assert('datev_export');
58
59   if ($::form->{exporttype} == 0) {
60     export_bewegungsdaten();
61   } else {
62     export_stammdaten();
63   }
64   $::lxdebug->leave_sub;
65 }
66
67 sub export_bewegungsdaten {
68   $::lxdebug->enter_sub;
69   $::auth->assert('datev_export');
70
71   $::form->header;
72   print $::form->parse_html_template('datev/export_bewegungsdaten');
73
74   $::lxdebug->leave_sub;
75 }
76
77 sub export_stammdaten {
78   $::lxdebug->enter_sub;
79   $::auth->assert('datev_export');
80
81   $::form->header;
82   print $::form->parse_html_template('datev/export_stammdaten');
83
84   $::lxdebug->leave_sub;
85 }
86
87 sub export3 {
88   $main::lxdebug->enter_sub();
89
90   my $form     = $main::form;
91   my %myconfig = %main::myconfig;
92   my $locale   = $main::locale;
93
94   $main::auth->assert('datev_export');
95
96   DATEV::clean_temporary_directories();
97
98   DATEV->save_datev_stamm(\%myconfig, \%$form);
99
100   my $link = "datev.pl?action=download&download_token=";
101
102   if ($form->{kne}) {
103     my $result = DATEV->kne_export(\%myconfig, \%$form);
104     if ($result && @{ $result->{filenames} }) {
105       $link .= Q($result->{download_token});
106
107       print(qq|<br><b>| . $locale->text('KNE-Export erfolgreich!') . qq|</b><br><br><a href="$link">Download</a>|);
108
109       print $form->parse_html_template('datev/net_gross_difference') if @{ $form->{net_gross_differences} };
110
111     } else {
112       $form->error("KNE-Export schlug fehl.");
113     }
114   } else {
115     # OBE-Export nicht implementiert.
116
117     # my @filenames = DATEV->obe_export(\%myconfig, \%$form);
118     # if (@filenames) {
119     #   print(qq|<br><b>| . $locale->text('OBE-Export erfolgreich!') . qq|</b><br>|);
120     #   $link .= "&filenames=" . $form->escape(join(":", @filenames));
121     #   print(qq|<br><a href="$link">Download</a>|);
122     # } else {
123     #   $form->error("OBE-Export schlug fehl.");
124     # }
125   }
126
127   print("</body></html>");
128
129   $main::lxdebug->leave_sub();
130 }
131
132 sub download {
133   $main::lxdebug->enter_sub();
134
135   my $form     = $main::form;
136   my $locale   = $main::locale;
137
138   $main::auth->assert('datev_export');
139
140   my $tmp_name = Common->tmpname();
141   my $zip_name = strftime("kivitendo-datev-export-%Y%m%d.zip", localtime(time()));
142
143   my $cwd = getcwd();
144
145   my $path = DATEV::get_path_for_download_token($form->{download_token});
146   if (!$path) {
147     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
148   }
149
150   chdir($path) || die("chdir $path");
151
152   my @filenames = glob "*";
153
154   if (!@filenames) {
155     chdir($cwd);
156     DATEV::clean_temporary_directories();
157     $form->error($locale->text("Your download does not exist anymore. Please re-run the DATEV export assistant."));
158   }
159
160   my $zip = Archive::Zip->new();
161   map { $zip->addFile($_); } @filenames;
162   $zip->writeToFileNamed($tmp_name);
163
164   chdir($cwd);
165
166   open(IN, $tmp_name) || die("open $tmp_name");
167   $::locale->with_raw_io(\*STDOUT, sub {
168     print("Content-Type: application/zip\n");
169     print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
170     while (<IN>) {
171       print($_);
172     }
173   });
174   close(IN);
175
176   unlink($tmp_name);
177
178   DATEV::clean_temporary_directories();
179
180   $main::lxdebug->leave_sub();
181 }