odoo 分享 PostgreSQL 语句2
----------前提:
1.需求是报表逻辑验证
2.供应链pc输入需求值
3.系统抓取产能
4.判断产能/需求>1.15 需要提示
5.需求描述:
产能合法性校验处理逻辑
- 第一步:根据【产品/物料编码】和【ODM/OEM加工厂产线】获取MRP需求计划值。
- 第二步:按照【周】进行MRP需求计划值求和;
- 第三步:获取对应【ODM/OEM加工厂产线】的产能;
- 第四步:通过【MRP需求的周计划值】和【ODM/OEM加工厂产线】的产能,计算所需的产线条数;【所需产线条数】=【MRP需求的周计划值】/【ODM/OEM加工厂产线】的产能,保留4位以上小数。
- 第五步:按照【ODM/OEM加工厂产线】和【周次】进行【所需产线条数】的求和。
- 第六步:系统校验:【所需产线条数】是否存在大于对应加工厂产线的产能负荷上限(115%)的记录。如果不存在,则产能负荷校验通过。如果存在【所需产线条数】是否存在大于对应加工厂产线的产能负荷上限(115%)的记录则给出提示:确认失败。如下【ODM/OEM加工厂产线】的产能负荷率超过产能负荷上限,【ODM/OEM加工厂产线】清单为:“1%”
- 1%为:ODM/OEM加工厂产线的名称,存在多条时,用中文的“,”隔开。
- 说明:如果找不到产能,按照产能为零计算,提示产线负荷超标。
需求地址:MRP需求计划
----------分析:
1.测试理解 关联很多临时表,需要生成多个视图以方便调用
----------测试实现:
With lumi_prod_line_all AS --工作日历的工厂 产线 具体某天 班制 ( select lpll.prod_line_id,lpll.system_id,lpl.factory_id,lpll.plan_date from lumi_prod_line lpl left join lumi_prod_line_line lpll on lpl.id=lpll.prod_line_id where lpll.is_fac_prod= 't' --and plan_date ='2020-01-01' and lpl.STATE= 'confirmed' ), lumi_capacity_all as --产能 ( select lc.factory_id,lc.prod_line_id,lc.system_id,lcl.product_id,lcl.std_cap from lumi_capacity lc left join lumi_capacity_line lcl on lcl.cap_id=lc.id where lc.state= 'confirmed' and lc.company_id=1 ), lumi_prod_line_and_capacity_all as ( select a.factory_id,a.prod_line_id,a.system_id, generate_series( extract (week from a.plan_date):: integer , -- 起始周 extract (week from a.plan_date):: integer , -- 结束周 1) as week, a.plan_date,b.product_id,b.std_cap from lumi_prod_line_all a left join lumi_capacity_all b on a.factory_id=b.factory_id and a.prod_line_id=b.prod_line_id and a.system_id=b.system_id order by a.plan_date ) , final_all as ( select factory_id,prod_line_id,system_id,product_id, substring (plan_date:: VARCHAR from 1 for 4)||week as year_week, sum (std_cap) std_cap, string_agg(plan_date:: VARCHAR , ',' order by plan_date) as string_agg_plan_date, count (plan_date) as work_dates from lumi_prod_line_and_capacity_all group by factory_id,prod_line_id,system_id,product_id,year_week ) , final_all_detail as ( select rp. name factory_name, lpl. name prod_line_name, lss. name shift_system_name, pp.default_code, fa.year_week, fa.std_cap, fa.string_agg_plan_date, fa.work_dates from final_all fa left join res_partner rp on rp.id=fa.factory_id left join lumi_prod_line lpl on lpl.id=fa.prod_line_id left join lumi_shift_system lss on lss.id=fa.system_id left join product_product pp on pp.id=fa.product_id where pp.default_code is not null and fa.std_cap is not null ) select * from final_all_detail |