1 #=====================================================================
 
   3 # Copyright (c) 2004 by Udo Spallek, Aachen
 
   7 #     Web: http://www.lx-office.org
 
  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.
 
  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 #======================================================================
 
  24 #=====================================================================
 
  28 use List::Util qw(first);
 
  35 my @tax_office_information = (
 
  36   { 'id' =>  8, 'name' => 'Baden-Württemberg',      'taxbird_nr' => '0',  'elster_format' => 'FF/BBB/UUUUP',  },
 
  37   { 'id' =>  9, 'name' => 'Bayern',                 'taxbird_nr' => '1',  'elster_format' => 'FFF/BBB/UUUUP', },
 
  38   { 'id' => 11, 'name' => 'Berlin',                 'taxbird_nr' => '2',  'elster_format' => 'FF/BBB/UUUUP',  },
 
  39   { 'id' => 12, 'name' => 'Brandenburg',            'taxbird_nr' => '3',  'elster_format' => 'FFF/BBB/UUUUP', },
 
  40   { 'id' =>  4, 'name' => 'Bremen',                 'taxbird_nr' => '4',  'elster_format' => 'FF BBB UUUUP',  },
 
  41   { 'id' =>  2, 'name' => 'Hamburg',                'taxbird_nr' => '5',  'elster_format' => 'FF/BBB/UUUUP',  },
 
  42   { 'id' =>  6, 'name' => 'Hessen',                 'taxbird_nr' => '6',  'elster_format' => '0FF BBB UUUUP', },
 
  43   { 'id' => 13, 'name' => 'Mecklenburg-Vorpommern', 'taxbird_nr' => '7',  'elster_format' => 'FFF/BBB/UUUUP', },
 
  44   { 'id' =>  3, 'name' => 'Niedersachsen',          'taxbird_nr' => '8',  'elster_format' => 'FF/BBB/UUUUP',  },
 
  45   { 'id' =>  5, 'name' => 'Nordrhein-Westfalen',    'taxbird_nr' => '9',  'elster_format' => 'FFF/BBBB/UUUP', },
 
  46   { 'id' =>  7, 'name' => 'Rheinland-Pfalz',        'taxbird_nr' => '10', 'elster_format' => 'FF/BBB/UUUU/P', },
 
  47   { 'id' => 10, 'name' => 'Saarland',               'taxbird_nr' => '11', 'elster_format' => 'FFF/BBB/UUUUP', },
 
  48   { 'id' => 14, 'name' => 'Sachsen',                'taxbird_nr' => '12', 'elster_format' => 'FFF/BBB/UUUUP', },
 
  49   { 'id' => 15, 'name' => 'Sachsen-Anhalt',         'taxbird_nr' => '13', 'elster_format' => 'FFF/BBB/UUUUP', },
 
  50   { 'id' =>  1, 'name' => 'Schleswig-Holstein',     'taxbird_nr' => '14', 'elster_format' => 'FF BBB UUUUP',  },
 
  51   { 'id' => 16, 'name' => 'Thüringen',              'taxbird_nr' => '15', 'elster_format' => 'FFF/BBB/UUUUP', },
 
  69   $self->{tax_office_information} = [];
 
  71   foreach (@tax_office_information) {
 
  73     $entry->{name} = $::locale->{iconv_utf8}->convert($entry->{name});
 
  74     push @{ $self->{tax_office_information} }, $entry;
 
  80   my ( $self, $form, $myconfig) = @_;
 
  82   my $query = q{ SELECT coa FROM defaults };
 
  84   my $dbh = $form->dbconnect($myconfig);
 
  85   my $sth = $dbh->prepare($query);
 
  86   $sth->execute() || $form->dberror($query);
 
  88   my ($coa) = selectrow_query($form, $dbh, $query);
 
  94   $form->{"COA_$coa"} = '1';
 
  95   $form->{COA_Germany} = '1' if ($coa =~ m/^germany/i);
 
 101 sub report_variables {
 
 102   # Get all positions for taxreport out of the database
 
 103   # Needs Databaseupdate Pg-upgrade2/USTVA_abstraction.pl
 
 105   return unless defined wantarray;
 
 110   my $myconfig   = $arg_ref->{myconfig};
 
 111   my $form       = $arg_ref->{form};
 
 112   my $type       = $arg_ref->{type}; # 'paied' || 'received' || ''
 
 113   my $attribute  = $arg_ref->{attribute}; #
 
 114   my $dec_places = (defined $arg_ref->{dec_places}) ? $arg_ref->{dec_places}:undef;
 
 116   my $where_type = $type ? "AND tax.report_headings.type = '$type'" : '';
 
 117   my $where_dcp  = defined $dec_places ? "AND tax.report_variables.dec_places = '$dec_places'" : '';
 
 121     FROM tax.report_variables
 
 122     LEFT JOIN tax.report_headings
 
 123       ON (tax.report_variables.heading_id = tax.report_headings.id)
 
 129   my $dbh = $form->dbconnect($myconfig);
 
 130   my $sth = $dbh->prepare($query);
 
 132   $sth->execute() || $form->dberror($query);
 
 136   while ( my $row_ref = $sth->fetchrow_arrayref() ) {
 
 137     push @positions, @$row_ref;  # Copy the array contents
 
 148 sub create_steuernummer {
 
 149   $main::lxdebug->enter_sub();
 
 151   my $form = $main::form;
 
 155   my $part           = $form->{part};
 
 156   my $patterncount   = $form->{patterncount};
 
 157   my $delimiter      = $form->{delimiter};
 
 158   my $elster_pattern = $form->{elster_pattern};
 
 160   # rebuild steuernummer and elstersteuernummer
 
 161   # es gibt eine gespeicherte steuernummer $form->{steuernummer}
 
 162   # und die parts und delimiter
 
 167   my $steuernummer_new        = $part;
 
 168   my $elstersteuernummer_new  = $elster_FFFF;
 
 169   $elstersteuernummer_new    .= '0';
 
 171   for ($h = 1; $h < $patterncount; $h++) {
 
 172     $steuernummer_new .= qq|$delimiter|;
 
 173     for ($i = 1; $i <= length($elster_pattern); $i++) {
 
 174       $steuernummer_new       .= $form->{"part_$h\_$i"};
 
 175       $elstersteuernummer_new .= $form->{"part_$h\_$i"};
 
 178   if ($form->{steuernummer} ne $steuernummer_new) {
 
 179     $form->{steuernummer}       = $steuernummer_new;
 
 180     $form->{elstersteuernummer} = $elstersteuernummer_new;
 
 181     $form->{steuernummer_new}   = $steuernummer_new;
 
 183   $main::lxdebug->leave_sub();
 
 184   return ($steuernummer_new, $elstersteuernummer_new);
 
 187 sub steuernummer_input {
 
 188   $main::lxdebug->enter_sub();
 
 190   my ($self, $elsterland, $elsterFFFF, $steuernummer) = @_;
 
 191   our ($elster_FFFF, $elster_land);
 
 193   my $steuernummer_input = '';
 
 195   $elster_land  = $elsterland;
 
 196   $elster_FFFF  = $elsterFFFF;
 
 197   $steuernummer = '0000000000' if ($steuernummer eq '');
 
 199   # $steuernummer formatieren (nur Zahlen) -> $stnr
 
 200   my $stnr = $steuernummer;
 
 203   #Pattern description Elstersteuernummer
 
 206   my $tax_office     = first { $_->{name} eq $elster_land } @{ $self->{tax_office_information} };
 
 207   my $elster_pattern = $tax_office->{elster_format};
 
 208   my @elster_pattern = split(' ', $elster_pattern);
 
 209   my $delimiter      = ' ';
 
 210   my $patterncount   = @elster_pattern;
 
 211   if ($patterncount < 2) {
 
 212     @elster_pattern = ();
 
 213     @elster_pattern = split('/', $elster_pattern);
 
 215     $patterncount   = @elster_pattern;
 
 218   # no we have an array of patternparts and a delimiter
 
 219   # create the first automated and fixed part and delimiter
 
 221   $steuernummer_input .= qq|<b><font size="+1">|;
 
 224     $elster_pattern[0] eq 'FFF' && do {
 
 225       $part = substr($elster_FFFF, 1, 4);
 
 226       $steuernummer_input .= qq|$part|;
 
 229     $elster_pattern[0] eq '0FF' && do {
 
 230       $part = '0' . substr($elster_FFFF, 2, 4);
 
 231       $steuernummer_input .= qq|$part|;
 
 234     $elster_pattern[0] eq 'FF' && do {
 
 235       $part = substr($elster_FFFF, 2, 4);
 
 236       $steuernummer_input .= qq|$part|;
 
 240       $steuernummer_input .= qq|Fehler!|;
 
 245   #now the rest of the Steuernummer ...
 
 246   $steuernummer_input .= qq|</b></font>|;
 
 247   $steuernummer_input .= qq|\n
 
 248            <input type=hidden name="elster_pattern" value="$elster_pattern">
 
 249            <input type=hidden name="patterncount" value="$patterncount">
 
 250            <input type=hidden name="patternlength" value="$patterncount">
 
 251            <input type=hidden name="delimiter" value="$delimiter">
 
 252            <input type=hidden name="part" value="$part">
 
 257   for (my $h = 1; $h < $patterncount; $h++) {
 
 258     $steuernummer_input .= qq| $delimiter \n|;
 
 259     for (my $i = 1; $i <= length($elster_pattern[$h]); $i++) {
 
 260       $steuernummer_input .= qq|<select name="part_$h\_$i">\n|;
 
 262       for (my $j = 0; $j <= 9; $j++) {
 
 263         $steuernummer_input .= qq|      <option value="$j"|;
 
 264         if ($steuernummer ne '') {
 
 265           if ($j eq substr($stnr, length($part) + $k, 1)) {
 
 266             $steuernummer_input .= qq| selected|;
 
 269         $steuernummer_input .= qq|>$j</option>\n|;
 
 272       $steuernummer_input .= qq|</select>\n|;
 
 276   $main::lxdebug->leave_sub();
 
 278   return $steuernummer_input;
 
 282   $main::lxdebug->enter_sub();
 
 286   # Referenz wird übergeben, hash of hash wird nicht
 
 287   # in neues  Hash kopiert, sondern direkt über die Referenz verändert
 
 288   # Prototyp für diese Konstruktion
 
 290   my ($self, $land, $elsterFFFF, $elster_init) = @_;
 
 293   my $FFFF     = $elsterFFFF;
 
 296   $checked = 'checked' if ($elsterFFFF eq '' and $land eq '');
 
 300         <script language="Javascript">
 
 301         function update_auswahl()
 
 303                 var elsterBLAuswahl = document.verzeichnis.elsterland_new;
 
 304                 var elsterFAAuswahl = document.verzeichnis.elsterFFFF_new;
 
 306                 elsterFAAuswahl.options.length = 0; // dropdown aufräumen
 
 309   foreach my $elster_land (sort keys %$elster_init) {
 
 311                if (elsterBLAuswahl.options[elsterBLAuswahl.selectedIndex].
 
 312                value == "$elster_land")
 
 316     %elster_land_fa = ();
 
 318     for $FFFF (keys %{ $elster_init->{$elster_land} }) {
 
 319       $elster_land_fa{$FFFF} = $elster_init->{$elster_land}->{$FFFF}->[0];
 
 321     foreach $ffff (sort { $elster_land_fa{$a} cmp $elster_land_fa{$b} }
 
 322                    keys(%elster_land_fa)
 
 325                    elsterFAAuswahl.options[$j] = new Option("$elster_land_fa{$ffff} ($ffff)","$ffff");|;
 
 341               <select size="1" name="elsterland_new" onchange="update_auswahl()">|;
 
 343     $fa_auswahl .= qq|<option value="Auswahl" $checked>| . $main::locale->text('Select federal state...') . qq|</option>\n|;
 
 345   foreach my $elster_land (sort keys %$elster_init) {
 
 347                   <option value="$elster_land"|;
 
 348     if ($elster_land eq $land and $checked eq '') {
 
 349       $fa_auswahl .= qq| selected|;
 
 351     $fa_auswahl .= qq|>$elster_land</option>
 
 359   my $elster_land = '';
 
 360   $elster_land = ($land ne '') ? $land : '';
 
 361   %elster_land_fa = ();
 
 362   for $FFFF (keys %{ $elster_init->{$elster_land} }) {
 
 363     $elster_land_fa{$FFFF} = $elster_init->{$elster_land}->{$FFFF}->[0];
 
 371                  <select size="1" name="elsterFFFF_new">|;
 
 372   if ($elsterFFFF eq '') {
 
 373     $fa_auswahl .= qq|<option value="Auswahl" $checked>| . $main::locale->text('Select tax office...') . qq|</option>|;
 
 375     foreach $ffff (sort { $elster_land_fa{$a} cmp $elster_land_fa{$b} }
 
 376                    keys(%elster_land_fa)
 
 380                         <option value="$ffff"|;
 
 381       if ($ffff eq $elsterFFFF and $checked eq '') {
 
 382         $fa_auswahl .= qq| selected|;
 
 384       $fa_auswahl .= qq|>$elster_land_fa{$ffff} ($ffff)</option>|;
 
 393   $main::lxdebug->leave_sub();
 
 399   $main::lxdebug->enter_sub();
 
 403   if ($ENV{HTTP_USER_AGENT}) {
 
 406     print qq|<body><h2 class=info>Hinweis</h2>
 
 412     <input type=button value="| . $main::locale->text('Back') . qq|" onClick="history.go(-1)">
 
 420     die "Hinweis: $msg\n";
 
 423   $main::lxdebug->leave_sub();
 
 426 # 20.10.2009 sschoeling: this sub seems to be orphaned.
 
 428   $main::lxdebug->enter_sub();
 
 431   # soll mal eine Erinnerungsfunktion für USTVA Abgaben werden, die automatisch
 
 432   # den Termin der nächsten USTVA anzeigt.
 
 435   my ($today, $FA_dauerfrist, $FA_voranmeld) = @_;
 
 440   $today =~ /(\d\d\d\d)(\d\d)(\d\d)/;
 
 446   my $yymmdd   = "$year$month$day" * 1;
 
 447   my $mmdd     = "$month$day" * 1;
 
 451   #$ical = '...vcal format';
 
 453   #if ($FA_voranmeld eq 'month'){
 
 456     "0110" => 'December',
 
 458     "0310" => 'February',
 
 465     "1010" => 'September',
 
 467     "1210" => 'November',
 
 472   $month += 1 if ($day > 10);
 
 473   $month    = sprintf("%02d", $month);
 
 474   $stichtag = $year . $month . "10";
 
 475   my $ust_va   = $month . "10";
 
 477   foreach my $date (%liste) {
 
 478     $ust_va = $liste{$date} if ($date eq $stichtag);
 
 481   #} elsif ($FA_voranmeld eq 'quarter'){
 
 486   #@stichtag = ('10.04.2004', '10.05.2004');
 
 488   #@liste = ['0110', '0210', '0310', '0410', '0510', '0610', '0710', '0810', '0910',
 
 489   #          '1010', '1110', '1210', ];
 
 491   #foreach $key (@liste){
 
 492   #  #if ($ddmm < ('0110' * 1));
 
 494   #  $stichtag = $liste[$key - 1] if ($ddmm > $key);
 
 498   #$stichtag =~ /([\d]\d)(\d\d)$/
 
 499   #$stichtag = "$1.$2.$yy"
 
 501   our $description; # most probably not existent.
 
 502   our $tage_bis;    # most probably not existent.
 
 503   our $ical;        # most probably not existent.
 
 505   $main::lxdebug->leave_sub();
 
 506   return ($stichtag, $description, $tage_bis, $ical);
 
 509 sub query_finanzamt {
 
 510   $main::lxdebug->enter_sub();
 
 512   my ($self, $myconfig, $form) = @_;
 
 514   my $dbh = $form->dbconnect($myconfig) or $self->error(DBI->errstr);
 
 516   #Test, if table finanzamt exist
 
 517   my $table    = 'finanzamt';
 
 518   my $filename = "sql/$table.sql";
 
 520   my $tst = $dbh->prepare("SELECT * FROM $table");
 
 524     #There is no table, read the table from sql/finanzamt.sql
 
 525     print qq|<p>Bitte warten, Tabelle $table wird einmalig in Datenbank:
 
 526     $myconfig->{dbname} als Benutzer: $myconfig->{dbuser} hinzugefügt...</p>|;
 
 527     process_query($form, $dbh, $filename) || $self->error(DBI->errstr);
 
 529     #execute second last call
 
 530     my $dbh = $form->dbconnect($myconfig) or $self->error(DBI->errstr);
 
 547     'FA_PLZ_Grosskunden',     #  9
 
 548     'FA_PLZ_Postfach',        # 10
 
 551     'FA_Kontonummer_1',       # 13
 
 552     'FA_Bankbezeichnung_1',   # 14
 
 553                               #'FA_BankIBAN_1',                            # 15
 
 554                               #'FA_BankBIC_1',                             # 16
 
 555                               #'FA_BankInhaber_BUFA_Nr_1',                 # 17
 
 557     'FA_Kontonummer_2',       # 19
 
 558     'FA_Bankbezeichnung_2',   # 20
 
 559                               #'FA_BankIBAN_2',                            # 21
 
 560                               #'FA_BankBIC_2',                             # 22
 
 561                               #'FA_BankInhaber_BUFA_Nr_2',                 # 23
 
 562     'FA_Oeffnungszeiten',     # 24
 
 565                               #'FA_zustaendige_Hauptstelle_BUFA_Nr',       # 27
 
 566                               #'FA_zustaendige_vorgesetzte_Finanzbehoerde' # 28
 
 569   my $field = join(', ', @vars);
 
 571   my $query = "SELECT $field FROM finanzamt ORDER BY FA_Land_nr";
 
 572   my $sth = $dbh->prepare($query) or $self->error($dbh->errstr);
 
 573   $sth->execute || $form->dberror($query);
 
 574   my $array_ref = $sth->fetchall_arrayref();
 
 577   foreach my $row (@$array_ref) {
 
 578     my $FA_finanzamt = $row;
 
 579     my $tax_office   = first { $_->{id} == $FA_finanzamt->[0] } @{ $self->{tax_office_information} };
 
 580     $land            = $tax_office->{name};
 
 582     # $land = $main::locale->{iconv}->convert($land);
 
 584     my $ffff = @$FA_finanzamt[1];
 
 587     $rec->{$land} = $ffff;
 
 592     $finanzamt{$land}{$ffff} = [@$FA_finanzamt];
 
 598   $main::lxdebug->leave_sub();
 
 604   $main::lxdebug->enter_sub();
 
 606   # Copyright D. Simander -> SL::Form under Gnu GPL.
 
 607   my ($form, $dbh, $filename) = @_;
 
 609   #  return unless (-f $filename);
 
 611   open my $FH, "<", "$filename" or $form->error("$filename : $!\n");
 
 618     # Remove DOS and Unix style line endings.
 
 621     # don't add comments or empty lines
 
 622     next if /^(--.*|\s+)$/;
 
 624     for (my $i = 0; $i < length($_); $i++) {
 
 625       my $char = substr($_, $i, 1);
 
 627       # Are we inside a string?
 
 629         if ($char eq $quote_chars[-1]) {
 
 635         if (($char eq "'") || ($char eq "\"")) {
 
 636           push(@quote_chars, $char);
 
 638         } elsif ($char eq ";") {
 
 640           # Query is complete. Send it.
 
 642           $sth = $dbh->prepare($query);
 
 643           $sth->execute || $form->dberror($query);
 
 657   $main::lxdebug->leave_sub();
 
 661   $main::lxdebug->enter_sub();
 
 663   my ($self, $myconfig, $form) = @_;
 
 665   # connect to database
 
 666   my $dbh = $form->dbconnect($myconfig);
 
 669   my $category        = "pos_ustva";
 
 671   my @category_cent = USTVA->report_variables({
 
 672       myconfig    => $myconfig,
 
 675       attribute   => 'position',
 
 679   push @category_cent, qw(83  Z43  Z45  Z53  Z62  Z65  Z67);
 
 681   my @category_euro = USTVA->report_variables({
 
 682       myconfig    => $myconfig,
 
 685       attribute   => 'position',
 
 689   push @category_euro, USTVA->report_variables({
 
 690       myconfig    => $myconfig,
 
 693       attribute   => 'position',
 
 697   $form->{decimalplaces} *= 1;
 
 699   foreach my $item (@category_cent) {
 
 700     $form->{"$item"} = 0;
 
 702   foreach my $item (@category_euro) {
 
 703     $form->{"$item"} = 0;
 
 705   my $coa_name = coa_get($dbh);
 
 706   $form->{coa} = $coa_name;
 
 708   # Controlvariable for templates
 
 709   $form->{"$coa_name"} = '1';
 
 711   $main::lxdebug->message(LXDebug->DEBUG2(), "COA: '$form->{coa}',  \$form->{$coa_name} = 1");
 
 713   &get_accounts_ustva($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
 716   ###########################################
 
 718   # Nationspecific Modfications
 
 720   ###########################################
 
 724   if ( $form->{coa} eq 'Germany-DATEV-SKR03EU' or $form->{coa} eq 'Germany-DATEV-SKR04EU'){
 
 727     # Umordnen der Kennziffern
 
 728     if ( $form->{year} < 2007) {
 
 729       $form->{35} += $form->{81};
 
 730       $form->{36} += $form->{811};
 
 731       $form->{95} += $form->{89};
 
 732       $form->{98} += $form->{891};
 
 733       map { delete $form->{$_} } qw(81 811 89 891);
 
 735       $form->{35} += $form->{51};
 
 736       $form->{36} += $form->{511};
 
 737       $form->{95} += $form->{97};
 
 738       $form->{98} += $form->{971};
 
 739       map { delete $form->{$_} } qw(51 511 97 971);
 
 745   # Fixme: Wird auch noch für Oesterreich gebraucht,
 
 746   # weil kein eigenes Ausgabeformular
 
 747   # sotte aber aus der allgeméinen Steuerberechnung verschwinden
 
 749   # Berechnung der USTVA Formularfelder laut Bogen 207
 
 752   $form->{"51r"} = $form->{"511"};
 
 753   $form->{"86r"} = $form->{"861"};
 
 754   $form->{"97r"} = $form->{"971"};
 
 755   $form->{"93r"} = $form->{"931"};
 
 757   $form->{"Z43"} = $form->{"511"}     + $form->{"811"} + $form->{"861"}
 
 758                      + $form->{"36"}  + $form->{"80"}  + $form->{"971"}
 
 759                      + $form->{"891"} + $form->{"931"} + $form->{"96"}
 
 762   $form->{"Z45"} = $form->{"Z43"};
 
 764   $form->{"Z53"} = $form->{"Z45"}     + $form->{"53"}  + $form->{"74"}
 
 765                      + $form->{"85"}  + $form->{"65"};
 
 767   $form->{"Z62"} = $form->{"Z43"}     - $form->{"66"}  - $form->{"61"}
 
 768                      - $form->{"62"}  - $form->{"67"}  - $form->{"63"}
 
 769                      - $form->{"64"}  - $form->{"59"};
 
 771   $form->{"Z65"} = $form->{"Z62"}     - $form->{"69"};
 
 772   $form->{"83"}  = $form->{"Z65"}     - $form->{"39"};
 
 776   $main::lxdebug->leave_sub();
 
 782   my $form  = $main::form;
 
 784   my $query= qq|SELECT coa FROM defaults|;
 
 786   my $sth = $dbh->prepare($query);
 
 788   $sth->execute || $form->dberror($query);
 
 790   my ($ref) = $sth->fetchrow_array;
 
 796 sub get_accounts_ustva {
 
 797   $main::lxdebug->enter_sub();
 
 799   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 811     my $gltaxkey_where = "(tk.pos_ustva>=59 AND tk.pos_ustva<=66)";
 
 814     if ($form->{method} eq 'cash') {
 
 815       $subwhere .= " AND transdate >= '$fromdate'";
 
 816       $glwhere = " AND ac.transdate >= '$fromdate'";
 
 817       $ARwhere .= " AND acc.transdate >= '$fromdate'";
 
 819     $APwhere .= " AND AP.transdate >= '$fromdate'";
 
 820     $where .= " AND ac.transdate >= '$fromdate'";
 
 824     $where    .= " AND ac.transdate <= '$todate'";
 
 825     $ARwhere  .= " AND acc.transdate <= '$todate'";
 
 828   my $acc_trans_where = '1=1';
 
 829   if ($fromdate || $todate) {
 
 830     $acc_trans_where = "ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE ";
 
 833       $acc_trans_where .= "transdate >= '$fromdate'";
 
 836       $acc_trans_where .= " AND " if ($fromdate);
 
 837       $acc_trans_where .= "transdate <= '$todate'";
 
 840     $acc_trans_where .= ")";
 
 843   ############################################
 
 844   # Method eq 'cash' = IST Versteuerung
 
 845   ############################################
 
 846   # Betrifft nur die eingenommene Umsatzsteuer
 
 848   ############################################
 
 850   if ($form->{method} eq 'cash') {
 
 854          -- USTVA IST-Versteuerung
 
 856          -- Alle tatsaechlichen _Zahlungseingaenge_
 
 857          -- im Voranmeldezeitraum erfassen
 
 858          -- (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
 
 860             -- Bezahlt / Rechnungssumme
 
 862              SELECT SUM(acc.amount)
 
 864              INNER JOIN chart c ON (acc.chart_id   =   c.id
 
 865                                     AND c.link   like  '%AR_paid%')
 
 869               AND acc.trans_id = ac.trans_id
 
 873             SELECT amount FROM ar WHERE id = ac.trans_id
 
 878        LEFT JOIN chart c ON (c.id  = ac.chart_id)
 
 879        LEFT JOIN ar      ON (ar.id = ac.trans_id)
 
 880        LEFT JOIN taxkeys tk ON (
 
 882            SELECT id FROM taxkeys
 
 883            WHERE chart_id   = ac.chart_id
 
 884              -- AND taxkey_id  = ac.taxkey
 
 885              AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
 
 886            ORDER BY startdate DESC LIMIT 1
 
 891        GROUP BY tk.pos_ustva
 
 894   } elsif ($form->{method} eq 'accrual') {
 
 895     #########################################
 
 896     # Method eq 'accrual' = Soll Versteuerung
 
 897     #########################################
 
 900        -- Alle Einnahmen AR und pos_ustva erfassen
 
 902          - sum(ac.amount) AS amount,
 
 905        JOIN chart c ON (c.id = ac.chart_id)
 
 906        JOIN ar ON (ar.id = ac.trans_id)
 
 909            SELECT id FROM taxkeys
 
 910            WHERE chart_id   = ac.chart_id
 
 911              AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
 
 912            ORDER BY startdate DESC LIMIT 1
 
 918        GROUP BY tk.pos_ustva
 
 923     $form->error("Unknown tax method: $form->{method}")
 
 927   #########################################
 
 928   # Ausgaben und Gl Buchungen sind gleich
 
 929   # für Ist- und Soll-Versteuerung
 
 930   #########################################
 
 932      UNION -- alle Ausgaben AP erfassen
 
 935          sum(ac.amount) AS amount,
 
 938        JOIN ap ON (ap.id = ac.trans_id )
 
 939        JOIN chart c ON (c.id = ac.chart_id)
 
 940        LEFT JOIN taxkeys tk ON (
 
 942              SELECT id FROM taxkeys
 
 944                AND chart_id=ac.chart_id
 
 945                --AND taxkey_id = ac.taxkey
 
 946                AND startdate <= COALESCE(AP.transdate)
 
 947              ORDER BY startdate DESC LIMIT 1
 
 953        GROUP BY tk.pos_ustva
 
 955      UNION -- Einnahmen direkter gl Buchungen erfassen
 
 958          ( - ac.amount) AS amount,
 
 961        JOIN chart c ON (c.id = ac.chart_id)
 
 962        JOIN gl a ON (a.id = ac.trans_id)
 
 963        LEFT JOIN taxkeys tk ON (
 
 965            SELECT id FROM taxkeys
 
 966            WHERE chart_id=ac.chart_id
 
 967              AND NOT $gltaxkey_where
 
 968              AND startdate <= COALESCE(ac.transdate)
 
 969            ORDER BY startdate DESC LIMIT 1
 
 976        GROUP BY tk.pos_ustva
 
 979      UNION -- Ausgaben direkter gl Buchungen erfassen
 
 982          (ac.amount) AS amount,
 
 985        JOIN chart c ON (c.id = ac.chart_id)
 
 986        JOIN gl a ON (a.id = ac.trans_id)
 
 987        LEFT JOIN taxkeys tk ON (
 
 989            SELECT id FROM taxkeys
 
 990            WHERE chart_id=ac.chart_id
 
 992              AND startdate <= COALESCE(ac.transdate)
 
 993            ORDER BY startdate DESC LIMIT 1
 
1000        GROUP BY tk.pos_ustva
 
1008   # Show all $query in Debuglevel LXDebug::QUERY
 
1009   my $callingdetails = (caller (0))[3];
 
1010   $main::lxdebug->message(LXDebug->QUERY(), "$callingdetails \$query=\n $query");
 
1012   my $sth = $dbh->prepare($query);
 
1014   $sth->execute || $form->dberror($query);
 
1016   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
1018     $ref->{amount} *= -1;
 
1019     $form->{ $ref->{$category} } += $ref->{amount};
 
1024   $main::lxdebug->leave_sub();
 
1029   $main::lxdebug->enter_sub();
 
1031   my ($self, $userspath, $filename) = @_;
 
1033   my $form = $main::form;
 
1035   $form->error("Missing Parameter: @_") if !$userspath || !$filename;
 
1037   $filename = "$form->{login}_$filename";
 
1038   $filename =~ s|.*/||;
 
1039   $filename = "$userspath/$filename";
 
1040   open my $FACONF, "<", $filename or do {# Annon Sub
 
1042     # create file if file does not exist
 
1043     open my $FANEW, ">", $filename  or $form->error("CREATE: $filename : $!");
 
1044     close $FANEW                    or $form->error("CLOSE: $filename : $!");
 
1046     #try again open file
 
1047     open my $FACONF, "<", $filename or $form->error("OPEN: $filename : $!");
 
1052     next if (/^(\#|\s)/);
 
1057     # remove any trailing whitespace
 
1058     s/^\s*(.*?)\s*$/$1/;
 
1059     my ($key, $value) = split(/=/, $_, 2);
 
1061     $form->{$key} = "$value";
 
1067   $main::lxdebug->leave_sub();