X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=inline;f=sql%2FPg-upgrade2%2Ftax_constraints.pl;h=cc2ccd2e46359f2ac9254da2b6ae66fc7330b1dd;hb=11e509315cee0449974caa6d681fa26e730bbeca;hp=7567930a21a639bfb3381e9cfcb521e86b20e434;hpb=18931692ebe56ef5a2afa471182d3845914979c2;p=kivitendo-erp.git
diff --git a/sql/Pg-upgrade2/tax_constraints.pl b/sql/Pg-upgrade2/tax_constraints.pl
index 7567930a2..cc2ccd2e4 100644
--- a/sql/Pg-upgrade2/tax_constraints.pl
+++ b/sql/Pg-upgrade2/tax_constraints.pl
@@ -1,304 +1,289 @@
# @tag: tax_constraints
# @description: Setzt Fremdschlüssel und andere constraints auf die Tabellen tax und taxkeys
-# @depends: release_3_0_0
-# @charset: utf-8
+# @depends: release_3_0_0 charts_without_taxkey
+package SL::DBUpgrade2::tax_constraints;
-use utf8;
use strict;
-use SL::Locale;
-
-die("This script cannot be run from the command line.") unless ($main::form);
-
-sub mydberror {
- my ($msg) = @_;
- die($dbup_locale->text("Database update error:") . "
$msg
" . $DBI::errstr);
-}
+use utf8;
-sub do_query {
- my ($query, $may_fail) = @_;
+use parent qw(SL::DBUpgrade2::Base);
- if (!$dbh->do($query)) {
- mydberror($query) unless ($may_fail);
- $dbh->rollback();
- $dbh->begin_work();
- }
-}
+sub run {
+ my ($self) = @_;
-sub do_update {
-#CHECK CONSISTANCY OF tax
+ #CHECK CONSISTANCY OF tax
#update tax.rate and tax.taxdescription in order to set later NOT NULL constraints
-my $query= <db_query($query);
#check automatic tax accounts
$query= <selectrow_array($query);
-
+
+ my ($invalid_tax_account) = $self->dbh->selectrow_array($query);
+
if ($invalid_tax_account > 0){
#list all invalid tax accounts
$query = <prepare($query);
- $sth->execute || $main::form->dberror($query);
+ my $sth = $self->dbh->prepare($query);
+ $sth->execute || $::form->dberror($query);
- $main::form->{TAX} = [];
+ $::form->{TAX} = [];
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- push @{ $main::form->{TAX} }, $ref;
+ push @{ $::form->{TAX} }, $ref;
}
$sth->finish;
- $main::form->{invalid_tax_account} = 1;
+ $::form->{invalid_tax_account} = 1;
print_error_message();
return 0;
- }
+ }
#check entry tax.taxkey of NOT NULL
$query= <selectrow_array($query);
-
+
+ my ($taxkey_is_null) = $self->dbh->selectrow_array($query);
+
if ($taxkey_is_null > 0){
#list all invalid tax accounts
$query = <prepare($query);
- $sth->execute || $main::form->dberror($query);
+ my $sth = $self->dbh->prepare($query);
+ $sth->execute || $::form->dberror($query);
- $main::form->{TAX} = [];
+ $::form->{TAX} = [];
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- push @{ $main::form->{TAX} }, $ref;
+ push @{ $::form->{TAX} }, $ref;
}
$sth->finish;
- $main::form->{taxkey_is_null} = 1;
+ $::form->{taxkey_is_null} = 1;
print_error_message();
return 0;
- }
-#END CHECK OF tax
+ }
+ #END CHECK OF tax
-#CHECK CONSISTANCY OF taxkeys
+ #CHECK CONSISTANCY OF taxkeys
#delete invalide entries in taxkeys
$query= <db_query($query);
-#There are 3 cases for taxkeys.tax_id and taxkeys.taxkey_id
-#taxkeys.taxkey_id is NULL and taxkeys.tax_id is not NULL:
+ #There are 3 cases for taxkeys.tax_id and taxkeys.taxkey_id
+ #taxkeys.taxkey_id is NULL and taxkeys.tax_id is not NULL:
#Update taxkeys.taxkey_id with tax.taxkey
$query= <db_query($query);
+
+ #taxkeys.taxkey_id and taxkeys.tax_id are NULL:
+
#Set taxkey 0 in this case:
$query= <db_query($query);
+
+ #Last case where taxkeys.taxkey_id is not null and taxkeys.tax_id is null
#If such entries exist we update with an entry in tax where tax.rate=0
#and tax.taxkey corresponds to taxkeys.taxkey_id.
- #If no entry in tax with rate 0 and taxkey taxkeys.taxkey_id exists
+ #If no entry in tax with rate 0 and taxkey taxkeys.taxkey_id exists
#we create one.
$query= <prepare($query);
- $sth->execute || $main::form->dberror($query);
+ my $sth = $self->dbh->prepare($query);
+ $sth->execute || $::form->dberror($query);
- $main::form->{TAXID} = [];
+ $::form->{TAXID} = [];
my $rowcount = 0;
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- push @{ $main::form->{TAXID} }, $ref;
+ push @{ $::form->{TAXID} }, $ref;
$rowcount++;
}
$sth->finish;
-
+
my $insertquery;
my $updatequery;
my $tax_id;
for my $i (0 .. $rowcount-1){
$query= qq|
-SELECT id FROM tax WHERE rate = 0 and taxkey=| . $main::form->{TAXID}[$i]->{taxkey_id} . qq| LIMIT 1
-|;
- ($tax_id) = $dbh->selectrow_array($query);
+ SELECT id FROM tax WHERE rate = 0 and taxkey=| . $::form->{TAXID}[$i]->{taxkey_id} . qq| LIMIT 1
+|;
+ ($tax_id) = $self->dbh->selectrow_array($query);
if ( not $tax_id ){
$insertquery=qq|
-INSERT INTO tax (rate, taxdescription, taxkey) VALUES (0, '| . $::locale->text('0% tax with taxkey') . $main::form->{TAXID}[$i]->{taxkey_id} . $::locale->text('. Automatically generated.') .
-qq|', | . $main::form->{TAXID}[$i]->{taxkey_id} . qq|);
+ INSERT INTO tax (rate, taxdescription, taxkey) VALUES (0, '| . $::locale->text('0% tax with taxkey') . $::form->{TAXID}[$i]->{taxkey_id} . $::locale->text('. Automatically generated.') .
+ qq|', | . $::form->{TAXID}[$i]->{taxkey_id} . qq|);
|;
- do_query($insertquery);
- ($tax_id) = $dbh->selectrow_array($query);
- $tax_id || $main::form->dberror($query);
+ $self->db_query($insertquery);
+ ($tax_id) = $self->dbh->selectrow_array($query);
+ $tax_id || $::form->dberror($query);
}
$updatequery = qq|
-UPDATE taxkeys SET tax_id= | . $tax_id . qq| WHERE taxkey_id = | . $main::form->{TAXID}[$i]->{taxkey_id} . qq| AND tax_id IS NULL
+ UPDATE taxkeys SET tax_id= | . $tax_id . qq| WHERE taxkey_id = | . $::form->{TAXID}[$i]->{taxkey_id} . qq| AND tax_id IS NULL
|;
- do_query($updatequery);
+ $self->db_query($updatequery);
}
-#The triple taxkey_id, chart_id, startdate in taxkeys has to be unique
+ #The triple taxkey_id, chart_id, startdate in taxkeys has to be unique
#Select these entries:
$query= < 1;
+ SELECT DISTINCT tk1.chart_id AS chart_id, tk1.startdate AS startdate
+ FROM taxkeys tk1
+ WHERE (SELECT count(*)
+ FROM taxkeys tk2
+ WHERE tk2.chart_id = tk1.chart_id
+ AND tk2.startdate = tk1.startdate) > 1;
SQL
- $sth = $dbh->prepare($query);
- $sth->execute || $main::form->dberror($query);
+ $sth = $self->dbh->prepare($query);
+ $sth->execute || $::form->dberror($query);
- $main::form->{TAXKEYS} = [];
+ $::form->{TAXKEYS} = [];
$rowcount = 0;
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- push @{ $main::form->{TAXKEYS} }, $ref;
+ push @{ $::form->{TAXKEYS} }, $ref;
$rowcount++;
}
$sth->finish;
for my $i (0 .. $rowcount-1){
- $query= qq|
-DELETE FROM taxkeys tk1
-WHERE (SELECT count(*)
- FROM taxkeys tk2
- WHERE tk2.chart_id = tk1.chart_id
- AND tk2.startdate = tk1.startdate) > 1
-AND NOT tk1.id = (SELECT id
- FROM taxkeys
- WHERE chart_id = | . $main::form->{TAXKEYS}[$i]->{chart_id} . qq|
- AND startdate = '| . $main::form->{TAXKEYS}[$i]->{startdate} . qq|'
- LIMIT 1)
-|;
+ $query= < (
+ SELECT id
+ FROM taxkeys
+ WHERE (chart_id = ?)
+ AND (startdate = ?)
+ LIMIT 1))
+SQL
- do_query($query);
+ $self->db_query($query, bind => [ ($::form->{TAXKEYS}[$i]->{chart_id}, $::form->{TAXKEYS}[$i]->{startdate}) x 2 ]);
}
-#END CHECK OF taxkeys
+ #END CHECK OF taxkeys
-#ADD CONSTRAINTS:
-#Now the database is consistent, so we can add constraints:
+ #ADD CONSTRAINTS:
+ #Now the database is consistent, so we can add constraints:
#Crate NOT NULL constraint for tax.rate with default value 0
$query= <db_query($query);
#Create NOT NULL constraint for tax.description
$query= <db_query($query);
#Create foreign key for tax.chart_id to chart.id
$query= <db_query($query);
+
#Create NOT NULL constraint for tax.taxkey
$query= <db_query($query);
#Create NOT NULL constraint for taxkey.chart_id and foreign key for taxkey.chart_id
$query= <db_query($query);
#Create NOT NULL constraint for taxkey.startdate
$query= <db_query($query);
#Create NOT NULL constraint for taxkey.taxkey_id
$query= <db_query($query);
#Create NOT NULL constraint for taxkey.tax_id
$query= <db_query($query);
#The triple chart_id, taxkey_id, startdate should be unique:
$query= <db_query($query);
+ #ALL CONSTRAINTS WERE ADDED
return 1;
-}; # end do_update
+} # end run
sub print_error_message {
- print $main::form->parse_html_template("dbupgrade/tax_constraints");
+ print $::form->parse_html_template("dbupgrade/tax_constraints");
}
-return do_update();
+1;