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