sql:[dbo].[smt_MES_RptProductDaily] 生产日报表

USE [ChangHongMES_904]
GO
/****** Object: StoredProcedure [dbo].[smt_MES_RptProductDaily] Script Date: 09/15/2015 12:26:38

页面在:RptProduct.aspx

******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[smt_MES_RptProductDaily]
(
@StartTime nvarchar(20)='',
@EndTime nvarchar(20)='',
@ItemNo nvarchar(30)='',
@ItemName nvarchar(50)='',
@DisPatchNO nvarchar(20)='',
@MO nvarchar(20)='',
@MachineNO nvarchar(20)='',
@Pagecount int=100,
@Pageindex int=1
)
--2015071005
--MO 工单号
--DispatchNO 派工单号
--MachineNO 机器编号
--ItemNO 产品编号
--DispatchQty 派工数量
--ItemName 产品名称(产品描述)

AS
begin
create table #ProductDaillyTemp
(
ID int identity(1,1),
CurrDate nvarchar(20),
MO nvarchar(20),
DispatchNO nvarchar(30),
MachineNO nvarchar(50),
ItemNo nvarchar(50),
ItemName nvarchar(50),
DispatchQty nvarchar(30), --派工单数量
ProdQty nvarchar(30), --生产数量
YiProdQty nvarchar(30),--已生产数量
RuProdQty nvarchar(30),--入库数量
ChuProdQty nvarchar(30),--出库数量
jiecunProdQty nvarchar(30)--结存数量
)

insert into #ProductDaillyTemp
select
A.CurrDate '生产日期',
A.MO '工单号',
A.DispatchNO '派工单号',
A.MachineNO '机器编号',
A.ItemNo '产品编号',
A.ItemName '产品名称',
max(B.DispatchQty) '派工数量',
sum(A.ProdQty) '生产数量',
(
select sum(rr.ProdQty) '已生产数量'
from dbo.JM_Interface RR
where RR.MO=A.MO
and RR.DispatchNO=A.DispatchNO
and RR.MachineNO=A.MachineNO
and RR.ItemNo=A.ItemNo
and RR.ItemName=A.ItemName
and RR.CurrDate<A.CurrDate
--group by RR.MO,RR.DispatchNO,RR.MachineNO,RR.ItemNo,RR.ItemName
),

'',--入库数量
'',--出库数量
'' --结存数量
from
dbo.JM_Interface A
--left join dbo.MES_DispatchOrder B
left join [dbo].[V_DispatchOrder] B
on A.MO=B.MO
and A.DispatchNO=B.DispatchNO
and A.MachineNO=B.MachineNO
and A.ItemNo=B.ItemNO
where A.MO<>''
group by A.MO,A.DispatchNO,A.MachineNO,A.ItemNo,A.ItemName,A.CurrDate

--------------------------------------------------------------------------
--select 'ProductDaillyTemp 表:'
--select * from #ProductDaillyTemp
--where MO='2015071005'
--order by CurrDate desc

--select * from dbo.JM_Interface where MO='2015071005'
--select DispatchQty,* from dbo.MES_DispatchOrder B where MO='2015071005'
--select A.MO, A.* from dbo.MES_DispatchOrder A where A.MO='2015071005'
-------------------------------------------------------------------------2015071005

--1.统计已经生产数量(没有用到,改为上面的方式统计已生产数量)
create table #YiProductTemp
(
MO nvarchar(20),
DispatchNO nvarchar(30),
MachineNO nvarchar(50),
ItemNo nvarchar(50),
ItemName nvarchar(50),
YiProdQty nvarchar(30)
)
insert into #YiProductTemp
select
A.MO '工单号',
A.DispatchNO '派工单号',
A.MachineNO '机器编号',
A.ItemNo '产品编号',
A.ItemName '产品名称',
sum(A.ProdQty) '已生产数量'
from
dbo.JM_Interface A
group by A.MO,A.DispatchNO,A.MachineNO,A.ItemNo,A.ItemName

--select 'AA'
--select * from #YiProductTemp

---更新到报表中去
--update T set T.YiProdQty=(T.YiProdQty-M.YiProdQty)
--update T set T.YiProdQty=convert(nvarchar(30),convert(float,ISNULL(T.YiProdQty,0))-convert(float,IsNull(M.YiProdQty,0)))
--from #ProductDaillyTemp T join #YiProductTemp M
--on T.MO=M.MO and T.DispatchNO=M.DispatchNO and T.MachineNO=M.MachineNO and T.ItemNo=M.ItemNo and T.ItemName=M.ItemName and T.CurrDate=M.YiProdQty

--2.统计入库数量(dbo.WMS_StockInItem在WMS数据库上 创建视图跨表)

--select A.ItemNO,A.Mo,A.CurrDate,SUM(A.InQty) AllInQty
--into #d from ChangHongWMS_904.dbo.WMS_StockInItem A
--group by A.ItemNO,A.Mo,A.CurrDate
select A.ItemNO,A.Mo,A.CurrDate,SUM(A.InQty) AllInQty
into #d
from
ChangHongWMS_904.dbo.WMS_StockInItem A
join ChangHongWMS_904.dbo.WMS_StockInBill B
on A.BillNO=B.BillNO
where B.Status='appr'--已经审核的
group by A.ItemNO,A.Mo,A.CurrDate
--20150616加按时间

update T
set T.RuProdQty=(select D.AllInQty from #d D where D.ItemNO=T.ItemNo and D.Mo=T.MO and D.CurrDate=T.CurrDate)
from #ProductDaillyTemp T

--3.统计出库数量

--select A.ItemNO,A.Mo,A.CurrDate,SUM(A.OutQty) AllOutQty
--into #Chu from ChangHongWMS_904.dbo.WMS_StockOutItem A
--group by A.ItemNO,A.Mo,A.CurrDate
select B.ItemNO,B.MO,A.[Date],SUM(B.OutQty) AllOutQty
into #Chu
--from ChangHongWMS_904.dbo.WMS_StockOutBill A--货品出库主表
--join ChangHongWMS_904.dbo.WMS_StockOutItem B--货品出库从表

from ChangHongWMS_904.dbo.WMS_StockOutItem B--货品出库主表
left join ChangHongWMS_904.dbo.WMS_StockOutBill A --货品出库从表
on B.BillNO=A.BillNO

where A.Status='appr'
group by B.ItemNO,B.MO,A.[Date]

update T set T.ChuProdQty=(select D.AllOutQty from #Chu D where D.ItemNO=T.ItemNo and D.MO=T.MO and D.[Date]=T.CurrDate)
from #ProductDaillyTemp T

--4.结存数量
update T set T.jiecunProdQty=convert(nvarchar(30),convert(float,ISNULL(T.RuProdQty,0))-convert(float,IsNull(T.ChuProdQty,0)))
from #ProductDaillyTemp T

--select * from #YiProductTemp--已生产表
------------报表生成
--分页
declare @row_from int, @row_to int,@out_total_rows int

select @out_total_rows = count(*) from #ProductDaillyTemp SS

where ((SS.CurrDate between @StartTime and @EndTime) or @StartTime='' or @EndTime='')
and (@ItemNo = '' or SS.ItemNo like '%' +@ItemNo+'%')
and (@ItemName = '' or ss.ItemName like '%' +@ItemName+'%')
and (@DisPatchNO = '' or SS.DispatchNO like '%'+@DisPatchNO+'%' )
and (@MO = '' or ss.MO like '%' +@MO+'%')
and (@MachineNO = '' or SS.MachineNO like '%'+@MachineNO+'%' )

print @out_total_rows
execute st_MES_RS_Pages @out_total_rows, @Pagecount, @Pageindex, @row_from output, @row_to output

select * from
(
select ROW_NUMBER()over(order by SS.CurrDate desc) as numder,SS.*
from #ProductDaillyTemp SS
where ((SS.CurrDate between @StartTime and @EndTime) or @StartTime='' or @EndTime='')
and (@ItemNo = '' or SS.ItemNo like '%' +@ItemNo+'%')
and (@ItemName = '' or ss.ItemName like '%' +@ItemName+'%')
and (@DisPatchNO = '' or SS.DispatchNO like '%'+@DisPatchNO+'%' )
and (@MO = '' or ss.MO like '%' +@MO+'%')
and (@MachineNO = '' or SS.MachineNO like '%'+@MachineNO+'%' )
) HH
where HH.numder between @row_from and @row_to
order by HH.CurrDate desc
--总行数
select @out_total_rows

---产品下面的原材料统计
--select '原材料统计'
--产品所属的原材料临时表
select MA.* into #ProMaterilTemp from
ChangHongMES_904.dbo.MES_BOM MA
where
MA.ParentId in (select CC.ID
From ChangHongMES_904.dbo.MES_BOM CC
join #ProductDaillyTemp MM
on CC.ItemNO=MM.ItemNo
--where CC.ItemNO=@ItemNo or @ItemNo=''
)

--原料表库存表
select AA.RawNO, SUM(AA.MQty) MQty into #YuanLiaoTemp from
ChangHongWMS_904.dbo.KIT_MaterialStock AA
group by AA.RawNO

select distinct AA.ItemNO,AA.ParentId,AA.ParentItemNO,AA.ItemName,
SUBSTRING(convert(nvarchar(50),(AA.Scrap/AA.Qty)*BB.DispatchQty,10),0,8) JIhuaNum,
AA.unit,
CC.MQty,
BB.MO--工单号
from #ProMaterilTemp AA
left join #ProductDaillyTemp BB on AA.ParentItemNO=BB.ItemNo
left join #YuanLiaoTemp CC on BB.ItemNo=CC.RawNO
where
--(AA.ParentItemNO=@ItemNo or @ItemNo='')
(AA.ParentItemNO like '%'+@ItemNo+'%' or @ItemNo='')
and
--(BB.MO=@MO or @MO='')
(BB.MO like '%'+@MO+'%' or @MO='')

--select '产品所属的原材料临时表'
--select AA.ItemNO,AA.ParentId,AA.ParentItemNO,AA.ItemName from #ProMaterilTemp AA order by AA.ParentItemNO

drop table #d
drop table #Chu
drop table #YiProductTemp
drop table #ProductDaillyTemp
end

--select MA.* from
--ChangHongMES_904.dbo.MES_BOM MA
--where MA.ParentId=671

----------------------------------前端

if (CacheHelper.GetCache(Page.User.Identity.Name + "ProductDailyMa") == null)
return;
DataTable dtStock = CacheHelper.GetCache(Page.User.Identity.Name + "ProductDailyMa") as DataTable;
ASPxGridView itemGridView = gr_Main.FindDetailRowTemplateControl(e.VisibleIndex, "detailGrid") as ASPxGridView;
if (itemGridView == null) return;

string itemNo = gr_Main.GetRowValues(e.VisibleIndex, "ItemNo").ToString();
string MO= gr_Main.GetRowValues(e.VisibleIndex, "MO").ToString();
string startDate = string.Empty;
string endDate = string.Empty;
DataTable newdt = new DataTable();
newdt = dtStock.Clone();

DataRow[] dr = dtStock.Select(" ParentItemNO like '%" + itemNo + "%' and MO like '%" +MO+ "%'");
for (int i = 0; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
itemGridView.DataSource = newdt;
itemGridView.DataBind();

上一篇:MYSQL数据库------操作命令笔记


下一篇:Studio-Class Diagram