Skip to content

Commit

Permalink
[IMP] (*)_loyalty: side improvements
Browse files Browse the repository at this point in the history
Some guidelines/docstring improvements.
Correct consideration of the currency decimal places
when checking whether an amount is zero.

Part-of: #144389
  • Loading branch information
Feyensv committed Apr 30, 2024
1 parent b09bb8c commit 73c3140
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion addons/loyalty/models/loyalty_reward.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import ast
Expand All @@ -7,6 +6,7 @@
from odoo import _, api, fields, models
from odoo.osv import expression


class LoyaltyReward(models.Model):
_name = 'loyalty.reward'
_description = 'Loyalty Reward'
Expand Down
29 changes: 22 additions & 7 deletions addons/sale_loyalty/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from odoo.exceptions import UserError, ValidationError
from odoo.fields import Command
from odoo.osv import expression
from odoo.tools.float_utils import float_is_zero, float_round
from odoo.tools import float_round, lazy


def _generate_random_reward_code():
Expand Down Expand Up @@ -43,9 +43,7 @@ def _compute_reward_total(self):
order.reward_amount = reward_amount

def _get_no_effect_on_threshold_lines(self):
"""
Returns the lines that have no effect on the minimum amount to reach
"""
"""Return the lines that have no effect on the minimum amount to reach."""
self.ensure_one()
return self.env['sale.order.line']

Expand Down Expand Up @@ -157,15 +155,32 @@ def _get_reward_values_product(self, reward, coupon, product=None, **kwargs):
}]

def _discountable_order(self, reward):
"""
Returns the discountable and discountable_per_tax for a discount that applies to the whole order
"""Compute the `discountable` amount (and amounts per tax group) for the current order.
:param reward: if provided, the reward whose discountable amounts must be computed.
It must be applicable at the order level.
:type reward: `loyalty.reward` record, can be empty to compute the amounts regardless of the
program configuration
:return: A tuple with the first element being the total discountable amount of the order,
and the second a dictionary mapping each non-fixed taxes group to its corresponding
total untaxed amount of the eligible order lines.
:rtype: tuple(float, dict(account.tax: float))
"""
self.ensure_one()
reward.ensure_one()
assert reward.discount_applicability == 'order'

discountable = 0
discountable_per_tax = defaultdict(int)
lines = self.order_line if reward.program_id.is_payment_program else (self.order_line - self._get_no_effect_on_threshold_lines())

if reward.program_id.is_payment_program:
# Gift cards and eWallets are applied on the total order amount
lines = self.order_line
else:
# Other types of programs are not expected to apply on delivery lines
lines = self.order_line - self._get_no_effect_on_threshold_lines()

for line in lines:
# Ignore lines from this reward
if not line.product_uom_qty or not line.price_unit:
Expand Down
5 changes: 2 additions & 3 deletions addons/website_sale_loyalty/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from odoo.exceptions import UserError
from odoo.http import request
from odoo.osv import expression
from odoo.tools import float_is_zero


class SaleOrder(models.Model):
_inherit = "sale.order"
_inherit = 'sale.order'

# List of disabled rewards for automatic claim
disabled_auto_rewards = fields.Many2many("loyalty.reward", relation="sale_order_disabled_auto_rewards_rel")
Expand Down Expand Up @@ -211,7 +210,7 @@ def _get_claimable_and_showable_rewards(self):
('program_id.trigger', '=', 'with_code'),
'&', ('program_id.trigger', '=', 'auto'), ('program_id.applies_on', '=', 'future'),
])
total_is_zero = float_is_zero(self.amount_total, precision_digits=2)
total_is_zero = self.currency_id.is_zero(self.amount_total)
global_discount_reward = self._get_applied_global_discount()
for coupon in loyality_cards:
points = self._get_real_points_for_coupon(coupon)
Expand Down

0 comments on commit 73c3140

Please sign in to comment.