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