treasuredb/schema/triggers/rebalanceIncreasedCredit.sql

18 lines
646 B
MySQL
Raw Normal View History

-- When we enter a transfer, the targetCredit of the associated bill might already be the credId
-- of a transfer for other dues itself. We can update (replace) the transfer for an unfullfilled one.
-- That way, a transfer may issue recursively chained transfers.
CREATE TRIGGER rebalanceIncreasedCredit
AFTER UPDATE OF value ON Credit
2017-01-22 21:20:17 +01:00
WHEN NEW.value > OLD.value
BEGIN
2017-01-22 21:20:17 +01:00
INSERT INTO __INTERNAL_TRIGGER_STACK
SELECT t.ROWID, t.billId, t.credId,
2017-01-22 21:20:17 +01:00
min(ca.difference, NEW.value - OLD.value)
FROM Transfer t
JOIN CurrentArrears ca ON t.billId = ca.billId
WHERE OLD.credId = t.credId
;
END;