2019-04-11 15:49:20 +02:00
|
|
|
# Copyright 2015-2019 See manifest
|
|
|
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
|
2020-11-20 04:59:59 +01:00
|
|
|
from ast import literal_eval
|
2019-04-11 15:49:20 +02:00
|
|
|
|
2022-06-03 01:15:16 +02:00
|
|
|
from odoo import Command, _, fields, models
|
2020-11-20 04:59:59 +01:00
|
|
|
from odoo.exceptions import UserError, ValidationError
|
2019-04-11 15:49:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccountMoveTemplateRun(models.TransientModel):
|
|
|
|
_name = "account.move.template.run"
|
|
|
|
_description = "Wizard to generate move from template"
|
2023-06-09 19:17:38 +02:00
|
|
|
_check_company_auto = True
|
2019-04-11 15:49:20 +02:00
|
|
|
|
2023-06-09 19:17:38 +02:00
|
|
|
template_id = fields.Many2one(
|
|
|
|
"account.move.template",
|
|
|
|
required=True,
|
|
|
|
check_company=True,
|
|
|
|
domain="[('company_id', '=', company_id)]",
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
company_id = fields.Many2one(
|
2020-04-24 17:27:55 +02:00
|
|
|
"res.company",
|
|
|
|
required=True,
|
|
|
|
readonly=True,
|
2020-11-20 04:59:59 +01:00
|
|
|
default=lambda self: self.env.company,
|
2020-04-24 17:27:55 +02:00
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
partner_id = fields.Many2one(
|
2020-04-24 17:27:55 +02:00
|
|
|
"res.partner",
|
|
|
|
"Override Partner",
|
|
|
|
domain=["|", ("parent_id", "=", False), ("is_company", "=", True)],
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
date = fields.Date(required=True, default=fields.Date.context_today)
|
2020-04-24 17:27:55 +02:00
|
|
|
journal_id = fields.Many2one("account.journal", string="Journal", readonly=True)
|
|
|
|
ref = fields.Char(string="Reference")
|
2019-04-11 15:49:20 +02:00
|
|
|
line_ids = fields.One2many(
|
2020-04-24 17:27:55 +02:00
|
|
|
"account.move.template.line.run", "wizard_id", string="Lines"
|
|
|
|
)
|
|
|
|
state = fields.Selection(
|
2020-04-24 17:30:12 +02:00
|
|
|
[("select_template", "Select Template"), ("set_lines", "Set Lines")],
|
2020-04-24 17:27:55 +02:00
|
|
|
readonly=True,
|
|
|
|
default="select_template",
|
|
|
|
)
|
2020-11-20 04:59:59 +01:00
|
|
|
overwrite = fields.Text(
|
|
|
|
help="""
|
|
|
|
Valid dictionary to overwrite template lines:
|
|
|
|
{'L1': {'partner_id': 1, 'amount': 100, 'name': 'some label'},
|
|
|
|
'L2': {'partner_id': 2, 'amount': 200, 'name': 'some label 2'}, }
|
|
|
|
"""
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
|
2019-04-16 11:15:58 +02:00
|
|
|
def _prepare_wizard_line(self, tmpl_line):
|
2019-04-11 15:49:20 +02:00
|
|
|
vals = {
|
2020-04-24 17:27:55 +02:00
|
|
|
"wizard_id": self.id,
|
|
|
|
"sequence": tmpl_line.sequence,
|
|
|
|
"name": tmpl_line.name,
|
|
|
|
"amount": 0.0,
|
|
|
|
"account_id": tmpl_line.account_id.id,
|
|
|
|
"partner_id": tmpl_line.partner_id.id or False,
|
|
|
|
"move_line_type": tmpl_line.move_line_type,
|
|
|
|
"tax_line_id": tmpl_line.tax_line_id.id,
|
2022-06-03 01:15:16 +02:00
|
|
|
"tax_ids": [Command.set(tmpl_line.tax_ids.ids)],
|
2023-06-06 12:38:05 +02:00
|
|
|
"analytic_distribution": tmpl_line.analytic_distribution,
|
2020-04-24 17:27:55 +02:00
|
|
|
"note": tmpl_line.note,
|
|
|
|
"payment_term_id": tmpl_line.payment_term_id.id or False,
|
2020-04-24 17:30:12 +02:00
|
|
|
"is_refund": tmpl_line.is_refund,
|
|
|
|
"tax_repartition_line_id": tmpl_line.tax_repartition_line_id.id or False,
|
2020-04-24 17:27:55 +02:00
|
|
|
}
|
2019-04-11 15:49:20 +02:00
|
|
|
return vals
|
|
|
|
|
|
|
|
# STEP 1
|
|
|
|
def load_lines(self):
|
|
|
|
self.ensure_one()
|
2020-11-20 04:59:59 +01:00
|
|
|
# Verify and get overwrite dict
|
|
|
|
overwrite_vals = self._get_overwrite_vals()
|
2020-04-24 17:27:55 +02:00
|
|
|
amtlro = self.env["account.move.template.line.run"]
|
2019-04-16 11:15:58 +02:00
|
|
|
tmpl_lines = self.template_id.line_ids
|
2020-04-24 17:27:55 +02:00
|
|
|
for tmpl_line in tmpl_lines.filtered(lambda l: l.type == "input"):
|
2019-04-16 11:15:58 +02:00
|
|
|
vals = self._prepare_wizard_line(tmpl_line)
|
2019-04-11 15:49:20 +02:00
|
|
|
amtlro.create(vals)
|
2020-04-24 17:27:55 +02:00
|
|
|
self.write(
|
|
|
|
{
|
|
|
|
"journal_id": self.template_id.journal_id.id,
|
|
|
|
"ref": self.template_id.ref,
|
|
|
|
"state": "set_lines",
|
|
|
|
}
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
if not self.line_ids:
|
|
|
|
return self.generate_move()
|
2023-06-09 19:17:38 +02:00
|
|
|
result = self.env["ir.actions.actions"]._for_xml_id(
|
|
|
|
"account_move_template.account_move_template_run_action"
|
|
|
|
)
|
2020-04-24 17:30:12 +02:00
|
|
|
result.update({"res_id": self.id, "context": self.env.context})
|
2020-11-20 04:59:59 +01:00
|
|
|
|
|
|
|
# Overwrite self.line_ids to show overwrite values
|
|
|
|
self._overwrite_line(overwrite_vals)
|
|
|
|
# Pass context furtner to generate_move function, only readonly field
|
|
|
|
for key in overwrite_vals.keys():
|
|
|
|
overwrite_vals[key].pop("amount", None)
|
2023-06-09 19:17:38 +02:00
|
|
|
result["context"] = dict(result.get("context", {}), overwrite=overwrite_vals)
|
2019-04-11 15:49:20 +02:00
|
|
|
return result
|
|
|
|
|
2020-11-20 04:59:59 +01:00
|
|
|
def _get_valid_keys(self):
|
|
|
|
return ["partner_id", "amount", "name", "date_maturity"]
|
|
|
|
|
|
|
|
def _get_overwrite_vals(self):
|
2020-11-26 14:57:05 +01:00
|
|
|
"""valid_dict = {
|
|
|
|
'L1': {'partner_id': 1, 'amount': 10},
|
|
|
|
'L2': {'partner_id': 2, 'amount': 20},
|
|
|
|
}
|
2020-11-20 04:59:59 +01:00
|
|
|
"""
|
|
|
|
self.ensure_one()
|
|
|
|
valid_keys = self._get_valid_keys()
|
|
|
|
overwrite_vals = self.overwrite or "{}"
|
|
|
|
try:
|
|
|
|
overwrite_vals = literal_eval(overwrite_vals)
|
|
|
|
assert isinstance(overwrite_vals, dict)
|
2022-06-03 01:15:16 +02:00
|
|
|
except (SyntaxError, ValueError, AssertionError) as err:
|
|
|
|
raise ValidationError(
|
|
|
|
_("Overwrite value must be a valid python dict")
|
|
|
|
) from err
|
2020-11-20 04:59:59 +01:00
|
|
|
# First level keys must be L1, L2, ...
|
|
|
|
keys = overwrite_vals.keys()
|
|
|
|
if list(filter(lambda x: x[:1] != "L" or not x[1:].isdigit(), keys)):
|
2023-11-03 13:25:04 +01:00
|
|
|
raise ValidationError(_("Keys must be line sequence i.e. L1, L2, ..."))
|
2020-11-20 04:59:59 +01:00
|
|
|
# Second level keys must be a valid keys
|
|
|
|
try:
|
|
|
|
if dict(
|
|
|
|
filter(lambda x: set(overwrite_vals[x].keys()) - set(valid_keys), keys)
|
|
|
|
):
|
|
|
|
raise ValidationError(
|
|
|
|
_("Valid fields to overwrite are %s") % valid_keys
|
|
|
|
)
|
|
|
|
except ValidationError as e:
|
|
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
msg = """
|
|
|
|
valid_dict = {
|
|
|
|
'L1': {'partner_id': 1, 'amount': 10},
|
|
|
|
'L2': {'partner_id': 2, 'amount': 20},
|
|
|
|
}
|
|
|
|
"""
|
2022-06-03 01:15:16 +02:00
|
|
|
raise ValidationError(
|
|
|
|
_(
|
|
|
|
"Invalid dictionary: %(exception)s\n%(msg)s",
|
|
|
|
exception=e,
|
|
|
|
msg=msg,
|
|
|
|
)
|
|
|
|
) from e
|
2020-11-20 04:59:59 +01:00
|
|
|
return overwrite_vals
|
|
|
|
|
|
|
|
def _safe_vals(self, model, vals):
|
|
|
|
obj = self.env[model]
|
|
|
|
copy_vals = vals.copy()
|
|
|
|
invalid_keys = list(
|
|
|
|
set(list(vals.keys())) - set(list(dict(obj._fields).keys()))
|
|
|
|
)
|
|
|
|
for key in invalid_keys:
|
|
|
|
copy_vals.pop(key)
|
|
|
|
return copy_vals
|
|
|
|
|
|
|
|
def _overwrite_line(self, overwrite_vals):
|
|
|
|
self.ensure_one()
|
|
|
|
for line in self.line_ids:
|
|
|
|
vals = overwrite_vals.get("L{}".format(line.sequence), {})
|
|
|
|
safe_vals = self._safe_vals(line._name, vals)
|
|
|
|
line.write(safe_vals)
|
|
|
|
|
2019-04-11 15:49:20 +02:00
|
|
|
# STEP 2
|
|
|
|
def generate_move(self):
|
|
|
|
self.ensure_one()
|
|
|
|
sequence2amount = {}
|
|
|
|
for wizard_line in self.line_ids:
|
|
|
|
sequence2amount[wizard_line.sequence] = wizard_line.amount
|
2020-11-26 14:57:05 +01:00
|
|
|
company_cur = self.company_id.currency_id
|
2020-04-24 17:30:12 +02:00
|
|
|
self.template_id.compute_lines(sequence2amount)
|
2020-11-26 14:57:05 +01:00
|
|
|
if all([company_cur.is_zero(x) for x in sequence2amount.values()]):
|
2019-04-11 15:49:20 +02:00
|
|
|
raise UserError(_("Debit and credit of all lines are null."))
|
|
|
|
move_vals = self._prepare_move()
|
|
|
|
for line in self.template_id.line_ids:
|
|
|
|
amount = sequence2amount[line.sequence]
|
2020-11-26 14:57:05 +01:00
|
|
|
if not company_cur.is_zero(amount):
|
2020-04-24 17:27:55 +02:00
|
|
|
move_vals["line_ids"].append(
|
2022-06-03 01:15:16 +02:00
|
|
|
Command.create(self._prepare_move_line(line, amount))
|
2020-04-24 17:27:55 +02:00
|
|
|
)
|
|
|
|
move = self.env["account.move"].create(move_vals)
|
2023-06-09 19:17:38 +02:00
|
|
|
result = self.env["ir.actions.actions"]._for_xml_id(
|
|
|
|
"account.action_move_journal_line"
|
|
|
|
)
|
2020-04-24 17:27:55 +02:00
|
|
|
result.update(
|
|
|
|
{
|
|
|
|
"name": _("Entry from template %s") % self.template_id.name,
|
|
|
|
"res_id": move.id,
|
|
|
|
"views": False,
|
|
|
|
"view_id": False,
|
|
|
|
"view_mode": "form,tree,kanban",
|
|
|
|
"context": self.env.context,
|
|
|
|
}
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _prepare_move(self):
|
|
|
|
move_vals = {
|
2020-04-24 17:27:55 +02:00
|
|
|
"ref": self.ref,
|
|
|
|
"journal_id": self.journal_id.id,
|
|
|
|
"date": self.date,
|
|
|
|
"company_id": self.company_id.id,
|
|
|
|
"line_ids": [],
|
2019-04-11 15:49:20 +02:00
|
|
|
}
|
|
|
|
return move_vals
|
|
|
|
|
|
|
|
def _prepare_move_line(self, line, amount):
|
2019-04-16 11:15:58 +02:00
|
|
|
date_maturity = False
|
|
|
|
if line.payment_term_id:
|
2020-11-20 04:59:59 +01:00
|
|
|
pterm_list = line.payment_term_id.compute(value=1, date_ref=self.date)
|
2020-11-26 14:57:05 +01:00
|
|
|
date_maturity = max(line[0] for line in pterm_list)
|
2020-04-24 17:27:55 +02:00
|
|
|
debit = line.move_line_type == "dr"
|
2019-04-11 15:49:20 +02:00
|
|
|
values = {
|
2020-04-24 17:27:55 +02:00
|
|
|
"name": line.name,
|
|
|
|
"account_id": line.account_id.id,
|
|
|
|
"credit": not debit and amount or 0.0,
|
|
|
|
"debit": debit and amount or 0.0,
|
|
|
|
"partner_id": self.partner_id.id or line.partner_id.id,
|
|
|
|
"date_maturity": date_maturity or self.date,
|
2020-04-24 17:30:12 +02:00
|
|
|
"tax_repartition_line_id": line.tax_repartition_line_id.id or False,
|
2023-06-09 19:17:38 +02:00
|
|
|
"analytic_distribution": line.analytic_distribution,
|
2019-04-11 15:49:20 +02:00
|
|
|
}
|
|
|
|
if line.tax_ids:
|
2022-06-03 01:15:16 +02:00
|
|
|
values["tax_ids"] = [Command.set(line.tax_ids.ids)]
|
2020-04-24 17:30:12 +02:00
|
|
|
tax_repartition = "refund_tax_id" if line.is_refund else "invoice_tax_id"
|
|
|
|
atrl_ids = self.env["account.tax.repartition.line"].search(
|
|
|
|
[
|
|
|
|
(tax_repartition, "in", line.tax_ids.ids),
|
|
|
|
("repartition_type", "=", "base"),
|
|
|
|
]
|
|
|
|
)
|
2022-06-03 01:15:16 +02:00
|
|
|
values["tax_tag_ids"] = [Command.set(atrl_ids.mapped("tag_ids").ids)]
|
2020-04-24 17:30:12 +02:00
|
|
|
if line.tax_repartition_line_id:
|
2022-06-03 01:15:16 +02:00
|
|
|
values["tax_tag_ids"] = [
|
|
|
|
Command.set(line.tax_repartition_line_id.tag_ids.ids)
|
|
|
|
]
|
2020-11-20 04:59:59 +01:00
|
|
|
# With overwrite options
|
|
|
|
overwrite = self._context.get("overwrite", {})
|
|
|
|
move_line_vals = overwrite.get("L{}".format(line.sequence), {})
|
|
|
|
values.update(move_line_vals)
|
|
|
|
# Use optional account, when amount is negative
|
|
|
|
self._update_account_on_negative(line, values)
|
2019-04-11 15:49:20 +02:00
|
|
|
return values
|
|
|
|
|
2020-11-20 04:59:59 +01:00
|
|
|
def _update_account_on_negative(self, line, vals):
|
|
|
|
if not line.opt_account_id:
|
|
|
|
return
|
|
|
|
for key in ["debit", "credit"]:
|
|
|
|
if vals[key] < 0:
|
|
|
|
ikey = (key == "debit") and "credit" or "debit"
|
|
|
|
vals["account_id"] = line.opt_account_id.id
|
|
|
|
vals[ikey] = abs(vals[key])
|
|
|
|
vals[key] = 0
|
|
|
|
|
2019-04-11 15:49:20 +02:00
|
|
|
|
|
|
|
class AccountMoveTemplateLineRun(models.TransientModel):
|
|
|
|
_name = "account.move.template.line.run"
|
2020-04-24 17:27:55 +02:00
|
|
|
_description = "Wizard Lines to generate move from template"
|
2023-06-09 19:17:38 +02:00
|
|
|
_inherit = "analytic.mixin"
|
2019-04-11 15:49:20 +02:00
|
|
|
|
2020-04-24 17:27:55 +02:00
|
|
|
wizard_id = fields.Many2one("account.move.template.run", ondelete="cascade")
|
|
|
|
company_id = fields.Many2one(related="wizard_id.company_id")
|
2019-04-11 15:49:20 +02:00
|
|
|
company_currency_id = fields.Many2one(
|
2020-04-24 17:27:55 +02:00
|
|
|
related="wizard_id.company_id.currency_id", string="Company Currency"
|
|
|
|
)
|
2022-06-03 01:15:16 +02:00
|
|
|
sequence = fields.Integer(required=True)
|
|
|
|
name = fields.Char(readonly=True)
|
2020-04-24 17:27:55 +02:00
|
|
|
account_id = fields.Many2one("account.account", required=True, readonly=True)
|
|
|
|
tax_ids = fields.Many2many("account.tax", string="Taxes", readonly=True)
|
2019-04-11 15:49:20 +02:00
|
|
|
tax_line_id = fields.Many2one(
|
2020-04-24 17:27:55 +02:00
|
|
|
"account.tax", string="Originator Tax", ondelete="restrict", readonly=True
|
|
|
|
)
|
|
|
|
partner_id = fields.Many2one("res.partner", readonly=True, string="Partner")
|
2019-04-16 11:15:58 +02:00
|
|
|
payment_term_id = fields.Many2one(
|
2020-04-24 17:27:55 +02:00
|
|
|
"account.payment.term", string="Payment Terms", readonly=True
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
move_line_type = fields.Selection(
|
2020-04-24 17:27:55 +02:00
|
|
|
[("cr", "Credit"), ("dr", "Debit")],
|
|
|
|
required=True,
|
|
|
|
readonly=True,
|
|
|
|
string="Direction",
|
|
|
|
)
|
2022-06-03 01:15:16 +02:00
|
|
|
amount = fields.Monetary(required=True, currency_field="company_currency_id")
|
2019-04-11 15:49:20 +02:00
|
|
|
note = fields.Char(readonly=True)
|
2020-11-26 14:57:05 +01:00
|
|
|
is_refund = fields.Boolean(string="Is a refund?", readonly=True)
|
2020-04-24 17:30:12 +02:00
|
|
|
tax_repartition_line_id = fields.Many2one(
|
2020-11-26 14:57:05 +01:00
|
|
|
"account.tax.repartition.line",
|
|
|
|
string="Tax Repartition Line",
|
|
|
|
readonly=True,
|
2020-04-24 17:30:12 +02:00
|
|
|
)
|