2
0
account-financial-tools/account_move_budget/models/account_move_budget_line.py

73 lines
2.4 KiB
Python
Raw Normal View History

2019-03-25 18:41:48 +01:00
# Copyright 2019 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class AccountMoveBudgetLine(models.Model):
_name = "account.move.budget.line"
_description = "Account Move Budget Line"
_order = "date desc, id desc"
budget_id = fields.Many2one(
comodel_name="account.move.budget",
2019-03-25 18:41:48 +01:00
string="Budget",
required=True,
ondelete="cascade",
2019-03-25 18:41:48 +01:00
index=True,
)
name = fields.Char(string="Label")
debit = fields.Monetary(default=0.0, currency_field="company_currency_id")
credit = fields.Monetary(default=0.0, currency_field="company_currency_id")
2019-03-25 18:41:48 +01:00
balance = fields.Monetary(
compute="_compute_store_balance",
2019-03-25 18:41:48 +01:00
store=True,
currency_field="company_currency_id",
2019-03-25 18:41:48 +01:00
help="Technical field holding the debit - "
"credit in order to open meaningful "
"graph views from reports",
2019-03-25 18:41:48 +01:00
)
company_currency_id = fields.Many2one(
"res.currency",
related="company_id.currency_id",
2019-03-25 18:41:48 +01:00
string="Company Currency",
readonly=True,
help="Utility field to express amount currency",
2019-03-25 18:41:48 +01:00
store=True,
)
account_id = fields.Many2one(
"account.account",
string="Account",
2019-03-25 18:41:48 +01:00
required=True,
index=True,
ondelete="cascade",
domain=[("deprecated", "=", False)],
default=lambda self: self._context.get("account_id", False),
2019-03-25 18:41:48 +01:00
)
date = fields.Date(string="Date", index=True, required=True)
2019-03-25 18:41:48 +01:00
analytic_account_id = fields.Many2one(
"account.analytic.account", string="Analytic Account"
2019-03-25 18:41:48 +01:00
)
company_id = fields.Many2one(
"res.company",
related="account_id.company_id",
string="Company",
2019-03-25 18:41:48 +01:00
store=True,
readonly=True,
)
partner_id = fields.Many2one("res.partner", string="Partner", ondelete="restrict")
2019-03-25 18:41:48 +01:00
@api.depends("debit", "credit")
2019-03-25 18:41:48 +01:00
def _compute_store_balance(self):
for line in self:
line.balance = line.debit - line.credit
@api.constrains("date")
2019-03-25 18:41:48 +01:00
def _constraint_date(self):
for rec in self:
if rec.budget_id.date_from > rec.date or rec.budget_id.date_to < rec.date:
raise ValidationError(
_("The date must be within the " "budget period.")
)