BEGIN;

-- MARKETING_OUT: if global rule exists, convert to AYAM-specific.
UPDATE fifo_stock_v2_overconsume_rules
SET
    flag_group_code = 'AYAM',
    allow_overconsume = FALSE,
    priority = 20,
    reason = 'fifo_v2_exception_marketing_block_ayam_only',
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_marketing_block';

-- MARKETING_OUT: if AYAM-specific row already exists, enforce desired value.
UPDATE fifo_stock_v2_overconsume_rules
SET
    allow_overconsume = FALSE,
    priority = 20,
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_marketing_block_ayam_only';

-- MARKETING_OUT: insert AYAM-specific if no suitable row exists.
INSERT INTO fifo_stock_v2_overconsume_rules(
    flag_group_code,
    function_code,
    lane,
    allow_overconsume,
    priority,
    reason,
    is_active
)
SELECT 'AYAM', 'MARKETING_OUT', 'USABLE', FALSE, 20, 'fifo_v2_exception_marketing_block_ayam_only', TRUE
WHERE NOT EXISTS (
    SELECT 1
    FROM fifo_stock_v2_overconsume_rules
    WHERE lane = 'USABLE'
      AND function_code = 'MARKETING_OUT'
      AND flag_group_code = 'AYAM'
      AND reason = 'fifo_v2_exception_marketing_block_ayam_only'
);

-- MARKETING_OUT: deactivate remaining global rule (if any duplicate row exists).
UPDATE fifo_stock_v2_overconsume_rules
SET
    is_active = FALSE
WHERE lane = 'USABLE'
  AND function_code = 'MARKETING_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_marketing_block';

-- STOCK_TRANSFER_OUT: if global rule exists, convert to AYAM-specific.
UPDATE fifo_stock_v2_overconsume_rules
SET
    flag_group_code = 'AYAM',
    allow_overconsume = FALSE,
    priority = 30,
    reason = 'fifo_v2_exception_transfer_block_ayam_only',
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_transfer_block';

-- STOCK_TRANSFER_OUT: if AYAM-specific row already exists, enforce desired value.
UPDATE fifo_stock_v2_overconsume_rules
SET
    allow_overconsume = FALSE,
    priority = 30,
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_transfer_block_ayam_only';

-- STOCK_TRANSFER_OUT: insert AYAM-specific if no suitable row exists.
INSERT INTO fifo_stock_v2_overconsume_rules(
    flag_group_code,
    function_code,
    lane,
    allow_overconsume,
    priority,
    reason,
    is_active
)
SELECT 'AYAM', 'STOCK_TRANSFER_OUT', 'USABLE', FALSE, 30, 'fifo_v2_exception_transfer_block_ayam_only', TRUE
WHERE NOT EXISTS (
    SELECT 1
    FROM fifo_stock_v2_overconsume_rules
    WHERE lane = 'USABLE'
      AND function_code = 'STOCK_TRANSFER_OUT'
      AND flag_group_code = 'AYAM'
      AND reason = 'fifo_v2_exception_transfer_block_ayam_only'
);

-- STOCK_TRANSFER_OUT: deactivate remaining global rule (if any duplicate row exists).
UPDATE fifo_stock_v2_overconsume_rules
SET
    is_active = FALSE
WHERE lane = 'USABLE'
  AND function_code = 'STOCK_TRANSFER_OUT'
  AND flag_group_code IS NULL
  AND reason = 'fifo_v2_exception_transfer_block';

-- CHICKIN_OUT: enforce AYAM-specific hard-block (cannot pending).
UPDATE fifo_stock_v2_overconsume_rules
SET
    allow_overconsume = FALSE,
    priority = 25,
    is_active = TRUE
WHERE lane = 'USABLE'
  AND function_code = 'CHICKIN_OUT'
  AND flag_group_code = 'AYAM'
  AND reason = 'fifo_v2_exception_chickin_block_ayam_only';

INSERT INTO fifo_stock_v2_overconsume_rules(
    flag_group_code,
    function_code,
    lane,
    allow_overconsume,
    priority,
    reason,
    is_active
)
SELECT 'AYAM', 'CHICKIN_OUT', 'USABLE', FALSE, 25, 'fifo_v2_exception_chickin_block_ayam_only', TRUE
WHERE NOT EXISTS (
    SELECT 1
    FROM fifo_stock_v2_overconsume_rules
    WHERE lane = 'USABLE'
      AND function_code = 'CHICKIN_OUT'
      AND flag_group_code = 'AYAM'
      AND reason = 'fifo_v2_exception_chickin_block_ayam_only'
);

COMMIT;
