Geierlein: alter Elsterexport überarbeitet
[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 # 08.01.14  ELSTER Interface software (taxbird/winston) removed
25 #======================================================================
26
27 use strict;
28 use utf8;
29
30 require "bin/mozilla/common.pl";
31
32 #use strict;
33 #no strict 'refs';
34 #use diagnostics;
35 #use warnings; # FATAL=> 'all';
36 #use vars qw($locale $form %myconfig);
37 #our ($myconfig);
38 #use CGI::Carp "fatalsToBrowser";
39
40 use List::Util qw(first);
41
42 use SL::DB::Default;
43 use SL::PE;
44 use SL::RP;
45 use SL::USTVA;
46 use SL::User;
47 1;
48
49 # this is for our long dates
50 # $locale->text('January')
51 # $locale->text('February')
52 # $locale->text('March')
53 # $locale->text('April')
54 # $locale->text('May ')
55 # $locale->text('June')
56 # $locale->text('July')
57 # $locale->text('August')
58 # $locale->text('September')
59 # $locale->text('October')
60 # $locale->text('November')
61 # $locale->text('December')
62
63 # this is for our short month
64 # $locale->text('Jan')
65 # $locale->text('Feb')
66 # $locale->text('Mar')
67 # $locale->text('Apr')
68 # $locale->text('May')
69 # $locale->text('Jun')
70 # $locale->text('Jul')
71 # $locale->text('Aug')
72 # $locale->text('Sep')
73 # $locale->text('Oct')
74 # $locale->text('Nov')
75 # $locale->text('Dec')
76
77 #############################
78
79 sub report {
80   $::lxdebug->enter_sub();
81
82   my $form     = $::form;
83   my $locale   = $::locale;
84   my %myconfig = %::myconfig;
85
86   $::auth->assert('advance_turnover_tax_return');
87
88   my $defaults   = SL::DB::Default->get;
89   $form->{title} = $locale->text('UStVA');
90   $form->{kz10}  = '';                       #Berichtigte Anmeldung? Ja =1 Nein=0
91
92   my $year = substr($form->datetonum($form->current_date(\%myconfig), \%myconfig ),
93              0, 4);
94
95   my $department = '';
96   my $hide = '';
97   $form->header;
98
99   # Einlesen der Finanzamtdaten
100   my $ustva = USTVA->new();
101   $ustva->get_config();
102   $ustva->get_finanzamt();
103
104   my $geierlein_enabled = 0;
105   my $geierlein_path = $::lx_office_conf{paths}{geierlein_path};
106
107   if ( $geierlein_path && length($geierlein_path) > 0 ) {$geierlein_enabled=1;} 
108
109 #  $::lxdebug->message(LXDebug->DEBUG2,"geierlein_enabled=".$geierlein_enabled." path=".$geierlein_path);
110   # Hier Einlesen der user-config
111   # steuernummer entfernt für prerelease
112   my @a = qw(
113     signature      name
114     tel            fax           email         co_chief       co_department
115     co_custom1     co_custom2    co_custom3    co_custom4     co_custom5
116     co_name1       co_name2      co_street     co_street1     co_zip
117     co_city        co_city1      co_country    co_tel         co_tel1
118     co_tel2        co_fax        co_fax1       co_email       co_email1
119     co_url         co_url1       co_bankname
120     co_bankname1   co_bankname2  co_bankname3  co_blz         co_blz1
121     co_blz2        co_blz3       co_accountnr  co_accountnr1  co_accountnr2
122     co_accountnr3
123   );
124
125   $form->{$_} = $myconfig{$_} for @a;
126
127   my $openings = $form->{fa_oeffnungszeiten};
128   $openings =~ s/\\\\n/<br>/g;
129
130   my $company_given = ($form->{company} ne '')
131     ? qq|<h3>$form->{company}</h3>\n|
132     : qq|<a href="controller.pl?action=ClientConfig/edit">|
133       . $locale->text('No Company Name given') . qq|!</a><br>|;
134
135
136   # Anpassungen der Variablennamen auf pre 2.1.1 Namen
137   # klären, ob $form->{company_street|_address} gesetzt sind
138   if ($form->{address} ne '') {
139     my $temp = $form->{address};
140     $temp =~ s/\n/<br \/>/;
141     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp);
142     $form->{co_city} =~ s/\n//g;
143   }
144
145
146   my $address_given =
147     ($form->{co_street} && ($form->{co_zip} || $form->{co_city}))
148     ? qq|$form->{co_street}<br>|
149         . qq|$form->{co_street1}<br>|
150         . qq|$form->{co_zip} $form->{co_city}|
151     : qq|<a href="controller.pl?action=ClientConfig/edit">|
152         . $locale->text('No Company Address given')
153         . qq|!</a>\n|;
154
155   $form->{co_email} = $form->{email} unless $form->{co_email};
156   $form->{co_tel}   = $form->{tel}   unless $form->{co_tel};
157   $form->{co_fax}   = $form->{fax}   unless $form->{co_fax};
158   $form->{co_url}   = $form->{urlx}  unless $form->{co_url};
159
160   my $taxnumber_given = ($form->{taxnumber} ne '') ? $form->{taxnumber} : qq|<a href="ustva.pl?action=config_step1">Keine Steuernummer hinterlegt!</a><br>|;
161   my $fa_name_given = ($form->{fa_name} ne '') ? $form->{fa_name} : qq|<a href="ustva.pl?action=config_step1">Kein Finanzamt hinterlegt!</a><br>|;
162   my $ustva_vorauswahl = &ustva_vorauswahl();
163
164   my @all_years = $form->all_years(\%myconfig);
165
166   my $select_year = qq|<select name=year title="|
167     . $locale->text('Year') . qq|">|;
168   foreach my $key (@all_years) {
169     $select_year .= qq|<option |;
170     $select_year .= qq|selected| if ($key eq $form->{year});
171     $select_year .= qq| >$key</option>|;
172   }
173   $select_year   .=  qq|</select>|;
174
175   my $_checked = '';
176   $_checked = "checked" if ($form->{kz10} eq '1');
177   my $checkbox_kz_10 = qq|<input name="FA_10" id=FA_10 class=checkbox|
178     . qq| type=checkbox value="1" $_checked title = "|
179     . $locale->text('Amended Advance Turnover Tax Return').'(Nr. 10)'
180     . qq|">|
181     . $locale->text('Amended Advance Turnover Tax Return');
182
183   $_checked = "checked" if ($form->{kz22} eq '1');
184   my $checkbox_kz_22 = qq|<input name="FA_22" id=FA_22 class=checkbox|
185     . qq| type=checkbox value="1" $_checked title = "|
186     . $locale->text('Receipts attached/extra').'(Nr. 22)'
187     . qq|">|
188     . $locale->text('Receipts attached/extra');
189
190   $_checked = "checked" if ($form->{kz29} eq '1');
191   my $checkbox_kz_29 = qq|<input name="FA_29" id=FA_29 class=checkbox|
192     . qq| type=checkbox value="1" $_checked title = "|
193     . $locale->text('Accounting desired').'(Nr. 29)'
194     . qq|">|
195     . $locale->text('Accounting desired');
196
197   $_checked = "checked" if ($form->{kz26} eq '1');
198   my $checkbox_kz_26 = qq|<input name="FA_26" id=FA_26 class=checkbox|
199     . qq| type=checkbox value="1" $_checked title = "|
200     . $locale->text('Direct debit revoked').'(Nr. 26)'
201     . qq|">|
202     . $locale->text('Direct debit revoked');
203
204   my $method_local = ($form->{accounting_method} eq 'accrual') ? $locale->text('accrual')
205                    : ($form->{accounting_method} eq 'cash')    ? $locale->text('cash')
206                    : '';
207
208   my $period_local = ( $form->{fa_voranmeld} eq 'month')   ? $locale->text('month')
209                    : ( $form->{fa_voranmeld} eq 'quarter') ? $locale->text('quarter')
210                    : '';
211
212   my @tax_office_banks_ref = (
213     { BLZ             => $form->{fa_blz_1},
214       Kontonummer     => $form->{fa_kontonummer_1},
215       Bankbezeichnung => $form->{fa_bankbezeichnung_1}
216     },
217     { BLZ             => $form->{fa_blz_2},
218       Kontonummer     => $form->{fa_kontonummer_2},
219       Bankbezeichnung => $form->{fa_bankbezeichnung_2}
220     }
221   );
222
223   $ustva->get_coa($form); # fetches coa and modifies some form variables
224
225   my $template_ref = {
226     openings         => $openings,
227     company_given    => $company_given,
228     address_given    => $address_given,
229     taxnumber_given  => $taxnumber_given,
230     fa_name_given    => $fa_name_given,
231     taxnumber        => $defaults->taxnumber,
232     select_year      => $select_year,
233     period_local     => $period_local,
234     method_local     => $method_local,
235     ustva_vorauswahl => $ustva_vorauswahl,
236     checkbox_kz_10   => $checkbox_kz_10,
237     checkbox_kz_22   => $checkbox_kz_22,
238     checkbox_kz_29   => $checkbox_kz_29,
239     checkbox_kz_26   => $checkbox_kz_26,
240     tax_office_banks => \@tax_office_banks_ref,
241     geierlein_enabled => $geierlein_enabled,
242     geierlein_path   => $geierlein_path,
243     select_options   => &show_options,
244
245   };
246
247   print($form->parse_html_template('ustva/report', $template_ref));
248
249
250
251   $::lxdebug->leave_sub();
252 }
253
254
255
256 sub help {
257   $::lxdebug->enter_sub();
258
259   $::auth->assert('advance_turnover_tax_return');
260
261   # parse help documents under doc
262   $::form->{templates} = 'doc';
263   $::form->{help}      = 'ustva';
264   $::form->{type}      = 'help';
265   $::form->{format}    = 'html';
266   generate_ustva();
267
268   $::lxdebug->leave_sub();
269 }
270
271 sub show {
272   $::lxdebug->enter_sub();
273
274   $::auth->assert('advance_turnover_tax_return');
275
276   #generate_ustva();
277   $::lxdebug->leave_sub();
278   call_sub($::form->{"nextsub"});
279 }
280
281 sub ustva_vorauswahl {
282   $::lxdebug->enter_sub();
283
284   my $form     = $::form;
285   my $locale   = $::locale;
286   my %myconfig = %::myconfig;
287
288   $::auth->assert('advance_turnover_tax_return');
289
290   my $select_vorauswahl;
291
292   #Aktuelles Datum zerlegen:
293   my $date = $form->datetonum($form->current_date(\%myconfig), \%myconfig);
294
295   #$locale->date($myconfig, $form->current_date($myconfig), 0)=~ /(\d\d).(\d\d).(\d\d\d\d)/;
296   $form->{day}   = substr($date, 6, 2);
297   $form->{month} = substr($date, 4, 2);
298   $form->{year}  = substr($date, 0, 4);
299   $::lxdebug->message(LXDebug->DEBUG1, qq|
300     Actual date from Database: $date\n
301     Actual year from Database: $form->{year}\n
302     Actual day from Database: $form->{day}\n
303     Actual month from Database: $form->{month}\n|);
304
305   my $sel    = '';
306   my $yymmdd = '';
307
308   # Testdaten erzeugen:
309   #$form->{day}= '11';
310   #$form->{month}= '01';
311   #$form->{year}= 2004;
312   $select_vorauswahl = qq|
313      <input type="hidden" name="day" value="$form->{day}">
314      <input type="hidden" name="month" value="$form->{month}">
315      <input type="hidden" name="yymmdd" value="$yymmdd">
316      <input type="hidden" name="sel" value="$sel">
317   |;
318
319   if ($form->{fa_voranmeld} eq 'month') {
320
321     # Vorauswahl bei monatlichem Voranmeldungszeitraum
322
323     my %liste = ('01' => $locale->text('January'),
324                  '02' => $locale->text('February'),
325                  '03' => $locale->text('March'),
326                  '04' => $locale->text('April'),
327                  '05' => $locale->text('May'),
328                  '06' => $locale->text('June'),
329                  '07' => $locale->text('July'),
330                  '08' => $locale->text('August'),
331                  '09' => $locale->text('September'),
332                  '10' => $locale->text('October'),
333                  '11' => $locale->text('November'),
334                  '12' => $locale->text('December'),
335                  '13' => $locale->text('Yearly'),
336                 );
337
338     my $yy = $form->{year} * 10000;
339     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
340     $sel    = '';
341     my $dfv = '';
342
343     # Offset für Dauerfristverlängerung
344     $dfv = '100' if ($form->{fa_dauerfrist} eq '1');
345
346   SWITCH: {
347       $yymmdd <= ($yy + 110 + $dfv) && do {
348         $form->{year} = $form->{year} - 1;
349         $sel = '12';
350         last SWITCH;
351       };
352       $yymmdd <= ($yy + 210 + $dfv) && do {
353         $sel = '01';
354         last SWITCH;
355       };
356       $yymmdd <= ($yy + 310 + $dfv) && do {
357         $sel = '02';
358         last SWITCH;
359       };
360       $yymmdd <= ($yy + 410 + $dfv) && do {
361         $sel = '03';
362         last SWITCH;
363       };
364       $yymmdd <= ($yy + 510 + $dfv) && do {
365         $sel = '04';
366         last SWITCH;
367       };
368       $yymmdd <= ($yy + 610 + $dfv) && do {
369         $sel = '05';
370         last SWITCH;
371       };
372       $yymmdd <= ($yy + 710 + $dfv) && do {
373         $sel = '06';
374         last SWITCH;
375       };
376       $yymmdd <= ($yy + 810 + $dfv) && do {
377         $sel = '07';
378         last SWITCH;
379       };
380       $yymmdd <= ($yy + 910 + $dfv) && do {
381         $sel = '08';
382         last SWITCH;
383       };
384       $yymmdd <= ($yy + 1010 + $dfv) && do {
385         $sel = '09';
386         last SWITCH;
387       };
388       $yymmdd <= ($yy + 1110 + $dfv) && do {
389         $sel = '10';
390         last SWITCH;
391       };
392       $yymmdd <= ($yy + 1210) && do {
393         $sel = '11';
394         last SWITCH;
395       };
396       $yymmdd <= ($yy + 1231) && do {
397         $sel = '12';
398         last SWITCH;
399       };
400
401     }
402     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
403   . $locale->text('Select a period') . qq|" >|;
404
405     my $key = '';
406     foreach $key (sort keys %liste) {
407       my $selected = '';
408       $selected = 'selected' if ($sel eq $key);
409       $select_vorauswahl .= qq|
410          <option value="$key" $selected> $liste{$key}</option>
411       |;
412     }
413     $select_vorauswahl .= qq|</select>|;
414
415   } elsif ($form->{fa_voranmeld} eq 'quarter') {
416
417     # Vorauswahl bei quartalsweisem Voranmeldungszeitraum
418     my %liste = ('41'  => $locale->text('1. Quarter'),
419                  '42'  => $locale->text('2. Quarter'),
420                  '43'  => $locale->text('3. Quarter'),
421                  '44'  => $locale->text('4. Quarter'),
422                  '13' => $locale->text('Yearly'),);
423
424     my $yy = $form->{year} * 10000;
425     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
426     $sel    = '';
427     my $dfv = '';    # Offset für Dauerfristverlängerung
428     $dfv = '100' if ($form->{fa_dauerfrist} eq '1');
429
430   SWITCH: {
431       $yymmdd <= ($yy + 110 + $dfv) && do {
432         $form->{year} = $form->{year} - 1;
433         $sel = '44';
434         last SWITCH;
435       };
436       $yymmdd <= ($yy + 410 + $dfv) && do {
437         $sel = '41';
438         last SWITCH;
439       };
440       $yymmdd <= ($yy + 710 + $dfv) && do {
441         $sel = '42';
442         last SWITCH;
443       };
444       $yymmdd <= ($yy + 1010 + $dfv) && do {
445         $sel = '43';
446         last SWITCH;
447       };
448       $yymmdd <= ($yy + 1231) && do {
449         $sel = '44';
450       };
451     }
452
453     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
454       . $locale->text('Select a period') . qq|" >|;
455     my $key = '';
456     foreach $key (sort keys %liste) {
457       my $selected = '';
458       $selected = 'selected' if ($sel eq $key);
459       $select_vorauswahl .= qq|
460          <option value="$key" $selected>$liste{$key}</option>
461      |;
462     }
463     $select_vorauswahl .= qq|\n</select>
464    |;
465
466   } else {
467
468     # keine Vorauswahl bei Voranmeldungszeitraum
469     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
470       . $locale->text('Select a period') . qq|" >|;
471
472     my %listea = ('41' => '1. Quarter',
473                   '42' => '2. Quarter',
474                   '43' => '3. Quarter',
475                   '44' => '4. Quarter',);
476
477     my %listeb = ('01' => 'January',
478                   '02' => 'February',
479                   '03' => 'March',
480                   '04' => 'April',
481                   '05' => 'May',
482                   '06' => 'June',
483                   '07' => 'July',
484                   '08' => 'August',
485                   '09' => 'September',
486                   '10' => 'October',
487                   '11' => 'November',
488                   '12' => 'December',
489                   '13' => 'Yearly',);
490     my $key = '';
491     foreach $key (sort keys %listea) {
492       $select_vorauswahl .= qq|
493          <option value="$key">|
494         . $locale->text("$listea{$key}")
495         . qq|</option>\n|;
496     }
497
498     foreach $key (sort keys %listeb) {
499       $select_vorauswahl .= qq|
500          <option value="$key">|
501         . $locale->text("$listeb{$key}")
502         . qq|</option>\n|;
503     }
504     $select_vorauswahl .= qq|</select>|;
505   }
506   $::lxdebug->leave_sub();
507
508   return $select_vorauswahl;
509 }
510
511 #sub config {
512 #  $::lxdebug->enter_sub();
513 #  config_step1();
514 #  $::lxdebug->leave_sub();
515 #}
516
517 sub debug {
518   $::lxdebug->enter_sub();
519   $::form->debug();
520   $::lxdebug->leave_sub();
521 }
522
523 sub show_options {
524   $::lxdebug->enter_sub();
525
526   $::auth->assert('advance_turnover_tax_return');
527
528   #  $form->{PD}{$form->{type}} = "selected";
529   #  $form->{DF}{$form->{format}} = "selected";
530   #  $form->{OP}{$form->{media}} = "selected";
531   #  $form->{SM}{$form->{sendmode}} = "selected";
532   my $type   = qq|      <input type=hidden name="type" value="ustva">|;
533   my $media  = qq|      <input type=hidden name="media" value="screen">|;
534   my $format =
535       qq|       <option value=html selected>|
536     . $::locale->text('HTML')
537     . qq|</option>|;
538
539   #my $disabled= qq|disabled="disabled"|;
540   #$disabled='' if ($form->{elster} eq '1' );
541   #if ($::form->{elster} eq '1') {
542   if ( 1 ) {
543     $format .=
544         qq|<option value=elstertaxbird>|
545       . $::locale->text('ELSTER Export (via Geierlein)')
546       . qq|</option>|;
547   }
548
549   my $show_options = qq|
550     $type
551     $media
552     <select name=format title = "|
553     . $::locale->text('Choose Outputformat') . qq|">$format</select>
554   |;
555   $::lxdebug->leave_sub();
556
557   return $show_options;
558 }
559
560 sub generate_ustva {
561   $::lxdebug->enter_sub();
562
563   my $form     = $::form;
564   my $locale   = $::locale;
565   my %myconfig = %::myconfig;
566
567   $::auth->assert('advance_turnover_tax_return');
568
569   my $defaults = SL::DB::Default->get;
570   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
571   $form->{templates} = $defaults->templates;
572
573
574   my $ustva = USTVA->new();
575   $ustva->get_config();
576   $ustva->get_finanzamt();
577
578   # Setze Anmeldungszeitraum
579
580   $ustva->set_FromTo(\%$form);
581
582   # Get the USTVA
583   $ustva->ustva(\%myconfig, \%$form);
584
585   # reformat Dates to dateformat
586   $form->{fromdate} = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
587
588   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
589   $form->{todate} = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
590
591   $form->{longperiod} =
592     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1, 0, 0);
593
594   # if there are any dates construct a where
595   if ($form->{fromdate} || $form->{todate}) {
596
597     $form->{todate} = $form->current_date(\%myconfig)  unless ($form->{todate});
598
599     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1, 0, 0);
600     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
601
602     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1, 0, 0);
603     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
604
605     $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
606     $form->{longperiod}      =
607         $locale->text('for Period')
608       . qq|<br>\n$longfromdate |
609       . $locale->text('to (date)')
610       . qq| $longtodate|;
611   }
612
613   if ($form->{comparefromdate} || $form->{comparetodate}) {
614     my $longcomparefromdate =
615       $locale->date(\%myconfig, $form->{comparefromdate}, 1, 0, 0);
616     my $shortcomparefromdate =
617       $locale->date(\%myconfig, $form->{comparefromdate}, 0, 0, 0);
618
619     my $longcomparetodate =
620       $locale->date(\%myconfig, $form->{comparetodate}, 1, 0, 0);
621     my $shortcomparetodate =
622       $locale->date(\%myconfig, $form->{comparetodate}, 0, 0, 0);
623
624     $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
625     $form->{longperiod} .=
626         "<br>\n$longcomparefromdate "
627       . $locale->text('to (date)')
628       . qq| $longcomparetodate|;
629   }
630
631   $form->{Datum_heute} =
632     $locale->date(\%myconfig, $form->current_date(\%myconfig), 0, 0, 0);
633
634   # setup variables for the form
635   my @a = qw(tel fax email
636     co_chief co_department co_custom1 co_custom2 co_custom3 co_custom4 co_custom5
637     co_name1 co_name2  co_street co_street1 co_zip co_city co_city1 co_country co_tel co_tel1 co_tel2
638     co_fax co_fax1 co_email co_email1 co_url co_url1
639     co_bankname co_bankname1 co_bankname2 co_bankname3 co_blz co_blz1
640     co_blz2 co_blz3 co_accountnr co_accountnr1 co_accountnr2 co_accountnr3);
641
642   $form->{$_} = $myconfig{$_} for @a;
643   $form->{$_} = $defaults->$_ for qw(company address co_ustid duns);
644
645   if ($form->{address} ne '') {
646     my $temp = $form->{address};
647     $temp =~ s/\n/<br \/>/;
648     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp,2);
649     $form->{co_city} =~ s/\\n//g;
650   }
651
652   ################################
653   #
654   # Nation specific customisations
655   #
656   ################################
657
658   # Germany
659
660   if ( $form->{coa} eq 'Germany-DATEV-SKR03EU' or $form->{coa} eq 'Germany-DATEV-SKR04EU') {
661
662    $form->{id} = [];
663    $form->{amount} = [];
664
665    if ( $form->{format} eq 'pdf' or $form->{format} eq 'postscript') {
666
667       $form->{IN} = "$form->{type}-$form->{year}.tex";
668       $form->{padding} = "~~";
669       $form->{bold}    = "\textbf{";
670       $form->{endbold} = "}";
671       $form->{br}      = '\\\\';
672
673       # Zahlenformatierung für Latex USTVA Formulare
674
675       foreach my $number (@{$::form->{category_euro}}) {
676         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '');
677       }
678
679       my ${decimal_comma} = ( $myconfig{numberformat} eq '1.000,00'
680            or $myconfig{numberformat} eq '1000,00' ) ? ',':'.';
681
682       foreach my $number (@{$::form->{category_cent}}) {
683         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '');
684         $form->{$number} =~ s/${decimal_comma}/~~/g;
685       }
686
687     } elsif ( $form->{format} eq 'html') { # Formatierungen für HTML Ausgabe
688
689       $form->{IN} = $form->{type} . '.html';
690       $form->{padding} = "&nbsp;&nbsp;";
691       $form->{bold}    = "<b>";
692       $form->{endbold} = "</b>";
693       $form->{br}      = "<br>";
694       $form->{address} =~ s/\\n/\n/g;
695
696       foreach my $number (@{$::form->{category_cent}}) {
697         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '0');
698       }
699
700       foreach my $number (@{$::form->{category_euro}}) {
701         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '0');
702       }
703     } elsif ( $form->{format} eq '' ){ # No format error.
704
705       $form->header;
706       USTVA::error( $locale->text('Application Error. No Format given' ) . "!");
707       $::dispatcher->end_request;
708
709     } else { # All other Formats are wrong
710       $form->header;
711       USTVA::error( $locale->text('Application Error. Wrong Format') . ": " . $form->{format} );
712       $::dispatcher->end_request;
713     }
714
715
716   } else  # Outputformat for generic output
717   {
718
719     $form->{USTVA} = [];
720
721     if ( $form->{format} eq 'generic') { # Formatierungen für HTML Ausgabe
722
723       my $rec_ref = {};
724       for my $kennziffer (@{$::form->{category_cent}}, @{$::form->{category_euro}}) {
725         $rec_ref = {};
726         $rec_ref->{id} = $kennziffer;
727         $rec_ref->{amount} = $form->format_amount(\%myconfig, $form->{$kennziffer}, 2, '0');
728
729         $::lxdebug->message($LXDebug::DEBUG, "Kennziffer $kennziffer: '$form->{$kennziffer}'" );
730         $::lxdebug->dump($LXDebug::DEBUG, $rec_ref );
731         push @ { $form->{USTVA} }, $rec_ref;
732       }
733
734     }
735
736   }
737
738   if ( $form->{period} eq '13' and $form->{format} ne 'html') {
739     $form->header;
740     USTVA::info(
741       $locale->text(
742       'Yearly taxreport not yet implemented')
743       . '!');
744   }
745
746   $form->{templates} = "doc" if ( $form->{type} eq 'help' );
747
748   if ($form->{format} eq 'generic'){
749
750     $form->header();
751
752     my $template_ref = {
753         taxnumber => $defaults->taxnumber,
754     };
755
756     print($form->parse_html_template('ustva/generic_taxreport', $template_ref));
757
758   } elsif ( $form->{format} eq 'elstertaxbird' ) {
759    $form->parse_template(\%myconfig);
760   } else
761   {
762    # add a prefix for ustva pos numbers, i.e.: 81 ->  post_ustva_81
763    $form->{"pos_ustva_$_"} = $form->{$_} for grep { m{^\d+} } keys %{ $form };
764    $form->{title} = $locale->text('Advance turnover tax return');
765
766    $form->header;
767    print $form->parse_html_template('ustva/ustva');
768
769
770   }
771
772   $::lxdebug->leave_sub();
773 }
774
775 sub config_step1 {
776   $::lxdebug->enter_sub();
777
778   $::auth->assert('advance_turnover_tax_return');
779
780 $::form->{title} = $::locale->text('Tax Office Preferences');
781
782   # edit all taxauthority prefs
783
784   $::form->header;
785
786   my $ustva = USTVA->new();
787   $ustva->get_config();
788   $ustva->get_finanzamt();
789
790   my $land = $::form->{fa_land_nr};
791   my $amt  = $::form->{fa_bufa_nr};
792
793
794   $::form->{title} = $::locale->text('Tax Office Preferences');
795
796
797   my $select_tax_office               = $ustva->fa_auswahl($land, $amt, $ustva->query_finanzamt(\%::myconfig, $::form));
798   my $method_local = ($::form->{accounting_method} eq 'accrual') ? $::locale->text('accrual')
799                    : ($::form->{accounting_method} eq 'cash')    ? $::locale->text('cash')
800                    : '';
801
802   my $checked_monthly                 = $::form->{fa_voranmeld}  eq 'month'   ? "checked"            : '';
803   my $checked_quarterly               = $::form->{fa_voranmeld}  eq 'quarter' ? "checked"            : '';
804   my $checked_dauerfristverlaengerung = $::form->{da_dauerfrist} eq '1'       ? "checked"            : '';
805
806   my $_hidden_variables_ref;
807
808   my %_hidden_local_variables = (
809     'saved'       => $::locale->text('Check Details'),
810     'nextsub'     => 'config_step2',
811     'warnung'     => '0',
812   );
813
814   foreach my $variable (keys %_hidden_local_variables) {
815     push @{ $_hidden_variables_ref },
816         { 'variable' => $variable, 'value' => $_hidden_local_variables{$variable} };
817   }
818
819   my @_hidden_form_variables = $ustva->get_fiamt_vars();
820   push @_hidden_form_variables ,qw(fa_bufa_nr taxnumber accounting_method coa);
821
822   foreach my $variable (@_hidden_form_variables) {
823     push @{ $_hidden_variables_ref},
824         { 'variable' => $variable, 'value' => $::form->{$variable} };
825   }
826
827   $ustva->get_coa($::form); # fetches coa and modifies some form variables
828
829   # Variablen für das Template zur Verfügung stellen
830   my $template_ref = {
831      select_tax_office               => $select_tax_office,
832      method_local                    => $method_local,
833      checked_monthly                 => $checked_monthly,
834      checked_quarterly               => $checked_quarterly,
835      checked_dauerfristverlaengerung => $checked_dauerfristverlaengerung,
836      hidden_variables                => $_hidden_variables_ref,
837
838   };
839
840   # Ausgabe des Templates
841   print($::form->parse_html_template('ustva/config_step1', $template_ref));
842
843   $::lxdebug->leave_sub();
844 }
845
846 sub config_step2 {
847   $::lxdebug->enter_sub();
848
849   my $form     = $::form;
850   my $locale   = $::locale;
851   my %myconfig = %::myconfig;
852
853   $::auth->assert('advance_turnover_tax_return');
854
855   $form->header();
856
857   my $fa_land_nr         = '';
858   my $fa_bufa_nr         = '';
859
860   my $ustva = USTVA->new();
861   $ustva->get_config() if ($form->{saved} eq $locale->text('saved'));
862   my $coa = $::form->{coa};
863   $form->{"COA_$coa"}  = '1';
864   $form->{COA_Germany} = '1' if ($coa =~ m/^germany/i);
865   $ustva->get_finanzamt();
866
867
868   # Auf Übergabefehler checken
869   USTVA::info(  $locale->text('Missing Tax Authoritys Preferences') . "\n"
870               . $locale->text('USTVA-Hint: Tax Authoritys'))
871     if (   $form->{fa_bufa_nr_new} eq 'Auswahl'
872         || $form->{fa_land_nr_new} eq 'Auswahl');
873   USTVA::info(  $locale->text('Missing Method!') . "\n"
874               . $locale->text('USTVA-Hint: Method'))
875     if ($form->{accounting_method} eq '');
876
877   # Klären, ob Variablen bereits befüllt sind UND ob veränderungen auf
878   # der vorherigen Maske stattfanden: $change = 1(in der edit sub,
879   # mittels get_config)
880
881 #  $::lxdebug->message(LXDebug->DEBUG2,"land old=".$form->{fa_land_nr}." new=".$form->{fa_land_nr_new});
882 #  $::lxdebug->message(LXDebug->DEBUG2,"bufa old=".$form->{fa_bufa_nr}." new=".$form->{fa_bufa_nr_new});
883   my $change = $form->{fa_land_nr} eq $form->{fa_land_nr_new}
884     && $form->{fa_bufa_nr} eq $form->{fa_bufa_nr_new} ? '0' : '1';
885   $change = '0' if ($form->{saved} eq $locale->text('saved'));
886
887
888   if ($change eq '1') {
889
890     # Daten ändern
891     $fa_land_nr           = $form->{fa_land_nr_new};
892     $fa_bufa_nr           = $form->{fa_bufa_nr_new};
893     $form->{fa_land_nr}   = $fa_land_nr;
894     $form->{fa_bufa_nr}   = $fa_bufa_nr;
895     $form->{taxnumber} = '';
896
897     create_steuernummer();
898
899     # rebuild elster_amt
900     $ustva->get_finanzamt();
901
902   } else {
903
904     $fa_land_nr = $form->{fa_land_nr};
905     $fa_bufa_nr = $form->{fa_bufa_nr};
906
907   }
908 #  $::lxdebug->message(LXDebug->DEBUG2, "form stnr=".$form->{taxnumber}." fa_bufa_nr=".$fa_bufa_nr.
909 #                      " pattern=".$form->{elster_pattern}." fa_land_nr=".$fa_land_nr);
910   my $stnr = $form->{taxnumber};
911   $stnr =~ s/\D+//g;
912   my $taxnumber      = $stnr eq '' ? $form->{taxnumber} : '';
913
914   $form->{fa_oeffnungszeiten} =~ s/\\\\n/\n/g;
915
916
917   $ustva->get_coa($form); # fetches coa and modifies some form variables
918
919   my $input_steuernummer = $ustva->steuernummer_input(
920                              $fa_land_nr,
921                              $fa_bufa_nr,
922                              $form->{taxnumber}
923   );
924
925 #  $::lxdebug->message(LXDebug->DEBUG2, qq|$input_steuernummer|);
926
927
928   my $_hidden_variables_ref;
929
930   my %_hidden_local_variables = (
931       'fa_land_nr'          => $fa_land_nr,
932       'fa_bufa_nr'          => $fa_bufa_nr,
933       'warnung'             => 0,
934       'taxnumber'           => $stnr,
935       'lastsub'             => 'config_step1',
936       'nextsub'             => 'save',
937
938   );
939
940   foreach my $variable (keys %_hidden_local_variables) {
941     push @{ $_hidden_variables_ref },
942         { 'variable' => $variable, 'value' => $_hidden_local_variables{$variable} };
943   }
944
945   my @_hidden_form_variables = qw(
946     fa_dauerfrist fa_steuerberater_city fa_steuerberater_name
947     fa_steuerberater_street fa_steuerberater_tel 
948     fa_voranmeld fa_dauerfrist
949     accounting_method
950     type
951     saved                   callback
952   );
953
954   foreach my $variable (@_hidden_form_variables) {
955     push @{ $_hidden_variables_ref},
956         { 'variable' => $variable, 'value' => $form->{$variable} };
957   }
958
959   my $template_ref = {
960      input_steuernummer              => $input_steuernummer,
961      readonly                        => '', #q|disabled="disabled"|,
962      callback                        => $form->{callback},
963      COA_Germany                     => $form->{COA_Germany},
964      hidden_variables                => $_hidden_variables_ref,
965   };
966
967   # Ausgabe des Templates
968   print($form->parse_html_template('ustva/config_step2', $template_ref));
969
970
971   $::lxdebug->leave_sub();
972 }
973
974 sub create_steuernummer {
975   $::lxdebug->enter_sub();
976
977   $::auth->assert('advance_turnover_tax_return');
978
979   my $part           = $::form->{part};
980   my $patterncount   = $::form->{patterncount};
981   my $delimiter      = $::form->{delimiter1};
982   my $elster_pattern = $::form->{elster_pattern};
983
984   # rebuild taxnumber
985   # es gibt eine gespeicherte steuernummer $form->{taxnumber}
986   # und die parts und delimiter
987
988   my $h = 0;
989   my $i = 0;
990
991   my $taxnumber_new       = $part;
992
993   for ($h = 1; $h < $patterncount; $h++) {
994     $delimiter = $::form->{delimiter2} if $h > 1;
995     $taxnumber_new .= qq|$delimiter|;
996     for (my $i = 1; $i <= length($elster_pattern); $i++) {
997       $taxnumber_new       .= $::form->{"part_$h\_$i"};
998     }
999   }
1000 #  $::lxdebug->message(LXDebug->DEBUG2, "oldstnr=".$::form->{taxnumber}." newstnr=".$taxnumber_new);
1001   if ($::form->{taxnumber} ne $taxnumber_new) {
1002     $::form->{taxnumber}       = $taxnumber_new;
1003     $::form->{taxnumber_new}   = $taxnumber_new;
1004   } else {
1005     $::form->{taxnumber_new}       = '';
1006   }
1007   $::lxdebug->leave_sub();
1008 }
1009
1010 sub save {
1011   $::lxdebug->enter_sub();
1012
1013   $::auth->assert('advance_turnover_tax_return');
1014
1015   #zuerst die steuernummer aus den part, parts_X_Y und delimiter herstellen
1016   create_steuernummer();
1017
1018   # Textboxen formatieren: Linebreaks entfernen
1019   #
1020   $::form->{fa_oeffnungszeiten} =~ s/\r\n/\\n/g;
1021
1022   #URL mit http:// davor?
1023   $::form->{fa_internet} =~ s/^http:\/\///;
1024   $::form->{fa_internet} = 'http://' . $::form->{fa_internet};
1025
1026   # Hier kommt dann die Plausibilitätsprüfung der ELSTERSteuernummer TODO ??
1027   if (1) {
1028     my $ustva = USTVA->new();
1029     $ustva->save_config();
1030
1031     #$::form->{elster} = '1';
1032     $::form->{saved} = $::locale->text('saved');
1033
1034   } else {
1035
1036     $::form->{saved} = $::locale->text('Choose a Tax Number');
1037   }
1038
1039   config_step2();
1040   $::lxdebug->leave_sub();
1041 }
1042
1043
1044 sub continue {
1045   $::lxdebug->enter_sub();
1046
1047   # allow Symbolic references just here:
1048   call_sub($::form->{"nextsub"});
1049   $::lxdebug->leave_sub();
1050 }
1051
1052 sub back {
1053   $::lxdebug->enter_sub();
1054   call_sub($::form->{"lastsub"});
1055   $::lxdebug->leave_sub();
1056 }