Versionsnummer im Adminloginscreen immer richtig ausgeben
[kivitendo-erp.git] / sql / Pg-upgrade2 / steuerfilterung.pl
1 # @tag: steuerfilterung
2 # @description: Steuern in Dialogbuchungen filtern.
3 # @depends: release_3_0_0 tax_constraints
4 package SL::DBUpgrade2::steuerfilterung;
5
6 use strict;
7 use utf8;
8 use List::Util qw(first);
9
10 use parent qw(SL::DBUpgrade2::Base);
11
12 sub run {
13   my ($self) = @_;
14
15   my $categories;
16   my $tax_id;
17
18   if ( $::form->{continued_tax} ) {
19     my $update_query;
20     foreach my $i (1 .. $::form->{rowcount}) {
21       $tax_id = $::form->{"tax_id_$i"};
22       $categories = '';
23       $categories .= 'A' if $::form->{"asset_$i"};
24       $categories .= 'L' if $::form->{"liability_$i"};
25       $categories .= 'Q' if $::form->{"equity_$i"};
26       $categories .= 'C' if $::form->{"costs_$i"};
27       $categories .= 'I' if $::form->{"revenue_$i"};
28       $categories .= 'E' if $::form->{"expense_$i"};
29       $update_query = qq|UPDATE tax SET chart_categories = '$categories' WHERE id=$tax_id;|;
30       $self->db_query($update_query);
31     }
32     $update_query = qq|ALTER TABLE tax ALTER COLUMN chart_categories SET NOT NULL|;
33     $self->db_query($update_query);
34     $self->dbh->commit();
35     return 1;
36   }
37
38   my $query = qq|ALTER TABLE tax ADD chart_categories TEXT|;
39   $self->db_query($query);
40   $self->dbh->commit();
41
42   my @well_known_taxes = (
43       { taxkey => 0,  rate => 0,    taxdescription => qr{keine.*steuer}i,                       categories => 'ALQCIE' },
44       { taxkey => 1,  rate => 0,    taxdescription => qr{frei}i,                                categories => 'ALQCIE' },
45       { taxkey => 2,  rate => 0.07, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
46       { taxkey => 3,  rate => 0.16, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
47       { taxkey => 3,  rate => 0.19, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
48       { taxkey => 5,  rate => 0.16, taxdescription => qr{umsatzsteuer}i,                        categories => 'I' },
49       { taxkey => 7,  rate => 0.16, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
50       { taxkey => 8,  rate => 0.07, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
51       { taxkey => 9,  rate => 0.16, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
52       { taxkey => 9,  rate => 0.19, taxdescription => qr{vorsteuer}i,                           categories => 'E' },
53       { taxkey => 10, rate => 0,    taxdescription => qr{andere.*steuerpflichtige.*lieferung}i, categories => 'I' },
54       { taxkey => 11, rate => 0,    taxdescription => qr{frei.*innergem.*mit}i,                 categories => 'I' },
55       { taxkey => 12, rate => 0.07, taxdescription => qr{steuerpflichtig.*lieferung.*erm}i,     categories => 'I' },
56       { taxkey => 13, rate => 0.16, taxdescription => qr{steuerpflichtig.*lieferung.*voll}i,    categories => 'I' },
57       { taxkey => 13, rate => 0.19, taxdescription => qr{steuerpflichtig.*lieferung.*voll}i,    categories => 'I' },
58       { taxkey => 15, rate => 0.16, taxdescription => qr{steuerpflicht.*eg.*lieferung}i,        categories => 'I' },
59       { taxkey => 17, rate => 0.16, taxdescription => qr{steuerpflicht.*eg.*erwerb}i,           categories => 'E' },
60       { taxkey => 18, rate => 0.07, taxdescription => qr{innergem.*erwerb.*erm}i,               categories => 'E' },
61       { taxkey => 19, rate => 0.16, taxdescription => qr{innergem.*erwerb.*voll}i,              categories => 'E' },
62       { taxkey => 19, rate => 0.19, taxdescription => qr{innergem.*erwerb.*voll}i,              categories => 'E' },
63       );
64
65   $query = qq|SELECT taxkey, taxdescription, rate, id AS tax_id FROM tax order by taxkey, rate;|;
66
67   my $sth = $self->dbh->prepare($query);
68   $sth->execute || $::form->dberror($query);
69
70   my $well_known_tax;
71
72   $::form->{PARTS} = [];
73   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
74     $well_known_tax = first {
75       ($ref->{taxkey} == $_->{taxkey})
76       && ($ref->{rate} == $_->{rate})
77       && ($ref->{taxdescription} =~ $_->{taxdescription})
78     } @well_known_taxes;
79     if ($well_known_tax) {
80       $categories = $well_known_tax->{categories};
81       $tax_id = $ref->{tax_id};
82       $query = qq|UPDATE tax SET chart_categories = '$categories' WHERE id=$tax_id;|;
83       $self->db_query($query);
84     } else {
85       $ref->{rate} = $::form->format_amount(\%::myconfig, $::form->round_amount($ref->{rate} * 100));
86       push @{ $::form->{PARTS} }, $ref;
87     }
88   }
89
90   if (scalar @{ $::form->{PARTS} } > 0){
91     &print_message;
92     return 2;
93   } else {
94     $query = qq|ALTER TABLE tax ALTER COLUMN chart_categories SET NOT NULL|;
95     $self->db_query($query);
96     return 1;
97   }
98 } # end run
99
100 sub print_message {
101   print $::form->parse_html_template("dbupgrade/steuerfilterung");
102 }
103
104 1;