Files

102 lines
3.2 KiB
Python

# Copyright 2004-2010 OpenERP SA
# Copyright 2014 Angel Moya <angel.moya@domatix.com>
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# Copyright 2016-2018 Carlos Dauden <carlos.dauden@tecnativa.com>
# Copyright 2016-2017 LasLabs Inc.
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class ContractTemplate(models.Model):
_name = "contract.template"
_description = "Contract Template"
_inherit = "contract.recurring.mixin"
_check_company_auto = True
# Fields not synced to the actual contract
NO_SYNC = ["name", "partner_id", "company_id"]
# === Basic Info ===
name = fields.Char(required=True)
partner_id = fields.Many2one(
comodel_name="res.partner",
string="Partner",
index=True,
)
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
required=True,
default=lambda self: self.env.company,
)
pricelist_id = fields.Many2one(
comodel_name="product.pricelist",
string="Pricelist",
)
# === Contract Settings ===
contract_type = fields.Selection(
selection=[("sale", "Customer"), ("purchase", "Supplier")],
default="sale",
index=True,
)
line_recurrence = fields.Boolean(
string="Recurrence at line level?",
help="Check this if you want to control recurrence at the line level "
"instead of for the whole contract.",
)
generation_type = fields.Selection(
selection=[("invoice", "Invoice")],
default=lambda self: self._default_generation_type(),
help="Defines what document is automatically generated by the cron.",
)
journal_id = fields.Many2one(
comodel_name="account.journal",
string="Journal",
domain="[('type', '=', contract_type)]",
compute="_compute_journal_id",
store=True,
readonly=False,
index=True,
check_company=True,
)
# === Contract Line Templates ===
contract_line_ids = fields.One2many(
comodel_name="contract.template.line",
inverse_name="contract_id",
copy=True,
string="Contract template lines",
)
def _get_valid_journal_type(self):
self.ensure_one()
if self.contract_type == "sale":
return ["sale"]
elif self.contract_type == "purchase":
return ["purchase"]
@api.model
def _default_generation_type(self):
"""Default generation type for the contract."""
return "invoice"
@api.depends("contract_type", "company_id")
def _compute_journal_id(self):
"""Auto-select a journal based on contract type and company."""
AccountJournal = self.env["account.journal"]
for contract in self:
# See Odoo account_move._search_default_journal()
company = contract.company_id or contract.env.company
domain = [
*self.env["account.journal"]._check_company_domain(company),
("type", "in", contract._get_valid_journal_type()),
]
contract.journal_id = AccountJournal.search(domain, limit=1).id or None