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