| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
|
| 5 |
|
| 6 |
class SaleOrderLine(models.Model): |
| 7 |
_inherit = 'sale.order.line' |
| 8 |
|
| 9 |
is_reward_line = fields.Boolean( |
| 10 |
string="Is a program reward line", compute='_compute_is_reward_line' |
| 11 |
) |
| 12 |
reward_id = fields.Many2one( |
| 13 |
comodel_name='loyalty.reward', ondelete='restrict', readonly=True |
| 14 |
) |
| 15 |
coupon_id = fields.Many2one(comodel_name='loyalty.card', ondelete='restrict', readonly=True) |
| 16 |
reward_identifier_code = fields.Char( |
| 17 |
help="Technical field used to link multiple reward lines from the same reward together." |
| 18 |
) |
| 19 |
points_cost = fields.Float(help="How much point this reward costs on the loyalty card.") |
| 20 |
|
| 21 |
def _compute_name(self): |
| 22 |
|
| 23 |
reward = self.filtered('reward_id') |
| 24 |
super(SaleOrderLine, self - reward)._compute_name() |
| 25 |
|
| 26 |
def _compute_discount(self): |
| 27 |
rewards = self.filtered('reward_id') |
| 28 |
return super(SaleOrderLine, self - rewards)._compute_discount() |
| 29 |
|
| 30 |
@api.depends('reward_id') |
| 31 |
def _compute_is_reward_line(self): |
| 32 |
for line in self: |
| 33 |
line.is_reward_line = bool(line.reward_id) |
| 34 |
|
| 35 |
def _compute_tax_ids(self): |
| 36 |
reward_lines = self.filtered('is_reward_line') |
| 37 |
super(SaleOrderLine, self - reward_lines)._compute_tax_ids() |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
for line in reward_lines: |
| 42 |
line = line.with_company(line.company_id) |
| 43 |
fpos = line.order_id.fiscal_position_id or line.order_id.fiscal_position_id._get_fiscal_position(line.order_partner_id) |
| 44 |
|
| 45 |
taxes = line.tax_ids.filtered(lambda r: not line.company_id or r.company_id == line.company_id) |
| 46 |
line.tax_ids = fpos.map_tax(taxes) |
| 47 |
|
| 48 |
def _get_display_price(self): |
| 49 |
|
| 50 |
|
| 51 |
if self.is_reward_line and self.reward_id.reward_type != 'product': |
| 52 |
return self.price_unit |
| 53 |
return super()._get_display_price() |
| 54 |
|
| 55 |
def _can_be_invoiced_alone(self): |
| 56 |
return super()._can_be_invoiced_alone() and not self.is_reward_line |
| 57 |
|
| 58 |
def _is_discount_line(self): |
| 59 |
return super()._is_discount_line() or self.reward_id.reward_type == 'discount' |
| 60 |
|
| 61 |
def _reset_loyalty(self, complete=False): |
| 62 |
""" |
| 63 |
Reset the line(s) to a state which does not impact reward computation. |
| 64 |
If complete is set to True we also remove the coupon and reward from the line(s). |
| 65 |
This option should be used when the line will be unlinked. |
| 66 |
|
| 67 |
Returns self |
| 68 |
""" |
| 69 |
vals = { |
| 70 |
'points_cost': 0, |
| 71 |
'price_unit': 0, |
| 72 |
'technical_price_unit': 0, |
| 73 |
} |
| 74 |
if complete: |
| 75 |
vals.update({ |
| 76 |
'coupon_id': False, |
| 77 |
'reward_id': False, |
| 78 |
}) |
| 79 |
self.write(vals) |
| 80 |
return self |
| 81 |
|
| 82 |
@api.model_create_multi |
| 83 |
def create(self, vals_list): |
| 84 |
res = super().create(vals_list) |
| 85 |
|
| 86 |
for line in res: |
| 87 |
if line.coupon_id and line.points_cost and line.state == 'sale': |
| 88 |
line.coupon_id.points -= line.points_cost |
| 89 |
line.order_id._update_loyalty_history(line.coupon_id, line.points_cost) |
| 90 |
return res |
| 91 |
|
| 92 |
def write(self, vals): |
| 93 |
cost_in_vals = 'points_cost' in vals |
| 94 |
if cost_in_vals: |
| 95 |
previous_vals = {line: (line.points_cost, line.coupon_id) for line in self} |
| 96 |
res = super().write(vals) |
| 97 |
if cost_in_vals: |
| 98 |
|
| 99 |
for line, (previous_cost, previous_coupon) in previous_vals.items(): |
| 100 |
if line.state != 'sale': |
| 101 |
continue |
| 102 |
if line.points_cost != previous_cost or line.coupon_id != previous_coupon: |
| 103 |
previous_coupon.points += previous_cost |
| 104 |
line.coupon_id.points -= line.points_cost |
| 105 |
return res |
| 106 |
|
| 107 |
def unlink(self): |
| 108 |
|
| 109 |
reward_coupon_set = {(l.reward_id, l.coupon_id, l.reward_identifier_code) for l in self if l.reward_id} |
| 110 |
related_lines = self.env['sale.order.line'] |
| 111 |
related_lines |= self.order_id.order_line.filtered(lambda l: (l.reward_id, l.coupon_id, l.reward_identifier_code) in reward_coupon_set) |
| 112 |
|
| 113 |
coupons_to_unlink = self.env['loyalty.card'] |
| 114 |
for line in self: |
| 115 |
if line.coupon_id: |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if line.coupon_id in line.order_id.applied_coupon_ids: |
| 120 |
line.order_id.applied_coupon_ids -= line.coupon_id |
| 121 |
elif line.coupon_id.order_id == line.order_id and line.coupon_id.program_id.applies_on == 'current' and\ |
| 122 |
not any(oLine.coupon_id == line.coupon_id and oLine not in related_lines for oLine in line.order_id.order_line): |
| 123 |
|
| 124 |
coupons_to_unlink |= line.coupon_id |
| 125 |
line.order_id.code_enabled_rule_ids = line.order_id.code_enabled_rule_ids.filtered(lambda r: r.program_id != line.coupon_id.program_id) |
| 126 |
|
| 127 |
for line in related_lines: |
| 128 |
if line.state == 'sale': |
| 129 |
line.coupon_id.points += line.points_cost |
| 130 |
res = super(SaleOrderLine, self | related_lines).unlink() |
| 131 |
coupons_to_unlink.sudo().unlink() |
| 132 |
return res |
| 133 |
|
| 134 |
def _sellable_lines_domain(self): |
| 135 |
return super()._sellable_lines_domain() + [('reward_id', '=', False)] |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
def _can_be_edited_on_portal(self): |
| 140 |
return super()._can_be_edited_on_portal() and not self.is_reward_line |