# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0 from odoo import api, models, _ class AccountMoveLine(models.Model): _inherit = "account.move.line" @api.depends("product_id", "product_uom_id") def _compute_price_unit(self): super()._compute_price_unit() for line in self: if line.move_id.is_invoice() and line.initial_membership_check(): product = line.product_id if product.initial_discount == "fixed" and product.fixed_discount: line.price_unit -= product.fixed_discount @api.onchange("product_id") def _inverse_product_id(self): super()._inverse_product_id() for line in self: if not line.product_id or line.display_type in ( "line_section", "line_note", ): continue if line.move_id.is_invoice() and line.initial_membership_check(): product = line.product_id if product.initial_discount == "percent" and product.percent_discount: line.discount = product.percent_discount else: pass def initial_membership_check(self): """ Inherit this method to implement a custom method to decide whether or not to create the initial discount :return: """ self.ensure_one() product = self.product_id if not product or not product.membership or product.initial_discount == "none": return False # If we are associated to another partner membership, evaluate that # partner lines partner = self.partner_id.associate_member or self.move_id.partner_id # By default, partner to check is the partner of the invoice, but # if a special method is found, overwritten in other modules, then # the partner is got from that method if hasattr(self, "_get_partner_for_membership"): # pragma: no cover partner = self._get_partner_for_membership() # See if partner has any membership line to decide whether or not # to create the initial discount member_lines = self.env["membership.membership_line"].search( [ ("partner", "=", partner.id), ( "account_invoice_line", "not in", [self.id or self._origin.id], ), ("state", "not in", ["none", "canceled"]), ] ) return not bool(member_lines)