BEGIN;

CREATE OR REPLACE FUNCTION _tmp_wo_transfer(p_name text, p_seq int, p_match text, p_acct int, p_label text) RETURNS void AS $$
DECLARE v_mid int; v_lid int;
BEGIN
  INSERT INTO account_reconcile_model (name, sequence, company_id, trigger, match_label, match_label_param, active, can_be_proposed, create_uid, write_uid, create_date, write_date)
  VALUES (jsonb_build_object('en_US', p_name), p_seq, 1, 'auto_reconcile', 'contains', p_match, true, true, 2, 2, NOW(), NOW()) RETURNING id INTO v_mid;
  INSERT INTO account_reconcile_model_line (model_id, company_id, sequence, account_id, amount_type, amount, amount_string, label, partner_id, create_uid, write_uid, create_date, write_date)
  VALUES (v_mid, 1, 10, p_acct, 'percentage', 100, '100', jsonb_build_object('en_US', p_label), 1, 2, 2, NOW(), NOW()) RETURNING id INTO v_lid;
  -- No tax on internal transfers
  INSERT INTO account_reconcile_model_line_account_tax_rel (account_reconcile_model_line_id, account_tax_id) VALUES (v_lid, 32);
END; $$ LANGUAGE plpgsql;

-- These models post PAYMENT FROM lines directly to Outstanding Receipts (493)
-- This handles cases where the source side was already reconciled
SELECT _tmp_wo_transfer('Scotia Visa - Payment From Current (7814)', 50, 'PAYMENT FROM', 493, 'CC Payment from Scotia Current');
SELECT _tmp_wo_transfer('Scotia Visa - Transfer From Current', 51, 'from - *****', 493, 'CC Payment from Scotia Current');
SELECT _tmp_wo_transfer('Scotia Visa - Payment From (X0)', 52, 'payment from -', 493, 'CC Payment from Scotia Current');

DROP FUNCTION _tmp_wo_transfer(text, int, text, int, text);

COMMIT;
