BEGIN;

CREATE OR REPLACE FUNCTION _tmp_wo(p_name text, p_seq int, p_match text, p_acct int, p_tax 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, 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), 2, 2, NOW(), NOW()) RETURNING id INTO v_lid;
  INSERT INTO account_reconcile_model_line_account_tax_rel (account_reconcile_model_line_id, account_tax_id) VALUES (v_lid, p_tax);
END; $$ LANGUAGE plpgsql;

-- ============================================================
-- GOVERNMENT CUSTOMER PAYMENTS → Outstanding Receipts (493)
-- These are payments FROM government agencies TO Westin for equipment/services
-- No tax on government transfer payments
-- ============================================================

-- ODSP = Ontario Disability Support Program (already partially matched by other models)
-- Check: model already exists? No - "Misc Payment ODSP" has no model
SELECT _tmp_wo('ODSP Government Payment', 700, 'ODSP', 493, 32, 'ODSP Customer Payment');

-- MODC = March of Dimes Canada (Expense Payment MODC = incoming govt payment)
SELECT _tmp_wo('MODC - March of Dimes Payment', 701, 'MODC', 493, 32, 'March of Dimes Customer Payment');

-- Revera Long Term Care payments
SELECT _tmp_wo('Revera LTC Payment', 702, 'Revera', 493, 32, 'Revera Long-Term Care Payment');

-- Medavie Blue Cross insurance payments
SELECT _tmp_wo('Medavie Blue Cross Payment', 703, 'MEDAVIE', 493, 32, 'Medavie Blue Cross Insurance Payment');

-- OMOD (Ontario March of Dimes variant)
SELECT _tmp_wo('OMOD Payment', 704, 'OMOD', 493, 32, 'Ontario March of Dimes Payment');

-- Peel Region payroll deposits (home care worker funding)
SELECT _tmp_wo('Peel Region North Deposit', 705, 'PEEL NORTH', 493, 32, 'Region of Peel North Payment');
SELECT _tmp_wo('Peel Region South Deposit', 706, 'PEEL SOUTH', 493, 32, 'Region of Peel South Payment');
SELECT _tmp_wo('Peel Region CMSM Deposit', 707, 'PEEL CMSM', 493, 32, 'Region of Peel CMSM Payment');

-- WSIB payments
SELECT _tmp_wo('WSIB Payment', 708, 'WSIB', 493, 32, 'WSIB Workers Compensation Payment');

-- GST Refund from CRA
SELECT _tmp_wo('CRA GST Refund', 709, 'GSTCANADA', 493, 32, 'CRA GST/HST Refund');

-- Affinity Health bill payments (incoming)
SELECT _tmp_wo('Affinity Health Payment', 710, 'Affinity Health', 493, 32, 'Affinity Health Customer Payment');

-- Amica Senior Living AP payments
SELECT _tmp_wo('Amica Senior Living Payment', 711, 'AMICA', 493, 32, 'Amica Senior Living Payment');

-- PCHS = Peel Community Health Services
SELECT _tmp_wo('PCHS Payment', 712, 'PCHS', 493, 32, 'PCHS Community Health Payment');

-- Run Care Canada
SELECT _tmp_wo('Run Care Canada Payment', 713, 'RUN CARE', 493, 32, 'Run Care Canada Payment');

-- Teskie International
SELECT _tmp_wo('Teskie International Payment', 714, 'TESKIE', 493, 32, 'Teskie International Payment');

-- ============================================================
-- STRIPE DEPOSITS → Outstanding Receipts (493)
-- Online payment gateway deposits
-- ============================================================
SELECT _tmp_wo('Stripe Payment Deposit', 720, 'STRIPE', 493, 32, 'Stripe Online Payment Deposit');

-- ============================================================
-- DEPOSITS / CHEQUE DEPOSITS → Outstanding Receipts (493)
-- Customer payments received
-- ============================================================
SELECT _tmp_wo('Mobile Cheque Deposit', 730, 'Mobile cheque deposit', 493, 32, 'Customer Cheque Deposit');
SELECT _tmp_wo('ATM Deposit', 731, 'ATM deposit', 493, 32, 'Customer ATM Cash/Cheque Deposit');

-- ============================================================
-- NSF RETURNS → Outstanding Receipts (493)
-- Bounced cheques — need to reverse original payment
-- ============================================================
SELECT _tmp_wo('Item Returned NSF', 740, 'Item returned NSF', 493, 32, 'NSF Item Return');
SELECT _tmp_wo('Cheque Returned NSF', 741, 'Cheque returned NSF', 493, 32, 'NSF Cheque Return');

-- ============================================================
-- OUTGOING PAYMENTS / BILLS
-- ============================================================

-- Personal Loan SPL (already has model 80 but checking)
-- Wawanesa Insurance (already model 28 — partner_map, needs bills)
-- Bill Payment Telus (already model for Telus)
-- Bill Payment BuildingStack
SELECT _tmp_wo('BuildingStack Rent Payment', 750, 'BUILDING_STACK', 560, 20, 'BuildingStack Building Rent');

-- Commercial Taxes
SELECT _tmp_wo('Commercial Property Tax', 751, 'COMMERCIAL TAXES', 507, 20, 'Commercial Property Tax Payment');

-- HMS Auto Service
SELECT _tmp_wo('HMS Auto Service', 752, 'HMS AUTO', 497, 20, 'HMS Auto Service Vehicle Repair');

-- Dixie Tailoring (alterations)
SELECT _tmp_wo('Dixie Tailoring', 753, 'DIXIE TAILORIN', 507, 20, 'Dixie Tailoring Services');

-- Hardware Agency
SELECT _tmp_wo('Hardware Agency', 754, 'HARDWARE AGENC', 507, 20, 'Hardware Agency Supplies');

-- Desi Haveli / Bamiyan Kabob (meals)
SELECT _tmp_wo('Desi Haveli Restaurant', 755, 'DESI HAVELI', 506, 20, 'Desi Haveli Restaurant Meals');
SELECT _tmp_wo('Bamiyan Kabob Restaurant', 756, 'BAMIYAN KABOB', 506, 20, 'Bamiyan Kabob Restaurant Meals');

-- Intuit/ADP payroll verification
SELECT _tmp_wo('Intuit Payroll Verification', 757, 'INTUITCANADAULC', 507, 32, 'Intuit Canada Payroll Verification');

-- ============================================================
-- BRANCH TRANSFERS → Outstanding Receipts (493)
-- Internal RBC account transfers
-- ============================================================
SELECT _tmp_wo('RBC Branch Transfer 1306', 760, 'BR TO BR - 1306', 493, 32, 'RBC Branch Transfer 1306');
SELECT _tmp_wo('RBC Branch Transfer 9970', 761, 'BR TO BR - 9970', 493, 32, 'RBC Branch Transfer 9970');

-- ============================================================
-- GENERIC DEPOSIT → Outstanding Receipts
-- ============================================================
SELECT _tmp_wo('Generic Bank Deposit', 770, 'Deposit', 493, 32, 'Bank Deposit');

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

COMMIT;
