Telefonnotizen: in Berichten Angebot/Auftrag filtern können.
[kivitendo-erp.git] / sql / Pg-upgrade2 / konjunkturpaket_2020_SKR03.sql
1 -- @tag: konjunkturpaket_2020_SKR03
2 -- @description: Anpassung des Deutschen DATEV-Kontenrahmen für SKR03 Konjunkturpaket
3 -- @depends: release_3_5_5
4 -- @ignore: 0
5
6 DO $$
7 BEGIN
8
9 IF ( select coa from defaults ) = 'Germany-DATEV-SKR03EU' THEN
10
11   -- DEBUG
12   -- UPDATE tax SET taxdescription = 'OLD ' || taxdescription WHERE (taxkey = 3 or taxkey = 9) and rate = 0.16;
13
14   -- rename some of the charts, 1773 already exists in kivitendo as Umsatzsteuer 16% innergem.Erwerb
15   -- this is being used by taxkey 13, which is called "Steuerpflichtige EG-Lieferung zum vollen Steuersatz" in kivitendo
16   -- in DATEV taxkey 13 is: innergem. Lieferung ohne USt-IdNr. and should use a different chart
17   UPDATE chart SET description = 'Umsatzsteuer 5 %' where accno = '1773';
18
19   -- rename charts if they weren't already changed
20   UPDATE chart SET description = 'Erlöse 19 % / 16 % USt' where accno = '8400' and description = 'Erlöse 16%/19% USt.';
21   UPDATE chart SET description = 'Erlöse 7 % / 5 % USt'   where accno = '8300' and description = 'Erlöse 7%USt';
22
23   -- there are two strategies for updating the taxkeys.
24
25   -- 1) in any case we need to add the 2 new cases for 5%: 2/0.05/1773 and 8/0.05/1568
26
27   -- 2) default kivi SKR03 already has the correct configuration for 16%, with two entries 3/0.16/1775 and 9/0.16/1575
28   --   a) we could move those to 5 and 7, and then create new 3/0.16/1775 and 9/0.16/1575 entries
29   --   b) simply keep those entries and don't use 5 and 7 (in which case ar/ap/gl must use deliverydate), or create 5 and 7 manually if needed
30
31   -- strategy a:
32   -- datev reactivated the previously reserved chart 1775 in 2020, but it still exists in kivitendo (at least for SKR03)
33   -- with a taxkey starting from 2007 and pointing to the existing automatic tax chart 1775
34
35   -- strategy b:
36   -- UPDATE tax SET taxkey = 5 WHERE taxkey = 3 and rate = 0.16;
37   -- UPDATE tax SET taxkey = 7 WHERE taxkey = 9 and rate = 0.16;
38
39   -- rename old 8735 to 8736
40   UPDATE chart SET accno = '8736', description = 'Gewährte Skonti 19 % USt' where accno = '8735' and description = 'Gewährte Skonti 16%/19% USt.';
41
42   -- new charts, each of these will need a manual taxkey entry for 2020-07-01 after their tax entries are added
43   -- 8732, 3732, 8735, 3737
44   INSERT INTO chart (accno, description, charttype, category, link, taxkey_id, pos_bwa, pos_bilanz, pos_eur, datevautomatik)
45          VALUES ('8732','Gewährte Skonti 5% USt','A', 'I', 'AR_paid', 2, 1, null,1, 't');
46
47   INSERT INTO chart (accno, description, charttype, category, link, taxkey_id, pos_bwa, pos_bilanz, pos_eur, datevautomatik)
48          VALUES ('3732','Erhaltene Skonti 5 % Vorsteuer','A', 'E', 'AP_paid', 8, 4, null, null, 't');
49
50   -- create new 16% charts Skonto
51   INSERT INTO chart(accno,                description, charttype, category,      link, taxkey_id, pos_bwa, pos_bilanz, pos_eur, datevautomatik, pos_er)
52             VALUES ('8735','Gewährte Skonti 16 % USt',       'A',      'I', 'AR_paid',         3,       1,       null,       1,            't',      1);
53
54   INSERT INTO chart(accno,                description, charttype, category,       link, taxkey_id, pos_bwa, pos_bilanz, pos_eur, datevautomatik, pos_er)
55             VALUES ('3737','Erhaltene Skonti 16 % USt',       'A',      'E', 'AP_paid',         9,       4,       null,    null,            't',   null);
56
57   -- create new chart for Abziehbare Vorsteuer 5 % with taxkey 8 for 3732
58   INSERT INTO chart (accno, description, charttype, category, link, taxkey_id, pos_bwa, pos_bilanz, pos_eur, datevautomatik, pos_er)
59          VALUES ('1568','Abziehbare Vorsteuer 5 %','A', 'E', 'AP_tax:IC_taxpart:IC_taxservice', 8, null, null, 27, 't', 27);
60   INSERT INTO taxkeys (chart_id, tax_id, taxkey_id, pos_ustva, startdate)
61                VALUES ( (select id from chart where accno = '1568'), 0, 0, 66, '1970-01-01');
62
63   -- taxkeys can't be inserted until the new taxes exist
64
65   -- new taxes:
66   -- 5% cases for 2 Umsatzsteuer and 8 Vorsteuer
67   INSERT INTO tax (chart_id, rate, taxkey, taxdescription, chart_categories, skonto_sales_chart_id, skonto_purchase_chart_id)
68   VALUES ( (select id from chart where accno = '1773'), 0.05, 2, 'Umsatzsteuer', 'I', (select id from chart where accno = '8732'), null),
69          -- don't add these two entries if we keep the original two 16% accounts, instead better to add new tax entries with taxkey 5 and 7
70          -- ( (select id from chart where accno = '1775'), 0.16, 3, 'Umsatzsteuer', 'I', (select id from chart where accno = '8735'), null),
71          -- ( (select id from chart where accno = '1575'), 0.16, 9, 'Vorsteuer',    'E', null, (select id from chart where accno = '3735')),
72          ( (select id from chart where accno = '1568'), 0.05, 8, 'Vorsteuer',    'E', null, (select id from chart where accno = '3732'));
73
74   UPDATE tax SET skonto_sales_chart_id    = (select id from chart where accno = '8735') where taxkey = 3 and rate = 0.16 and skonto_sales_chart_id    is null;
75   UPDATE tax SET skonto_purchase_chart_id = (select id from chart where accno = '3737') where taxkey = 9 and rate = 0.16 and skonto_purchase_chart_id is null;
76
77   -- new taxkeys for 5% charts only need one startdate, not valid before and won't change back to anything later
78   -- these taxkeys won't be valid on 2020-06-30, so won't be affected later by big taxkeys update
79   -- However, this will also cause opening the charts before 2020-07-01 via the
80   -- interface to break, as AM.pm always calls get_active_taxkey and there won't
81   -- be an active taxkey before 2020-07-01.
82   -- Alternatively you could set those active from 2020-06-01 and in the taxkey upgrade script check for taxkey entries before that date
83   INSERT INTO taxkeys (chart_id, tax_id, taxkey_id, pos_ustva, startdate)
84                VALUES ( (select id from chart where accno = '8732'), (select id from tax where rate = 0.05 and taxkey = 2 and chart_id = (select id from chart where accno = '1773')), 2, 861, '2020-07-01');
85
86   INSERT INTO taxkeys (chart_id, tax_id, taxkey_id, pos_ustva, startdate)
87                VALUES ( (select id from chart where accno = '3732'), (select id from tax where rate = 0.05 and taxkey = 8 and chart_id = (select id from chart where accno = '1568')), 8, 861, '2020-07-01');
88
89   -- 8735 / 3737 - these were never created in the original SKR03, so also start using them from 2020-07-01
90   -- taxkey for Gewährte Skonti 16 % USt pointing to tax 1775 Umsatzsteuer 16%
91   INSERT INTO taxkeys (chart_id, tax_id, taxkey_id, pos_ustva, startdate)
92                VALUES ( (select id from chart where accno = '8735'), (select id from tax where rate = 0.16 and taxkey = 3 and chart_id = (select id from chart where accno = '1775')), 3, 81, '2020-07-01');
93
94   -- taxkey for Erhaltene Skonti 16 % USt pointing to tax 1575 Vorsteuer 16%
95   INSERT INTO taxkeys (chart_id, tax_id, taxkey_id, pos_ustva, startdate)
96                VALUES ( (select id from chart where accno = '3737'), (select id from tax where rate = 0.16 and taxkey = 9 and chart_id = (select id from chart where accno = '1575')), 9, 66, '2020-07-01');
97
98 END IF;
99
100 END $$;