USE [ERPTOOL_JY_20250826Back]
GO

-- 核心物料搜索栏增加物料编码查询条件
-- 需求提出人：罗伟
-- 说明：以下过程补充 @物料编码_check、@物料编码 可选参数。未传入物料编码时保持原查询逻辑不变。
-- 备注：修改人:Ld 修改时间:2026-06-29 14:30:00

ALTER PROCEDURE [dbo].[仓储管理_物料记录_查询新]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null
AS
BEGIN
  declare @selectStr nvarchar(4000)
  declare @selectStrWhere nvarchar(4000)

  set @selectStr =
  'SELECT row_number() OVER (ORDER BY b.记录时间) AS id, d.*, CONVERT(int, isnull(出库数, 0)) AS 出库数, isnull(入库数, 0) AS 入库数, 库存管理_货位主文件.货位名称, 单位名称_基础表.单位, isnull(库存数, 0) AS 库存数, 记录时间
  FROM (SELECT 物料流水号, 库存数, CONVERT(varchar(10), 操作日期, 23) AS 记录时间 FROM 库存管理_每日库存结余表) AS b
  LEFT JOIN (SELECT sum(领料数) AS 出库数, 物料流水号, 领料日期 FROM 仓储管理_出库记录_视图 GROUP BY 物料流水号, 领料日期) AS c ON b.物料流水号 = c.物料流水号 AND c.领料日期 = b.记录时间
  LEFT JOIN (SELECT sum(入库数) AS 入库数, 物料流水号, 入库日期 FROM 仓储管理_入库记录_视图 GROUP BY 物料流水号, 入库日期) AS a ON b.物料流水号 = a.物料流水号 AND a.入库日期 = b.记录时间
  INNER JOIN (SELECT 物料编码, 名称, 代号, 物料流水号, 货位流水号, 单位流水号 FROM 库存管理_物料主文件_基本) AS d ON b.物料流水号 = d.物料流水号
  LEFT JOIN 单位名称_基础表 ON d.单位流水号 = 单位名称_基础表.单位流水号
  LEFT JOIN 库存管理_货位主文件 ON d.货位流水号 = 库存管理_货位主文件.货位流水号 '

  set @selectStrWhere = ' where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and replace(isnull(名称,''''),'' '','''') + replace(isnull(代号,''''),'' '','''') + replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料名称,'' '','''') + ''%'' '

  if(@物料编码_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  if(@时间_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and 记录时间 >= convert(varchar(10),@开始时间,23) and 记录时间 <= convert(varchar(10),@结束时间,23) '

  set @selectStr = @selectStr + @selectStrWhere + ' order by 记录时间 desc'

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime',
    @物料名称,@物料编码,@开始时间,@结束时间
END
GO

ALTER PROCEDURE [dbo].[仓储管理_物料记录_查询]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null
AS
BEGIN
  declare @selectStr nvarchar(4000)

  set @selectStr =
  ' select sum(convert(int,isnull(出库数,0))) as 出库数,sum(isnull(入库数,0)) as 入库数,sum(isnull(库存数,0)) as 库存数 from (select 物料流水号,库存数,convert(varchar(10),操作日期,23) as 记录时间 from 库存管理_每日库存结余表 where 1 = 1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and convert(varchar(10),操作日期,23) >= @开始时间 and convert(varchar(10),操作日期,23) <= @结束时间 '

  set @selectStr = @selectStr +
  ' ) as b left join (select sum(领料数) as 出库数,物料流水号,领料日期 from 仓储管理_出库记录_视图 where 1=1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and 领料日期 >= @开始时间 and 领料日期 <= @结束时间 '

  set @selectStr = @selectStr +
  ' group by 物料流水号,领料日期 ) as c on b.物料流水号 = c.物料流水号 and c.领料日期 = b.记录时间 left join (select sum(入库数) as 入库数,物料流水号,入库日期 from 仓储管理_入库记录_视图 where 1=1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and 入库日期 >= @开始时间 and 入库日期 <= @结束时间 '

  set @selectStr = @selectStr +
  ' group by 物料流水号,入库日期 ) as a on b.物料流水号 = a.物料流水号 and a.入库日期 = b.记录时间
    inner join (select 物料编码,名称,代号,物料流水号,货位流水号,单位流水号 from 库存管理_物料主文件_基本 where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(名称,''''),'' '','''') + replace(isnull(代号,''''),'' '','''') + replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料名称,'' '','''') + ''%'' '

  if(@物料编码_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  set @selectStr = @selectStr +
  ' ) as d on b.物料流水号 = d.物料流水号 left join 单位名称_基础表 on d.单位流水号 = 单位名称_基础表.单位流水号
    left join 库存管理_货位主文件 on d.货位流水号 = 库存管理_货位主文件.货位流水号 '

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime',
    @物料名称,@物料编码,@开始时间,@结束时间
END
GO

ALTER PROCEDURE [dbo].[报表_仓储管理_物料记录_查询]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null
AS
BEGIN
  select '物料记录_' + replace(replace(replace(CONVERT(nvarchar(19),GETDATE(),21),':',''),'-',''),' ','-') as tablename,'15' as tabletype
  select 256*15 as 物料编码,256*30 as 名称,256*30 as 代号,256*10 as 单位,256*15 as 出库数,256*15 as 入库数,256*15 as 库存数,256*15 as 库位,256*15 as 记录日期
  select 0 as 物料编码,0 as 名称,0 as 代号,0 as 单位,1 as 出库数,1 as 入库数,1 as 库存数,0 as 库位,0 as 记录日期

  declare @selectStr nvarchar(4000)

  set @selectStr =
  ' select d.物料编码,名称,代号,单位名称_基础表.单位,isnull(出库数,0) as 出库数,isnull(入库数,0) as 入库数,isnull(库存数,0) as 库存数,库存管理_货位主文件.货位名称 as 库位,记录时间 from (select 物料流水号,库存数,convert(varchar(10),操作日期,23) as 记录时间 from 库存管理_每日库存结余表 where 1 = 1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and convert(varchar(10),操作日期,23) >= @开始时间 and convert(varchar(10),操作日期,23) <= @结束时间 '

  set @selectStr = @selectStr +
  ' ) as b left join (select sum(领料数) as 出库数,物料流水号,领料日期 from 仓储管理_出库记录_视图 where 1=1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and 领料日期 >= @开始时间 and 领料日期 <= @结束时间 '

  set @selectStr = @selectStr +
  ' group by 物料流水号,领料日期 ) as c on b.物料流水号 = c.物料流水号 and c.领料日期 = b.记录时间 left join (select sum(入库数) as 入库数,物料流水号,入库日期 from 仓储管理_入库记录_视图 where 1=1 '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and 入库日期 >= @开始时间 and 入库日期 <= @结束时间 '

  set @selectStr = @selectStr +
  ' group by 物料流水号,入库日期 ) as a on b.物料流水号 = a.物料流水号 and a.入库日期 = b.记录时间
    inner join (select 物料编码,名称,代号,物料流水号,货位流水号,单位流水号 from 库存管理_物料主文件_基本 where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(名称,''''),'' '','''') + replace(isnull(代号,''''),'' '','''') + replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料名称,'' '','''') + ''%'' '

  if(@物料编码_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  set @selectStr = @selectStr +
  ' ) as d on b.物料流水号 = d.物料流水号 left join 单位名称_基础表 on d.单位流水号 = 单位名称_基础表.单位流水号
    left join 库存管理_货位主文件 on d.货位流水号 = 库存管理_货位主文件.货位流水号 '

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime',
    @物料名称,@物料编码,@开始时间,@结束时间
END
GO

ALTER PROCEDURE [dbo].[仓储管理_盘点记录_查询]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null
AS
BEGIN
  declare @selectStr nvarchar(4000)

  set @selectStr = ' select * from 库存管理_盘点记录_视图 where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(名称,''''),'' '','''') + replace(isnull(代号,''''),'' '','''') + replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料名称,'' '','''') + ''%'' '

  if(@物料编码_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and convert(varchar(10),修改时间,23) >= @开始时间 and convert(varchar(10),修改时间,23) <= @结束时间 '

  set @selectStr = @selectStr + ' order by 修改时间 desc '

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime',
    @物料名称,@物料编码,@开始时间,@结束时间
END
GO

ALTER PROCEDURE [dbo].[报表_仓储管理_盘点记录_查询]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null
AS
BEGIN
  select '盘点记录_' + replace(replace(replace(CONVERT(nvarchar(19),GETDATE(),21),':',''),'-',''),' ','-') as tablename,'15' as tabletype
  select 256*15 as 物料编码,256*30 as 名称,256*30 as 代号,256*10 as 单位,256*15 as 原库存,256*15 as 盘点后库存,256*15 as 盘点人,256*25 as 盘点时间
  select 0 as 物料编码,0 as 名称,0 as 代号,0 as 单位,1 as 原库存,1 as 盘点后库存,0 as 盘点人,0 as 盘点时间

  declare @selectStr nvarchar(4000)

  set @selectStr = ' select * from 库存管理_盘点记录_视图 where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(名称,''''),'' '','''') + replace(isnull(代号,''''),'' '','''') + replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料名称,'' '','''') + ''%'' '

  if(@物料编码_check = 1)
    set @selectStr = @selectStr + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  if(@时间_check = 1)
    set @selectStr = @selectStr + ' and convert(varchar(10),修改时间,23) >= @开始时间 and convert(varchar(10),修改时间,23) <= @结束时间 '

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime',
    @物料名称,@物料编码,@开始时间,@结束时间
END
GO

ALTER PROCEDURE [dbo].[仓储管理_入库记录退货_条件查询]
@物料名称_check bit = null,
@物料名称 nvarchar(50) = null,
@采购合同_check bit = null,
@采购合同 nvarchar(50) = null,
@供应商_check bit = null,
@供应商 nvarchar(50) = null,
@物料编码_check bit = null,
@物料编码 nvarchar(50) = null,
@时间_check bit = null,
@开始时间 nvarchar(50) = null,
@结束时间 nvarchar(50) = null
AS
BEGIN
  declare @selectStr nvarchar(4000)
  declare @selectStrWhere nvarchar(4000)

  set @selectStr = ' select * from [dbo].[仓储管理_入库记录退货_视图] '
  set @selectStrWhere = ' where 1 = 1 '

  if(@物料名称_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and 物料名称 + 图号或型号 like ''%'' + @物料名称 + ''%'' '

  if(@采购合同_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and 采购合同 like ''%'' + @采购合同 + ''%'' '

  if(@供应商_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and 供应商 like ''%'' + @供应商 + ''%'' '

  if(@物料编码_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and replace(isnull(物料编码,''''),'' '','''') like ''%'' + replace(@物料编码,'' '','''') + ''%'' '

  if(@时间_check = 1)
    set @selectStrWhere = @selectStrWhere + ' and convert(varchar(10),退货时间,23) >= @开始时间 and convert(varchar(10),退货时间,23) <= @结束时间 '

  set @selectStr = @selectStr + @selectStrWhere + ' order by 退货时间 desc '

  EXEC sp_executesql @selectStr,
    N'@物料名称 nvarchar(50),@采购合同 nvarchar(50),@供应商 nvarchar(50),@物料编码 nvarchar(50),@开始时间 nvarchar(50),@结束时间 nvarchar(50)',
    @物料名称,@采购合同,@供应商,@物料编码,@开始时间,@结束时间
END
GO
