Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 18931692

Von Niclas Zimmermann vor fast 12 Jahren hinzugefügt

  • ID 18931692ebe56ef5a2afa471182d3845914979c2
  • Vorgänger 233c00b1
  • Nachfolger 32ddb71c

Constraints für tax und taxkeys

Dieser Commit fügt für die Tabellen tax und taxkeys folgende
Constraints hinzu:

Tabelle tax:

1.chart_id hat jetzt einen Fremdschlüssel auf chart.id.
2.rate darf nicht mehr NULL sein und hat DEFAULT-Wert 0.
3.taxkey darf nicht mehr NULL sein.
4.taxdescription darf nicht mehr NULL sein.

Tabelle taxkeys:

1.chart_id darf nicht mehr NULL sein, hat jetzt einen Fremdschlüssel
auf chart.id.
2.tax_id darf nicht mehr NULL sein.
3.taxkey_id darf nicht mehr NULL sein.
4.startdate darf nicht mehr NULL sein.
5.chart_id und startdate müssen eindeutig sein.

Unterschiede anzeigen:

locale/de/all
20 20
  '*) Since version 2.7 these parameters ares set in the client database and not in the configuration file, details in chapter:' => '*) Seit 2.7 werden Gewinnermittlungsart, Versteuerungsart und Warenbuchungsmethode in der Mandanten-DB gesteuert und nicht mehr in der Konfigurationsdatei, Umstellungs-Details:',
21 21
  '*/'                          => '*/',
22 22
  '---please select---'         => '---bitte auswählen---',
23
  '. Automatically generated.'  => '. Automatisch erzeugt.',
23 24
  '...after loggin in'          => '...nach dem Anmelden',
24 25
  '...done'                     => '...fertig',
25 26
  '...on the TODO list'         => '...auf der Aufgabenliste',
27
  '0% tax with taxkey'          => '0% Steuer mit Steuerschlüssel ',
26 28
  '1. Quarter'                  => '1. Quartal',
27 29
  '2. Quarter'                  => '2. Quartal',
28 30
  '3. Quarter'                  => '3. Quartal',
......
1008 1010
  'Income accno'                => 'Erlöskonto',
1009 1011
  'Incoming Payments'           => 'Zahlungseingänge',
1010 1012
  'Incoming invoice number'     => 'Eingangsrechnungsnummer',
1013
  'Inconsistency in database'   => 'Unstimmigkeiten in der Datenbank',
1011 1014
  'Incorrect Password!'         => 'Ungültiges Passwort!',
1012 1015
  'Incorrect password!'         => 'Ungültiges Passwort!',
1013 1016
  'Incorrect username or password!' => 'Ungültiger Benutzername oder falsches Passwort!',
......
1416 1419
  'Please Check the bank information for each vendor:' => 'Bitte überprüfen Sie die Kontoinformationen der Lieferanten:',
1417 1420
  'Please ask your administrator to create warehouses and bins.' => 'Bitten Sie Ihren Administrator, dass er Lager und Lagerplätze anlegt.',
1418 1421
  'Please contact your administrator.' => 'Bitte wenden Sie sich an Ihren Administrator.',
1422
  'Please define a taxkey for the following taxes and run the update again:' => 'Bitte definieren Sie einen Steuerschlüssel für die folgenden Steuern und starten Sie dann das Update erneut:',
1419 1423
  'Please enter a profile name.' => 'Bitte geben Sie einen Profilnamen an.',
1420 1424
  'Please enter the login for the new user.' => 'Bitte geben Sie das Login für den neuen Benutzer ein.',
1421 1425
  'Please enter the name of the database that will be used as the template for the new database:' => 'Bitte geben Sie den Namen der Datenbank an, die als Vorlage für die neue Datenbank benutzt wird:',
......
1439 1443
  'Please select the destination bank account for the collections:' => 'Bitte wählen Sie das Bankkonto als Ziel für die Einzüge aus:',
1440 1444
  'Please select the source bank account for the transfers:' => 'Bitte wählen Sie das Bankkonto als Quelle für die Überweisungen aus:',
1441 1445
  'Please seletct the dataset you want to delete:' => 'Bitte wählen Sie die zu löschende Datenbank aus:',
1446
  'Please set another taxnumber for the following taxes and run the update again:' => 'Bitte wählen Sie ein anderes Steuerautomatik-Konto für die folgenden Steuern aus uns starten Sie dann das Update erneut.',
1442 1447
  'Please specify a description for the warehouse designated for these goods.' => 'Bitte geben Sie den Namen des Ziellagers für die übernommenen Daten ein.',
1443 1448
  'Plural'                      => 'Plural',
1444 1449
  'Port'                        => 'Port',
......
2051 2056
  'There are #1 more open invoices from this vendor with other currencies.' => 'Es gibt #1 weitere offene Rechnungen von diesem Lieferanten, die in anderen Währungen ausgestellt wurden.',
2052 2057
  'There are #1 unfinished follow-ups of which #2 are due.' => 'Es gibt #1 Wiedervorlage(n), von denen #2 fällig ist/sind.',
2053 2058
  'There are bookings to the account 3803 after 01.01.2007. If you didn\'t change this account manually to 19% the bookings are probably incorrect.' => 'Das Konto 3803 wurde nach dem 01.01.2007 bebucht. Falls Sie dieses Konto nicht manuell auf 19% gestellt haben sind die Buchungen wahrscheinlich mit falscher Umsatzsteuer gebucht worden.',
2059
  'There are entries in tax where taxkey is NULL.' => 'In der Datenbank sind Steuern ohne Steuerschlüssel vorhanden (in der Tabelle tax Spalte taxkey).',
2054 2060
  'There are four tax zones.'   => 'Es gibt vier Steuerzonen.',
2061
  'There are invalid taxnumbers in use.' => 'Es werden ungültige Steuerautomatik-Konten benutzt.',
2055 2062
  'There are no entries in the background job history.' => 'Es gibt keine Einträge im Hintergrund-Job-Verlauf.',
2056 2063
  'There are no items in stock.' => 'Dieser Artikel ist nicht eingelagert.',
2057 2064
  'There are no items on your TODO list at the moment.' => 'Ihre Aufgabenliste enthält momentan keine Einträge.',
......
2059 2066
  'There are still transfers not matching the qty of the delivery order. Stock operations can not be changed later. Do you really want to proceed?' => 'Einige der Lagerbewegungen sind nicht vollständig und Lagerbewegungen können nachträglich nicht mehr verändert werden. Wollen Sie wirklich fortfahren?',
2060 2067
  'There are usually three ways to install Perl modules.' => 'Es gibt normalerweise drei Arten, ein Perlmodul zu installieren.',
2061 2068
  'There is already a taxkey 0 with tax rate not 0.' => 'Es existiert bereits ein Steuerschlüssel mit Steuersatz ungleich 0%.',
2069
  'There is an inconsistancy in your database.' => 'In Ihrer Datenbank sind Unstimmigkeiten vorhanden.',
2062 2070
  'There is at least one sales or purchase invoice for which kivitendo recorded an inventory transaction with taxkeys even though no tax was recorded.' => 'Es gibt mindestens eine Verkaufs- oder Einkaufsrechnung, für die kivitendo eine Warenbestandsbuchung ohne dazugehörige Steuerbuchung durchgeführt hat.',
2063 2071
  'There is at least one transaction for which the user has chosen a logically wrong taxkey.' => 'Es gibt mindestens eine Buchung, bei der ein logisch nicht passender Steuerschlüssel ausgewählt wurde.',
2064 2072
  'There is not enough available of \'#1\' at warehouse \'#2\', bin \'#3\', #4, #5, for the transfer of #6.' => 'Von \'#1\' ist in Lager \'#2\', Lagerplatz \'#3\', #4, #5, nicht genügend eingelagert, um insgesamt #6 auszulagern.',
locale/en/all
20 20
  '*) Since version 2.7 these parameters ares set in the client database and not in the lx-erp.conf / lx_office.conf file, details in chapter:' => '',
21 21
  '*/'                          => '',
22 22
  '---please select---'         => '',
23
  '. Automatically generated.'  => '',
23 24
  '...after loggin in'          => '',
24 25
  '...done'                     => '',
25 26
  '...on the TODO list'         => '',
27
  '0% tax with taxkey'          => '',
26 28
  '1. Quarter'                  => '',
27 29
  '2. Quarter'                  => '',
28 30
  '3. Quarter'                  => '',
......
1985 1987
  'There are #1 more open invoices from this vendor with other currencies.' => '',
1986 1988
  'There are #1 unfinished follow-ups of which #2 are due.' => '',
1987 1989
  'There are bookings to the account 3803 after 01.01.2007. If you didn\'t change this account manually to 19% the bookings are probably incorrect.' => '',
1990
  'There are entries in tax where taxkey is NULL.' => '',
1988 1991
  'There are four tax zones.'   => '',
1992
  'There are invalid taxnumbers in use.' => '',
1993
  'There are no entries in the background job history.' => '',
1989 1994
  'There are no items in stock.' => '',
1990 1995
  'There are no items on your TODO list at the moment.' => '',
1991 1996
  'There are still entries in the database for which no unit has been assigned.' => '',
1992 1997
  'There are still transfers not matching the qty of the delivery order. Stock operations can not be changed later. Do you really want to proceed?' => '',
1993 1998
  'There are usually three ways to install Perl modules.' => '',
1994 1999
  'There is already a taxkey 0 with tax rate not 0.' => '',
2000
  'There is an inconsistancy in your database.' => '',
1995 2001
  'There is at least one sales or purchase invoice for which kivitendo recorded an inventory transaction with taxkeys even though no tax was recorded.' => '',
1996 2002
  'There is at least one transaction for which the user has chosen a logically wrong taxkey.' => '',
1997 2003
  'There is not enough available of \'#1\' at warehouse \'#2\', bin \'#3\', #4, #5, for the transfer of #6.' => '',
sql/Pg-upgrade2/tax_constraints.pl
1
# @tag: tax_constraints
2
# @description: Setzt Fremdschlüssel und andere constraints auf die Tabellen tax und taxkeys
3
# @depends: release_3_0_0
4
# @charset: utf-8
5

  
6
use utf8;
7
use strict;
8
use SL::Locale;
9

  
10
die("This script cannot be run from the command line.") unless ($main::form);
11

  
12
sub mydberror {
13
  my ($msg) = @_;
14
  die($dbup_locale->text("Database update error:") . "<br>$msg<br>" . $DBI::errstr);
15
}
16

  
17
sub do_query {
18
  my ($query, $may_fail) = @_;
19

  
20
  if (!$dbh->do($query)) {
21
    mydberror($query) unless ($may_fail);
22
    $dbh->rollback();
23
    $dbh->begin_work();
24
  }
25
}
26

  
27
sub do_update {
28
#CHECK CONSISTANCY OF tax
29
  #update tax.rate and tax.taxdescription in order to set later NOT NULL constraints
30
my $query= <<SQL;
31
UPDATE tax SET rate=0 WHERE rate IS NULL;
32
UPDATE tax SET taxdescription='-' WHERE taxdescription IS NULL;
33
SQL
34

  
35
  do_query($query);
36

  
37
  #check automatic tax accounts
38
  $query= <<SQL;
39
SELECT count(*) FROM tax WHERE chart_id NOT IN (SELECT id FROM chart);
40
SQL
41
   
42
  my ($invalid_tax_account) = $dbh->selectrow_array($query);
43
  
44
  if ($invalid_tax_account > 0){
45
    #list all invalid tax accounts
46
    $query = <<SQL;
47
SELECT id,
48
  taxkey,
49
  taxdescription, 
50
  round(rate * 100, 2) AS rate 
51
FROM tax WHERE chart_id NOT IN (SELECT id FROM chart);
52
SQL
53

  
54
    my $sth = $dbh->prepare($query);
55
    $sth->execute || $main::form->dberror($query);
56

  
57
    $main::form->{TAX} = [];
58
    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
59
      push @{ $main::form->{TAX} }, $ref;
60
    }
61
    $sth->finish;
62

  
63
    $main::form->{invalid_tax_account} = 1;
64
    print_error_message();
65
    return 0;
66
  } 
67

  
68
  #check entry tax.taxkey of NOT NULL
69
  $query= <<SQL;
70
SELECT count(*) FROM tax WHERE taxkey IS NULL;
71
SQL
72
   
73
  my ($taxkey_is_null) = $dbh->selectrow_array($query);
74
  
75
  if ($taxkey_is_null > 0){
76
    #list all invalid tax accounts
77
    $query = <<SQL;
78
SELECT id, 
79
  taxdescription, 
80
  round(rate * 100, 2) AS rate, 
81
  (SELECT accno FROM chart WHERE id = chart_id) AS taxnumber, 
82
  (SELECT description FROM chart WHERE id = chart_id) AS account_description
83
FROM tax 
84
WHERE taxkey IS NULL;
85
SQL
86

  
87
    my $sth = $dbh->prepare($query);
88
    $sth->execute || $main::form->dberror($query);
89

  
90
    $main::form->{TAX} = [];
91
    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
92
      push @{ $main::form->{TAX} }, $ref;
93
    }
94
    $sth->finish;
95

  
96
    $main::form->{taxkey_is_null} = 1;
97
    print_error_message();
98
    return 0;
99
  } 
100
#END CHECK OF tax
101

  
102
#CHECK CONSISTANCY OF taxkeys
103
  #delete invalide entries in taxkeys
104
  $query= <<SQL;
105
DELETE FROM taxkeys 
106
WHERE chart_id IS NULL 
107
OR chart_id NOT IN (SELECT id FROM chart) 
108
OR startdate IS NULL;
109
SQL
110

  
111
  do_query($query);
112

  
113
#There are 3 cases for taxkeys.tax_id and taxkeys.taxkey_id
114
#taxkeys.taxkey_id is NULL and taxkeys.tax_id is not NULL:
115

  
116
  #Update taxkeys.taxkey_id with tax.taxkey
117
  $query= <<SQL;
118
UPDATE taxkeys 
119
SET taxkey_id = (SELECT t.taxkey 
120
                        FROM tax t 
121
                        WHERE t.id=tax_id)
122
WHERE taxkey_id IS NULL 
123
AND tax_id IS NOT NULL;
124
SQL
125
  
126
  do_query($query);
127

  
128
#taxkeys.taxkey_id and taxkeys.tax_id are NULL:
129
  
130
  #Set taxkey 0 in this case:
131
  $query= <<SQL;
132
UPDATE taxkeys
133
SET taxkey_id = 0, tax_id = (SELECT id FROM tax WHERE taxkey=0)
134
WHERE taxkey_id IS NULL
135
AND tax_id IS NULL;
136
SQL
137
   
138
  do_query($query);
139

  
140
#Last case where taxkeys.taxkey_id is not null and taxkeys.tax_id is null
141

  
142
  #If such entries exist we update with an entry in tax where tax.rate=0
143
  #and tax.taxkey corresponds to taxkeys.taxkey_id.
144
  #If no entry in tax with rate 0 and taxkey taxkeys.taxkey_id exists 
145
  #we create one.
146
  $query= <<SQL;
147
SELECT DISTINCT taxkey_id
148
FROM taxkeys 
149
WHERE taxkey_id IS NOT NULL 
150
AND tax_id IS NULL;
151
SQL
152

  
153
  my $sth = $dbh->prepare($query);
154
  $sth->execute || $main::form->dberror($query);
155

  
156
  $main::form->{TAXID} = [];
157
  my $rowcount = 0;
158
  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
159
    push @{ $main::form->{TAXID} }, $ref;
160
    $rowcount++;
161
  }
162
  $sth->finish;
163
  
164
  my $insertquery;
165
  my $updatequery;
166
  my $tax_id;
167
  for my $i (0 .. $rowcount-1){
168
    $query= qq|
169
SELECT id FROM tax WHERE rate = 0 and taxkey=| . $main::form->{TAXID}[$i]->{taxkey_id} . qq| LIMIT 1
170
|;  
171
    ($tax_id) = $dbh->selectrow_array($query); 
172
    if ( not $tax_id ){
173
      $insertquery=qq|
174
INSERT INTO tax (rate, taxdescription, taxkey) VALUES (0, '| . $::locale->text('0% tax with taxkey') . $main::form->{TAXID}[$i]->{taxkey_id} .  $::locale->text('. Automatically generated.') . 
175
qq|', | . $main::form->{TAXID}[$i]->{taxkey_id} . qq|);
176
|;
177
      do_query($insertquery);
178
      ($tax_id) = $dbh->selectrow_array($query);
179
      $tax_id || $main::form->dberror($query); 
180
    }
181
    $updatequery = qq|
182
UPDATE taxkeys SET tax_id= | . $tax_id . qq| WHERE taxkey_id = | . $main::form->{TAXID}[$i]->{taxkey_id} . qq| AND tax_id IS NULL
183
|;
184
    do_query($updatequery);
185
  }
186

  
187
#The triple taxkey_id, chart_id, startdate in taxkeys has to be unique
188
  #Select these entries:
189
  $query= <<SQL;
190
SELECT DISTINCT tk1.chart_id AS chart_id, tk1.startdate AS startdate
191
FROM taxkeys tk1 
192
WHERE (SELECT count(*) 
193
       FROM taxkeys tk2 
194
       WHERE tk2.chart_id  = tk1.chart_id 
195
       AND   tk2.startdate = tk1.startdate) > 1;
196
SQL
197

  
198
  $sth = $dbh->prepare($query);
199
  $sth->execute || $main::form->dberror($query);
200

  
201
  $main::form->{TAXKEYS} = [];
202
  $rowcount = 0;
203
  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
204
    push @{ $main::form->{TAXKEYS} }, $ref;
205
    $rowcount++;
206
  }
207
  $sth->finish;
208

  
209
  for my $i (0 .. $rowcount-1){
210
    $query= qq| 
211
DELETE FROM taxkeys tk1 
212
WHERE (SELECT count(*) 
213
       FROM taxkeys tk2 
214
       WHERE tk2.chart_id  = tk1.chart_id 
215
       AND   tk2.startdate = tk1.startdate) > 1 
216
AND NOT tk1.id = (SELECT id 
217
                  FROM taxkeys 
218
                  WHERE chart_id  = | . $main::form->{TAXKEYS}[$i]->{chart_id} . qq|
219
                  AND   startdate = '| . $main::form->{TAXKEYS}[$i]->{startdate} . qq|'
220
                  LIMIT 1)
221
|;
222

  
223
    do_query($query);
224
  }
225

  
226
#END CHECK OF taxkeys
227

  
228
#ADD CONSTRAINTS:
229
#Now the database is consistent, so we can add constraints:
230
  #Crate NOT NULL constraint for tax.rate with default value 0
231
  $query= <<SQL;
232
ALTER TABLE tax ALTER COLUMN rate SET NOT NULL;
233
ALTER TABLE tax ALTER COLUMN rate SET DEFAULT 0;
234
SQL
235

  
236
  do_query($query);
237

  
238
  #Create NOT NULL constraint for tax.description
239
  $query= <<SQL;
240
ALTER TABLE tax ALTER COLUMN taxdescription SET NOT NULL;
241
SQL
242

  
243
  do_query($query);
244

  
245
  #Create foreign key for tax.chart_id to chart.id
246
  $query= <<SQL;
247
ALTER TABLE tax ADD FOREIGN KEY (chart_id) REFERENCES chart(id);
248
SQL
249
  
250
  do_query($query);
251
  
252
  #Create NOT NULL constraint for tax.taxkey
253
  $query= <<SQL;
254
ALTER TABLE tax ALTER COLUMN taxkey SET NOT NULL;
255
SQL
256

  
257
  do_query($query);
258

  
259
  #Create NOT NULL constraint for taxkey.chart_id and foreign key for taxkey.chart_id
260
  $query= <<SQL;
261
ALTER TABLE taxkeys ALTER COLUMN chart_id SET NOT NULL;
262
ALTER TABLE taxkeys ADD FOREIGN KEY (chart_id) REFERENCES chart(id);
263
SQL
264
  
265
  do_query($query);
266

  
267
  #Create NOT NULL constraint for taxkey.startdate
268
  $query= <<SQL;
269
ALTER TABLE taxkeys ALTER COLUMN startdate SET NOT NULL;
270
SQL
271
  
272
  do_query($query);
273

  
274
  #Create NOT NULL constraint for taxkey.taxkey_id
275
  $query= <<SQL;
276
ALTER TABLE taxkeys ALTER COLUMN taxkey_id SET NOT NULL;
277
SQL
278
   
279
  do_query($query);
280

  
281
  #Create NOT NULL constraint for taxkey.tax_id
282
  $query= <<SQL;
283
ALTER TABLE taxkeys ALTER COLUMN tax_id SET NOT NULL;
284
SQL
285
   
286
  do_query($query);
287

  
288
  #The triple chart_id, taxkey_id, startdate should be unique:
289
  $query= <<SQL;
290
CREATE UNIQUE INDEX taxkeys_chartid_startdate ON taxkeys(chart_id, startdate);
291
SQL
292
  
293
  do_query($query);
294
#ALL CONSTRAINTS WERE ADDED
295

  
296
  return 1;
297
}; # end do_update
298

  
299

  
300
sub print_error_message {
301
  print $main::form->parse_html_template("dbupgrade/tax_constraints");
302
}
303

  
304
return do_update();
templates/webpages/dbupgrade/tax_constraints.html
1
[%- USE T8 %]
2
[% USE HTML %]<div class="listtop">[% 'Inconsistency in database' | $T8 %]</div>
3

  
4
<form name="Form" method="post" action="login.pl">
5
<input type="hidden" name="action" value="login">
6

  
7
<p>[% 'There is an inconsistancy in your database.' | $T8 %]</p>
8
<p>[% 'Please contact your administrator.' | $T8 %]</p>
9

  
10
[% IF invalid_tax_account %]
11
  <p>[% 'There are invalid taxnumbers in use.' | $T8 %]</p>
12
  <p>[% 'Please set another taxnumber for the following taxes and run the update again:' | $T8 %]</p>
13
  <table>
14
    <tr>
15
      <th class="listheading">[% 'tax_taxkey' | $T8 %]</th>
16
      <th class="listheading">[% 'tax_taxdescription' | $T8 %]</th>
17
      <th class="listheading">[% 'tax_rate' | $T8 %]</th>
18
    </tr>
19
  
20
    [% SET row_odd = '1' %][% FOREACH row = TAX %]
21
    <tr class="listrow[% IF row_odd %]1[% SET row_odd = '0' %][% ELSE %]0[% SET row_odd = '1' %][% END %]">
22
      <td align="right">[% HTML.escape(row.taxkey) %]</td>
23
      <td align="left"> [% HTML.escape(row.taxdescription) %]</a></td>
24
      <td align="right">[% HTML.escape(row.rate) %] %</td>
25
    </tr>
26
    [% END %]
27
  </table>
28
  
29
[% END %]
30
  
31
[% IF taxkey_is_null %]
32
  <p>[% 'There are entries in tax where taxkey is NULL.' | $T8 %]</p>
33
  <p>[% 'Please define a taxkey for the following taxes and run the update again:' | $T8 %]</p>
34
  <table>
35
    <tr>
36
      <th class="listheading">[% 'tax_taxdescription' | $T8 %]</th>
37
      <th class="listheading">[% 'tax_rate' | $T8 %]</th>
38
      <th class="listheading">[% 'taxnumber' | $T8 %]</th>
39
      <th class="listheading">[% 'account_description' | $T8 %]</th>
40
    </tr>
41
  
42
    [% SET row_odd = '1' %][% FOREACH row = TAX %]
43
    <tr class="listrow[% IF row_odd %]1[% SET row_odd = '0' %][% ELSE %]0[% SET row_odd = '1' %][% END %]">
44
      <td align="left"> [% HTML.escape(row.taxdescription) %]</a></td>
45
      <td align="right">[% HTML.escape(row.rate) %] %</td>
46
      <td align="right">[% HTML.escape(row.taxnumber) %]</td>
47
      <td>[% HTML.escape(row.account_description) %]</td>
48
    </tr>
49
    [% END %]
50
  </table>
51
  
52
[% END %]
53
</form>

Auch abrufbar als: Unified diff