USE [DBMoin9001]
GO
/****** Object:  StoredProcedure [dbo].[AA_CreateEdit_RowWork_OnStuff]    Script Date: 05/25/2026 13:09:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AA_CreateEdit_RowWork_OnStuff]
	(@IdMain_In													int					=					0
	,@IDStore_In												int					=					0
	,@IdStuff_In												int					=					0
	,@StuffDesc_In												nvarchar(500)		=					''
	,@IdRowUp4_SubRow_In										int					=					0
		/*	ID Row Or Radif In Orginal WorkStuff	*/
	,@IDTag4SubGroup_In											int					=					0
	,@ValueUnit01_In											float				=					0
	,@ValueUnit02_In											float				=					0
	,@Fee_In													float				=					0
	,@FeeOut_In													numeric(18, 6)		=					0
	,@Discount01_In												int					=					0
	,@Discount02_In												numeric(5, 2)		=					0
	,@DisCount03_In												Bigint				=					0
	,@Tax01_In													int					=					0
	,@Tax02_In													numeric(5, 2)		=					0
	,@Serial_In													nvarchar(50)		=					''
	,@RowDesc_In												nvarchar(100)		=					''
	,@SellerCent_In												numeric(5, 2)		=					0	
	,@Id_Work													Int					=					1
	/*
		@Id_Work			=		8888			Ins FromExcel
		@Id_Work			=		666666			Cancel Sale
	*/
	)

	   WITH ENCRYPTION
AS
BEGIN
	SET NOCOUNT ON;
	Declare	@TmpTb4Exec						Table
			([id]							[int]				IDENTITY(1,1)		NOT NULL,
			[ID4Use]						[int]				NULL)

	Set		@StuffDesc_In										=					replace(@StuffDesc_In,	nchar(1610),	nchar(1740));
	Set		@StuffDesc_In										=					replace(@StuffDesc_In,	nchar(1603),	nchar(1705));

	Set		@Serial_In											=					replace(@Serial_In,		nchar(1610),	nchar(1740));
	Set		@Serial_In											=					replace(@Serial_In,		nchar(1603),	nchar(1705));

	Set		@RowDesc_In											=					replace(@RowDesc_In,	nchar(1610),	nchar(1740));
	Set		@RowDesc_In											=					replace(@RowDesc_In,	nchar(1603),	nchar(1705));

	IF		(@Id_Work											=					8888)
	Begin
		IF	(@IdStuff_In										>					0)
		Begin
			Set	@IdStuff_In										=
				(Select	Top	1	[id]
					FROM [dbo].[MainStuff]
					Where	([code]								=					@IdStuff_In)
				)
		End
		Else
		Begin
			Declare	@IdStuff4_CreateStuff						Int					=					0
					,@IdStuffDet4_CreateStuff					Int					=					0
					,@IDUp4_CreateStuff							Int					=					0
					,@Name4_CreateStuff							NVarChar(100)		=					N''
					,@IDStuffUnit014_CreateStuff				Int					=					0;
			Set	@IdStuff4_CreateStuff							=
				(SELECT TOP 1 [Id_Stuff]
					FROM [dbo].[DetailStuff]
					Where [TaxStuffID]							=					@Serial_In
				)
			Set	@IdStuff4_CreateStuff							=					IsNull(@IdStuff4_CreateStuff,			0)
			Set	@IdStuff_In										=					@IdStuff4_CreateStuff
			IF	(@IdStuff4_CreateStuff							=					0)
			Begin
				Set	@IDUp4_CreateStuff							=
					(SELECT TOP 1 [id]
						FROM [dbo].[MainStuff]
						Where ([SubStuff]						=					2)
					)
				Set	@IDStuffUnit014_CreateStuff					=
					(SELECT TOP 1[id]
						FROM [dbo].[StuffUnit]
						Where [name]							Like				'%'	+	@RowDesc_In	+	'%'
					)
				Set	@IDStuffUnit014_CreateStuff					=					IsNull(@IDStuffUnit014_CreateStuff,		0)
				IF	(@IDStuffUnit014_CreateStuff				=					0)
					Set	@IDStuffUnit014_CreateStuff				=					2
				Set	@Name4_CreateStuff							=					Convert(NVarChar(100),					@StuffDesc_In)

				Insert Into	@TmpTb4Exec
					([ID4Use])
				EXEC	[dbo].[AA_CreateEdit_Stuff]	
					@Id_In										=					0
					,@Shakhes_In								=					0
					,@Code_In									=					0
					,@LvStuff_In								=					0
					,@Name_In									=					@Name4_CreateStuff
					,@SubStuff_In								=					1
					,@IDUp_In									=					@IDUp4_CreateStuff
					,@IDStuffService_In							=					1
					,@CodeAnbar4Stuff01							=					0
					,@VisableRow_In								=					1
					,@Desc01_In									=					N''
					,@Desc02_In									=					N''
					,@Desc03_In									=					N''
					,@ID4FinalWorkedFee_In						=					1
					,@Work_ID									=					10051;

				Set	@IdStuff4_CreateStuff						=
					(Select	Max([id])
						From	[dbo].[MainStuff]
					)

				Insert Into	@TmpTb4Exec
					([ID4Use])
				EXEC	[dbo].[AA_CreateEdit_DetailStuff]
					@IDStuff_In									=					@IdStuff4_CreateStuff
					,@ID_StuffUnit_01_In						=					@IDStuffUnit014_CreateStuff
					,@ID_StuffUnit_02_In						=					0
					,@RespectUnit_In							=					0
					,@FirstFeeUnit01_In							=					0
					,@FirstStoreUnit01_In						=					0
					,@NowStoreUnit01_In							=					0
					,@FirstStoreUnit02_In						=					0
					,@NowStoreUnit02_In							=					0
					,@MaxStoreUnit01_In							=					0
					,@MinStoreUnit01_In							=					0
					,@AvgFee_In									=					0
					,@Barcode_In								=					N''
					,@Num4Tozin_In								=					0
					,@ProfitCent_In								=					0
					,@DiscountCent_In							=					0
					,@TaxCent_In								=					@Tax02_In
					,@TollCent_In								=					0
					,@SellerCent_In								=					0
					,@ExpDate_In								=					N''
					,@CreateDate_In								=					N''
					,@NtswStuffID_In							=					@Serial_In
					,@TaxStuffID_In								=					@Serial_In
					,@IranCodeStuffID_In						=					@Serial_In
					,@YearsID_In								=					1
					,@IDWork_In									=					159753;
			End
			Set	@IdStuff_In										=					@IdStuff4_CreateStuff;
		End
	End

	IF		(@Fee_In											=					0)
		And	(@FeeOut_In											>					0)
	Begin		
		Set	@Fee_In												=
			(Select	Top 1 Case	When	IsNull([RespectUnit],	0)					>						0
							Then	@FeeOut_In					/					IsNull([RespectUnit],	0)
							Else								0
					End
				FROM [dbo].[DetailStuff]
				Where	([Id_Stuff]								=					@IdStuff_In)
			)
	End
	
	IF		(@ValueUnit01_In									=					0)
		And	(@ValueUnit02_In									>					0)
	Begin
		Set	@ValueUnit01_In										=
			(Select	Top 1 Case	When	IsNull([RespectUnit],		0)					>						0
							Then	@ValueUnit02_In				/					IsNull([RespectUnit],	0)
							Else								0
					End
				FROM [dbo].[DetailStuff]
				Where	([Id_Stuff]								=					@IdStuff_In)
			)
	End

	IF	(@Tax02_In												=					0)
	Begin
		Set	@Tax02_In											=
			(Select Top 1	IsNull([TaxCent],					0)
				FROM [dbo].[DetailStuff]
				Where	([Id_Stuff]								=					@IdStuff_In)
			)
	End

	IF	Not Exists (SELECT TOP 1 [id]
						FROM [dbo].[RowWorkOnStuff]
						Where	([Id_Main]						=					@IdMain_In)
							And	([Id_Stuff]						=					@IdStuff_In)
							And	([IdRowUp4_SubRow]				=					@IdRowUp4_SubRow_In)
					)
		And			(@IdStuff_In								>					0)
	Begin
		Insert Into	[dbo].[RowWorkOnStuff]
				([Id_Main]
			  ,[IDStore]
			  ,[Id_Stuff]
			  ,[StuffDesc]
			  ,[IdRowUp4_SubRow]
			  ,[IDTag4SubGroup]
			  ,[ValueUnit01]
			  ,[ValueUnit02]
			  ,[Fee]
			  ,[FeeOut]
			  ,[Discount01]
			  ,[Discount02]
			  ,[DisCount03]
			  ,[Tax01]
			  ,[Tax02]
			  ,[Serial]
			  ,[RowDesc]
			  ,[SellerCent]
			  )
		Values
				(@IdMain_In
			  ,@IDStore_In
			  ,@IdStuff_In
			  ,@StuffDesc_In
			  ,@IdRowUp4_SubRow_In
			  ,@IDTag4SubGroup_In
			  ,@ValueUnit01_In
			  ,@ValueUnit02_In
			  ,@Fee_In
			  ,@FeeOut_In
			  ,@Discount01_In
			  ,@Discount02_In
			  ,@DisCount03_In
			  ,@Tax01_In
			  ,@Tax02_In
			  ,@Serial_In
			  ,@RowDesc_In
			  ,@SellerCent_In
			  )

		Select Scope_Identity()									AS					Expr1;
	End
	Else
		Select	(-1)											AS					Expr1;
	
END
