epic-s6ts
[kivitendo-erp.git] / sql / Pg-upgrade2 / convert_taxzone.pl
1 # @tag: convert_taxzone
2 # @description: Setzt Fremdschlüssel und andere constraints auf die Tabellen tax und taxkeys
3 # @depends: taxzone_charts
4 package SL::DBUpgrade2::convert_taxzone;
5
6 use strict;
7 use utf8;
8
9 use parent qw(SL::DBUpgrade2::Base);
10
11 sub run {
12   my ($self) = @_;
13
14     # extract all buchungsgruppen data
15     my $buchungsgruppen_query = <<SQL;
16       SELECT * from buchungsgruppen;
17 SQL
18
19     my $sth = $self->dbh->prepare($buchungsgruppen_query);
20     $sth->execute || $::form->dberror($buchungsgruppen_query);
21
22     $::form->{buchungsgruppen} = [];
23     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
24       push @{ $::form->{buchungsgruppen} }, $ref;
25     }
26     $sth->finish;
27
28     # extract all tax_zone data
29     my $taxzone_query = <<SQL;
30       SELECT * from tax_zones;
31 SQL
32
33     $sth = $self->dbh->prepare($taxzone_query);
34     $sth->execute || $::form->dberror($taxzone_query);
35
36     $::form->{taxzones} = [];
37     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
38       push @{ $::form->{taxzones} }, $ref;
39     }
40     $sth->finish;
41
42     my $taxzone_charts_update_query = "INSERT INTO taxzone_charts (taxzone_id, buchungsgruppen_id, income_accno_id, expense_accno_id) VALUES (?, ?, ?, ?)";
43     $sth = $self->dbh->prepare($taxzone_charts_update_query);
44
45     # convert Buchungsgruppen to taxzone_charts if any exist
46     # the default swiss COA doesn't have any, for example
47     if ( scalar @{ $::form->{buchungsgruppen} } > 0 ) {
48         foreach my $taxzone (  @{$::form->{taxzones}} ) {
49             foreach my $buchungsgruppe (  @{$::form->{buchungsgruppen}} ) {
50                 my $id = $taxzone->{id};
51                 my $income_accno_id = $buchungsgruppe->{"income_accno_id_$id"};
52                 my $expense_accno_id = $buchungsgruppe->{"expense_accno_id_$id"};
53                 my @values           = ($taxzone->{id}, $buchungsgruppe->{id}, $income_accno_id, $expense_accno_id);
54                 $sth->execute(@values) && next;
55                 $taxzone_charts_update_query =~ s{\?}{shift(@values)}eg;
56                 $::form->dberror($taxzone_charts_update_query);
57             };
58         };
59     };
60
61     $sth->finish;
62
63     my $clean_buchungsgruppen_query = <<SQL;
64 alter table buchungsgruppen drop column income_accno_id_0;
65 alter table buchungsgruppen drop column income_accno_id_1;
66 alter table buchungsgruppen drop column income_accno_id_2;
67 alter table buchungsgruppen drop column income_accno_id_3;
68 alter table buchungsgruppen drop column expense_accno_id_0;
69 alter table buchungsgruppen drop column expense_accno_id_1;
70 alter table buchungsgruppen drop column expense_accno_id_2;
71 alter table buchungsgruppen drop column expense_accno_id_3;
72 SQL
73   $sth = $self->dbh->prepare($clean_buchungsgruppen_query);
74   $sth->execute || $::form->dberror($clean_buchungsgruppen_query);
75   return 1;
76 } # end run
77
78 1;