Skip to content

Commit

Permalink
[RFC] l10n_br_account: refactor method to override create in fiscal.d…
Browse files Browse the repository at this point in the history
…ocument.line
  • Loading branch information
marcelsavegnago committed May 6, 2024
1 parent 1c2a15d commit 8d648d7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions l10n_br_account/models/fiscal_document_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,26 @@ class FiscalDocumentLine(models.Model):
@api.model_create_multi
def create(self, vals_list):
"""
It's not allowed to create a fiscal document line without a document_id anyway.
But instead of letting Odoo crash in this case we simply avoid creating the
record. This makes it possible to create an account.move.line without
a fiscal_document_line_id: Odoo will write NULL as the value in this case.
This is a requirement to allow account moves without fiscal documents despite
the _inherits system.
Override the create method to ensure it filters out account.move.line records
that lack a valid document_id or fiscal_operation_line_id. Prevent the
creation of fiscal document lines without these mandatory fields to avoid
system crashes due to invalid records. If the conditions are not met, return an
empty list instead of creating any records. This supports the creation of
account.move.line records with NULL values for fiscal_document_line_id where
necessary.
"""

if self._context.get("create_from_move_line"):
if not any(vals.get("document_id") for vals in vals_list):
# Filter out the dictionaries that do not meet the conditions
filtered_vals_list = [
vals
for vals in vals_list
if vals.get("document_id") and vals.get("fiscal_operation_line_id")
]
# Stop execution and return empty if no dictionary meets the conditions
if not filtered_vals_list:
return []
# Assign the filtered list back to the original list for further processing
vals_list = filtered_vals_list

return super().create(vals_list)

0 comments on commit 8d648d7

Please sign in to comment.