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;
 
   9 use parent qw(SL::DBUpgrade2::Base);
 
  14     # extract all buchungsgruppen data
 
  15     my $buchungsgruppen_query = <<SQL;
 
  16       SELECT * from buchungsgruppen;
 
  19     my $sth = $self->dbh->prepare($buchungsgruppen_query);
 
  20     $sth->execute || $::form->dberror($buchungsgruppen_query);
 
  22     $::form->{buchungsgruppen} = [];
 
  23     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  24       push @{ $::form->{buchungsgruppen} }, $ref;
 
  28     # extract all tax_zone data
 
  29     my $taxzone_query = <<SQL;
 
  30       SELECT * from tax_zones;
 
  33     $sth = $self->dbh->prepare($taxzone_query);
 
  34     $sth->execute || $::form->dberror($taxzone_query);
 
  36     $::form->{taxzones} = [];
 
  37     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  38       push @{ $::form->{taxzones} }, $ref;
 
  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);
 
  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);
 
  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;
 
  73   $sth = $self->dbh->prepare($clean_buchungsgruppen_query);
 
  74   $sth->execute || $::form->dberror($clean_buchungsgruppen_query);