-- schema version 10 to 11

CREATE TABLE payment_delivery_part_new (
    year       INTEGER NOT NULL,
    did        INTEGER NOT NULL,
    dpnr       INTEGER NOT NULL,
    avnr       INTEGER NOT NULL,

    net_amount INTEGER NOT NULL,
    mod_abs    INTEGER NOT NULL DEFAULT 0,
    mod_rel    REAL    NOT NULL DEFAULT 0,
    amount     INTEGER NOT NULL GENERATED ALWAYS AS (ROUND(net_amount * (1 + mod_rel) + mod_abs)) STORED,

    CONSTRAINT pk_payment_delivery_part PRIMARY KEY (year, did, dpnr, avnr),
    CONSTRAINT fk_payment_delivery_part_delivery_part FOREIGN KEY (year, did, dpnr) REFERENCES delivery_part (year, did, dpnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_payment_delivery_part_payment_variant FOREIGN KEY (year, avnr) REFERENCES payment_variant (year, avnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) STRICT;
INSERT INTO payment_delivery_part_new (year, did, dpnr, avnr, net_amount, mod_abs, mod_rel)
SELECT year, did, dpnr, avnr, net_amount, mod_abs, mod_rel
FROM payment_delivery_part;
PRAGMA foreign_keys = OFF;
PRAGMA writable_schema = ON;
DROP TABLE payment_delivery_part;
ALTER TABLE payment_delivery_part_new RENAME TO payment_delivery_part;
PRAGMA writable_schema = OFF;
PRAGMA foreign_keys = ON;

DROP TRIGGER IF EXISTS t_payment_delivery_part_i;
CREATE TRIGGER t_payment_delivery_part_i
    AFTER INSERT ON payment_delivery_part FOR EACH ROW
BEGIN
    INSERT INTO payment_member (year, avnr, mgnr, net_amount)
    SELECT year, NEW.avnr, mgnr, NEW.amount FROM delivery WHERE (year, did) = (NEW.year, NEW.did)
    ON CONFLICT DO UPDATE SET net_amount = net_amount + excluded.net_amount;
END;

DROP TRIGGER IF EXISTS t_payment_delivery_part_u;
CREATE TRIGGER t_payment_delivery_part_u
    AFTER UPDATE ON payment_delivery_part FOR EACH ROW
BEGIN
    UPDATE payment_member
    SET net_amount = net_amount - OLD.amount
    WHERE (year, avnr, mgnr) IN (SELECT year, OLD.avnr, mgnr FROM delivery WHERE (year, did) = (OLD.year, OLD.did));
    INSERT INTO payment_member (year, avnr, mgnr, net_amount)
    SELECT year, NEW.avnr, mgnr, NEW.amount FROM delivery WHERE (year, did) = (NEW.year, NEW.did)
    ON CONFLICT DO UPDATE SET net_amount = net_amount + excluded.net_amount;
END;

DROP TRIGGER IF EXISTS t_payment_delivery_part_d;
CREATE TRIGGER t_payment_delivery_part_d
    AFTER DELETE ON payment_delivery_part FOR EACH ROW
BEGIN
    UPDATE payment_member
    SET net_amount = net_amount - OLD.amount
    WHERE (year, avnr, mgnr) IN (SELECT year, OLD.avnr, mgnr FROM delivery WHERE (year, did) = (OLD.year, OLD.did));
END;

DROP TRIGGER IF EXISTS t_payment_delivery_part_bucket_u;
CREATE TRIGGER t_payment_delivery_part_bucket_u
    AFTER UPDATE ON payment_delivery_part_bucket FOR EACH ROW
BEGIN
    UPDATE payment_delivery_part
    SET net_amount = net_amount - OLD.amount
    WHERE (year, did, dpnr, avnr) = (OLD.year, OLD.did, OLD.dpnr, OLD.avnr);
    UPDATE payment_delivery_part
    SET net_amount = net_amount + NEW.amount
    WHERE (year, did, dpnr, avnr) = (NEW.year, NEW.did, NEW.dpnr, NEW.avnr);
END;

ALTER TABLE payment_member DROP COLUMN amount;
ALTER TABLE payment_member ADD COLUMN amount INTEGER NOT NULL GENERATED ALWAYS AS (ROUND(net_amount * (1 + mod_rel) + mod_abs)) VIRTUAL;
