-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 删除旧的仅允许采购商插入的策略
DROP POLICY IF EXISTS users_insert_plan_factories ON plan_factories;

-- 创建新策略：允许采购商创建关联，也允许工厂（纺织厂/水洗厂）加入计划
-- 采购商：purchaser_id = 当前用户公司ID
-- 工厂：factory_id = 当前用户公司ID（纺织厂/水洗厂加入计划时使用）
CREATE POLICY users_insert_plan_factories ON plan_factories
 FOR INSERT WITH CHECK (
 EXISTS (
 SELECT 1 FROM production_plans
 WHERE production_plans.id = plan_factories.plan_id
 AND production_plans.purchaser_id = get_user_master_company_id()
 )
 OR plan_factories.factory_id = get_user_master_company_id()
 );

