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