Kostenarten und geschützte Standkosten umsetzen
This commit is contained in:
1 parent
313475afec
commit
3a0cb81a97
7 files changed
+200
-17
No files matched your search
@@ -0,0 +1,63 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE market_cost_type (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
code TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
is_system BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
INSERT INTO market_cost_type (code, name, is_system)
|
||||
VALUES ('stand_fee', 'Standkosten', TRUE);
|
||||
|
||||
ALTER TABLE market_occurrence_cost_item
|
||||
ADD COLUMN cost_type_id BIGINT REFERENCES market_cost_type(id) ON DELETE RESTRICT;
|
||||
|
||||
INSERT INTO market_cost_type (code, name)
|
||||
SELECT DISTINCT
|
||||
'legacy_' || md5(trim(cost_type)),
|
||||
trim(cost_type)
|
||||
FROM market_occurrence_cost_item
|
||||
WHERE trim(cost_type) <> ''
|
||||
AND lower(trim(cost_type)) <> 'standkosten'
|
||||
ON CONFLICT (name) DO NOTHING;
|
||||
|
||||
UPDATE market_occurrence_cost_item item
|
||||
SET cost_type_id = type.id
|
||||
FROM market_cost_type type
|
||||
WHERE type.name = CASE
|
||||
WHEN lower(trim(item.cost_type)) = 'standkosten' THEN 'Standkosten'
|
||||
ELSE trim(item.cost_type)
|
||||
END;
|
||||
|
||||
UPDATE market_occurrence_cost_item item
|
||||
SET cost_type_id = type.id
|
||||
FROM market_cost_type type
|
||||
WHERE type.code = 'stand_fee'
|
||||
AND item.id = (
|
||||
SELECT MIN(first_item.id)
|
||||
FROM market_occurrence_cost_item first_item
|
||||
WHERE first_item.occurrence_id = item.occurrence_id
|
||||
);
|
||||
|
||||
INSERT INTO market_occurrence_cost_item (occurrence_id, cost_type_id, description, gross_amount, tax_status, tax_rate)
|
||||
SELECT occurrence.id, type.id, NULL, 0.00, 'taxable', 8.10
|
||||
FROM market_occurrence occurrence
|
||||
JOIN market_cost_type type ON type.code = 'stand_fee'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM market_occurrence_cost_item item WHERE item.occurrence_id = occurrence.id
|
||||
);
|
||||
|
||||
ALTER TABLE market_occurrence_cost_item
|
||||
ALTER COLUMN cost_type_id SET NOT NULL;
|
||||
|
||||
ALTER TABLE market_occurrence_cost_item
|
||||
DROP COLUMN cost_type;
|
||||
|
||||
CREATE INDEX idx_market_occurrence_cost_item_type
|
||||
ON market_occurrence_cost_item(cost_type_id);
|
||||
|
||||
COMMIT;
|
||||
Reference in new issue
Block a user