]> wagnertech.de Git - mfinanz.git/blob - sql/Pg-upgrade2/convert_taxzone.pl
Änderung von Upgrade-Script change_taxzone_id_0
[mfinanz.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;
43     foreach my $taxzone (  @{$::form->{taxzones}} ) {
44         foreach my $buchungsgruppe (  @{$::form->{buchungsgruppen}} ) {
45             my $id = $taxzone->{id};
46             my $income_accno_id = $buchungsgruppe->{"income_accno_id_$id"};
47             my $expense_accno_id = $buchungsgruppe->{"expense_accno_id_$id"};
48             # TODO: check if the variables have a value
49             $taxzone_charts_update_query .= "INSERT INTO taxzone_charts (taxzone_id, buchungsgruppen_id, income_accno_id, expense_accno_id) VALUES ('$taxzone->{id}', '$buchungsgruppe->{id}', $income_accno_id, $expense_accno_id);\n";
50         };
51     };
52     $self->db_query($taxzone_charts_update_query);
53
54     my $clean_buchungsgruppen_query = <<SQL;
55 alter table buchungsgruppen drop column income_accno_id_0;
56 alter table buchungsgruppen drop column income_accno_id_1;
57 alter table buchungsgruppen drop column income_accno_id_2;
58 alter table buchungsgruppen drop column income_accno_id_3;
59 alter table buchungsgruppen drop column expense_accno_id_0;
60 alter table buchungsgruppen drop column expense_accno_id_1;
61 alter table buchungsgruppen drop column expense_accno_id_2;
62 alter table buchungsgruppen drop column expense_accno_id_3;
63 SQL
64   $sth = $self->dbh->prepare($clean_buchungsgruppen_query);
65   $sth->execute || $::form->dberror($clean_buchungsgruppen_query);
66   return 1;
67 } # end run
68
69 1;