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.

65 lines
2.3 KiB

  1. from odoo import models, api, _
  2. from odoo.exceptions import UserError
  3. class ResPartner(models.Model):
  4. _inherit = "res.partner"
  5. def initial_membership_check(self):
  6. self.ensure_one()
  7. partner = self.associate_member or self
  8. member_lines = self.env["membership.membership_line"].search(
  9. [
  10. ("partner", "=", partner.id),
  11. ("state", "not in", ["none", "canceled"]),
  12. ]
  13. )
  14. return not bool(member_lines)
  15. def prepare_membership_invoice_vals(self, product, amount):
  16. discount = 0.0
  17. if self.initial_membership_check():
  18. if product.initial_discount == "fixed" and product.fixed_discount:
  19. print("fixed discount")
  20. fixed_disc = product.fixed_discount
  21. amount -= fixed_disc
  22. elif product.initial_discount == "percent" and product.percent_discount:
  23. print("percent discount")
  24. discount = product.percent_discount
  25. else:
  26. pass
  27. vals = {
  28. "move_type": "out_invoice",
  29. "partner_id": self.id,
  30. "invoice_line_ids": [
  31. (
  32. 0,
  33. None,
  34. {
  35. "product_id": product.id,
  36. "quantity": 1,
  37. "price_unit": amount,
  38. "discount": discount,
  39. "tax_ids": [(6, 0, product.taxes_id.ids)],
  40. },
  41. )
  42. ],
  43. }
  44. return vals
  45. def create_membership_invoice(self, product, amount):
  46. """Create Customer Invoice of Membership for partners."""
  47. invoice_vals_list = []
  48. for partner in self:
  49. addr = partner.address_get(["invoice"])
  50. if partner.free_member:
  51. raise UserError(_("Partner is a free Member."))
  52. if not addr.get("invoice", False):
  53. raise UserError(
  54. _("Partner doesn't have an address to make the invoice.")
  55. )
  56. invoice_vals_list.append(
  57. partner.prepare_membership_invoice_vals(product, amount)
  58. )
  59. return self.env["account.move"].create(invoice_vals_list)