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