Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions addons/pos_loyalty/static/src/overrides/models/pos_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,21 +1186,25 @@ patch(PosOrder.prototype, {
return _t("No product is compatible with this promotion.");
}

const untaxedAmount = matchingLines.reduce(
(sum, line) => sum + line.get_price_without_tax(),
0
);
// Discount amount should not exceed total untaxed amount of the matching lines
const applicableAmount = matchingLines.reduce((sum, line) => {
const lineAmount = line.tax_ids.some((tax) => tax.price_include)
? line.get_price_with_tax()
: line.get_price_without_tax();
return sum + lineAmount;
}, 0);

// Discount amount should not exceed applicable amount
rewardLineValues[0].price_unit = Math.max(
-untaxedAmount,
-applicableAmount,
rewardLineValues[0].price_unit
);

rewardLineValues[0].tax_ids = rewardTaxes;
}
// Discount amount should not exceed the untaxed amount on the order
if (Math.abs(rewardLineValues[0].price_unit) > this.amount_untaxed) {
rewardLineValues[0].price_unit = -this.amount_untaxed;
} else {
// Discount amount should not exceed the untaxed amount on the order
if (Math.abs(rewardLineValues[0].price_unit) > this.amount_untaxed) {
rewardLineValues[0].price_unit = -this.amount_untaxed;
}
}
return rewardLineValues;
}
Expand Down
17 changes: 10 additions & 7 deletions addons/sale_loyalty/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,21 @@ def _get_reward_values_discount(self, reward, coupon, **kwargs):
if not matching_lines:
raise ValidationError(_("No product is compatible with this promotion."))

untaxed_amount = sum(line.price_subtotal for line in matching_lines)
# Discount amount should not exceed total untaxed amount of the matching lines
applicable_amount = sum(
line.price_total if any(mapped_taxes.mapped('price_include'))
else line.price_subtotal for line in matching_lines
)
# Discount amount should not exceed applicable amount
reward_line_values['price_unit'] = max(
-untaxed_amount,
-applicable_amount,
reward_line_values['price_unit']
)

reward_line_values['tax_id'] = [Command.set(mapped_taxes.ids)]

# Discount amount should not exceed the untaxed amount on the order
if abs(reward_line_values['price_unit']) > self.amount_untaxed:
reward_line_values['price_unit'] = -self.amount_untaxed
else:
# Discount amount should not exceed the untaxed amount on the order
if abs(reward_line_values['price_unit']) > self.amount_untaxed:
reward_line_values['price_unit'] = -self.amount_untaxed

return [reward_line_values]

Expand Down