Developer forum

Forum » Dynamicweb 10 » Order Line Discount after Live Integration

Order Line Discount after Live Integration

Jason
Reply

We are upgrading dw from 10.15.9 to 10.25.12

orderLineDiscountPercentage,  orderLineTotalDiscountWithVAT and orderLineTotalDiscountWithoutVAT have values in Line number 953-956. However, setting those values into orderline (line 949 to 951) does not change anything and resulting zeros (line 959-961). OrderLineType is product.

It was working in 10.15.9.


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jason,
there were new features implemented for the Discounts and now the property TotalDiscount is recalculated on the getter access. So that is why setting it in the custom provider doesn't work.
The workaround is to use the framework's own existing mechanism for an explicit, exact discount amount: a sibling OrderLineType.ProductDiscount line, which CalculateTotalDiscount() sums additively on top of the percentage-based part:

orderLine.DiscountPercentage = 0; // avoid double-applying a percentage-based discount too

var marker = "LiveIntegrationDiscount"; // any stable identifier you control
var discountLine = order.OrderLines.FirstOrDefault(l =>
    l.OrderLineType == OrderLineType.ProductDiscount &&
    string.Equals(l.ParentLineId, orderLine.Id, StringComparison.OrdinalIgnoreCase) &&
    string.Equals(l.DiscountId, marker, StringComparison.Ordinal));

bool isNew = discountLine is null;
discountLine ??= new OrderLine(order)
{
    OrderLineType = OrderLineType.ProductDiscount,
    ParentLineId = orderLine.Id,
    Quantity = 1,
    DiscountId = marker,
    ProductName = "Discount"
};

var discountPrice = new PriceInfo(order.Currency)
{
    PriceWithVAT = -Math.Abs(orderLineTotalDiscountWithVAT),
    PriceWithoutVAT = -Math.Abs(orderLineTotalDiscountWithoutVAT)
};

// forcePriceRecalculation:true is required here — Order.Calculate is false for imported/live-integration
// orders, so OrderLine.Price would otherwise stay at its old stored value instead of picking up UnitPrice.
Services.OrderLines.SetUnitPrice(discountLine, discountPrice, forcePriceRecalculation: true);

if (isNew)
    order.OrderLines.Add(discountLine);


The DiscountId marker + lookup means re-running the live integration against the same order updates the existing discount line instead of stacking a new one every time (which would otherwise double/triple the discount on each sync).
After this, orderLine.TotalDiscount.PriceWithVAT/PriceWithoutVAT will read back correctly (via the getter, which now sums this child line's Price plus a zero percentage-based contribution) — matching what the integration intended, without touching any of the currently-broken direct-assignment paths.

Kind regards,
Dmitrij

 
Jason
Reply

Hello
Dmitrij

FYI. My provided screenshot codes is from my customize liveintegration. So there is no way to have Discount information on the Product OrderLineType without creating productdiscount line?


This what I am expecting.



i want to have Discount % saving functionality on my LiveIntegration. Can we do that?

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Jason,
it's not "always needs a ProductDiscount line" — it's "needs one only when the discount's VAT doesn't match the product line's own VAT rate." If your ERP always computes discounts using the same VAT rate as the line itself (common case), setting DiscountPercentage correctly is sufficient and simpler.

If your ERP's orderLineTotalDiscountWithVAT/WithoutVAT are consistent with the order line's own VAT% (i.e. WithVAT ≈ WithoutVAT × (1 + VATPercent/100)), just set the percentage and no ProductDiscount line needed:

orderLine.DiscountPercentage = (orderLineTotalDiscountWithVAT / (orderLine.UnitPrice.PriceWithVAT * orderLine.Quantity)) * 100;

If those two ERP numbers aren't proportionally consistent with the line's VAT% (different VAT treatment on the discount, independent rounding from the ERP's own tax engine, etc.) — then yes, a ProductDiscount line is the only way, because that's the only construct with its own independent Price (its own PriceWithVAT, PriceWithoutVAT, VATPercent) not derived by scaling the parent line.

BR, Dmitrij

 

 

You must be logged in to post in the forum