-- =========================================================
-- 098_purchase_orders_ordered_step.sql
-- Ordered step after finalization
-- =========================================================

ALTER TABLE purchase_orders
  MODIFY status ENUM('draft','finalized','ordered','partial_receiving','sent','completed') NOT NULL DEFAULT 'draft';

ALTER TABLE purchase_orders
  ADD COLUMN ordered_by_user_id BIGINT UNSIGNED DEFAULT NULL AFTER finalized_at,
  ADD COLUMN ordered_by_name_snapshot VARCHAR(120) DEFAULT NULL AFTER ordered_by_user_id,
  ADD COLUMN ordered_at DATETIME DEFAULT NULL AFTER ordered_by_name_snapshot;

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
('purchase_orders.mark_ordered', 'Tandai Purchase Orders Sudah Diorder', 'purchase_orders', NOW());

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.code IN (
  'purchase_orders.mark_ordered'
)
WHERE r.code IN ('owner', 'admin', 'kepala_toko');
