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
|
|
|
|
2020-04-24 17:27:55 +02:00
|
|
|
from odoo import _, 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"
|
|
|
|
|
2020-04-24 17:27:55 +02:00
|
|
|
template_id = fields.Many2one("account.move.template", required=True)
|
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,
|
2020-04-24 17:30:12 +02:00
|
|
|
"tax_ids": [(6, 0, tmpl_line.tax_ids.ids)],
|
2020-04-24 17:27:55 +02:00
|
|
|
"analytic_account_id": tmpl_line.analytic_account_id.id,
|
|
|
|
"analytic_tag_ids": [(6, 0, tmpl_line.analytic_tag_ids.ids)],
|
|
|
|
"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-11 15:49:20 +02:00
|
|
|
if self.company_id != self.template_id.company_id:
|
2020-04-24 17:27:55 +02:00
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"The selected template (%s) is not in the same company (%s) "
|
|
|
|
"as the current user (%s)."
|
|
|
|
)
|
|
|
|
% (
|
2019-04-11 15:49:20 +02:00
|
|
|
self.template_id.name,
|
|
|
|
self.template_id.company_id.display_name,
|
2020-04-24 17:27:55 +02:00
|
|
|
self.company_id.display_name,
|
|
|
|
)
|
|
|
|
)
|
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()
|
2020-04-24 17:27:55 +02:00
|
|
|
action = self.env.ref("account_move_template.account_move_template_run_action")
|
2021-12-16 07:52:51 +01:00
|
|
|
result = action.sudo().read()[0]
|
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)
|
|
|
|
context = result.get("context", {}).copy()
|
|
|
|
context.update({"overwrite": overwrite_vals})
|
|
|
|
result["context"] = context
|
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)
|
|
|
|
except (SyntaxError, ValueError, AssertionError):
|
|
|
|
raise ValidationError(_("Overwrite value must be a valid python dict"))
|
|
|
|
# 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)):
|
|
|
|
raise ValidationError(_("Keys must be line sequence, i..e, L1, L2, ..."))
|
|
|
|
# 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},
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
raise ValidationError(_("Invalid dictionary: {}\n{}".format(e, msg)))
|
|
|
|
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(
|
|
|
|
(0, 0, self._prepare_move_line(line, amount))
|
|
|
|
)
|
|
|
|
move = self.env["account.move"].create(move_vals)
|
|
|
|
action = self.env.ref("account.action_move_journal_line")
|
2021-12-16 07:52:51 +01:00
|
|
|
result = action.sudo().read()[0]
|
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,
|
|
|
|
"analytic_account_id": line.analytic_account_id.id,
|
|
|
|
"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,
|
2019-04-11 15:49:20 +02:00
|
|
|
}
|
|
|
|
if line.analytic_tag_ids:
|
2020-04-24 17:27:55 +02:00
|
|
|
values["analytic_tag_ids"] = [(6, 0, line.analytic_tag_ids.ids)]
|
2019-04-11 15:49:20 +02:00
|
|
|
if line.tax_ids:
|
2020-04-24 17:27:55 +02:00
|
|
|
values["tax_ids"] = [(6, 0, 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"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
values["tag_ids"] = [(6, 0, atrl_ids.mapped("tag_ids").ids)]
|
|
|
|
if line.tax_repartition_line_id:
|
|
|
|
values["tag_ids"] = [(6, 0, 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"
|
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"
|
|
|
|
)
|
|
|
|
sequence = fields.Integer("Sequence", required=True)
|
|
|
|
name = fields.Char("Name", readonly=True)
|
|
|
|
account_id = fields.Many2one("account.account", required=True, readonly=True)
|
|
|
|
analytic_account_id = fields.Many2one("account.analytic.account", readonly=True)
|
2019-04-11 15:49:20 +02:00
|
|
|
analytic_tag_ids = fields.Many2many(
|
2020-04-24 17:27:55 +02:00
|
|
|
"account.analytic.tag", string="Analytic Tags", 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",
|
|
|
|
)
|
2019-04-11 15:49:20 +02:00
|
|
|
amount = fields.Monetary(
|
2020-04-24 17:27:55 +02:00
|
|
|
"Amount", 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
|
|
|
)
|