Skip to content

Commit

Permalink
imp: added feature of lines con account.payment.register
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisOForgeFlow committed Feb 6, 2023
1 parent d28447a commit c5f5f31
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 131 deletions.
1 change: 1 addition & 0 deletions account_payment_line/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import models
from . import wizard
from .hooks import post_load_hook
1 change: 1 addition & 0 deletions account_payment_line/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"data": [
"security/ir.model.access.csv",
"views/account_payment_views.xml",
"wizard/account_payment_register_view.xml",
],
"maintainers": ["ChrisOForgeFlow"],
"installable": True,
Expand Down
1 change: 1 addition & 0 deletions account_payment_line/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import counterpart_line
from . import account_payment
from . import account_move
145 changes: 14 additions & 131 deletions account_payment_line/models/account_payment.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Copyright 2022 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo import fields, models
from odoo.tools import float_is_zero

dict_payment_type = dict(
inbound=["out_invoice", "out_refund", "out_receipt"],
outbound=["in_invoice", "in_refund", "in_receipt"],
)


class AccountPayment(models.Model):
_inherit = "account.payment"
Expand Down Expand Up @@ -180,139 +174,28 @@ def action_draft(self):
return res


class AccountPaymentCounterLines(models.Model):
class AccountPaymentCounterLine(models.Model):
_name = "account.payment.counterpart.line"
_inherit = "account.payment.counterpart.line.abstract"
_description = "Counterpart line payment"

payment_id = fields.Many2one(
"account.payment", string="Payment", required=False, ondelete="cascade"
)
company_id = fields.Many2one(related="payment_id.company_id")
name = fields.Char(string="Description", required=True, default="/")
account_id = fields.Many2one(
"account.account",
string="Account",
required=True,
ondelete="restrict",
check_company=True,
)
analytic_account_id = fields.Many2one(
comodel_name="account.analytic.account",
string="Analytic Account",
ondelete="restrict",
check_company=True,
)
analytic_tag_ids = fields.Many2many(
"account.analytic.tag",
comodel_name="account.analytic.tag",
relation="counterpart_line_analytic_tag_rel",
column1="line_id",
column2="tag_id",
string="Analytic Tags",
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
check_company=True,
)
currency_id = fields.Many2one(
comodel_name="res.currency", string="Currency", related="payment_id.currency_id"
)
amount = fields.Monetary(string="Amount", required=True)
amount_currency = fields.Monetary(
string="Amount in Company Currency", compute="_compute_amounts"
)
aml_amount_residual = fields.Monetary(
string="Amount Residual",
compute="_compute_amounts",
)
residual_after_payment = fields.Monetary(
compute="_compute_amounts",
)
aml_amount_residual_currency = fields.Monetary(
string="Amount Residual Currency",
compute="_compute_amounts",
)
residual_after_payment_currency = fields.Monetary(
compute="_compute_amounts",
)

@api.depends(
"aml_id.amount_residual", "amount", "payment_id.currency_id", "payment_id.date"
)
def _compute_amounts(self):
for rec in self:
rec.amount_currency = rec.payment_id.currency_id._convert(
rec.amount,
rec.payment_id.company_id.currency_id,
rec.payment_id.company_id,
date=rec.payment_id.date,
)
rec.aml_amount_residual = rec.aml_id.amount_residual
rec.residual_after_payment = max(
abs(rec.aml_id.amount_residual) - rec.amount_currency, 0
)
rec.aml_amount_residual_currency = rec.aml_id.amount_residual_currency
rec.residual_after_payment_currency = max(
abs(rec.aml_id.amount_residual_currency) - rec.amount_currency, 0
)

partner_id = fields.Many2one("res.partner", string="Partner", ondelete="restrict")
commercial_partner_id = fields.Many2one(related="partner_id.commercial_partner_id")
move_id = fields.Many2one(
"account.move", string="Journal Entry", ondelete="set null"
)
move_ids = fields.One2many(
"account.move.line",
"payment_line_id",
string="Journal Entries Created",
)
aml_id = fields.Many2one(
"account.move.line", string="Journal Item to Reconcile", ondelete="set null"
)
aml_date_maturity = fields.Date(
string="Date Maturity", required=False, related="aml_id.date_maturity"
)

@api.onchange("move_id", "aml_id")
def _onchange_move_id(self):
aml_model = self.env["account.move.line"]
for rec in self:
type_move = dict_payment_type.get(rec.payment_id.payment_type, [])
if rec.move_id and not rec.aml_id:
domain = [
("move_id", "=", rec.move_id.id),
("amount_residual", "!=", 0.0),
]
lines_ordered = aml_model.search(
domain, order="date_maturity ASC", limit=1
)
if lines_ordered:
rec.aml_id = lines_ordered.id
if rec.aml_id:
rec.move_id = rec.aml_id.move_id.id
rec.account_id = rec.aml_id.account_id.id
rec.amount = abs(rec.aml_id.amount_residual)
rec.partner_id = rec.aml_id.partner_id.id
if rec.move_id.move_type == "entry":
if rec.payment_id.partner_type == "supplier":
if rec.payment_id.payment_type == "outbound":
rec.amount = -rec.aml_id.amount_residual
else:
rec.amount = rec.aml_id.amount_residual
else:
if rec.payment_id.payment_type == "outbound":
rec.amount = -rec.aml_id.amount_residual
else:
rec.amount = rec.aml_id.amount_residual
else:
if (
type_move
and rec.move_id.move_type not in type_move
and rec.amount
):
rec.amount *= -1

@api.constrains("amount", "aml_amount_residual")
def constrains_amount_residual(self):
for rec in self:
if rec.aml_id and 0 < rec.aml_amount_residual < rec.amount:
raise ValidationError(
_(
"the amount exceeds the residual amount, please check the invoice %s"
),
(rec.aml_id.move_id.name or rec.aml_id.name),
)
def _get_onchange_fields(self):
return (
"aml_id.amount_residual",
"amount",
"payment_id.currency_id",
"payment_id.date",
)
154 changes: 154 additions & 0 deletions account_payment_line/models/counterpart_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Copyright 2022 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

dict_payment_type = dict(
inbound=["out_invoice", "out_refund", "out_receipt"],
outbound=["in_invoice", "in_refund", "in_receipt"],
)


class AccountPaymentCounterLinesAbstract(models.AbstractModel):
_name = "account.payment.counterpart.line.abstract"
_description = "Counterpart line payment Abstract"

company_id = fields.Many2one(
comodel_name="res.company", compute="_compute_company_fields"
)
name = fields.Char(string="Description", required=True, default="/")
account_id = fields.Many2one(
"account.account",
string="Account",
required=True,
ondelete="restrict",
check_company=True,
)
analytic_account_id = fields.Many2one(
comodel_name="account.analytic.account",
string="Analytic Account",
ondelete="restrict",
check_company=True,
)
currency_id = fields.Many2one(
comodel_name="res.currency",
string="Currency",
compute="_compute_company_fields",
)

def _compute_company_fields(self):
for rec in self:
rec.company_id = self.env.company.id
rec.currency_id = self.env.company.currency_id.id

amount = fields.Monetary(string="Amount", required=True)
amount_currency = fields.Monetary(
string="Amount in Company Currency", compute="_compute_amounts"
)
aml_amount_residual = fields.Monetary(
string="Amount Residual",
compute="_compute_amounts",
)
residual_after_payment = fields.Monetary(
compute="_compute_amounts",
)
aml_amount_residual_currency = fields.Monetary(
string="Amount Residual Currency",
compute="_compute_amounts",
)
residual_after_payment_currency = fields.Monetary(
compute="_compute_amounts",
)

def _get_onchange_fields(self):
return "aml_id.amount_residual", "amount"

@api.depends(lambda x: x._get_onchange_fields())
def _compute_amounts(self):
for rec in self:
payment_date = (
hasattr(rec.payment_id, "payment_date")
and rec.payment_id.payment_date
or rec.payment_id.date
)
rec.amount_currency = rec.payment_id.currency_id._convert(
rec.amount,
rec.payment_id.company_id.currency_id,
rec.payment_id.company_id,
date=payment_date,
)
rec.aml_amount_residual = rec.aml_id.amount_residual
rec.residual_after_payment = max(
abs(rec.aml_id.amount_residual) - rec.amount_currency, 0
)
rec.aml_amount_residual_currency = rec.aml_id.amount_residual_currency
rec.residual_after_payment_currency = max(
abs(rec.aml_id.amount_residual_currency) - rec.amount_currency, 0
)

partner_id = fields.Many2one("res.partner", string="Partner", ondelete="restrict")
commercial_partner_id = fields.Many2one(related="partner_id.commercial_partner_id")
move_id = fields.Many2one(
"account.move", string="Journal Entry", ondelete="set null"
)
move_ids = fields.One2many(
"account.move.line",
"payment_line_id",
string="Journal Entries Created",
)
aml_id = fields.Many2one(
"account.move.line", string="Journal Item to Reconcile", ondelete="set null"
)
aml_date_maturity = fields.Date(
string="Date Maturity", required=False, related="aml_id.date_maturity"
)

@api.onchange("move_id", "aml_id")
def _onchange_move_id(self):
aml_model = self.env["account.move.line"]
for rec in self:
type_move = dict_payment_type.get(rec.payment_id.payment_type, [])
if rec.move_id and not rec.aml_id:
domain = [
("move_id", "=", rec.move_id.id),
("amount_residual", "!=", 0.0),
]
lines_ordered = aml_model.search(
domain, order="date_maturity ASC", limit=1
)
if lines_ordered:
rec.aml_id = lines_ordered.id
if rec.aml_id:
rec.move_id = rec.aml_id.move_id.id
rec.account_id = rec.aml_id.account_id.id
rec.amount = abs(rec.aml_id.amount_residual)
rec.partner_id = rec.aml_id.partner_id.id
if rec.move_id.move_type == "entry":
if rec.payment_id.partner_type == "supplier":
if rec.payment_id.payment_type == "outbound":
rec.amount = -rec.aml_id.amount_residual
else:
rec.amount = rec.aml_id.amount_residual
else:
if rec.payment_id.payment_type == "outbound":
rec.amount = -rec.aml_id.amount_residual
else:
rec.amount = rec.aml_id.amount_residual
else:
if (
type_move
and rec.move_id.move_type not in type_move
and rec.amount
):
rec.amount *= -1

@api.constrains("amount", "aml_amount_residual")
def constrains_amount_residual(self):
for rec in self:
if rec.aml_id and 0 < rec.aml_amount_residual < rec.amount:
raise ValidationError(
_(
"the amount exceeds the residual amount, please check the invoice %s"
),
(rec.aml_id.move_id.name or rec.aml_id.name),
)
4 changes: 4 additions & 0 deletions account_payment_line/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_payment_register_counterpart_line_all,access_account_payment_register_counterpart_line_all,model_account_payment_register_counterpart_line,,1,0,0,0
access_account_payment_register_counterpart_line_group_account_invoice,access_account_payment_register_counterpart_line_group_account_invoice,model_account_payment_register_counterpart_line,account.group_account_invoice,1,1,1,1
access_account_payment_register_counterpart_line_group_account_user,access_account_payment_register_counterpart_line_group_account_user,model_account_payment_register_counterpart_line,account.group_account_user,1,1,1,1
access_account_payment_register_counterpart_line_group_account_manager,access_account_payment_register_counterpart_line_group_account_manager,model_account_payment_register_counterpart_line,account.group_account_manager,1,1,1,1
access_account_payment_counterpart_line_all,access_account_payment_counterpart_line_all,model_account_payment_counterpart_line,,1,0,0,0
access_account_payment_counterpart_line_group_account_invoice,access_account_payment_counterpart_line_group_account_invoice,model_account_payment_counterpart_line,account.group_account_invoice,1,1,1,1
access_account_payment_counterpart_line_group_account_user,access_account_payment_counterpart_line_group_account_user,model_account_payment_counterpart_line,account.group_account_user,1,1,1,1
Expand Down
1 change: 1 addition & 0 deletions account_payment_line/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_payment_register
Loading

0 comments on commit c5f5f31

Please sign in to comment.