CREATE PROCEDURE [dbo].[UpdateMojodi]  @Flag  Char(1) , @ACode Varchar(3), @KCode Varchar(20), @msg_num SmallInt OUTPUT
AS
Declare @i  Smallint
Set @i = 0
Set @msg_num = 0
IF @Flag = '0' 
Begin
  Update Anbar Set Exit_Fa = 0 , Mojodi = 0 , Resive_Fa = 0
  Declare Cursor_Update Cursor Local For
  Select A_Code From AnbarId
  Open Cursor_Update
  Fetch Next From Cursor_Update Into @ACode
  While @@Fetch_Status = 0 
  Begin
      Update Anbar Set Mojodi = T3.SumKqty, Resive_Fa = T3.SumVorod, Exit_Fa = T3.SumKhoroj From Anbar
      Inner Join 
       (
              Select T2.K_Code, Anbar.Mojodi, T2.SumKqty, T2.SumVorod, T2.SumKhoroj From Anbar 
              Inner Join 
  	(
  	  Select K_Code, IsNull(Sum(SumVorod),0) - IsNull(Sum(SumKhoroj),0) SumKQty, 
                IsNull(Sum(SumVorod),0) SumVorod,  IsNull(Sum(SumKhoroj),0) SumKhoroj From (
  	  Select K_Code, 
  	 Case 
  	    When HeadH.Kind_Flag in ('2','3','6','8','0','1') And Havaleh.Reciver = @Acode Then Sum(K_Qty1)
  	 End As SumVorod,
  	 Case
  	    When HeadH.Kind_Flag in ('5','7','9','4','1') And Havaleh.Sender = @Acode  Then Sum(K_Qty1)
  	 End As SumKhoroj
  	From Havaleh 
  	Inner Join HeadH On (HeadH.Id_Havaleh = Havaleh.Id_Havaleh)
  	Where (Sender = @Acode Or Reciver = @Acode)
  	Group By HeadH.Kind_Flag, K_Code, Havaleh.Sender, Havaleh.Reciver) As T1
  	Group By K_Code) As T2
  	On Anbar.K_Code = T2.K_Code 
  	Where A_Code = @Acode And (Anbar.Mojodi <> T2.SumKQty Or Anbar.Exit_Fa <> T2.SumKhoroj Or  Anbar.Resive_Fa <> T2.SumVorod)
     ) As T3
    On (Anbar.K_Code = T3.K_Code) 
    Where Anbar.A_Code= @Acode
    Select @i = @@RowCount
    IF @i > 0 
      Set @Msg_Num = @Msg_Num+ @i 
     Fetch Next From Cursor_Update Into @ACode
  End
  Close Cursor_Update
  Deallocate Cursor_Update
End
Else IF @Flag = '1' 
Begin
      Update Anbar Set Exit_Fa = 0 , Mojodi = 0 , Resive_Fa = 0  Where A_Code = @Acode
      Update Anbar Set Mojodi = T3.SumKqty, Resive_Fa = T3.SumVorod, Exit_Fa = T3.SumKhoroj From Anbar
      Inner Join 
       (
              Select T2.K_Code, Anbar.Mojodi, T2.SumKqty, T2.SumVorod, T2.SumKhoroj From Anbar 
              Inner Join 
  	(
  	  Select K_Code, IsNull(Sum(SumVorod),0) - IsNull(Sum(SumKhoroj),0) SumKQty, 
                IsNull(Sum(SumVorod),0) SumVorod,  IsNull(Sum(SumKhoroj),0) SumKhoroj From (
  	  Select K_Code, 
  	 Case 
  	    When HeadH.Kind_Flag in ('2','3','6','8','0','1') And Havaleh.Reciver = @Acode Then Sum(K_Qty1)
  	 End As SumVorod,
  	 Case
  	    When HeadH.Kind_Flag in ('5','7','9','4','1') And Havaleh.Sender = @Acode  Then Sum(K_Qty1)
  	 End As SumKhoroj
  	From Havaleh 
  	Inner Join HeadH On (HeadH.Id_Havaleh = Havaleh.Id_Havaleh)
  	Where (Sender = @Acode Or Reciver = @Acode)
  	Group By HeadH.Kind_Flag, K_Code, Havaleh.Sender, Havaleh.Reciver) As T1
  	Group By K_Code) As T2
  	On Anbar.K_Code = T2.K_Code 
  	Where A_Code = @Acode And (Anbar.Mojodi <> T2.SumKQty Or Anbar.Exit_Fa <> T2.SumKhoroj Or  Anbar.Resive_Fa <> T2.SumVorod)
     ) As T3
    On (Anbar.K_Code = T3.K_Code) 
    Where Anbar.A_Code= @Acode
    Select @i = @@RowCount
    IF @i > 0 
      Set @Msg_Num = @Msg_Num+ @i 
End
Else IF @Flag = '2' 
Begin
  Update Anbar Set Exit_Fa = 0 , Mojodi = 0 , Resive_Fa = 0 Where K_Code =@KCode
  Declare Cursor_Update Cursor Local For
  Select A_Code From AnbarId
  Open Cursor_Update
  Fetch Next From Cursor_Update Into @ACode
  While @@Fetch_Status = 0 
  Begin
      Update Anbar Set Mojodi = T3.SumKqty, Resive_Fa = T3.SumVorod, Exit_Fa = T3.SumKhoroj From Anbar
      Inner Join 
       (
         Select T2.K_Code, Anbar.Mojodi, T2.SumKqty, T2.SumVorod, T2.SumKhoroj From Anbar 
         Inner Join 
  	     (
  	       Select K_Code, IsNull(Sum(SumVorod),0) - IsNull(Sum(SumKhoroj),0) SumKQty, 
                          IsNull(Sum(SumVorod),0) SumVorod,  IsNull(Sum(SumKhoroj),0) SumKhoroj 
                          From (
  	                            Select K_Code, 
  	                            Case 
  	                               When HeadH.Kind_Flag in ('2','3','6','8','0','1') And Havaleh.Reciver = @Acode Then Sum(K_Qty1)
  	                            End As SumVorod,
  	                            Case
  	                              When HeadH.Kind_Flag in ('5','7','9','4','1') And Havaleh.Sender = @Acode  Then Sum(K_Qty1)
  	                            End As SumKhoroj
  	                            From Havaleh 
  	                            Inner Join HeadH On (HeadH.Id_Havaleh = Havaleh.Id_Havaleh)
  								Where (Sender = @Acode Or Reciver = @Acode)
  								And K_Code = @KCode
  								Group By HeadH.Kind_Flag, K_Code, Havaleh.Sender, Havaleh.Reciver
 								) As T1
 								Group By K_Code
    	) As T2
		On Anbar.K_Code = T2.K_Code 
	  ) As T3
	  On Anbar.K_Code = T3.K_Code
	  Where Anbar.K_Code= @Kcode And A_Code = @ACode
     Fetch Next From Cursor_Update Into @ACode
  End
  Close Cursor_Update
  Deallocate Cursor_Update
End
Return @Msg_Num
