Skip to content

Commit

Permalink
T1657 - Fix invoice generation for Sub Proposal (3/3) (#1947)
Browse files Browse the repository at this point in the history
* pass the current contract when generating invoices

* removed useless comment

* ruff-format

* ruff... again...

* Adapted to new API

* inherit _should_skip_invoice_generation to handle specific case of sub proposal

* adapted condiciton to determine if sponsorship comes from sub proposal

* changed how we check if contract comes from sub proposal

* Added check to see if first invoice generation from sub proposal
  • Loading branch information
Prazn authored Aug 22, 2024
1 parent 7f73bb3 commit 4d2cab6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
39 changes: 37 additions & 2 deletions sponsorship_compassion/models/contract_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

5 changes: 4 additions & 1 deletion sponsorship_compassion/wizards/generate_gift_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 4d2cab6

Please sign in to comment.