**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.

Overview

The stored procedure can be used to add a line item to an existing order or estimate in Control.

You must identify the order or estimate with one (and only one) of the following:

  • @OrderNumber - The order number
  • @EstimateNumber - The estimate number
  • @THID - The TransHeader.ID database key field for the record.

For the line item, you must supply one (and only one) of the following:

  • @ProductName - The exact name of the product (not case sensitive).
  • @ProductID - The Product.ID database key field for the product.

You must also supply all of the following required information:

  • @Quantity - The number of items for this line item
  • @RecomputeOnSave - Whether you want the order recomputed when you save this line item. Set this to 1 to recompute, or 0 to not recompute. If you are going to make other changes to the order, it is recommended you only do one recompute at the end.

The following optional values may also be supplied. If you leave a value out, it generally will default to the value Control would set if you did not change it.

  • @RefreshOnSave - Set this 1 to send a refresh message to Control to re-load the order. Set it to 0 to not send this. If you are recomputing the order, this is done automatically and may be ignored. If you are going to make other changes to the order, it is recommended you only do one refresh at the end.
  • @AddJournal - Set this to 1 to create a journal entry on the order showing the line item was added.
  • @EmployeeID - The Employee.ID of the user making the change. This is used in the Journal.
  • @BasePrice - The price of the line item before discounts and taxes.
  • @BasePriceOV - Set this to 1 if the price is overridden and should not be recalculated. Set this to 0 if the price should be recalculated when the order is recomputed. Defaults to 1 (overridden).
  • @Discount - The discount amount for this line.
  • @DiscountRate - The percentage discount rate for this line, entered as a decimal (e.g., enter 0.10 for 10%). Do not enter anything here unless you want the discount to recalculate as the price changes.
  • @Tax - The tax amount for this line (in dollars, not percent). The tax amount is always recalculated on recompute.
  • @Description - The description of the line item purchase.
  • @ProductionNotes - The internal or production notes for this line item.
  • @StationID - The production Station.ID this line item should start in. Defaults to the products default Station if left blank.
  • @IsComplete - Set this to 1 if the line item is complete in production, otherwise set this to zero. Defaults to 0.
  • @AssignedToID - The Employee.ID of the person currently assigned to produce the order.
  • @GLAccountID - The income Account for this line. If left blank, this defaults to the products default income account.
  • @GLDepartmentID - The accounting GL Department for the income and expense for this line item.
  • @ImageHeight - The Image Height (in input units) for the line item.
  • @ImageWidth - The Image Width (in input units) for the line item.
  • @ImportDictionary - (future) An representation of other variable to be saved for the product.

The Stored Procedure returns the ID and ClassTypeID of the new line item.

Notes:

  • In general, don't set values you want to use the default to. For instance, if you set the @TaxClassID to the value the customer is set to, this will force it to be overridden and it won't change even if the customer's tax class is updated. Just leave these columns out of the call if you want to use the default value.

The stored procedure requires sql_bridge in order to obtain IDs for the inserted records. sql_bridge is a collection of SQL Server stored prodedures and functions and external modules that can be called to safely insert or update data into the Control database and notify the CHAPI service that data has been updated. The CHAPI service is a controller program that will then notify the SSLIP and all copies of Control about the changes.

Example Usage

This inserts two line items in order #1181. The order is recalculated after the 2nd line item.

-- =============================================
-- Author:        Cyrious Sofware
-- Create date: May-2016
-- Description:    This stored procedure imports a line item into an existing order or estimate.
--                Many of the parameters are option, but if not supplied will used
--                The default behavior.
--
-- Returns:     New TransDetailID, TransDetailClassTypeID
-- =============================================
ALTER PROCEDURE csp_ImportLineItem
    -- You must supply one (and only one) of the following
    @THID                INT                = NULL,
    @OrderNumber        INT             = NULL,
    @EstimateNumber        INT                = NULL,
    -- You must supply one (and only one) of the following
    @ProductName            VARCHAR(255)    = NULL,     -- Product Name.  Must match exactly.
    @ProductID              INT             = NULL,     -- Product.ID   You must supply either the ProductName of the ProductID.
    -- You must supply all of the following
    @Quantity           DECIMAL(18,4),      -- the quantity of items for ths line
    @RecomputeOnSave    BIT,                -- set to 1 to have the order to recompute and update on Save.  0 to do it manually afterwards.
    -- Optional values
    @RefreshOnSave      BIT                 = 1,        -- 0 = Don't Refresh the order on Save, 1 = Do Refresh
    @AddJournal            BIT                    = 1,        -- 0 = Don't save a journal recording the new line, 1 = Do Save it
    @EmployeeID            INT                    = NULL,        -- The Employee ID making the save.  Used in the Journal if provided.
    @BasePrice          DECIMAL(18,4)    = NULL,     -- the extended base price of the line (pre-discount and taxes)
    @BasePriceOV        BIT             = 1,        -- 1 if the price should not be recalculated on edit, 0 if the price can be racalculated on edit.
    @Discount           DECIMAL(18,4)    = NULL,     -- the discount for this line.  Always put the dollar value of the discount here.
    @DiscountRate       DECIMAL(18,4)    = NULL,     -- the discount rate for this line.  Only use this if the discount should be adjusted as a percentage.
    @Tax                DECIMAL(18,4)    = NULL,     -- the amount of taxes for this line item, if applicable.
    @Description        VARCHAR(4096)   = NULL,     -- any line item notes for this product
    @ProductionNotes    VARCHAR(4096)   = NULL,     -- any production notes for this product.
    @StationID          INT             = NULL,     -- the line item station
    @IsComplete         BIT             = 0,        -- 1 = Item is Complete, 0 = not complete.  Only used for WIP orders.
    @AssignedToID       INT             = NULL,     -- Enter the employee this is assigned to. Leave blank for no assignment.
    @GLAccountID        INT             = NULL,     -- the Income Account ID for this product.  Leave blank to use the default.
    @GLDepartmentID     INT             = NULL,     -- the GL Department for this order, if desired.
    -- set variables informtion here.
    -- This data should be in INI format (variablename=value) with paragraph delimiters
    --      All strings must be delimited with single quotes
    --      Multiple line strings are not supported
    @ImageHeight        DECIMAL(18,4)   = NULL,        -- the height of the product.  Leave blank to use the default
    @ImageWidth         DECIMAL(18,4)   = NULL,        -- the width of the proudct.  Leave blank to use the default
    @ImportDictionary   VARCHAR(MAX)    = NULL,         -- addtional variables can be set using the import dictionary and product variable mapping
     -- Some OUTPUT Parameters in case the caller wants any of these value back
    @TDID                   INT             = NULL  OUTPUT
AS
BEGIN
    --
    -- Step 0. Validate the Input
    --
    DECLARE @Logs                               TABLE( ID INT, ClassTypeID INT, ParentID INT, IsError BIT,
                                                       Summary VARCHAR(255),
                                                       Detail VARCHAR(2000)
                                                       );
    DECLARE @ValidationError VARCHAR(2048) = '';
    IF (IsNumeric(@THID) +IsNumeric(@OrderNumber) + IsNumeric(@EstimateNumber)  1)
        SET @ValidationError = @ValidationError + 'You must supply one and only one of these inputs: @OrderNumber, @EstimateNumber, and @THID; ';
    IF ( ( (CASE WHEN @ProductID IS NULL THEN 0 ELSE 1 END) + (CASE WHEN @ProductName IS NULL THEN 0 ELSE 1 END) )  1)
        SET @ValidationError = @ValidationError + 'You must supply one and only one of these inputs: @ProductName and @ProductID; ';
    IF (@THID IS NULL)
    BEGIN
        IF (@OrderNumber IS NOT NULL)
        BEGIN
            SET @THID = (SELECT ID FROM TransHeader WHERE  (OrderNumber = @OrderNumber AND TransactionType IN (1,6)) );
            IF (@THID IS NULL)
                SET @ValidationError = @ValidationError + 'Order Number '+CONVERT(VARCHAR(12), @OrderNumber)+' does not exist; ';
        END;
        IF (@EstimateNumber IS NOT NULL)
        BEGIN
            SET @THID = (SELECT ID FROM TransHeader WHERE  (EstimateNumber = @EstimateNumber AND TransactionType = 2) );
            IF (@THID IS NULL)
                SET @ValidationError = @ValidationError + 'Estimate Number '+CONVERT(VARCHAR(12), @EstimateNumber)+' does not exist; ';
        END;
    END
    ELSE
    BEGIN
        IF NOT EXISTS( SELECT 1 FROM TransHeader WHERE ID = @THID AND TransactionType IN (1,2,6) )
            SET @ValidationError = @ValidationError + 'TransHeader.ID = '+CONVERT(VARCHAR(12), @THID)+' does not exist or is not an order or estimate; ';
    END;
    IF (@ValidationError  '')
    BEGIN
        INSERT INTO @Logs (ID, ClassTypeID, ParentID, IsError, Summary, Detail)
        VALUES(
            NULL, 10100, @THID, 1,
            'Line Item Not Imported due to Validation Errors.',
            'Validation Errors: '+@ValidationError
        )
        SELECT * FROM @Logs;
        RETURN;
    END;
    --
    -- Step 1. Declare some variable and look up some information
    --
    -- Define some fixed values for reference
    DECLARE @DT                     SMALLDATETIME   = GetDate();
    DECLARE @ComputerName           VARCHAR(25)     = @@ServerName;
    DECLARE @NewLine                CHAR(2)         =  CHAR(10)+CHAR(13);
    -- Determine the Order or Estimate
    -- Order Related Variables
    DECLARE @TransNumber            INT             ;
    DECLARE @TransactionType        TINYINT         ;
    DECLARE    @OrderDueDate            SMALLDATETIME    ;
    DECLARE @OrderProofDate         SMALLDATETIME   ;
    DECLARE @DivisionID                INT                ;
    DECLARE @AccountID                INT                ;
    DECLARE @ContactID                INT                ;
    SELECT  @THID            = ID,
            @EstimateNumber = CASE WHEN TransactionType = 2 THEN EstimateNumber ELSE NULL END,
            @OrderNumber    = CASE WHEN TransactionType IN (1,6) THEN OrderNumber ELSE NULL END,
            @TransNumber    = CASE WHEN TransactionType = 2 THEN EstimateNumber ELSE OrderNumber END,
            @TransactionType= TransactionType,
            @OrderDueDate    = DueDate,
            @OrderProofDate = ProofDate,
            @DivisionID        = DivisionID,
            @AccountID        = AccountID,
            @ContactID        = ContactID
    FROM   TransHeader
    WHERE  ID = @THID;
    -- Variation Information
    DECLARE @TVID                  INT          = (SELECT Top(1) ID FROM TransVariation WHERE ParentID = @THID ORDER BY SortIndex ASC);
    -- Journal Related Information
    DECLARE @JournalID             INT     = (SELECT dbo.csf_chapi_nextid( 20500, 1)); -- Order Activity
    IF (@EmployeeID IS NULL)       SET @EmployeeID = 10;    -- House Account
    -- Company Information
    DECLARE @PricingPlanID          INT             = COALESCE((SELECT PricingPlanTypeID FROM Account WHERE ID = @AccountID), 10);
    -- Line Item Information
    SET     @TDID                                 = (SELECT dbo.csf_chapi_nextid( 10100, 1));
    DECLARE @GLAccountOV           BIT            = (CASE WHEN @GLAccountID IS NULL THEN 0 ELSE 1 END);
    DECLARE @DiscountRateOV           BIT              = (CASE WHEN @DiscountRate IS NULL THEN 0 ELSE 1 END);
    DECLARE @BaseProductID         INT;
    DECLARE @IsDerivedProduct      BIT;
    SELECT  Top(1)
            @ProductID      = COALESCE( @ProductID, ID),
            @ProductName    = COALESCE( @ProductName, ItemName ),
            @GLAccountID    = COALESCE( @GLAccountID, ( SELECT top(1) PP.AccountCodeID
                                                        FROM PricingPlan PP
                                                        WHERE PP.GoodsItemID = P.ID
                                                            AND PP.PricingPlanTypeID IN (10, @PricingPlanID)
                                                        ORDER BY PricingPlanTypeID DESC
                                                        ) ),
            @StationID      = COALESCE( @StationID, StationID ),
            @BaseProductID    = BaseProductID,
            @IsDerivedProduct    = (CASE WHEN BaseProductID IS NULL THEN 0 ELSE 1 END)
    FROM CustomerGoodsItem P
    WHERE (ItemName = @ProductName OR ID = @ProductID);
    SET @Discount           = COALESCE( @Discount, @BasePrice * @DiscountRate );
    DECLARE @SubTotalPrice      DECIMAL(18,4)   = (@BasePrice - COALESCE(@Discount, 0.0));
    DECLARE @TotalPrice         DECIMAL(18,4)   = (@SubTotalPrice + COALESCE(@Tax, 0.0));
    DECLARE @LineItemIndex      INT     = (SELECT COUNT(*) FROM TransDetail WHERE ParentID = @THID);
    DECLARE @LineItemNumber     CHAR(3) = CONVERT(CHAR(3), @LineItemIndex+1);
    -- Create the Variable XML Record
    DECLARE @VariableXML            XML;
    -- encode variables for XML (or saving as XML will fail)
    SET @Description     = (SELECT @Description     FOR XML PATH(''));
    SET @ProductionNotes = (SELECT @ProductionNotes FOR XML PATH(''));
    SET @VariableXML    =
        ''
            + ''
                    + 'Quantity'
                    + ''+CONVERT(VARCHAR(16), @Quantity)+''
                    + '1'
            + ''
            + ''
                    + 'Description'
                    + ''
                    + @Description
                    +''
                    + '1'
            + ''
            + CASE WHEN @ImageHeight IS NOT NULL THEN
                ''
                    + 'ImageHeight'
                    + ''+CONVERT(VARCHAR(19),@ImageHeight)+''
                    + '1'
                + ''
            ELSE '' END
            + CASE WHEN @ImageWidth IS NOT NULL THEN
                ''
                    + 'ImageWidth'
                    + ''+CONVERT(VARCHAR(19),@ImageWidth)+''
                    + '1'
                + ''
            ELSE '' END
            + CASE WHEN @ProductionNotes IS NOT NULL THEN
                ''
                    + 'InternalNotes'
                    + ''
                    + @ProductionNotes
                    + ''
                    + '1'
                + ''
            ELSE '' END
        + '';
    --
    -- Step 2. Create the Records
    --
    BEGIN TRANSACTION
    BEGIN TRY
        -- Update TransHeader Sequence ID
        UPDATE TransHeader
        SET SeqID = SeqID + 1,
            ModifiedDate = @DT,
            ModifiedByUser = 'SQLBridge'
        WHERE ID = @THID
        ;
        -- Insert TransDetail
        INSERT INTO [dbo].[TransDetail]
            ([ID] ,[StoreID] ,[ClassTypeID] ,[ModifiedByUser] ,[ModifiedByComputer] ,[ModifiedDate] ,[SeqID] ,[IsSystem] ,[IsActive]
            , [TransactionType] , [ParentID], [ParentClassTypeID] , [TransHeaderID] , [TransHeaderClassTypeID]
            , [TransHeaderTransNumber] , [GoodsItemID] , [GoodsItemClassTypeID] , [GoodsItemCode] , [PricingLevel]
            , [PricingLevelOverridden] , [DiscountLevel] , [DiscountLevelOverridden] , [Description] , [Quantity] , [LineItemIndex]
            , [LineItemNumber] , [HTMLShortFormat] , [HTMLLongFormat] , [BasePrice] , [BaseCalcPrice] , [BaseOverriddenPrice] , [BaseIsOverridden]
            , [DiscountPrice] , [DiscountCalcPrice] , [DiscountOverriddenPrice] , [DiscountIsOverridden] , [ModifierPrice] , [ModifierCalcPrice]
            , [ModifierOverriddenPrice] , [ModifierIsOverridden] , [RawSubTotalPrice] , [RawSubTotalCalcPrice] , [RawSubTotalOverriddenPrice]
            , [RawSubTotalIsOverridden] , [RoundingPrice] , [RoundingCalcPrice] , [RoundingOverriddenPrice] , [RoundingIsOverridden]
            , [SubTotalPrice] , [SubTotalCalcPrice] , [SubTotalOverriddenPrice] , [SubTotalIsOverridden] , [TaxesPrice] , [TaxesCalcPrice]
            , [TaxesOverriddenPrice] , [TaxesIsOverridden] , [TotalPrice] , [TotalCalcPrice] , [TotalOverriddenPrice] , [TotalIsOverridden]
            , [MeAndSonsBasePrice] , [MeAndSonsDiscountPrice] , [MeAndSonsModifierPrice] , [MeAndSonsRawSubTotalPrice] , [MeAndSonsRoundingPrice]
            , [MeAndSonsSubTotalPrice] , [MeAndSonsTaxesPrice] , [MeAndSonsTotalPrice] , [InternalNotes] , [UnitParamName] , [UnitParamValue]
            , [VariationID] , [ImageParamID] , [ImageParamStoreID] , [ImageParamClassTypeID] , [StageID] , [StageStoreID] , [StageClassTypeID]
            , [SaleAccountID] , [SaleAccountStoreID] , [SaleAccountClassTypeID] , [SaleAccountOverridden] , [ChildItemCount] , [ParameterStr]
            , [ModifierStr] , [PartStr] , [VarGridParam1ID] , [VarGridParam2ID] , [VarGridValues1] , [VarGridValues2] , [VarGridParam1OV]
            , [VarGridParam2OV] , [TaxItems] , [IsComplete] , [AssignedToID] , [TicketItemNumber] , [ShippingCompanyID] , [ShippingCompanyOV]
            , [ShippingContactID] , [ShippingContactOV] , [ShippingAddressID] , [ShippingAddressLinkID] , [ShippingAddressOV] , [ShippingTaxClassID]
            , [ShippingTaxClassOV] , [TotMaterialCost] , [TotLaborCost] , [TotEquipmentCost] , [TotOutSourceCost] , [TotOtherCost] , [TotPartCost]
            , [HasWorkAssignment] , [WorkAssignmentID] , [WorkAssignmentStoreID] , [WorkAssignmentClassTypeID] , [TotFreightCost] , [PriorityID]
            , [PriorityStoreID] , [PriorityClassTypeID] , [AssignedToClassTypeID] , [ProofApproved] , [ProofApprovedDate] , [ProofApprovedByID]
            , [PercentComplete] , [PercentCompleteOV] , [TermID] , [ContractPeriodID] , [ContractStartDate] , [ContractEndDate] , [ShipFromAddressID]
            , [ShipFromAddressLinkID] , [ShipFromAddressOV] , [IsValidTax] , [IsDirty] , [TaxNumber] , [ShipFromID] , [ShipFromClassTypeID]
            , [AssemblyIncluded] , [AssemblyIncludedOV] , [AssemblyLocked] , [AssemblyLinkID] , [AssemblyLinkClassTypeID] , [OverriddenAssemblies]
            , [StationID] , [StationClassTypeID] , [GLDepartmentID] , [GLDepartmentIDOV] , [TaxablePrice] , [MeAndSonsTaxablePrice]
            , [StationActivityID] , [StationActivityClassTypeID] , [MaterialExpenseID] , [LaborExpenseID] , [EquipmentExpenseID]
            , [OutsourceExpenseID] , [OtherExpenseID] , [FreightExpenseID] , [LocationExpenseID] , [UseParentExpenseAccounts] , [WarehouseID]
            , [WarehouseIDOV] , [RanReturnSQLFuncStatements] , [QuantityShipped] , [DueDate] , [DueDateOV] , [ShipFromCustomerID]
            , [ShipFromCustomerClassTypeID] , [ShowMultipleQuantityGrid] , [MultipleQuantityGridXML] , [OldDescription] , [EstimatingWarehouseID]
            , [EstimatingWarehouseIDOV] , [ProductionDivisionID] , [ProductionDivisionIDOverridden]
            , [ProofDate], [ProofDateOV], [CalcTaxablePrice], [TaxInfo], [UsesExternalCFL], [IsPricingLocked], [BaseProductID], [ImportDictionary]
            , [ApplyDPChanges]
            )
        VALUES
        ( @TDID  -- 
        , -1     -- ,
        , 10100     -- ,
        , 'SQLBridge'     -- ,
        , @@ServerName     -- ,
        , @DT    -- ,
        , 0    -- ,
        , 0    -- ,
        , 1    -- ,
        , @TransactionType         -- 
        , @THID         -- 
        , 10000         -- 
        , @THID         -- 
        , 10000         -- 
        , @OrderNumber         -- 
        , @ProductID         -- 
        , (CASE WHEN @IsDerivedProduct = 1 THEN 12005 ELSE 12000 END)         -- 
        , @ProductName         -- 
        , 1.0         -- 
        , 0         -- 
        , @DiscountRate         -- 
        , @DiscountRateOV         -- 
        , @Description         -- 
        , @Quantity         -- 
        , @LineItemIndex         -- 
        , @LineItemNumber         -- 
        , NULL         -- 
        , NULL         -- 
        , @BasePrice         -- 
        , @BasePrice         -- 
        , @BasePrice         -- 
        , @BasePriceOV         -- 
        , @Discount         -- 
        , @Discount         -- 
        , @Discount         -- 
        , (CASE WHEN @Discount IS NULL THEN 0 ELSE 1 END )         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0         -- 
        , @SubTotalPrice         -- 
        , @SubTotalPrice         -- 
        , @SubTotalPrice         -- 
        , 0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0         -- 
        , @SubTotalPrice         -- 
        , @SubTotalPrice         -- 
        , @SubTotalPrice         -- 
        , 0         -- 
        , @Tax         -- 
        , @Tax         -- 
        , @Tax         -- 
        , 0         -- 
        , @TotalPrice         -- 
        , @TotalPrice         -- 
        , @TotalPrice         -- 
        , 0         -- 
        , @BasePrice         -- 
        , @Discount         -- 
        , 0.0         -- 
        , @SubTotalPrice         -- 
        , 0.0         -- 
        , @SubTotalPrice         -- 
        , @Tax         -- 
        , @TotalPrice         -- 
        , @ProductionNotes         -- 
        , 'Quantity'         -- 
        , @Quantity         -- 
        , @TVID         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , @GLAccountID -- 
        , NULL         -- 
        , 8001         -- 
        , @GLAccountOV         -- 
        , 0         -- 
        , CONVERT(VARCHAR(MAX),@VariableXML)         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , @AssignedToID         -- 
        , (COALESCE(@OrderNumber, @EstimateNumber)*1000 + @LineItemIndex + 1)         -- 
        , @AccountID         -- 
        , 0         -- 
        , @ContactID         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , NULL         -- 
        , 0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0.0         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0.0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 3500         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , 0.0         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , 1         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , 0         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , @StationID         -- 
        , 26100         -- 
        , @GLDepartmentID         -- 
        , 0         -- 
        , @SubTotalPrice         -- 
        , @SubTotalPrice         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , NULL         -- 
        , 0         -- 
        , NULL         -- 
        , 0.0         -- 
        , @OrderDueDate         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , NULL         -- 
        , NULL         -- 
        , NULL         -- 
        , 0         -- 
        , @DivisionID         -- 
        , 0       -- 
        , @OrderProofDate        -- 
        , 0       -- 
        , @SubTotalPrice        -- 
        , NULL        -- 
        , 0        -- 
        , 0   -- 
        , @BaseProductID        -- 
        , @ImportDictionary        -- 
        , (CASE WHEN (@ISDerivedProduct = 1) OR (@ImportDictionary IS NOT NULL) THEN 1 ELSE 0 END)    -- )
        );
        -- Insert Order Creation into Journal
        IF (@AddJournal = 1)
        BEGIN
            INSERT INTO [Journal] ([ID], [StoreID], [ClassTypeID] ,[ModifiedByUser] ,[ModifiedByComputer] ,[ModifiedDate] ,[SeqID] ,[IsSystem] ,[IsActive] ,[EmployeeID],[JournalActivityType] ,[JournalActivityText] ,[Description] ,[Notes] ,[StartDateTime] ,[EndDateTime] ,[TotalTime] ,[ScheduledDateTime]  ,[CompletedByID] ,[CompletedDateTime] ,[IsSummary] ,[IsDetail] ,[SummaryID] ,[SummaryClassTypeID] ,[SummaryAmount] ,[DetailAmount] ,[StartGLGroupID]  ,[EndGLGroupID]  ,[AccountID] ,[AccountClassTypeID]  ,[ContactID]  ,[ContactClassTypeID]  ,[TransactionID] ,[TransactionClassTypeID]  ,[IsVoided]  ,[VoidedDateTime]  ,[VoidedEntryID] ,[VoidedEntryClassTypeID]  ,[VoidedReason]  ,[QueryStartDateTime] ,[QueryEndDateTime]  ,[ReminderDateTime]  ,[ReminderPrompt]  ,[PartID]  ,[ActivityType]  ,[ActivityTypeText]  ,[IsBillable] ,[BillableDateTime]  ,[UseActualTime]  ,[BillingNotes]  ,[BillingType]  ,[TotalBilledTime]  ,[RecurringActivityID]  ,[LinkID]  ,[LinkStoreID] ,[LinkClassTypeID] ,[SpecialCode] ,[DivisionID] ,[HasCalendarLinks] ,[TipRecipientID] ,[PartClassTypeID] ,[RecurringClassTypeID] ,[StationID] ,[StationClassTypeID] ,[CurrentState] ,[StageID] ,[StageClassTypeID])
            VALUES
                ( @JournalID     -- (
                , -1     -- ,
                , 20500     -- ,
                , 'SQLBridge'     -- ,
                , @@ServerName     -- ,
                , @DT    -- ,
                ,  0    -- ,
                ,  0    -- ,
                ,  1    -- ,
                ,  @EmployeeID  -- 
                , 3  -- 
                , 'Order'  -- 
                , 'Line Item Added'  -- 
                , 'Inserted by SQLBridge on '+CONVERT(VARCHAR(20), @DT)  -- 
                , @DT  -- 
                , @DT  -- 
                , NULL  -- 
                , NULL  -- 
                , @EmployeeID  -- 
                , @DT  -- 
                , 1  -- 
                , 1  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , @AccountID  -- 
                , 2000  -- 
                , @ContactID  -- 
                , 3000  -- 
                , @THID -- 
                , 10000 -- 
                , 0  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , @DT  -- 
                , @DT  -- 
                , NULL  -- 
                , 0  -- 
                , NULL  -- 
                , 0  -- 
                , NULL  -- 
                , 0  -- 
                , NULL  -- 
                , 0  -- 
                , NULL  -- 
                , 0  -- 
                , 0  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , @DivisionID  -- 
                , 0  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- 
                , NULL  -- )
            );
        END;  -- IF (@AddJournal=1)
        -- Now commit the Transaction
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION;
        INSERT INTO @Logs (ID, ClassTypeID, ParentID, IsError, Summary, Detail)
        VALUES(
            NULL, 10100, @THID, 1,
            'Line Item '+@ProductName+' Import for Order #'+CONVERT(VARCHAR(12),@OrderNumber)+' FAILED due to Unhandled Exception.',
            'Exception: '+ERROR_MESSAGE()
        );
        RETURN;
    END CATCH;
    --
    -- Step 3. Recalculate the Order if desired; refresh if not.
    --
    IF (@RecomputeOnSave = 1)
        EXEC dbo.csf_chapi_recompute @THID, 10000, 'Recompute after Import';
    IF (@RefreshOnSave = 1)
        EXEC dbo.csf_chapi_refresh @THID, 10000, -1;
    --
    -- Step 4. Return the New Detail ID
    --
    INSERT INTO @Logs (ID, ClassTypeID, ParentID, IsError, Summary, Detail)
    VALUES(
        @TDID, 10100, @THID, 0,
        'Line Item '+@ProductName+' imported to Order #'+CONVERT(VARCHAR(12),@OrderNumber),
        ''
    );
    SELECT * FROM @Logs;
    RETURN;
END;

Derived Product changes are normally applied when the line item is create and are not re-applied after that. When importing into a derived product, this poses a challenge since the line item is created outside of Control and the import routine does not (usually) apply all the configuration from the derived product setup.

To overcome this, when you import into a derived product, you must set the TransDetail.ApplyDPChanges field to 1. When this field is set, Control will re-apply the derived product settings when the order is first edited.

By default in the line item import, this is set for derived products (as of the 6/28 version of the wiki).

Contributor: Cyrious Software

Date: 5/2016

Version: Control 5.75+

You could leave a comment if you were logged in.