USE [prince]
GO
/****** Object:  StoredProcedure [Common].[SYS_TransferDataSaffronToPrince]    Script Date: 11/17/2025 10:57:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		zareie
-- Create date: 1397-06-18
-- Description:	انتقال اطلاعات  از زعفران به پرنس
-- =============================================
ALTER PROCEDURE [Common].[SYS_TransferDataSaffronToPrince]
      @Index    [Common].[_INT_],
      @Col_ID   [Common].[_INT_], 
      @DO		[Common].[_INT_] OUTPUT
AS
BEGIN
/*
@DO
 -1 -- موسسه اصلی نیست
 -2 -- دیتابیس زعفران اتچ نیست  
 -3 -- انتقال اطلاعات یا قبلا به طور کامل انجام شده است و یا از انجام آن توسط کاربر جلوگیری شده است
 -4 -- انجام عملیات در یکی از قسمت ها با خطا مواجه شده است
*/
    SET NOCOUNT ON;
    
    SET @DO = 1
    
    DECLARE @ActiveCompanys  NVARCHAR(2000)
     
	SELECT @ActiveCompanys = COM_DataBaseName FROM [prince].[Common].[Companys] WHERE COM_IsActive = 1  

	IF (@ActiveCompanys <> 'Prince')
	   SET @DO = -1 -- موسسه اصلی نیست

	IF (@DO = 1 ) AND (NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE ('[' + name + ']' = 'Saffron' OR name = 'Saffron')))
	   SET @DO = -2 -- دیتابیس زعفران اتچ نیست        
    
    IF (@DO = 1 ) AND (SELECT SST_Value FROM [prince].[Common].[SystemSetting] WHERE SST_Title = 'TransferDataSP' AND SST_UserID = 0) IN (1,2,3)
       SET @DO = -3 
    
/*
  -- TransferDataSP
  1 --> اطلاعات قبلا انتقال داده شده است
  2 --> کاربر قبلا انصراف داده است
  3 --> اولین سال مالی در سیستم کلاینت ساخته شده است
  
*/    
    
    
    IF @DO < 0
       RETURN

    IF (@Index = -1) AND (EXISTS (SELECT 1 FROM Common.CompanyPerson WHERE CPN_ID = 5 AND CPN_FirstName = N'همکار پيش فرض'))
    BEGIN
        BEGIN TRY
			-- Added By Ghasem Babaie - 1401.09.22 [ PRI-537 ]
			DECLARE @MiscellaneousPersonID	[Common].[_INT_] = 0  
			SELECT @MiscellaneousPersonID = [CNL_ReferID] FROM [Common].[ConstantLookup] WHERE CNL_StaticCode = 15 AND CNL_Name = 'MiscellaneousPerson'        
        
			DELETE FROM [Common].[CompanyPersonGroupMembership]
			DELETE FROM [Common].[CompanyPersonGroup]
			-- Delete Person Tables Condition Changed By Ghasem Babaie - 1401.09.22 [ PRI-537 ]
			DELETE FROM [Common].[ContactInformation] WHERE (CIN_CPN_ID > 4) AND (CIN_CPN_ID <> @MiscellaneousPersonID)
			DELETE FROM [Common].[BankAccount] WHERE (BAC_CPN_ID > 4) AND (BAC_CPN_ID <> @MiscellaneousPersonID)
			DELETE FROM [Common].[Address] WHERE (ADR_CPN_ID > 4) AND (ADR_CPN_ID <> @MiscellaneousPersonID)
			DELETE FROM [Common].[CompanyPerson] WHERE (CPN_ID > 4) AND (CPN_ID <> @MiscellaneousPersonID)
			DELETE FROM Common.Account WHERE 
									1 = CASE WHEN ((ACN_TBL_Holder IN (15,199)) AND (ACN_HolderID > 4) AND (ACN_HolderID <> @MiscellaneousPersonID)) OR
												  (ACN_TBL_Holder IN (54,200) AND (ACN_HolderID <> 0)) THEN
												   1
											 ELSE
												   0
										END
            -- حذف سطوح دسترسی اشخاص
		    EXEC Common.RegisterAndDeleteObjectPermission 2,6,NULL,NULL,NULL,4 			
			
			
			UPDATE [Common].[CompanyPerson]
			   SET CPN_LKP_PriceLevel = 0	
		END TRY
		BEGIN CATCH
			 SET @DO = -5
			 RETURN 
		END CATCH			          
    END

    DECLARE @SAF_LKP_ID								[Common].[_INT_],
			@PRI_LKP_ID								[Common].[_INT_],
            @LKP_Caption							[Common].[_SMALL_] ,
			@LKP_IsDefault							[Common].[_BOOLEAN_] ,
			@LKP_Description						[Common].[_BIG_] ,
			@LKP_CityCode							[Common].[_INT_],
			@LKP_Order								[Common].[_INT_],
			@PRI_LKP_Caption						[Common].[_SMALL_],
			@SAF_LKP_Type							[Common].[_INT_],
			@PRI_LKP_Type							[Common].[_INT_],
			@CBX_ID									[Common].[_INT_] ,
			@CBX_Name								[Common].[_SMALL_] ,
			@CBX_AbbName							[Common].[_TINY_] ,
			@CBX_Code								[Common].[_TINY_] ,
			@CBX_Description						[Common].[_BIG_] ,
			@CBX_InitCash							[Common].[_MONEY_] ,
			@CBX_MinWarning							[Common].[_MONEY_] ,
			@CBX_MaxWarning							[Common].[_MONEY_] ,
			@PRI_CBX_ID								[Common].[_INT_] ,
			@PRI_CBX_Code							[Common].[_TINY_] ,
			@ACN_CurrentAmount						[Common].[_MONEY_],
			@OpeningBalance_ID						[Common].[_INT_],
			@PRI_ACN_ID								[Common].[_INT_] ,
			@CST_ID									[Common].[_INT_] ,
			@CST_Name								[Common].[_SMALL_] ,
			@CST_LKP_Title							[Common].[_INT_] ,
			@CST_Code								[Common].[_TINY_] ,
			@CST_LKP_Group							[Common].[_INT_] ,
			@CST_CostLevel							[Common].[_TINYINT_] ,
			@CST_SCP_ID								[Common].[_INT_] ,
			@CST_CostChange							[Common].[_MONEY_] ,
			@CST_IsCredit							[Common].[_BOOLEAN_] ,
			@CST_MaxCredit							[Common].[_MONEY_] ,
			@CST_BirthDate							[Common].[_DATE_] ,
			@CST_Phone1								[Common].[_PHONE_] ,
			@CST_Phone2								[Common].[_PHONE_] ,
			@CST_Mobile								[Common].[_PHONE_] ,
			@CST_Fax								[Common].[_PHONE_] ,
			@CST_Address1							[Common].[_BIG_] ,
			@CST_Address2							[Common].[_BIG_] ,
			@CST_Description						[Common].[_BIG_] ,
			@CST_Type								[Common].[_TINYINT_] ,
			@CST_CPN_ID								[Common].[_INT_] ,
			@CST_SaloonOrder						[Common].[_INT_] ,
			@CST_Phone3								[Common].[_PHONE_] ,
			@CST_Phone4								[Common].[_PHONE_] ,
			@CST_Phone5								[Common].[_PHONE_] ,
			@CST_Phone6								[Common].[_PHONE_] ,
			@CST_LKP_Section						[Common].[_INT_] ,
			@CST_NationCode							[Common].[_CODE_] ,
			@CST_CommercialCode						[Common].[_TINY_] ,
			@CST_LKP_City							[Common].[_INT_] ,
			@CST_LKP_State							[Common].[_INT_] ,
			@CST_HTypePersion						[Common].[_TINYINT_] ,
			@CST_DefaultDiscountType				[Common].[_INT_] ,
			@CST_DefalutDiscountValue				[Common].[_INT_] ,
			@CST_DateofMarriage						[Common].[_DATE_] ,
			@CST_PostalCode							[Common].[_CODE_] ,
			@CST_DefaultServiceType					[Common].[_INT_] ,
			@CST_DefalutServiceValue				[Common].[_INT_] ,
			@CST_DontApplyTax						[Common].[_BOOLEAN_] ,
			@CST_BirthDate_Int						[Common].[_INT_] ,
			@CST_DateofMarriage_Int					[Common].[_INT_] , 
			@PRI_CPN_ID								[Common].[_INT_] ,
			@PRI_CPN_LKP_Title   					[Common].[_INT_] ,
			@Temp_LKP_Caption						[Common].[_SMALL_],
			@PRI_CPG_ID								[Common].[_INT_] ,
			@Temp_LKP_GroupCaption					[Common].[_SMALL_],
			@PRI_CPN_PriceLevel   					[Common].[_INT_] ,
			@Temp_LKP_PriceLevel					[Common].[_SMALL_],
			@PRI_LKP_Section						[Common].[_INT_] ,
			@Temp_LKP_Section						[Common].[_SMALL_] ,		
			@ADR_LKP_Province           			[Common].[_INT_] ,
			@Temp_LKP_Province          			[Common].[_SMALL_] ,		
			@ADR_LKP_City							[Common].[_INT_] ,
			@Temp_LKP_City							[Common].[_SMALL_] ,
			@CPN_MaritalStatus						[Common].[_BOOLEAN_] ,
			@CPN_SCP_ID								[Common].[_INT_] ,
			@Temp_SCP_Title							[Common].[_SMALL_] ,
			@CIN_Order								[Common].[_INT_] ,
			@CIN_IsDefault              			[Common].[_BOOLEAN_],
			@CPN_IS_Emp								[Common].[_BOOLEAN_],
			@CPN_IS_Subscriber          			[Common].[_BOOLEAN_],
			@CPN_IS_Personnel						[Common].[_BOOLEAN_],	
			@CPN_IS_SellerBuyer						[Common].[_BOOLEAN_],
			@POP_From								[Common].[_INT_] ,
			@POP_To									[Common].[_INT_] ,
			@FNY_Name								[Common].[_SMALL_],
			@Fny_Date								[Common].[_DATE_],
			@CRR_ID									[Common].[_INT_] ,
			@CRR_Name								[Common].[_SMALL_] ,
			@CRR_BDate								[Common].[_DATE_] ,
			@CRR_IDNumber							[Common].[_CODE_] ,
			@CRR_FatherName							[Common].[_SMALL_] ,
			@CRR_NationalCode						[Common].[_CODE_] ,
			@CRR_Guarantee							[Common].[_SMALL_] ,
			@CRR_Mobile								[Common].[_PHONE_] ,
			@CRR_Phone								[Common].[_PHONE_] ,
			@CRR_StartDate							[Common].[_DATE_] ,
			@CRR_IsDisabled							[Common].[_BOOLEAN_] ,
			@CRR_Address							[Common].[_BIG_] ,
			@CRR_Description						[Common].[_BIG_] ,
			@CRR_CPN_ID								[Common].[_INT_] ,
			@CRR_BDate_Int							[Common].[_INT_] ,
			@CRR_StartDate_Int						[Common].[_INT_] ,
			@CIT_Name								[Common].[_SMALL_] ,
			@CIT_AbbName							[Common].[_TINY_] ,
			@CIT_Description						[Common].[_BIG_] ,
			@PTL_Name								[Common].[_SMALL_],
			@PTL_PrinterName						[Common].[_SMALL_],
			@PTL_Type								[Common].[_TINY_] ,
			@PTL_RightMargin						[Common].[_FLOAT_] ,
			@PTL_LeftMargin							[Common].[_FLOAT_] ,
			@PTL_TopMargin							[Common].[_FLOAT_] ,
			@PTL_BottomMargin						[Common].[_FLOAT_] ,
			@PTL_Font								[Common].[_SMALL_] ,
			@PTL_FontSize							[Common].[_TINYINT_] ,
			@PTL_FontStyle							[Common].[_SMALL_] ,
			@PTL_TitleFont							[Common].[_SMALL_] ,
			@PTL_TitleFontSize						[Common].[_TINYINT_] ,
			@PTL_PrintCount							[Common].[_INT_],
			@PRL_PTL_Width							[Common].[_FLOAT_],
			@PRL_PTL_Length							[Common].[_FLOAT_],
			@PRL_PTL_BottomFontSize					[Common].[_TINYINT_]  ,
			@SAF_PTL_ID								[Common].[_INT_],
			@PRI_PTL_ID								[Common].[_INT_],
			@CMD_ID									[Common].[_INT_]  ,
			@CMD_Name								[Common].[_SMALL_] ,
			@CMD_AbbName							[Common].[_TINY_] ,
			@CMD_LKP_Unit							[Common].[_INT_] ,
			@CMD_LKP_Group							[Common].[_INT_] ,
			@CMD_PTL_ID								[Common].[_INT_] ,
			@CMD_InCost								[Common].[_MONEY_] ,
			@CMD_Cost1								[Common].[_MONEY_] ,
			@CMD_Cost2								[Common].[_MONEY_] ,
			@CMD_Cost3								[Common].[_MONEY_] ,
			@CMD_Cost4								[Common].[_MONEY_] ,
			@CMD_Cost5								[Common].[_MONEY_] ,
			@CMD_Description						[Common].[_BIG_] ,
			@CMD_Order								[Common].[_INT_] ,
			@CMD_HotKeyCode							[Common].[_INT_] ,
			@CMD_IMG_ID								[Common].[_INT_] ,
			@CMD_IsFood								[Common].[_BOOLEAN_] ,
			@CMD_InitialCount						[Common].[_INT_] ,
			@CMD_LKP_RecGrpID						[Common].[_INT_] ,
			@CMD_IsDisabled							[Common].[_BOOLEAN_] ,
			@CMD_LKP_Section						[Common].[_INT_] ,
			@CMD_PrintOrder							[Common].[_INT_] ,
			@CMD_Saloon_PTL_ID1						[Common].[_INT_] ,
			@CMD_Saloon_PTL_ID2						[Common].[_INT_] ,
			@CMD_Out_PTL_ID1						[Common].[_INT_] ,
			@CMD_Out_PTL_ID2						[Common].[_INT_] ,
			@CMD_IsIngredient						[Common].[_BOOLEAN_] ,
			@CMD_UseProductionForProfit				[Common].[_BOOLEAN_] ,
			@CMD_UserProductionForStore				[Common].[_BOOLEAN_] ,
			@CMD_CountForProduct					[Common].[_INT_] ,
			@CMD_SalesCount							[Common].[_FLOAT_] ,
			@CMD_CurrentAmount						[Common].[_FLOAT_] ,
			@CMD_MinimumCount						[Common].[_FLOAT_] ,
			@CMD_InputCount							[Common].[_FLOAT_] ,
			@CMD_Saloon_PTL_ID3						[Common].[_INT_] ,
			@CMD_Out_PTL_ID3						[Common].[_INT_] ,
			@CMD_FoodContents           			[Common].[_BIG_] , 
			@CMD_IsHide_Kiosk           			[Common].[_TINYINT_], 
			@CMD_IsHide_VirtualMenuApp      		[Common].[_TINYINT_],
	        @CMD_LKP_KioskAppGroup					[Common].[_INT_],
	        @CMD_LKP_VirtualMenuAppGroup			[Common].[_INT_],
			@PRI_CMD_PTL_ID							[Common].[_INT_] ,
			@PRI_CMD_Saloon_PTL_ID1					[Common].[_INT_] ,
			@PRI_CMD_Saloon_PTL_ID2					[Common].[_INT_] ,
			@PRI_CMD_Out_PTL_ID1					[Common].[_INT_] ,
			@PRI_CMD_Out_PTL_ID2					[Common].[_INT_] ,
			@PRI_CMD_Saloon_PTL_ID3					[Common].[_INT_] ,
			@PRI_CMD_Out_PTL_ID3					[Common].[_INT_] ,	
			@PRI_CMD_ID								[Common].[_INT_],
			@PRI_LKP_RecGrpID						[Common].[_INT_],
			@PRI_LKP_RecGrpTitle					[Common].[_SMALL_],
			@PRI_CMD_LKP_Unit_ID					[Common].[_INT_],
			@PRI_CMD_LKP_Unit_Title					[Common].[_SMALL_],
			@PRI_CNU_ID								[Common].[_INT_],
			@PRI_CUM_ID								[Common].[_INT_],
			@PRI_LKP_Price_Usual					[Common].[_INT_],
			@PRI_LKP_Price_ORG						[Common].[_INT_],
			@PRI_LKP_Price_CS						[Common].[_INT_],
			@PRI_LKP_Price_Student      			[Common].[_INT_],
			@PRI_LKP_Price_Special					[Common].[_INT_],
			@PRI_CMD_Group_ID						[Common].[_INT_],
			@PRI_CMD_Group_Title					[Common].[_SMALL_],
			@PRI_CMD_KioskAppGroup_ID				[Common].[_INT_],
			@PRI_CMD_VirtualMenuAppGroup_ID			[Common].[_INT_],
				
			@PRI_Profit								[Common].[_MONEY_] ,			
			@PRI_DefaultPartID						[Common].[_INT_], -- بخش پیش فرض
            @PRI_DefaultPlaceID						[Common].[_INT_],  -- انبار پیش فرض  
			@PRD_CMD_ID_Product						[Common].[_INT_] ,
			@PRD_CMD_ID_Ingredint					[Common].[_INT_] ,
			@PRD_Amount								[Common].[_FLOAT_],
			@PRI_PRD_CMD_ID_Product					[Common].[_INT_] ,
			@PRI_PRD_CMD_ID_Ingredint				[Common].[_INT_] ,
			@CML_CMD_BaseID							[Common].[_INT_] ,
			@CML_CMD_LinkID							[Common].[_INT_] ,
			@CML_Count								[Common].[_INT_] ,
			@CML_Order								[Common].[_INT_] ,
			@CML_SalesType							[Common].[_INT_],
			@PRI_CML_CMD_BaseID						[Common].[_INT_] ,
			@PRI_CML_CMD_LinkID						[Common].[_INT_] ,
			@PRI_DefaultCMDUnitID					[Common].[_INT_] ,
            @Query									NVARCHAR(MAX),
            @Param                      			Common._BIG_ 
    

    IF (@Index IN (0,1)) -- lookup & محدوده
    BEGIN 

		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferLookUp')
		BEGIN
	    

			UPDATE [Saffron].[dbo].[Lookup]
			   SET [LKP_Caption] = LTRIM(RTRIM(REPLACE(REPLACE([LKP_Caption], N'ی', N'ي'), N'ک', N'ك')));
		    
		    
			UPDATE [Prince].[Common].[Lookup]
			   SET [LKP_Caption] = LTRIM(RTRIM(REPLACE(REPLACE([LKP_Caption], N'ی', N'ي'), N'ک', N'ك')));
	   
		 --  *****************************************
		    
		    DELETE FROM [Prince].[Common].[Lookup] WHERE LKP_Type = 16 AND LKP_Saffron = 1
		    
 			DECLARE LKP CURSOR for
				   SELECT [LKP_ID],[LKP_Type],[LKP_Caption],[LKP_IsDefault],[LKP_Description],[LKP_CityCode],[LKP_Order]
					 FROM [Saffron].[dbo].[Lookup]
					WHERE [LKP_Type] IN (18,5,6,8,9,14,15,16,21,3,19,2,4,23,24) 
					  --AND [LKP_ID] NOT IN (434,1109) 
			OPEN LKP
			FETCH NEXT FROM LKP INTO @SAF_LKP_ID,@SAF_LKP_Type,@LKP_Caption,@LKP_IsDefault,@LKP_Description,@LKP_CityCode,@LKP_Order
			WHILE @@FETCH_STATUS =0
			BEGIN     

				  IF @SAF_LKP_Type = 18 --غرفه
				  BEGIN
					 SET @PRI_LKP_Type = 68
					 SET @LKP_Caption = REPLACE (@LKP_Caption,'بخش','غرفه')
				  END 
				  ELSE
				  IF @SAF_LKP_Type = 5 --واحد شمارش کالا
					 SET @PRI_LKP_Type = 1
				  ELSE 
				  IF @SAF_LKP_Type = 6 -- پیشوندها
					 SET @PRI_LKP_Type = 3 	
				  ELSE 
				  IF @SAF_LKP_Type = 8 -- توضیحات
					 SET @PRI_LKP_Type = 18 	
				  ELSE 
				  IF @SAF_LKP_Type = 9 -- گروه فیش غذا
					 SET @PRI_LKP_Type = 66 	
				  ELSE 
				  IF @SAF_LKP_Type = 14 -- کشور
					 SET @PRI_LKP_Type = 5
				  ELSE 
				  IF @SAF_LKP_Type = 15 -- استان
					 SET @PRI_LKP_Type = 6   
				  ELSE 
				  IF @SAF_LKP_Type = 16 -- شهر
					 SET @PRI_LKP_Type = 7     
				  ELSE 
				  IF @SAF_LKP_Type = 21 -- نوع مراسم
					 SET @PRI_LKP_Type = 70  
				  ELSE 
				  IF @SAF_LKP_Type = 3 -- گروه کالا
					 SET @PRI_LKP_Type = 10  
				  ELSE 
				  IF @SAF_LKP_Type = 23 -- گروه کالا در کیوسک
					 SET @PRI_LKP_Type = 75  
			      ELSE 
				  IF @SAF_LKP_Type = 24 --  گروه کالا در منو مجازی
					 SET @PRI_LKP_Type = 76  		 	 
		          ELSE 
				  IF @SAF_LKP_Type = 19 -- زعفران - PDA
					 SET @PRI_LKP_Type = 69 
		          ELSE 
				  IF @SAF_LKP_Type = 4 -- سطح قیمت
					 SET @PRI_LKP_Type = 16  
		          
		          IF @SAF_LKP_Type = 4
		             SET @LKP_Order = (SELECT (ISNULL(MAX(LKP_Order),0) + 1) FROM [Prince].[Common].[Lookup] WHERE LKP_Type = @PRI_LKP_Type AND LKP_Saffron = 1)
		          ELSE
		          IF @LKP_Order IS NULL  
		             SET @LKP_Order = 0   
		          
		          
		          IF @SAF_LKP_Type IN (18,5,6,8,9,14,15,16,21,3,19,4,23,24)
		          BEGIN
					  SET @PRI_LKP_ID = 0
					  IF @SAF_LKP_Type = 5 
                         SELECT @PRI_LKP_ID = LKP_ID FROM [Prince].[Common].[Lookup] WHERE LKP_Type = @PRI_LKP_Type AND REPLACE ([LKP_Caption],' ','') = REPLACE (@LKP_Caption,' ','') 
					  ELSE
					     SELECT @PRI_LKP_ID = LKP_ID FROM [Prince].[Common].[Lookup] WHERE LKP_Type = @PRI_LKP_Type AND [LKP_Caption] = @LKP_Caption 
					  
					  IF ISNULL(@PRI_LKP_ID,0)=0  
					  BEGIN  
						 EXEC [Prince].Common.LKP_Insert @PRI_LKP_Type,'',@LKP_Caption,@LKP_Order,@LKP_IsDefault,0,0,0,0,0,0,0,0,1,@LKP_CityCode,@LKP_Description,'0',@PRI_LKP_ID OUTPUT
						 
						 IF @SAF_LKP_Type = 4 -- سطح قیمت
						 BEGIN
						    UPDATE [Prince].[Common].[Lookup] 
						       SET LKP_EngCaption = CASE @LKP_Order 
						                                 WHEN 1 THEN 'PriceLevel_Common'
														 WHEN 2 THEN 'PriceLevel_Organization'
														 WHEN 3 THEN 'PriceLevel_SpecialCustomers'
														 WHEN 4 THEN 'PriceLevel_Student'
														 WHEN 5 THEN 'PriceLevel_Special'
						                            END 
						     WHERE LKP_ID = @PRI_LKP_ID  
						 END
					  END
					  ELSE 
						 UPDATE [Prince].[Common].[Lookup] SET LKP_Saffron = 1 WHERE LKP_Type = @PRI_LKP_Type AND [LKP_Caption] = @LKP_Caption
					  
					  INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (3,@SAF_LKP_ID,@LKP_Caption,@PRI_LKP_ID,@LKP_Caption) 
				  END
				  ELSE IF @SAF_LKP_Type = 2
				  BEGIN
				  
					  SET @PRI_CPG_ID = 0
					  SELECT @PRI_CPG_ID = CPG_ID FROM Prince.Common.CompanyPersonGroup WHERE CPG_Name = @LKP_Caption
				          
					  IF ISNULL(@PRI_CPG_ID,0) = 0 
					 	 EXEC Common.CPG_Insert @LKP_Caption,NULL,@PRI_CPG_ID OUTPUT
                     
                      INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (16,@SAF_LKP_ID,@LKP_Caption,@PRI_CPG_ID,@LKP_Caption) 
					  
				  END
				     
			   FETCH NEXT FROM LKP INTO @SAF_LKP_ID,@SAF_LKP_Type,@LKP_Caption,@LKP_IsDefault,@LKP_Description,@LKP_CityCode,@LKP_Order
			END
			CLOSE LKP
			DEALLOCATE LKP	       
		   
             
			DECLARE @DefaultPriceLevelTitleID INT = 0            
			 SELECT @DefaultPriceLevelTitleID = LKP_ID FROM [Prince].[Common].[Lookup] WHERE LKP_Type = 16 AND LKP_Saffron = 1 AND LKP_Order = 1            

            UPDATE [Prince].[Common].[SystemSetting] 
               SET SST_Value = @DefaultPriceLevelTitleID
             WHERE SST_Title = 'DefaultPriceLevelTitleID' 
		    
		    
		    UPDATE [Prince].[Common].[SystemSetting] 
               SET SST_Value = (SELECT SST_Value FROM [saffron].[dbo].[SystemSetting] WHERE SST_Title = 'CurrencySymbol' AND SST_UserID = '1000000000')
             WHERE SST_Title = 'CurrencySymbol' AND 
                   SST_UserID = '1000000000'  
             
            UPDATE [Prince].[Common].[SystemSetting] 
               SET SST_Value = (SELECT SST_Value FROM [saffron].[dbo].[SystemSetting] WHERE SST_Title = 'CurrencySymbol' AND SST_UserID = '0')
             WHERE SST_Title = 'CurrencySymbol' AND 
                   SST_UserID = '0'
		    
		----- ******************************************

			DECLARE @SAF_SCP_ID		[Common].[_INT_],
			        @SCP_Name		[Common].[_SMALL_],
					@SCP_Cost		[Common].[_MONEY_],
					@PRI_SCP_ID		[Common].[_INT_]  
		        
		        
			 UPDATE [Saffron].[dbo].[Scope]
			   SET [SCP_Name] = LTRIM(RTRIM(REPLACE(REPLACE([SCP_Name], N'ی', N'ي'), N'ک', N'ك')));
		    
		    
			UPDATE [Prince].[Common].[SaffronScope]
			   SET [SCP_Name] = LTRIM(RTRIM(REPLACE(REPLACE([SCP_Name], N'ی', N'ي'), N'ک', N'ك')));  
		       
		       
  			DECLARE SCP CURSOR for
				   SELECT [SCP_ID],[SCP_Name],[SCP_Cost]
					 FROM [Saffron].[dbo].[Scope]
			OPEN SCP
			FETCH NEXT FROM SCP INTO @SAF_SCP_ID,@SCP_Name,@SCP_Cost
			WHILE @@FETCH_STATUS =0
			BEGIN     
                 
                  SET @PRI_SCP_ID = 0
                  SELECT @PRI_SCP_ID = SCP_ID FROM [Prince].[Common].[SaffronScope] WHERE [SCP_Name] = @SCP_Name
                  
				  IF ISNULL(@PRI_SCP_ID ,0) = 0    
					 EXEC [Prince].Common.SCP_Insert @SCP_Name,@SCP_Cost,@PRI_SCP_ID OUTPUT
				  
				  INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (343,@SAF_SCP_ID,@SCP_Name,@PRI_SCP_ID,@SCP_Name) 
				  
			   FETCH NEXT FROM SCP INTO @SAF_SCP_ID,@SCP_Name,@SCP_Cost
			END
			CLOSE SCP
			DEALLOCATE SCP      
	    
	        IF (@Col_ID IS NULL)
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferLookUp', 1)
	    
		END
    END
    --***************************************************
    IF (@Index IN (0,2)) -- CashBox
    BEGIN 
    
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCashBox')
		BEGIN 
	    
			SET @OpeningBalance_ID = 0
			SET @OpeningBalance_ID = (SELECT ACN_ID FROM Prince.Common.Account WHERE ACN_TBL_Holder = 86)
		    
			SELECT @FNY_Name = FNY_Name , @Fny_Date = FNY_OpenDate FROM Prince.Common.FinancialYear WHERE ISNULL(FNY_CloseDate_Int , 0)= 0    

			--DECLARE CBX CURSOR for
			--	   SELECT [CBX_ID],[CBX_Name],[CBX_AbbName],[CBX_Code],[CBX_Description],[CBX_InitCash],[CBX_MinWarning],[CBX_MaxWarning] 
			--		 FROM Saffron.dbo.CashBox 
			--		WHERE [CBX_IsDeleted] = 0 AND
  	--			      1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  	--			               CASE WHEN [CBX_ID] = @Col_ID THEN 1 ELSE 0 END END
			--OPEN CBX
			--FETCH NEXT FROM CBX INTO @CBX_ID,@CBX_Name,@CBX_AbbName,@CBX_Code,@CBX_Description,@CBX_InitCash,@CBX_MinWarning,@CBX_MaxWarning
			--WHILE @@FETCH_STATUS =0
			--BEGIN     
                  BEGIN TRY 
                  
                      SELECT @CBX_ID			= [CBX_ID],
							 @CBX_Name			= [CBX_Name],
							 @CBX_AbbName		= [CBX_AbbName],
							 @CBX_Code			= [CBX_Code],
							 @CBX_Description	= [CBX_Description],
							 @CBX_InitCash		= [CBX_InitCash],
							 @CBX_MinWarning	= [CBX_MinWarning],
							 @CBX_MaxWarning	= [CBX_MaxWarning] ,
							 @ACN_CurrentAmount = [ACN_CurrentAmount]
                        FROM Saffron.dbo.CashBox 
                        LEFT JOIN Saffron.dbo.Account ON ACN_TBL_Holder = 18 AND ACN_HolderID = CBX_ID
					   WHERE [CBX_IsDeleted] = 0 AND
  				         1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  				                  CASE WHEN [CBX_ID] = @Col_ID THEN 1 ELSE 0 END END 
                  
                  
					  IF (@CBX_ID = 1) AND (@CBX_Name <> 'پیش فرض')
					  BEGIN					     
						 EXEC prince.Common.CBX_Update 1,@CBX_Name,@CBX_AbbName,1,@CBX_Description,@CBX_InitCash,@CBX_MinWarning,@CBX_MaxWarning,0,0
						 SET @PRI_CBX_ID = 1
					  END
					  ELSE
					  BEGIN
						 SET @PRI_CBX_ID = 0
						 SELECT @PRI_CBX_ID =[CBX_ID],@PRI_CBX_Code = CBX_Code FROM Prince.Common.CashBox WHERE CBX_Name = @CBX_Name 
			             
						 IF @PRI_CBX_ID > 0
							EXEC prince.Common.CBX_Update @PRI_CBX_ID,@CBX_Name,@CBX_AbbName,@PRI_CBX_Code,@CBX_Description,@CBX_InitCash,@CBX_MinWarning,@CBX_MaxWarning,0,0
						 ELSE
							EXEC prince.Common.CBX_Insert @CBX_Name,@CBX_AbbName,NULL,@CBX_Description,@CBX_InitCash,@CBX_MinWarning,@CBX_MaxWarning,0,0,1,'',@PRI_CBX_ID OUTPUT
					  END
			          
			        --  INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (19,@CBX_ID,@CBX_Name,@PRI_CBX_ID,@CBX_Name) 
			          
					  --SET @ACN_CurrentAmount = 0
					  --SET @ACN_CurrentAmount = (SELECT ACN_CurrentAmount FROM Saffron.dbo.Account WHERE ACN_TBL_Holder = 18 AND ACN_HolderID = @CBX_ID) 
			         
					  SET @PRI_ACN_ID = 0
					  SET @PRI_ACN_ID = (SELECT ACN_ID FROM prince.Common.Account WHERE ACN_TBL_Holder = 19 AND ACN_HolderID = @PRI_CBX_ID) 
			         
					  IF (ISNULL(@ACN_CurrentAmount,0) > 0)
					  BEGIN
						 SET @Query = ''
						 SET @Query = ' EXEC prince.['+@FNY_Name+'].GeneralTransferCredit 29,27,'+CAST(@ACN_CurrentAmount AS NVARCHAR(2000))+','''+@Fny_Date+''',''00:00:01'','+
									  +LTRIM(RTRIM(STR(@OpeningBalance_ID)))+','''','+LTRIM(RTRIM(STR(@PRI_ACN_ID)))+','''',19,'+LTRIM(RTRIM(STR(@PRI_CBX_ID)))+',NULL,'+
									  ''''','''','''','''',''موجودي ابتداي دوره'','''','''','''','''','''','+
									  '1,NULL,Null'
							 EXEC(@Query)              
			             
						 UPDATE prince.Common.CashBox
							SET CBX_InitCash = @ACN_CurrentAmount
						  WHERE CBX_ID = @PRI_CBX_ID  
			                
					  END
					  
				  END TRY
				  BEGIN CATCH
				      SET @DO = -6
				      --CLOSE CBX
			       --   DEALLOCATE CBX	
				      RETURN 
				  END CATCH
			--   FETCH NEXT FROM CBX INTO @CBX_ID,@CBX_Name,@CBX_AbbName,@CBX_Code,@CBX_Description,@CBX_InitCash,@CBX_MinWarning,@CBX_MaxWarning
			--END
			--CLOSE CBX
			--DEALLOCATE CBX	       
	        
	        IF (@Col_ID IS NULL) OR ((SELECT MAX([CBX_ID]) FROM Saffron.dbo.CashBox WHERE [CBX_IsDeleted] = 0) = @Col_ID)
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCashBox', 1)
	    
		END
    END
    --***************************************************
    IF (@Index IN (0,3)) -- Person
    BEGIN 
    
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCustomer')
		BEGIN 
    
			-- حساب افتتاحیه
			SET @OpeningBalance_ID = 0
			SET @OpeningBalance_ID = (SELECT ACN_ID FROM Prince.Common.Account WHERE ACN_TBL_Holder = 86)
		    
			-- نام سال مالی
			SELECT @FNY_Name = FNY_Name , @Fny_Date = FNY_OpenDate FROM Prince.Common.FinancialYear WHERE ISNULL(FNY_CloseDate_Int , 0)= 0    
		    
			-- لود تمام مشتری ها بجز اول و دوم که در دیتابیس پرنس هستند
			--DECLARE CST CURSOR for
			--	   SELECT [CST_ID],[CST_Name],[CST_LKP_Title],[CST_Code],[CST_LKP_Group],[CST_CostLevel],[CST_SCP_ID],[CST_CostChange]
			--			 ,[CST_IsCredit],[CST_MaxCredit],[CST_BirthDate],[CST_Phone1],[CST_Phone2],[CST_Mobile],[CST_Fax],[CST_Address1]
			--			 ,[CST_Address2],[CST_Description],[CST_Type],[CST_CPN_ID],[CST_SaloonOrder],[CST_Phone3],[CST_Phone4],[CST_Phone5]
			--			 ,[CST_Phone6],[CST_LKP_Section],[CST_NationCode],[CST_CommercialCode],[CST_LKP_City],[CST_LKP_State],[CST_HTypePersion]
			--			 ,[CST_DefaultDiscountType],[CST_DefalutDiscountValue],[CST_DateofMarriage],[CST_PostalCode],[CST_DefaultServiceType]
			--			 ,[CST_DefalutServiceValue],[CST_DontApplyTax],[ACN_CurrentAmount] 
			--		 FROM [saffron].[dbo].[Customer]
			--       JOIN [Saffron].[dbo].[Account] ON ACN_TBL_Holder = 20 AND ACN_HolderID = CST_CPN_ID
			--		WHERE [CST_ID] NOT IN (1,2) AND
			--		  1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  	--			               CASE WHEN [CST_ID] = @Col_ID THEN 1 ELSE 0 END END

			--OPEN CST
			--FETCH NEXT FROM CST INTO @CST_ID ,@CST_Name ,@CST_LKP_Title ,@CST_Code ,@CST_LKP_Group ,@CST_CostLevel ,@CST_SCP_ID ,@CST_CostChange ,
			--						 @CST_IsCredit ,@CST_MaxCredit ,@CST_BirthDate ,@CST_Phone1 ,@CST_Phone2 ,@CST_Mobile ,@CST_Fax ,@CST_Address1 ,
			--						 @CST_Address2 ,@CST_Description ,@CST_Type ,@CST_CPN_ID ,@CST_SaloonOrder ,@CST_Phone3 ,@CST_Phone4 ,@CST_Phone5 ,
			--						 @CST_Phone6 ,@CST_LKP_Section ,@CST_NationCode ,@CST_CommercialCode ,@CST_LKP_City ,@CST_LKP_State ,@CST_HTypePersion ,
			--						 @CST_DefaultDiscountType ,@CST_DefalutDiscountValue ,@CST_DateofMarriage ,@CST_PostalCode ,@CST_DefaultServiceType ,
			--						 @CST_DefalutServiceValue ,@CST_DontApplyTax,@ACN_CurrentAmount
			--WHILE @@FETCH_STATUS =0
			--BEGIN     
		         BEGIN TRY
		         
		            SELECT  @CST_ID = [CST_ID],
		                    @CST_Name = [CST_Name],
		                    @CST_LKP_Title = [CST_LKP_Title],
							@CST_Code = [CST_Code],
							@CST_LKP_Group = [CST_LKP_Group],
							@CST_CostLevel = [CST_CostLevel],
							@CST_SCP_ID = [CST_SCP_ID],
							@CST_CostChange = [CST_CostChange],
							@CST_IsCredit = [CST_IsCredit],
							@CST_MaxCredit = [CST_MaxCredit],
							@CST_BirthDate = [CST_BirthDate],
							@CST_Phone1 = [CST_Phone1],
							@CST_Phone2 = [CST_Phone2],
							@CST_Mobile = [CST_Mobile],
							@CST_Fax = [CST_Fax],
							@CST_Address1 = [CST_Address1],
							@CST_Address2 = [CST_Address2],
							@CST_Description = [CST_Description],
							@CST_Type = [CST_Type],
							@CST_CPN_ID = [CST_CPN_ID],
							@CST_SaloonOrder = [CST_SaloonOrder],
							@CST_Phone3 = [CST_Phone3],
							@CST_Phone4 = [CST_Phone4],
							@CST_Phone5 = [CST_Phone5],
							@CST_Phone6 = [CST_Phone6],
							@CST_LKP_Section = [CST_LKP_Section],
							@CST_NationCode = [CST_NationCode],
							@CST_CommercialCode = [CST_CommercialCode],
							@CST_LKP_City = [CST_LKP_City],
							@CST_LKP_State = [CST_LKP_State],
							@CST_HTypePersion = [CST_HTypePersion],
							@CST_DefaultDiscountType = [CST_DefaultDiscountType],
							@CST_DefalutDiscountValue = [CST_DefalutDiscountValue],
							@CST_DateofMarriage = [CST_DateofMarriage],
							@CST_PostalCode = [CST_PostalCode],
							@CST_DefaultServiceType = [CST_DefaultServiceType],
							@CST_DefalutServiceValue = [CST_DefalutServiceValue],
							@CST_DontApplyTax  = [CST_DontApplyTax],
							@ACN_CurrentAmount = [ACN_CurrentAmount] 
                      FROM [saffron].[dbo].[Customer]
                      LEFT JOIN [Saffron].[dbo].[Account] ON ACN_TBL_Holder = 20 AND ACN_HolderID = CST_CPN_ID
					WHERE [CST_ID] NOT IN (1,2) AND
					  1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  				               CASE WHEN [CST_ID] = @Col_ID THEN 1 ELSE 0 END END
		      
		      
					 IF @CST_Type IN (0,1)
					 BEGIN
						SET @CPN_IS_Emp = 0
						SET @CPN_IS_Subscriber = 1
						SET @CPN_IS_Personnel = 0  
						SET @CPN_IS_SellerBuyer = 1   
						IF @CST_Type = 1
						   SET @CST_IsCredit = 1       
					 END 
					 ELSE
					 BEGIN -- پرسنل
						SET @CPN_IS_Emp = 1
						SET @CPN_IS_Subscriber = 0
						SET @CPN_IS_Personnel = 1
						SET @CST_Type = 0
						SET @CST_MaxCredit = 0
						SET @CST_HTypePersion = 0
						SET @CST_DontApplyTax = 0
						SET @CPN_IS_SellerBuyer = 1    
					 END 
			         
					 ---- پیشوند *************************
			         
					 SELECT @PRI_CPN_LKP_Title = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CST_LKP_Title)
					 
					 ---- گروه شخص *************************
				     
				     SELECT @PRI_CPG_ID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 16) AND ([TRD_OldID] = @CST_LKP_Group)
					 
					 ---- سطح قیمت *************************
			         
			         SELECT @PRI_CPN_PriceLevel = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CST_CostLevel)
					 
					 ---- غرفه *************************  
			         
			         SELECT @PRI_LKP_Section = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CST_LKP_Section)
					 IF ISNULL(@PRI_LKP_Section,-1)<0
					    SET @PRI_LKP_Section = 0    
					 ---- شهر و استان *************************  
			         
			         SELECT @ADR_LKP_Province = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CST_LKP_State)
					 SELECT @ADR_LKP_City = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CST_LKP_City)
					 
					 ---- وضعیت تاهل *************************
			         
					 IF ISNULL(@CST_DateofMarriage,'') = ''
						SET @CPN_MaritalStatus = 0
					 ELSE
						SET @CPN_MaritalStatus = 1          
			         
					 ---- محدوده *************************
			         
			         SELECT @CPN_SCP_ID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 343) AND ([TRD_OldID] = @CST_SCP_ID)
					  
			         SET @CST_HTypePersion = ISNULL(@CST_HTypePersion,0)  
			         IF @CST_DateofMarriage = ''
			         SET @CST_DateofMarriage = NULL
			                    
                     IF CAST((ISNULL(@CST_Code,0)) AS BIGINT) BETWEEN 0 AND 6
                        SET @CST_Code = NULL
                       
					 ---- ثبت اطلاعات اشخاص ************************* 
			         
					 SET @PRI_CPN_ID = 0
                     
                     IF @CST_DefaultDiscountType = 0
                        SET @CST_DefaultDiscountType = 1
                     IF @CST_DefaultServiceType = 0
                        SET @CST_DefaultServiceType = 1   
                     
					 EXEC prince.Common.CPN_Insert  @PRI_CPN_LKP_Title,@CST_Name,'',@CST_Code,NULL,NULL,@CST_CommercialCode,@CST_Description,0,
													@CST_MaxCredit,NULL,@CST_NationCode,@CST_BirthDate,'','',1,0,0,0,0,@CPN_IS_SellerBuyer,@CST_IsCredit,
													@CPN_IS_Emp,0,0,'',@CST_HTypePersion,@PRI_CPN_PriceLevel,0,0,0,0,@CST_DontApplyTax,0,0,0,@CPN_MaritalStatus,
													@CST_DateofMarriage,0,'','',@CPN_IS_Personnel ,@CPN_IS_Subscriber,@CST_Type,@CPN_SCP_ID,@CST_CostChange,0,@PRI_LKP_Section,
													@CST_DefaultDiscountType ,@CST_DefalutDiscountValue,@CST_DefaultServiceType,@CST_DefalutServiceValue,'',0,@CST_Mobile,
													'',0,0,'',1,1,@PRI_CPN_ID OUTPUT
	
					 IF ISNULL (@PRI_CPN_ID,0)>0
					 BEGIN
				         
				       --  INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (15,@CST_ID,@CST_Name,@PRI_CPN_ID,@CST_Name) 
				         
						 UPDATE prince.Common.CompanyPerson
							SET CPN_Parent_ID = 0 /*CPN_ID*/
						  WHERE CPN_ID = @PRI_CPN_ID      
				          
						-- -- گروه اشخاص ************************* 
				         
						 IF @PRI_CPG_ID > 0
							EXEC prince.Common.PGM_Insert @PRI_CPN_ID,@PRI_CPG_ID,NULL
				            
				             
						-- -- آدرس *************************
				          
						 IF ISNULL(@CST_Address1,'') <> ''
						 BEGIN
							EXEC prince.Common.ADR_Insert NULL,@ADR_LKP_Province,@ADR_LKP_City,@CST_Address1,@CST_PostalCode,1,@PRI_CPN_ID,NULL
						 END
				          
						 IF ISNULL(@CST_Address2,'') <> ''
						 BEGIN
							IF ISNULL(@CST_Address1,'') = ''
							   EXEC prince.Common.ADR_Insert NULL,@ADR_LKP_Province,@ADR_LKP_City,@CST_Address2,@CST_PostalCode,1,@PRI_CPN_ID,NULL
							ELSE
							   EXEC prince.Common.ADR_Insert NULL,NULL,NULL,@CST_Address2,NULL,2,@PRI_CPN_ID,NULL
						 END 
				          
				         -- Added By Ghasem Babaie - 1401.03.26 
				         DECLARE @LKP_Contact_Mobile	[Common].[_INT_] = 0
				         DECLARE @LKP_Contact_Fax		[Common].[_INT_] = 0
				         DECLARE @LKP_Contact_HomePhone	[Common].[_INT_] = 0
				          
						-- -- شماره تلفن *************************
				         
						 IF ISNULL(@CST_Mobile,'') <> '' 
						 BEGIN
							-- Added By Ghasem Babaie - 1401.03.26
							SET @LKP_Contact_Mobile = [Common].[LKP_EngCaptionToID]('Contact_Mobile', N'تلفن همراه', 30)
							
							EXEC prince.Common.CIN_Insert @LKP_Contact_Mobile,@CST_Mobile,1,@PRI_CPN_ID,1,NULL
						 END
				         
						 IF ISNULL(@CST_Fax,'') <> ''
						 BEGIN
							-- Added By Ghasem Babaie - 1401.03.26
							SET @LKP_Contact_Fax = [Common].[LKP_EngCaptionToID]('Contact_Fax', N'فاكس', 462)
							
							EXEC prince.Common.CIN_Insert @LKP_Contact_Fax,@CST_Fax,1,@PRI_CPN_ID,1,NULL
						 END
				         
				         -- Added By Ghasem Babaie - 1401.03.26
						 SET @LKP_Contact_HomePhone = [Common].[LKP_EngCaptionToID]('Contact_HomePhone', N'تلفن منزل', 21)
				         
						 IF ISNULL(@CST_Phone1,'') <> ''
						 BEGIN
							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1							
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone1,@CIN_Order,@PRI_CPN_ID,1,NULL
						 END
				         
						 IF ISNULL(@CST_Phone2,'') <> ''
						 BEGIN
							IF ISNULL(@CST_Phone1,'') = ''
							   SET @CIN_IsDefault = 1
							ELSE
							   SET @CIN_IsDefault = 0
				                  
							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone2,@CIN_Order,@PRI_CPN_ID,@CIN_IsDefault,NULL
						 END
				         
						 IF ISNULL(@CST_Phone3,'') <> ''
						 BEGIN
							IF (ISNULL(@CST_Phone1,'') <> '') OR (ISNULL(@CST_Phone2,'') <> '')
							   SET @CIN_IsDefault = 0
							ELSE
							   SET @CIN_IsDefault = 1
				                  
							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone3,@CIN_Order,@PRI_CPN_ID,@CIN_IsDefault,NULL
						 END
				         
						 IF ISNULL(@CST_Phone4,'') <> ''
						 BEGIN
							IF (ISNULL(@CST_Phone1,'') <> '') OR (ISNULL(@CST_Phone2,'') <> '') OR (ISNULL(@CST_Phone3,'') <> '')
							   SET @CIN_IsDefault = 0
							ELSE
							   SET @CIN_IsDefault = 1

							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone4,@CIN_Order,@PRI_CPN_ID,@CIN_IsDefault,NULL
						 END
				         
						 IF ISNULL(@CST_Phone5,'') <> ''
						 BEGIN
							IF (ISNULL(@CST_Phone1,'') <> '') OR (ISNULL(@CST_Phone2,'') <> '') OR (ISNULL(@CST_Phone3,'') <> '') OR (ISNULL(@CST_Phone4,'') <> '')
							   SET @CIN_IsDefault = 0
							ELSE
							   SET @CIN_IsDefault = 1

							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone5,@CIN_Order,@PRI_CPN_ID,@CIN_IsDefault,NULL
						 END 
				          
						 IF ISNULL(@CST_Phone6,'') <> ''
						 BEGIN
							IF (ISNULL(@CST_Phone1,'') <> '') OR (ISNULL(@CST_Phone2,'') <> '') OR (ISNULL(@CST_Phone3,'') <> '') OR (ISNULL(@CST_Phone4,'') <> '') OR (ISNULL(@CST_Phone5,'') <> '')
							   SET @CIN_IsDefault = 0
							ELSE
							   SET @CIN_IsDefault = 1

							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CST_Phone6,@CIN_Order,@PRI_CPN_ID,@CIN_IsDefault,NULL
						 END  
				          
						-- -- بدهکاری / بستانکاری *************************
				         
						 --SET @ACN_CurrentAmount = 0
						 --SET @ACN_CurrentAmount = (SELECT ACN_CurrentAmount FROM Saffron.dbo.Account WHERE ACN_TBL_Holder = 20 AND ACN_HolderID = @CST_CPN_ID) 
				
				         				         
						  SET @PRI_ACN_ID = 0
						  SET @PRI_ACN_ID = (SELECT ACN_ID FROM prince.Common.Account WHERE ACN_TBL_Holder = 15 AND ACN_HolderID = @PRI_CPN_ID) 
				          
				         
						  IF (ISNULL(@ACN_CurrentAmount,0) <> 0)
						  BEGIN
				          
							 IF (@ACN_CurrentAmount > 0)
							 BEGIN
								SET @POP_From = @OpeningBalance_ID
								SET @POP_To = @PRI_ACN_ID
							 END
							 ELSE
							 BEGIN
								SET @POP_From = @PRI_ACN_ID
								SET @POP_To = @OpeningBalance_ID
								SET @ACN_CurrentAmount = ABS(@ACN_CurrentAmount)
							 END
				         
							 SET @Query = ''
							 SET @Query = ' EXEC prince.['+@FNY_Name+'].GeneralTransferCredit 29,27,'+CAST(@ACN_CurrentAmount AS NVARCHAR(2000))+','''+@Fny_Date+''',''00:00:01'','+
										  +LTRIM(RTRIM(STR(@POP_From)))+','''','+LTRIM(RTRIM(STR(@POP_To)))+','''',15,'+LTRIM(RTRIM(STR(@PRI_CPN_ID)))+',NULL,'+
										  ''''','''','''','''',''موجودي ابتداي دوره'','''','''','''','''','''','+'1,NULL,Null'
							 EXEC(@Query)  
				             
						 END         
					  END
					  
				  END TRY
				  BEGIN CATCH
				      SET @DO = -7
			
				      --CLOSE CST
			       --   DEALLOCATE CST	
				      RETURN 
				  END CATCH
				  				  
			--   FETCH NEXT FROM CST INTO  @CST_ID ,@CST_Name ,@CST_LKP_Title ,@CST_Code ,@CST_LKP_Group ,@CST_CostLevel ,@CST_SCP_ID ,@CST_CostChange ,
			--							 @CST_IsCredit ,@CST_MaxCredit ,@CST_BirthDate ,@CST_Phone1 ,@CST_Phone2 ,@CST_Mobile ,@CST_Fax ,@CST_Address1 ,
			--							 @CST_Address2 ,@CST_Description ,@CST_Type ,@CST_CPN_ID ,@CST_SaloonOrder ,@CST_Phone3 ,@CST_Phone4 ,@CST_Phone5 ,
			--							 @CST_Phone6 ,@CST_LKP_Section ,@CST_NationCode ,@CST_CommercialCode ,@CST_LKP_City ,@CST_LKP_State ,@CST_HTypePersion ,
			--							 @CST_DefaultDiscountType ,@CST_DefalutDiscountValue ,@CST_DateofMarriage ,@CST_PostalCode ,@CST_DefaultServiceType ,
			--							 @CST_DefalutServiceValue ,@CST_DontApplyTax,@ACN_CurrentAmount 
			--END
			--CLOSE CST
			--DEALLOCATE CST	       
		   
		    --IF (@Col_ID IS NULL) OR ((SELECT MAX([CST_ID]) FROM [saffron].[dbo].[Customer] WHERE [CST_ID] NOT IN (1,2)) = @Col_ID)
			--INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCustomer', 1)
	    
		END
    
    END
        --***************************************************
    IF (@Index IN (0,4)) -- پیک
    BEGIN     
        INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCustomer', 1)
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCourier')
		BEGIN 
			-- حساب افتتاحیه
			SET @OpeningBalance_ID = 0
			SET @OpeningBalance_ID = (SELECT ACN_ID FROM Prince.Common.Account WHERE ACN_TBL_Holder = 86)
		    
			-- نام سال مالی
			SELECT @FNY_Name = FNY_Name , @Fny_Date = FNY_OpenDate FROM Prince.Common.FinancialYear WHERE ISNULL(FNY_CloseDate_Int , 0)= 0    
		    		

			--DECLARE CRR CURSOR for
			--		SELECT [CRR_ID],[CRR_Name],[CRR_BDate],[CRR_IDNumber],[CRR_FatherName],[CRR_NationalCode],[CRR_Guarantee]
			--			  ,[CRR_Mobile],[CRR_Phone],[CRR_StartDate],[CRR_IsDisabled],[CRR_Address],[CRR_Description]
			--			  ,[CRR_CPN_ID]
			--		  FROM [saffron].[dbo].[Courier]
			--		 WHERE
			--		  1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  	--			               CASE WHEN [CRR_ID] = @Col_ID THEN 1 ELSE 0 END END 

			--OPEN CRR
			--FETCH NEXT FROM CRR INTO @CRR_ID,@CRR_Name,@CRR_BDate,@CRR_IDNumber,@CRR_FatherName,@CRR_NationalCode,@CRR_Guarantee,		
			--						 @CRR_Mobile,@CRR_Phone,@CRR_StartDate,@CRR_IsDisabled,@CRR_Address,@CRR_Description,	
			--						 @CRR_CPN_ID
			--WHILE @@FETCH_STATUS =0
			--BEGIN  
			
			
			     BEGIN TRY
			     
			         SELECT @CRR_ID = [CRR_ID],
							@CRR_Name = [CRR_Name],
							@CRR_BDate = [CRR_BDate],
							@CRR_IDNumber = [CRR_IDNumber],
							@CRR_FatherName = [CRR_FatherName],
							@CRR_NationalCode = [CRR_NationalCode],
							@CRR_Guarantee = [CRR_Guarantee],
							@CRR_Mobile = [CRR_Mobile],
							@CRR_Phone = [CRR_Phone],
							@CRR_StartDate = [CRR_StartDate],
							@CRR_IsDisabled = [CRR_IsDisabled],
							@CRR_Address = [CRR_Address],
							@CRR_Description = [CRR_Description],
							@CRR_CPN_ID = [CRR_CPN_ID],
							@ACN_CurrentAmount = [ACN_CurrentAmount]
                      FROM [saffron].[dbo].[Courier]
                      LEFT JOIN Saffron.dbo.Account ON ACN_TBL_Holder = 20 AND ACN_HolderID = [CRR_CPN_ID]
					 WHERE
					  1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  				               CASE WHEN [CRR_ID] = @Col_ID THEN 1 ELSE 0 END END  

			         
			       
				 -- ثبت اطلاعات اشخاص ************************* 
		         
		         
					 SET @PRI_CPN_ID = 0
					 EXEC prince.Common.CPN_Insert  NULL,@CRR_Name,'',NULL,NULL,NULL,NULL,@CRR_Description,0,
													0,NULL,@CRR_NationalCode,@CRR_BDate,@CRR_FatherName,@CRR_IDNumber,1,1,0,0,0,1,1,
													1,0,0,'',0,0,0,0,0,0,0,@CRR_IsDisabled,0,0,0,NULL,1,@CRR_Guarantee,@CRR_StartDate,
													0 ,0,0,0,0,0,0,0,0,0,0,'',0,@CRR_Mobile,'',0,0,'',1,1,@PRI_CPN_ID OUTPUT
			 
					 IF ISNULL (@PRI_CPN_ID,0)>0
					 BEGIN
				         
				       --  INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (15,@CRR_ID,@CRR_Name,@PRI_CPN_ID,@CRR_Name) 
				         
						 UPDATE prince.Common.CompanyPerson
							SET CPN_Parent_ID = 0 /*CPN_ID*/
						  WHERE CPN_ID = @PRI_CPN_ID      
				          
			             
						 -- آدرس *************************
				          
						 IF ISNULL(@CRR_Address,'') <> ''
						 BEGIN
							EXEC prince.Common.ADR_Insert NULL,NULL,NULL,@CRR_Address,'',1,@PRI_CPN_ID,NULL
						 END
				          
						 -- شماره تلفن *************************
				         
						 IF ISNULL(@CRR_Mobile,'') <> '' 
						 BEGIN
							SET @LKP_Contact_Mobile = [Common].[LKP_EngCaptionToID]('Contact_Mobile', N'تلفن همراه', 30)
							
							EXEC prince.Common.CIN_Insert @LKP_Contact_Mobile,@CRR_Mobile,1,@PRI_CPN_ID,1,NULL
						 END
				         
						 IF ISNULL(@CRR_Phone,'') <> ''
						 BEGIN
							-- Added By Ghasem Babaie - 1401.03.26
							SET @LKP_Contact_HomePhone = [Common].[LKP_EngCaptionToID]('Contact_HomePhone', N'تلفن منزل', 21)
							 
							SET @CIN_Order = ISNULL((SELECT MAX(CIN_Order) FROM prince.Common.ContactInformation WHERE CIN_LKP_Type = @LKP_Contact_HomePhone AND CIN_CPN_ID = @PRI_CPN_ID),0)+1
							EXEC prince.Common.CIN_Insert @LKP_Contact_HomePhone,@CRR_Phone,@CIN_Order,@PRI_CPN_ID,1,NULL
						 END
							          
						 -- بدهکاری / بستانکاری *************************
				         
						 --SET @ACN_CurrentAmount = 0
						 --SET @ACN_CurrentAmount = (SELECT ACN_CurrentAmount FROM Saffron.dbo.Account WHERE ACN_TBL_Holder = 20 AND ACN_HolderID = @CRR_CPN_ID) 
				         
						  SET @PRI_ACN_ID = 0
						  SET @PRI_ACN_ID = (SELECT ACN_ID FROM prince.Common.Account WHERE ACN_TBL_Holder = 15 AND ACN_HolderID = @PRI_CPN_ID) 
				          
				         
						  IF (ISNULL(@ACN_CurrentAmount,0) <> 0)
						  BEGIN
				          
							 IF (@ACN_CurrentAmount > 0)
							 BEGIN
								SET @POP_From = @OpeningBalance_ID
								SET @POP_To = @PRI_ACN_ID
							 END
							 ELSE
							 BEGIN
								SET @POP_From = @PRI_ACN_ID
								SET @POP_To = @OpeningBalance_ID
								SET @ACN_CurrentAmount = ABS(@ACN_CurrentAmount)
							 END
				         
							 SET @Query = ''
							 SET @Query = ' EXEC prince.['+@FNY_Name+'].GeneralTransferCredit 29,27,'+CAST(@ACN_CurrentAmount AS NVARCHAR(2000))+','''+@Fny_Date+''',''00:00:01'','+
										  +LTRIM(RTRIM(STR(@POP_From)))+','''','+LTRIM(RTRIM(STR(@POP_To)))+','''',15,'+LTRIM(RTRIM(STR(@PRI_CPN_ID)))+',NULL,'+
										  ''''','''','''','''',''موجودي ابتداي دوره'','''','''','''','''','''','+'1,NULL,Null'
							 EXEC(@Query)  
				             
						 END         	
				
					END
			
				  END TRY
				  BEGIN CATCH
				      SET @DO = -8
				      --CLOSE CRR
			       --   DEALLOCATE CRR	
				      RETURN 
				  END CATCH			
			
			--   FETCH NEXT FROM CRR INTO  @CRR_ID,@CRR_Name,@CRR_BDate,@CRR_IDNumber,@CRR_FatherName,@CRR_NationalCode,@CRR_Guarantee,		
			--							 @CRR_Mobile,@CRR_Phone,@CRR_StartDate,@CRR_IsDisabled,@CRR_Address,@CRR_Description,	
			--							 @CRR_CPN_ID
			--END
			--CLOSE CRR
			--DEALLOCATE CRR	
		   
	        IF (@Col_ID IS NULL) OR ((SELECT MAX([CRR_ID]) FROM [Saffron].[dbo].[Courier]) = @Col_ID)
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCourier', 1)
	    
		END
    
    END
    --***************************************************
    IF (@Index IN (0,5)) -- هزینه
    BEGIN 
    
		UPDATE [Saffron].[dbo].[CostIncomeType]
		   SET [CIT_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CIT_Name], N'ی', N'ي'), N'ک', N'ك')));
	    
	    
		UPDATE [Prince].[Common].[CostIncomeType]
		   SET [CIT_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CIT_Name], N'ی', N'ي'), N'ک', N'ك')));
	 
	 
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCostIncomeType')
		BEGIN 
	 --  *****************************************
		    
 			DECLARE CIT CURSOR for
				   SELECT [CIT_Name],[CIT_AbbName],[CIT_Description]
					 FROM [saffron].[dbo].[CostIncomeType]
					WHERE [CIT_ID] > 4 AND
					   1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  				               CASE WHEN [CIT_ID] = @Col_ID THEN 1 ELSE 0 END END 
			OPEN CIT
			FETCH NEXT FROM CIT INTO @CIT_Name,@CIT_AbbName,@CIT_Description
			WHILE @@FETCH_STATUS =0
			BEGIN     
				IF NOT EXISTS (SELECT * FROM [Prince].[Common].[CostIncomeType] WHERE [CIT_Name] = @CIT_Name)    
					EXEC [Prince].Common.CIT_Insert @CIT_Name,@CIT_AbbName,NULL,0,@CIT_Description,0,0,1,0,'',NULL
				 
				FETCH NEXT FROM CIT INTO @CIT_Name,@CIT_AbbName,@CIT_Description
			END
			CLOSE CIT
			DEALLOCATE CIT	       
		    
		    IF (@Col_ID IS NULL) OR ((SELECT MAX([CIT_ID]) FROM [saffron].[dbo].[CostIncomeType] WHERE [CIT_ID] > 4) = @Col_ID)
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCostIncomeType', 1)
	    
		END    
    
    END
    --***************************************************
    IF (@Index IN (0,6)) -- PrintLine
    BEGIN 
    
		UPDATE [Saffron].[dbo].[PrintLine]
		   SET [PTL_Name] = LTRIM(RTRIM(REPLACE(REPLACE([PTL_Name], N'ی', N'ي'), N'ک', N'ك')));
	    
	    
		UPDATE [Prince].[Common].[PrintLine]
		   SET [PTL_Name] = LTRIM(RTRIM(REPLACE(REPLACE([PTL_Name], N'ی', N'ي'), N'ک', N'ك')));
	 
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferPrintLine')
		BEGIN  
		    
		    --SAC-432
		    TRUNCATE TABLE [Prince].[Common].[PrintLine]

 			DECLARE PTL CURSOR for
				   SELECT [PTL_ID],[PTL_Name],[PTL_PrinterName],[PTL_Type],[PTL_RightMargin],[PTL_LeftMargin],[PTL_TopMargin],[PTL_BottomMargin]
						 ,[PTL_Font],[PTL_FontSize],[PTL_FontStyle],[PTL_TitleFont],[PTL_TitleFontSize],[PTL_PrintCount]
					 FROM [saffron].[dbo].[PrintLine]
			OPEN PTL
			FETCH NEXT FROM PTL INTO @SAF_PTL_ID,@PTL_Name,@PTL_PrinterName,@PTL_Type,@PTL_RightMargin,@PTL_LeftMargin,@PTL_TopMargin,@PTL_BottomMargin,
									 @PTL_Font,@PTL_FontSize,@PTL_FontStyle,@PTL_TitleFont,@PTL_TitleFontSize,@PTL_PrintCount	
			WHILE @@FETCH_STATUS =0
			BEGIN     

                     IF (@PTL_Type = 0)
                     BEGIN
                        SET @PRL_PTL_Width = 210
                        SET @PRL_PTL_Length = 297 
                        SET @PRL_PTL_BottomFontSize = 10
                     END
                     ELSE IF (@PTL_Type = 1)
                     BEGIN
                        SET @PRL_PTL_Width = 297
                        SET @PRL_PTL_Length = 210 
                        SET @PRL_PTL_BottomFontSize = 10
                     END
                     ELSE IF (@PTL_Type = 2)
                     BEGIN
                        SET @PRL_PTL_Width = 148
                        SET @PRL_PTL_Length = 210 
                        SET @PRL_PTL_BottomFontSize = 10
                     END
                     ELSE IF (@PTL_Type = 3)
                     BEGIN
                        SET @PRL_PTL_Width = 210
                        SET @PRL_PTL_Length = 148 
                        SET @PRL_PTL_BottomFontSize = 10
                     END
                     ELSE IF (@PTL_Type = 4)
                     BEGIN
                        SET @PRL_PTL_Width = 56
                        SET @PRL_PTL_Length = 3276 
                        SET @PRL_PTL_BottomFontSize = 10
                     END
                     ELSE IF (@PTL_Type = 5)
                     BEGIN
                        SET @PRL_PTL_Width = 78
                        SET @PRL_PTL_Length = 3276 
                        SET @PRL_PTL_BottomFontSize = 12
                     END
                      
				--  IF NOT EXISTS (SELECT * FROM [Prince].[Common].[PrintLine] WHERE [PTL_Name] = @PTL_Name )    
					 EXEC [Prince].Common.PTL_INSERT @PTL_Name,@PTL_PrinterName,@PTL_Type,@PTL_RightMargin,@PTL_LeftMargin,@PTL_TopMargin,@PTL_BottomMargin,
													 @PTL_Font,@PTL_FontSize,@PTL_FontStyle,@PTL_TitleFont,@PTL_TitleFontSize,@PRL_PTL_BottomFontSize,
													 @PRL_PTL_Width,@PRL_PTL_Length,@PTL_PrintCount,@PRI_PTL_ID OUTPUT
				    
				    
				    
				     INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (53,@SAF_PTL_ID,@PTL_Name,@PRI_PTL_ID,@PTL_Name) 
				    
			   FETCH NEXT FROM PTL INTO @SAF_PTL_ID,@PTL_Name,@PTL_PrinterName,@PTL_Type,@PTL_RightMargin,@PTL_LeftMargin,@PTL_TopMargin,@PTL_BottomMargin,
										@PTL_Font,@PTL_FontSize,@PTL_FontStyle,@PTL_TitleFont,@PTL_TitleFontSize,@PTL_PrintCount	
			END
			CLOSE PTL
			DEALLOCATE PTL	 
			
			IF (@Col_ID IS NULL)      
			    INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferPrintLine', 1)
	    
		END    
        
    END
    --***************************************************
    IF (@Index IN (0,7)) -- کالا
    BEGIN 
    
	   IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCommodity')
	   BEGIN
	 
		   SET @PRI_DefaultPartID = (SELECT SST_Value FROM Common.SystemSetting WHERE SST_Title = 'DefaultPartID' AND SST_UserID = 0)
		   SET @PRI_DefaultPlaceID = (SELECT SST_Value FROM Common.SystemSetting WHERE SST_Title = 'DefaultPlaceID' AND SST_UserID = 0)

           SET @PRI_DefaultCMDUnitID = (SELECT TOP(1) LKP_ID FROM [PRINCE].[Common].[Lookup] WHERE ([LKP_Type] = 1) AND (LKP_Saffron = 1) AND (LKP_Order = 1))


		   SELECT @FNY_Name = FNY_Name , @Fny_Date = FNY_OpenDate FROM Prince.Common.FinancialYear WHERE ISNULL(FNY_CloseDate_Int , 0)= 0    

           IF NOT EXISTS (SELECT 1 FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 8))
           BEGIN
			   UPDATE [Saffron].[dbo].[Commodity]
				   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
			    
			    
			   UPDATE [Prince].[Common].[Commodity]
				   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
		    
				-- تغییر نام کالاهای هم نام
				;WITH CTE
				 AS
				 (
				SELECT * 
				  FROM (
				SELECT CMD_ID,CMD_Name,ROW_NUMBER() OVER (PARTITION BY CMD_Name
					 ORDER BY CMD_ID  
					) rn		        		  
				  FROM [Saffron].[dbo].[Commodity]   
				 WHERE CMD_Name IN (
				 SELECT CMD_Name       		  
				  FROM [Saffron].[dbo].[Commodity] 
			  GROUP BY CMD_Name
				HAVING COUNT(1) > 1)
				) A WHERE RN > 1
				)
				
				UPDATE SC
				   SET CMD_Name = SC.CMD_Name+' '+CAST(RN AS NVARCHAR(200))
				  FROM [Saffron].[dbo].[Commodity] SC
				  JOIN CTE ON (SC.CMD_ID = CTE.CMD_ID)	
		    END
		    
			SET @PRI_LKP_Price_Usual =    (SELECT LKP_ID FROM prince.Common.[Lookup] WHERE LKP_Type = 16 AND LKP_Order = 1 AND LKP_Saffron = 1)
			SET @PRI_LKP_Price_ORG =      (SELECT LKP_ID FROM prince.Common.[Lookup] WHERE LKP_Type = 16 AND LKP_Order = 2 AND LKP_Saffron = 1)
			SET @PRI_LKP_Price_CS =		  (SELECT LKP_ID FROM prince.Common.[Lookup] WHERE LKP_Type = 16 AND LKP_Order = 3 AND LKP_Saffron = 1)
			SET @PRI_LKP_Price_Student =  (SELECT LKP_ID FROM prince.Common.[Lookup] WHERE LKP_Type = 16 AND LKP_Order = 4 AND LKP_Saffron = 1)
			SET @PRI_LKP_Price_Special =  (SELECT LKP_ID FROM prince.Common.[Lookup] WHERE LKP_Type = 16 AND LKP_Order = 5 AND LKP_Saffron = 1)

 		--	DECLARE CMD CURSOR for
			--		SELECT [CMD_ID],[CMD_Name],[CMD_AbbName],[CMD_LKP_Unit],[CMD_LKP_Group],[CMD_PTL_ID],[CMD_InCost],[CMD_Cost1]
			--			  ,[CMD_Cost2],[CMD_Cost3],[CMD_Cost4],[CMD_Cost5],[CMD_Description],[CMD_Order],[CMD_HotKeyCode]
			--			  ,[CMD_IMG_ID],[CMD_IsFood],[CMD_InitialCount],[CMD_LKP_RecGrpID],[CMD_IsDisabled],[CMD_LKP_Section]
			--			  ,[CMD_PrintOrder],[CMD_Saloon_PTL_ID1],[CMD_Saloon_PTL_ID2],[CMD_Out_PTL_ID1],[CMD_Out_PTL_ID2]
			--			  ,[CMD_IsIngredient],[CMD_UseProductionForProfit],[CMD_UserProductionForStore],[CMD_CountForProduct]
			--			  ,[CMD_SalesCount],[CMD_CurrentAmount],[CMD_MinimumCount],[CMD_InputCount],[CMD_Saloon_PTL_ID3],[CMD_Out_PTL_ID3]
			--		  FROM [saffron].[dbo].[Commodity]
			--		 WHERE 
			--		 1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  	--			               CASE WHEN [CMD_ID] = @Col_ID THEN 1 ELSE 0 END END 
			--OPEN CMD
			--FETCH NEXT FROM CMD INTO @CMD_ID,@CMD_Name,@CMD_AbbName,@CMD_LKP_Unit,@CMD_LKP_Group,@CMD_PTL_ID,@CMD_InCost,@CMD_Cost1,@CMD_Cost2,
			--						 @CMD_Cost3,@CMD_Cost4,@CMD_Cost5,@CMD_Description,@CMD_Order,@CMD_HotKeyCode,@CMD_IMG_ID,@CMD_IsFood,
			--						 @CMD_InitialCount,@CMD_LKP_RecGrpID,@CMD_IsDisabled,@CMD_LKP_Section,@CMD_PrintOrder,@CMD_Saloon_PTL_ID1,
			--						 @CMD_Saloon_PTL_ID2,@CMD_Out_PTL_ID1,@CMD_Out_PTL_ID2,@CMD_IsIngredient,@CMD_UseProductionForProfit,
			--						 @CMD_UserProductionForStore,@CMD_CountForProduct,@CMD_SalesCount,@CMD_CurrentAmount,@CMD_MinimumCount,
			--						 @CMD_InputCount,@CMD_Saloon_PTL_ID3,@CMD_Out_PTL_ID3		
			--WHILE @@FETCH_STATUS =0
			--BEGIN    
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*      
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CMD_FoodContents'' AND Object_ID = Object_ID(N''[dbo].[Commodity]''))
		    BEGIN
		       ALTER TABLE [dbo].[Commodity]
		       ADD CMD_FoodContents nvarchar(2048)		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CMD_IsHide_Kiosk'' AND Object_ID = Object_ID(N''[dbo].[Commodity]''))
		    BEGIN
		       ALTER TABLE [dbo].[Commodity]
		       ADD CMD_IsHide_Kiosk TINYINT		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CLI_Type'' AND Object_ID = Object_ID(N''[dbo].[CommodityLargeImage]''))
		    BEGIN
		       ALTER TABLE [dbo].[CommodityLargeImage]
		       ADD CLI_Type INT		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query 		      
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CMD_IsHide_VirtualMenuApp'' AND Object_ID = Object_ID(N''[dbo].[Commodity]''))
		    BEGIN
		       ALTER TABLE [dbo].[Commodity]
		       ADD CMD_IsHide_VirtualMenuApp TINYINT		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CMD_LKP_KioskAppGroup'' AND Object_ID = Object_ID(N''[dbo].[Commodity]''))
		    BEGIN
		       ALTER TABLE [dbo].[Commodity]
		       ADD CMD_LKP_KioskAppGroup INT		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 
		    SET @Query = N'
		          
		    IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE Name = N''CMD_LKP_VirtualMenuAppGroup'' AND Object_ID = Object_ID(N''[dbo].[Commodity]''))
		    BEGIN
		       ALTER TABLE [dbo].[Commodity]
		       ADD CMD_LKP_VirtualMenuAppGroup INT		    
		    END '
		    
		    EXECUTE [saffron].[sys].sp_executesql @Query
		    --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 
		          
		         BEGIN TRY 
		         
		             SELECT @CMD_ID = [CMD_ID],
							@CMD_Name = [CMD_Name],
							@CMD_AbbName = [CMD_AbbName],
							@CMD_LKP_Unit = [CMD_LKP_Unit],
							@CMD_LKP_Group = [CMD_LKP_Group],
							@CMD_PTL_ID = [CMD_PTL_ID],
							@CMD_InCost = [CMD_InCost],
							@CMD_Cost1 = [CMD_Cost1],
							@CMD_Cost2 = [CMD_Cost2],
							@CMD_Cost3 = [CMD_Cost3],
							@CMD_Cost4 = [CMD_Cost4],
							@CMD_Cost5 = [CMD_Cost5],
							@CMD_Description = [CMD_Description],
							@CMD_Order = [CMD_Order],
							@CMD_HotKeyCode = [CMD_HotKeyCode],
							@CMD_IMG_ID = [CMD_IMG_ID],
							@CMD_IsFood = [CMD_IsFood],
							@CMD_InitialCount = [CMD_InitialCount],
							@CMD_LKP_RecGrpID = [CMD_LKP_RecGrpID],
							@CMD_IsDisabled = [CMD_IsDisabled],
							@CMD_LKP_Section = [CMD_LKP_Section],
							@CMD_PrintOrder = [CMD_PrintOrder],
							@CMD_Saloon_PTL_ID1 = [CMD_Saloon_PTL_ID1],
							@CMD_Saloon_PTL_ID2 = [CMD_Saloon_PTL_ID2],
							@CMD_Out_PTL_ID1 = [CMD_Out_PTL_ID1],
							@CMD_Out_PTL_ID2 = [CMD_Out_PTL_ID2],
							@CMD_IsIngredient = [CMD_IsIngredient],
							@CMD_UseProductionForProfit = 0,-- [CMD_UseProductionForProfit],
							@CMD_UserProductionForStore = [CMD_UserProductionForStore],
							@CMD_CountForProduct = [CMD_CountForProduct],
							@CMD_SalesCount = [CMD_SalesCount],
							@CMD_CurrentAmount = [CMD_CurrentAmount],
							@CMD_MinimumCount = [CMD_MinimumCount],
							@CMD_InputCount = [CMD_InputCount],
							@CMD_Saloon_PTL_ID3 = [CMD_Saloon_PTL_ID3],
							@CMD_Out_PTL_ID3 = [CMD_Out_PTL_ID3]
					   FROM [saffron].[dbo].[Commodity]
					  WHERE 
					    1 = CASE WHEN ISNULL(@Col_ID,0) = 0 THEN 1 ELSE 
  				                 CASE WHEN [CMD_ID] = @Col_ID THEN 1 ELSE 0 END END 	

                      
                      --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 
                      SET @Param = N'@POUT varchar(2048) OUTPUT'  
                      SET @Query = N'SELECT @POUT = CMD_FoodContents
         			                   FROM [saffron].[dbo].[Commodity]
			                          WHERE ([CMD_ID] = '+LTRIM(RTRIM(STR(@Col_ID)))+') '

			          EXECUTE sp_executesql @Query ,@Param,@CMD_FoodContents OUTPUT    
			 
			          SET @CMD_FoodContents = ISNULL(@CMD_FoodContents , '') 
		              --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
		              
		              SET @Param = N'@POUT TINYINT OUTPUT'  
                      SET @Query = N'SELECT @POUT = CMD_IsHide_Kiosk
         			                   FROM [saffron].[dbo].[Commodity]
			                          WHERE ([CMD_ID] = '+LTRIM(RTRIM(STR(@Col_ID)))+') '

			          EXECUTE sp_executesql @Query ,@Param,@CMD_IsHide_Kiosk OUTPUT    
			 
			          SET @CMD_IsHide_Kiosk = ISNULL(@CMD_IsHide_Kiosk , 0)  
		              --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
		              
		              SET @Param = N'@POUT TINYINT OUTPUT'  
                      SET @Query = N'SELECT @POUT = CMD_IsHide_VirtualMenuApp
         			                   FROM [saffron].[dbo].[Commodity]
			                          WHERE ([CMD_ID] = '+LTRIM(RTRIM(STR(@Col_ID)))+') '

			          EXECUTE sp_executesql @Query ,@Param,@CMD_IsHide_VirtualMenuApp OUTPUT    
			 
			          SET @CMD_IsHide_VirtualMenuApp = ISNULL(@CMD_IsHide_VirtualMenuApp , 0)  
		              --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
		              
		              SET @Param = N'@POUT INT OUTPUT'  
                      SET @Query = N'SELECT @POUT = CMD_LKP_KioskAppGroup
         			                   FROM [saffron].[dbo].[Commodity]
			                          WHERE ([CMD_ID] = '+LTRIM(RTRIM(STR(@Col_ID)))+') '

			          EXECUTE sp_executesql @Query ,@Param,@CMD_LKP_KioskAppGroup OUTPUT    
			 
			          SET @CMD_LKP_KioskAppGroup = ISNULL(@CMD_LKP_KioskAppGroup , 0)  
		              --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	              
		              SET @Param = N'@POUT INT OUTPUT'  
                      SET @Query = N'SELECT @POUT = CMD_LKP_VirtualMenuAppGroup
         			                   FROM [saffron].[dbo].[Commodity]
			                          WHERE ([CMD_ID] = '+LTRIM(RTRIM(STR(@Col_ID)))+') '

			          EXECUTE sp_executesql @Query ,@Param,@CMD_LKP_VirtualMenuAppGroup OUTPUT    
			 
			          SET @CMD_LKP_VirtualMenuAppGroup = ISNULL(@CMD_LKP_VirtualMenuAppGroup , 0)  
		              --*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
		               
		          -- خطوط چاپ غذا *************************
		             
				     SELECT @PRI_CMD_PTL_ID = [TRD_NewID]		  FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_PTL_ID)
					 SELECT @PRI_CMD_Saloon_PTL_ID1 = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Saloon_PTL_ID1)
					 SELECT @PRI_CMD_Saloon_PTL_ID2 = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Saloon_PTL_ID2)
					 SELECT @PRI_CMD_Out_PTL_ID1 = [TRD_NewID]	  FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Out_PTL_ID1)
					 SELECT @PRI_CMD_Out_PTL_ID2 = [TRD_NewID]	  FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Out_PTL_ID2)
					 SELECT @PRI_CMD_Saloon_PTL_ID3 = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Saloon_PTL_ID3)
					 SELECT @PRI_CMD_Out_PTL_ID3 = [TRD_NewID]	  FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 53) AND ([TRD_OldID] = @CMD_Out_PTL_ID3)
		          
				  -- گروه فیش غذا *************************
		             
				     SELECT @PRI_LKP_RecGrpID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CMD_LKP_RecGrpID)

				  -- سطح واحد *************************
			      
			         SELECT @PRI_CMD_LKP_Unit_ID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CMD_LKP_Unit)

				  -- گروه کالا *************************
					 
					 SELECT @PRI_CMD_Group_ID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CMD_LKP_Group)

				  -- گروه کالا در کیوسک *************************
					 IF ISNULL(@CMD_LKP_KioskAppGroup , 0) = 0
					    SET @PRI_CMD_KioskAppGroup_ID = 0
					 ELSE   
					    SELECT @PRI_CMD_KioskAppGroup_ID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CMD_LKP_KioskAppGroup)
                    
                  --  گروه کالا در منو مجازی *************************
					 IF ISNULL(@CMD_LKP_VirtualMenuAppGroup , 0) = 0
					    SET @PRI_CMD_VirtualMenuAppGroup_ID = 0
					 ELSE 
					    SELECT @PRI_CMD_VirtualMenuAppGroup_ID	 = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 3) AND ([TRD_OldID] = @CMD_LKP_VirtualMenuAppGroup)

  
				  -- ثبت اطلاعات کالا
			         
			          IF (LTRIM(RTRIM(@CMD_AbbName)) = '')  OR (@CMD_AbbName LIKE '%[^0-9]%') OR (@CMD_IsFood = 0)
			            SET @CMD_AbbName = NULL 
		            
					  /*
						Added By Ghasem Babaie - 1399.07.14
						با صحبت با خانم زارعی مقرر شد این مقدار در صورت منفی بودن و نال بودن به صفر تغییر پیدا کند
					  */
					  IF ((ISNULL(@CMD_LKP_Section,0) = 0) OR (@CMD_LKP_Section < 0))
						 SET @CMD_LKP_Section = 0
						 
					  -- Added By Ghasem Babaie - 1401.03.26 
				      DECLARE @LKP_CmdType_Commodity	[Common].[_INT_] = 0
				      SET @LKP_CmdType_Commodity = [Common].[LKP_EngCaptionToID]('CmdType_Commodity', N'كالا', 145)

					  IF NOT EXISTS (SELECT * FROM [Prince].[Common].[Commodity] WHERE [CMD_Name] = @CMD_Name )    
						 EXEC [Prince].Common.CMD_Insert @CMD_Name = @CMD_Name,@CMD_AbbName = @CMD_Name,@CMD_Code = @CMD_AbbName,@CMD_Description = @CMD_Description,
														 @CMD_LKP_Type = @LKP_CmdType_Commodity,@CMD_PLC_Store = @PRI_DefaultPlaceID,@CMD_PLC_Department = @PRI_DefaultPartID,
														 @CMD_CMC_ID = NULL,@CMD_MinimumAvailable = @CMD_MinimumCount,@CMD_MaximumAvailable = NULL,@CMD_PrintBarcode = 0,
														 @CMD_HasSerialNumber = 0,@CMD_HasTax = 0,@CMD_HasToll = 0,@CMD_Image = NULL,@CMD_IsDisabled = @CMD_IsDisabled,@CMD_HotKeyCode = @CMD_HotKeyCode,
														 @UserID = 1,@CMD_HasPercent = 0,@CMD_Market_Percent = 0,@CMD_Tax = 0,@CMD_Toll = 0,@CMD_Weighted = 0,@CMD_Display_BTN = 0,
														 @CMD_ORDER = @CMD_Order,@CMD_CoverPrice = 0,@CMD_IncDec = 0,@CMD_CoverPercent = 0,@CMD_IsEssential = 0,@CMD_AvailableToCustomer = 0,
														 @CMD_IsIngredient = @CMD_IsIngredient,@CMD_UserProductionForStore = @CMD_UserProductionForStore, @CMD_KitchenUse = 0,
														 @CMD_CountForProduct = @CMD_CountForProduct,@CMD_IsCalcGuest = 0,@CMD_FirstCategory_ID = 0,@CMD_SecondCategory_ID = 0,
														 @CMD_ThirdCategory_ID = 0,@CMD_CategoryCaption = '',@CMD_PTL_ID = @PRI_CMD_PTL_ID,@CMD_IMG_ID = @CMD_IMG_ID,@CMD_IsFood = @CMD_IsFood,
														 @CMD_InitialCount = 0,@CMD_LKP_RecGrpID = @PRI_LKP_RecGrpID,@CMD_LKP_Section = @CMD_LKP_Section,@CMD_PrintOrder = @CMD_PrintOrder,
														 @CMD_Saloon_PTL_ID1 = @PRI_CMD_Saloon_PTL_ID1,@CMD_Saloon_PTL_ID2 = @PRI_CMD_Saloon_PTL_ID2,@CMD_Out_PTL_ID1 = @PRI_CMD_Out_PTL_ID1,
														 @CMD_Out_PTL_ID2 = @PRI_CMD_Out_PTL_ID2,@CMD_SalesCount = @CMD_SalesCount,@CMD_InputCount = @CMD_InputCount,
														 @CMD_Saloon_PTL_ID3 = @PRI_CMD_Saloon_PTL_ID3,@CMD_Out_PTL_ID3 = @PRI_CMD_Out_PTL_ID3,@CMD_LKP_Group	= @PRI_CMD_Group_ID,			                                 
														 @CMD_UseProductionForProfit = @CMD_UseProductionForProfit, @CMD_Saloon_PTL_ID4 = 0,@CMD_Out_PTL_ID4 = 0, @CMD_Saloon_PTL_ID5 = 0,
														 @CMD_Out_PTL_ID5 = 0, @CMD_IsRent = 0, @CMD_IsBrand = 1, @CMD_MaxSaleInOneFactor = 0, @CMD_SourceBranchCode = '', 
														 @CMD_FoodContents = @CMD_FoodContents, @CMD_IsHide_Kiosk = @CMD_IsHide_Kiosk, @CMD_IsHide_VirtualMenuApp = @CMD_IsHide_VirtualMenuApp,
														 @CMD_LKP_KioskAppGroup = @PRI_CMD_KioskAppGroup_ID , @CMD_LKP_VirtualMenuAppGroup = @PRI_CMD_VirtualMenuAppGroup_ID,		
														 @CMD_ID = @PRI_CMD_ID OUTPUT
						                                  
					  IF @PRI_CMD_ID > 0
					  BEGIN

					     INSERT INTO [Common].[TransferDataSaffronToPrince] VALUES (8,@CMD_ID,@CMD_Name,@PRI_CMD_ID,@CMD_Name) 
					     
						 -- ثبت سطح واحد  
						 
						 IF ISNULL(@PRI_CMD_LKP_Unit_ID,0) = 0
						    SET @PRI_CMD_LKP_Unit_ID = @PRI_DefaultCMDUnitID 
						
						 EXEC [Prince].Common.CNU_Insert @PRI_CMD_ID,NULL,NULL,@PRI_CMD_LKP_Unit_ID,NULL,0,1,0,@PRI_CNU_ID OUTPUT,@PRI_CUM_ID OUTPUT
					     
						 -- ثبت سطوح قیمت
						 IF (@PRI_CNU_ID > 0) AND (@PRI_CUM_ID > 0)
						 BEGIN
							 IF (@CMD_IsFood = 0) -- کالا و مواد اولیه
							 BEGIN
								SET @PRI_Profit = ABS(@CMD_Cost1 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_Usual,@PRI_Profit,@CMD_InCost,@CMD_Cost1,0,1,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL
							 END
							 ELSE
							 BEGIN
								SET @PRI_Profit = ABS(@CMD_Cost1 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_Usual,@PRI_Profit,@CMD_InCost,@CMD_Cost1,0,1,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL 
					            
								SET @PRI_Profit = ABS(@CMD_Cost2 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_ORG,@PRI_Profit,@CMD_InCost,@CMD_Cost2,0,0,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL
					            
								SET @PRI_Profit = ABS(@CMD_Cost3 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_CS,@PRI_Profit,@CMD_InCost,@CMD_Cost3,0,0,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL
					            
								SET @PRI_Profit = ABS(@CMD_Cost4 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_Student ,@PRI_Profit,@CMD_InCost,@CMD_Cost4,0,0,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL
					            
								SET @PRI_Profit = ABS(@CMD_Cost5 - @CMD_InCost)
								EXEC [Prince].Common.PRL_Insert @PRI_CMD_ID,@PRI_LKP_Price_Special,@PRI_Profit,@CMD_InCost,@CMD_Cost5,0,0,@PRI_CNU_ID,@PRI_CUM_ID,0,NULL
							 END
						 END
					     
						 -- ثبت عکس 
				 SET @Query = N'	
				         DECLARE @CLI_Image                  [Common].[_MAXBINARY_] 	 
						 IF EXISTS (SELECT * FROM Saffron.dbo.CommodityLargeImage WHERE CLI_CMD_ID = '+CAST(@CMD_ID AS NVARCHAR(200))+ ' AND CLI_Type = 1) 
						 BEGIN
					        
							SET @CLI_Image = (SELECT CLI_Image FROM Saffron.dbo.CommodityLargeImage WHERE CLI_CMD_ID = '+CAST(@CMD_ID AS NVARCHAR(200))+ ' AND CLI_Type = 1)
					        
							EXEC [Prince].Common.CLI_Insert @CLI_Image,'+CAST(@PRI_CMD_ID AS NVARCHAR(200))+ ',1,1,NULL
						 END 
			             
			             IF EXISTS (SELECT * FROM Saffron.dbo.CommodityLargeImage WHERE CLI_CMD_ID = '+CAST(@CMD_ID AS NVARCHAR(200))+ ' AND CLI_Type = 2) 
						 BEGIN
					        
							SET @CLI_Image = (SELECT CLI_Image FROM Saffron.dbo.CommodityLargeImage WHERE CLI_CMD_ID = '+CAST(@CMD_ID AS NVARCHAR(200))+ ' AND CLI_Type = 2)
					        
							EXEC [Prince].Common.CLI_Insert @CLI_Image,'+CAST(@PRI_CMD_ID AS NVARCHAR(200))+ ',1,2,NULL
						 END '
						
					EXEC(@Query)	 
			             
						 -- ثبت موجودی اولیه

						 IF (ISNULL(@CMD_IsIngredient , 0) = 1 )AND(ISNULL(@CMD_CurrentAmount , 0) > 0)
						 BEGIN
							  SET @Query = ''
							  SET @Query = '
							  EXEC [Prince].['+@FNY_Name+'].STO_Insert;1 NULL,'+LTRIM(RTRIM(STR(@PRI_DefaultPlaceID)))+','+LTRIM(RTRIM(STR(@PRI_CNU_ID)))+
																		 ','+LTRIM(RTRIM(STR(@PRI_CMD_ID)))+','''+@Fny_Date+''',''00:00:01'''+
																		 ','+LTRIM(RTRIM(STR(@CMD_CurrentAmount)))+',3,NULL,NULL,'+LTRIM(RTRIM(STR(@CMD_InCost)))+
																		 ',1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL'
			                                                                                  
							  EXEC(@Query) 
			             
						 END
					  END
				 
				  END TRY
				  BEGIN CATCH
				      SET @DO = -9
				      --CLOSE CMD
			       --   DEALLOCATE CMD	
				      RETURN 
				  END CATCH				 
				 
			--   FETCH NEXT FROM CMD INTO  @CMD_ID,@CMD_Name,@CMD_AbbName,@CMD_LKP_Unit,@CMD_LKP_Group,@CMD_PTL_ID,@CMD_InCost,@CMD_Cost1,@CMD_Cost2,
			--							 @CMD_Cost3,@CMD_Cost4,@CMD_Cost5,@CMD_Description,@CMD_Order,@CMD_HotKeyCode,@CMD_IMG_ID,@CMD_IsFood,
			--							 @CMD_InitialCount,@CMD_LKP_RecGrpID,@CMD_IsDisabled,@CMD_LKP_Section,@CMD_PrintOrder,@CMD_Saloon_PTL_ID1,
			--							 @CMD_Saloon_PTL_ID2,@CMD_Out_PTL_ID1,@CMD_Out_PTL_ID2,@CMD_IsIngredient,@CMD_UseProductionForProfit,
			--							 @CMD_UserProductionForStore,@CMD_CountForProduct,@CMD_SalesCount,@CMD_CurrentAmount,@CMD_MinimumCount,
			--							 @CMD_InputCount,@CMD_Saloon_PTL_ID3,@CMD_Out_PTL_ID3	
			--END
			--CLOSE CMD
			--DEALLOCATE CMD	       

			IF (@Col_ID IS NULL) OR ((SELECT COUNT(1) FROM [saffron].[dbo].[Commodity]) = (SELECT COUNT(1) FROM [Prince].[Common].[Commodity]))
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCommodity', 1)
	    
		END        
    END
    --***************************************************
    IF (@Index IN (0,8)) -- تولید
    BEGIN 
    
		UPDATE [Saffron].[dbo].[Commodity]
		   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
	    
	    
		UPDATE [Prince].[Common].[Commodity]
		   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
	 
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferProduction')
		BEGIN 

		 --  *****************************************
		    
 			DECLARE SPR CURSOR for
				   SELECT [PRD_CMD_ID_Product],[PRD_CMD_ID_Ingredint],[PRD_Amount]
					 FROM [saffron].[dbo].[Production]
					WHERE [PRD_Amount] > 0 
			OPEN SPR
			FETCH NEXT FROM SPR INTO @PRD_CMD_ID_Product,@PRD_CMD_ID_Ingredint,@PRD_Amount
			WHILE @@FETCH_STATUS =0
			BEGIN     
		         
		         BEGIN TRY
		             
		             IF (@PRD_CMD_ID_Product <> @PRD_CMD_ID_Ingredint)
		             BEGIN
			               
					     -- اول 
					     SELECT @PRI_PRD_CMD_ID_Product = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 8) AND ([TRD_OldID] = @PRD_CMD_ID_Product)
				          
						 -- دوم   
						 SELECT @PRI_PRD_CMD_ID_Ingredint = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 8) AND ([TRD_OldID] = @PRD_CMD_ID_Ingredint)
				         
						 EXEC [Prince].Common.SPR_Insert @PRI_PRD_CMD_ID_Product,@PRI_PRD_CMD_ID_Ingredint,@PRD_Amount,NULL
				     END
				     
				  END TRY
				  BEGIN CATCH
				      SET @DO = -10
				      CLOSE SPR
			          DEALLOCATE SPR	
				      RETURN 
				  END CATCH				 
				 
			   FETCH NEXT FROM SPR INTO @PRD_CMD_ID_Product,@PRD_CMD_ID_Ingredint,@PRD_Amount
			END
			CLOSE SPR
			DEALLOCATE SPR	   
			    
		    IF (@Col_ID IS NULL)
			   INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferProduction', 1)
	    
		END        
    END
    --***************************************************
    IF (@Index IN (0,9)) -- مشتقات
    BEGIN 
    
		UPDATE [Saffron].[dbo].[Commodity]
		   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
	    
	    
		UPDATE [Prince].[Common].[Commodity]
		   SET [CMD_Name] = LTRIM(RTRIM(REPLACE(REPLACE([CMD_Name], N'ی', N'ي'), N'ک', N'ك')));
	 
	 
		IF NOT EXISTS( SELECT UPB_ID FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name = 'TransferCommodityLink')
		BEGIN

	 --  *****************************************
		    
 			DECLARE CML CURSOR for
				   SELECT [CML_CMD_BaseID],[CML_CMD_LinkID],[CML_Count],[CML_Order],[CML_SalesType]
					 FROM [saffron].[dbo].[CommodityLink]
			OPEN CML
			FETCH NEXT FROM CML INTO @CML_CMD_BaseID,@CML_CMD_LinkID,@CML_Count,@CML_Order,@CML_SalesType
			WHILE @@FETCH_STATUS =0
			BEGIN     
		         BEGIN TRY
		               
					 -- اول      
					 SELECT @PRI_CML_CMD_BaseID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 8) AND ([TRD_OldID] = @CML_CMD_BaseID)
				          
					 -- دوم   
					 SELECT @PRI_CML_CMD_LinkID = [TRD_NewID] FROM [Common].[TransferDataSaffronToPrince] WHERE ([TRD_TBL_ID] = 8) AND ([TRD_OldID] = @CML_CMD_LinkID)
			               
					 EXEC [Prince].Common.CML_Insert @PRI_CML_CMD_BaseID,@PRI_CML_CMD_LinkID,@CML_Count,@CML_Order,@CML_SalesType,NULL

				  END TRY
				  BEGIN CATCH
				      SET @DO = -11
				      CLOSE CML
			          DEALLOCATE CML	
				      RETURN 
				  END CATCH
				 
			   FETCH NEXT FROM CML INTO @CML_CMD_BaseID,@CML_CMD_LinkID,@CML_Count,@CML_Order,@CML_SalesType
			END
			CLOSE CML
			DEALLOCATE CML	       
		    
		    IF (@Col_ID IS NULL)
			  INSERT INTO [Prince].[Common].[UpdatesBugResolve]  VALUES ('TransferCommodityLink', 1)
	    
		END  
    END
    
    IF (@Index = 0)
    BEGIN 
		IF (SELECT COUNT(UPB_ID) FROM [Prince].[Common].[UpdatesBugResolve] WHERE UPB_Name IN ('TransferLookUp','TransferCashBox',
		'TransferPerson','TransferCostIncomeType','TransferPrintLine','TransferCommodity','TransferProduction','TransferCommodityLink')) = 8
		BEGIN
		   SET @DO = 1
		   UPDATE [prince].[Common].[SystemSetting] 
		      SET SST_Value = '1'
		    WHERE SST_Title = 'TransferDataSP' AND SST_UserID = 0
		END   
		ELSE
		   SET @DO = -4 -- انجام عملیات در یکی از قسمت ها با خطا مواجه شده است   
    END
    
   SET NOCOUNT OFF;
    
END
    
    