File::Temp für Erzeugung temporärer Dateinamen nutzen
[kivitendo-erp.git] / bin / mozilla / ustva.pl
1 #=====================================================================
2 # Lx-Office ERP
3 # Copyright (c) 2004 by Udo Spallek, Aachen
4 #
5 #  Author: Udo Spallek
6 #   Email: udono@gmx.net
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 # German Tax authority Module and later ELSTER Interface
24 #======================================================================
25
26 use strict;
27 use utf8;
28
29 require "bin/mozilla/common.pl";
30
31 #use strict;
32 #no strict 'refs';
33 #use diagnostics;
34 #use warnings; # FATAL=> 'all';
35 #use vars qw($locale $form %myconfig);
36 #our ($myconfig);
37 #use CGI::Carp "fatalsToBrowser";
38
39 use List::Util qw(first);
40
41 use SL::PE;
42 use SL::RP;
43 use SL::USTVA;
44 use SL::User;
45 1;
46
47 # this is for our long dates
48 # $locale->text('January')
49 # $locale->text('February')
50 # $locale->text('March')
51 # $locale->text('April')
52 # $locale->text('May ')
53 # $locale->text('June')
54 # $locale->text('July')
55 # $locale->text('August')
56 # $locale->text('September')
57 # $locale->text('October')
58 # $locale->text('November')
59 # $locale->text('December')
60
61 # this is for our short month
62 # $locale->text('Jan')
63 # $locale->text('Feb')
64 # $locale->text('Mar')
65 # $locale->text('Apr')
66 # $locale->text('May')
67 # $locale->text('Jun')
68 # $locale->text('Jul')
69 # $locale->text('Aug')
70 # $locale->text('Sep')
71 # $locale->text('Oct')
72 # $locale->text('Nov')
73 # $locale->text('Dec')
74
75 #############################
76
77 sub report {
78   $::lxdebug->enter_sub();
79
80   my $form     = $::form;
81   my $locale   = $::locale;
82   my %myconfig = %::myconfig;
83
84   $::auth->assert('advance_turnover_tax_return');
85
86   $form->{title} = $locale->text('UStVA');
87   $form->{kz10}  = '';                       #Berichtigte Anmeldung? Ja =1 Nein=0
88
89   my $year = substr($form->datetonum($form->current_date(\%myconfig), \%myconfig ),
90              0, 4);
91
92   my $department = '';
93   my $hide = '';
94   $form->header;
95
96   # Einlesen der Finanzamtdaten
97   my $ustva = USTVA->new();
98   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
99
100   # Hier Einlesen der user-config
101   # steuernummer entfernt für prerelease
102   my @a = qw(
103     signature      name          company       address        businessnumber
104     tel            fax           email         co_chief       co_department
105     co_custom1     co_custom2    co_custom3    co_custom4     co_custom5
106     co_name1       co_name2      co_street     co_street1     co_zip
107     co_city        co_city1      co_country    co_tel         co_tel1
108     co_tel2        co_fax        co_fax1       co_email       co_email1
109     co_url         co_url1       ustid         duns           co_bankname
110     co_bankname1   co_bankname2  co_bankname3  co_blz         co_blz1
111     co_blz2        co_blz3       co_accountnr  co_accountnr1  co_accountnr2
112     co_accountnr3
113   );
114
115   map { $form->{$_} = $myconfig{$_} } @a;
116
117   my $openings = $form->{FA_Oeffnungszeiten};
118   $openings =~ s/\\\\n/<br>/g;
119
120   my $company_given = ($form->{company} ne '')
121     ? qq|<h3>$form->{company}</h3>\n|
122     : qq|<a href="am.pl?action=config">|
123       . $locale->text('No Company Name given') . qq|!</a><br>|;
124
125
126   # Anpassungen der Variablennamen auf pre 2.1.1 Namen
127   # klären, ob $form->{company_street|_address} gesetzt sind
128   if ($form->{address} ne '') {
129     my $temp = $form->{address};
130     $temp =~ s/\n/<br \/>/;
131     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp);
132     $form->{co_city} =~ s/\n//g;
133   }
134
135
136   my $address_given =
137     ($form->{co_street} && ($form->{co_zip} || $form->{co_city}))
138     ? qq|$form->{co_street}<br>|
139         . qq|$form->{co_street1}<br>|
140         . qq|$form->{co_zip} $form->{co_city}|
141     : qq|<a href="am.pl?action=config|
142         . qq|&level=Programm--Preferences">|
143         . $locale->text('No Company Address given')
144         . qq|!</a>\n|;
145
146   $form->{co_email} = $form->{email} unless $form->{co_email};
147   $form->{co_tel}   = $form->{tel}   unless $form->{co_tel};
148   $form->{co_fax}   = $form->{fax}   unless $form->{co_fax};
149   $form->{co_url}   = $form->{urlx}  unless $form->{co_url};
150
151   my $taxnumber_given = ($form->{steuernummer} ne '') ? $form->{steuernummer} : qq|<a href="ustva.pl?action=config_step1">Keine Steuernummer hinterlegt!</a><br>|;
152
153   my $ustva_vorauswahl = &ustva_vorauswahl();
154
155   my @all_years = $form->all_years(\%myconfig);
156
157   my $select_year = qq|<select name=year title="|
158     . $locale->text('Year') . qq|">|;
159   foreach my $key (@all_years) {
160     $select_year .= qq|<option |;
161     $select_year .= qq|selected| if ($key eq $form->{year});
162     $select_year .= qq| >$key</option>|;
163   }
164   $select_year   .=  qq|</select>|;
165
166   my $_checked = '';
167   $_checked = "checked" if ($form->{kz10} eq '1');
168   my $checkbox_kz_10 = qq|<input name="FA_10" id=FA_10 class=checkbox|
169     . qq| type=checkbox value="1" $_checked title = "|
170     . $locale->text('Amended Advance Turnover Tax Return (Nr. 10)')
171     . qq|">|
172     . $locale->text('Amended Advance Turnover Tax Return');
173
174   my $method_local = ($form->{method} eq 'accrual') ? $locale->text('accrual')
175                    : ($form->{method} eq 'cash')    ? $locale->text('cash')
176                    : '';
177
178   my $period_local = ( $form->{FA_voranmeld} eq 'month')   ? $locale->text('month')
179                    : ( $form->{FA_voranmeld} eq 'quarter') ? $locale->text('quarter')
180                    : '';
181
182   my $tax_office_banks_ref = [
183     { BLZ             => $form->{FA_BLZ_1},
184       Kontonummer     => $form->{FA_Kontonummer_1},
185       Bankbezeichnung => $form->{FA_Bankbezeichnung_1}
186     },
187     { BLZ             => $form->{FA_BLZ_2},
188       Kontonummer     => $form->{FA_Kontonummer_2},
189       Bankbezeichnung => $form->{FA_Bankbezeichnung_oertlich}
190     }
191   ];
192
193   # Which COA is in use?
194
195   $ustva->get_coa($form, \%myconfig);
196
197   my $template_ref = {
198     openings         => $openings,
199     company_given    => $company_given,
200     address_given    => $address_given,
201     taxnumber_given  => $taxnumber_given,
202     taxnumber        => $myconfig{taxnumber},
203     select_year      => $select_year,
204     period_local     => $period_local,
205     method_local     => $method_local,
206     ustva_vorauswahl => $ustva_vorauswahl,
207     checkbox_kz_10   => $checkbox_kz_10,
208     tax_office_banks => $tax_office_banks_ref,
209     select_options   => &show_options,
210
211   };
212
213   print($form->parse_html_template('ustva/report', $template_ref));
214
215
216
217   $::lxdebug->leave_sub();
218 }
219
220
221
222 sub help {
223   $::lxdebug->enter_sub();
224
225   $::auth->assert('advance_turnover_tax_return');
226
227   # parse help documents under doc
228   my $tmp = $::form->{templates};
229   $::form->{templates} = 'doc';
230   $::form->{help}      = 'ustva';
231   $::form->{type}      = 'help';
232   $::form->{format}    = 'html';
233   generate_ustva();
234
235   #$form->{templates} = $tmp;
236   $::lxdebug->leave_sub();
237 }
238
239 sub show {
240   $::lxdebug->enter_sub();
241
242   $::auth->assert('advance_turnover_tax_return');
243
244   #&generate_ustva();
245   $::lxdebug->leave_sub();
246   call_sub($::form->{"nextsub"});
247 }
248
249 sub ustva_vorauswahl {
250   $::lxdebug->enter_sub();
251
252   my $form     = $::form;
253   my $locale   = $::locale;
254   my %myconfig = %::myconfig;
255
256   $::auth->assert('advance_turnover_tax_return');
257
258   my $select_vorauswahl;
259
260   #Aktuelles Datum zerlegen:
261   my $date = $form->datetonum($form->current_date(\%myconfig), \%myconfig);
262
263   #$locale->date($myconfig, $form->current_date($myconfig), 0)=~ /(\d\d).(\d\d).(\d\d\d\d)/;
264   $form->{day}   = substr($date, 6, 2);
265   $form->{month} = substr($date, 4, 2);
266   $form->{year}  = substr($date, 0, 4);
267   $::lxdebug->message(LXDebug->DEBUG1, qq|
268     Actual date from Database: $date\n
269     Actual year from Database: $form->{year}\n
270     Actual day from Database: $form->{day}\n
271     Actual month from Database: $form->{month}\n|);
272
273   my $sel    = '';
274   my $yymmdd = '';
275
276   # Testdaten erzeugen:
277   #$form->{day}= '11';
278   #$form->{month}= '01';
279   #$form->{year}= 2004;
280   $select_vorauswahl = qq|
281      <input type=hidden name=day value=$form->{day}>
282      <input type=hidden name=month value=$form->{month}>
283      <input type=hidden name=yymmdd value=$yymmdd>
284      <input type=hidden name=sel value=$sel>
285   |;
286
287   if ($form->{FA_voranmeld} eq 'month') {
288
289     # Vorauswahl bei monatlichem Voranmeldungszeitraum
290
291     my %liste = ('01' => $locale->text('January'),
292                  '02' => $locale->text('February'),
293                  '03' => $locale->text('March'),
294                  '04' => $locale->text('April'),
295                  '05' => $locale->text('May'),
296                  '06' => $locale->text('June'),
297                  '07' => $locale->text('July'),
298                  '08' => $locale->text('August'),
299                  '09' => $locale->text('September'),
300                  '10' => $locale->text('October'),
301                  '11' => $locale->text('November'),
302                  '12' => $locale->text('December'),
303                  '13' => $locale->text('Yearly'),
304                 );
305
306     my $yy = $form->{year} * 10000;
307     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
308     $sel    = '';
309     my $dfv = '';
310
311     # Offset für Dauerfristverlängerung
312     $dfv = '100' if ($form->{FA_dauerfrist} eq '1');
313
314   SWITCH: {
315       $yymmdd <= ($yy + 110 + $dfv) && do {
316         $form->{year} = $form->{year} - 1;
317         $sel = '12';
318         last SWITCH;
319       };
320       $yymmdd <= ($yy + 210 + $dfv) && do {
321         $sel = '01';
322         last SWITCH;
323       };
324       $yymmdd <= ($yy + 310 + $dfv) && do {
325         $sel = '02';
326         last SWITCH;
327       };
328       $yymmdd <= ($yy + 410 + $dfv) && do {
329         $sel = '03';
330         last SWITCH;
331       };
332       $yymmdd <= ($yy + 510 + $dfv) && do {
333         $sel = '04';
334         last SWITCH;
335       };
336       $yymmdd <= ($yy + 610 + $dfv) && do {
337         $sel = '05';
338         last SWITCH;
339       };
340       $yymmdd <= ($yy + 710 + $dfv) && do {
341         $sel = '06';
342         last SWITCH;
343       };
344       $yymmdd <= ($yy + 810 + $dfv) && do {
345         $sel = '07';
346         last SWITCH;
347       };
348       $yymmdd <= ($yy + 910 + $dfv) && do {
349         $sel = '08';
350         last SWITCH;
351       };
352       $yymmdd <= ($yy + 1010 + $dfv) && do {
353         $sel = '09';
354         last SWITCH;
355       };
356       $yymmdd <= ($yy + 1110 + $dfv) && do {
357         $sel = '10';
358         last SWITCH;
359       };
360       $yymmdd <= ($yy + 1210) && do {
361         $sel = '11';
362         last SWITCH;
363       };
364       $yymmdd <= ($yy + 1231) && do {
365         $sel = '12';
366         last SWITCH;
367       };
368
369     }
370     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
371   . $locale->text('Select a period') . qq|" >|;
372
373     my $key = '';
374     foreach $key (sort keys %liste) {
375       my $selected = '';
376       $selected = 'selected' if ($sel eq $key);
377       $select_vorauswahl .= qq|
378          <option value="$key" $selected> $liste{$key}</option>
379       |;
380     }
381     $select_vorauswahl .= qq|</select>|;
382
383   } elsif ($form->{FA_voranmeld} eq 'quarter') {
384
385     # Vorauswahl bei quartalsweisem Voranmeldungszeitraum
386     my %liste = ('41'  => $locale->text('1. Quarter'),
387                  '42'  => $locale->text('2. Quarter'),
388                  '43'  => $locale->text('3. Quarter'),
389                  '44'  => $locale->text('4. Quarter'),
390                  '13' => $locale->text('Yearly'),);
391
392     my $yy = $form->{year} * 10000;
393     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
394     $sel    = '';
395     my $dfv = '';    # Offset für Dauerfristverlängerung
396     $dfv = '100' if ($form->{FA_dauerfrist} eq '1');
397
398   SWITCH: {
399       $yymmdd <= ($yy + 110 + $dfv) && do {
400         $form->{year} = $form->{year} - 1;
401         $sel = '44';
402         last SWITCH;
403       };
404       $yymmdd <= ($yy + 410 + $dfv) && do {
405         $sel = '41';
406         last SWITCH;
407       };
408       $yymmdd <= ($yy + 710 + $dfv) && do {
409         $sel = '42';
410         last SWITCH;
411       };
412       $yymmdd <= ($yy + 1010 + $dfv) && do {
413         $sel = '43';
414         last SWITCH;
415       };
416       $yymmdd <= ($yy + 1231) && do {
417         $sel = '44';
418       };
419     }
420
421     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
422       . $locale->text('Select a period') . qq|" >|;
423     my $key = '';
424     foreach $key (sort keys %liste) {
425       my $selected = '';
426       $selected = 'selected' if ($sel eq $key);
427       $select_vorauswahl .= qq|
428          <option value="$key" $selected>$liste{$key}</option>
429      |;
430     }
431     $select_vorauswahl .= qq|\n</select>
432    |;
433
434   } else {
435
436     # keine Vorauswahl bei Voranmeldungszeitraum
437     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
438       . $locale->text('Select a period') . qq|" >|;
439
440     my %listea = ('41' => '1. Quarter',
441                   '42' => '2. Quarter',
442                   '43' => '3. Quarter',
443                   '44' => '4. Quarter',);
444
445     my %listeb = ('01' => 'January',
446                   '02' => 'February',
447                   '03' => 'March',
448                   '04' => 'April',
449                   '05' => 'May',
450                   '06' => 'June',
451                   '07' => 'July',
452                   '08' => 'August',
453                   '09' => 'September',
454                   '10' => 'October',
455                   '11' => 'November',
456                   '12' => 'December',
457                   '13' => 'Yearly',);
458     my $key = '';
459     foreach $key (sort keys %listea) {
460       $select_vorauswahl .= qq|
461          <option value="$key">|
462         . $locale->text("$listea{$key}")
463         . qq|</option>\n|;
464     }
465
466     foreach $key (sort keys %listeb) {
467       $select_vorauswahl .= qq|
468          <option value="$key">|
469         . $locale->text("$listeb{$key}")
470         . qq|</option>\n|;
471     }
472     $select_vorauswahl .= qq|</select>|;
473   }
474   $::lxdebug->leave_sub();
475
476   return $select_vorauswahl;
477 }
478
479 #sub config {
480 #  $::lxdebug->enter_sub();
481 #  config_step1();
482 #  $::lxdebug->leave_sub();
483 #}
484
485 sub debug {
486   $::lxdebug->enter_sub();
487   $::form->debug();
488   $::lxdebug->leave_sub();
489 }
490
491 sub show_options {
492   $::lxdebug->enter_sub();
493
494   $::auth->assert('advance_turnover_tax_return');
495
496   #  $form->{PD}{$form->{type}} = "selected";
497   #  $form->{DF}{$form->{format}} = "selected";
498   #  $form->{OP}{$form->{media}} = "selected";
499   #  $form->{SM}{$form->{sendmode}} = "selected";
500   my $type   = qq|      <input type=hidden name="type" value="ustva">|;
501   my $media  = qq|      <input type=hidden name="media" value="screen">|;
502   my $format =
503       qq|       <option value=html selected>|
504     . $::locale->text('Preview')
505     . qq|</option>|;
506   if ($::lx_office_conf{print_templates}{latex}) {
507     $format .=
508         qq|    <option value=pdf>|
509       . $::locale->text('UStVA (PDF-Dokument)')
510       . qq|</option>|;
511   }
512
513   #my $disabled= qq|disabled="disabled"|;
514   #$disabled='' if ($form->{elster} eq '1' );
515   if ($::form->{elster} eq '1') {
516     $format .=
517         qq|<option value=elsterwinston>|
518       . $::locale->text('ELSTER Export (Winston)')
519       . qq|</option>|
520       . qq|<option value=elstertaxbird>|
521       . $::locale->text('ELSTER Export (Taxbird)')
522       . qq|</option>|;
523   }
524
525   #$format .= qq|<option value=elster>|.$locale->text('ELSTER Export nach Winston').qq|</option>|;
526   my $show_options = qq|
527     $type
528     $media
529     <select name=format title = "|
530     . $::locale->text('Choose Outputformat') . qq|">$format</select>
531   |;
532   $::lxdebug->leave_sub();
533
534   return $show_options;
535 }
536
537 sub generate_ustva {
538   $::lxdebug->enter_sub();
539
540   my $form     = $::form;
541   my $locale   = $::locale;
542   my %myconfig = %::myconfig;
543
544   $::auth->assert('advance_turnover_tax_return');
545
546   # Aufruf von get_config zum Einlesen der Finanzamtdaten aus finanzamt.ini
547
548   my $ustva = USTVA->new();
549   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
550
551   # init some form vars
552   my @anmeldungszeitraum =
553     qw('0401' '0402' '0403'
554        '0404' '0405' '0406'
555        '0407' '0408' '0409'
556        '0410' '0411' '0412'
557        '0441' '0442' '0443' '0444');
558
559   foreach my $item (@anmeldungszeitraum) {
560     $form->{$item} = "";
561   }
562
563     #forgotten the year --> thisyear
564     if ($form->{year} !~ m/^\d\d\d\d$/) {
565       $form->{year} = substr(
566                              $form->datetonum(
567                                     $form->current_date(\%myconfig), \%myconfig
568                              ),
569                              0, 4);
570       $::lxdebug->message(LXDebug->DEBUG1,
571                         qq|Actual year from Database: $form->{year}\n|);
572     }
573
574     #
575     # using dates in ISO-8601 format: yyyymmmdd  for Postgres...
576     #
577
578     #yearly report
579     if ($form->{period} eq "13") {
580       $form->{fromdate} = "$form->{year}0101";
581       $form->{todate}   = "$form->{year}1231";
582     }
583
584     #Quater reports
585     if ($form->{period} eq "41") {
586       $form->{fromdate} = "$form->{year}0101";
587       $form->{todate}   = "$form->{year}0331";
588       $form->{'0441'}   = "X";
589     }
590     if ($form->{period} eq "42") {
591       $form->{fromdate} = "$form->{year}0401";
592       $form->{todate}   = "$form->{year}0630";
593       $form->{'0442'}   = "X";
594     }
595     if ($form->{period} eq "43") {
596       $form->{fromdate} = "$form->{year}0701";
597       $form->{todate}   = "$form->{year}0930";
598       $form->{'0443'}   = "X";
599     }
600     if ($form->{period} eq "44") {
601       $form->{fromdate} = "$form->{year}1001";
602       $form->{todate}   = "$form->{year}1231";
603       $form->{'0444'}   = "X";
604     }
605
606     #Monthly reports
607   SWITCH: {
608       $form->{period} eq "01" && do {
609         $form->{fromdate} = "$form->{year}0101";
610         $form->{todate}   = "$form->{year}0131";
611         $form->{'0401'}   = "X";
612         last SWITCH;
613       };
614       $form->{period} eq "02" && do {
615         $form->{fromdate} = "$form->{year}0201";
616
617         #this works from 1901 to 2099, 1900 and 2100 fail.
618         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
619         $form->{todate} = "$form->{year}02$leap";
620         $form->{"0402"} = "X";
621         last SWITCH;
622       };
623       $form->{period} eq "03" && do {
624         $form->{fromdate} = "$form->{year}0301";
625         $form->{todate}   = "$form->{year}0331";
626         $form->{"0403"}   = "X";
627         last SWITCH;
628       };
629       $form->{period} eq "04" && do {
630         $form->{fromdate} = "$form->{year}0401";
631         $form->{todate}   = "$form->{year}0430";
632         $form->{"0404"}   = "X";
633         last SWITCH;
634       };
635       $form->{period} eq "05" && do {
636         $form->{fromdate} = "$form->{year}0501";
637         $form->{todate}   = "$form->{year}0531";
638         $form->{"0405"}   = "X";
639         last SWITCH;
640       };
641       $form->{period} eq "06" && do {
642         $form->{fromdate} = "$form->{year}0601";
643         $form->{todate}   = "$form->{year}0630";
644         $form->{"0406"}   = "X";
645         last SWITCH;
646       };
647       $form->{period} eq "07" && do {
648         $form->{fromdate} = "$form->{year}0701";
649         $form->{todate}   = "$form->{year}0731";
650         $form->{"0407"}   = "X";
651         last SWITCH;
652       };
653       $form->{period} eq "08" && do {
654         $form->{fromdate} = "$form->{year}0801";
655         $form->{todate}   = "$form->{year}0831";
656         $form->{"0408"}   = "X";
657         last SWITCH;
658       };
659       $form->{period} eq "09" && do {
660         $form->{fromdate} = "$form->{year}0901";
661         $form->{todate}   = "$form->{year}0930";
662         $form->{"0409"}   = "X";
663         last SWITCH;
664       };
665       $form->{period} eq "10" && do {
666         $form->{fromdate} = "$form->{year}1001";
667         $form->{todate}   = "$form->{year}1031";
668         $form->{"0410"}   = "X";
669         last SWITCH;
670       };
671       $form->{period} eq "11" && do {
672         $form->{fromdate} = "$form->{year}1101";
673         $form->{todate}   = "$form->{year}1130";
674         $form->{"0411"}   = "X";
675         last SWITCH;
676       };
677       $form->{period} eq "12" && do {
678         $form->{fromdate} = "$form->{year}1201";
679         $form->{todate}   = "$form->{year}1231";
680         $form->{"0412"}   = "X";
681         last SWITCH;
682       };
683     }
684
685   # Kontrollvariablen für die Templates
686   $form->{"year$_"} = ($form->{year} >= $_ ) ? "1":"0" for 2007..2107;
687
688   # Get the USTVA
689   $ustva->ustva(\%myconfig, \%$form);
690
691   # reformat Dates to dateformat
692   $form->{fromdate} = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
693
694   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
695   $form->{todate} = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
696
697   $form->{longperiod} =
698     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1, 0, 0);
699
700   # if there are any dates construct a where
701   if ($form->{fromdate} || $form->{todate}) {
702
703     $form->{todate} = $form->current_date(\%myconfig)  unless ($form->{todate});
704
705     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1, 0, 0);
706     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
707
708     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1, 0, 0);
709     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
710
711     $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
712     $form->{longperiod}      =
713         $locale->text('for Period')
714       . qq|<br>\n$longfromdate |
715       . $locale->text('to (date)')
716       . qq| $longtodate|;
717   }
718
719   if ($form->{comparefromdate} || $form->{comparetodate}) {
720     my $longcomparefromdate =
721       $locale->date(\%myconfig, $form->{comparefromdate}, 1, 0, 0);
722     my $shortcomparefromdate =
723       $locale->date(\%myconfig, $form->{comparefromdate}, 0, 0, 0);
724
725     my $longcomparetodate =
726       $locale->date(\%myconfig, $form->{comparetodate}, 1, 0, 0);
727     my $shortcomparetodate =
728       $locale->date(\%myconfig, $form->{comparetodate}, 0, 0, 0);
729
730     $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
731     $form->{longperiod} .=
732         "<br>\n$longcomparefromdate "
733       . $locale->text('to (date)')
734       . qq| $longcomparetodate|;
735   }
736
737   $form->{Datum_heute} =
738     $locale->date(\%myconfig, $form->current_date(\%myconfig), 0, 0, 0);
739
740   # setup variables for the form
741   my @a = qw(company businessnumber tel fax email
742     co_chief co_department co_custom1 co_custom2 co_custom3 co_custom4 co_custom5
743     co_name1 co_name2  co_street co_street1 co_zip co_city co_city1 co_country co_tel co_tel1 co_tel2
744     co_fax co_fax1 co_email co_email1 co_url co_url1 ustid duns
745     co_bankname co_bankname1 co_bankname2 co_bankname3 co_blz co_blz1
746     co_blz2 co_blz3 co_accountnr co_accountnr1 co_accountnr2 co_accountnr3);
747
748   map { $form->{$_} = $myconfig{$_} } @a;
749
750   if ($form->{address} ne '') {
751     my $temp = $form->{address};
752     $temp =~ s/\\n/<br \/>/;
753     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp);
754     $form->{co_city} =~ s/\\n//g;
755   }
756
757   ################################
758   #
759   # Nation specific customisations
760   #
761   ################################
762
763   # Germany
764
765   if ( $form->{coa} eq 'Germany-DATEV-SKR03EU' or $form->{coa} eq 'Germany-DATEV-SKR04EU') {
766
767     #
768     # Outputformat specific customisation's
769     #
770
771     my @category_cent = $ustva->report_variables({
772         myconfig    => \%myconfig,
773         form        => $form,
774         type        => '',
775         attribute   => 'position',
776         dec_places  => '2',
777     });
778
779     push @category_cent, qw(83  Z43  Z45  Z53  Z62  Z65  Z67);
780
781     my @category_euro = $ustva->report_variables({
782         myconfig    => \%myconfig,
783         form        => $form,
784         type        => '',
785         attribute   => 'position',
786         dec_places  => '0',
787     });
788
789     $form->{id} = [];
790     $form->{amount} = [];
791
792     if ( $form->{format} eq 'pdf' or $form->{format} eq 'postscript') {
793
794       $form->{IN} = "$form->{type}-$form->{year}.tex";
795       $form->{padding} = "~~";
796       $form->{bold}    = "\textbf{";
797       $form->{endbold} = "}";
798       $form->{br}      = '\\\\';
799
800       # Zahlenformatierung für Latex USTVA Formulare
801
802       foreach my $number (@category_euro) {
803         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '');
804       }
805
806       my ${decimal_comma} = ( $myconfig{numberformat} eq '1.000,00'
807            or $myconfig{numberformat} eq '1000,00' ) ? ',':'.';
808
809       foreach my $number (@category_cent) {
810         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '');
811         $form->{$number} =~ s/${decimal_comma}/~~/g;
812       }
813
814     } elsif ( $form->{format} eq 'html') { # Formatierungen für HTML Ausgabe
815
816       $form->{IN} = $form->{type} . '.html';
817       $form->{padding} = "&nbsp;&nbsp;";
818       $form->{bold}    = "<b>";
819       $form->{endbold} = "</b>";
820       $form->{br}      = "<br>";
821       $form->{address} =~ s/\\n/\n/g;
822
823       foreach my $number (@category_cent) {
824         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '0');
825       }
826
827       foreach my $number (@category_euro) {
828         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '0');
829       }
830
831     } elsif ( $form->{format} eq 'elsterwinston' ) {
832
833       $form->{IN} = 'winston.xml';
834
835       #
836       # Build Winston filename
837       #
838
839       my $file = 'U';     # 1. char 'U' = USTVA
840       $file .= $form->{period};
841       #4. and 5. char = year modulo 100
842       $file .= sprintf("%02d", $form->{year} % 100);
843       #6. to 18. char = Elstersteuernummer
844       #Beispiel: Steuernummer in Bayern
845       #111/222/33334 ergibt für UStVA Jan 2004: U01049111022233334
846       $file .= $form->{elsterFFFF};
847       $file .= $form->{elstersteuernummer};
848       #file suffix
849       $file .= '.xml';
850       $file =~ s|.*/||;
851
852       $form->{attachment_filename} = $file;
853
854       # Zahlenformatierung für Winston
855
856       my $temp_numberformat = $myconfig{numberformat};
857
858       # Numberformat must be '1000.00' for Winston
859
860       $myconfig{numberformat} = '1000.00';
861
862       foreach my $number (@category_cent) {
863         $form->{$number} = ( $form->{$number} !=0 ) ? $form->format_amount(\%myconfig, $form->{$number}, '2', '') : '';
864       }
865
866       foreach my $number (@category_euro) {
867         $form->{$number} = ( $form->{$number} !=0 ) ? $form->format_amount(\%myconfig, $form->{$number}, '0', '') : '';
868       }
869       # Re-set Numberformat
870       $myconfig{numberformat} = $temp_numberformat;
871
872       # push Kennziffern to <%foreach Array fo easyer
873       # output in xml format. Thx to Moritz.
874       my %winston_id_for = (
875          # No Winston remap?!
876       );
877
878       foreach my $kennziffer (@category_cent, @category_euro) {
879
880         next if ( $kennziffer =~ m/Z\d\d/);
881         next if (   $form->{$kennziffer} == 0 );
882
883         if (defined $winston_id_for{$kennziffer} ) {
884           push(@{ $form->{id}}, $winston_id_for{$kennziffer});
885         } else {
886           push(@{ $form->{id}}, "Kz$kennziffer");
887         }
888         push(@{ $form->{amount}}, $form->{$kennziffer});
889       }
890
891     } elsif ( $form->{format} eq 'elstertaxbird' ) {
892
893       # Define serveral filenames
894       $form->{IN} = 'taxbird.txb';
895
896       $form->{attachment_filename} = "USTVA-" . ($form->{period} * 1) . sprintf("%02d", $form->{year} % 100) . ".txb";
897
898       $form->{attachment_filename} =~ s|.*/||;
899
900       # TODO: set Output to UTF-8 or system Preference
901       #$form->{"iconv"} = Text::Iconv->new($myconfig{dbcharset}, "UTF-8");
902       #my $iconv = $self->{"iconv"};
903       #$iconv->convert($variable);
904       if ($form->{period} =~ /^[4]\d$/ ){
905         my %periods = ( # Lx => taxbird
906                      '41' => '12',
907                      '42' => '13',
908                      '43' => '14',
909                      '44' => '15',
910                    );
911
912         foreach my $quarter ( keys %periods ) {
913           $form->{taxbird_period} = $periods{$quarter} if ( $form->{period} eq $quarter);
914         }
915       } elsif ($form->{period} =~ /^\d+$/ ) {
916         $form->{period} =~ s/^0//g;
917         my $period = $form->{period};
918         $period *= 1;
919         $period--;
920         $form->{taxbird_period} = $period;
921       } else {
922         $form->header;
923         USTVA::error( $locale->text('Wrong Period' ));
924         ::end_of_request();
925       }
926
927       # heuristics for address splitting
928       # lx-office holds the entire address in a single field.
929       # taxbird expects it to be splitted into street, zipcode and city
930       if ($form->{co_street} =~ /\n/) {
931         my $new_co_street;
932         for (split /\n/, $form->{co_street}) {
933           if (/(\d{3,5})\s+(\w+)/) {
934             $form->{co_zip}  = $1;
935             $form->{co_city} = $2;
936           } else {
937             $new_co_street .= $_;
938           }
939         }
940         $form->{co_street} = $new_co_street;
941       } else {
942         $form->{co_zip} = $form->{co_city};
943         $form->{co_zip} =~ s/\D//g;
944         $form->{co_city} =~ s/\d//g;
945         $form->{co_city} =~ s/^\s//g;
946       }
947
948       my $tax_office           = first { $_->{name} eq $form->{elsterland} } @{ $ustva->{tax_office_information} };
949       $form->{taxbird_land_nr} = $tax_office->{taxbird_nr} if $tax_office;
950
951       ($form->{co_phone_prefix}, $form->{co_phone}) = split("-", $form->{tel});
952       $form->{co_phone_prefix} =~ s/\s//g;
953       $form->{co_phone} =~ s/\s//g;
954
955        $form->{taxbird_steuernummer} = $form->{steuernummer};
956       #      $form->{taxbird_steuernummer} =~ s/\D//g;
957 #      $form->{taxbird_steuernummer} =~ s/\///; # ersten Querstrich ersetzen
958
959       # Numberformatting for Taxbird
960       my $temp_numberformat = $myconfig{numberformat};
961       # Numberformat must be '1000,00' for Taxbird ?!
962       $myconfig{numberformat} = '1000,00';
963       foreach my $number (@category_cent) {
964         $form->{$number} = ( $form->{$number} !=0 ) ? $form->format_amount(\%myconfig, $form->{$number}, '2', '') : '';
965       }
966
967       foreach my $number (@category_euro) {
968         $form->{$number} = ( $form->{$number} !=0 ) ? $form->format_amount(\%myconfig, $form->{$number}, '0', '') : '';
969       }
970       # Re-set Numberformat
971       $myconfig{numberformat} = $temp_numberformat;
972
973       # push Kennziffern to <%foreach Array fo easyer
974       # output in xml format. Thx to Moritz.
975       my %taxbird_id_for = (
976
977           '511'  =>  'Kz51-calc',
978           '861'  =>  'Kz86-calc',
979           '971'  =>  'Kz97-calc',
980           '931'  =>  'Kz93-calc',
981           '811'  =>  'Kz81-calc',
982           '891'  =>  'Kz89-calc',
983           'Z45'  =>  'uebertrag',
984           'Z53'  =>  'ust-sum',
985           'Z62'  =>  'ust-minus-vost',
986           'Z65'  =>  'ust-sum+69',
987           'Z67'  =>  'ust-vz',
988       );
989
990
991       for my $kennziffer (@category_cent, @category_euro) {
992
993         next if ($kennziffer eq 'Z43');
994
995         if ($form->{$kennziffer} != 0){
996           if (defined $taxbird_id_for{$kennziffer}) {
997             push(@{ $form->{id}}, $taxbird_id_for{$kennziffer});
998           } else {
999             push(@{ $form->{id}}, "Kz$kennziffer");
1000           }
1001           push(@{ $form->{amount}}, $form->{$kennziffer});
1002         }
1003       }
1004
1005     } elsif ( $form->{format} eq '' ){ # No format error.
1006       $form->header;
1007       USTVA::error( $locale->text('Application Error. No Format given' ) . "!");
1008       ::end_of_request();
1009
1010     } else { # All other Formats are wrong
1011       $form->header;
1012       USTVA::error( $locale->text('Application Error. Wrong Format') . ": " . $form->{format} );
1013       ::end_of_request();
1014     }
1015
1016
1017   } else  # Outputformat for generic output
1018   {
1019
1020     my @category_cent = $ustva->report_variables({
1021         myconfig    => \%myconfig,
1022         form        => $form,
1023         type        => '',
1024         attribute   => 'position',
1025         dec_places  => '2',
1026     });
1027
1028     my @category_euro = $ustva->report_variables({
1029         myconfig    => \%myconfig,
1030         form        => $form,
1031         type        => '',
1032         attribute   => 'position',
1033         dec_places  => '0',
1034     });
1035
1036     $form->{USTVA} = [];
1037
1038     if ( $form->{format} eq 'generic') { # Formatierungen für HTML Ausgabe
1039
1040       my $rec_ref = {};
1041       for my $kennziffer (@category_cent, @category_euro) {
1042         $rec_ref = {};
1043         $rec_ref->{id} = $kennziffer;
1044         $rec_ref->{amount} = $form->format_amount(\%myconfig, $form->{$kennziffer}, 2, '0');
1045
1046         $::lxdebug->message($LXDebug::DEBUG, "Kennziffer $kennziffer: '$form->{$kennziffer}'" );
1047         $::lxdebug->dump($LXDebug::DEBUG, $rec_ref );
1048         push @ { $form->{USTVA} }, $rec_ref;
1049       }
1050
1051     }
1052
1053   }
1054
1055   if ( $form->{period} eq '13' and $form->{format} ne 'html') {
1056     $form->header;
1057     USTVA::info(
1058       $locale->text(
1059       'Yearly taxreport not yet implemented')
1060       . '!');
1061   }
1062
1063   $form->{templates} = $myconfig{templates};
1064   $form->{templates} = "doc" if ( $form->{type} eq 'help' );
1065
1066   if ($form->{format} eq 'generic'){
1067
1068     $form->header();
1069
1070     my $template_ref = {
1071         taxnumber => $myconfig{taxnumber},
1072     };
1073
1074     print($form->parse_html_template('ustva/generic_taxreport', $template_ref));
1075
1076   } else
1077   {
1078
1079     $form->parse_template(\%myconfig, $::lx_office_conf{paths}{userspath});
1080
1081   }
1082
1083   $::lxdebug->leave_sub();
1084 }
1085
1086 sub config_step1 {
1087   $::lxdebug->enter_sub();
1088
1089   $::auth->assert('advance_turnover_tax_return');
1090
1091 $::form->{title} = $::locale->text('Tax Office Preferences');
1092
1093   # edit all taxauthority prefs
1094
1095   $::form->header;
1096
1097   my $ustva = USTVA->new();
1098   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
1099
1100   my $land = $::form->{elsterland};
1101   my $amt  = $::form->{elsterFFFF};
1102
1103
1104   $::form->{title} = $::locale->text('Tax Office Preferences');
1105
1106
1107   my $select_tax_office               = $ustva->fa_auswahl($land, $amt, $ustva->query_finanzamt(\%::myconfig, $::form));
1108   my $checked_accrual                 = $::form->{method}        eq 'accrual' ? q|checked="checked"| : '';
1109   my $checked_cash                    = $::form->{method}        eq 'cash'    ? q|checked="checked"| : '';
1110   my $checked_monthly                 = $::form->{FA_voranmeld}  eq 'month'   ? "checked"            : '';
1111   my $checked_quarterly               = $::form->{FA_voranmeld}  eq 'quarter' ? "checked"            : '';
1112   my $checked_dauerfristverlaengerung = $::form->{FA_dauerfrist} eq '1'       ? "checked"            : '';
1113   my $checked_kz_71                   = $::form->{FA_71}         eq 'X'       ? "checked"            : '';
1114
1115   my $_hidden_variables_ref;
1116
1117   my %_hidden_local_variables = (
1118     'saved'       => $::locale->text('Check Details'),
1119     'nextsub'     => 'config_step2',
1120     'warnung'     => '0',
1121   );
1122
1123   foreach my $variable (keys %_hidden_local_variables) {
1124     push @{ $_hidden_variables_ref },
1125         { 'variable' => $variable, 'value' => $_hidden_local_variables{$variable} };
1126   }
1127
1128   my @_hidden_form_variables = qw(
1129     FA_Name             FA_Strasse        FA_PLZ
1130     FA_Ort              FA_Telefon        FA_Fax
1131     FA_PLZ_Grosskunden  FA_PLZ_Postfach   FA_Postfach
1132     FA_BLZ_1            FA_Kontonummer_1  FA_Bankbezeichnung_1
1133     FA_BLZ_2            FA_Kontonummer_2  FA_Bankbezeichnung_oertlich
1134     FA_Oeffnungszeiten  FA_Email          FA_Internet
1135     steuernummer        elsterland        elstersteuernummer
1136     elsterFFFF
1137   );
1138
1139   foreach my $variable (@_hidden_form_variables) {
1140     push @{ $_hidden_variables_ref},
1141         { 'variable' => $variable, 'value' => $::form->{$variable} };
1142   }
1143
1144 # Which COA is in use?
1145
1146   $ustva->get_coa($::form, \%::myconfig);
1147
1148   # hä? kann die weg?
1149   my $steuernummer_new = '';
1150
1151   # Variablen für das Template zur Verfügung stellen
1152   my $template_ref = {
1153      select_tax_office               => $select_tax_office,
1154      checked_accrual                 => $checked_accrual,
1155      checked_cash                    => $checked_cash,
1156      checked_monthly                 => $checked_monthly,
1157      checked_quarterly               => $checked_quarterly,
1158      checked_dauerfristverlaengerung => $checked_dauerfristverlaengerung,
1159      hidden_variables                => $_hidden_variables_ref,
1160
1161   };
1162
1163   # Ausgabe des Templates
1164   print($::form->parse_html_template('ustva/config_step1', $template_ref));
1165
1166   $::lxdebug->leave_sub();
1167 }
1168
1169 sub config_step2 {
1170   $::lxdebug->enter_sub();
1171
1172   my $form     = $::form;
1173   my $locale   = $::locale;
1174   my %myconfig = %::myconfig;
1175
1176   $::auth->assert('advance_turnover_tax_return');
1177
1178   $form->header();
1179
1180 #  print qq|
1181 #    <body>
1182 #  |;
1183
1184   my $elsterland         = '';
1185   my $elster_amt         = '';
1186   my $elsterFFFF         = '';
1187   my $elstersteuernummer = '';
1188
1189   my $ustva = USTVA->new();
1190   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini')
1191     if ($form->{saved} eq $locale->text('saved'));
1192
1193   # Auf Übergabefehler checken
1194   USTVA::info(  $locale->text('Missing Tax Authoritys Preferences') . "\n"
1195               . $locale->text('USTVA-Hint: Tax Authoritys'))
1196     if (   $form->{elsterFFFF_new} eq 'Auswahl'
1197         || $form->{elsterland_new} eq 'Auswahl');
1198   USTVA::info(  $locale->text('Missing Method!') . "\n"
1199               . $locale->text('USTVA-Hint: Method'))
1200     if ($form->{method} eq '');
1201
1202   # Klären, ob Variablen bereits befüllt sind UND ob veräderungen auf
1203   # der vorherigen Maske stattfanden: $change = 1(in der edit sub,
1204   # mittels get_config)
1205
1206   my $change = $form->{elsterland} eq $form->{elsterland_new}
1207     && $form->{elsterFFFF} eq $form->{elsterFFFF_new} ? '0' : '1';
1208   $change = '0' if ($form->{saved} eq $locale->text('saved'));
1209   my $elster_init = $ustva->query_finanzamt(\%myconfig, $form);
1210
1211   my %elster_init = %$elster_init;
1212
1213   if ($change eq '1') {
1214
1215     # Daten ändern
1216     $elsterland           = $form->{elsterland_new};
1217     $elsterFFFF           = $form->{elsterFFFF_new};
1218     $form->{elsterland}   = $elsterland;
1219     $form->{elsterFFFF}   = $elsterFFFF;
1220     $form->{steuernummer} = '';
1221
1222     create_steuernummer();
1223
1224     # rebuild elster_amt
1225     my $amt = '';
1226     foreach $amt (keys %{ $elster_init{ $form->{elsterland} } }) {
1227       $elster_amt = $amt
1228         if ($elster_init{ $form->{elsterland}{$amt} eq $form->{elsterFFFF} });
1229     }
1230
1231     # load the predefined hash data into the FA_* Vars
1232     my @variables = qw(FA_Name FA_Strasse FA_PLZ FA_Ort
1233       FA_Telefon FA_Fax FA_PLZ_Grosskunden FA_PLZ_Postfach
1234       FA_Postfach
1235       FA_BLZ_1 FA_Kontonummer_1 FA_Bankbezeichnung_1
1236       FA_BLZ_2 FA_Kontonummer_2 FA_Bankbezeichnung_oertlich
1237       FA_Oeffnungszeiten FA_Email FA_Internet);
1238
1239     for (my $i = 0; $i <= 20; $i++) {
1240       $form->{ $variables[$i] } =
1241         $elster_init->{$elsterland}->{$elsterFFFF}->[$i];
1242     }
1243
1244   } else {
1245
1246     $elsterland = $form->{elsterland};
1247     $elsterFFFF = $form->{elsterFFFF};
1248
1249   }
1250   my $stnr = $form->{steuernummer};
1251   $stnr =~ s/\D+//g;
1252   my $patterncount   = $form->{patterncount};
1253   my $elster_pattern = $form->{elster_pattern};
1254   my $delimiter      = $form->{delimiter};
1255   my $steuernummer   = $stnr eq '' ? $form->{steuernummer} : '';
1256
1257   $form->{FA_Oeffnungszeiten} =~ s/\\\\n/\n/g;
1258
1259
1260   $ustva->get_coa($form, \%myconfig);
1261
1262   my $input_steuernummer = $ustva->steuernummer_input(
1263                              $form->{elsterland},
1264                              $form->{elsterFFFF},
1265                              $form->{steuernummer}
1266   );
1267
1268   $::lxdebug->message(LXDebug->DEBUG1, qq|$input_steuernummer|);
1269
1270
1271   my $_hidden_variables_ref;
1272
1273   my %_hidden_local_variables = (
1274       'elsterland'          => $elsterland,
1275       'elsterFFFF'          => $elsterFFFF,
1276       'warnung'             => 0,
1277       'elstersteuernummer'  => $elstersteuernummer,
1278       'steuernummer'        => $stnr,
1279       'lastsub'             => 'config_step1',
1280       'nextsub'             => 'save',
1281
1282   );
1283
1284   foreach my $variable (keys %_hidden_local_variables) {
1285     push @{ $_hidden_variables_ref },
1286         { 'variable' => $variable, 'value' => $_hidden_local_variables{$variable} };
1287   }
1288
1289   my @_hidden_form_variables = qw(
1290     FA_steuerberater_name   FA_steuerberater_street
1291     FA_steuerberater_city   FA_steuerberater_tel
1292     FA_voranmeld            method
1293     FA_dauerfrist           FA_71
1294     elster
1295     type                    elster_init
1296     saved                   callback
1297   );
1298
1299   foreach my $variable (@_hidden_form_variables) {
1300     push @{ $_hidden_variables_ref},
1301         { 'variable' => $variable, 'value' => $form->{$variable} };
1302   }
1303
1304   my $template_ref = {
1305      input_steuernummer              => $input_steuernummer,
1306      readonly                        => '', #q|disabled="disabled"|,
1307      callback                        => $form->{callback},
1308      hidden_variables                => $_hidden_variables_ref,
1309   };
1310
1311   # Ausgabe des Templates
1312   print($form->parse_html_template('ustva/config_step2', $template_ref));
1313
1314
1315   $::lxdebug->leave_sub();
1316 }
1317
1318 sub create_steuernummer {
1319   $::lxdebug->enter_sub();
1320
1321   $::auth->assert('advance_turnover_tax_return');
1322
1323   my $part           = $::form->{part};
1324   my $patterncount   = $::form->{patterncount};
1325   my $delimiter      = $::form->{delimiter};
1326   my $elster_pattern = $::form->{elster_pattern};
1327
1328   # rebuild steuernummer and elstersteuernummer
1329   # es gibt eine gespeicherte steuernummer $form->{steuernummer}
1330   # und die parts und delimiter
1331
1332   my $h = 0;
1333   my $i = 0;
1334
1335   my $steuernummer_new       = $part;
1336   my $elstersteuernummer_new = $::form->{elster_FFFF};
1337   $elstersteuernummer_new .= '0';
1338
1339   for ($h = 1; $h < $patterncount; $h++) {
1340     $steuernummer_new .= qq|$delimiter|;
1341     for (my $i = 1; $i <= length($elster_pattern); $i++) {
1342       $steuernummer_new       .= $::form->{"part_$h\_$i"};
1343       $elstersteuernummer_new .= $::form->{"part_$h\_$i"};
1344     }
1345   }
1346   if ($::form->{steuernummer} ne $steuernummer_new) {
1347     $::form->{steuernummer}       = $steuernummer_new;
1348     $::form->{elstersteuernummer} = $elstersteuernummer_new;
1349     $::form->{steuernummer_new}   = $steuernummer_new;
1350   } else {
1351     $::form->{steuernummer_new}       = '';
1352     $::form->{elstersteuernummer_new} = '';
1353   }
1354   $::lxdebug->leave_sub();
1355 }
1356
1357 sub save {
1358   $::lxdebug->enter_sub();
1359
1360   $::auth->assert('advance_turnover_tax_return');
1361
1362   my $filename = "$::form->{login}_$::form->{filename}";
1363   $filename =~ s|.*/||;
1364
1365   #zuerst die steuernummer aus den part, parts_X_Y und delimiter herstellen
1366   create_steuernummer();
1367
1368   # Textboxen formatieren: Linebreaks entfernen
1369   #
1370   $::form->{FA_Oeffnungszeiten} =~ s/\r\n/\\n/g;
1371
1372   #URL mit http:// davor?
1373   $::form->{FA_Internet} =~ s/^http:\/\///;
1374   $::form->{FA_Internet} = 'http://' . $::form->{FA_Internet};
1375
1376   my @config = qw(
1377     elster              elsterland            elstersteuernummer  steuernummer
1378     elsteramt           elsterFFFF            FA_Name             FA_Strasse
1379     FA_PLZ              FA_Ort                FA_Telefon          FA_Fax
1380     FA_PLZ_Grosskunden  FA_PLZ_Postfach       FA_Postfach         FA_BLZ_1
1381     FA_Kontonummer_1    FA_Bankbezeichnung_1  FA_BLZ_2            FA_Kontonummer_2
1382     FA_Bankbezeichnung_oertlich FA_Oeffnungszeiten
1383     FA_Email FA_Internet FA_voranmeld method FA_steuerberater_name
1384     FA_steuerberater_street FA_steuerberater_city FA_steuerberater_tel
1385     FA_71 FA_dauerfrist);
1386
1387   # Hier kommt dann die Plausibilitätsprüfung der ELSTERSteuernummer
1388   if ($::form->{elstersteuernummer} ne '000000000') {
1389
1390     $::form->{elster} = '1';
1391
1392     open my $ustvaconfig, ">", "$::lx_office_conf{paths}{userspath}/$filename" or $::form->error("$filename : $!");
1393
1394     # create the config file
1395     print {$ustvaconfig} qq|# Configuration file for USTVA\n\n|;
1396     my $key = '';
1397     foreach $key (sort @config) {
1398       $::form->{$key} =~ s/\\/\\\\/g;
1399       # strip M
1400       $::form->{$key} =~ s/\r\n/\n/g;
1401
1402       print {$ustvaconfig} qq|$key=|;
1403       if ($::form->{$key} ne 'Y') {
1404         print {$ustvaconfig} qq|$::form->{$key}\n|;
1405       }
1406       if ($::form->{$key} eq 'Y') {
1407         print {$ustvaconfig} qq|checked \n|;
1408       }
1409     }
1410     print {$ustvaconfig} qq|\n\n|;
1411     close $ustvaconfig;
1412     $::form->{saved} = $::locale->text('saved');
1413
1414   } else {
1415
1416     $::form->{saved} = $::locale->text('Choose a Tax Number');
1417   }
1418
1419   config_step2();
1420   $::lxdebug->leave_sub();
1421 }
1422
1423
1424 sub continue {
1425   $::lxdebug->enter_sub();
1426
1427   # allow Symbolic references just here:
1428   call_sub($::form->{"nextsub"});
1429   $::lxdebug->leave_sub();
1430 }
1431
1432 sub back {
1433   $::lxdebug->enter_sub();
1434   call_sub($::form->{"lastsub"});
1435   $::lxdebug->leave_sub();
1436 }
1437