declare
cursor c_cur1 is
select distinct c.segment1 as productno1
from BOM_BILL_OF_MATERIALS a, ---bom头表
BOM_INVENTORY_COMPONENTS b, ---bom明细
mtl_system_items_b c, ---物料表
mtl_system_items_b d,
bom_operation_sequences bos, ---工序明细(料品制程明细表)
bom_operational_routings bor, ---工艺路线
BOM_DEPARTMENTs bd, ---部门代码对应表
org_organization_definitions ood
where a.BILL_SEQUENCE_ID = b.BILL_SEQUENCE_ID ---清单序号(关键字)无实际含义,类似序列
and a.ORGANIZATION_ID = c.organization_id ---组织代码
and a.ASSEMBLY_ITEM_ID = c.inventory_item_id ---a.装配件内码 c 料品id
and a.ORGANIZATION_ID = d.organization_id
and b.COMPONENT_ITEM_ID = d.inventory_item_id --- b 构件序号 d 料品id
and bos.routing_sequence_id = bor.common_routing_sequence_id
and bor.assembly_item_id = a.ASSEMBLY_ITEM_ID ---bor 料品id
and bor.organization_id = a.ORGANIZATION_ID
and nvl(bor.alternate_routing_designator, 1) = nvl(a.ALTERNATE_BOM_DESIGNATOR, 1)
and bos.operation_seq_num = b.OPERATION_SEQ_NUM
and nvl(bos.disable_date, trunc(sysdate) + 1) >= sysdate
and nvl(operation_type, 1) = 1
and bos.department_id = bd.department_id
and a.ORGANIZATION_ID = ood.ORGANIZATION_ID
and c.segment1 like'5%' ---半成品
and b.disable_date is null
and b.IMPLEMENTATION_DATE is not null
;
/**计算半成品成本**/
cursor c_cur3(v_productno1 IN mtl_system_items_b.segment1 %type) is
select a.segment1
,to_number(listagg(DECODE(a.costs_flag,'SB',a.costs),'') within group (order by DECODE(a.costs_flag,'SB',a.costs))) AS costs_sb
,to_number(listagg(DECODE(a.costs_flag,'RG',a.costs),'') within group (order by DECODE(a.costs_flag,'RG',a.costs))) AS costs_rg
from
(
SELECT msi.segment1
,case when br.resource_code like '%-SB' then 'SB' else 'RG' end as costs_flag
,sum(v.resource_rate*decode(bors.basis_type, 2, NULL, bors.usage_rate_or_amount)) as costs -- 成本
FROM bom_operational_routings bor
,bom_operation_sequences bos
,bom_operation_resources bors
,bom_sub_operation_resources bsor
,org_organization_definitions ood
,mtl_system_items_b msi
,bom_resources br
,bom_resources br1
,bom_departments bod
,cst_resource_costs_v v
WHERE bor.routing_sequence_id = bos.routing_sequence_id
AND bos.operation_sequence_id = bors.operation_sequence_id
AND bors.operation_sequence_id = bsor.operation_sequence_id(+)
AND bors.substitute_group_num = bsor.substitute_group_num(+)
AND bor.organization_id = ood.organization_id
AND bor.assembly_item_id = msi.inventory_item_id
AND bor.organization_id = msi.organization_id
AND bors.resource_id = br.resource_id
AND bsor.resource_id = br1.resource_id(+)
AND bos.department_id = bod.department_id
AND V.resource_id = BORS.RESOURCE_ID
AND nvl(bos.effectivity_date, SYSDATE - 1) < SYSDATE
AND nvl(bos.disable_date, SYSDATE + 1) > SYSDATE
AND bor.organization_id = 84
AND msi.segment1 = v_productno1
GROUP BY msi.segment1,case when br.resource_code like '%-SB' then 'SB' else 'RG' end
) a
group by a.segment1
;
row_semi_product c_cur3%rowtype;
BEGIN
/**将半成品的成本数据插入新建的临时表semi_product_costs**/
FOR r_cur1 IN c_cur1 LOOP
FOR r_cur3 IN c_cur3(r_cur1.productno1) loop
fetch c_cur3 into row_semi_product;
exit when c_cur3%notfound;
Insert Into semi_product_costs values row_semi_product;
DBMS_OUTPUT.ENABLE(1000000);
DBMS_OUTPUT.PUT_LINE(r_cur3.segment1||' '||r_cur3.costs_rg||' '||r_cur3.costs_sb);
END LOOP;
commit;
END LOOP;
end;