2020-09-07 14:31:18 +02:00
|
|
|
# Copyright 2016-2020 Onestein (<https://www.onestein.eu>)
|
2018-10-22 12:27:50 +02:00
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
|
|
|
|
|
|
|
from odoo import _, api, fields, models
|
2020-09-07 14:31:18 +02:00
|
|
|
from odoo.exceptions import UserError, ValidationError
|
2018-10-22 12:27:50 +02:00
|
|
|
|
|
|
|
|
2020-09-07 14:31:18 +02:00
|
|
|
class AccountMoveLine(models.Model):
|
|
|
|
_inherit = "account.move.line"
|
2018-10-22 12:27:50 +02:00
|
|
|
|
2020-09-07 14:17:04 +02:00
|
|
|
spread_id = fields.Many2one("account.spread", string="Spread Board", copy=False)
|
|
|
|
spread_check = fields.Selection(
|
|
|
|
[
|
|
|
|
("linked", "Linked"),
|
|
|
|
("unlinked", "Unlinked"),
|
|
|
|
("unavailable", "Unavailable"),
|
|
|
|
],
|
|
|
|
compute="_compute_spread_check",
|
|
|
|
)
|
2018-10-22 12:27:50 +02:00
|
|
|
|
2020-09-07 14:31:18 +02:00
|
|
|
@api.depends("spread_id", "move_id.state")
|
2018-10-22 12:27:50 +02:00
|
|
|
def _compute_spread_check(self):
|
|
|
|
for line in self:
|
|
|
|
if line.spread_id:
|
2020-09-07 14:17:04 +02:00
|
|
|
line.spread_check = "linked"
|
2020-09-07 14:31:18 +02:00
|
|
|
elif line.move_id.state == "draft":
|
2020-09-07 14:17:04 +02:00
|
|
|
line.spread_check = "unlinked"
|
2018-10-22 12:27:50 +02:00
|
|
|
else:
|
2020-09-07 14:17:04 +02:00
|
|
|
line.spread_check = "unavailable"
|
2018-10-22 12:27:50 +02:00
|
|
|
|
|
|
|
def spread_details(self):
|
|
|
|
"""Button on the invoice lines tree view of the invoice
|
|
|
|
form to show the spread form view."""
|
|
|
|
if not self:
|
|
|
|
# In case the widget clicked before the creation of the line
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.spread_id:
|
|
|
|
return {
|
2020-09-07 14:17:04 +02:00
|
|
|
"name": _("Spread Details"),
|
|
|
|
"view_mode": "form",
|
|
|
|
"res_model": "account.spread",
|
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"target": "current",
|
|
|
|
"readonly": False,
|
|
|
|
"res_id": self.spread_id.id,
|
2018-10-22 12:27:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# In case no spread board is linked to the invoice line
|
|
|
|
# open the wizard to link them
|
|
|
|
ctx = dict(
|
|
|
|
self.env.context,
|
|
|
|
default_invoice_line_id=self.id,
|
2020-09-07 14:31:18 +02:00
|
|
|
default_company_id=self.move_id.company_id.id,
|
|
|
|
allow_spread_planning=self.move_id.company_id.allow_spread_planning,
|
2018-10-22 12:27:50 +02:00
|
|
|
)
|
|
|
|
return {
|
2020-09-07 14:17:04 +02:00
|
|
|
"name": _("Link Invoice Line with Spread Board"),
|
|
|
|
"view_mode": "form",
|
|
|
|
"res_model": "account.spread.invoice.line.link.wizard",
|
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"target": "new",
|
|
|
|
"context": ctx,
|
2018-10-22 12:27:50 +02:00
|
|
|
}
|
2020-05-17 16:37:40 +02:00
|
|
|
|
2020-09-07 14:31:18 +02:00
|
|
|
@api.constrains("spread_id", "account_id")
|
|
|
|
def _check_spread_account_balance_sheet(self):
|
|
|
|
for line in self:
|
|
|
|
if not line.spread_id:
|
|
|
|
pass
|
2021-10-01 11:50:58 +02:00
|
|
|
elif line.move_id.move_type in ("out_invoice", "in_refund"):
|
2020-09-07 14:31:18 +02:00
|
|
|
if line.account_id != line.spread_id.debit_account_id:
|
|
|
|
raise ValidationError(
|
|
|
|
_(
|
|
|
|
"The account of the invoice line does not correspond "
|
|
|
|
"to the Balance Sheet (debit account) of the spread"
|
|
|
|
)
|
|
|
|
)
|
2021-10-01 11:50:58 +02:00
|
|
|
elif line.move_id.move_type in ("in_invoice", "out_refund"):
|
2020-09-07 14:31:18 +02:00
|
|
|
if line.account_id != line.spread_id.credit_account_id:
|
|
|
|
raise ValidationError(
|
|
|
|
_(
|
|
|
|
"The account of the invoice line does not correspond "
|
|
|
|
"to the Balance Sheet (credit account) of the spread"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def write(self, vals):
|
|
|
|
if vals.get("spread_id"):
|
|
|
|
spread = self.env["account.spread"].browse(vals.get("spread_id"))
|
|
|
|
if spread.invoice_type in ["out_invoice", "in_refund"]:
|
|
|
|
vals["account_id"] = spread.debit_account_id.id
|
|
|
|
else:
|
|
|
|
vals["account_id"] = spread.credit_account_id.id
|
|
|
|
return super().write(vals)
|
|
|
|
|
|
|
|
def _check_spread_reconcile_validity(self):
|
|
|
|
# Improve error messages of standard Odoo
|
|
|
|
reconciled_lines = self.filtered(lambda l: l.reconciled)
|
2023-05-29 05:38:12 +02:00
|
|
|
msg_line = _(
|
|
|
|
"Move line: %(line_id)s (%(line_name)s), account code: %(account_code)s\n"
|
|
|
|
)
|
2020-09-07 14:31:18 +02:00
|
|
|
if reconciled_lines:
|
|
|
|
msg = _("Cannot reconcile entries that are already reconciled:\n")
|
|
|
|
for line in reconciled_lines:
|
|
|
|
msg += msg_line % (line.id, line.name, line.account_id.code)
|
|
|
|
raise ValidationError(msg)
|
|
|
|
if len(self.mapped("account_id").ids) > 1:
|
|
|
|
msg = _("Some entries are not from the same account:\n")
|
|
|
|
for line in self:
|
|
|
|
msg += msg_line % (line.id, line.name, line.account_id.code)
|
|
|
|
raise ValidationError(msg)
|
|
|
|
|
2020-05-17 16:37:40 +02:00
|
|
|
def create_auto_spread(self):
|
2021-10-01 11:50:58 +02:00
|
|
|
"""Create auto spread table for each invoice line, when needed"""
|
2020-05-17 16:37:40 +02:00
|
|
|
|
|
|
|
def _filter_line(aline, iline):
|
2021-10-01 11:50:58 +02:00
|
|
|
"""Find matching template auto line with invoice line"""
|
2020-05-17 16:37:40 +02:00
|
|
|
if aline.product_id and iline.product_id != aline.product_id:
|
|
|
|
return False
|
|
|
|
if aline.account_id and iline.account_id != aline.account_id:
|
|
|
|
return False
|
2020-09-07 14:17:04 +02:00
|
|
|
if (
|
|
|
|
aline.analytic_account_id
|
|
|
|
and iline.account_analytic_id != aline.analytic_account_id
|
|
|
|
):
|
2020-05-17 16:37:40 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
for line in self:
|
2020-09-07 14:17:04 +02:00
|
|
|
if line.spread_check == "linked":
|
2020-05-17 16:37:40 +02:00
|
|
|
continue
|
|
|
|
spread_type = (
|
2020-09-07 14:17:04 +02:00
|
|
|
"sale"
|
2021-10-01 11:50:58 +02:00
|
|
|
if line.move_id.move_type in ["out_invoice", "out_refund"]
|
2020-09-07 14:17:04 +02:00
|
|
|
else "purchase"
|
|
|
|
)
|
|
|
|
spread_auto = self.env["account.spread.template.auto"].search(
|
|
|
|
[
|
|
|
|
("template_id.auto_spread", "=", True),
|
|
|
|
("template_id.spread_type", "=", spread_type),
|
|
|
|
]
|
|
|
|
)
|
2020-05-17 16:37:40 +02:00
|
|
|
matched = spread_auto.filtered(lambda a, i=line: _filter_line(a, i))
|
2020-09-07 14:17:04 +02:00
|
|
|
template = matched.mapped("template_id")
|
2020-05-17 16:37:40 +02:00
|
|
|
if not template:
|
|
|
|
continue
|
|
|
|
elif len(template) > 1:
|
|
|
|
raise UserError(
|
2020-09-07 14:17:04 +02:00
|
|
|
_(
|
2023-05-29 05:38:12 +02:00
|
|
|
"Too many auto spread templates ({}) matched with the "
|
|
|
|
"invoice line, {}"
|
|
|
|
).format(len(template), line.display_name)
|
2020-09-07 14:17:04 +02:00
|
|
|
)
|
2020-05-17 16:37:40 +02:00
|
|
|
# Found auto spread template for this invoice line, create it
|
2020-09-07 14:17:04 +02:00
|
|
|
wizard = self.env["account.spread.invoice.line.link.wizard"].new(
|
|
|
|
{
|
|
|
|
"invoice_line_id": line.id,
|
|
|
|
"company_id": line.company_id.id,
|
|
|
|
"spread_action_type": "template",
|
|
|
|
"template_id": template.id,
|
|
|
|
}
|
|
|
|
)
|
2020-05-17 16:37:40 +02:00
|
|
|
wizard.confirm()
|