2
0

[FIX] account_move_template: fix JS crash in analytic distribution

Add _check_company_auto = True where relevant
Fix layout of account.move.template form view
Code cleanup
This commit is contained in:
Alexis de Lattre 2023-06-09 19:17:38 +02:00
parent e85ddb6842
commit 35e45894ec
4 changed files with 82 additions and 76 deletions

View File

@ -9,6 +9,7 @@ from odoo.tools.safe_eval import safe_eval
class AccountMoveTemplate(models.Model):
_name = "account.move.template"
_description = "Journal Entry Template"
_check_company_auto = True
name = fields.Char(required=True)
company_id = fields.Many2one(
@ -18,7 +19,13 @@ class AccountMoveTemplate(models.Model):
ondelete="cascade",
default=lambda self: self.env.company,
)
journal_id = fields.Many2one("account.journal", string="Journal", required=True)
journal_id = fields.Many2one(
"account.journal",
string="Journal",
required=True,
check_company=True,
domain="[('company_id', '=', company_id)]",
)
ref = fields.Char(string="Reference", copy=False)
line_ids = fields.One2many(
"account.move.template.line", inverse_name="template_id", string="Lines"
@ -106,27 +113,29 @@ class AccountMoveTemplateLine(models.Model):
_name = "account.move.template.line"
_description = "Journal Item Template"
_order = "sequence, id"
_inherit = "analytic.mixin"
_check_company_auto = True
template_id = fields.Many2one(
"account.move.template", string="Move Template", ondelete="cascade"
)
name = fields.Char(string="Label", required=True)
name = fields.Char(string="Label")
sequence = fields.Integer(required=True)
account_id = fields.Many2one(
"account.account",
string="Account",
required=True,
domain=[("deprecated", "=", False)],
domain="[('company_id', '=', company_id), ('deprecated', '=', False)]",
check_company=True,
)
partner_id = fields.Many2one(
"res.partner",
string="Partner",
domain=["|", ("parent_id", "=", False), ("is_company", "=", True)],
)
analytic_distribution = fields.Json(string="New Analytic Distribution")
tax_ids = fields.Many2many("account.tax", string="Taxes")
tax_ids = fields.Many2many("account.tax", string="Taxes", check_company=True)
tax_line_id = fields.Many2one(
"account.tax", string="Originator Tax", ondelete="restrict"
"account.tax", string="Originator Tax", ondelete="restrict", check_company=True
)
company_id = fields.Many2one(related="template_id.company_id", store=True)
company_currency_id = fields.Many2one(
@ -136,11 +145,14 @@ class AccountMoveTemplateLine(models.Model):
)
note = fields.Char()
type = fields.Selection(
[("computed", "Computed"), ("input", "User input")],
[
("input", "User input"),
("computed", "Computed"),
],
required=True,
default="input",
)
python_code = fields.Text()
python_code = fields.Text(string="Formula")
move_line_type = fields.Selection(
[("cr", "Credit"), ("dr", "Debit")], required=True, string="Direction"
)
@ -158,12 +170,12 @@ class AccountMoveTemplateLine(models.Model):
string="Tax Repartition Line",
compute="_compute_tax_repartition_line_id",
store=True,
readonly=True,
)
opt_account_id = fields.Many2one(
"account.account",
string="Account Opt.",
domain=[("deprecated", "=", False)],
domain="[('company_id', '=', company_id), ('deprecated', '=', False)]",
check_company=True,
help="When amount is negative, use this account instead",
)
@ -181,6 +193,23 @@ class AccountMoveTemplateLine(models.Model):
limit=1,
)
@api.depends("account_id", "partner_id")
def _compute_analytic_distribution(self):
for line in self:
distribution = self.env[
"account.analytic.distribution.model"
]._get_distribution(
{
"partner_id": line.partner_id.id,
"partner_category_id": line.partner_id.category_id.ids,
"product_id": False,
"product_categ_id": False,
"account_prefix": line.account_id.code,
"company_id": line.template_id.company_id.id,
}
)
line.analytic_distribution = distribution or line.analytic_distribution
_sql_constraints = [
(
"sequence_template_uniq",
@ -194,6 +223,6 @@ class AccountMoveTemplateLine(models.Model):
for line in self:
if line.type == "computed" and not line.python_code:
raise ValidationError(
_("Python Code must be set for computed line with " "sequence %d.")
_("Python Code must be set for computed line with sequence %d.")
% line.sequence
)

View File

@ -6,11 +6,8 @@
<field name="arch" type="xml">
<tree>
<field name="sequence" />
<field name="account_id" domain="[('company_id', '=', company_id)]" />
<field
name="opt_account_id"
domain="[('company_id', '=', company_id)]"
/>
<field name="account_id" />
<field name="opt_account_id" />
<field name="partner_id" />
<field name="name" />
<field
@ -40,38 +37,20 @@
<form string="Journal Entry Template Line">
<sheet>
<group name="main">
<group name="account">
<group name="main-left">
<field name="sequence" />
<field name="name" />
<field
name="account_id"
domain="[('company_id', '=', company_id)]"
/>
<field
name="opt_account_id"
domain="[('company_id', '=', company_id)]"
/>
<field name="account_id" />
<field name="opt_account_id" />
<field name="partner_id" />
<field name="payment_term_id" />
<field name="company_id" invisible="1" />
</group>
<group
groups="analytic.group_analytic_accounting"
string="Analytic"
name="analytic"
>
<field name="name" />
<field
name="analytic_distribution"
widget="analytic_distribution"
groups="analytic.group_analytic_accounting"
options="{'account_field': 'account_id'}"
/>
</group>
<group name="amount" string="Amount">
<field name="move_line_type" />
<field name="type" />
<field name="note" />
</group>
<group string="Taxes" name="tax">
<field name="payment_term_id" />
<field name="is_refund" />
<field name="tax_line_id" />
<field
@ -79,18 +58,22 @@
attrs="{'invisible': [('tax_line_id','=',False)]}"
/>
<field name="tax_ids" widget="many2many_tags" />
<field name="company_id" invisible="1" />
</group>
</group>
<group
name="python_code"
attrs="{'invisible': [('type', '!=', 'computed')]}"
col="1"
string="Compute Formula"
<group name="main-right">
<field name="move_line_type" />
<field name="type" widget="radio" />
<field name="note" />
>
<div class="oe_account_help">
<div
name="python_code_help"
colspan="2"
attrs="{'invisible': [('type', '!=', 'computed')]}"
>
<p
>You can refer to other lines using their sequence number e.g. <i
>L1</i> for line with sequence = 1. Examples:</p>
>You can refer to other lines using their sequence number e.g. <i
>L1</i> for line with sequence = 1. Examples:</p>
<ul>
<li>L1 * 0.2</li>
<li>L2 - L1</li>
@ -99,10 +82,10 @@
</ul>
</div>
<field
name="python_code"
nolabel="1"
attrs="{'required': [('type', '=', 'computed')]}"
/>
name="python_code"
attrs="{'invisible': [('type', '!=', 'computed')], 'required': [('type', '=', 'computed')]}"
/>
</group>
</group>
</sheet>
</form>

View File

@ -9,8 +9,14 @@ from odoo.exceptions import UserError, ValidationError
class AccountMoveTemplateRun(models.TransientModel):
_name = "account.move.template.run"
_description = "Wizard to generate move from template"
_check_company_auto = True
template_id = fields.Many2one("account.move.template", required=True)
template_id = fields.Many2one(
"account.move.template",
required=True,
check_company=True,
domain="[('company_id', '=', company_id)]",
)
company_id = fields.Many2one(
"res.company",
required=True,
@ -66,16 +72,6 @@ Valid dictionary to overwrite template lines:
# Verify and get overwrite dict
overwrite_vals = self._get_overwrite_vals()
amtlro = self.env["account.move.template.line.run"]
if self.company_id != self.template_id.company_id:
raise UserError(
_(
"The selected template (%(template)s) is not in the same company "
"(%(company)s) as the current user (%(user_company)s).",
template=self.template_id.name,
company=self.template_id.company_id.display_name,
user_company=self.company_id.display_name,
)
)
tmpl_lines = self.template_id.line_ids
for tmpl_line in tmpl_lines.filtered(lambda l: l.type == "input"):
vals = self._prepare_wizard_line(tmpl_line)
@ -89,8 +85,9 @@ Valid dictionary to overwrite template lines:
)
if not self.line_ids:
return self.generate_move()
action = self.env.ref("account_move_template.account_move_template_run_action")
result = action.sudo().read()[0]
result = self.env["ir.actions.actions"]._for_xml_id(
"account_move_template.account_move_template_run_action"
)
result.update({"res_id": self.id, "context": self.env.context})
# Overwrite self.line_ids to show overwrite values
@ -98,9 +95,7 @@ Valid dictionary to overwrite template lines:
# Pass context furtner to generate_move function, only readonly field
for key in overwrite_vals.keys():
overwrite_vals[key].pop("amount", None)
context = result.get("context", {}).copy()
context.update({"overwrite": overwrite_vals})
result["context"] = context
result["context"] = dict(result.get("context", {}), overwrite=overwrite_vals)
return result
def _get_valid_keys(self):
@ -187,8 +182,9 @@ Valid dictionary to overwrite template lines:
Command.create(self._prepare_move_line(line, amount))
)
move = self.env["account.move"].create(move_vals)
action = self.env.ref("account.action_move_journal_line")
result = action.sudo().read()[0]
result = self.env["ir.actions.actions"]._for_xml_id(
"account.action_move_journal_line"
)
result.update(
{
"name": _("Entry from template %s") % self.template_id.name,
@ -225,9 +221,8 @@ Valid dictionary to overwrite template lines:
"partner_id": self.partner_id.id or line.partner_id.id,
"date_maturity": date_maturity or self.date,
"tax_repartition_line_id": line.tax_repartition_line_id.id or False,
"analytic_distribution": line.analytic_distribution,
}
if line.analytic_distribution:
values["analytic_distribution"] = line.analytic_distribution
if line.tax_ids:
values["tax_ids"] = [Command.set(line.tax_ids.ids)]
tax_repartition = "refund_tax_id" if line.is_refund else "invoice_tax_id"
@ -264,6 +259,7 @@ Valid dictionary to overwrite template lines:
class AccountMoveTemplateLineRun(models.TransientModel):
_name = "account.move.template.line.run"
_description = "Wizard Lines to generate move from template"
_inherit = "analytic.mixin"
wizard_id = fields.Many2one("account.move.template.run", ondelete="cascade")
company_id = fields.Many2one(related="wizard_id.company_id")
@ -273,7 +269,6 @@ class AccountMoveTemplateLineRun(models.TransientModel):
sequence = fields.Integer(required=True)
name = fields.Char(readonly=True)
account_id = fields.Many2one("account.account", required=True, readonly=True)
analytic_distribution = fields.Json(string="New Analytic Distribution")
tax_ids = fields.Many2many("account.tax", string="Taxes", readonly=True)
tax_line_id = fields.Many2one(
"account.tax", string="Originator Tax", ondelete="restrict", readonly=True

View File

@ -9,7 +9,6 @@
<field
name="template_id"
widget="selection"
domain="[('company_id', '=', company_id)]"
attrs="{'readonly': [('state', '=', 'set_lines')]}"
/>
<field