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