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