CREATE PROCEDURE [dbo].[CheckMandeh10000] @TmpId Bigint
AS
BEGIN
 DECLARE @TmpHeadh as Table (Id_Havaleh  numeric(18, 0) , Kind_Flag CHAR(1) ,Tot_Price_forosh MONEY )
 INSERT INTO @TmpHeadh ( Id_Havaleh   , Kind_Flag  , Tot_Price_forosh )
 SELECT Id_Havaleh , Kind_Flag  , Tot_Price_forosh FROM dbo.TmpHeadH WHERE Id_Havaleh = @Tmpid
 
 DECLARE @TmpHavaleh as Table ( Id_Havaleh  numeric(18, 0) , Sender VARCHAR(8) , Reciver  VARCHAR(8)) 
 INSERT INTO @TmpHavaleh ( Id_Havaleh , Sender, Reciver)
 SELECT Id_Havaleh  , Sender, Reciver  FROM dbo.TmpHavaleh WHERE Id_Havaleh = @Tmpid
 
 DECLARE @Tmp_Over_Under_Factor as Table (Id_Havaleh  numeric(18, 0) , Type CHAR(1) ,Price MONEY, Guy_Code VARCHAR(5) )
 INSERT INTO @Tmp_Over_Under_Factor ( Id_Havaleh   , Type  , Price, Guy_Code )
 SELECT Id_Havaleh , Type  , Price, Guy_Code FROM Tmp_Over_Under_Factor WHERE Id_Havaleh = @Tmpid

IF Exists(
          Select TOP 1 Reciver From @TmpHavaleh TmpHavaleh  Where LEN(Sender) = 3 And Reciver  = '10000'
          And TmpHavaleh.Id_Havaleh = @TmpId
         )
Begin
	Declare @IdTmpHavaleh Bigint
	Declare @PriceTmpCheqe Money
	Declare @SumFactor Money
	Declare @PriceOverUnder Money
	Declare @IdSanad Bigint
	Declare @AccCode10000 Varchar(20)
	Declare @UserId Smallint
	Declare @TarikhBargeh DateTime
	Declare @MaxNu  Bigint
	Declare @IdCheqe Bigint
	Declare @Price Money

	Select @AccCode10000 = Acc_Code From Acc_link 
	Where Code = '10000' And Acc_Parent_Code 
	in(Select Acc_Code From classify Where Autoid in(20,21))

	Update Tmp_Over_Under_Factor Set Guy_Code = '10000' From @Tmp_Over_Under_Factor Tmp_Over_Under_Factor
	Inner Join @TmpHeadH TmpHeadH ON TmpHeadH.Id_Havaleh = Tmp_Over_Under_Factor.Id_Havaleh
	Inner Join @TmpHavaleh TmpHavaleh ON TmpHavaleh.Id_Havaleh = TmpHeadH.Id_Havaleh
	Where TmpHeadH.Id_Havaleh = @TmpId 
	And Guy_Code <> '10000'
	And Reciver = '10000' 
	And Kind_Flag = 7

	-----------------------------------------------------------
	Declare Cursor_Select Cursor For
	Select Id_Havaleh , SumForosh , Ch_Price,  SumOverUnder From (
	Select T1.Id_Havaleh, SumForosh , IsNull(T3.Ch_Price,0) Ch_Price, IsNull(T2.SumOverUnder,0) SumOverUnder From (
	Select TmpHeadH.Id_Havaleh, Tot_Price_forosh SumForosh 
	From @TmpHavaleh TmpHavaleh INNER Join @TmpHeadH TmpHeadH 
	on TmpHeadH.Id_Havaleh  = TmpHavaleh.Id_Havaleh
	Where Kind_Flag = 7 And Reciver  = '10000'
	And TmpHeadH.Id_Havaleh = @TmpId
	Group By TmpHeadH.Id_Havaleh, Reciver, Tot_Price_forosh
	) As T1 
	Left Outer Join 
	(
	 Select Id_Havaleh, 
	 Sum(Case When Type = '1' Then Round(Price,0) Else 0 End) - 
	 Sum(Case When Type = '0' Then Round(Price,0) Else 0 End) SumOverUnder
	 From @Tmp_Over_Under_Factor
	 Where Id_Havaleh = @TmpId
	Group By Id_Havaleh
	 
	) As T2 
	On T1.Id_Havaleh = T2.Id_Havaleh
	Left Outer Join 
	(
	Select Id_Havaleh , Round(Ch_Price,0) Ch_Price From TmpHeadC 
	Inner Join TmpCheqe on TmpCheqe.Id_Cheqe = TmpHeadC.Id_Cheqe
	Where Id_Havaleh = @TmpId And Ch_No is Null
	) As T3
	on T1.Id_Havaleh = T3.Id_Havaleh
	) As TotalDiffer
	Where SumForosh <> Ch_Price - SumOverUnder
	-----------------------------------------------------------
	Open Cursor_Select
	Fetch Next FRom Cursor_Select Into @IdTmpHavaleh, @SumFactor,@PriceTmpCheqe , @PriceOverUnder
	While @@FETCH_STATUS = 0 
	Begin
	  /*******************************************************************/
	  IF ((@PriceTmpCheqe = 0 And @PriceOverUnder = 0)  Or 
		  (@PriceTmpCheqe = 0 And @PriceOverUnder <> 0))
	  Begin

		IF (@PriceTmpCheqe = 0 And @PriceOverUnder = 0)
		  Set @Price = @SumFactor
		Else IF (@PriceTmpCheqe = 0 And @PriceOverUnder <> 0)
		  Set @Price = @SumFactor + @PriceOverUnder
	      
		Select @TarikhBargeh = Tarikh_Faktor, 
			   @UserId = User_Id FROM TmpHeadH WHERE Id_Havaleh = @IdTmpHavaleh

		Select @IdSanad = Id_Sanad From Sanad 
						  Where Nu_Havaleh = @IdTmpHavaleh
						  Group By Id_Sanad
	   
		Select @MaxNu = Max(Id_Manual) + 1 From HeadC 
	    
		Delete From TmpHeadC Where Id_Havaleh = @Tmpid
	    
		Insert Into TmpHeadC (Id_Havaleh, Id_Sanad , Id_Manual , Kind_Flag, TarikhBargeh, Note, 	Guy_Code, Tarikh, User_Id, Guy_Bank, AccCode)
				   Values (@IdTmpHavaleh, @Idsanad, @MaxNu, '0', @TarikhBargeh, '   	', '10000', @TarikhBargeh, @UserId, '10000', @AccCode10000)
		Select @IdCheqe = SCOPE_IDENTITY()

		Insert Into TmpCheqe(Id_Cheqe, Ch_Tarikh, Ch_Price, Ch_Note, Ch_Pass, Acc_Code, Act_Code, 	Ch_Bank, Tmp_Main_Auto_Sanad)
				   Values(@IdCheqe, @TarikhBargeh, @Price, '   ', 10,'0','11','0','0')
	  End
	  /*******************************************************************/
	  Else IF (@PriceTmpCheqe <> 0 And @PriceOverUnder = 0 )
	  Begin
		Update TmpCheqe Set Ch_Price = @SumFactor 
		Where Id_Cheqe = (Select Id_Cheqe From TmpHeadC Where Id_Havaleh = @IdTmpHavaleh)
	  End
	  /*******************************************************************/
	  Else IF (@PriceTmpCheqe <> 0 And @PriceOverUnder <> 0 )
	  Begin
		Update TmpCheqe Set Ch_Price = @SumFactor + @PriceOverUnder
		Where Id_Cheqe = (Select Id_Cheqe From TmpHeadC Where Id_Havaleh = @IdTmpHavaleh)
	  End
	  /*******************************************************************/
	  Fetch Next From Cursor_Select Into @IdTmpHavaleh, @SumFactor,@PriceTmpCheqe , @PriceOverUnder
	end
	Close Cursor_Select
	Deallocate Cursor_Select
	END
End


