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


-- yarn_stock 表 RLS 策略
CREATE POLICY users_select_company_yarn_stock ON public.yarn_stock
FOR SELECT USING (
 company_id = public.get_user_master_company_id()
);

CREATE POLICY users_insert_company_yarn_stock ON public.yarn_stock
FOR INSERT WITH CHECK (
 company_id = public.get_user_master_company_id()
);

CREATE POLICY users_update_company_yarn_stock ON public.yarn_stock
FOR UPDATE USING (
 company_id = public.get_user_master_company_id()
);

CREATE POLICY users_delete_company_yarn_stock ON public.yarn_stock
FOR DELETE USING (
 company_id = public.get_user_master_company_id()
);

-- yarn_ratios 表 RLS 策略
-- 通过 plan 关联到采购商
CREATE POLICY users_select_yarn_ratios ON public.yarn_ratios
FOR SELECT USING (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

CREATE POLICY users_insert_yarn_ratios ON public.yarn_ratios
FOR INSERT WITH CHECK (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

CREATE POLICY users_delete_yarn_ratios ON public.yarn_ratios
FOR DELETE USING (
 EXISTS (
 SELECT 1 FROM public.production_plans
 WHERE id = plan_id AND purchaser_id = public.get_user_master_company_id()
 )
);

