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;

CREATE OR REPLACE FUNCTION _tmp_pm(p_name text, p_seq int, p_match text, p_pid int) RETURNS void AS $$
BEGIN
  INSERT INTO account_reconcile_model (name, sequence, company_id, trigger, match_label, match_label_param, mapped_partner_id, 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, p_pid, true, true, 2, 2, NOW(), NOW());
END; $$ LANGUAGE plpgsql;

-- MJR Capital = collections payments (Office Expense, HST)
SELECT _tmp_wo('MJR Capital Services - Collections', 600, 'mjr capital', 507, 20, 'MJR Capital Collections Payment');

-- Landry & Jacobs = legal/collections (Office Expense, NO TAX - US company in AZ)
SELECT _tmp_wo('Landry & Jacobs - Collections', 601, 'landry', 507, 32, 'Landry & Jacobs Collections');

-- Micro Center = US electronics retailer (Computer/IT, NO TAX - US)
SELECT _tmp_wo('Micro Center Electronics', 602, 'MICRO CENTER', 495, 32, 'Micro Center Electronics Purchase');

-- Maravi Canada = medical supplies vendor (partner mapping)
-- Need partner ID first - create as writeoff to Office for now
SELECT _tmp_wo('Maravi Canada Medical', 603, 'MARAVI', 507, 20, 'Maravi Canada Medical Supplies');

-- Google Turbo AI Note (SaaS, HST Canadian)
SELECT _tmp_wo('Google Turbo AI Note', 604, 'TURBO AI NOTE', 495, 20, 'Google Turbo AI Note');

-- FUSION NEXASYSTEMS = own company test charges (Office Expense, HST)
SELECT _tmp_wo('Fusion NexaSystems Test', 605, 'NEXASYSTEMS', 507, 20, 'NexaSystems Test Charge');

-- VPS IT NEXASYSTEMS = own company VPS hosting (Computer/IT, HST)
-- already covered by NEXASYSTEMS match above

-- Sunnybrook / St Josephs = hospital parking (Meals & Ent or Office, HST)
SELECT _tmp_wo('Hospital Parking - Sunnybrook', 606, 'sunnybrook', 506, 20, 'Sunnybrook Hospital Parking/Meals');
SELECT _tmp_wo('Hospital Parking - St Josephs', 607, 'st josephs', 506, 20, 'St Josephs Hospital Parking');

-- Canada Post (CPC SCP) - already exists but let's check
-- Model 49 matches "CPC SCP" - should work

-- Bolts Plus Inc = hardware supplies (Office Expense, HST)
SELECT _tmp_wo('Bolts Plus Hardware', 608, 'BOLTS PLUS', 507, 20, 'Bolts Plus Hardware Supplies');

-- Durafast Label Company = labels/printing (Office Expense, HST)
SELECT _tmp_wo('Durafast Label Company', 609, 'durafast', 507, 20, 'Durafast Label Printing');

-- Better Business Bureau = membership (Dues & Subs, HST)
SELECT _tmp_wo('Better Business Bureau', 610, 'better business bureau', 501, 20, 'Better Business Bureau Membership');

-- AmySystems = software (Computer/IT, HST Canadian - QC)
SELECT _tmp_wo('AmySystems Software', 611, 'AMYSYSTEMS', 495, 20, 'AmySystems Software');

-- Thermor Limited = medical equipment vendor
SELECT _tmp_pm('Thermor Limited', 612, 'thermor', NULL);

-- Aqua Creek Products = pool/medical equipment (US vendor)
-- Large amounts ($21K) - this is a PO vendor
SELECT _tmp_pm('Aqua Creek Products', 613, 'aqua creek', NULL);

-- Rogers (line 20184 with ******4596) - existing model should match
-- 407 ETR (line 20131) - existing model matches "407 ETR" but this says "407ETR (WEB)"
SELECT _tmp_wo('407 ETR Web Payment', 614, '407ETR', 497, 20, '407 ETR Web Highway Tolls');

-- 7 Spice Bistro / The Kebob / Momo2Go = restaurants (Meals, HST)
SELECT _tmp_wo('7 Spice Bistro', 615, '7 SPICE', 506, 20, '7 Spice Bistro Meals');
SELECT _tmp_wo('The Kebob Restaurant', 616, 'KEBOB', 506, 20, 'The Kebob Restaurant Meals');
SELECT _tmp_wo('Momo2Go Restaurant', 617, 'MOMO2GO', 506, 20, 'Momo2Go Restaurant Meals');

-- Jay Cee Sales & Rivet = hardware/industrial (Office, NO TAX - US in MI)
SELECT _tmp_wo('Jay Cee Sales & Rivet', 618, 'jay cee sales', 507, 32, 'Jay Cee Sales Industrial Supplies');

-- Kickstarter / Eufymake = crowdfunding purchase (Computer/IT, NO TAX - US)
SELECT _tmp_wo('Kickstarter Purchase', 619, 'kickstarter', 495, 32, 'Kickstarter Crowdfunding Purchase');

-- Bambu Lab = 3D printer (Computer/IT, NO TAX - Hong Kong)
SELECT _tmp_wo('Bambu Lab 3D Printer', 620, 'bambulab', 495, 32, 'Bambu Lab 3D Printer');

-- Dhillon Video Karo = video production (Advertising, HST)
SELECT _tmp_wo('Dhillon Video Karo', 621, 'dhillon video', 496, 20, 'Dhillon Video Production');

-- Cansew = sewing/upholstery supplies (Office Expense, HST)
SELECT _tmp_wo('Cansew Supplies', 622, 'cansew', 507, 20, 'Cansew Sewing/Upholstery Supplies');

-- NuthutVancouver = food/snacks (Meals, HST)
SELECT _tmp_wo('SP Nuthut', 623, 'NUTHUT', 506, 20, 'Nuthut Food/Snacks');

-- Flywire = payment processing for education (Office, HST)
SELECT _tmp_wo('Flywire Payment', 624, 'flywire', 507, 20, 'Flywire Education Payment');

-- IELTS Humber = education/testing (Office, HST)
SELECT _tmp_wo('IELTS Humber College', 625, 'IELTS', 507, 20, 'IELTS Testing Fee');

-- York University = education (Office, HST)
SELECT _tmp_wo('York University', 626, 'york u', 507, 20, 'York University Application Fee');

-- ESW US Direct = e-commerce (Office, NO TAX - US)
SELECT _tmp_wo('ESW US Direct E-Commerce', 627, 'ESW U.S.', 507, 32, 'ESW US Direct E-Commerce');

-- Corp Canada = already created (419), skip

-- NextDigitalKeys = software keys (Computer/IT, NO TAX - UK)
SELECT _tmp_wo('NextDigitalKeys Software', 628, 'nextdigitalkeys', 495, 32, 'NextDigitalKeys Software License');

-- StenoKeyboards = keyboard hardware (Computer/IT, NO TAX - foreign)
SELECT _tmp_wo('StenoKeyboards', 629, 'stenokeyboards', 495, 32, 'StenoKeyboards Hardware');

-- Global Technologies of Barrie = IT services vendor
SELECT _tmp_wo('Global Technologies Barrie', 630, 'global technologies', 495, 20, 'Global Technologies IT Services');

-- Milutin Vuicin = contractor/consultant (Computer/IT, NO TAX - US TX)
SELECT _tmp_wo('Milutin Vuicin Consulting', 631, 'milutin vuicin', 495, 32, 'Milutin Vuicin Consulting');

-- Maple Leaf Wheelchair = PO vendor
SELECT _tmp_pm('Maple Leaf Wheelchair', 632, 'maple leaf wheelchair', NULL);

-- Distributions GNX = distribution vendor (QC)
SELECT _tmp_wo('Distributions GNX', 633, 'distributions gnx', 507, 20, 'Distributions GNX');

-- ParkWhiz / ParkLink = parking (Car/Van, HST)
SELECT _tmp_wo('ParkWhiz / ParkLink Parking', 634, 'park', 497, 20, 'Parking Fee');
-- Actually 'park' is too broad, skip that. Use specific ones:
-- delete that last one, too generic
DELETE FROM account_reconcile_model WHERE name::text LIKE '%ParkWhiz%';
-- Re-do with specific matches
SELECT _tmp_wo('ParkWhiz Parking', 635, 'ParkWhiz', 497, 20, 'ParkWhiz Parking Fee');
SELECT _tmp_wo('Precise ParkLink', 636, 'parklink', 497, 20, 'Precise ParkLink Parking');

-- Span Medical Products = PO vendor
SELECT _tmp_pm('Span Medical Products', 637, 'SPAN MEDICAL', NULL);

-- NSC Medical = PO vendor
SELECT _tmp_pm('NSC Medical', 638, 'nsc medical', NULL);

-- WOW Mobile Boutique = phone accessories (Office, HST)
SELECT _tmp_wo('WOW Mobile Boutique', 639, 'MOBILE BOUTIQ', 507, 20, 'WOW Mobile Boutique');

-- Triumph Mobility = PO vendor
SELECT _tmp_pm('Triumph Mobility', 640, 'triumph mobility', NULL);

-- Home Healthcare Store = PO vendor
SELECT _tmp_pm('Home Healthcare Store', 641, 'home healthcare store', NULL);

-- Ubiquiti already created (409)
-- Anthropic already matched by model 138 (ANTHROPIC)

-- Royalmount Town = travel/accommodation (Travel, HST QC)
SELECT _tmp_wo('Royalmount Town Hotel', 642, 'royalmount', 525, 20, 'Royalmount Town Accommodation');

-- Westin Healthcare own charges = test transactions
SELECT _tmp_wo('Westin Healthcare Test', 643, 'WESTIN HEALTHCARE', 507, 20, 'Westin Healthcare Test Charge');

-- XTool Canada = laser cutter/tools (Computer/IT, HST - Canadian store)
SELECT _tmp_wo('XTool Canada', 644, 'xtool', 495, 20, 'XTool Canada Equipment');

-- Providence Healthcare = hospital parking (Meals, HST)
SELECT _tmp_wo('Providence Healthcare', 645, 'providence healthcare', 506, 20, 'Providence Healthcare Parking');

-- Glentel Wirelesswave = phone accessory (Office, HST)
SELECT _tmp_wo('Glentel Wirelesswave', 646, 'wirelesswave', 507, 20, 'Glentel Wirelesswave Phone');

-- 3DMouse = computer peripheral (Computer/IT, HST)
SELECT _tmp_wo('3DMouse Input Device', 647, '3dmouse', 495, 20, '3DMouse Input Device');

-- LawDepot already created (422)

-- Best Buy Medical already created as partner_map (501)

-- Catherwood & Vittoria = restaurant (Meals, HST)
SELECT _tmp_wo('Catherwood & Vittoria', 648, 'catherwood', 506, 20, 'Catherwood & Vittoria Restaurant');

-- SB M Wing = hospital cafeteria (Meals, HST)
SELECT _tmp_wo('Sunnybrook M Wing Cafe', 649, 'sb m wing', 506, 20, 'Sunnybrook M Wing Cafeteria');

-- Canada/Ottawa lines = government fees/parking
SELECT _tmp_wo('Canada Ottawa Govt Fee', 650, 'canada-Ottawa', 507, 20, 'Ottawa Government Fee');
SELECT _tmp_wo('Canada Ottawa Fee 2', 651, 'canada ottawa on', 507, 20, 'Ottawa Government Fee');

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

COMMIT;
