WARNING: This is not kids stuff. Use of this feature requires expertise in SQL Server and in the Cyrious database structure. It is very possible to irreversibly damage your Cyrious Control database if you make a mistake. Always develop your tests using a separate database on a separate machine. If you are interested in using SQL Bridge but are not a SQL guru, please contact a sales or consulting at Cyrious.
Note: This feature requires Control 5.75 or higher .
The stored procedure can be used to import a part into Control`
You must supply the following values:
You can start with a copy an existing part by specifying one of the following two values
If you don't supply a CopyFromPart, you must supply the following information:
0 = Material 4 = Other
1 = Labor 5 = Freight
2 = Equipment 6 = Location
3 = Outsource
* CategoryID - The ID of the Part Category. Use this value or the CategoryName, but not both.
The following information is optional. If you supply values, it will override the defaulr or the CopyFromPart.
, @ErrorNumber INT = 99
, @ErrorSeverity INT = 15
, @ErrorState INT = 0
, @ErrorLine INT = 0
, @ErrorProcedure VARCHAR(200) = @ProcName;
DECLARE @UnitTypeID TINYINT
, @UnitID TINYINT
, @GUsInUnit DECIMAL(18,4)
, @DisplayUnitTypeID TINYINT
, @DisplayUnitID TINYINT
, @TransferUnitTypeID TINYINT
, @TransferUnitID TINYINT
, @PartUnitConversionID INT
, @MultiplierType TINYINT; – 0 for Default, 1 for Multiplier Formula, 2 for Price Formula
DECLARE @InventoryID INT = NULL – New ID for the FIRST Warehouse detail Record (if inventory is enabled)
, @InventorySummaryID INT = NULL – New ID for the FIRST Summary Warehouse Record (if inventory is enabled)
, @WarehouseCount INT = NULL – The number of active Warehouses
, @WarehouseGroupCount INT = NULL – The number of active Warehouse Groups
– —————————————————-
– Lookup the correct keys
– —————————————————-
IF (@CopyFromPartID IS NULL AND @CopyFromPartName IS NOT NULL)
SET @CopyFromPartID = (SELECT ID FROM Part WHERE ItemName = @CopyFromPartName );
IF (@CopyFromPartID IS NOT NULL)
SELECT @CategoryID = COALESCE( @CategoryID , CategoryID )
, @PartTypeID = COALESCE( @PartTypeID , PartType )
, @Description = COALESCE( @Description , (CASE WHEN Description = ItemName THEN NULL ELSE Description END) )
, @SKU = COALESCE( @SKU , (CASE WHEN Description = SKU THEN NULL ELSE SKU END) )
, @DefaultStationID = COALESCE( @DefaultStationID , StationID )
, @UDFXML = COALESCE( @UDFXML , CONVERT(VARCHAR(MAX), UDFXML) )
, @UDFLayoutSettings = COALESCE( @UDFLayoutSettings , CONVERT(VARCHAR(MAX), UDFLayoutSettings) )
, @LocalTags = COALESCE( @LocalTags , LocalTags )
, @UnitText = COALESCE( @UnitText , (Select U.UnitText from [_Unit] U WHERE U.UnitID = Part.UnitID) )
, @DisplayUnitText = COALESCE( @DisplayUnitText , DisplayUnitText )
–, @DisplayUnitsInUnit = COALESCE( @DisplayUnitsInUnit , DisplayUnitsInUnit )
, @UnitCost = COALESCE( @UnitCost , UnitCost )
, @UnitCostFormula = COALESCE( @UnitCostFormula , UnitCostFormula )
, @MultiplierType = CostMultiplierType
, @CostMultiplierFormula = COALESCE( @CostMultiplierFormula , CostMultiplier )
, @PriceMultiplierFormula = COALESCE( @PriceMultiplierFormula, FixedCostMultiplier )
, @ScrapFactor = COALESCE( @ScrapFactor , ScrapFactor )
, @RoundingFactor = COALESCE( @RoundingFactor , RoundingFactor )
, @UseCategoryDefaults = COALESCE( @UseCategoryDefaults , UseCategoryDefaults )
, @TrackCosts = COALESCE( @TrackCosts , TrackCosts )
, @ExpenseAccountID = COALESCE( @ExpenseAccountID , ExpenseAccountID )
, @TrackInventory = COALESCE( @TrackInventory , TrackInventory )
, @AccrueCosts = COALESCE( @AccrueCosts , AccrueCosts )
, @AccrualAccountID = COALESCE( @AccrualAccountID , AssetAccountID )
, @ResellProductID = COALESCE( @ResellProductID , ResellProductID )
, @ResellVariableID = COALESCE( @ResellVariableID , ResellProductVariableID )
FROM Part
WHERE ID = @CopyFromPartID
– Else set default values if not passed in
ELSE
SELECT @Description = COALESCE( @Description , @PartName )
, @UDFXML = COALESCE( @UDFXML , )
, @UnitText = COALESCE( @UnitText , 'Each' )
, @ScrapFactor = COALESCE( @ScrapFactor , 0.0 )
, @RoundingFactor = COALESCE( @RoundingFactor , 1.00 )
, @UseCategoryDefaults = COALESCE( @UseCategoryDefaults , 1 )
, @TrackCosts = COALESCE( @TrackCosts , 1 )
, @TrackInventory = COALESCE( @TrackInventory , 0 )
, @AccrueCosts = COALESCE( @AccrueCosts , 0 )
;
IF (@CategoryID IS NULL)
SET @CategoryID = ( SELECT ID
FROM PricingElement
WHERE ElementName = @CategoryName
AND ClassTypeID = 12035 );
SELECT @UnitID = UnitID, @UnitTypeID = UnitType, @GUsInUnit = ConversionUnit
FROM [_Unit]
WHERE (UnitText = @UnitText OR UnitAbbreviation = @UnitText);
IF (COALESCE(@GUsInUnit, 0.0)=0.0) SET @GUsInUnit = 1.0;
IF (@DisplayUnitText = @UnitText) SET @DisplayUnitText = NULL;
IF (@DisplayUnitText IS NOT NULL)
SELECT @DisplayUnitID = UnitID, @DisplayUnitTypeID = UnitType,
@DisplayUnitsInUnit = (case when @DisplayUnitsInUnit IS NULL then ConversionUnit / @GUsInUnit ELSE @DisplayUnitsInUnit end)
FROM [_Unit]
WHERE (UnitText = @DisplayUnitText OR UnitAbbreviation = @DisplayUnitText);
IF (@UseCategoryDefaults = 1)
SELECT @TrackCosts = COALESCE(DefaultTrackCosts, @TrackCosts, 1)
, @ExpenseAccountID= COALESCE(ExpenseID, @ExpenseAccountID)
, @TrackInventory = COALESCE(DefaultTrackInventory, @TrackInventory, 0)
, @AccrueCosts = COALESCE(DefaultAccrueCosts, @AccrueCosts, 0)
, @AccrualAccountID= COALESCE(DefaultAssetAccountID, @AccrualAccountID)
, @ResellProductID = COALESCE(ResellProductID, @ResellProductID)
, @ResellVariableID= COALESCE(ResellProductVariableID, @ResellVariableID)
– Copy these if NULL
, @UDFLayoutSettings= COALESCE(@UDFLayoutSettings, UDFLayoutSettings)
, @LocalTags = COALESCE(@LocalTags, LocalTags)
FROM PricingElement
WHERE ID = @CategoryID AND ClassTypeID = 12035
ELSE IF (@UDFLayoutSettings IS NULL or @LocalTags IS NULL)
– Copy these if NULL
SELECT @UDFLayoutSettings= COALESCE(@UDFLayoutSettings, UDFLayoutSettings, )
, @LocalTags = COALESCE(@LocalTags, LocalTags)
FROM PricingElement
WHERE ID = @CategoryID AND ClassTypeID = 12035
;
IF (@MultiplierType IS NULL)
SET @MultiplierType = (CASE WHEN @CostMultiplierFormula IS NULL THEN 0
WHEN @PriceMultiplierFormula IS NOT NULL THEN 2
ELSE 1
END);
IF (@TrackInventory = 1)
BEGIN
SET @WarehouseCount = (SELECT COUNT(*) FROM Warehouse WHERE ID>0 AND IsActive = 1 AND IsGroup = 0);
SET @WarehouseGroupCount = (SELECT COUNT(*) FROM Warehouse WHERE ID>0 AND IsActive = 1 AND IsGroup = 1);
END;
– ———————————————————–
– Validate the Input
– ———————————————————–
IF (@CopyFromPartID IS NULL AND @CopyFromPartName IS NOT NULL)
SET @ErrorMessage = @ErrorMessage + '@CopyFromPartName “'+@CopyFromPartName+'” not found“.; ';
IF (@CopyFromPartID IS NOT NULL AND @CategoryID IS NULL)
SET @ErrorMessage = @ErrorMessage + '@CopyFromPartID '+CONVERT(INT, @CopyFromPartID)+' not found”.; ';
IF (@CategoryID IS NULL)
SET @ErrorMessage = @ErrorMessage + '@CategoryID not set or found“.; ';
IF (@PartTypeID IS NULL)
SET @ErrorMessage = @ErrorMessage + '@PartTypeID not specified.; ';
IF (@UnitID IS NULL)
SET @ErrorMessage = @ErrorMessage + '@UnitText not Found in _Unit table.; ';
IF (@DisplayUnitID IS NULL AND @DisplayUnitText IS NOT NULL)
SET @ErrorMessage = @ErrorMessage + '@DisplayUnitText not Found in _Unit table.; ';
IF (@PartTypeID != 0 AND @TrackInventory = 1)
SET @ErrorMessage = @ErrorMessage + 'You can only Track Inventory (@TrackInventory=1) on Material Parts (@PartType=0); ';
IF (@ErrorMessage != )
BEGIN
– Build the message string that will contain original error information.
SET @ErrorMessage = 'Error %d, Level %d, State %d, Procedure %s, Line %d, Message: '+ @ErrorMessage;
RAISERROR
(
@ErrorMessage,
@ErrorSeverity,
1,
@ErrorNumber, – parameter: original error number.
@ErrorSeverity, – parameter: original error severity.
@ErrorState, – parameter: original error state.
@ErrorProcedure, – parameter: original error procedure name.
@ErrorLine – parameter: original error line number.
);
RETURN;
END;
– —————————————————-
– FOR NOW, WE AREN'T HANDLING INVENTORY * SET @TrackInventory = 0;
IF (@PartID IS NULL ) SET @PartID = (SELECT dbo.csf_chapi_nextid( 12014, 1));
IF (@DisplayUnitID IS NOT NULL AND @DisplayUnitID != @UnitID)
SET @PartUnitConversionID = (SELECT dbo.csf_chapi_nextid( 12016, 1 ));
IF (@TrackInventory =1)
BEGIN
SET @InventoryID = (SELECT dbo.csf_chapi_nextid( 12200, @WarehouseCount ));
SET @InventorySummaryID = (SELECT dbo.csf_chapi_nextid( 12200, @WarehouseGroupCount ));
END;
BEGIN TRANSACTION
BEGIN TRY
INSERT INTO Part
(ID, StoreID, ClassTypeID, ModifiedByUser, ModifiedByComputer, ModifiedDate, SeqID, IsSystem, IsActive
, CategoryID, CategoryStoreID, CategoryClassTypeID, ItemName, Description, HelpText, InternalNotes
, BarCode, SKU, AssetAccountID, ExpenseAccountID, InventoryUnitType, PartType, TrackInventory, QuantityOnHand
, QuantityReserved, QuantityAvailable, QuantityOnOrder, QuantityExpected, ReOrderPoint, ReOrderQuantity
, YellowNotificationPoint, RedNotificationPoint, NotificationAddresses, NotifyOtherContacts, UnitID
, RoundingOption, ScrapFactor, UnitCost, TrackCosts, UnitCostFormula, UseFixedCost, CostMultiplier
, CostMultiplierType, FixedCostMultiplier, ShowOnProductionSchedule, InstancesAllowedType, InstancesAllowed
, Vendor, VendorPartNumber, RoundingFactor, StationID, StationClassTypeID, GLDepartmentID, GLDepartmentClassTypeID
, InventoryID, AccrueCosts, ExpenseAccountClassTypeID, AssetAccountClassTypeID, UseCategoryDefaults
, ComputedCostAccountID, ComputedCostAccountClassTypeID, LastModifiedDate, UDFLayoutSettings, LocalTags
, UDFXML, UseInvUnitsForDisplay, DisplayUnitID, DisplayUnitType, DisplayUnitText, DisplayConversionFormula
, GLDepartmentIDOV, ExpenseAccountIDOV, AssetAccountIDOV, TransferUnitID, TransferUnitQuantity, TransferUnitText
, ResellProductID, ResellProductIDOV, ResellProductVariableID)
VALUES (
@PartID --
, -1 -- ,
, 12014 -- ,
, @ProcName -- ,
, @ComputerName -- ,
, @DT -- ,
, 0 -- ,
, 0 -- ,
, 1 -- ,
, @CategoryID --
, NULL --
, 12035 --
, @PartName --
, coalesce(@Description, @PartName) --
, NULL --
, NULL --
, NULL --
, @SKU --
, @AccrualAccountID --
, @ExpenseAccountID --
, @UnitTypeID --
, @PartTypeID --
, @TrackInventory --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, NULL --
, @UnitID --
, (CASE @RoundingFactor
WHEN 1.0 THEN 0
WHEN 0.5 THEN 1
WHEN 0.25 THEN 2
WHEN 0.20 THEN 3
WHEN 0.10 THEN 4
WHEN 0.01 THEN 5
WHEN 0.001 THEN 6
WHEN 0.0001 THEN 7
WHEN 0.125 THEN 8
WHEN 5.0 THEN 9
WHEN 10.0 THEN 10
WHEN 25.0 THEN 11
WHEN 50.0 THEN 12
WHEN 100.0 THEN 13
WHEN 250.0 THEN 14
WHEN 500.0 THEN 15
WHEN 1000.0 THEN 16
ELSE 17 -- Custom
END ) --
, @ScrapFactor --
, @UnitCost --
, @TrackCosts --
, @UnitCostFormula --
, ISNUMERIC(@UnitCost) --
, @CostMultiplierFormula --
, @MultiplierType --
, @PriceMultiplierFormula --
, 0 --
, 0 --
, 1 --
, NULL --
, NULL --
, @RoundingFactor --
, @DefaultStationID --
, 26100 --
, NULL --
, NULL --
, NULL --
, @AccrueCosts --
, 8001 --
, 8001 --
, @UseCategoryDefaults --
, NULL --
, NULL --
, @DT --
, @UDFLayoutSettings --
, @LocalTags --
, @UDFXML --
, (CASE WHEN @DisplayUnitID IS NULL THEN 1 ELSE 0 END) --
, COALESCE(@DisplayUnitID, @UnitID) --
, COALESCE(@DisplayUnitTypeID, @UnitTypeID) --
, COALESCE(@DisplayUnitText, @UnitText) --
, '1 / '+convert(varchar(18), @DisplayUnitsInUnit) --
, NULL --
, NULL --
, NULL --
, @UnitID --
, 1.00 --
, @UnitText --
, @ResellProductID --
, (CASE WHEN @ResellProductID IS NULL THEN 0 ELSE 1 END) --
, @ResellVariableID --
);
IF (@PartUnitConversionID IS NOT NULL)
INSERT INTO PartInventoryConversion
(ID, StoreID, ClassTypeID, ModifiedByUser, ModifiedByComputer, ModifiedDate, SeqID, IsSystem, IsActive
, PartID, IsAutomatic, IsDefault, ConversionFormula, ConsumptionFormula, InclusionFormula, UnitID
, UnitType)
VALUES(
@PartUnitConversionID --
, -1 -- ,
, 12016 -- ,
, @ProcName -- ,
, @ComputerName -- ,
, @DT -- ,
, 0 -- ,
, 0 -- ,
, 1 -- ,
, @PartID --
, 0 --
, 0 --
, '1 / '+convert(varchar(18), @DisplayUnitsInUnit) --
, '1' --
, 'Always' --
, @DisplayUnitID --
, @DisplayUnitTypeID --
);
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
SELECT
@ErrorNumber = ERROR_NUMBER(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE(),
@ErrorLine = ERROR_LINE(),
@ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');
SELECT @ErrorMessage =
N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' +
'Message: '+ ERROR_MESSAGE();
RAISERROR
(
@ErrorMessage,
@ErrorSeverity,
1,
@ErrorNumber, -- parameter: original error number.
@ErrorSeverity, -- parameter: original error severity.
@ErrorState, -- parameter: original error state.
@ErrorProcedure, -- parameter: original error procedure name.
@ErrorLine -- parameter: original error line number.
);
END CATCH;
IF (@RefreshOnSave=1)
BEGIN
EXEC dbo.csf_chapi_refresh @PartID, 12014, -1;
EXEC dbo.csf_chapi_refresh @CategoryID, 12035, -1;
END;
SELECT @PartID, 12035
END;
Contributor: Cyrious Software
Date: 9/2016
Version: Control 5.7+