diff --git a/sponsorship_compassion/models/contract_group.py b/sponsorship_compassion/models/contract_group.py index 8087c8b58..3fd5758e8 100644 --- a/sponsorship_compassion/models/contract_group.py +++ b/sponsorship_compassion/models/contract_group.py @@ -38,11 +38,11 @@ def _compute_contains_sponsorship(self): and s.state not in ("terminated", "cancelled") ) - def _generate_invoices(self, invoicer): + def _generate_invoices(self, invoicer, contract_id=None): # Exclude gifts from regular generation super( ContractGroup, self.with_context(open_invoices_sponsorship_only=True) - )._generate_invoices(invoicer) + )._generate_invoices(invoicer, contract_id) # We don't generate gift if the contract isn't active contracts = self.mapped("contract_ids").filtered(lambda c: c.state == "active") contracts._generate_gifts(invoicer, BIRTHDAY_GIFT) @@ -70,3 +70,38 @@ def _get_partner_for_contract(self, contract): if not contract.send_gifts_to else contract[contract.send_gifts_to] ) + + def _should_skip_invoice_generation(self, invoicing_date, contract=None): + self.ensure_one() + + if contract is None: + return super()._should_skip_invoice_generation(invoicing_date) + + search_filter = [ + ("state", "!=", "cancel"), + ("invoice_date_due", "=", invoicing_date), + ("partner_id", "=", self.partner_id.id), + ("move_type", "=", "out_invoice"), + ("line_ids.contract_id", "=", contract.id), + ( + "line_ids.product_id", + "in", + contract.product_ids.ids, + ), + ] + + existing_invoices = self.env["account.move"].search_count(search_filter) + + is_sub_proposal = contract.parent_id.child_id and not contract.invoice_line_ids + + # If invoices come from sub proposal, ignore group suspension to also generate + # already paid invoices + if is_sub_proposal: + return bool(existing_invoices) + else: + is_suspended = ( + self.invoice_suspended_until + and self.invoice_suspended_until > invoicing_date + ) + return bool(existing_invoices) or is_suspended + diff --git a/sponsorship_compassion/wizards/generate_gift_wizard.py b/sponsorship_compassion/wizards/generate_gift_wizard.py index f882b5525..41d092b03 100644 --- a/sponsorship_compassion/wizards/generate_gift_wizard.py +++ b/sponsorship_compassion/wizards/generate_gift_wizard.py @@ -88,5 +88,8 @@ def _build_invoice_gen_data(self, invoicing_date, invoicer): if not invoicer: invoicer = self.env["recurring.invoicer"].create({}) return self.contract_id.group_id._build_invoice_gen_data( - invoicing_date=invoicing_date, invoicer=invoicer, gift_wizard=self + invoicing_date=invoicing_date, + invoicer=invoicer, + contracts=self.contract_id, + gift_wizard=self, )