epic-ts
[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($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
102
103   # Hier Einlesen der user-config
104   # steuernummer entfernt für prerelease
105   my @a = qw(
106     signature      name
107     tel            fax           email         co_chief       co_department
108     co_custom1     co_custom2    co_custom3    co_custom4     co_custom5
109     co_name1       co_name2      co_street     co_street1     co_zip
110     co_city        co_city1      co_country    co_tel         co_tel1
111     co_tel2        co_fax        co_fax1       co_email       co_email1
112     co_url         co_url1       co_bankname
113     co_bankname1   co_bankname2  co_bankname3  co_blz         co_blz1
114     co_blz2        co_blz3       co_accountnr  co_accountnr1  co_accountnr2
115     co_accountnr3
116   );
117
118   $form->{$_} = $myconfig{$_} for @a;
119   $form->{$_} = $defaults->$_ for qw(company address co_ustid duns);
120
121   my $openings = $form->{FA_Oeffnungszeiten};
122   $openings =~ s/\\\\n/<br>/g;
123
124   my $company_given = ($form->{company} ne '')
125     ? qq|<h3>$form->{company}</h3>\n|
126     : qq|<a href="am.pl?action=config">|
127       . $locale->text('No Company Name given') . qq|!</a><br>|;
128
129
130   # Anpassungen der Variablennamen auf pre 2.1.1 Namen
131   # klären, ob $form->{company_street|_address} gesetzt sind
132   if ($form->{address} ne '') {
133     my $temp = $form->{address};
134     $temp =~ s/\n/<br \/>/;
135     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp);
136     $form->{co_city} =~ s/\n//g;
137   }
138
139
140   my $address_given =
141     ($form->{co_street} && ($form->{co_zip} || $form->{co_city}))
142     ? qq|$form->{co_street}<br>|
143         . qq|$form->{co_street1}<br>|
144         . qq|$form->{co_zip} $form->{co_city}|
145     : qq|<a href="am.pl?action=config|
146         . qq|&level=Programm--Preferences">|
147         . $locale->text('No Company Address given')
148         . qq|!</a>\n|;
149
150   $form->{co_email} = $form->{email} unless $form->{co_email};
151   $form->{co_tel}   = $form->{tel}   unless $form->{co_tel};
152   $form->{co_fax}   = $form->{fax}   unless $form->{co_fax};
153   $form->{co_url}   = $form->{urlx}  unless $form->{co_url};
154
155   my $taxnumber_given = ($form->{steuernummer} ne '') ? $form->{steuernummer} : qq|<a href="ustva.pl?action=config_step1">Keine Steuernummer hinterlegt!</a><br>|;
156
157   my $ustva_vorauswahl = &ustva_vorauswahl();
158
159   my @all_years = $form->all_years(\%myconfig);
160
161   my $select_year = qq|<select name=year title="|
162     . $locale->text('Year') . qq|">|;
163   foreach my $key (@all_years) {
164     $select_year .= qq|<option |;
165     $select_year .= qq|selected| if ($key eq $form->{year});
166     $select_year .= qq| >$key</option>|;
167   }
168   $select_year   .=  qq|</select>|;
169
170   my $_checked = '';
171   $_checked = "checked" if ($form->{kz10} eq '1');
172   my $checkbox_kz_10 = qq|<input name="FA_10" id=FA_10 class=checkbox|
173     . qq| type=checkbox value="1" $_checked title = "|
174     . $locale->text('Amended Advance Turnover Tax Return (Nr. 10)')
175     . qq|">|
176     . $locale->text('Amended Advance Turnover Tax Return');
177
178   my $method_local = ($form->{method} eq 'accrual') ? $locale->text('accrual')
179                    : ($form->{method} eq 'cash')    ? $locale->text('cash')
180                    : '';
181
182   my $period_local = ( $form->{FA_voranmeld} eq 'month')   ? $locale->text('month')
183                    : ( $form->{FA_voranmeld} eq 'quarter') ? $locale->text('quarter')
184                    : '';
185
186   my $tax_office_banks_ref = [
187     { BLZ             => $form->{FA_BLZ_1},
188       Kontonummer     => $form->{FA_Kontonummer_1},
189       Bankbezeichnung => $form->{FA_Bankbezeichnung_1}
190     },
191     { BLZ             => $form->{FA_BLZ_2},
192       Kontonummer     => $form->{FA_Kontonummer_2},
193       Bankbezeichnung => $form->{FA_Bankbezeichnung_oertlich}
194     }
195   ];
196
197   # Which COA is in use?
198
199   $ustva->get_coa($form); # fetches coa and modifies some form variables
200
201   my $template_ref = {
202     openings         => $openings,
203     company_given    => $company_given,
204     address_given    => $address_given,
205     taxnumber_given  => $taxnumber_given,
206     taxnumber        => $defaults->taxnumber,
207     select_year      => $select_year,
208     period_local     => $period_local,
209     method_local     => $method_local,
210     ustva_vorauswahl => $ustva_vorauswahl,
211     checkbox_kz_10   => $checkbox_kz_10,
212     tax_office_banks => $tax_office_banks_ref,
213     select_options   => &show_options,
214
215   };
216
217   print($form->parse_html_template('ustva/report', $template_ref));
218
219
220
221   $::lxdebug->leave_sub();
222 }
223
224
225
226 sub help {
227   $::lxdebug->enter_sub();
228
229   $::auth->assert('advance_turnover_tax_return');
230
231   # parse help documents under doc
232   $::form->{templates} = 'doc';
233   $::form->{help}      = 'ustva';
234   $::form->{type}      = 'help';
235   $::form->{format}    = 'html';
236   generate_ustva();
237
238   $::lxdebug->leave_sub();
239 }
240
241 sub show {
242   $::lxdebug->enter_sub();
243
244   $::auth->assert('advance_turnover_tax_return');
245
246   #&generate_ustva();
247   $::lxdebug->leave_sub();
248   call_sub($::form->{"nextsub"});
249 }
250
251 sub ustva_vorauswahl {
252   $::lxdebug->enter_sub();
253
254   my $form     = $::form;
255   my $locale   = $::locale;
256   my %myconfig = %::myconfig;
257
258   $::auth->assert('advance_turnover_tax_return');
259
260   my $select_vorauswahl;
261
262   #Aktuelles Datum zerlegen:
263   my $date = $form->datetonum($form->current_date(\%myconfig), \%myconfig);
264
265   #$locale->date($myconfig, $form->current_date($myconfig), 0)=~ /(\d\d).(\d\d).(\d\d\d\d)/;
266   $form->{day}   = substr($date, 6, 2);
267   $form->{month} = substr($date, 4, 2);
268   $form->{year}  = substr($date, 0, 4);
269   $::lxdebug->message(LXDebug->DEBUG1, qq|
270     Actual date from Database: $date\n
271     Actual year from Database: $form->{year}\n
272     Actual day from Database: $form->{day}\n
273     Actual month from Database: $form->{month}\n|);
274
275   my $sel    = '';
276   my $yymmdd = '';
277
278   # Testdaten erzeugen:
279   #$form->{day}= '11';
280   #$form->{month}= '01';
281   #$form->{year}= 2004;
282   $select_vorauswahl = qq|
283      <input type=hidden name=day value=$form->{day}>
284      <input type=hidden name=month value=$form->{month}>
285      <input type=hidden name=yymmdd value=$yymmdd>
286      <input type=hidden name=sel value=$sel>
287   |;
288
289   if ($form->{FA_voranmeld} eq 'month') {
290
291     # Vorauswahl bei monatlichem Voranmeldungszeitraum
292
293     my %liste = ('01' => $locale->text('January'),
294                  '02' => $locale->text('February'),
295                  '03' => $locale->text('March'),
296                  '04' => $locale->text('April'),
297                  '05' => $locale->text('May'),
298                  '06' => $locale->text('June'),
299                  '07' => $locale->text('July'),
300                  '08' => $locale->text('August'),
301                  '09' => $locale->text('September'),
302                  '10' => $locale->text('October'),
303                  '11' => $locale->text('November'),
304                  '12' => $locale->text('December'),
305                  '13' => $locale->text('Yearly'),
306                 );
307
308     my $yy = $form->{year} * 10000;
309     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
310     $sel    = '';
311     my $dfv = '';
312
313     # Offset für Dauerfristverlängerung
314     $dfv = '100' if ($form->{FA_dauerfrist} eq '1');
315
316   SWITCH: {
317       $yymmdd <= ($yy + 110 + $dfv) && do {
318         $form->{year} = $form->{year} - 1;
319         $sel = '12';
320         last SWITCH;
321       };
322       $yymmdd <= ($yy + 210 + $dfv) && do {
323         $sel = '01';
324         last SWITCH;
325       };
326       $yymmdd <= ($yy + 310 + $dfv) && do {
327         $sel = '02';
328         last SWITCH;
329       };
330       $yymmdd <= ($yy + 410 + $dfv) && do {
331         $sel = '03';
332         last SWITCH;
333       };
334       $yymmdd <= ($yy + 510 + $dfv) && do {
335         $sel = '04';
336         last SWITCH;
337       };
338       $yymmdd <= ($yy + 610 + $dfv) && do {
339         $sel = '05';
340         last SWITCH;
341       };
342       $yymmdd <= ($yy + 710 + $dfv) && do {
343         $sel = '06';
344         last SWITCH;
345       };
346       $yymmdd <= ($yy + 810 + $dfv) && do {
347         $sel = '07';
348         last SWITCH;
349       };
350       $yymmdd <= ($yy + 910 + $dfv) && do {
351         $sel = '08';
352         last SWITCH;
353       };
354       $yymmdd <= ($yy + 1010 + $dfv) && do {
355         $sel = '09';
356         last SWITCH;
357       };
358       $yymmdd <= ($yy + 1110 + $dfv) && do {
359         $sel = '10';
360         last SWITCH;
361       };
362       $yymmdd <= ($yy + 1210) && do {
363         $sel = '11';
364         last SWITCH;
365       };
366       $yymmdd <= ($yy + 1231) && do {
367         $sel = '12';
368         last SWITCH;
369       };
370
371     }
372     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
373   . $locale->text('Select a period') . qq|" >|;
374
375     my $key = '';
376     foreach $key (sort keys %liste) {
377       my $selected = '';
378       $selected = 'selected' if ($sel eq $key);
379       $select_vorauswahl .= qq|
380          <option value="$key" $selected> $liste{$key}</option>
381       |;
382     }
383     $select_vorauswahl .= qq|</select>|;
384
385   } elsif ($form->{FA_voranmeld} eq 'quarter') {
386
387     # Vorauswahl bei quartalsweisem Voranmeldungszeitraum
388     my %liste = ('41'  => $locale->text('1. Quarter'),
389                  '42'  => $locale->text('2. Quarter'),
390                  '43'  => $locale->text('3. Quarter'),
391                  '44'  => $locale->text('4. Quarter'),
392                  '13' => $locale->text('Yearly'),);
393
394     my $yy = $form->{year} * 10000;
395     $yymmdd = "$form->{year}$form->{month}$form->{day}" * 1;
396     $sel    = '';
397     my $dfv = '';    # Offset für Dauerfristverlängerung
398     $dfv = '100' if ($form->{FA_dauerfrist} eq '1');
399
400   SWITCH: {
401       $yymmdd <= ($yy + 110 + $dfv) && do {
402         $form->{year} = $form->{year} - 1;
403         $sel = '44';
404         last SWITCH;
405       };
406       $yymmdd <= ($yy + 410 + $dfv) && do {
407         $sel = '41';
408         last SWITCH;
409       };
410       $yymmdd <= ($yy + 710 + $dfv) && do {
411         $sel = '42';
412         last SWITCH;
413       };
414       $yymmdd <= ($yy + 1010 + $dfv) && do {
415         $sel = '43';
416         last SWITCH;
417       };
418       $yymmdd <= ($yy + 1231) && do {
419         $sel = '44';
420       };
421     }
422
423     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
424       . $locale->text('Select a period') . qq|" >|;
425     my $key = '';
426     foreach $key (sort keys %liste) {
427       my $selected = '';
428       $selected = 'selected' if ($sel eq $key);
429       $select_vorauswahl .= qq|
430          <option value="$key" $selected>$liste{$key}</option>
431      |;
432     }
433     $select_vorauswahl .= qq|\n</select>
434    |;
435
436   } else {
437
438     # keine Vorauswahl bei Voranmeldungszeitraum
439     $select_vorauswahl .= qq|<select id="zeitraum" name="period" title="|
440       . $locale->text('Select a period') . qq|" >|;
441
442     my %listea = ('41' => '1. Quarter',
443                   '42' => '2. Quarter',
444                   '43' => '3. Quarter',
445                   '44' => '4. Quarter',);
446
447     my %listeb = ('01' => 'January',
448                   '02' => 'February',
449                   '03' => 'March',
450                   '04' => 'April',
451                   '05' => 'May',
452                   '06' => 'June',
453                   '07' => 'July',
454                   '08' => 'August',
455                   '09' => 'September',
456                   '10' => 'October',
457                   '11' => 'November',
458                   '12' => 'December',
459                   '13' => 'Yearly',);
460     my $key = '';
461     foreach $key (sort keys %listea) {
462       $select_vorauswahl .= qq|
463          <option value="$key">|
464         . $locale->text("$listea{$key}")
465         . qq|</option>\n|;
466     }
467
468     foreach $key (sort keys %listeb) {
469       $select_vorauswahl .= qq|
470          <option value="$key">|
471         . $locale->text("$listeb{$key}")
472         . qq|</option>\n|;
473     }
474     $select_vorauswahl .= qq|</select>|;
475   }
476   $::lxdebug->leave_sub();
477
478   return $select_vorauswahl;
479 }
480
481 #sub config {
482 #  $::lxdebug->enter_sub();
483 #  config_step1();
484 #  $::lxdebug->leave_sub();
485 #}
486
487 sub debug {
488   $::lxdebug->enter_sub();
489   $::form->debug();
490   $::lxdebug->leave_sub();
491 }
492
493 sub show_options {
494   $::lxdebug->enter_sub();
495
496   $::auth->assert('advance_turnover_tax_return');
497
498   #  $form->{PD}{$form->{type}} = "selected";
499   #  $form->{DF}{$form->{format}} = "selected";
500   #  $form->{OP}{$form->{media}} = "selected";
501   #  $form->{SM}{$form->{sendmode}} = "selected";
502   my $type   = qq|      <input type=hidden name="type" value="ustva">|;
503   my $media  = qq|      <input type=hidden name="media" value="screen">|;
504   my $format =
505       qq|       <option value=html selected>|
506     . $::locale->text('HTML')
507     . qq|</option>|;
508
509   my $show_options = qq|
510     $type
511     $media
512     <select name=format title = "|
513     . $::locale->text('Choose Outputformat') . qq|">$format</select>
514   |;
515   $::lxdebug->leave_sub();
516
517   return $show_options;
518 }
519
520 sub generate_ustva {
521   $::lxdebug->enter_sub();
522
523   my $form     = $::form;
524   my $locale   = $::locale;
525   my %myconfig = %::myconfig;
526
527   $::auth->assert('advance_turnover_tax_return');
528
529   my $defaults = SL::DB::Default->get;
530   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
531   $form->{templates} = $defaults->templates;
532
533   # Aufruf von get_config zum Einlesen der Finanzamtdaten aus finanzamt.ini
534
535   my $ustva = USTVA->new();
536   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
537
538   # init some form vars
539   my @anmeldungszeitraum =
540     qw('0401' '0402' '0403'
541        '0404' '0405' '0406'
542        '0407' '0408' '0409'
543        '0410' '0411' '0412'
544        '0441' '0442' '0443' '0444');
545
546   foreach my $item (@anmeldungszeitraum) {
547     $form->{$item} = "";
548   }
549
550     #forgotten the year --> thisyear
551     if ($form->{year} !~ m/^\d\d\d\d$/) {
552       $form->{year} = substr(
553                              $form->datetonum(
554                                     $form->current_date(\%myconfig), \%myconfig
555                              ),
556                              0, 4);
557       $::lxdebug->message(LXDebug->DEBUG1,
558                         qq|Actual year from Database: $form->{year}\n|);
559     }
560
561     #
562     # using dates in ISO-8601 format: yyyymmmdd  for Postgres...
563     #
564
565     #yearly report
566     if ($form->{period} eq "13") {
567       $form->{fromdate} = "$form->{year}0101";
568       $form->{todate}   = "$form->{year}1231";
569     }
570
571     #Quater reports
572     if ($form->{period} eq "41") {
573       $form->{fromdate} = "$form->{year}0101";
574       $form->{todate}   = "$form->{year}0331";
575       $form->{'0441'}   = "X";
576     }
577     if ($form->{period} eq "42") {
578       $form->{fromdate} = "$form->{year}0401";
579       $form->{todate}   = "$form->{year}0630";
580       $form->{'0442'}   = "X";
581     }
582     if ($form->{period} eq "43") {
583       $form->{fromdate} = "$form->{year}0701";
584       $form->{todate}   = "$form->{year}0930";
585       $form->{'0443'}   = "X";
586     }
587     if ($form->{period} eq "44") {
588       $form->{fromdate} = "$form->{year}1001";
589       $form->{todate}   = "$form->{year}1231";
590       $form->{'0444'}   = "X";
591     }
592
593     #Monthly reports
594   SWITCH: {
595       $form->{period} eq "01" && do {
596         $form->{fromdate} = "$form->{year}0101";
597         $form->{todate}   = "$form->{year}0131";
598         $form->{'0401'}   = "X";
599         last SWITCH;
600       };
601       $form->{period} eq "02" && do {
602         $form->{fromdate} = "$form->{year}0201";
603
604         #this works from 1901 to 2099, 1900 and 2100 fail.
605         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
606         $form->{todate} = "$form->{year}02$leap";
607         $form->{"0402"} = "X";
608         last SWITCH;
609       };
610       $form->{period} eq "03" && do {
611         $form->{fromdate} = "$form->{year}0301";
612         $form->{todate}   = "$form->{year}0331";
613         $form->{"0403"}   = "X";
614         last SWITCH;
615       };
616       $form->{period} eq "04" && do {
617         $form->{fromdate} = "$form->{year}0401";
618         $form->{todate}   = "$form->{year}0430";
619         $form->{"0404"}   = "X";
620         last SWITCH;
621       };
622       $form->{period} eq "05" && do {
623         $form->{fromdate} = "$form->{year}0501";
624         $form->{todate}   = "$form->{year}0531";
625         $form->{"0405"}   = "X";
626         last SWITCH;
627       };
628       $form->{period} eq "06" && do {
629         $form->{fromdate} = "$form->{year}0601";
630         $form->{todate}   = "$form->{year}0630";
631         $form->{"0406"}   = "X";
632         last SWITCH;
633       };
634       $form->{period} eq "07" && do {
635         $form->{fromdate} = "$form->{year}0701";
636         $form->{todate}   = "$form->{year}0731";
637         $form->{"0407"}   = "X";
638         last SWITCH;
639       };
640       $form->{period} eq "08" && do {
641         $form->{fromdate} = "$form->{year}0801";
642         $form->{todate}   = "$form->{year}0831";
643         $form->{"0408"}   = "X";
644         last SWITCH;
645       };
646       $form->{period} eq "09" && do {
647         $form->{fromdate} = "$form->{year}0901";
648         $form->{todate}   = "$form->{year}0930";
649         $form->{"0409"}   = "X";
650         last SWITCH;
651       };
652       $form->{period} eq "10" && do {
653         $form->{fromdate} = "$form->{year}1001";
654         $form->{todate}   = "$form->{year}1031";
655         $form->{"0410"}   = "X";
656         last SWITCH;
657       };
658       $form->{period} eq "11" && do {
659         $form->{fromdate} = "$form->{year}1101";
660         $form->{todate}   = "$form->{year}1130";
661         $form->{"0411"}   = "X";
662         last SWITCH;
663       };
664       $form->{period} eq "12" && do {
665         $form->{fromdate} = "$form->{year}1201";
666         $form->{todate}   = "$form->{year}1231";
667         $form->{"0412"}   = "X";
668         last SWITCH;
669       };
670     }
671
672   # Kontrollvariablen für die Templates
673   $form->{"year$_"} = ($form->{year} >= $_ ) ? "1":"0" for 2007..2107;
674
675   # Get the USTVA
676   $ustva->ustva(\%myconfig, \%$form);
677
678   # reformat Dates to dateformat
679   $form->{fromdate} = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
680
681   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
682   $form->{todate} = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
683
684   $form->{longperiod} =
685     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1, 0, 0);
686
687   # if there are any dates construct a where
688   if ($form->{fromdate} || $form->{todate}) {
689
690     $form->{todate} = $form->current_date(\%myconfig)  unless ($form->{todate});
691
692     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1, 0, 0);
693     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0, 0, 0);
694
695     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1, 0, 0);
696     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0, 0, 0);
697
698     $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
699     $form->{longperiod}      =
700         $locale->text('for Period')
701       . qq|<br>\n$longfromdate |
702       . $locale->text('to (date)')
703       . qq| $longtodate|;
704   }
705
706   if ($form->{comparefromdate} || $form->{comparetodate}) {
707     my $longcomparefromdate =
708       $locale->date(\%myconfig, $form->{comparefromdate}, 1, 0, 0);
709     my $shortcomparefromdate =
710       $locale->date(\%myconfig, $form->{comparefromdate}, 0, 0, 0);
711
712     my $longcomparetodate =
713       $locale->date(\%myconfig, $form->{comparetodate}, 1, 0, 0);
714     my $shortcomparetodate =
715       $locale->date(\%myconfig, $form->{comparetodate}, 0, 0, 0);
716
717     $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
718     $form->{longperiod} .=
719         "<br>\n$longcomparefromdate "
720       . $locale->text('to (date)')
721       . qq| $longcomparetodate|;
722   }
723
724   $form->{Datum_heute} =
725     $locale->date(\%myconfig, $form->current_date(\%myconfig), 0, 0, 0);
726
727   # setup variables for the form
728   my @a = qw(tel fax email
729     co_chief co_department co_custom1 co_custom2 co_custom3 co_custom4 co_custom5
730     co_name1 co_name2  co_street co_street1 co_zip co_city co_city1 co_country co_tel co_tel1 co_tel2
731     co_fax co_fax1 co_email co_email1 co_url co_url1
732     co_bankname co_bankname1 co_bankname2 co_bankname3 co_blz co_blz1
733     co_blz2 co_blz3 co_accountnr co_accountnr1 co_accountnr2 co_accountnr3);
734
735   $form->{$_} = $myconfig{$_} for @a;
736   $form->{$_} = $defaults->$_ for qw(company address co_ustid duns);
737
738   if ($form->{address} ne '') {
739     my $temp = $form->{address};
740     $temp =~ s/\\n/<br \/>/;
741     ($form->{co_street}, $form->{co_city}) = split("<br \/>", $temp);
742     $form->{co_city} =~ s/\\n//g;
743   }
744
745   ################################
746   #
747   # Nation specific customisations
748   #
749   ################################
750
751   # Germany
752
753   if ( $form->{coa} eq 'Germany-DATEV-SKR03EU' or $form->{coa} eq 'Germany-DATEV-SKR04EU') {
754
755     #
756     # Outputformat specific customisation's
757     #
758
759     my @category_cent = $ustva->report_variables({
760         myconfig    => \%myconfig,
761         form        => $form,
762         type        => '',
763         attribute   => 'position',
764         dec_places  => '2',
765     });
766
767     push @category_cent, qw(Z43  Z45  Z53  Z62  Z65  Z67);
768
769     my @category_euro = $ustva->report_variables({
770         myconfig    => \%myconfig,
771         form        => $form,
772         type        => '',
773         attribute   => 'position',
774         dec_places  => '0',
775     });
776
777     $form->{id} = [];
778     $form->{amount} = [];
779
780     if ( $form->{format} eq 'pdf' or $form->{format} eq 'postscript') {
781
782       $form->{IN} = "$form->{type}-$form->{year}.tex";
783       $form->{padding} = "~~";
784       $form->{bold}    = "\textbf{";
785       $form->{endbold} = "}";
786       $form->{br}      = '\\\\';
787
788       # Zahlenformatierung für Latex USTVA Formulare
789
790       foreach my $number (@category_euro) {
791         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '');
792       }
793
794       my ${decimal_comma} = ( $myconfig{numberformat} eq '1.000,00'
795            or $myconfig{numberformat} eq '1000,00' ) ? ',':'.';
796
797       foreach my $number (@category_cent) {
798         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '');
799         $form->{$number} =~ s/${decimal_comma}/~~/g;
800       }
801
802     } elsif ( $form->{format} eq 'html') { # Formatierungen für HTML Ausgabe
803
804       $form->{IN} = $form->{type} . '.html';
805       $form->{padding} = "&nbsp;&nbsp;";
806       $form->{bold}    = "<b>";
807       $form->{endbold} = "</b>";
808       $form->{br}      = "<br>";
809       $form->{address} =~ s/\\n/\n/g;
810
811       foreach my $number (@category_cent) {
812         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '2', '0');
813       }
814
815       foreach my $number (@category_euro) {
816         $form->{$number} = $form->format_amount(\%myconfig, $form->{$number}, '0', '0');
817       }
818     } elsif ( $form->{format} eq '' ){ # No format error.
819       $form->header;
820       USTVA::error( $locale->text('Application Error. No Format given' ) . "!");
821       ::end_of_request();
822
823     } else { # All other Formats are wrong
824       $form->header;
825       USTVA::error( $locale->text('Application Error. Wrong Format') . ": " . $form->{format} );
826       ::end_of_request();
827     }
828
829
830   } else  # Outputformat for generic output
831   {
832
833     my @category_cent = $ustva->report_variables({
834         myconfig    => \%myconfig,
835         form        => $form,
836         type        => '',
837         attribute   => 'position',
838         dec_places  => '2',
839     });
840
841     my @category_euro = $ustva->report_variables({
842         myconfig    => \%myconfig,
843         form        => $form,
844         type        => '',
845         attribute   => 'position',
846         dec_places  => '0',
847     });
848
849     $form->{USTVA} = [];
850
851     if ( $form->{format} eq 'generic') { # Formatierungen für HTML Ausgabe
852
853       my $rec_ref = {};
854       for my $kennziffer (@category_cent, @category_euro) {
855         $rec_ref = {};
856         $rec_ref->{id} = $kennziffer;
857         $rec_ref->{amount} = $form->format_amount(\%myconfig, $form->{$kennziffer}, 2, '0');
858
859         $::lxdebug->message($LXDebug::DEBUG, "Kennziffer $kennziffer: '$form->{$kennziffer}'" );
860         $::lxdebug->dump($LXDebug::DEBUG, $rec_ref );
861         push @ { $form->{USTVA} }, $rec_ref;
862       }
863
864     }
865
866   }
867
868   if ( $form->{period} eq '13' and $form->{format} ne 'html') {
869     $form->header;
870     USTVA::info(
871       $locale->text(
872       'Yearly taxreport not yet implemented')
873       . '!');
874   }
875
876   $form->{templates} = "doc" if ( $form->{type} eq 'help' );
877
878   if ($form->{format} eq 'generic'){
879
880     $form->header();
881
882     my $template_ref = {
883         taxnumber => $defaults->taxnumber,
884     };
885
886     print($form->parse_html_template('ustva/generic_taxreport', $template_ref));
887
888   } else
889   {
890    # add a prefix for ustva pos numbers, i.e.: 81 ->  post_ustva_81
891    $form->{"pos_ustva_$_"} = $form->{$_} for grep { m{^\d+} } keys %{ $form };
892    $form->{title} = $locale->text('Advance turnover tax return');
893
894    $form->header;
895    print $form->parse_html_template('ustva/ustva');
896
897
898   }
899
900   $::lxdebug->leave_sub();
901 }
902
903 sub config_step1 {
904   $::lxdebug->enter_sub();
905
906   $::auth->assert('advance_turnover_tax_return');
907
908 $::form->{title} = $::locale->text('Tax Office Preferences');
909
910   # edit all taxauthority prefs
911
912   $::form->header;
913
914   my $ustva = USTVA->new();
915   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini');
916
917   my $land = $::form->{elsterland};
918   my $amt  = $::form->{elsterFFFF};
919
920
921   $::form->{title} = $::locale->text('Tax Office Preferences');
922
923
924   my $select_tax_office               = $ustva->fa_auswahl($land, $amt, $ustva->query_finanzamt(\%::myconfig, $::form));
925   my $checked_accrual                 = $::form->{method}        eq 'accrual' ? q|checked="checked"| : '';
926   my $checked_cash                    = $::form->{method}        eq 'cash'    ? q|checked="checked"| : '';
927   my $checked_monthly                 = $::form->{FA_voranmeld}  eq 'month'   ? "checked"            : '';
928   my $checked_quarterly               = $::form->{FA_voranmeld}  eq 'quarter' ? "checked"            : '';
929   my $checked_dauerfristverlaengerung = $::form->{FA_dauerfrist} eq '1'       ? "checked"            : '';
930   my $checked_kz_71                   = $::form->{FA_71}         eq 'X'       ? "checked"            : '';
931
932   my $_hidden_variables_ref;
933
934   my %_hidden_local_variables = (
935     'saved'       => $::locale->text('Check Details'),
936     'nextsub'     => 'config_step2',
937     'warnung'     => '0',
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_Name             FA_Strasse        FA_PLZ
947     FA_Ort              FA_Telefon        FA_Fax
948     FA_PLZ_Grosskunden  FA_PLZ_Postfach   FA_Postfach
949     FA_BLZ_1            FA_Kontonummer_1  FA_Bankbezeichnung_1
950     FA_BLZ_2            FA_Kontonummer_2  FA_Bankbezeichnung_oertlich
951     FA_Oeffnungszeiten  FA_Email          FA_Internet
952     steuernummer        elsterland        elstersteuernummer
953     elsterFFFF
954   );
955
956   foreach my $variable (@_hidden_form_variables) {
957     push @{ $_hidden_variables_ref},
958         { 'variable' => $variable, 'value' => $::form->{$variable} };
959   }
960
961 # Which COA is in use?
962
963   $ustva->get_coa($::form); # fetches coa and modifies some form variables
964
965   # hä? kann die weg?
966   my $steuernummer_new = '';
967
968   # Variablen für das Template zur Verfügung stellen
969   my $template_ref = {
970      select_tax_office               => $select_tax_office,
971      checked_accrual                 => $checked_accrual,
972      checked_cash                    => $checked_cash,
973      checked_monthly                 => $checked_monthly,
974      checked_quarterly               => $checked_quarterly,
975      checked_dauerfristverlaengerung => $checked_dauerfristverlaengerung,
976      hidden_variables                => $_hidden_variables_ref,
977
978   };
979
980   # Ausgabe des Templates
981   print($::form->parse_html_template('ustva/config_step1', $template_ref));
982
983   $::lxdebug->leave_sub();
984 }
985
986 sub config_step2 {
987   $::lxdebug->enter_sub();
988
989   my $form     = $::form;
990   my $locale   = $::locale;
991   my %myconfig = %::myconfig;
992
993   $::auth->assert('advance_turnover_tax_return');
994
995   $form->header();
996
997   my $elsterland         = '';
998   my $elster_amt         = '';
999   my $elsterFFFF         = '';
1000   my $elstersteuernummer = '';
1001
1002   my $ustva = USTVA->new();
1003   $ustva->get_config($::lx_office_conf{paths}{userspath}, 'finanzamt.ini')
1004     if ($form->{saved} eq $locale->text('saved'));
1005
1006   # Auf Übergabefehler checken
1007   USTVA::info(  $locale->text('Missing Tax Authoritys Preferences') . "\n"
1008               . $locale->text('USTVA-Hint: Tax Authoritys'))
1009     if (   $form->{elsterFFFF_new} eq 'Auswahl'
1010         || $form->{elsterland_new} eq 'Auswahl');
1011   USTVA::info(  $locale->text('Missing Method!') . "\n"
1012               . $locale->text('USTVA-Hint: Method'))
1013     if ($form->{method} eq '');
1014
1015   # Klären, ob Variablen bereits befüllt sind UND ob veräderungen auf
1016   # der vorherigen Maske stattfanden: $change = 1(in der edit sub,
1017   # mittels get_config)
1018
1019   my $change = $form->{elsterland} eq $form->{elsterland_new}
1020     && $form->{elsterFFFF} eq $form->{elsterFFFF_new} ? '0' : '1';
1021   $change = '0' if ($form->{saved} eq $locale->text('saved'));
1022   my $elster_init = $ustva->query_finanzamt(\%myconfig, $form);
1023
1024   my %elster_init = %$elster_init;
1025
1026   if ($change eq '1') {
1027
1028     # Daten ändern
1029     $elsterland           = $form->{elsterland_new};
1030     $elsterFFFF           = $form->{elsterFFFF_new};
1031     $form->{elsterland}   = $elsterland;
1032     $form->{elsterFFFF}   = $elsterFFFF;
1033     $form->{steuernummer} = '';
1034
1035     create_steuernummer();
1036
1037     # rebuild elster_amt
1038     my $amt = $elster_init{$elsterFFFF};
1039
1040     # load the predefined hash data into the FA_* Vars
1041     my @variables = qw(FA_Name FA_Strasse FA_PLZ FA_Ort
1042       FA_Telefon FA_Fax FA_PLZ_Grosskunden FA_PLZ_Postfach
1043       FA_Postfach
1044       FA_BLZ_1 FA_Kontonummer_1 FA_Bankbezeichnung_1
1045       FA_BLZ_2 FA_Kontonummer_2 FA_Bankbezeichnung_oertlich
1046       FA_Oeffnungszeiten FA_Email FA_Internet);
1047
1048     for (my $i = 0; $i <= 20; $i++) {
1049       $form->{ $variables[$i] } =
1050         $elster_init->{$elsterland}->{$elsterFFFF}->[$i];
1051     }
1052
1053   } else {
1054
1055     $elsterland = $form->{elsterland};
1056     $elsterFFFF = $form->{elsterFFFF};
1057
1058   }
1059   my $stnr = $form->{steuernummer};
1060   $stnr =~ s/\D+//g;
1061   my $patterncount   = $form->{patterncount};
1062   my $elster_pattern = $form->{elster_pattern};
1063   my $delimiter      = $form->{delimiter};
1064   my $steuernummer   = $stnr eq '' ? $form->{steuernummer} : '';
1065
1066   $form->{FA_Oeffnungszeiten} =~ s/\\\\n/\n/g;
1067
1068
1069   $ustva->get_coa($form); # fetches coa and modifies some form variables
1070
1071   my $input_steuernummer = $ustva->steuernummer_input(
1072                              $form->{elsterland},
1073                              $form->{elsterFFFF},
1074                              $form->{steuernummer}
1075   );
1076
1077   $::lxdebug->message(LXDebug->DEBUG1, qq|$input_steuernummer|);
1078
1079
1080   my $_hidden_variables_ref;
1081
1082   my %_hidden_local_variables = (
1083       'elsterland'          => $elsterland,
1084       'elsterFFFF'          => $elsterFFFF,
1085       'warnung'             => 0,
1086       'elstersteuernummer'  => $elstersteuernummer,
1087       'steuernummer'        => $stnr,
1088       'lastsub'             => 'config_step1',
1089       'nextsub'             => 'save',
1090
1091   );
1092
1093   foreach my $variable (keys %_hidden_local_variables) {
1094     push @{ $_hidden_variables_ref },
1095         { 'variable' => $variable, 'value' => $_hidden_local_variables{$variable} };
1096   }
1097
1098   my @_hidden_form_variables = qw(
1099     FA_steuerberater_name   FA_steuerberater_street
1100     FA_steuerberater_city   FA_steuerberater_tel
1101     FA_voranmeld            method
1102     FA_dauerfrist           FA_71
1103     elster
1104     type                    elster_init
1105     saved                   callback
1106   );
1107
1108   foreach my $variable (@_hidden_form_variables) {
1109     push @{ $_hidden_variables_ref},
1110         { 'variable' => $variable, 'value' => $form->{$variable} };
1111   }
1112
1113   my $template_ref = {
1114      input_steuernummer              => $input_steuernummer,
1115      readonly                        => '', #q|disabled="disabled"|,
1116      callback                        => $form->{callback},
1117      hidden_variables                => $_hidden_variables_ref,
1118   };
1119
1120   # Ausgabe des Templates
1121   print($form->parse_html_template('ustva/config_step2', $template_ref));
1122
1123
1124   $::lxdebug->leave_sub();
1125 }
1126
1127 sub create_steuernummer {
1128   $::lxdebug->enter_sub();
1129
1130   $::auth->assert('advance_turnover_tax_return');
1131
1132   my $part           = $::form->{part};
1133   my $patterncount   = $::form->{patterncount};
1134   my $delimiter      = $::form->{delimiter};
1135   my $elster_pattern = $::form->{elster_pattern};
1136
1137   # rebuild steuernummer and elstersteuernummer
1138   # es gibt eine gespeicherte steuernummer $form->{steuernummer}
1139   # und die parts und delimiter
1140
1141   my $h = 0;
1142   my $i = 0;
1143
1144   my $steuernummer_new       = $part;
1145   my $elstersteuernummer_new = $::form->{elster_FFFF};
1146   $elstersteuernummer_new .= '0';
1147
1148   for ($h = 1; $h < $patterncount; $h++) {
1149     $steuernummer_new .= qq|$delimiter|;
1150     for (my $i = 1; $i <= length($elster_pattern); $i++) {
1151       $steuernummer_new       .= $::form->{"part_$h\_$i"};
1152       $elstersteuernummer_new .= $::form->{"part_$h\_$i"};
1153     }
1154   }
1155   if ($::form->{steuernummer} ne $steuernummer_new) {
1156     $::form->{steuernummer}       = $steuernummer_new;
1157     $::form->{elstersteuernummer} = $elstersteuernummer_new;
1158     $::form->{steuernummer_new}   = $steuernummer_new;
1159   } else {
1160     $::form->{steuernummer_new}       = '';
1161     $::form->{elstersteuernummer_new} = '';
1162   }
1163   $::lxdebug->leave_sub();
1164 }
1165
1166 sub save {
1167   $::lxdebug->enter_sub();
1168
1169   $::auth->assert('advance_turnover_tax_return');
1170
1171   my $filename = "$::myconfig{login}_$::form->{filename}";
1172   $filename =~ s|.*/||;
1173
1174   #zuerst die steuernummer aus den part, parts_X_Y und delimiter herstellen
1175   create_steuernummer();
1176
1177   # Textboxen formatieren: Linebreaks entfernen
1178   #
1179   $::form->{FA_Oeffnungszeiten} =~ s/\r\n/\\n/g;
1180
1181   #URL mit http:// davor?
1182   $::form->{FA_Internet} =~ s/^http:\/\///;
1183   $::form->{FA_Internet} = 'http://' . $::form->{FA_Internet};
1184
1185   my @config = qw(
1186     elster              elsterland            elstersteuernummer  steuernummer
1187     elsteramt           elsterFFFF            FA_Name             FA_Strasse
1188     FA_PLZ              FA_Ort                FA_Telefon          FA_Fax
1189     FA_PLZ_Grosskunden  FA_PLZ_Postfach       FA_Postfach         FA_BLZ_1
1190     FA_Kontonummer_1    FA_Bankbezeichnung_1  FA_BLZ_2            FA_Kontonummer_2
1191     FA_Bankbezeichnung_oertlich FA_Oeffnungszeiten
1192     FA_Email FA_Internet FA_voranmeld method FA_steuerberater_name
1193     FA_steuerberater_street FA_steuerberater_city FA_steuerberater_tel
1194     FA_71 FA_dauerfrist);
1195
1196   # Hier kommt dann die Plausibilitätsprüfung der ELSTERSteuernummer
1197   if ($::form->{elstersteuernummer} ne '000000000') {
1198
1199     $::form->{elster} = '1';
1200
1201     open my $ustvaconfig, ">", "$::lx_office_conf{paths}{userspath}/$filename" or $::form->error("$filename : $!");
1202
1203     # create the config file
1204     print {$ustvaconfig} qq|# Configuration file for USTVA\n\n|;
1205     my $key = '';
1206     foreach $key (sort @config) {
1207       $::form->{$key} =~ s/\\/\\\\/g;
1208       # strip M
1209       $::form->{$key} =~ s/\r\n/\n/g;
1210
1211       print {$ustvaconfig} qq|$key=|;
1212       if ($::form->{$key} ne 'Y') {
1213         print {$ustvaconfig} qq|$::form->{$key}\n|;
1214       }
1215       if ($::form->{$key} eq 'Y') {
1216         print {$ustvaconfig} qq|checked \n|;
1217       }
1218     }
1219     print {$ustvaconfig} qq|\n\n|;
1220     close $ustvaconfig;
1221     $::form->{saved} = $::locale->text('saved');
1222
1223   } else {
1224
1225     $::form->{saved} = $::locale->text('Choose a Tax Number');
1226   }
1227
1228   config_step2();
1229   $::lxdebug->leave_sub();
1230 }
1231
1232
1233 sub continue {
1234   $::lxdebug->enter_sub();
1235
1236   # allow Symbolic references just here:
1237   call_sub($::form->{"nextsub"});
1238   $::lxdebug->leave_sub();
1239 }
1240
1241 sub back {
1242   $::lxdebug->enter_sub();
1243   call_sub($::form->{"lastsub"});
1244   $::lxdebug->leave_sub();
1245 }