-- Slice 8Z-t · Supplier PR execution — journal + LPO auto-close (Slice D3)
--
-- Adds:
--  1) Seed COA 1101 Cash on Hand + 2110 Trade Payables (needed for supplier payment journals).
--     Idempotent — INSERT IGNORE respects a UNIQUE key on account_code.
--  2) payment_schedule_items.payment_request_id link (optional) so a schedule item can point at
--     a specific PR rather than an LPO whole. Enables batch PR execution via Payment Schedules.

-- ── 1. COA seed ─────────────────────────────────────────────────────────
INSERT IGNORE INTO chart_of_accounts (account_code, account_name, account_type, is_active, created_on)
VALUES
    ('1101', 'Cash on Hand',   'ASSET',     1, NOW()),
    ('2110', 'Trade Payables', 'LIABILITY', 1, NOW());

-- ── 2. payment_schedule_items.payment_request_id ───────────────────────
SET @sch := DATABASE();
SET @c := (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@sch AND table_name='payment_schedule_items' AND column_name='payment_request_id');
SET @sql := IF(@c=0,
    "ALTER TABLE payment_schedule_items
        ADD COLUMN payment_request_id INT UNSIGNED NULL AFTER lpo_id,
        ADD KEY idx_pr (payment_request_id)",
    'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ── 3. payment_requests.acct_record_note (missed in D2) ────────────────
SET @c := (SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@sch AND table_name='payment_requests' AND column_name='acct_record_note');
SET @sql := IF(@c=0,
    "ALTER TABLE payment_requests ADD COLUMN acct_record_note VARCHAR(500) NULL",
    'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
