-- Phase 13.2.5: cancel posted expense payment vouchers safely
-- Adds cancel metadata columns and permission for expense voucher cancellation.

ALTER TABLE `cash_payments`
  ADD COLUMN IF NOT EXISTS `canceled_at` DATETIME NULL AFTER `created_at`,
  ADD COLUMN IF NOT EXISTS `canceled_by` BIGINT UNSIGNED NULL AFTER `canceled_at`,
  ADD COLUMN IF NOT EXISTS `cancel_reason` VARCHAR(255) NULL AFTER `canceled_by`;

INSERT IGNORE INTO `permissions` (`code`,`name`,`module`) VALUES
('cash.payments.cancel_expense', 'إلغاء سند صرف مصروف', 'cashbox');

INSERT IGNORE INTO `role_permissions` (`role_id`, `permission_id`)
SELECT 1, p.id
FROM permissions p
WHERE p.code = 'cash.payments.cancel_expense';
