
create PROCEDURE [dbo].[EShop_FactorDetailInsert]
    @Number NUMERIC(18, 0) ,
    @CustomerId UNIQUEIDENTIFIER ,
    @WarehouseGoodsId UNIQUEIDENTIFIER ,
    @RowNumber SMALLINT ,
    @Qty INT ,
    @SalesPrice MONEY,
	@Mobile Varchar(20)
AS
    DECLARE @Kind CHAR(1) ,
            @gcode VARCHAR(20) ,
            @wcode VARCHAR(5) ,
            @ccode VARCHAR(5)




	begin try
	begin tran

    set @ccode = '10000'
	SELECT @ccode = Guy_Code
    FROM   dbo.Guy WITH(nolock)
    WHERE  mobile = @Mobile 

    SELECT @wcode = ai.A_Code 
    FROM   dbo.Anbar ai WITH(nolock)
    WHERE  ai.Id = @WarehouseGoodsId

    INSERT INTO TmpHavaleh ( [Id_Havaleh] ,
                             [Sender] ,
                             [Reciver] ,
                             [Radif] ,
                             [K_Code] ,
                             [K_Qty1] ,
                             [K_Qty2] ,
                             [R_Unit1_2] ,
                             [Price_KHarid] ,
                             [Price_Omdeh] ,
                             [Price_Forosh] ,
                             [Takhfif_Flag] ,
                             [Point_Flag] ,
                             [Hamrah_Flag] ,
                             [EditQty_Flag] ,
                             [CtrMojodi] ,
                             [Link_Record] ,
                             [Note] ,
                             [Price_Consumer] )
                SELECT @Number ,
				       @wcode,
					   @ccode,
                       --( CASE WHEN @Kind IN ( 1, 5, 7, 9 ) THEN @wcode
                       --       ELSE @ccode
                       --  END ) ,
                       --( CASE WHEN @Kind IN ( 1, 5, 7, 9 ) THEN @ccode
                       --       ELSE @wcode
                       --  END ) ,
                       @RowNumber ,
                       a.K_Code ,
                       @Qty ,
                       ( @Qty / ( CASE WHEN R_Unit1_2 > 0 THEN R_Unit1_2
                                       ELSE 1
                                  END )) ,
                       ki.R_Unit1_2 ,
                       a.Price_Kharid ,
                       a.Price_Omdeh ,
                       @SalesPrice ,
                       0 ,
                       0 ,
                       0 ,
                       0 ,
                       0 ,
                       NULL ,
                       NULL ,
                       a.Price_Consumer
                FROM   dbo.Anbar a WITH(nolock)
                       JOIN dbo.KalaId ki ON ki.K_Code = a.K_Code
                WHERE  a.Id = @WarehouseGoodsId
				Commit Tran
				return 0
				end try
				begin catch
				  rollback tran
				  return -1
				end catch

