Skip to content

Commit 289f083

Browse files
leomp12claude
andcommitted
fix: properly distribute order discount proportionally across items
Updates discount calculation to use total item value (price × quantity) instead of just unit price, ensuring accurate proportional distribution based on each item's contribution to the order subtotal. Fixes #28 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent f55eb6e commit 289f083

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

functions/lib/integration/exports/orders-to-horus.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ module.exports = async ({ appSdk, storeId, auth }, orderId, opts = {}) => {
271271
// logger.info(`QTD_PEDIDA: ${body?.QTD_PEDIDA} itemHorus ${itemHorus?.QTD_PEDIDA} itemOrder: ${item?.quantity}`)
272272

273273
if (isImportItem) {
274-
const itemDiscount = amountSubtotal > 0 ? vlrBruto * amountDiscount / amountSubtotal : 0
274+
const totalItemValue = vlrBruto * item.quantity
275+
const itemDiscount = amountSubtotal > 0 ? totalItemValue * amountDiscount / amountSubtotal : 0
276+
const discountPerUnit = itemDiscount / item.quantity
275277
isAllImportedItems = false
276-
const vlrItem = parsePrice(vlrBruto - itemDiscount)
278+
const vlrItem = parsePrice(vlrBruto - discountPerUnit)
277279
subtotal += (vlrItem * (item.quantity || 1))
278280
body.VLR_LIQUIDO = vlrItem
279281
logger.info(`>> vlrBruto: ${vlrBruto} discount: ${itemDiscount} total: ${vlrItem}`)

0 commit comments

Comments
 (0)