Skip to content

Commit

Permalink
T1657 - Fix dupplicate invoice generation with CRON (#242)
Browse files Browse the repository at this point in the history
* Added some comments where the issue is, still not sure how we should handle it...

* Changed condition to check if invoiced need to be generated in the case of groups, seems to make more sense to me...

* code cleanup

* added comment

* comment

* reverted dummy changes + PR suggestion
  • Loading branch information
Prazn authored and ecino committed Sep 10, 2024
1 parent beea78b commit edd60b2
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions recurring_contract/models/contract_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,17 @@ def _calculate_start_date_and_offset(self):

def _should_skip_invoice_generation(self, invoicing_date, contract=None):
"""In such cases, we should skip the invoice generation:
- There is already an invoice for this due date which has been cancelled or
edited.
- Contract group suspension.
- A specific contract is given and an invoice for this due date already exists
and isn't cancelled.
- All active contracts already have an invoice for this due date that
isn't cancelled.
- Contract group suspension.
"""
self.ensure_one()

has_all_invoices = False

if contract:
search_filter = [
("state", "!=", "cancel"),
Expand All @@ -343,6 +346,8 @@ def _should_skip_invoice_generation(self, invoicing_date, contract=None):
contract.product_ids.ids,
),
]

has_all_invoices = bool(self.env["account.move"].search_count(search_filter))
else:
search_filter = [
("invoice_date_due", "=", invoicing_date),
Expand All @@ -354,19 +359,20 @@ def _should_skip_invoice_generation(self, invoicing_date, contract=None):
"in",
self.active_contract_ids.mapped("product_ids").ids,
),
"|",
("payment_state", "not in", ["paid", "not_paid"]),
("state", "=", "cancel"),
('state', '!=', 'cancel')
]

existing_invoices = self.env["account.move"].search_count(search_filter)
open_invoices = self.env["account.move"].search(search_filter)

has_all_invoices = len(self.active_contract_ids -
open_invoices.mapped("line_ids.contract_id")) == 0

is_suspended = (
self.invoice_suspended_until
and self.invoice_suspended_until > invoicing_date
)

return bool(existing_invoices) or is_suspended
return has_all_invoices or is_suspended

def _process_invoice_generation(self, invoicer, invoicing_date, contract=None):
self.ensure_one()
Expand Down

0 comments on commit edd60b2

Please sign in to comment.