﻿USE [AIS20240128162426]
GO
/****** Object:  StoredProcedure [dbo].[GetFmaterialDataNew]    Script Date: 2024/4/1 9:36:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[GetFmaterialDataNew] 
(
	@FMaterID INT,
	@FDATE date,
	@FStockId nvarchar(1000)

)
as
begin

set @FStockId=RTRIM(@FStockId)
set @FStockId=LTRIM(@FStockId)
declare @Fprice decimal(28,10)
declare @FNOTE varchar(50)
declare @FAmount decimal(28,10)
declare @FQTY1 decimal(28,10)
EXEC GetPriceNew  @FMaterID ,@FDATE ,@Fprice output ,@FNOTE output,@FAmount output,@FQTY1 output,@FStockId
	
if(@Fprice<>0)
	begin
		select @Fprice as Fprice ,@FNOTE as FNOTE,@FAmount as Amount,@FQTY1 as FQTY
	end
else 
	begin
		SELECT 
			CASE WHEN FPrice = 0 THEN round( C.FCostPrice ,2) 
				ELSE round(C.FPrice ,2) 
			END as Fprice 
		,'取采购入库单单价,单据编号为：' + A.FBILLNO as FNOTE
		,CASE WHEN FPrice = 0 THEN   round(  C.FCostPrice ,2) * b.FREALQTY  
			else  round(  C.FPrice ,2) * b.FREALQTY 
		end as Amount
		,b.FREALQTY as FQTY 
		FROM T_STK_INSTOCK A
			INNER JOIN T_STK_INSTOCKENTRY B ON A.FID = B.FID
			INNER JOIN T_STK_INSTOCKENTRY_F C ON C.FENTRYID = B.FENTRYID
			INNER JOIN T_BD_MATERIAL D ON D.FMATERIALID = B.FMATERIALID
		WHERE
			A.FDOCUMENTSTATUS = 'C'
			AND A.FCANCELSTATUS = 'A'
			AND A.FSTOCKORGID = 101542   
			AND (c.FPrice <> 0 or c.FCostPrice <> 0)
			AND b.FMATERIALID = @FMaterID 
			and b.fstockid = @FStockId
			AND CONVERT(nvarchar(10),A.FDATE,23)  <= CONVERT(nvarchar(10),@FDATE,23)  
		ORDER BY a.FDATE DESC, a .FAPPROVEDATE DESC,b.FSEQ DESC 
	end 
end


	 