CREATE PROCEDURE [dbo].[CheckMandeh10000] @TmpId Bigint
AS
BEGIN
Declare @IdTmpHavaleh Bigint
Declare @PriceTmpCheqe Money
Declare @SumFactor Money
Declare @PriceOverUnder Money
Declare @IdSanad Bigint
Declare @AccCode10000 Varchar(20)
Declare @UserId Smallint


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
Inner Join TmpHeadH on TmpHeadH.Id_Havaleh = Tmp_Over_Under_Factor.Id_Havaleh
Inner Join TmpHavaleh on TmpHavaleh.Id_Havaleh = TmpHeadH.Id_Havaleh
Where TmpHeadH.Id_Havaleh = @TmpId 
And Guy_Code <> '10000'
And Reciver = '10000' 
And Kind_Flag in('7','5','9')


-----------------------------------------------------------
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, Sum(Round(K_Qty1 * Price_Forosh,0)) SumForosh 
From TmpHavaleh Inner Join TmpHeadH 
on TmpHeadH.Id_Havaleh  = TmpHavaleh.Id_Havaleh
Where Kind_Flag in('7','5','9') And Reciver  = '10000'
And TmpHeadH.Id_Havaleh = @TmpId
Group By TmpHeadH.Id_Havaleh
) As T1 
Left Outer Join 
(
Select Id_Havaleh, 
Sum(Case When Type = '1' Then Price Else 0 End) - 
Sum(Case When Type = '0' Then Price Else 0 End) SumOverUnder
From Tmp_Over_Under_Factor
Group By Id_Havaleh
 
) As T2 
On T1.Id_Havaleh = T2.Id_Havaleh
Left Outer Join 
(
Select Id_Havaleh , Ch_Price From TmpHeadC 
Inner Join TmpCheqe on TmpCheqe.Id_Cheqe = TmpHeadC.Id_Cheqe
) 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
    Declare @TarikhBargeh DateTime
    Declare @MaxNu  Bigint
    Declare @IdCheqe Bigint
    Declare @Price Money

    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 = @@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
