Eine Funktion zum "sicheren" Aufrufen von Unterfunktionen eingebaut, damit &{ $form...
[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 1;
35
36 # end of main
37
38 require "bin/mozilla/common.pl";
39
40 sub continue { call_sub($form->{"nextsub"}); }
41
42 sub export {
43   $lxdebug->enter_sub();
44
45   $form->{title} = $locale->text("DATEX - Export Assistent");
46
47   DATEV->get_datev_stamm(\%myconfig, \%$form);
48   $form->header;
49
50   print qq|
51 <body>
52
53 <form method=post action=$form->{script}>
54
55
56 <table width=100%>
57   <tr>
58     <th class=listtop>$form->{title}</th>
59   </tr>
60   <tr>
61     <th align=left>| . $locale->text("DATEV Angaben") . qq|</th>
62   </tr>
63   <tr height="5"></tr>
64   <tr valign=top>
65     <td>
66       <table>
67         <tr>
68           <td align=left nowrap>| . $locale->text("Beraternummer") . qq|</td>
69           <td><input name=beraternr size=10 maxlength=7 value="$form->{beraternr}"></td>
70
71           <td align=left nowrap>| . $locale->text("DFV-Kennzeichen") . qq|</td>
72           <td><input name=dfvkz size=5 maxlength=2 value="$form->{dfvkz}"></td>
73         </tr>
74         <tr>
75           <td align=left nowrap>| . $locale->text("Beratername") . qq|</td>
76           <td><input name=beratername size=10 maxlength=9 value="$form->{beratername}"></td>
77
78           <td align=left nowrap>| . $locale->text("Password") . qq|</td>
79           <td><input name=passwort size=5 maxlength=4 value="$form->{passwort}"></td>
80         </tr>
81         <tr>
82           <td align=left nowrap>| . $locale->text("Mandantennummer") . qq|</td>
83           <td><input name=mandantennr size=10 maxlength=5 value="$form->{mandantennr}"></td>
84
85           <td align=left nowrap>| . $locale->text("Datenträgernummer") . qq|</td>
86           <td><input name=datentraegernr size=5 maxlength=3 value="$form->{datentraegernr}"></td>
87         </tr>
88         <tr>
89           | . # OBE-Export noch nicht implementiert! <td><input checked name=kne type=checkbox class=checkbox value=1> | . $locale->text("Kontonummernerweiterung (KNE)") . qq|</td>
90     qq|<td><input type="hidden" name="kne" value="1"></td>
91           <td></td>
92
93           <td align=left nowrap>| . $locale->text("Abrechnungsnummer") . qq|</td>
94           <td><input name=abrechnungsnr size=5 maxlength=3 value="$form->{abrechnungsnr}"></td>
95         </tr>
96
97         <tr>
98           <td><input name=exporttype type=radio class=radio value=0 checked> |
99     . $locale->text("Export Buchungsdaten") . qq|</td>
100           <td></td>
101
102           <td><input name=exporttype type=radio class=radio value=1> |
103     . $locale->text("Export Stammdaten") . qq|</td>
104           <td></td>
105         </td>
106       </table>
107     </td>
108   </tr>
109   <tr>
110     <td><hr size=3 noshade></td>
111   </tr>
112 </table>
113
114 <input type=hidden name=nextsub value=export2>
115
116 <input type=hidden name=path value=$form->{path}>
117 <input type=hidden name=login value=$form->{login}>
118 <input type=hidden name=password value=$form->{password}>
119
120 <br>
121 <input type=submit class=submit name=action value="|
122     . $locale->text('Continue') . qq|">
123 </form>
124
125 </body>
126 </html>
127 |;
128   $lxdebug->leave_sub();
129 }
130
131 sub export2 {
132   $lxdebug->enter_sub();
133
134   if ($form->{exporttype} == 0) {
135     &export_bewegungsdaten();
136   } else {
137     &export_stammdaten();
138   }
139   $lxdebug->leave_sub();
140 }
141
142 sub export_bewegungsdaten {
143   $lxdebug->enter_sub();
144
145   $form->{title} = $locale->text("DATEX - Export Assistent");
146
147   $form->{allemonate} =
148       qq|<option value=1>|
149     . $locale->text('January')
150     . qq|</option>
151                          <option value=2>|
152     . $locale->text('February') . qq|</option>
153                          <option value=3>|
154     . $locale->text('March') . qq|</option>
155                          <option value=4>|
156     . $locale->text('April') . qq|</option>
157                          <option value=5>|
158     . $locale->text('May') . qq|</option>
159                          <option value=6>|
160     . $locale->text('June') . qq|</option>
161                          <option value=7>|
162     . $locale->text('July') . qq|</option>
163                          <option value=8>|
164     . $locale->text('August') . qq|</option>
165                          <option value=9>|
166     . $locale->text('September') . qq|</option>
167                          <option value=10>|
168     . $locale->text('October') . qq|</option>
169                          <option value=11>|
170     . $locale->text('November') . qq|</option>
171                          <option value=12>|
172     . $locale->text('December') . qq|</option>|;
173
174   $form->{allequartale} =
175       qq|<option selected value=1>|
176     . $locale->text('I')
177     . qq|</option>
178                          <option value=2>| . $locale->text('II') . qq|</option>
179                          <option value=3>|
180     . $locale->text('III') . qq|</option>
181                          <option value=4>|
182     . $locale->text('IV') . qq|</option>|;
183   $form->{"jsscript"} = 1;
184   $form->header;
185
186   print qq|
187 <body>
188
189 <form method=post action=$form->{script}>
190
191
192 <table width=100%>
193   <tr>
194     <th class=listtop>$form->{title}</th>
195   </tr>
196   <tr>
197     <th align=left>| . $locale->text("Zeitraum") . qq|</th>
198   </tr>
199   <tr height="5"></tr>
200   <tr valign=top>
201     <td>
202       <table>
203         <tr>
204           <td align=left><input checked name=zeitraum class=radio type=radio value=monat>&nbsp;</td><td align=left>|
205     . $locale->text('Monat') . qq|</td>
206           <td align=left></td>
207           <td align=left></td>
208           <td align=left><select name=monat>$form->{allemonate}</select></td>
209         </tr>
210         <tr>
211           <td align=left><input name=zeitraum class=radio type=radio value=quartal>&nbsp;</td><td align=left>|
212     . $locale->text('Quartal') . qq|</td>
213           <td align=left></td>
214           <td align=left></td>
215           <td align=left><select name=quartal>$form->{allequartale}</select></td>
216         </tr>
217         <tr>
218           <td align=left><input name=zeitraum class=radio type=radio value=zeit>&nbsp;</td><td align=left>|
219     . $locale->text('Datum von') . qq|</td>
220           <td align=left><input id=transdatefrom name=transdatefrom size=10>
221             <input type="button" name="transdatefrom" id="trigger_transdatefrom" value="?"></td>
222           <td align=left>| . $locale->text('bis') . qq|</td>
223           <td align=left><input id=transdateto name=transdateto size=10>
224             <input type="button" name="transdateto" id="trigger_transdateto" value="?"></td>
225         </tr>
226       </table>
227     </td>
228   </tr>
229   <tr>
230     <td><hr size=3 noshade></td>
231   </tr>
232 </table>
233
234 | . $form->write_trigger(\%myconfig, 2,
235                          "transdatefrom", "BL", "trigger_transdatefrom",
236                          "transdateto", "BL", "trigger_transdateto") . qq|
237
238 <input type=hidden name=beraternr value="$form->{beraternr}">
239 <input type=hidden name=dfvkz value="$form->{dfvkz}">
240 <input type=hidden name=beratername value="$form->{beratername}">
241 <input type=hidden name=passwort value="$form->{passwort}">
242 <input type=hidden name=mandantennr value="$form->{mandantennr}">
243 <input type=hidden name=datentraegernr value="$form->{datentraegernr}">
244 <input type=hidden name=kne value="$form->{kne}">
245 <input type=hidden name=abrechnungsnr value="$form->{abrechnungsnr}">
246
247 <input type=hidden name=exporttype value="$form->{exporttype}">
248
249 <input type=hidden name=nextsub value=export3>
250
251 <input type=hidden name=path value=$form->{path}>
252 <input type=hidden name=login value=$form->{login}>
253 <input type=hidden name=password value=$form->{password}>
254
255 <br>
256 <input type=submit class=submit name=action value="|
257     . $locale->text('Continue') . qq|">
258 </form>
259
260 </body>
261 </html>
262 |;
263
264   $lxdebug->leave_sub();
265 }
266
267 sub export_stammdaten {
268   $lxdebug->enter_sub();
269
270   $form->{title} = $locale->text("DATEX - Export Assistent");
271
272   $form->header;
273
274   print qq|
275 <body>
276
277 <form method=post action=$form->{script}>
278 <table width=100%>
279   <tr>
280     <th class=listtop>$form->{title}</th>
281   </tr>
282   <tr>
283     <th align=left>| . $locale->text("Konten") . qq|</th>
284   </tr>
285   <tr height="5"></tr>
286   <tr valign=top>
287     <td>
288       <table>
289         <tr>
290           <td align=left>| . $locale->text('Von Konto: ') . qq|</td>
291           <td align=left><input name=accnofrom size=8 maxlength=8></td>
292         </tr>
293         <tr>
294           <td align=left>| . $locale->text('Bis Konto: ') . qq|</td>
295           <td align=left><input name=accnoto size=8 maxlength=8></td>
296         </tr>
297       </table>
298     </td>
299   </tr>
300   <tr>
301     <td><hr size=3 noshade></td>
302   </tr>
303 </table>
304 <input type=hidden name=beraternr value="$form->{beraternr}">
305 <input type=hidden name=dfvkz value="$form->{dfvkz}">
306 <input type=hidden name=beratername value="$form->{beratername}">
307 <input type=hidden name=passwort value="$form->{passwort}">
308 <input type=hidden name=mandantennr value="$form->{mandantennr}">
309 <input type=hidden name=datentraegernr value="$form->{datentraegernr}">
310 <input type=hidden name=kne value="$form->{kne}">
311 <input type=hidden name=abrechnungsnr value="$form->{abrechnungsnr}">
312
313 <input type=hidden name=exporttype value="$form->{exporttype}">
314
315 <input type=hidden name=nextsub value=export3>
316
317 <input type=hidden name=path value=$form->{path}>
318 <input type=hidden name=login value=$form->{login}>
319 <input type=hidden name=password value=$form->{password}>
320
321 <br>
322 <input type=submit class=submit name=action value="|
323     . $locale->text('Continue') . qq|">
324 </form>
325
326 </body>
327 </html>
328 |;
329
330   $lxdebug->leave_sub();
331 }
332
333 sub export3 {
334   $lxdebug->enter_sub();
335
336   DATEV->save_datev_stamm(\%myconfig, \%$form);
337
338   my $link = $form->{"script"} . "?";
339   map({ $link .= "${_}=" . $form->escape($form->{$_}) . "&"; } qw(path login password));
340   $link .= "action=download";
341
342   if ($form->{kne}) {
343     my @filenames = DATEV->kne_export(\%myconfig, \%$form);
344     if (@filenames) {
345       print(qq|<br><b>| . $locale->text('KNE-Export erfolgreich!') . qq|</b><br>|);
346       $link .= "&filenames=" . $form->escape(join(":", @filenames));
347       print(qq|<br><a href="$link">Download</a>|);
348     } else {
349       $form->error("KNE-Export schlug fehl.");
350     }
351   } else {
352     my @filenames = DATEV->obe_export(\%myconfig, \%$form);
353     if (@filenames) {
354       print(qq|<br><b>| . $locale->text('OBE-Export erfolgreich!') . qq|</b><br>|);
355       $link .= "&filenames=" . $form->escape(join(":", @filenames));
356       print(qq|<br><a href="$link">Download</a>|);
357     } else {
358       $form->error("OBE-Export schlug fehl.");
359     }
360   }
361
362   print("</body></html>");
363
364   $lxdebug->leave_sub();
365 }
366
367 sub download {
368   $lxdebug->enter_sub();
369
370   my $tmp_name = Common->tmpname();
371   my $zip_name = strftime("lx-office-datev-export-%Y%m%d.zip",
372                           localtime(time()));
373
374   my $cwd = getcwd();
375   chdir("users") || die("chdir users");
376
377   my @filenames = split(/:/, $form->{"filenames"});
378   map({ s|.*/||; $form->error("Eine der KNE-Exportdateien wurde nicht " .
379                               "gefunden. Wurde der Export bereits " .
380                               "durchgef&uuml;hrt?") unless (-f $_); }
381       @filenames);
382
383   my $zip = Archive::Zip->new();
384   map({ $zip->addFile($_); } @filenames);
385   $zip->writeToFileNamed($tmp_name);
386   chdir($cwd);
387
388   open(IN, $tmp_name) || die("open $tmp_name");
389   print("Content-Type: application/zip\n");
390   print("Content-Disposition: attachment; filename=\"${zip_name}\"\n\n");
391   while (<IN>) {
392     print($_);
393   }
394   close(IN);
395
396   unlink($tmp_name);
397
398   $lxdebug->leave_sub();
399 }