You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.5 KiB

11 months ago
11 months ago
11 months ago
  1. # License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0
  2. from odoo import api, models, _
  3. class AccountMoveLine(models.Model):
  4. _inherit = "account.move.line"
  5. @api.depends("product_id", "product_uom_id")
  6. def _compute_price_unit(self):
  7. super()._compute_price_unit()
  8. for line in self:
  9. if line.move_id.is_invoice() and line.initial_membership_check():
  10. product = line.product_id
  11. if product.initial_discount == "fixed" and product.fixed_discount:
  12. line.price_unit -= product.fixed_discount
  13. @api.onchange("product_id")
  14. def _inverse_product_id(self):
  15. super()._inverse_product_id()
  16. for line in self:
  17. if not line.product_id or line.display_type in (
  18. "line_section",
  19. "line_note",
  20. ):
  21. continue
  22. if line.move_id.is_invoice() and line.initial_membership_check():
  23. product = line.product_id
  24. if product.initial_discount == "percent" and product.percent_discount:
  25. line.discount = product.percent_discount
  26. else:
  27. pass
  28. def initial_membership_check(self):
  29. """
  30. Inherit this method to implement a custom method
  31. to decide whether or not to create the initial discount
  32. :return:
  33. """
  34. self.ensure_one()
  35. product = self.product_id
  36. if not product or not product.membership or product.initial_discount == "none":
  37. return False
  38. # If we are associated to another partner membership, evaluate that
  39. # partner lines
  40. partner = self.partner_id.associate_member or self.move_id.partner_id
  41. # By default, partner to check is the partner of the invoice, but
  42. # if a special method is found, overwritten in other modules, then
  43. # the partner is got from that method
  44. if hasattr(self, "_get_partner_for_membership"): # pragma: no cover
  45. partner = self._get_partner_for_membership()
  46. # See if partner has any membership line to decide whether or not
  47. # to create the initial discount
  48. member_lines = self.env["membership.membership_line"].search(
  49. [
  50. ("partner", "=", partner.id),
  51. (
  52. "account_invoice_line",
  53. "not in",
  54. [self.id or self._origin.id],
  55. ),
  56. ("state", "not in", ["none", "canceled"]),
  57. ]
  58. )
  59. return not bool(member_lines)