1 # @tag: steuerfilterung
 
   2 # @description: Steuern in Dialogbuchungen filtern.
 
   3 # @depends: release_3_0_0 tax_constraints
 
   4 package SL::DBUpgrade2::steuerfilterung;
 
   8 use List::Util qw(first);
 
  10 use parent qw(SL::DBUpgrade2::Base);
 
  18   my $query = qq|ALTER TABLE tax ADD chart_categories TEXT|;
 
  19   $self->db_query($query);
 
  21   if ( $::form->{continued_tax} ) {
 
  22     foreach my $i (1 .. $::form->{rowcount}) {
 
  23       $tax_id = $::form->{"tax_id_$i"};
 
  25       $categories .= 'A' if $::form->{"asset_$i"};
 
  26       $categories .= 'L' if $::form->{"liability_$i"};
 
  27       $categories .= 'Q' if $::form->{"equity_$i"};
 
  28       $categories .= 'C' if $::form->{"costs_$i"};
 
  29       $categories .= 'I' if $::form->{"revenue_$i"};
 
  30       $categories .= 'E' if $::form->{"expense_$i"};
 
  31       $self->db_query(qq|UPDATE tax SET chart_categories = ? WHERE id = ?|, bind => [ $categories, $tax_id ]);
 
  33     $self->db_query(qq|UPDATE tax SET chart_categories = 'ALQCIE' WHERE chart_categories IS NULL|);
 
  34     $self->db_query(qq|ALTER TABLE tax ALTER COLUMN chart_categories SET NOT NULL|);
 
  38   my @well_known_taxes = (
 
  39       { taxkey => 0,  rate => 0,    taxdescription => qr{keine.*steuer}i,                       categories => 'ALQCIE' },
 
  40       { taxkey => 1,  rate => 0,    taxdescription => qr{frei}i,                                categories => 'ALQCIE' },
 
  41       { taxkey => 2,  rate => 0.07, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
 
  42       { taxkey => 3,  rate => 0.16, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
 
  43       { taxkey => 3,  rate => 0.19, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
 
  44       { taxkey => 5,  rate => 0.16, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
 
  45       { taxkey => 7,  rate => 0.16, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
 
  46       { taxkey => 8,  rate => 0.07, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
 
  47       { taxkey => 9,  rate => 0.16, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
 
  48       { taxkey => 9,  rate => 0.19, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
 
  49       { taxkey => 10, rate => 0,    taxdescription => qr{andere.*steuerpflichtige.*lieferung}i, categories => 'I' },
 
  50       { taxkey => 11, rate => 0,    taxdescription => qr{frei.*innergem.*mit}i,                 categories => 'I' },
 
  51       { taxkey => 12, rate => 0.07, taxdescription => qr{steuerpflichtig.*lieferung.*erm}i,     categories => 'I' },
 
  52       { taxkey => 13, rate => 0.16, taxdescription => qr{steuerpflichtig.*lieferung.*voll}i,    categories => 'I' },
 
  53       { taxkey => 13, rate => 0.19, taxdescription => qr{steuerpflichtig.*lieferung.*voll}i,    categories => 'I' },
 
  54       { taxkey => 15, rate => 0.16, taxdescription => qr{steuerpflicht.*eg.*lieferung}i,        categories => 'I' },
 
  55       { taxkey => 17, rate => 0.16, taxdescription => qr{steuerpflicht.*eg.*erwerb}i,           categories => 'E' },
 
  56       { taxkey => 18, rate => 0.07, taxdescription => qr{innergem.*erwerb.*erm}i,               categories => 'E' },
 
  57       { taxkey => 19, rate => 0.16, taxdescription => qr{innergem.*erwerb.*voll}i,              categories => 'E' },
 
  58       { taxkey => 19, rate => 0.19, taxdescription => qr{innergem.*erwerb.*voll}i,              categories => 'E' },
 
  61   $query = qq|SELECT taxkey, taxdescription, rate, id AS tax_id FROM tax order by taxkey, rate;|;
 
  63   my $sth = $self->dbh->prepare($query);
 
  64   $sth->execute || $::form->dberror($query);
 
  68   $::form->{PARTS} = [];
 
  69   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  70     $well_known_tax = first {
 
  71       ($ref->{taxkey} == $_->{taxkey})
 
  72       && ($ref->{rate} == $_->{rate})
 
  73       && ($ref->{taxdescription} =~ $_->{taxdescription})
 
  75     if ($well_known_tax) {
 
  76       $self->db_query(qq|UPDATE tax SET chart_categories = ? WHERE id = ?|, bind => [ $well_known_tax->{categories}, $ref->{tax_id} ]);
 
  78       $ref->{rate} = $::form->format_amount(\%::myconfig, $ref->{rate} * 100);
 
  79       push @{ $::form->{PARTS} }, $ref;
 
  83   if (scalar @{ $::form->{PARTS} } > 0){
 
  87     $query = qq|ALTER TABLE tax ALTER COLUMN chart_categories SET NOT NULL|;
 
  88     $self->db_query($query);
 
  94   print $::form->parse_html_template("dbupgrade/steuerfilterung");