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