Skip to content

Commit

Permalink
[RFC] l10n_br_account: refactor to prevent potential incorret entries
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Dec 9, 2024
1 parent ca193ec commit 9741c82
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
15 changes: 15 additions & 0 deletions l10n_br_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ class AccountMove(models.Model):
compute="_compute_fiscal_operation_type",
)

@api.constrains("fiscal_document_id", "fiscal_operation_id")
def _check_fiscal_operation(self):
for rec in self:
if (
rec.fiscal_operation_id
and not rec.fiscal_operation_id.create_account_move
):
raise UserError(
_(
"You cannot create an invoice when the fiscal operation is set "
"to not create account move. You can use this operation to "
"create a fiscal document only from the Fiscal App."
)
)

@api.constrains("fiscal_document_id", "document_type_id")
def _check_fiscal_document_type(self):
for rec in self:
Expand Down
15 changes: 11 additions & 4 deletions l10n_br_account/models/fiscal_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Operation(models.Model):
company_dependent=True,
)

create_account_move = fields.Boolean(string="Create Account Move", default=True)

def _change_action_view(self, action):
fiscal_op_type = action.get("context")
if fiscal_op_type == "out":
Expand All @@ -51,7 +53,9 @@ def _change_action_view(self, action):
# TODO FIXME migrate!
journal_type = "TODO" # TYPE2JOURNAL[invoice_type]
new_action["context"] = {
"create": False,
"move_type": invoice_type,
"default_company_id": self.env.company.id,
"default_fiscal_operation_type": self.fiscal_operation_type,
"default_fiscal_operation_id": self.id,
"journal_type": journal_type,
Expand All @@ -61,14 +65,17 @@ def _change_action_view(self, action):

def action_create_new(self):
action = super().action_create_new()
action["res_model"] = "account.move"
action["view_id"] = self.env.ref("l10n_br_account.fiscal_invoice_form").id
action["context"] = self._change_action_view(action)["context"]
if self.create_account_move:
action["res_model"] = "account.move"
action["view_id"] = self.env.ref("l10n_br_account.fiscal_invoice_form").id
action["context"] = self._change_action_view(action)["context"]
return action

def open_action(self):
action = super().open_action()
return self._change_action_view(action)
if self.create_account_move:
return self._change_action_view(action)
return action

def _fiscal_document_object(self):
return self.env["account.move"]
Expand Down
4 changes: 4 additions & 0 deletions l10n_br_account/views/fiscal_operation_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
name="deductible_taxes"
attrs="{'readonly': [('state', '!=', 'draft')]}"
/>
<field
name="create_account_move"
attrs="{'readonly': [('state', '!=', 'draft')]}"
/>
</group>
</group>
</page>
Expand Down
5 changes: 3 additions & 2 deletions l10n_br_account/views/l10n_br_account_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
>[('move_type', 'in', ('in_invoice', 'in_receipt', 'out_refund')), ('fiscal_operation_type', '=', 'in')]</field>
<field
name="context"
>{'move_type':'in_invoice', 'journal_type': 'purchase'}</field>
>{'move_type':'in_invoice', 'journal_type': 'purchase', 'create': False}</field>
<field name="search_view_id" ref="l10n_br_account.fiscal_invoice_search" />
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Expand Down Expand Up @@ -127,7 +127,7 @@
>[('move_type', 'in', ('out_invoice', 'out_receipt', 'in_refund')), ('fiscal_operation_type', '=', 'out')]</field>
<field
name="context"
>{'move_type':'out_invoice', 'journal_type': 'sale', 'default_fiscal_operation_type': 'out'}</field>
>{'move_type':'out_invoice', 'journal_type': 'sale', 'default_fiscal_operation_type': 'out', 'create': False}</field>
<field name="search_view_id" ref="l10n_br_account.fiscal_invoice_search" />
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Expand Down Expand Up @@ -172,6 +172,7 @@
<field name="view_mode">tree,kanban,form,pivot,graph,activity</field>
<field eval="False" name="view_id" />
<field name="search_view_id" ref="l10n_br_account.fiscal_invoice_search" />
<field name="context">{'create': False}</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create a new Document
Expand Down

0 comments on commit 9741c82

Please sign in to comment.