-- Let a sale record WHICH asset/tender account its cash was received INTO, now
-- that a farm can have several (Cash on Hand, Bank, Mobile Money, custom floats).
-- Mirrors the expense "paid from" account added in migration-2026-08-expense-cash-account.
--   * sales.cash_account_id         — account the cash paid at sale time landed in
--   * sale_payments.cash_account_id — account a later settlement's cash landed in
-- Plain nullable columns (no FK): the controller validates farm-scope + that the
-- account is a cash account, and the ledger captures the account at post time.
-- Safe/idempotent: each column add is guarded.

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

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