Developer forum

Forum » Integration » BC Integration - Invoice discount including shipping fee

BC Integration - Invoice discount including shipping fee

Jon Thorne
Jon Thorne
Reply

I have found an issue when trying to apply a full credit/discount to an order. This issue is related to the shipping fee.

I want to apply an invoice discount amount of the full value of the order including the shipping fee.

In the BC code unit the shipping fee is added after the discount order line. So it gives an error when trying to apply a discount amount greater than the sum of all the product lines.

Is there any workaround for this? Would it be possible to update the integration to allow applying an invoice discount including shipping fee?


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jon,
we need more information about your setup. Who is handling discounts BC or Dynamicweb? Who is handling the shipping fee BC or Dynamicweb?
Is it a problem in Calculate order or Create order request?
BR, Dmitrij

 
Jon Thorne
Jon Thorne
Reply

Hi Dmitrij,

Dynamicweb is handing the shipping fee and discounts.

There is a problem when the discount = orderlines total+shipping fee. Total price for the order is $0.

When BC integration code handles this it adds all the order lines and then tries to apply the invoice discount (before adding the shipping line). BC will give an error as the invoice discount is greater than the total of the order lines as the shipping is not added until later in the integration code.

Regards,

Jon.

 
Jon Thorne
Jon Thorne
Reply

My temporary workaround for now is to check if the discount includes the shipping fee amount and update the order xml to set the shipping fee to "0" and reduce the invoice discount to only the total of the product order lines. This gives the same result for sales order total ($0), but does not record a shipping fee line in BC. But at least it gets around this issue for now and I can transfer the order to BC.

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jon,
then you can use this approach, in the CreateOrderOnAddCustomOrderLines subscriber you can add the Dynamicweb shipping line so its sales line is then inserted  before the discount calculations are made. Then in the other subcriber OnBeforeInsertDynamicwebShippingSalesLine cancel the regular codeunit shipping line addition.
So the code for this can look like this:

[EventSubscriber(ObjectType::Codeunit, Codeunit::DynamicwebOrdersPublisher, 'OnBeforeInsertDynamicwebShippingSalesLine', '', true, true)]
    procedure OnBeforeInsertDynamicwebShippingSalesLine(var salesline: Record "Sales Line"; var cancelAdd: Boolean);
    begin        
        cancelAdd := true;
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::DynamicwebOrdersPublisher, 'CreateOrderOnAddCustomOrderLines', '', true, true)]
    procedure CreateOrderOnAddCustomOrderLines(var responseEcomOrderLinesNode: XmlNode; var lastLineNo: Integer; var salesheader: Record "Sales Header"; var Currency: Record Currency; useDynamicwebDiscount: boolean; var customer: Record Customer;
    var OrderPriceWithVAT: Decimal; var OrderPriceWithoutVAT: Decimal; var OrderPriceVAT: Decimal; var OrderSalesDiscount: Decimal; var requestOrderNode: XmlNode);
    VAR        
        dynamicwebOrderShippingItemKey: Text;
        dynamicwebShippingFee: Decimal;
        dynamicwebControlsShipping: Boolean;
        node: XmlNode;
        dynamicwebShippingFeeWithoutVat: Decimal;
        itemType: Text;
        XmlHelper: Codeunit DynamicwebXmlHelper;
    begin
        if requestOrderNode.SelectSingleNode('column[@columnName=''OrderShippingItemKey'']', node) then
            dynamicwebControlsShipping := true;

        IF dynamicwebControlsShipping THEN BEGIN
            dynamicwebShippingFee := XmlHelper.GetDecimalFromNode(requestOrderNode, 'column[@columnName=''OrderShippingFee'']');
            IF dynamicwebShippingFee <> 0 THEN BEGIN
                dynamicwebShippingFeeWithoutVat := XmlHelper.GetDecimalFromNode(requestOrderNode, 'column[@columnName=''OrderShippingFeeWithoutVat'' or @columnName=''OrderShippingFeeWithoutVAT'']');
                IF (dynamicwebShippingFeeWithoutVat = 0) THEN
                    dynamicwebShippingFeeWithoutVat := dynamicwebShippingFee;
                
                lastLineNo += 10000;

                dynamicwebOrderShippingItemKey := XmlHelper.GetTextFromNode(requestOrderNode, 'column[@columnName=''OrderShippingItemKey'']');
                    IF dynamicwebOrderShippingItemKey <> '' THEN
                        itemType := XmlHelper.GetTextFromNode(requestOrderNode, 'column[@columnName=''OrderShippingItemType'']');
                        
                        //find some first product from the sales header order lines and set the firstOrderLineItem
                        VATPostingSetup.GET(customer."VAT Bus. Posting Group", firstOrderLineItem."VAT Prod. Posting Group");
                     case itemType of
                    //     'Account':
                    //         //AddShippingAccountLine(requestOrderNode, lastLineNo, salesHeader, firstOrderLineItem, dynamicwebOrderShippingItemKey,
                    //             //dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
                    //     'Resource':
                    //         //AddShippingResourceLine(requestOrderNode, lastLineNo, salesHeader, firstOrderLineItem, dynamicwebOrderShippingItemKey,
                    //                     //dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
                    //     'FixedAsset':
                    //         //AddShippingFixedAssetLine(requestOrderNode, lastLineNo, salesHeader, firstOrderLineItem, dynamicwebOrderShippingItemKey,
                    //                         //dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
                         'Item':
                             AddShippingItemLine(requestOrderNode, lastLineNo, salesHeader, firstOrderLineItem, dynamicwebOrderShippingItemKey,
                                                 dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
                    //     else
                    //         //AddShippingItemChargeLine(requestOrderNode, lastLineNo, salesHeader, firstOrderLineItem, dynamicwebOrderShippingItemKey,
                    //                     //dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
            END;
        END;
    end;

    LOCAL PROCEDURE AddShippingItemLine(VAR XMLOrderNode: XmlNode; lineNo: Integer; salesHeader: Record "Sales Header";
         var firstOrderLineItem: Record Item; dynamicwebOrderShippingItemKey: Text; dynamicwebShippingFee: Decimal; dynamicwebShippingFeeWithoutVat: Decimal;
         var VATPostingSetup: Record "VAT Posting Setup"; var customer: Record Customer);
    VAR
        salesLine: Record "Sales Line";
        cancelAdd: Boolean;
        item: Record Item;
        XmlHelper: Codeunit DynamicwebXmlHelper;
    BEGIN
        IF item.GET(dynamicwebOrderShippingItemKey) THEN BEGIN
            PrepareShippingLine(salesLine, salesLine.Type::Item, item."No.",
                item.Description, lineNo, item."VAT Prod. Posting Group", salesHeader,
                firstOrderLineItem, dynamicwebShippingFee, dynamicwebShippingFeeWithoutVat, VATPostingSetup, customer);
            salesLine."Tax Group Code" := item."Tax Group Code";

            
        END ELSE BEGIN
            XmlHelper.AddField(XMLOrderNode, 'OrderShippingWarning', STRSUBSTNO('Can not add Dynamicweb shipping cost to BC Sales order due to missing Item: "%1"', dynamicwebOrderShippingItemKey));
        END;
    END;

LOCAL PROCEDURE PrepareShippingLine(var salesLine: Record "Sales Line"; lineType: Enum "Sales Line Type"; no: Code[20]; description: Text; lineNo: Integer;
            VATProdPostingGroup: Code[20]; salesHeader: Record "Sales Header";
            firstOrderLineItem: Record Item; dynamicwebShippingFee: Decimal; dynamicwebShippingFeeWithoutVat: Decimal;
            var SalesLineVATPostingSetup: Record "VAT Posting Setup"; var customer: Record Customer);
    var
        VATSetup: Record "VAT Posting Setup";
    BEGIN
        if not VATSetup.Get(customer."VAT Bus. Posting Group", VATProdPostingGroup) then
            VATSetup := SalesLineVATPostingSetup;
        salesLine.INIT;
        salesLine."Document Type" := salesHeader."Document Type";
        salesLine."Document No." := salesHeader."No.";
        salesLine."Line No." := lineNo;
        salesLine.Type := lineType;
        salesLine."No." := no;
        salesLine.Description := description;
        salesLine."Qty. Assigned" := 1;
        salesLine."Sell-to Customer No." := salesHeader."Sell-to Customer No.";
        salesLine."Bill-to Customer No." := salesHeader."Bill-to Customer No.";
        salesLine."Currency Code" := salesheader."Currency Code";
        salesLine."Location Code" := salesHeader."Location Code";
        salesLine."VAT Identifier" := VATSetup."VAT Identifier";
        IF VATSetup."VAT Calculation Type" <> VATSetup."VAT Calculation Type"::"Reverse Charge VAT" THEN
            salesLine."VAT %" := VATSetup."VAT %";

        if salesHeader."Prices Including VAT" then begin
            salesLine."Unit Price" := dynamicwebShippingFee;
        end else begin
            salesLine."Unit Price" := dynamicwebShippingFeeWithoutVat;
        end;
        salesLine.VALIDATE(Quantity, 1);
        if salesHeader."Prices Including VAT" then begin
            salesLine."Line Amount" := dynamicwebShippingFee;
        end else begin
            salesLine."Line Amount" := dynamicwebShippingFeeWithoutVat;
        end;
        salesLine."VAT Base Amount" := dynamicwebShippingFeeWithoutVat;
        salesLine."VAT Difference" := dynamicwebShippingFee - dynamicwebShippingFeeWithoutVat;
        salesLine.VALIDATE(Amount, dynamicwebShippingFeeWithoutVat);
        salesLine."Amount Including VAT" := dynamicwebShippingFee;
        salesLine."Outstanding Amount" := dynamicwebShippingFee;
        salesLine."VAT Prod. Posting Group" := VATProdPostingGroup;
        salesLine."Gen. Bus. Posting Group" := salesHeader."Gen. Bus. Posting Group";
        salesLine."VAT Bus. Posting Group" := salesHeader."VAT Bus. Posting Group";
        salesLine."Gen. Prod. Posting Group" := firstOrderLineItem."Gen. Prod. Posting Group";
        salesLine."Dimension Set ID" := salesHeader."Dimension Set ID";
        salesLine."Unit of Measure" := firstOrderLineItem."Base Unit of Measure";
        salesLine."Unit of Measure Code" := firstOrderLineItem."Base Unit of Measure";
        salesLine."Outstanding Amount" := salesLine."Amount Including VAT";
        salesLine."Outstanding Amount (LCY)" := salesLine."Amount Including VAT";
        salesLine."Shortcut Dimension 1 Code" := salesHeader."Shortcut Dimension 1 Code";
        salesLine."Shortcut Dimension 2 Code" := salesHeader."Shortcut Dimension 2 Code";
        salesLine."VAT Calculation Type" := VATSetup."VAT Calculation Type";
    END;

BR, Dmitrij

 
Jon Thorne
Jon Thorne
Reply

Hi Dmitrij,

Thanks so much for the detailed help with this. I will give it a try.

Regards, Jon.

 

You must be logged in to post in the forum