-- Add a "Due from Owner" asset account (code 1150, role 'owner_receivable') for
-- every farm. Money LENT to the owner that they will repay is a receivable (an
-- asset), not a drawing:
--   taking a loan    → Dr Due from Owner   Cr Cash / Bank / Mobile Money
--   owner repays     → Dr Cash / Bank / Mobile Money   Cr Due from Owner
-- The outstanding balance is what the owner still owes the farm.
--
-- Idempotent — safe to re-run.

INSERT INTO chart_of_accounts (farm_id, code, name, type, role, map_key, is_system, sort_order)
SELECT f.id, '1150', 'Due from Owner', 'asset', 'owner_receivable', NULL, 1, 4
FROM farms f
WHERE NOT EXISTS (
  SELECT 1 FROM chart_of_accounts c
  WHERE c.farm_id = f.id AND c.role = 'owner_receivable'
);
