/****** Object:  StoredProcedure [dbo].[CheckSellFactorTruckDupplicate]    Script Date: 26/10/2024 11:36:54 ق.ظ ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


 ALTER PROCEDURE [dbo].[CheckSellFactorTruckDupplicate]
	@ExID as BigInt,
	@TruckDate as DateTime,
	@TruckNo as NVARCHAR(50),
	@Weight as decimal,
	@Pack as int,
	@Barname as NVARCHAR(50),
	@Serial as NVARCHAR(50),
	@RESULT int OUTPUT
AS
BEGIN
	DECLARE @ID as BIGINT
	DECLARE @Filter as NVARCHAR(max)
	DECLARE @FLAGS as NVARCHAR(1024)
	SET @Filter='ID <>' + STR(@ExID)
	select @FLAGS=TheValue from AppSetting where Name = 'Company.SellFactor.TrucksUniqueFlags'
	if @FLAGS='YesNo' or @FLAGS='' 
	BEGIN
		SET @RESULT=0
		return
	END
	--YesNo, Date, TruckNo, Weight, Pack, Barname, Serial
	--print 'XX'+ Str(CHARINDEX('TruckNo',@FLAGS))
	--print 'XX'+ @FLAGS
	if CHARINDEX('Date',@FLAGS) > 0
		set @Filter=@Filter+' AND [Date]= ''' + Cast(@TruckDate as nvarchar(20)) + ''''
	if CHARINDEX('TruckNo',@FLAGS) > 0
		set @Filter=@Filter+' AND TruckNo= N''' + @TruckNo + ''''
	if CHARINDEX('Weight',@FLAGS) > 0
		set @Filter=@Filter+' AND Weigh= ' + Cast(@Weight as nvarchar(20))
	if CHARINDEX('Pack',@FLAGS) > 0
		set @Filter=@Filter+' AND Pack= ' + Cast(@Pack as nvarchar(20))
	if CHARINDEX('Barname',@FLAGS) > 0
		set @Filter=@Filter+' AND Barname= N''' + @Barname + ''''
	if CHARINDEX('Serial',@FLAGS) > 0
		set @Filter=@Filter+' AND BarnameSerial= N''' + @Serial + ''''
	--SET @Filter=SUBSTRING(@Filter,5,Len(@Filter)-5)
	
	DECLARE @rowcount int
	SET @rowcount=0
	if LEN(@Filter)>5 
	BEGIN
		set @Filter = N'Select @rowcount=Count(ID) from AccSellFactorTrucks with(nolock) where ' + @Filter
		--print @Filter
		exec sp_executesql  @Filter, N'@rowcount int output', @rowcount output;
	END
	--print 'Select Count(*) from AccSellFactorTrucks with(nolock) where' + @Filter
	set @RESULT=@rowcount
END