码迷,mamicode.com
首页 > 其他好文 > 详细

加料记录日报表的存储过程

时间:2016-03-30 17:52:21      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

USE [ChiefWMS]
GO
/****** Object: StoredProcedure [dbo].[st_WMS_JobGenRptMaterialPut] Script Date: 03/28/2016 10:47:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
原存储过程名:Job_GenRptMaterialPut

exec [dbo].[st_WMS_JobGenRptMaterialPut] ‘2016-03-25 09:30:00‘
*/
ALTER Proc [dbo].[st_WMS_JobGenRptMaterialPut](
@CreateTime varchar(19) = ‘2014-12-16 09:30:00‘,-- @r_Date varchar(19) = ‘2014-12-10 21:30:00‘,
@IsFeed bit = 0--@i_Debug bit = 0
)
as
--计算上一个班次的开始和结束时间
declare @Date varchar(10), @CurrDate varchar(19), @BC varchar(5), @BCName varchar(10)
declare @BeginDate varchar(19), @EndDate varchar(19)
if(@CreateTime = ‘‘)
begin
select @Date=convert(varchar(10), getdate(),120), @CurrDate=convert(varchar(19), getdate(),120)
end
else
begin
select @Date=convert(varchar(10), @CreateTime,120), @CurrDate=convert(varchar(19),@CreateTime,120)
end

if (@CurrDate between @Date+‘ 20:00:00‘ and @Date+‘ 23:59:00‘)--如果程序在晚上8点到11点钟执行,则计算白班的OEE
begin
select @BC=‘AC‘, @BCName=‘白班‘, @BeginDate=@Date+‘ 08:00:00‘, @EndDate=@Date+‘ 19:59:59‘
end
else--如果程序在早上9点到11点钟执行,则计算昨天晚班的OEE
begin
select @Date=convert(varchar(10), DATEADD(D, -1, @Date),120)--如果是晚班,日期变成昨天
select @BC=‘BC‘, @BCName=‘晚班‘, @BeginDate=@Date+‘ 20:00:00‘, @EndDate=convert(varchar(10), DATEADD(D, 1, @Date), 120)+ ‘ 07:59:59‘
end

delete from WMS_RptMaterialPutDaily where CurrDate=@Date and MESBC=@BC
if object_id(‘tempdb..#t_RptMaterialPutDaily‘) is not null drop table #t_RptMaterialPutDaily
select *, RdCount = CONVERT(int,null),Flag2=CONVERT(int,null) into #t_RptMaterialPutDaily from WMS_RptMaterialPutDaily where 1 = 0

--取班次有生产的工单
insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, MouldNO, ProdWeight,JiaoKouWeight,MultiNum,DispatchNO)
select distinct d.CurrDate, d.MO, d.ItemNo, d.ItemName, d.BCCode, d.MachineNO, sum(d.ProdQty), d.MouldNO, d.ProdWeight/1000, d.JiaoKouWeight/1000,d.MultiNum, MAX(d.DispatchNO)
from v_MOProdByDay d
where d.CurrDate = @Date and d.BCCode = @BC
and d.Mo<>‘‘
group by d.CurrDate, d.MO, d.ItemNo, d.ItemName, d.BCCode, d.MachineNO, d.MouldNO, d.ProdWeight, d.JiaoKouWeight,d.MultiNum


--insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight,MultiNum,DispatchNO)
-- select distinct d.CurrDate, d.MO, d.ItemNo, d.ItemName, d.BCCode, d.MachineNO, sum(d.ProdQty), d.socketnum, d.MouldNO, d.ProdWeight/1000, d.JiaoKouWeight/1000,d.MultiNum, MAX(d.DispatchNO)
-- from v_MOProdByDay2 d
-- where d.CurrDate = @Date and d.BCCode = @BC
-- and d.Mo<>‘‘ and not exists(select 1 from #t_RptMaterialPutDaily t2 where d.MO = t2.MO and t2.MachineNO=d.MachineNO and d.ItemNo = t2.ItemNo)
-- group by d.CurrDate, d.MO, d.ItemNo, d.ItemName, d.BCCode, d.MachineNO, d.socketnum, d.MouldNO, d.ProdWeight, d.JiaoKouWeight,d.MultiNum

---填补当前班次未生产但有投料的订单


insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight,DispatchNo)
select distinct @Date, d.MO, d.ItemNO, i.ItemName, @BC, m.MachineNO, 0, null, d.MouldNO, null, null,m.DispatchNO
from ChiefMESNew.dbo.MES_Material m
join (select distinct MO, DispatchNO, ItemNO , MachineNO, MouldNO from ChiefMESNew.dbo.V_DispatchOrder) d on d.DispatchNO = m.DispatchNO and d.MachineNO=m.MachineNO
join ChiefMESNew.dbo.MES_Item i on d.ItemNO = i.ItemNO
where m.FeedingTime between @BeginDate and @EndDate and m.BatchNO<>‘退仓库‘
and not exists(select 1 from #t_RptMaterialPutDaily t1 where t1.MO = d.MO and t1.ItemNo = d.ItemNo and t1.MachineNO=d.MachineNO)

--填补有机床机放料的订单
insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight)
select distinct @Date, w.MO, w.ItemNO, w.ItemName, @BC, MachineNO = m.MachineNO, 0, t1.SocketNum, t1.MouldNO, null, null
from KIT_MOMix m
join ChiefMESNew.dbo.MES_WorkOrder w on w.MO = m.MO
join (select distinct MO, MachineNO, SocketNum,ItemNO,MouldNO,DispatchNO from ChiefMESNew.dbo.V_DispatchOrder ) t1 on t1.MO = w.MO and t1.MachineNO=m.MachineNO
where m.MixTime between @BeginDate and @EndDate and m.State in(0,1)
and not exists(select 1 from #t_RptMaterialPutDaily t2 where t1.MO = t2.MO and t2.MachineNO=t1.MachineNO and t1.ItemNo = t2.ItemNo)

--填补有机加放放料的订单
insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight)
select distinct @Date, w.MO, w.ItemNO, w.ItemName, @BC, MachineNO = m.RecMachineNo, 0, t1.SocketNum, t1.MouldNO, null, null
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join ChiefMESNew.dbo.MES_WorkOrder w on w.MO = m.ReceiveMO
join (select distinct MO, MachineNO, SocketNum,ItemNo,MouldNO, DispatchNO from ChiefMESNew.dbo.V_DispatchOrder ) t1 on t1.MO = w.MO and t1.MachineNO=m.RecMachineNo
where m.RecTime between @BeginDate and @EndDate and m.State=1
and not exists(select 1 from #t_RptMaterialPutDaily t2 where t1.MO = t2.MO and t2.MachineNO=t1.MachineNO and t1.ItemNo = t2.ItemNo)

---填补试模的MO
insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight)
select distinct @Date, v.MO, d.ItemNO, i.ItemName, @BC, MachineNO = v.MachineNO, ChiefMESNew.dbo.FN_GetAdjustMachineQty(v.MO,v.DispatchNo,v.ItemNo,v.DispatchPrior,@BeginDate,@EndDate,‘‘,‘‘), d.SocketNum, d.MouldNO, null, null
from ChiefMESNew.dbo.MES_QC q
join ChiefMESNew.dbo.V_DispatchOrder v on q.DispatchNo = v.DispatchNO and q.DispatchPrior = v.DispatchPrior
join ChiefMESNew.dbo.MES_DispatchOrder d on v.DispatchNO = d.DispatchNO and v.DispatchPrior = d.DispatchPrior
join ChiefMESNew.dbo. MES_Item i on d.ItemNO = i.ItemNO
where q.CheckDate between @BeginDate and @EndDate
and not exists(select 1 from #t_RptMaterialPutDaily t1 where t1.MO = d.MO and t1.ItemNo = d.ItemNo)

--填补班次内有做盘点的MO
insert into #t_RptMaterialPutDaily(CurrDate, MO, ItemNO, ItemName, MESBC, MachineNO, ProdQty, SocketQty, MouldNO, ProdWeight,JiaoKouWeight)
select distinct @Date, w.MO, w.ItemNO, w.ItemName, @BC, MachineNO = t1.MachineNO, 0, t1.SocketNum, t1.MouldNO, null, null
from KIT_HopperRecord m
join ChiefMESNew.dbo.MES_WorkOrder w on w.MO = m.MO
join (select distinct MO, MachineNO, SocketNum, MouldNO=null from ChiefMESNew.dbo.V_DispatchOrder ) t1 on t1.MO = w.MO
where m.MO not in(select MO from #t_RptMaterialPutDaily)
and m.HopperTime between @BeginDate and @EndDate

update p set SocketQty=m.GoodSocketNum from #t_RptMaterialPutDaily p join ChiefMESNew.dbo.MES_MouldDetail m on p.MouldNO=m.MouldNO where SocketQty is null
--select * from #t_RptMaterialPutDaily where machineno=‘A13-HF180‘
--更新同班次同产品有工单数 Flag =1 仅用于精细化报表显示 MFlag =1 加料日报显示
update t1 set Flag =1, RdCount = (select COUNT(*) from #t_RptMaterialPutDaily t2 where t1.CurrDate = t2.CurrDate and t1.ItemNO = t2.ItemNO and t1.MESBC = t2.MESBC)
from #t_RptMaterialPutDaily t1

update t1 set MFlag = case when RdCount = 1 then 1 else 0 end ---用于加料日报显示(含合并)
from #t_RptMaterialPutDaily t1


--没有实际零件称重则取工艺净重
update t1 set ProdWeight = t2.NetWeight/1000.0 from #t_RptMaterialPutDaily t1
join ChiefMESNew.dbo.MES_Item t2 on t1.ItemNO = t2.ItemNO
where t1.ProdWeight is null

--没有实际浇口重量则取工艺浇口重量
update t1 set JiaoKouWeight = (select WaterGapScale/1000.0 from ChiefMESNew.dbo.FN_GetItemExtInjection(t1.ItemNO) where ISNULL(WaterGapScale,‘‘) <> ‘‘)
from #t_RptMaterialPutDaily t1
where t1.JiaoKouWeight is null

--获取投料重量
select ID=identity(int),seq=CONVERT(int,null), d.MO, MouldNO = CONVERT(varchar(50),d.MouldNO), 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), m.MachineNO
into #t_Material
from ChiefMESNew.dbo.MES_Material m
join ChiefMESNew.dbo.MES_MaterialPacket p on m.MaterialNO = p.MaterialNO and p.SupNO = m.Supplier
join ( select distinct MO,DispatchNO, MouldNO,MachineNO from ChiefMESNew.dbo.MES_DispatchOrder
) d on m.DispatchNO = d.DispatchNO and m.MachineNO = d.MachineNO
where m.FeedingTime between @BeginDate and @EndDate
group by d.MO,m.MachineNO, d.MouldNO,m.Supplier,m.MaterialNO,p.FeedGrade,p.ItemType
order by d.MO,m.MachineNO, d.MouldNO,p.ItemType,FeedingQty desc,m.MaterialNO

---填补未投料但有机床放本料的原料
insert into #t_Material(MO, Supplier, MaterialNO, FeedGrade, ItemType, FeedingQty)
select distinct k.MO, k.SupplyNO, k.RawNO, p.FeedGrade, p.ItemType, 0
from KIT_MixOver k
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and p.SupNO = k.SupplyNO
where STATE = 0 and k.MixTime between @BeginDate and @EndDate
and not exists(select top 1 * from #t_Material t1 where k.RawNO = t1.MaterialNO and k.SupplyNO = t1.Supplier)

update t set EmpName = e.EmpNameCN, EmpBC = d.DeptName
from #t_Material t
join ChiefMESNew.dbo.MES_Material m on t.mID = m.ID
join ChiefMESNew.dbo.MES_Employee e on m.EmpID = e.EmpID
join ChiefMESNew.dbo.Sys_Department d on e.DeptCode = d.DeptCode and d.DeptName like ‘%塑压%‘

declare @MO varchar(50) ,@seq int , @ItemType varchar(50), @DispatchNO varchar(50), @MouldNO varchar(50)
select @MO = ‘‘,@seq =1,@ItemType = ‘‘,@DispatchNO = ‘‘

update #t_Material
set Seq = @seq , @seq = case when @MO <> MO or (@MO=MO and @MouldNO <> MouldNO ) or @ItemType <> ItemType then 1 else @seq+1 end , @MO=MO, @MouldNO=isnull(MouldNO,‘‘), @ItemType =ItemTYpe

---本料1
update t1 set MaterialNO1 = m.MaterialNO, MaterialName1 = m.FeedGrade, MaterialQty1 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, EmpBC = m.EmpBC, PutMan = m.EmpName, SupplyCode = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘Material‘ and m.Seq = 1

if(@IsFeed =1)
begin
select * from #t_Material where MachineNO like ‘D6%‘
select * from #t_RptMaterialPutDaily where MachineNO like ‘D6%‘
end

---本料2
update t1 set MaterialNO2 = m.MaterialNO, MaterialName2 = m.FeedGrade, MaterialQty2 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, SupplyCode2 = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘Material‘ and m.Seq = 2

---本料3
update t1 set MaterialNO3 = m.MaterialNO, MaterialName3 = m.FeedGrade, MaterialQty3 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, SupplyCode3 = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘Material‘ and m.Seq = 3


---色母
update t1 set SheMuCode = m.MaterialNO, SheMuName = m.FeedGrade, SheMuQty= case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, SheMuSupply = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘Masterbatch‘ and m.Seq = 1

---色母1
update t1 set SheMuCode1 = m.MaterialNO, SheMuName1 = m.FeedGrade, SheMuQty1= case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, SheMu1Supply = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘Masterbatch‘ and m.Seq = 2

--原料总量
update t1 set MaterialSumQty = (select SUM(FeedingQty) from #t_Material where MO = t1.MO and MachineNO = t1.MachineNO)
from #t_RptMaterialPutDaily t1

---回料1
update t1 set CycleNO1 = m.MaterialNO, CycleName1 = m.FeedGrade, CycleQty1 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, EmpBC = m.EmpBC, PutMan = m.EmpName
, Cycle1Supply = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘RecycleMaterial‘ and m.Seq = 1

---回料2
update t1 set CycleNO2 = m.MaterialNO, CycleName2 = m.FeedGrade, CycleQty2 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, Cycle2Supply = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘RecycleMaterial‘ and m.Seq = 2

---回料3
update t1 set CycleNO3 = m.MaterialNO, CycleName3 = m.FeedGrade, CycleQty3 = case when t1.MouldNO = m.MouldNO then m.FeedingQty else 0 end
, Cycle3Supply = m.Supplier
from #t_RptMaterialPutDaily t1
join #t_Material m on t1.MO = m.MO and t1.MachineNO = m.MachineNO and m.ItemType = ‘RecycleMaterial‘ and m.Seq = 3

--机床放本料
update t1 set MachMaterialNO = m1.HH_GNO, MachMaterialName = m1.HH_GNO_DESC, MachMaterialQty = m1.NUM
from #t_RptMaterialPutDaily t1
join (select m.MO,m.MachineNo, NUM = SUM(OverQty),HH_GNO=MAX(RawNO),HH_GNO_DESC=MAX(p.FeedGrade)
from KIT_MOMix m
join KIT_MixOver k on m.mixNo=k.mixNo
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘Material‘,‘Masterbatch‘)
where k.MixTime between @BeginDate and @EndDate
group by m.MO,m.MachineNo
) m1 on t1.MO = m1.MO and t1.MachineNo=m1.MachineNo

create table #MaxMaterial
(MO varchar(50),
Supplier varchar(50),
MaterialNO varchar(50)
)

insert into #MaxMaterial(MO,Supplier,MaterialNO) select a.MO,m.Supplier,m.MaterialNO from ChiefMESNew.dbo.MES_Material m join
(select MO,Max(m.ID) ID from ChiefMESNew.dbo.MES_Material m join ChiefMESNew.dbo.MES_DispatchOrder d on
m.DispatchNO = d.DispatchNO and m.MachineNO = d.MachineNO
join ChiefMESNew.dbo.MES_MaterialPacket p on m.MaterialNO = p.MaterialNO and m.Supplier = p.SupNO
where FeedingTime<@EndDate group by d.MO,p.ItemType) a on m.ID=a.ID

insert into #MaxMaterial(MO,Supplier,MaterialNO) select m.ReceiveMO,k.SupplyNO,k.RawNO
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
where m.RecTime between @BeginDate and @EndDate and k.State = 1
and not exists (select 1 from #MaxMaterial m1 where m1.MO=m.ReceiveMO and m1.MaterialNO=k.RawNO and m1.Supplier=k.SupplyNO)

update t1 set MachMaterialNO = m1.HH_GNO, MachMaterialName = m1.HH_GNO_DESC
from #t_RptMaterialPutDaily t1
join (select m.MO,m.MachineNo, HH_GNO=RawNO,HH_GNO_DESC=p.FeedGrade
from KIT_MOMix m
join KIT_MixOver k on m.mixNo=k.mixNo
join #MaxMaterial k2 on k2.MO=k.MO and k2.Supplier=k.SupplyNO and k2.MaterialNO=k.RawNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘Material‘)
where k.MixTime between @BeginDate and @EndDate
) m1 on t1.MO = m1.MO and t1.MachineNo=m1.MachineNo

--机床放回收料
update t1 set MachCycleNO = HH_GNO, MachCycleName = HH_GNO_DESC, MachCycleQty = m1.NUM
from #t_RptMaterialPutDaily t1
join (select m.MO,m.MachineNo, NUM = SUM(k.OverQty),HH_GNO=MAX(RawNO),HH_GNO_DESC=MAX(p.FeedGrade)
from KIT_MOMix m
join KIT_MixOver k on m.mixNo=k.mixNo
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
where k.MixTime between @BeginDate and @EndDate
and p.ItemType in (‘RecycleMaterial‘)
group by m.MO,m.MachineNo
) m1 on t1.MO=m1.MO and t1.MachineNo=m1.MachineNo

update t1 set MachCycleNO = m1.HH_GNO, MachCycleName = m1.HH_GNO_DESC
from #t_RptMaterialPutDaily t1
join (select m.MO,m.MachineNo, HH_GNO=RawNO,HH_GNO_DESC=p.FeedGrade
from KIT_MOMix m
join KIT_MixOver k on m.mixNo=k.mixNo
join #MaxMaterial k2 on k2.MO=k.MO and k2.Supplier=k.SupplyNO and k2.MaterialNO=k.RawNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘RecycleMaterial‘)
where k.MixTime between @BeginDate and @EndDate
) m1 on t1.MO = m1.MO and t1.MachineNo=m1.MachineNo

--加放本料
update t1 set AddMaterialNO = HH_GNO, AddMaterialName = HH_GNO_DESC, AddMaterialQty = m1.NUM,AddMaterialSupply=m1.AddMaterialSupply
from #t_RptMaterialPutDaily t1
join (select m.ReceiveMO,RecMachineNo, NUM = SUM(OverQty),HH_GNO=MAX(RawNO),HH_GNO_DESC=MAX(p.FeedGrade),AddMaterialSupply=max(k.SupplyNO)
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘Material‘,‘Masterbatch‘)
where m.RecTime between @BeginDate and @EndDate and k.State = 1
group by m.ReceiveMO,RecMachineNo
) m1 on t1.MO = m1.ReceiveMO and t1.MachineNo=m1.RecMachineNo

update t1 set AddMaterialNO = HH_GNO, AddMaterialName = HH_GNO_DESC,AddMaterialSupply=m1.AddMaterialSupply
from #t_RptMaterialPutDaily t1
join (select m.ReceiveMO,RecMachineNo, HH_GNO=RawNO,HH_GNO_DESC=p.FeedGrade,AddMaterialSupply=k.SupplyNO
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join #MaxMaterial k2 on k2.MO=m.ReceiveMO and k2.Supplier=k.SupplyNO and k2.MaterialNO=k.RawNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘Material‘)
where m.RecTime between @BeginDate and @EndDate and k.State = 1
) m1 on t1.MO = m1.ReceiveMO and t1.MachineNo=m1.RecMachineNo

--加放回收料
update t1 set AddCycleNO = isnull(HH_GNO,t1.CycleNO1), AddCycleName = isnull(HH_GNO_DESC,t1.CycleName1), AddCycleQty = m1.NUM,AddCycleSupply=m1.AddCycleSupply
from #t_RptMaterialPutDaily t1
join (select m.ReceiveMO,RecMachineNo, NUM = SUM(OverQty),HH_GNO=MAX(RawNO),HH_GNO_DESC=MAX(p.FeedGrade),AddCycleSupply=MAX(k.SupplyNO)
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘RecycleMaterial‘)
where m.RecTime between @BeginDate and @EndDate and k.STATE = 1
group by m.ReceiveMO,RecMachineNo
) m1 on t1.MO = m1.ReceiveMO and t1.MachineNo=m1.RecMachineNo

update t1 set AddCycleNO = HH_GNO, AddCycleName = HH_GNO_DESC,AddCycleSupply=m1.AddCycleSupply
from #t_RptMaterialPutDaily t1
join (select m.ReceiveMO,RecMachineNo, HH_GNO=RawNO,HH_GNO_DESC=p.FeedGrade,AddCycleSupply=k.SupplyNO
from KIT_MOMix m
join KIT_MixOver k on m.MixNO=k.MixNO
join #MaxMaterial k2 on k2.MO=k.MO and k2.Supplier=k.SupplyNO and k2.MaterialNO=k.RawNO
join ChiefMESNew.dbo.MES_MaterialPacket p on k.RawNO = p.MaterialNO and k.SupplyNO = p.SupNO
and p.ItemType in (‘RecycleMaterial‘)
where m.RecTime between @BeginDate and @EndDate and k.State = 1
) m1 on t1.MO = m1.ReceiveMO and t1.MachineNo=m1.RecMachineNo

declare @lastDate varchar(10), @lastBC varchar(10), @LastBCBegin varchar(19), @LastBCEnd varchar(19)
if(@BC = ‘BC‘)
begin
select @lastDate = @Date, @lastBC = ‘AC‘, @LastBCBegin = @Date+‘ 08:30:00‘, @LastBCEnd = @Date+‘ 20:29:59‘
end
else
begin
select @lastDate = convert(varchar(10),dateadd(day,-1,convert(datetime,@Date)),121), @lastBC = ‘BC‘
select @LastBCBegin = @lastDate+‘ 20:30:00‘, @LastBCEnd = @Date+‘ 08:29:59‘
end

---同一机床 同一物料、模具、原料 换工单的话,加班余料数变为加放本料
declare @cMO varchar(50), @cMouldNO varchar(50),@MachineNO varchar(50), @ItemNO varchar(50)
declare @cJiaoPlusQty decimal(18,3), @cItemNO varchar(50), @cMaterialNO1 varchar(50), @MachMaterialNO varchar(50)
declare @ID int ,@MaterialNO1 varchar(50), @MaterialName1 varchar(100), @AddMaterialNo varchar(50)
declare @AddMaterialName varchar(50), @MachMaterialName varchar(50),@MultiNum int
--declare CursorD2 cursor local
-- For Select MO, MouldNO , MachineNO , MaterialNO1, MaterialName1, ItemNO, isnull(AddMaterialNO,MaterialNo1) , isnull(AddMaterialName, MaterialName1)
-- From #t_RptMaterialPutDaily
--Open CursorD2
--Fetch next From CursorD2 Into @MO , @MouldNO, @MachineNO, @MaterialNO1, @MaterialName1, @ItemNO, @AddMaterialNO, @AddMaterialName
--While(@@Fetch_Status = 0)
--Begin
-- select @ID=-1, @cMO = ‘‘, @cMouldNO =‘‘
-- select top 1 @cMO = MO, @cMouldNO = MouldNO,@cJiaoPlusQty = JiaoPlusQty, @ID = ID
-- , @cItemNO = ItemNO, @cMaterialNO1 = MaterialNo1, @MachMaterialNO=isnull(MachMaterialNO,MaterialNo1), @MachMaterialName = isnull(MachMaterialName,MaterialName1)
-- from WMS_RptMaterialPutDaily
-- where MachineNO = @MachineNO
-- and BCBeginTime < @BeginDate
-- and JiaoPlusQty>0
-- order by BCBeginTime desc

-- --当前班次有生产数但没有投料则取上一个班次的投料信息
-- update t1 set SupplyCode = r.SupplyCode, MaterialNO1 = r.MaterialNO1, MaterialName1 = r.MaterialName1,MaterialNO2 = r.MaterialNO2, MaterialName2 = r.MaterialName2,MaterialNO3 = r.MaterialNO3, MaterialName3 = r.MaterialName3
-- , [CycleNo1] = r.[CycleNo1], [CycleName1] = r.[CycleName1], [CycleNo2] = r.[CycleNo2], [CycleName2] = r.[CycleName2], [CycleNo3] = r.[CycleNo3], [CycleName3] = r.[CycleName3]
-- --,MachMaterialNO = isnull(MachMaterialNO,r.MachMaterialNO), [MachMaterialName] = isnull([MachMaterialName],r.[MachMaterialName],[MachCycleNO] = r.[MachCycleNO],[MachCycleName] = r.[MachCycleName]
-- --, [AddMaterialNO] = r.[AddMaterialNO], [AddMaterialName]= r.[AddMaterialName], [AddCycleNO]= r.[AddCycleNO], [AddCycleName]=r.[AddCycleName]
-- from #t_RptMaterialPutDaily t1
-- join WMS_RptMaterialPutDaily r on r.ID = @ID and r.MO = t1.MO and r.ItemNO = t1.ItemNO
-- where t1.Flag = 1 and t1.MaterialQty1 is null and t1.MO = @MO and t1.MouldNO = @MouldNO and t1.MachineNO = @MachineNO

-- if(@Mo <> @cMO and @cMouldNO = @MouldNO and @cItemNO = @ItemNO and @MaterialNO1 = @cMaterialNO1 and @ID <> -1)
-- begin
-- update #t_RptMaterialPutDaily set AddMaterialNO = isnull(AddMaterialNO,@AddMaterialNO)
-- , AddMaterialName = @AddMaterialName, AddMaterialQty = ISNULL(AddMaterialQty,0)+ISNULL(@cJiaoPlusQty,0)
-- where MO = @MO and MouldNO = @MouldNO and MachineNO = @MachineNO and MaterialNo1 = @MaterialNO1

-- update WMS_RptMaterialPutDaily set MachMaterialNO = ISNULL(MachMaterialNO, @MachMaterialNO)
-- , MachMaterialName = @MachMaterialName, MachMaterialQty = ISNULL(MachMaterialQty,0)+ISNULL(@cJiaoPlusQty,0)
-- , JiaoPlusQty = 0 , ActualQty = 0
-- where ID=@ID

-- end
-- Fetch next From CursorD2 Into @MO , @MouldNO, @MachineNO, @MaterialNO1, @MaterialName1, @ItemNO,@AddMaterialNO, @AddMaterialName
--End
--Close CursorD2
--Deallocate CursorD2


--加料数量=本料数量1+本料数量2+母料数量+回收料数量1+回收料数量2-机床放本料-机床放回收料+加放本料+加放回收料
update t1 set PutQty = isnull(MaterialQty1,0)+isnull(MaterialQty2,0)+isnull(SheMuQty,0)+isnull(SheMuQty1,0)+isnull(CycleQty1,0)+isnull(CycleQty2,0)
-isnull(MachMaterialQty,0)-isnull(MachCycleQty,0)
+isnull(AddMaterialQty,0)+isnull(AddCycleQty,0)
from #t_RptMaterialPutDaily t1



--期初余料数

--期初余料数:如果做的是同一个物料,班次内有换单则为取前一个单的交班余料数,其他情况取上一个班次交班余料数
declare @PreItemNO varchar(50),@ActualTime datetime
--根据单的下发时间来确定单的先后顺序
UPDATE t1 SET BCBeginTime = @BeginDate, ActualTime = (SELECT TOP 1 DispatchTime FROM Chiefmesnew.dbo.V_DispatchOrderTime WHERE DispatchNO= t1.DispatchNo AND MouldNO = t1.MouldNO AND MachineNO = t1.MachineNO and DispatchTime< @EndDate ORDER BY DispatchTime desc)
FROM #t_RptMaterialPutDaily t1

UPDATE t1 SET ActualTime = (SELECT TOP 1 DispatchTime FROM Chiefmesnew.dbo.V_DispatchOrderTime WHERE MO= t1.MO AND MouldNO = t1.MouldNO AND MachineNO = t1.MachineNO and DispatchTime< @EndDate ORDER BY DispatchTime desc)
FROM #t_RptMaterialPutDaily t1
where ActualTime is null

UPDATE t1 SET ActualTime = @EndDate
FROM #t_RptMaterialPutDaily t1
where ActualTime is null

--如果有换单则把期初余料数置为-1
update t1 set TermBeginQty =-1
from #t_RptMaterialPutDaily t1
where Exists( select 1 from #t_RptMaterialPutDaily d
where d.MachineNO = t1.MachineNO group by MachineNO having COUNT(*)>1)

update t1 set TermBeginQty =isnull((select top 1 JiaoPlusQty from WMS_RptMaterialPutDaily r
join (select Max(ActualTime) ActualTime,MAX(BCBeginTime) BCBeginTime,MachineNO from WMS_RptMaterialPutDaily d
where d.BCBeginTime<@BeginDate and datediff(hh,d.BCBeginTime,@BeginDate)<=12
group by MachineNO) r1 on r.MachineNO=r1.MachineNO and r.ActualTime=r1.ActualTime
and r.BCBeginTime=r1.BCBeginTime
where t1.ItemNO = r.ItemNO and t1.MachineNO = r.MachineNO ),0)
from #t_RptMaterialPutDaily t1
where TermBeginQty is null

----如果前一个班次有机床放料则期初余料为0
--update t1 set TermBeginQty = 0
-- from #t_RptMaterialPutDaily t1
-- where TermBeginQty is null and Exists(select top 1 case when JiaoPlusQty<>0 then 0 else 0 end from WMS_RptMaterialPutDaily r
-- where t1.Mo=r.Mo and (t1.ItemNO = r.ItemNO) and t1.MachineNO = r.MachineNO
-- and r.BCBeginTime<@BeginDate and datediff(hh,r.BCBeginTime,@BeginDate)<=12 and r.MachMaterialNO is not null
-- order by r.BCBeginTime desc,r.ActualTime desc)

update t1 set TermBeginQty =isnull((select top 1 JiaoPlusQty from WMS_RptMaterialPutDaily r
join (select Max(ActualTime) ActualTime,MAX(BCBeginTime) BCBeginTime,MachineNO from WMS_RptMaterialPutDaily d
where d.BCBeginTime<@BeginDate and datediff(hh,d.BCBeginTime,@BeginDate)<=12
group by MachineNO) r1 on r.MachineNO=r1.MachineNO and r.ActualTime=r1.ActualTime
and r.BCBeginTime=r1.BCBeginTime
where t1.ItemNO=r.ItemNO and t1.MachineNO = r.MachineNO order by currdate desc),0)
from #t_RptMaterialPutDaily t1 where ActualTime in( select min(ActualTime) from #t_RptMaterialPutDaily d
group by MachineNO having COUNT(*)>1)

--update t1 set TermBeginQty = 0
-- from #t_RptMaterialPutDaily t1
-- where ActualTime in( select min(ActualTime) from #t_RptMaterialPutDaily d
-- group by MachineNO having COUNT(*)>1)
-- and Exists(select top 1 case when JiaoPlusQty<>0 then 0 else 0 end from WMS_RptMaterialPutDaily r
-- where t1.ItemNO=r.ItemNO and (t1.MouldNO = r.ItemNO) and t1.MachineNO = r.MachineNO
-- and r.BCBeginTime<@BeginDate and datediff(hh,r.BCBeginTime,@BeginDate)<=12 and r.MachMaterialNO is not null
-- order by r.BCBeginTime desc,r.ActualTime desc)

--没做盘点的工单 实际消耗:理论消耗

update t1 set ActualQty = ProdQty*(isnull(ProdWeight,0)+isnull(JiaoKouWeight,0)/SocketQty)
from #t_RptMaterialPutDaily t1
where t1.MO not in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and h.MachineNO=t1.MachineNO
)

--- 交班余料数:本班次有做混料盘点则取盘点数
update t1 set JiaoPlusQty = (select top 1 HQty
from KIT_HopperRecord r
where HopperTime between @BeginDate and @EndDate
and t1.MO = r.MO and t1.MachineNo=r.MachineNo
order by HopperTime desc
)
from #t_RptMaterialPutDaily t1

--做盘点的工单 实际消耗:实际消耗=期初余料+加料数量-余料交班数
update t1 set ActualQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(JiaoPlusQty,0)
from #t_RptMaterialPutDaily t1
where t1.MO in (select distinct MO from KIT_HopperRecord h
where h.HopperTime between @BeginDate and @EndDate and h.machineno=t1.machineno
)

--交班余料数 :期初余料+加料数量-生产数*(产品重量+料头重量/模件)
update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-ISNULL(ActualQty,0)
from #t_RptMaterialPutDaily t1
where JiaoPlusQty is null

----机床放料的交班数为0
-- update t1 set JiaoPlusQty = 0
--from #t_RptMaterialPutDaily t1
--where t1.MachMaterialNO is not null
-----同一个工单 同个模次 换了派工单但是不置为0
update #t_RptMaterialPutDaily set JiaoPlusQty=null where TermBeginQty = -1

declare CursorD1 cursor local
For Select MO,ItemNO, MouldNO , MachineNO,MultiNum,DispatchNo, ActualTime
From #t_RptMaterialPutDaily
where TermBeginQty = -1
order by MachineNo, ActualTime
Open CursorD1
Fetch next From CursorD1 Into @MO ,@ItemNO, @MouldNO, @MachineNO,@MultiNum,@DispatchNo,@ActualTime
While(@@Fetch_Status = 0)
Begin
--select top 1 @cMO = MO, @cMouldNO = MouldNO,@cJiaoPlusQty = JiaoPlusQty
-- from #t_RptMaterialPutDaily
-- where MachineNO =@MachineNO and BCBeginTime < @BeginDate
-- order by ID desc

--- 交班余料数:本班次有做混料盘点则取盘点数
update t1 set JiaoPlusQty = (select top 1 HQty
from KIT_HopperRecord r
where HopperTime between @BeginDate and @EndDate
and t1.MO = r.MO and t1.MachineNO=r.MachineNO
order by HopperTime desc
)
from #t_RptMaterialPutDaily t1 where mo=@MO and MachineNO=@MachineNO

--做盘点的工单 实际消耗:实际消耗=期初余料+加料数量-余料交班数
update t1 set ActualQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(JiaoPlusQty,0)
from #t_RptMaterialPutDaily t1
where t1.MO in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
) and mo=@MO and MachineNO=@MachineNO


--交班余料数 :期初余料+加料数量-生产数*(产品重量+料头重量/模件)
update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-ISNULL(ActualQty,0)
from #t_RptMaterialPutDaily t1
where mo=@MO and MachineNO=@MachineNO and t1.MO not in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
)

select top 1 @PreItemNO=ItemNo from #t_RptMaterialPutDaily where MachineNO=@MachineNO and ActualTime<@ActualTime order by ActualTime desc

if (@PreItemNO=@ItemNO)
begin
if (@MultiNum=1)
begin
update t set TermBeginQty=(select top 1 JiaoPlusQty from #t_RptMaterialPutDaily t1 where t1.MachineNO=t.MachineNO and t1.MouldNO=t.MouldNO and t1.MO<>t.MO
and t1.ActualTime<t.ActualTime order by ActualTime desc)
from #t_RptMaterialPutDaily t
where mo=@MO and MachineNO=@MachineNO

update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(ActualQty,0)
from #t_RptMaterialPutDaily t1
where mo=@MO and MachineNO=@MachineNO and t1.MO not in (select distinct MO from KIT_HopperRecord h where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO)


end
else
begin
update t set TermBeginQty=(select top 1 JiaoPlusQty from #t_RptMaterialPutDaily t1 where t1.MachineNO=t.MachineNO and t1.ItemNO=t.ItemNO and t1.MO<>t.MO
and t1.ActualTime<t.ActualTime order by ActualTime desc)
from #t_RptMaterialPutDaily t
where mo=@MO and MachineNO=@MachineNO and dispatchno=@DispatchNo and TermBeginQty in(0,-1)

update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(ActualQty,0)
from #t_RptMaterialPutDaily t1
where mo=@MO and MachineNO=@MachineNO and t1.MO not in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
)

update t set TermBeginQty=(select top 1 JiaoPlusQty from #t_RptMaterialPutDaily t1 where t1.MachineNO=t.MachineNO and t1.ItemNO=t.ItemNO and t1.MO<>t.MO
and t1.ActualTime<t.ActualTime order by ActualTime desc)
from #t_RptMaterialPutDaily t
where mo<>@MO and MachineNO=@MachineNO and dispatchno=@DispatchNo and TermBeginQty in(0,-1)
update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(ActualQty,0)
from #t_RptMaterialPutDaily t1
where mo<>@MO and MachineNO=@MachineNO and DispatchNo=@DispatchNo and t1.MO not in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
)
end
--做盘点的工单 实际消耗:实际消耗=期初余料+加料数量-余料交班数
update t1 set ActualQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(JiaoPlusQty,0)
from #t_RptMaterialPutDaily t1
where t1.MO in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
) and mo=@MO and MachineNO=@MachineNO
--update t set TermBeginQty= 0
--from #t_RptMaterialPutDaily t
--where mo=@MO and MachineNO=@MachineNO and exists(select top 1 case when JiaoPlusQty<>0 then 0 else 0 end from #t_RptMaterialPutDaily t1 where t1.MachineNO=t.MachineNO and t1.MouldNO=t.MouldNO and t1.MO<>t.MO
-- and t1.ActualTime<t.ActualTime and t1.MachMaterialNO is not null)

----机床放料的交班数为0
--update t1 set JiaoPlusQty = 0
--from #t_RptMaterialPutDaily t1
--where t1.MachMaterialNO is not null
--and mo=@MO and MachineNO=@MachineNO
end
else
begin
update t1 set TermBeginQty = 0
from #t_RptMaterialPutDaily t1
where mo=@MO and MachineNO=@MachineNO and TermBeginQty in(0,-1)
update t1 set JiaoPlusQty = isnull(TermBeginQty,0)+isnull(PutQty,0)-isnull(ActualQty,0)
from #t_RptMaterialPutDaily t1
where t1.MO not in (select distinct MO from KIT_HopperRecord h
where HopperTime between @BeginDate and @EndDate and t1.MachineNO=h.MachineNO
) and mo=@MO and MachineNO=@MachineNO
end

Fetch next From CursorD1 Into @MO , @ItemNO,@MouldNO, @MachineNO,@MultiNum,@DispatchNo,@ActualTime
End
Close CursorD1
Deallocate CursorD1
declare @TodayAffactRows int

--色母比例
update t1 set SheMuPercent = case when (ISNULL(MaterialQty1,0)+isnull(MaterialQty2,0)+isnull(MaterialQty3,0)) = 0 then 0 else (ISNULL(SheMuQty,0)+isnull(SheMuQty1,0))/(ISNULL(MaterialQty1,0)+isnull(MaterialQty2,0)+isnull(MaterialQty3,0)) end
from #t_RptMaterialPutDaily t1
update #t_RptMaterialPutDaily set Flag2=0

/********---一模多件的工单分摊加料数量等字段逻辑开始 ********************/
declare @PutQty decimal(18,3), @ActualQty decimal(18,3)
declare @MaterialQty1 decimal(18,3), @MaterialQty2 decimal(18,3), @SheMuQty decimal(18,3), @SheMuQty1 decimal(18,3)
declare @CycleQty1 decimal(18,3), @CycleQty2 decimal(18,3) , @MachMaterialQty decimal(18,3), @MachCycleQty decimal(18,3)
declare @AddMaterialQty decimal(18,3), @AddCycleQty decimal(18,3), @JiaoPlusQty decimal(18,3), @TermBeginQty decimal(18,3)
declare @MaterialNO2 varchar(50), @MaterialName2 varchar(50),@Material2Supply varchar(50)
declare @CycleNO1 varchar(50), @CycleName1 varchar(100),@Cycle1Supply varchar(50), @CycleNO2 varchar(50), @CycleName2 varchar(100),
@Cycle2Supply varchar(50)
declare @SheMuCode varchar(50), @SheMuName varchar(100),@SheMuSupply varchar(50), @SheMuCode1 varchar(50), @SheMuName1 varchar(100)
,@SheMu1Supply varchar(50)
declare @SupplyCode varchar(50)

declare CursorD cursor local
For Select DispatchNO, MultiNum, MachineNO
From #t_RptMaterialPutDaily
where MultiNum>1
Open CursorD
Fetch next From CursorD Into @DispatchNO , @MultiNum, @MachineNO
While(@@Fetch_Status = 0)
Begin
if exists(select DispatchNo,Mo from #t_RptMaterialPutDaily where DispatchNo=@DispatchNO group by DispatchNo,Mo having COUNT(*)>1)
begin
set @MultiNum=@MultiNum*@MultiNum
end

select @PutQty = SUM(PutQty), @MaterialQty1 = SUM(MaterialQty1), @MaterialQty2 = SUM(@MaterialQty2)
, @SheMuQty = SUM(SheMuQty), @SheMuQty1 = SUM(@SheMuQty1), @ActualQty = SUM(ActualQty)
, @CycleQty1 = SUM(CycleQty1), @CycleQty2 = SUM(CycleQty2), @MachMaterialQty = sum(MachMaterialQty)
, @MachCycleQty = SUM(MachCycleQty), @AddMaterialQty = SUM(AddMaterialQty), @AddCycleQty = SUM(AddCycleQty)
, @JiaoPlusQty = SUM(JiaoPlusQty), @TermBeginQty = SUM(TermBeginQty)

,@MaterialNO1 = MAX(MaterialNO1), @MaterialNO2 = MAX(MaterialNO2)
,@MaterialName1= MAX(MaterialName1), @MaterialName2= MAX(MaterialName2)

, @CycleNO1 = MAX(CycleNO1),@Cycle1Supply=max(cycle1supply),@CycleNO2 = MAX(CycleNO2),@Cycle2Supply=max(cycle2supply), @CycleName1= MAX(CycleName1),@CycleName2= MAX(CycleName2)
, @SheMuCode = MAX(SheMuCode),@SheMuSupply=MAX(SheMuSupply), @SheMuCode1 = MAX(SheMuCode1),@SheMu1Supply=MAX(SheMu1Supply), @SheMuName = MAX(SheMuName), @SheMuName1=MAX(SheMuName1)
, @SupplyCode = MAX(SupplyCode),@Material2Supply=MAX(SupplyCode2)
from #t_RptMaterialPutDaily
where DispatchNO = @DispatchNO and MachineNO = @MachineNO

update #t_RptMaterialPutDaily
set PutQty = Convert(decimal(18,3),@PutQty/@MultiNum)

, MaterialQty1 = Convert(decimal(18,3),@MaterialQty1/@MultiNum)

, MaterialQty2 = Convert(decimal(18,3),@MaterialQty2/@MultiNum)

, CycleQty1 = Convert(decimal(18,3),@CycleQty1/@MultiNum), CycleQty2 = Convert(decimal(18,3),@CycleQty2/@MultiNum)
, SheMuQty =Convert(decimal(18,3),@SheMuQty/@MultiNum), SheMuQty1 =Convert(decimal(18,3),@SheMuQty1/@MultiNum)
, MachMaterialQty = Convert(decimal(18,3),@MachMaterialQty/@MultiNum), MachCycleQty = Convert(decimal(18,3),@MachCycleQty/@MultiNum)
, AddMaterialQty = Convert(decimal(18,3),@AddMaterialQty/@MultiNum), AddCycleQty = Convert(decimal(18,3),@AddCycleQty/@MultiNum)
, JiaoPlusQty = Convert(decimal(18,3),@JiaoPlusQty/@MultiNum), TermBeginQty = Convert(decimal(18,3),@TermBeginQty/@MultiNum)
, ActualQty =Convert(decimal(18,3),@ActualQty/@MultiNum)

, MaterialNO1 =@MaterialNO1, MaterialNO2 = @MaterialNO2
, MaterialName1 = @MaterialName1, MaterialName2 = @MaterialName2

, CycleNO1 = @CycleNO1, CycleNO2 = @CycleNO2,Cycle1Supply=@Cycle1Supply,cycle2supply=@Cycle2Supply, CycleName1= @CycleName1, CycleName2 = @CycleName2
, SheMuCode = @SheMuCode, SheMuCode1 = @SheMuCode1,@SheMuSupply=SheMuSupply,SheMu1Supply=@SheMu1Supply, SheMuName = @SheMuName, SheMuName1 = @SheMuName1
, SupplyCode = @SupplyCode,SupplyCode2=@Material2Supply
where DispatchNO = @DispatchNO and MachineNO = @MachineNO and Flag2=0

if exists(select DispatchNo,mo from #t_RptMaterialPutDaily where DispatchNo=@DispatchNO group by DispatchNo,mo having COUNT(*)>1)
begin
update #t_RptMaterialPutDaily set Flag2=1 where DispatchNO = @DispatchNO and MachineNO = @MachineNO
end
Fetch next From CursorD Into @DispatchNO , @MultiNum, @MachineNO
End
Close CursorD
Deallocate CursorD
/********---一模多件的工单分摊加料数量等字段逻辑结束 ********************/

---计算原材料消耗比例,如果没有根据物料与机器取最近的一条,再没有则根据物料最近的一条
UPDATE t1 SET MaterialSumQty = ISNULL(MaterialQty1,0)+ISNULL(MaterialQty2,0)+ISNULL(MaterialQty3,0)+ISNULL(CycleQty1,0)+ISNULL(CycleQty2,0)+ISNULL(CycleQty3,0)+ISNULL(SheMuQty,0)+ISNULL(SheMuQty1,0)
FROM #t_RptMaterialPutDaily t1

UPDATE t1 SET MPercent1 = CONVERT(decimal(10,5),ISNULL(MaterialQty1,0)/MaterialSumQty),MPercent2 = CONVERT(decimal(10,5),ISNULL(MaterialQty2,0)/MaterialSumQty)
, MPercent3 = CONVERT(decimal(10,5),ISNULL(MaterialQty3,0)/MaterialSumQty)
, CPercent1 = CONVERT(decimal(10,5),ISNULL(CycleQty1,0)/MaterialSumQty)
, CPercent2 = CONVERT(decimal(10,5),ISNULL(CycleQty2,0)/MaterialSumQty)
, CPercent3 = CONVERT(decimal(10,5),ISNULL(CycleQty3,0)/MaterialSumQty)
, SPercent1 = CONVERT(decimal(10,5),ISNULL(SheMuQty,0)/MaterialSumQty)
, SPercent2 = CONVERT(decimal(10,5),ISNULL(SheMuQty1,0)/MaterialSumQty)
FROM #t_RptMaterialPutDaily t1 WHERE isnull(MaterialSumQty,0) <> 0

UPDATE t1 SET MaterialSumQty = isnull(AddCycleQty,0)+isnull(AddMaterialQty,0)
FROM #t_RptMaterialPutDaily t1 where MPercent1 is null or CPercent1 is null

UPDATE t1 SET MPercent1 = CONVERT(decimal(10,5),ISNULL(AddMaterialQty,0)/MaterialSumQty),MPercent2 = CONVERT(decimal(10,5),ISNULL(MaterialQty2,0)/MaterialSumQty)
, MPercent3 = CONVERT(decimal(10,5),ISNULL(MaterialQty3,0)/MaterialSumQty)
, CPercent1 = CONVERT(decimal(10,5),ISNULL(AddCycleQty,0)/MaterialSumQty)
, CPercent2 = CONVERT(decimal(10,5),ISNULL(CycleQty2,0)/MaterialSumQty)
, CPercent3 = CONVERT(decimal(10,5),ISNULL(CycleQty3,0)/MaterialSumQty)
, SPercent1 = CONVERT(decimal(10,5),ISNULL(SheMuQty,0)/MaterialSumQty)
, SPercent2 = CONVERT(decimal(10,5),ISNULL(SheMuQty1,0)/MaterialSumQty)
FROM #t_RptMaterialPutDaily t1 WHERE isnull(MaterialSumQty,0) <> 0 and (MPercent1 is null or CPercent1 is null)

--如果当班次有加放料则投料信息取当班次的加放料信息
UPDATE A SET SupplyCode = A.AddMaterialSupply, MaterialNo1 = A.AddMaterialNO,MaterialName1 = A.AddMaterialName,MaterialQty1 = 0,MaterialNo2 = A.MaterialNo2
, MaterialName2 = A.MaterialName2,MaterialQty2 = 0,MaterialNo3 = A.MaterialNo3,MaterialName3 = A.MaterialName3,MaterialQty3 = 0,SheMuCode = A.SheMuCode,SheMuName = A.SheMuName,SheMuQty = 0
, SheMuPercent = A.SheMuPercent,MaterialSumQty = 0,CycleNo1 = A.AddCycleNO,CycleName1 = A.AddCycleName,CycleQty1 = 0,CycleNo2 = A.CycleNo2,CycleName2 = A.CycleName2,CycleQty2 = 0
, CycleNo3 = A.CycleNo3,CycleName3 = A.CycleName3,CycleQty3 = 0
, SheMuCode1 = A.SheMuCode1,SheMuName1 = A.SheMuName1,SheMuQty1 = 0,SheMuPercent1 = A.SheMuPercent1
, SheMuSupply = A.SheMuSupply, SheMu1Supply = A.SheMu1Supply, Cycle1Supply = A.AddCycleSupply,Cycle2Supply = A.Cycle2Supply,Cycle3Supply = A.Cycle3Supply
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM #t_RptMaterialPutDaily A
WHERE A.SupplyCode is null AND A.Cycle1Supply is null and (AddMaterialQty is not null or AddCycleQty is not null)

select a.machineno,a.BCBeginTime,a.ItemNO,a.Actualtime into #WMS_RptMaterialPutDaily2 from WMS_RptMaterialPutDaily A join
(select machineno, max(BCBeginTime) as BCBeginTime, ItemNO FROM WMS_RptMaterialPutDaily WHERE (MPercent1 is not null )
AND BCBEGINTime <= @BEGINDate group by machineno,ItemNO) b
on A.machineno=B.machineno AND A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime

select a.BCBeginTime,a.ItemNO,a.Actualtime into #WMS_RptMaterialPutDaily3 from WMS_RptMaterialPutDaily A join
(select max(BCBeginTime) as BCBeginTime, ItemNO FROM WMS_RptMaterialPutDaily WHERE (MPercent1 is not null )
AND BCBEGINTime <= @BEGINDate group by ItemNO,ActualTime) b
on A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime

UPDATE RM SET MPercent1 = A.MPercent1, MPercent2 = A.MPercent2, MPercent3 = A.MPercent3
, CPercent1 = A.CPercent1, CPercent2 = A.CPercent2, CPercent3 = A.CPercent3
, SPercent1 = A.SPercent1, SPercent2 = A.SPercent2
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM WMS_RptMaterialPutDaily A
INNER JOIN (select machineno, ItemNO,BCBeginTime,max(ActualTime) ActualTime FROM #WMS_RptMaterialPutDaily2
group by machineno, ItemNO,BCBeginTime) b on a.MachineNO=b.MachineNO and a.ItemNO=b.ItemNO
and a.BCBeginTime=b.BCBeginTime and a.ActualTime=b.ActualTime
JOIN #t_RptMaterialPutDaily RM on RM.MACHINENO = A.MachineNO and RM.ItemNO=a.ItemNO WHERE RM.MPercent1 is null

UPDATE RM SET MPercent1 = A.MPercent1, MPercent2 = A.MPercent2, MPercent3 = A.MPercent3
, CPercent1 = A.CPercent1, CPercent2 = A.CPercent2, CPercent3 = A.CPercent3
, SPercent1 = A.SPercent1, SPercent2 = A.SPercent2
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM WMS_RptMaterialPutDaily A
INNER JOIN (select ItemNO,BCBeginTime, max(ActualTime) ActualTime FROM #WMS_RptMaterialPutDaily3
group by ItemNO,BCBeginTime)
b on a.ItemNO=b.ItemNO
and a.BCBeginTime=b.BCBeginTime and a.ActualTime=b.ActualTime
JOIN #t_RptMaterialPutDaily RM on RM.ItemNO=a.ItemNO WHERE RM.MPercent1 is null

select a.machineno,a.BCBeginTime,a.ItemNO,a.Actualtime into #WMS_RptMaterialPutDaily from WMS_RptMaterialPutDaily A join
(select machineno, max(BCBeginTime) as BCBeginTime, ItemNO FROM WMS_RptMaterialPutDaily WHERE (AddMaterialNO is not null OR AddCycleNO is not null) and (MaterialNo1 is null or CycleNo1 is null)
AND BCBEGINTime <= @BEGINDate and datediff(hh,BCBeginTime,@BeginDate)<=12 group by machineno,ItemNO) b
on A.machineno=B.machineno AND A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime

select a.machineno,a.BCBeginTime,a.ItemNO,a.Actualtime into #WMS_RptMaterialPutDaily1 from WMS_RptMaterialPutDaily A join
(select machineno, max(BCBeginTime) as BCBeginTime, ItemNO FROM WMS_RptMaterialPutDaily WHERE (MaterialNo1 is not null OR SheMucode is not null OR CycleNo1 is not null)
AND BCBEGINTime <= @BEGINDate group by machineno,ItemNO) b
on A.machineno=B.machineno AND A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime

--当前班次有生产但没有投料则取上一个班次的加放料信息
UPDATE RM SET SupplyCode = A.AddMaterialSupply, MaterialNo1 = A.AddMaterialNO,MaterialName1 = A.AddMaterialName,MaterialQty1 = 0,MaterialNo2 = A.MaterialNo2
, MaterialName2 = A.MaterialName2,MaterialQty2 = 0,MaterialNo3 = A.MaterialNo3,MaterialName3 = A.MaterialName3,MaterialQty3 = 0,SheMuCode = A.SheMuCode,SheMuName = A.SheMuName,SheMuQty = 0
, SheMuPercent = A.SheMuPercent,MaterialSumQty = 0,CycleNo1 = A.AddCycleNO,CycleName1 = A.AddCycleName,CycleQty1 = 0,CycleNo2 = A.CycleNo2,CycleName2 = A.CycleName2,CycleQty2 = 0
, CycleNo3 = A.CycleNo3,CycleName3 = A.CycleName3,CycleQty3 = 0
, SheMuCode1 = A.SheMuCode1,SheMuName1 = A.SheMuName1,SheMuQty1 = 0,SheMuPercent1 = A.SheMuPercent1
, SheMuSupply = A.SheMuSupply, SheMu1Supply = A.SheMu1Supply, Cycle1Supply = A.AddCycleSupply,Cycle2Supply = A.Cycle2Supply,Cycle3Supply = A.Cycle3Supply
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM WMS_RptMaterialPutDaily A

INNER JOIN (select machineno, max(ActualTime) ActualTime,ItemNO,BCBeginTime from #WMS_RptMaterialPutDaily group by machineno,ItemNO,BCBeginTime) B on A.machineno=B.machineno AND A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime and a.ActualTime=b.ActualTime
JOIN #t_RptMaterialPutDaily RM on RM.MACHINENO = A.MachineNO and RM.ItemNO=a.ItemNO WHERE RM.SupplyCode is null AND RM.SheMuCode is null AND RM.Cycle1Supply is null

--当前班次有生产但没有投料则取上一个班次的投料信息
UPDATE RM SET SupplyCode = A.SupplyCode, MaterialNo1 = A.MaterialNo1,MaterialName1 = A.MaterialName1,MaterialQty1 = 0,MaterialNo2 = A.MaterialNo2
, MaterialName2 = A.MaterialName2,MaterialQty2 = 0,MaterialNo3 = A.MaterialNo3,MaterialName3 = A.MaterialName3,MaterialQty3 = 0,SheMuCode = A.SheMuCode,SheMuName = A.SheMuName,SheMuQty = 0
, SheMuPercent = A.SheMuPercent,MaterialSumQty = 0,CycleNo1 = A.CycleNo1,CycleName1 = A.CycleName1,CycleQty1 = 0,CycleNo2 = A.CycleNo2,CycleName2 = A.CycleName2,CycleQty2 = 0
, CycleNo3 = A.CycleNo3,CycleName3 = A.CycleName3,CycleQty3 = 0
, SheMuCode1 = A.SheMuCode1,SheMuName1 = A.SheMuName1,SheMuQty1 = 0,SheMuPercent1 = A.SheMuPercent1
, SheMuSupply = A.SheMuSupply, SheMu1Supply = A.SheMu1Supply, Cycle1Supply = A.Cycle1Supply,Cycle2Supply = A.Cycle2Supply,Cycle3Supply = A.Cycle3Supply
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM WMS_RptMaterialPutDaily A
INNER JOIN (select machineno, max(ActualTime) ActualTime, ItemNO,BCBeginTime from #WMS_RptMaterialPutDaily1
group by machineno,ItemNO,BCBeginTime) B on A.machineno=B.machineno AND A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime
and a.ActualTime=b.ActualTime
JOIN #t_RptMaterialPutDaily RM on RM.MACHINENO = A.MachineNO and RM.ItemNO=A.ItemNO WHERE RM.SupplyCode is null AND RM.SheMuCode is null AND RM.Cycle1Supply is null

--当前班次有生产但没有投料则取上一个班次的投料信息
UPDATE RM SET SupplyCode = A.SupplyCode, MaterialNo1 = A.MaterialNo1,MaterialName1 = A.MaterialName1,MaterialQty1 = 0,MaterialNo2 = A.MaterialNo2
, MaterialName2 = A.MaterialName2,MaterialQty2 = 0,MaterialNo3 = A.MaterialNo3,MaterialName3 = A.MaterialName3,MaterialQty3 = 0,SheMuCode = A.SheMuCode,SheMuName = A.SheMuName,SheMuQty = 0
, SheMuPercent = A.SheMuPercent,MaterialSumQty = 0,CycleNo1 = A.CycleNo1,CycleName1 = A.CycleName1,CycleQty1 = 0,CycleNo2 = A.CycleNo2,CycleName2 = A.CycleName2,CycleQty2 = 0
, CycleNo3 = A.CycleNo3,CycleName3 = A.CycleName3,CycleQty3 = 0
, SheMuCode1 = A.SheMuCode1,SheMuName1 = A.SheMuName1,SheMuQty1 = 0,SheMuPercent1 = A.SheMuPercent1
, SheMuSupply = A.SheMuSupply, SheMu1Supply = A.SheMu1Supply, Cycle1Supply = A.Cycle1Supply,Cycle2Supply = A.Cycle2Supply,Cycle3Supply = A.Cycle3Supply
, SupplyCode2 = A.SupplyCode2,SupplyCode3 = A.SupplyCode3
FROM WMS_RptMaterialPutDaily A
INNER JOIN (select max(ActualTime) ActualTime, ItemNO,BCBeginTime from #WMS_RptMaterialPutDaily1
group by ItemNO,BCBeginTime) B on A.ItemNO=B.ItemNO and a.BCBeginTime=b.BCBeginTime
and a.ActualTime=b.ActualTime
JOIN #t_RptMaterialPutDaily RM on RM.ItemNO=A.ItemNO WHERE RM.SupplyCode is null AND RM.SheMuCode is null AND RM.Cycle1Supply is null

/**************************************************合并工单逻辑开始 **************************************************/
--按日期、产品等归集产生合并工单
--insert into #t_RptMaterialPutDaily([CurrDate],[MO],[EmpBC],[MESBC],[MachineNO],[ItemNO],[ItemName],[MouldNO],[ProdQty]
-- ,[ProdWeight],[JiaoKouWeight],[SocketNum],[SupplyCode],[MaterialNo1],[MaterialName1],[MaterialQty1],[MaterialNo2]
-- ,[MaterialName2],[MaterialQty2],[MaterialNo3],[MaterialName3],[MaterialQty3],[SheMuCode],[SheMuName],[SheMuQty]
-- ,[SheMuPercent],[MaterialSumQty],[CycleNo1],[CycleName1],[CycleQty1],[CycleNo2],[CycleName2],[CycleQty2]
-- ,[CycleNo3],[CycleName3],[CycleQty3],[MachMaterialNO],[MachMaterialName],[MachMaterialQty]
-- ,[MachCycleNO],[MachCycleName],[MachCycleQty],[AddMaterialNO],[AddMaterialName],[AddMaterialQty]
-- ,[AddCycleNO],[AddCycleName],[AddCycleQty],[PutQty],[PutMan],[TermBeginQty],[JiaoPlusQty],[ActualQty],SupplyCode2,SupplyCode3, Flag, MFlag)
-- select [CurrDate],null,max([EmpBC]),[MESBC],[MachineNO],[ItemNO],max([ItemName]),max([MouldNO]),sum([ProdQty])
-- ,max([ProdWeight]),max([JiaoKouWeight]),max([SocketNum]),max([SupplyCode]),max([MaterialNo1]),max([MaterialName1]),sum([MaterialQty1]),max([MaterialNo2])
-- ,max([MaterialName2]),sum([MaterialQty2]),max([MaterialNo3]),max([MaterialName3]),sum([MaterialQty3]),max([SheMuCode]),max([SheMuName]),sum([SheMuQty])
-- ,max([SheMuPercent]),sum([MaterialSumQty]),max([CycleNo1]),max([CycleName1]),sum([CycleQty1]),max([CycleNo2]),max([CycleName2]),sum([CycleQty2])
-- ,max([CycleNo3]),max([CycleName3]),sum([CycleQty3]),max([MachMaterialNO]),max([MachMaterialName]),sum([MachMaterialQty])
-- ,max([MachCycleNO]),max([MachCycleName]),sum([MachCycleQty]),max([AddMaterialNO]),max([AddMaterialName]),sum([AddMaterialQty])
-- ,max([AddCycleNO]),max([AddCycleName]),sum([AddCycleQty]),sum([PutQty]),max([PutMan]),sum([TermBeginQty]),sum([JiaoPlusQty]),sum([ActualQty]),max(SupplyCode2),max(SupplyCode3), 0, 1
-- from #t_RptMaterialPutDaily
-- where MFlag = 0
-- group by CurrDate, ItemNO, MESBC, MachineNO

-- declare @MOList varchar(8000), @ItemNO varchar(50), @vMESBC varchar(50)

---取消合并工单
-- declare CursorD cursor local
-- For select ItemNO, MESBC
-- from #t_RptMaterialPutDaily b
-- where Flag = 0 and MFlag = 1
--Open CursorD
--Fetch next From CursorD Into @ItemNO,@vMESBC
--While(@@Fetch_Status = 0)
--Begin
-- select @MOList =‘‘
-- select @MOList = case when @MOList = ‘‘ then MO else @MOList + ‘,‘+MO end
-- from #t_RptMaterialPutDaily
-- where ItemNO = @ItemNO and MESBC = @vMESBC
-- and Flag = 1 and RdCount>1
-- update #t_RptMaterialPutDaily set MO = @MOList where ItemNO = @ItemNO and MESBC = @vMESBC and Flag = 0 and MFlag = 1
-- Fetch next From CursorD Into @ItemNO,@vMESBC
--End
--Close CursorD
--Deallocate CursorD
/**************************************************合并工单逻辑结束**************************************************/

--delete #t_RptMaterialPutDaily where ProdQty = 0 and isnull(MaterialQty1,0)=0 and ISNULL(SheMuQty,0)=0 and isnull(CycleQty1,0) = 0
-- and isnull(MachMaterialQty,0) = 0 and PutQty = 0 and TermBeginQty = JiaoPlusQty and ActualQty = 0 and isnull(AddCycleQty,0) = 0
--select * from #t_RptMaterialPutDaily where MachineNO=‘A16-1300-1#‘ and CurrDate=‘2015-07-31‘
insert into WMS_RptMaterialPutDaily([CurrDate],[MO],[EmpBC],[MESBC],[MachineNO],[ItemNO],[ItemName],[MouldNO],[ProdQty]
,[ProdWeight],[JiaoKouWeight],[SocketQty],[SupplyCode],[MaterialNo1],[MaterialName1],[MaterialQty1],[MaterialNo2]
,[MaterialName2],[MaterialQty2],[MaterialNo3],[MaterialName3],[MaterialQty3],[SheMuCode],[SheMuName],[SheMuQty]
,[SheMuPercent],[MaterialSumQty],[CycleNo1],[CycleName1],[CycleQty1],[CycleNo2],[CycleName2],[CycleQty2]
,[CycleNo3],[CycleName3],[CycleQty3],[MachMaterialNO],[MachMaterialName],[MachMaterialQty]
,[SheMuCode1],[SheMuName1],[SheMuQty1],[SheMuPercent1]
,[MachCycleNO],[MachCycleName],[MachCycleQty],AddMaterialSupply,[AddMaterialNO],[AddMaterialName],[AddMaterialQty]
, SheMuSupply, SheMu1Supply, Cycle1Supply,Cycle2Supply,Cycle3Supply
, BCBeginTime, ActualTime,AddCycleSupply,[AddCycleNO],[AddCycleName],[AddCycleQty],[PutQty],[PutMan],[TermBeginQty],[JiaoPlusQty],[ActualQty],SupplyCode2,SupplyCode3,Flag,MFlag,MultiNum,DispatchNo,
MPercent1,MPercent2,MPercent3,CPercent1,CPercent2,CPercent3,SPercent1,SPercent2, GenTime)
select [CurrDate],[MO],[EmpBC],[MESBC],[MachineNO],[ItemNO],[ItemName],[MouldNO],[ProdQty]
,[ProdWeight],[JiaoKouWeight],[SocketQty],[SupplyCode],[MaterialNo1],[MaterialName1],[MaterialQty1],[MaterialNo2]
,[MaterialName2],[MaterialQty2],[MaterialNo3],[MaterialName3],[MaterialQty3],[SheMuCode],[SheMuName],[SheMuQty]
,[SheMuPercent],[MaterialSumQty],[CycleNo1],[CycleName1],[CycleQty1],[CycleNo2],[CycleName2],[CycleQty2]
,[CycleNo3],[CycleName3],[CycleQty3],[MachMaterialNO],[MachMaterialName],[MachMaterialQty]
,[SheMuCode1],[SheMuName1],[SheMuQty1],[SheMuPercent1]
,[MachCycleNO],[MachCycleName],[MachCycleQty],AddMaterialSupply,[AddMaterialNO],[AddMaterialName],[AddMaterialQty]
, SheMuSupply, SheMu1Supply, Cycle1Supply,Cycle2Supply,Cycle3Supply
, BCBeginTime, ActualTime,AddCycleSupply,[AddCycleNO],[AddCycleName],[AddCycleQty],[PutQty],[PutMan],[TermBeginQty],[JiaoPlusQty],[ActualQty],SupplyCode2,SupplyCode3,Flag,MFlag,MultiNum,DispatchNo,
MPercent1,MPercent2,MPercent3,CPercent1,CPercent2,CPercent3,SPercent1,SPercent2,GETDATE()
from #t_RptMaterialPutDaily
where Flag =1
select @TodayAffactRows = @@ROWCOUNT
--select AddMaterialQty, * from WMS_RptMaterialPutDaily where MachineNO like ‘C1%‘ and currdate = ‘2014-12-10‘ and mESBC= ‘BC‘
--if(not exists(select * from WMS_JobDoRpt where JobName = ‘RptMaterialPutDaily‘ and CurrDate = @CurrDate))
--begin
-- insert into WMS_JobDoRpt(JobName,CurrDate,UpdateTime)
-- select ‘RptMaterialPutDaily‘,@CurrDate ,GETDATE()
--end
--else
--begin
-- update WMS_JobDoRpt set Updatetime = getdate() ,LastAffactRows =@TodayAffactRows
-- where JobName = ‘RptMaterialPutDaily‘ and CurrDate = @CurrDate
--end
drop table #t_RptMaterialPutDaily

加料记录日报表的存储过程

标签:

原文地址:http://www.cnblogs.com/chengjun/p/5338129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!