2017-10-13 17:27:59 +02:00
|
|
|
# Copyright 2015 Pedro M. Baeza
|
|
|
|
# Copyright 2017 Tecnativa - Vicent Cubells
|
2015-08-07 18:05:03 +02:00
|
|
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
2023-02-22 23:53:29 +01:00
|
|
|
from odoo import _, api, fields, models
|
|
|
|
from odoo.exceptions import UserError
|
2015-08-07 18:05:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AccountMoveMakeNetting(models.TransientModel):
|
|
|
|
_name = "account.move.make.netting"
|
2020-03-19 09:13:15 +01:00
|
|
|
_description = "Wizard to generate account moves for netting"
|
2023-02-22 23:53:29 +01:00
|
|
|
_check_company_auto = True
|
2015-08-07 18:05:03 +02:00
|
|
|
|
2023-02-22 23:53:29 +01:00
|
|
|
company_id = fields.Many2one("res.company", required=True)
|
2017-10-13 17:27:59 +02:00
|
|
|
journal_id = fields.Many2one(
|
|
|
|
comodel_name="account.journal",
|
|
|
|
required=True,
|
2023-02-22 23:53:29 +01:00
|
|
|
domain="[('type', '=', 'general'), ('company_id', '=', company_id)]",
|
|
|
|
check_company=True,
|
2017-10-13 17:27:59 +02:00
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
move_line_ids = fields.Many2many(
|
|
|
|
comodel_name="account.move.line",
|
|
|
|
check_company=True,
|
|
|
|
)
|
|
|
|
partner_id = fields.Many2one("res.partner", readonly=True)
|
|
|
|
company_currency_id = fields.Many2one(related="company_id.currency_id")
|
|
|
|
balance = fields.Monetary(readonly=True, currency_field="company_currency_id")
|
2015-08-07 18:05:03 +02:00
|
|
|
balance_type = fields.Selection(
|
2020-11-26 15:23:56 +01:00
|
|
|
selection=[("pay", "To pay"), ("receive", "To receive")],
|
|
|
|
readonly=True,
|
2017-10-13 17:27:59 +02:00
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
|
|
|
|
@api.model
|
2019-03-19 23:12:50 +01:00
|
|
|
def default_get(self, fields_list):
|
2020-03-19 09:13:15 +01:00
|
|
|
if len(self.env.context.get("active_ids", [])) < 2:
|
2023-02-22 23:53:29 +01:00
|
|
|
raise UserError(_("You should compensate at least 2 journal items."))
|
2020-03-19 09:13:15 +01:00
|
|
|
move_lines = self.env["account.move.line"].browse(
|
|
|
|
self.env.context["active_ids"]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
partners = self.env["res.partner"]
|
|
|
|
for line in move_lines:
|
|
|
|
if line.parent_state != "posted":
|
|
|
|
raise UserError(_("Line '%s' is not posted.") % line.display_name)
|
|
|
|
if line.account_id.account_type not in (
|
|
|
|
"liability_payable",
|
|
|
|
"asset_receivable",
|
|
|
|
):
|
|
|
|
raise UserError(
|
|
|
|
_(
|
|
|
|
"Line '%(line)s' has account '%(account)s' which is not "
|
|
|
|
"a payable nor a receivable account."
|
|
|
|
)
|
|
|
|
% {
|
|
|
|
"line": line.display_name,
|
|
|
|
"account": line.account_id.display_name,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if line.reconciled:
|
|
|
|
raise UserError(
|
|
|
|
_("Line '%s' is already reconciled.") % line.display_name
|
|
|
|
)
|
|
|
|
if not line.partner_id:
|
|
|
|
raise UserError(
|
|
|
|
_("Line '%s' doesn't have a partner.") % line.display_name
|
|
|
|
)
|
|
|
|
partners |= line.partner_id
|
|
|
|
|
|
|
|
if len(move_lines.account_id) == 1:
|
|
|
|
raise UserError(
|
2020-03-19 09:13:15 +01:00
|
|
|
_(
|
|
|
|
"The 'Compensate' function is intended to balance "
|
2023-02-22 23:53:29 +01:00
|
|
|
"operations on different accounts for the same partner. "
|
2020-03-19 09:13:15 +01:00
|
|
|
"In this case all selected entries belong to the same "
|
2023-02-22 23:53:29 +01:00
|
|
|
"account '%s'. Use the 'Reconcile' function instead."
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
% move_lines.account_id.display_name
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
if len(partners) != 1:
|
|
|
|
raise UserError(
|
2020-03-19 09:13:15 +01:00
|
|
|
_(
|
2023-02-22 23:53:29 +01:00
|
|
|
"The selected journal items have different partners: %s. "
|
|
|
|
"The partner must be the same for all the selected journal items."
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
% ", ".join([p.display_name for p in partners])
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2020-03-19 09:13:16 +01:00
|
|
|
res = super().default_get(fields_list)
|
2023-02-22 23:53:29 +01:00
|
|
|
company = self.env.company
|
|
|
|
ccur = company.currency_id
|
2020-03-19 09:13:15 +01:00
|
|
|
debit_move_lines_debit = move_lines.filtered("debit")
|
|
|
|
credit_move_lines_debit = move_lines.filtered("credit")
|
2023-02-22 23:53:29 +01:00
|
|
|
balance = ccur.round(
|
|
|
|
abs(sum(debit_move_lines_debit.mapped("amount_residual")))
|
|
|
|
- abs(sum(credit_move_lines_debit.mapped("amount_residual")))
|
|
|
|
)
|
|
|
|
res.update(
|
|
|
|
{
|
|
|
|
"balance": abs(balance),
|
|
|
|
"balance_type": "pay"
|
|
|
|
if ccur.compare_amounts(balance, 0) < 0
|
|
|
|
else "receive",
|
|
|
|
"company_id": company.id,
|
|
|
|
"move_line_ids": move_lines.ids,
|
|
|
|
"partner_id": partners.id,
|
|
|
|
}
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
return res
|
|
|
|
|
2023-02-22 23:53:29 +01:00
|
|
|
def _prepare_account_move(self):
|
2015-08-07 18:05:03 +02:00
|
|
|
# Group amounts by account
|
2018-01-25 14:11:48 +01:00
|
|
|
account_groups = self.move_line_ids.read_group(
|
2020-03-19 09:13:15 +01:00
|
|
|
[("id", "in", self.move_line_ids.ids)],
|
|
|
|
["account_id", "amount_residual"],
|
|
|
|
["account_id"],
|
2017-10-13 17:27:59 +02:00
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
debtors = []
|
|
|
|
creditors = []
|
2023-02-22 23:53:29 +01:00
|
|
|
total_debtors = 0.0
|
|
|
|
total_creditors = 0.0
|
|
|
|
ccur = self.company_id.currency_id
|
2015-08-07 18:05:03 +02:00
|
|
|
for account_group in account_groups:
|
2020-03-19 09:13:15 +01:00
|
|
|
balance = account_group["amount_residual"]
|
2015-08-07 18:05:03 +02:00
|
|
|
group_vals = {
|
2020-03-19 09:13:15 +01:00
|
|
|
"account_id": account_group["account_id"][0],
|
|
|
|
"balance": abs(balance),
|
2015-08-07 18:05:03 +02:00
|
|
|
}
|
2023-02-22 23:53:29 +01:00
|
|
|
if ccur.compare_amounts(balance, 0) > 0:
|
2015-08-07 18:05:03 +02:00
|
|
|
debtors.append(group_vals)
|
|
|
|
total_debtors += balance
|
|
|
|
else:
|
|
|
|
creditors.append(group_vals)
|
|
|
|
total_creditors += abs(balance)
|
2023-02-22 23:53:29 +01:00
|
|
|
# Compute move lines
|
2015-08-07 18:05:03 +02:00
|
|
|
netting_amount = min(total_creditors, total_debtors)
|
2020-03-19 09:13:15 +01:00
|
|
|
field_map = {1: "debit", 0: "credit"}
|
2017-10-13 17:27:59 +02:00
|
|
|
move_lines = []
|
2015-08-07 18:05:03 +02:00
|
|
|
for i, group in enumerate([debtors, creditors]):
|
|
|
|
available_amount = netting_amount
|
|
|
|
for account_group in group:
|
|
|
|
move_line_vals = {
|
2023-02-22 23:53:29 +01:00
|
|
|
field_map[i]: min(available_amount, account_group["balance"]),
|
|
|
|
"partner_id": self.partner_id.id,
|
2020-03-19 09:13:15 +01:00
|
|
|
"account_id": account_group["account_id"],
|
2015-08-07 18:05:03 +02:00
|
|
|
}
|
2017-10-13 17:27:59 +02:00
|
|
|
move_lines.append((0, 0, move_line_vals))
|
2020-03-19 09:13:15 +01:00
|
|
|
available_amount -= account_group["balance"]
|
2023-02-22 23:53:29 +01:00
|
|
|
if ccur.compare_amounts(available_amount, 0) <= 0:
|
2015-08-07 18:05:03 +02:00
|
|
|
break
|
2023-02-22 23:53:29 +01:00
|
|
|
vals = {
|
|
|
|
"ref": _("AR/AP netting"),
|
|
|
|
"journal_id": self.journal_id.id,
|
|
|
|
"company_id": self.company_id.id,
|
|
|
|
"line_ids": move_lines,
|
|
|
|
}
|
|
|
|
return vals
|
|
|
|
|
|
|
|
def button_compensate(self):
|
|
|
|
self.ensure_one()
|
|
|
|
# Create account move
|
|
|
|
move = self.env["account.move"].create(self._prepare_account_move())
|
|
|
|
move.action_post()
|
2015-08-07 18:05:03 +02:00
|
|
|
# Make reconciliation
|
2017-10-13 17:27:59 +02:00
|
|
|
for move_line in move.line_ids:
|
|
|
|
to_reconcile = move_line + self.move_line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == move_line.account_id
|
|
|
|
)
|
2017-10-13 17:27:59 +02:00
|
|
|
to_reconcile.reconcile()
|
2015-08-07 18:05:03 +02:00
|
|
|
# Open created move
|
2023-02-22 23:53:29 +01:00
|
|
|
action = self.env["ir.actions.actions"]._for_xml_id(
|
2022-10-11 13:54:03 +02:00
|
|
|
"account.action_move_journal_line"
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
action.update(
|
|
|
|
{
|
|
|
|
"view_mode": "form",
|
|
|
|
"views": False,
|
|
|
|
"view_id": False,
|
|
|
|
"res_id": move.id,
|
|
|
|
}
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
return action
|