-- Let a credit feed-purchase settlement name the exact cash/bank account the
-- money left, the same way a manual expense payment does. Nullable: when it is
-- left empty the settlement keeps routing by payment method (original behaviour).
-- Safe / idempotent.

SET @has := (SELECT COUNT(*) FROM information_schema.columns
             WHERE table_schema = DATABASE() AND table_name = 'feed_purchase_payments' AND column_name = 'cash_account_id');
SET @sql := IF(@has = 0, 'ALTER TABLE feed_purchase_payments ADD COLUMN cash_account_id INT UNSIGNED NULL AFTER method', 'SELECT 1');
PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;
