Merge branch 'b-3.6.1' into mebil
[kivitendo-erp.git] / sql / Pg-upgrade2 / remove_double_tax_entries_skr04.pl
1 # @tag: remove_double_tax_entries_skr04
2 # @description: doppelte Steuer-Einträge und alte 16% Konten für SKR04 entfernen, wenn unbebucht
3 # @depends: release_3_5_5
4 package SL::DBUpgrade2::remove_double_tax_entries_skr04;
5
6 use strict;
7 use utf8;
8
9 use parent qw(SL::DBUpgrade2::Base);
10
11 use SL::DBUtils;
12
13 sub run {
14   my ($self) = @_;
15
16   if (!$self->check_coa('Germany-DATEV-SKR04EU')) {
17     return 1;
18   }
19
20   my $query = <<SQL;
21     SELECT id FROM tax WHERE chart_id = (SELECT id FROM chart WHERE accno LIKE ?) AND taxkey = ? AND rate = ? ORDER BY id;
22 SQL
23
24   my $query2 = <<SQL;
25     DELETE FROM taxkeys WHERE tax_id = ?;
26 SQL
27
28   my $query3 = <<SQL;
29     DELETE FROM tax WHERE id = ?;
30 SQL
31
32   my @taxes_to_test = (
33     {accno => '3806', taxkey => 3, rate => 0.19},
34     {accno => '1406', taxkey => 9, rate => 0.19},
35     {accno => '3805', taxkey => 5, rate => 0.16},
36     {accno => '1405', taxkey => 7, rate => 0.16},
37
38   );
39
40   foreach my $tax_to_test (@taxes_to_test) {
41     my @entries = selectall_hashref_query($::form, $self->dbh, $query, ($tax_to_test->{accno}, $tax_to_test->{taxkey}, $tax_to_test->{rate}));
42
43     if (scalar @entries > 1) {
44       foreach my $tax (@entries) {
45         my ($num_acc_trans_entries) = $self->dbh->selectrow_array("SELECT COUNT(*) FROM acc_trans WHERE tax_id = ?", undef, $tax->{id});
46         next if $num_acc_trans_entries > 0;
47
48         $self->db_query($query2, bind => [ $tax->{id} ]);
49         $self->db_query($query3, bind => [ $tax->{id} ]);
50
51         last; # delete only one tax
52       }
53     }
54   }
55
56   return 1;
57 }
58
59 1;