from odoo import models, api, _ from odoo.exceptions import UserError class ResPartner(models.Model): _inherit = "res.partner" def initial_membership_check(self): self.ensure_one() partner = self.associate_member or self member_lines = self.env["membership.membership_line"].search( [ ("partner", "=", partner.id), ("state", "not in", ["none", "canceled"]), ] ) return not bool(member_lines) def prepare_membership_invoice_vals(self, product, amount): discount = 0.0 if self.initial_membership_check(): if product.initial_discount == "fixed" and product.fixed_discount: print("fixed discount") fixed_disc = product.fixed_discount amount -= fixed_disc elif product.initial_discount == "percent" and product.percent_discount: print("percent discount") discount = product.percent_discount else: pass vals = { "move_type": "out_invoice", "partner_id": self.id, "invoice_line_ids": [ ( 0, None, { "product_id": product.id, "quantity": 1, "price_unit": amount, "discount": discount, "tax_ids": [(6, 0, product.taxes_id.ids)], }, ) ], } return vals def create_membership_invoice(self, product, amount): """Create Customer Invoice of Membership for partners.""" invoice_vals_list = [] for partner in self: addr = partner.address_get(["invoice"]) if partner.free_member: raise UserError(_("Partner is a free Member.")) if not addr.get("invoice", False): raise UserError( _("Partner doesn't have an address to make the invoice.") ) invoice_vals_list.append( partner.prepare_membership_invoice_vals(product, amount) ) return self.env["account.move"].create(invoice_vals_list)