• 月报表存储过程


    USE [ChiefWMS]
    GO
    /****** Object: StoredProcedure [dbo].[st_WMS_GenRptMaterialInOutMonth] Script Date: 12/28/2015 10:48:28 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER procedure [dbo].[st_WMS_GenRptMaterialInOutMonth]
    (
    @StartDate nvarchar(10)='',
    @EndDate nvarchar(10)='' ,
    @MaterialNO varchar(50) = '',
    @in_pagesize int =200 --每页数量
    ,@in_page int = 1 --页数
    ,@out_total_rows int = 1 output --总数量
    )
    AS
    begin
    if (@StartDate='' and @EndDate='')
    begin
    set @StartDate=CONVERT(varchar(10),dateadd(day,-10,GETDATE()),121)
    set @EndDate=convert(varchar(10),GETDATE(),121)
    end
    --select @StartDate+'---'+@EndDate
    create table #t_Material
    (
    ID int identity(1,1),
    CurrDate varchar(20),--日期
    MaterialNO varchar(50),--物料代码
    MaterialName varchar(100),--物料描述
    SupplyCode varchar(50),--供应商代码
    FeedGrade varchar(50),--材料实际牌号
    MaterialClass varchar(100),--材料类别
    Unit varchar(50),--计量单位
    --SumStockQty decimal(18,3),--合计库存
    Price decimal(18,3),--原料单价

    SumPrice decimal(18,3),--合计金额

    SumFirstStockQty decimal(18,3),--期初结存合计
    FirstStockQty decimal(18,3), --期初结存
    FirstHopperTermQty decimal(18,3),--期初料斗余料
    FirstMixQty decimal(18,3), --期初混料结存

    MonthLinInQty decimal(18,3),--仓库本月领入
    MonthZhuanInQty decimal(18,3), --仓库月累计转入
    ZhunInUnit nvarchar(50),--转入单位
    MonthOutQty decimal(18,3),--仓库月累计发出
    MonthZhuanOutQty decimal(18,3),--仓库月累计转出
    ZhunOutUnit varchar(50),--转出单位
    BZMonthPutQty decimal(18,3),--班组月累计加料

    EndQty decimal(18,3),--期末结存

    HopperTermEndQty decimal(18,3), --料斗期末余料
    TodayMixQty decimal(18,3),--混料结存
    TermEndSumQty decimal(18,3),--期末结存合计
    BenMonthXiaoHao decimal(18,3),--本月实际消耗

    zhangLing nvarchar(10),--账龄
    Reason nvarchar(100),--积压原因
    ChuLi nvarchar(100) --处理意见
    )
    insert into #t_Material(CurrDate,MaterialNO,MaterialName,SupplyCode,FeedGrade,MaterialClass,Unit)

    select distinct @EndDate,MaterialNO,MaterialName,SupNO,FeedGrade,MaterialsClassification,Units
    from ChiefmesNEW.dbo.MES_MaterialPacket

    --原料单价
    select a.* into #Price from
    WMS_PriceDetail a,(select ProductNo,max(id) as id from WMS_PriceDetail group by ProductNo) b
    where a.ProductNo=b.ProductNo and a.id=b.id

    update d set d.Price=p.Price
    from #t_Material d join #Price p on d.MaterialNO=p.ProductNo

    ---期初结存合计=开始时间的上一天的合计结存(WMS_RptMaterialInOutDaily的SumStockQty)
    declare @ReStartDate nvarchar(10)=CONVERT(varchar(10),dateadd(day,-1,@StartDate),121)

    update d set d.SumFirstStockQty=p.SumStockQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@ReStartDate

    --期初结存=开始时间的上一天本日结存(WMS_RptMaterialInOutDaily的TodayStockQty)+班组本日结存

    update d set d.FirstStockQty=(p.TodayStockQty+p.BZTodayStock)
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@ReStartDate

    --期初料斗余料=开始时间的上一天(料斗期末余料)HopperTermEndQty

    update d set d.FirstHopperTermQty=p.HopperTermEndQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@ReStartDate

    --期初混料结存=开始时间的上一天(混料结存) TodayMixQty

    update d set d.FirstMixQty=p.TodayMixQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@ReStartDate


    --add by CJ 2015-12-23 期初结存合计=期初结存+期初料斗余料
    update d set d.SumFirstStockQty=d.FirstStockQty+d.FirstHopperTermQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@ReStartDate


    --仓库本月领入 MonthLinInQty
    update t1 set t1.MonthLinInQty=isnull((select SUM(case when unit='T' then m.MQty*1000 when unit='g' then m.MQty/1000 else m.MQty end)
    from KIT_MaterialStockIn m
    where
    m.MQty>0
    and charindex('转入',isnull(m.StateDesc,''))=0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO
    and m.SupplyNO = t1.SupplyCode
    ),0)
    -isnull((select SUM(case when unit='T' then m.GQty*1000 when unit='g' then m.GQty/1000 else m.GQty end)
    from KIT_MaterialStockIn m
    where
    m.GQty>0
    and state=3
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO
    and m.SupplyNO = t1.SupplyCode
    ),0)
    from #t_Material t1

    -- 仓库月累计转入 MonthZhuanInQty
    update t1 set MonthZhuanInQty =(select SUM(case when unit='T' then m.MQty*1000 when unit='g' then m.MQty/1000 else m.MQty end)
    from KIT_MaterialStockIn m
    where m.MQty>0
    and charindex('转入',isnull(m.StateDesc,''))>0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO
    and m.SupplyNO = t1.SupplyCode
    )
    from #t_Material t1

    --转入单位 ZhunInUnit
    update t1 set ZhunInUnit=(select max(Remark)
    from KIT_MaterialStockIn m
    where m.MQty>0 and charindex('转入',isnull(m.StateDesc,''))>0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO
    and m.SupplyNO = t1.SupplyCode
    )
    from #t_Material t1

    --MonthOutQty --仓库月累计发出
    update t1 set MonthOutQty =(select SUM(isnull(case when unit='T' then m.GQty*1000 when unit='g' then m.GQty/1000 else m.GQty end,0)-
    ISNULL(case when unit='T' then m.RQty*1000 when unit='g' then m.RQty/1000 else m.RQty end,0))
    from KIT_MaterialStockIn m
    where
    (m.GQty>0 or m.RQty>0)
    and charindex('转移',isnull(m.StateDesc,''))=0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO
    and m.SupplyNO = t1.SupplyCode
    and State<>3
    )
    from #t_Material t1

    --仓库月累计转出 MonthZhuanOutQty
    update t1 set MonthZhuanOutQty =(select SUM(case when unit='T' then m.GQty*1000 when unit='g' then m.GQty/1000 else m.GQty end)
    from KIT_MaterialStockIn m
    where m.GQty>0 and charindex('转移',isnull(m.StateDesc,''))>0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO and m.SupplyNO = t1.SupplyCode
    and State<>3)
    from #t_Material t1

    --转出单位 ZhunOutUnit
    update t1 set ZhunOutUnit =(select max(m.Remark)
    from KIT_MaterialStockIn m
    where m.GQty>0 and charindex('转移',isnull(m.StateDesc,''))>0
    and convert(varchar(10),isnull(m.CheckTime,m.CreateTime),121) between @StartDate and @EndDate
    and m.RawNO = t1.MaterialNO and m.SupplyNO = t1.SupplyCode
    )
    from #t_Material t1


    --班组月累计加料 BZMonthPutQty
    update t1 set BZMonthPutQty =(select SUM(m.feedingQty)
    from ChiefmesNEW.dbo.MES_Material m
    where
    CONVERT(varchar(10),m.feedingTime,121) between @StartDate and @EndDate
    and m.MaterialNO = t1.MaterialNO
    and m.supplier = t1.SupplyCode
    )
    from #t_Material t1

    --期末(本料)结存EndQty=期初结存+仓库本月领入+仓库月累计转入-仓库月累计转出-班组月累计加料
    update #t_Material
    set EndQty=isnull(FirstStockQty,0)+ISNULL(MonthLinInQty,0)+ISNULL(MonthZhuanInQty,0)-ISNULL(MonthZhuanOutQty,0)-ISNULL(BZMonthPutQty,0)


    -- 料斗期末余料 HopperTermEndQty= WMS_RptMaterialInOutDaily表的料斗期末余料
    update d set d.HopperTermEndQty=p.HopperTermEndQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@EndDate

    -- 混料结存 TodayMixQty
    update d set d.TodayMixQty=p.TodayMixQty
    from #t_Material d join WMS_RptMaterialInOutDaily p on d.MaterialNO=p.MaterialNO and d.SupplyCode=p.SupplyNO
    where p.CurrDate=@EndDate

    -- 期末结存合计 TermEndSumQty=期末本料结存+料斗期末余料+混料结存
    update #t_Material
    set TermEndSumQty=ISNULL(EndQty,0)+ISNULL(HopperTermEndQty,0)+ISNULL(TodayMixQty,0)

    -- 本月实际消耗 BenMonthXiaoHao =期初料斗余料+班组月累计加料-料斗期末余料-混料结存
    update #t_Material
    set BenMonthXiaoHao=ISNULL(FirstHopperTermQty,0)+ISNULL(BZMonthPutQty,0)-ISNULL(HopperTermEndQty,0)-ISNULL(TodayMixQty,0)


    --合计金额 SumPrice
    update #t_Material
    set SumPrice=ISNULL(TermEndSumQty,0)*Price


    -- 账龄
    update t1
    set t1.zhangLing=datediff(day,(select top 1 m.CreateTime
    from KIT_MaterialStockIn m
    where m.MQty>0 and m.RawNO = t1.MaterialNO and m.SupplyNO = t1.SupplyCode
    order by m.CreateTime desc),getdate())
    from #t_Material t1



    select ID= identity(int),CurrDate,MaterialNO,MaterialName,MaterialClass,Unit,Price,FeedGrade,
    SUM(isNull(SumPrice,0)) SumPrice,
    SUM(isNull(SumFirstStockQty,0)) SumFirstStockQty,
    SUM(isNull(FirstStockQty,0)) FirstStockQty,
    SUM(isNull(FirstHopperTermQty,0)) FirstHopperTermQty,
    SUM(isNull(FirstMixQty,0)) FirstMixQty,

    SUM(isNull(MonthLinInQty,0)) MonthLinInQty,
    SUM(isNull(MonthZhuanInQty,0)) MonthZhuanInQty,

    SUM(isNull(MonthOutQty,0)) MonthOutQty,
    SUM(isNull(MonthZhuanOutQty,0)) MonthZhuanOutQty,

    SUM(isNull(BZMonthPutQty,0)) BZMonthPutQty,
    SUM(isNull(EndQty,0)) EndQty,

    SUM(isNull(HopperTermEndQty,0)) HopperTermEndQty,
    SUM(isNull(TodayMixQty,0)) TodayMixQty,
    SUM(isNull(TermEndSumQty,0)) TermEndSumQty,
    sum(isNull(BenMonthXiaoHao,0)) BenMonthXiaoHao
    ,
    '' zhangLing,
    '' Reason,
    '' ChuLi,
    '' SupplyCode,
    '' ZhunInUnit,
    '' ZhunOutUnit

    into #Temp

    from #t_Material A
    where
    (@MaterialNO = '' or (MaterialNO like '%' +@MaterialNO+'%' or MaterialName like '%' +@MaterialNO+'%'))
    and CONVERT(nvarchar(10),SumPrice)<>'0.000'
    group by CurrDate,MaterialNO,MaterialName,MaterialClass,Unit,Price,FeedGrade
    order by A.CurrDate,A.MaterialNO

    --SumPrice decimal(18,3),--合计金额

    --SumFirstStockQty decimal(18,3),--期初结存合计

    --FirstStockQty decimal(18,3), --期初结存
    --FirstHopperTermQty decimal(18,3),--期初料斗余料
    --FirstMixQty decimal(18,3), --期初混料结存

    --MonthLinInQty decimal(18,3),--仓库本月领入
    --MonthZhuanInQty decimal(18,3), --仓库月累计转入
    --ZhunInUnit nvarchar(50),--转入单位
    --MonthOutQty decimal(18,3),--仓库月累计发出
    --MonthZhuanOutQty decimal(18,3),--仓库月累计转出

    --ZhunOutUnit varchar(50),--转出单位
    --BZMonthPutQty decimal(18,3),--班组月累计加料

    -- EndQty decimal(18,3),--期末结存

    -- HopperTermEndQty decimal(18,3), --料斗期末余料
    -- TodayMixQty decimal(18,3),--混料结存
    -- TermEndSumQty decimal(18,3),--期末结存合计
    -- BenMonthXiaoHao decimal(18,3),--本月实际消耗

    -- zhangLing nvarchar(10),--账龄
    -- Reason nvarchar(100),--积压原因
    -- ChuLi nvarchar(100) --处理意见


    --/* pages */

    declare @row_from int, @row_to int
    select @out_total_rows = count(*) from #Temp
    execute st_MES_RS_Pages @out_total_rows, @in_pagesize, @in_page, @row_from output, @row_to output

    select * from #Temp
    where ID between @row_from and @row_to
    order by ID
    select @out_total_rows


    drop table #Temp
    drop table #t_Material
    end
    -----------------------------------------------------------------------
    --select top 10 MaterialNO from WMS_RptMaterialInOutDaily
    --where MaterialNO='810035519'
    --group by MaterialNO
    --order by MaterialNO desc

    --810035519
    --select * from ChiefmesNEW.dbo.MES_MaterialPacket where MaterialNO like '%810575564%'

    --select distinct object_name(id) from syscomments
    --where
    --id in (select object_id from sys.objects where type ='P')
    --and text like '%SUM(case when unit=''T'' then m.MQty*1000 when unit=''g'' then m.MQty/1000 else m.MQty end)%'

    --st_WMS_JobGenRptMaterialInOut

    --and SumPrice is not null

    --and SumFirstStockQty not in (0.000)

    --select * from ChiefmesNEW.dbo.MES_MaterialPacket

    --update D
    --set D.BuyProp=(select * from ChiefmesNEW.dbo.MES_MaterialPacket s where s.MaterialNO=D.MaterialName for )
    --from ChiefmesNEW.dbo.MES_MaterialPacket D

    --SELECT ','+ convert(varchar(10),m.InQty) FROM WMS_StockInItem m WHERE m.billno='GD20151124001' FOR XML PATH('')

    --SELECT BillNO FROM WMS_StockInItem group by BillNO having COUNT(1)>1

  • 相关阅读:
    TWinControl、TCustomControl和TGraphicControl对WM_PAINT消息的三种不同处理(虚函数的特点就是升升降降)
    VCL里的构造函数
    从良难
    TTimer源码研究
    Delphi的RTTI(许多参考链接)
    对ShortCut和TWMKey的研究
    TTimer很特殊
    TEdit的创建与显示过程
    VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management
    Delphi Math里的基本函数,以及浮点数比较函数
  • 原文地址:https://www.cnblogs.com/chengjun/p/5085914.html
Copyright © 2020-2023  润新知