-- 入库记录查询关联入库单去重修正
-- 修改人:Ld 修改时间:2026-07-02 16:50:22;

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,
@开始时间 datetime = null,
@结束时间 datetime = null,
@采购合同_check bit = null,
@采购合同流水号	int = null,
@供应商_check bit = null,
@供应商名称 nvarchar(50) = null,
@排序名称 int =null,
@排序方式 int =null,
@入库单号_check bit = null,
@入库单号 varchar(50) = null
 AS

declare @selectStr nvarchar(4000)
declare @selectStrWhere nvarchar(4000)

set @selectStr =
-- 修改人:Ld 修改时间:2026-07-02 16:50:22; 入库单先按采购合同流水号聚合，避免一合同多入库单行导致入库记录重复。
' select A.*,1 as 打印条码数,库存管理_物料主文件_基本.物料编码,采购管理_供应商.供应商编号,货位名称,isnull(累计库存,0) as 库存,入库单去重.入库单号 from (SELECT   *
FROM      仓储管理_采购入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_自制入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_退料入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_通用入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_耗材采购入库记录_视图
UNION ALL
SELECT     *
FROM         仓储管理_备货入库记录_视图) AS A LEFT JOIN
库存管理_物料主文件_基本 ON A.物料流水号 = 库存管理_物料主文件_基本.物料流水号 LEFT JOIN
采购管理_供应商 ON A.供应商流水号 = 采购管理_供应商.供应商流水号 LEFT JOIN
库存管理_货位主文件 ON 库存管理_物料主文件_基本.货位流水号 = 库存管理_货位主文件.货位流水号 LEFT JOIN
库存管理_外购件累计库存 ON 库存管理_物料主文件_基本.物料流水号 = 库存管理_外购件累计库存.物料流水号 LEFT JOIN
(SELECT 采购合同流水号, MAX(入库单号) AS 入库单号 FROM 仓储管理_入库单 GROUP BY 采购合同流水号) AS 入库单去重 ON A.采购合同流水号 = 入库单去重.采购合同流水号  '

set @selectStrWhere = 'where 1 = 1 '

if(@物料名称_check = 1)
	set @selectStrWhere = @selectStrWhere+' and replace(isnull(物料名称,''''),'' '','''') + replace(isnull(图号或型号,''''),'' '','''') + 批次编号   like + ''%'' + replace(@物料名称,'' '','''') + ''%''  '
if(@物料编码_check = 1)
	set @selectStrWhere = @selectStrWhere+' and replace(isnull(库存管理_物料主文件_基本.物料编码,''''),'' '','''') like + ''%'' + replace(@物料编码,'' '','''') + ''%''  '
if(@入库时间_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 入库日期 >=@开始时间 and 入库日期 <=@结束时间 '
if(@订单号_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 订单号 like + ''%'' + @订单号 + ''%'' '
if(@类型_check = 1)
	set @selectStrWhere = @selectStrWhere+' and A.类型 = @类型 '
if(@采购合同_check = 1)
	set @selectStrWhere = @selectStrWhere+' and A.采购合同流水号 = @采购合同流水号 '
if(@供应商_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 供应商 = @供应商名称 '
if(@入库单号_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 入库单号 like + ''%'' + @入库单号 + ''%'' '

if @排序名称 = 1
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 物料编码 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 物料编码 ASC'
		end
end
else if @排序名称 = 2
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 物料名称 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 物料名称 ASC'
		end
end
else if @排序名称 = 3
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 图号或型号 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 图号或型号 ASC'
		end
end
else if @排序名称 = 4
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 货位名称 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 货位名称 ASC'
		end
end
else if @排序名称 = 5
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 批次编号 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 批次编号 ASC'
		end
end
else if @排序名称 = 6
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 供应商编号 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 供应商编号 ASC'
		end
end
else if @排序名称 = 7
begin
  if @排序方式 = 1
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 采购合同编号 DESC'
		end
	else
		begin
			set @selectStr = @selectStr+@selectStrWhere+'order by 采购合同编号 ASC'
		end
end
else
begin
     set @selectStr = @selectStr+@selectStrWhere + ' order by 入库日期 desc '
end

EXEC sp_executesql @selectStr,N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime,@订单号 nvarchar(50),@类型 nvarchar(50),@采购合同流水号 int,@供应商名称 nvarchar(50),@入库单号 nvarchar(50)  ',
@物料名称,@物料编码,@开始时间,@结束时间,@订单号,@类型,@采购合同流水号,@供应商名称,@入库单号
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,
@入库时间_check bit = null,
@开始时间 datetime = null,
@结束时间 datetime = null,
@采购合同_check bit = null,
@采购合同流水号	int = null,
@供应商_check bit = null,
@供应商名称 nvarchar(50) = null
 AS

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*20 as [图号/型号],256*15 as 单位,256*15 as 入库数,256*15 as 库位,246*20 as 入库日期,256*30 as 入库单号,
256*30 as 批次编号,256*15 as 供应商编号,256*30 as 供应商,256*20 as 采购合同,256*30 as 订单号,256*20 as [产品/组件名称],256*20 as 采购备注

select 0 as 类型,0 as 物料编号,0 as 物料名称,0 as [图号/型号],0 as 单位,1 as 入库数,0 as 库位,0 as 入库日期,0 as 入库单号
,0 as 批次编号,0 as 供应商编号,0 as 供应商,0 as 采购合同,0 as 订单号,0 as [产品/组件名称],0 as 采购备注

declare @selectStr nvarchar(4000)
declare @selectStrWhere nvarchar(4000)

set @selectStr =
-- 修改人:Ld 修改时间:2026-07-02 16:50:22; 入库单先按采购合同流水号聚合，保持导出与页面查询去重口径一致。
' select A.类型,库存管理_物料主文件_基本.物料编码  AS 物料编号,A.物料名称,A.图号或型号 AS [图号/型号],A.单位,A.入库数,货位名称,A.入库日期,
A.批次编号,采购管理_供应商.供应商编号,A.供应商,A.采购合同编号 AS 采购合同,A.订单号,A.产品名称 AS [产品/组件名称],A.备注,入库单去重.入库单号 from (SELECT   *
FROM      仓储管理_采购入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_自制入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_退料入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_通用入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_耗材采购入库记录_视图
UNION ALL
SELECT   *
FROM      仓储管理_备货入库记录_视图) AS A LEFT JOIN
库存管理_物料主文件_基本 ON A.物料流水号 = 库存管理_物料主文件_基本.物料流水号 LEFT JOIN
采购管理_供应商 ON A.供应商流水号 = 采购管理_供应商.供应商流水号 LEFT JOIN
库存管理_货位主文件 ON 库存管理_物料主文件_基本.货位流水号 = 库存管理_货位主文件.货位流水号 LEFT JOIN
(SELECT 采购合同流水号, MAX(入库单号) AS 入库单号 FROM 仓储管理_入库单 GROUP BY 采购合同流水号) AS 入库单去重 ON A.采购合同流水号 = 入库单去重.采购合同流水号 '

set @selectStrWhere = 'where 1 = 1 '

if(@物料名称_check = 1)
	set @selectStrWhere = @selectStrWhere+' and replace(isnull(物料名称,''''),'' '','''') + replace(isnull(图号或型号,''''),'' '','''')  like + ''%'' + replace(@物料名称,'' '','''') + ''%''  '
if(@物料编码_check = 1)
	set @selectStrWhere = @selectStrWhere+' and replace(isnull(库存管理_物料主文件_基本.物料编码,''''),'' '','''') like + ''%'' + replace(@物料编码,'' '','''') + ''%''  '
if(@入库时间_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 入库日期 >=@开始时间 and 入库日期 <=@结束时间 '
if(@订单号_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 订单号 like + ''%'' + @订单号 + ''%'' '
if(@类型_check = 1)
	set @selectStrWhere = @selectStrWhere+' and A.类型 = @类型 '
if(@采购合同_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 采购合同流水号 = @采购合同流水号 '
if(@供应商_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 供应商 = @供应商名称 '
if(@入库单号_check = 1)
	set @selectStrWhere = @selectStrWhere+' and 入库单号 like + ''%'' + @入库单号 + ''%'' '

set @selectStr = @selectStr+@selectStrWhere+'order by 入库日期 DESC'

EXEC sp_executesql @selectStr,N'@物料名称 nvarchar(50),@物料编码 nvarchar(50),@开始时间 datetime,@结束时间 datetime,@订单号 nvarchar(50),@类型 nvarchar(50),@采购合同流水号 int,@供应商名称 nvarchar(50),@入库单号 nvarchar(50) ',
@物料名称,@物料编码,@开始时间,@结束时间,@订单号,@类型,@采购合同流水号,@供应商名称,@入库单号
GO
