USE [ChangHongWMS612]
GO
/****** Object: StoredProcedure [dbo].[WMS_MonitorMaterialPut] Script Date: 10/08/2015 09:55:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Create by:WMS
Remark:加料记录(大屏幕)
*/
--exec WMS_MonitorMaterialPut ''
ALTER Proc [dbo].[WMS_MonitorMaterialPut](
@r_Date varchar(19) = ''
)
as
--计算上一个班次的开始和结束时间
declare @Date varchar(10), @CurrDate varchar(19), @BC varchar(5), @BCName varchar(10), @NextDay varchar(10)
declare @BeginDate varchar(19), @EndDate varchar(19)
if(@r_Date = '')
begin
select @Date=convert(varchar(10), getdate(),120), @CurrDate=convert(varchar(19), getdate(),120)
end
else
begin
select @Date=convert(varchar(10), @r_Date,120), @CurrDate=convert(varchar(19),@r_Date,120)
end
select @NextDay = convert(varchar(10), dateadd(day,1,convert(datetime,@Date)), 121)
if (@CurrDate >= @Date+' 20:30:00' or @CurrDate <= @Date+' 08:30:00')--如果程序在晚上8点到11点钟执行,则计算白班的OEE
begin
if(@CurrDate <= @Date+' 08:30:00')
select @Date=convert(varchar(10), DATEADD(D, -1, @Date),120)--如果是晚班,日期变成昨天
select @BC='BC', @BCName='晚班', @BeginDate=@Date+' 20:30:00', @EndDate=convert(varchar(10), DATEADD(D, 1, @Date), 120)+ ' 08:29:59'
end
else
begin
select @BC='AC', @BCName='白班', @BeginDate=@Date+' 08:30:00', @EndDate=@Date+' 20:29:59'
end
if object_id('tempdb..#t_MonitorMaterialPut') is not null drop table #t_MonitorMaterialPut
create table #t_MonitorMaterialPut
(
MachineNO varchar(50),--机床号
MO varchar(50),
CurrDate varchar(10),
BC varchar(50),
BCName varchar(50),
ItemNO varchar(50),
ItemName varchar(100),
SupplyCode varchar(50),
MaterialNO varchar(50),
MaterialName varchar(100),
ProdCost decimal(18,3), --产品定额
MOPlanMQty decimal(18,3),--工单计划用量
MOUseMQty decimal(18,3),--工单累计投料量
BCPutQty decimal(18,3), --本班投料量
Unit varchar(50), --单位
MOPlanQty int, --工单计划生产数量
MOProdQty int , --工单已生产数量
ProdUnit varchar(10),
PutMan varchar(50),
Remark varchar(100),
DispatchNO varchar(50),
DispatchPrior float,
Flag int ,
BOMNO varchar(50),
SubSupplyCode varchar(50),
SubItemName varchar(100),
SubItemNo varchar(50),
SubMOUseMQty decimal(18,3),
SubBCPutQty decimal(18,3)
)
declare @CurrTime datetime
select @CurrTime = GETDATE()
---取当前正在做的订单
select v.MO , v.DispatchNO, v.DispatchPrior, v.ItemNO, m1.ItemName, v.MachineNO, m1.PlanQty, ProdUnit = '件',
MOProdQty = ChangHong_612.dbo.FN_GetHaveWorkProdedNum(v.MO, @CurrTime)
into #MO
from ChangHong_612.dbo.V_DispatchOrder v
join ChangHong_612.dbo.MES_WorkOrder m1 on v.MO= m1.MO
where v.DispatchStatus = 1
select ID=identity(int),seq=CONVERT(int,null), d.MO, m.MachineNO ,m.Supplier,m.MaterialNO,p.FeedGrade,p.ItemType, FeedingQty = SUM(FeedingQty),mID =MAX(m.id)
, EmpName = CONVERT(varchar(50),null), EmpBC = CONVERT(varchar(20),null)
into #t_Material
from ChangHong_612.dbo.MES_Material m
join ChangHong_612.dbo.MES_MaterialPacket p on m.MaterialNO = p.MaterialNO and p.SupNO = m.Supplier
join (select distinct MO,DispatchNO from ChangHong_612.dbo.MES_DispatchOrder) d on m.DispatchNO = d.DispatchNO
where m.FeedingTime between @BeginDate and @EndDate
group by d.MO,m.MachineNO,m.Supplier,m.MaterialNO,p.FeedGrade,p.ItemType
order by d.MO, p.ItemType,m.MaterialNO
insert into #t_MonitorMaterialPut(CurrDate,MO, DispatchNO, DispatchPrior,ItemNO, ItemName, MachineNO, MOPlanQty, ProdUnit, MOProdQty
, MaterialNO, MaterialName, ProdCost, MOPlanMQty, Unit, BC, BCName, Flag, BOMNO)
select @Date, t1.MO, t1.DispatchNO, t1.DispatchPrior, t1.ItemNO, t1.ItemName , t1.MachineNO, t1.PlanQty, t1.ProdUnit, t1.MOProdQty
, b1.ItemNO, b1.ItemName, b1.Scrap/1000.0, t1.PlanQty*b1.Scrap/b1.Qty,b1.Unit, @BC, @BCName, 0, b1.BOMNO
from #MO t1
join ChangHong_612.dbo.MES_BOM b1 on b1.ParentItemNO = t1.ItemNO
update t set EmpName = e.EmpNameCN, EmpBC = d.DeptName
from #t_Material t
join ChangHong_612.dbo.MES_Material m on t.mID = m.ID
join ChangHong_612.dbo.MES_Employee e on m.EmpID = e.EmpID
join ChangHong_612.dbo.Sys_Department d on e.DeptCode = d.DeptCode
update t1 set t1.SupplyCode = m.Supplier , t1.BCPutQty = m.FeedingQty, t1.PutMan = m.EmpName
from #t_MonitorMaterialPut t1
left join #t_material m on t1.MO = m.MO and t1.MaterialNO = m.MaterialNO and t1.MachineNO = m.MachineNO
where isnull(m.FeedingQty,0)>0
insert into #t_MonitorMaterialPut (MachineNO, MO, CurrDate, BC, BCName, ItemNO, ItemName, SupplyCode, MaterialNO, MaterialName, BCPutQty
,Unit, MOPlanQty, MOProdQty, ProdUnit, ProdCost, MOPlanMQty, PutMan, DispatchNO, DispatchPrior, Flag, BOMNO)
select t1.MachineNO, t1.MO, t1.CurrDate, t1.BC, t1.BCName, t1.ItemNO, t1.ItemName, m.Supplier, m.MaterialNO, m.FeedGrade, m.FeedingQty
,t1.Unit, t1.MOPlanQty, t1.MOProdQty, t1.ProdUnit, t1.ProdCost, t1.MOPlanMQty, m.EmpName, t1.DispatchNO, t1.DispatchPrior, t1.Flag, t1.BOMNO
from #t_MonitorMaterialPut t1
left join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and t1.MaterialNO <> m.MaterialNO
where m.Supplier is not null
update t1 set t1.MOUseMQty = M.FeedingQty
from (select d.MO,m.MaterialNO,m.Supplier,P.FeedGrade, sum(m.FeedingQty) as FeedingQty
from ChangHong_612.dbo.MES_Material m
join ChangHong_612.dbo.MES_MaterialPacket p on m.MaterialNO = p.MaterialNO and p.SupNO = m.Supplier
join (select distinct MO,DispatchNO from ChangHong_612.dbo.MES_DispatchOrder) d on m.DispatchNO = d.DispatchNO
group by d.MO,m.MaterialNO,m.Supplier,P.FeedGrade
) M ,
#t_MonitorMaterialPut t1
where t1.MO = M.MO and t1.SupplyCode = m.Supplier and t1.MaterialNO = m.MaterialNO
select * from #t_MonitorMaterialPut order by MachineNO,MO
drop table #t_material