1 # @tag: tax_constraints
2 # @description: Setzt Fremdschlüssel und andere constraints auf die Tabellen tax und taxkeys
3 # @depends: release_3_0_0 charts_without_taxkey
4 package SL::DBUpgrade2::tax_constraints;
9 use parent qw(SL::DBUpgrade2::Base);
14 #CHECK CONSISTANCY OF tax
15 #update tax.rate and tax.taxdescription in order to set later NOT NULL constraints
17 UPDATE tax SET rate=0 WHERE rate IS NULL;
18 UPDATE tax SET taxdescription='-' WHERE COALESCE(taxdescription, '') = '';
21 $self->db_query($query);
23 #check automatic tax accounts
25 SELECT count(*) FROM tax WHERE chart_id NOT IN (SELECT id FROM chart);
28 my ($invalid_tax_account) = $self->dbh->selectrow_array($query);
30 if ($invalid_tax_account > 0){
31 #list all invalid tax accounts
36 round(rate * 100, 2) AS rate
37 FROM tax WHERE chart_id NOT IN (SELECT id FROM chart);
40 my $sth = $self->dbh->prepare($query);
41 $sth->execute || $::form->dberror($query);
44 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
45 push @{ $::form->{TAX} }, $ref;
49 $::form->{invalid_tax_account} = 1;
50 print_error_message();
54 #check entry tax.taxkey of NOT NULL
56 SELECT count(*) FROM tax WHERE taxkey IS NULL;
59 my ($taxkey_is_null) = $self->dbh->selectrow_array($query);
61 if ($taxkey_is_null > 0){
62 #list all invalid tax accounts
66 round(rate * 100, 2) AS rate,
67 (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber,
68 (SELECT description FROM chart WHERE id = chart_id) AS account_description
73 my $sth = $self->dbh->prepare($query);
74 $sth->execute || $::form->dberror($query);
77 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
78 push @{ $::form->{TAX} }, $ref;
82 $::form->{taxkey_is_null} = 1;
83 print_error_message();
88 #CHECK CONSISTANCY OF taxkeys
89 #delete invalide entries in taxkeys
92 WHERE chart_id IS NULL
93 OR chart_id NOT IN (SELECT id FROM chart)
97 $self->db_query($query);
99 #There are 3 cases for taxkeys.tax_id and taxkeys.taxkey_id
100 #taxkeys.taxkey_id is NULL and taxkeys.tax_id is not NULL:
102 #Update taxkeys.taxkey_id with tax.taxkey
105 SET taxkey_id = (SELECT t.taxkey
108 WHERE taxkey_id IS NULL
109 AND tax_id IS NOT NULL;
112 $self->db_query($query);
114 #taxkeys.taxkey_id and taxkeys.tax_id are NULL:
116 #Set taxkey 0 in this case:
119 SET taxkey_id = 0, tax_id = (SELECT id FROM tax WHERE taxkey=0)
120 WHERE taxkey_id IS NULL
124 $self->db_query($query);
126 #Last case where taxkeys.taxkey_id is not null and taxkeys.tax_id is null
128 #If such entries exist we update with an entry in tax where tax.rate=0
129 #and tax.taxkey corresponds to taxkeys.taxkey_id.
130 #If no entry in tax with rate 0 and taxkey taxkeys.taxkey_id exists
133 SELECT DISTINCT taxkey_id
135 WHERE taxkey_id IS NOT NULL
139 my $sth = $self->dbh->prepare($query);
140 $sth->execute || $::form->dberror($query);
142 $::form->{TAXID} = [];
144 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
145 push @{ $::form->{TAXID} }, $ref;
153 for my $i (0 .. $rowcount-1){
155 SELECT id FROM tax WHERE rate = 0 and taxkey=| . $::form->{TAXID}[$i]->{taxkey_id} . qq| LIMIT 1
157 ($tax_id) = $self->dbh->selectrow_array($query);
160 INSERT INTO tax (rate, taxdescription, taxkey) VALUES (0, '| . $::locale->text('0% tax with taxkey') . $::form->{TAXID}[$i]->{taxkey_id} . $::locale->text('. Automatically generated.') .
161 qq|', | . $::form->{TAXID}[$i]->{taxkey_id} . qq|);
163 $self->db_query($insertquery);
164 ($tax_id) = $self->dbh->selectrow_array($query);
165 $tax_id || $::form->dberror($query);
168 UPDATE taxkeys SET tax_id= | . $tax_id . qq| WHERE taxkey_id = | . $::form->{TAXID}[$i]->{taxkey_id} . qq| AND tax_id IS NULL
170 $self->db_query($updatequery);
173 #The triple taxkey_id, chart_id, startdate in taxkeys has to be unique
174 #Select these entries:
176 SELECT DISTINCT tk1.chart_id AS chart_id, tk1.startdate AS startdate
178 WHERE (SELECT count(*)
180 WHERE tk2.chart_id = tk1.chart_id
181 AND tk2.startdate = tk1.startdate) > 1;
184 $sth = $self->dbh->prepare($query);
185 $sth->execute || $::form->dberror($query);
187 $::form->{TAXKEYS} = [];
189 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
190 push @{ $::form->{TAXKEYS} }, $ref;
195 for my $i (0 .. $rowcount-1){
197 DELETE FROM taxkeys tk1
198 WHERE (tk1.chart_id = ?)
199 AND (tk1.startdate = ?)
208 $self->db_query($query, bind => [ ($::form->{TAXKEYS}[$i]->{chart_id}, $::form->{TAXKEYS}[$i]->{startdate}) x 2 ]);
211 #END CHECK OF taxkeys
214 #Now the database is consistent, so we can add constraints:
215 #Crate NOT NULL constraint for tax.rate with default value 0
217 ALTER TABLE tax ALTER COLUMN rate SET NOT NULL;
218 ALTER TABLE tax ALTER COLUMN rate SET DEFAULT 0;
221 $self->db_query($query);
223 #Create NOT NULL constraint for tax.description
225 ALTER TABLE tax ALTER COLUMN taxdescription SET NOT NULL;
228 $self->db_query($query);
230 #Create foreign key for tax.chart_id to chart.id
232 ALTER TABLE tax ADD FOREIGN KEY (chart_id) REFERENCES chart(id);
235 $self->db_query($query);
237 #Create NOT NULL constraint for tax.taxkey
239 ALTER TABLE tax ALTER COLUMN taxkey SET NOT NULL;
242 $self->db_query($query);
244 #Create NOT NULL constraint for taxkey.chart_id and foreign key for taxkey.chart_id
246 ALTER TABLE taxkeys ALTER COLUMN chart_id SET NOT NULL;
247 ALTER TABLE taxkeys ADD FOREIGN KEY (chart_id) REFERENCES chart(id);
250 $self->db_query($query);
252 #Create NOT NULL constraint for taxkey.startdate
254 ALTER TABLE taxkeys ALTER COLUMN startdate SET NOT NULL;
257 $self->db_query($query);
259 #Create NOT NULL constraint for taxkey.taxkey_id
261 ALTER TABLE taxkeys ALTER COLUMN taxkey_id SET NOT NULL;
264 $self->db_query($query);
266 #Create NOT NULL constraint for taxkey.tax_id
268 ALTER TABLE taxkeys ALTER COLUMN tax_id SET NOT NULL;
271 $self->db_query($query);
273 #The triple chart_id, taxkey_id, startdate should be unique:
275 CREATE UNIQUE INDEX taxkeys_chartid_startdate ON taxkeys(chart_id, startdate);
278 $self->db_query($query);
279 #ALL CONSTRAINTS WERE ADDED
285 sub print_error_message {
286 print $::form->parse_html_template("dbupgrade/tax_constraints");