[IMP] hr_expense: multicompany hard constraint

This commit provides hard constraints (SQL level) for expense to work in multi company
environement. This is related to a R&D task, the rest of the code need to be adapt, but
freeze is coming.
As expense implies accounting entries, it is required to generate them in the right
company, the company field should then be required.

Task-1999686

closes odoo/odoo#35953

Signed-off-by: Jérome Maes (jem) <jem@openerp.com>
This commit is contained in:
jem-odoo
2019-08-22 07:37:23 +00:00
parent a2c52e9efd
commit 8d0aee2293
2 changed files with 8 additions and 4 deletions
+7 -3
View File
@@ -57,11 +57,11 @@ class HrExpense(models.Model):
total_amount = fields.Monetary("Total", compute='_compute_amount', store=True, currency_field='currency_id')
company_currency_id = fields.Many2one('res.currency', string="Report Company Currency", related='sheet_id.currency_id', store=True, readonly=False)
total_amount_company = fields.Monetary("Total (Company Currency)", compute='_compute_total_amount_company', store=True, currency_field='company_currency_id')
company_id = fields.Many2one('res.company', string='Company', readonly=True, states={'draft': [('readonly', False)], 'refused': [('readonly', False)]}, default=lambda self: self.env.company)
company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, states={'draft': [('readonly', False)], 'refused': [('readonly', False)]}, default=lambda self: self.env.company)
currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, states={'draft': [('readonly', False)], 'refused': [('readonly', False)]}, default=lambda self: self.env.company.currency_id)
analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags', states={'post': [('readonly', True)], 'done': [('readonly', True)]}, domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
account_id = fields.Many2one('account.account', domain="[('company_id', '=', company_id)]", string='Account', default=_default_account_id, help="An expense account is expected")
account_id = fields.Many2one('account.account', string='Account', default=_default_account_id, domain="[('internal_type', '=', 'other'), ('company_id', '=', company_id)]", help="An expense account is expected")
description = fields.Text('Notes...', readonly=True, states={'draft': [('readonly', False)], 'reported': [('readonly', False)], 'refused': [('readonly', False)]})
payment_mode = fields.Selection([
("own_account", "Employee (to reimburse)"),
@@ -662,7 +662,7 @@ class HrExpenseSheet(models.Model):
payment_mode = fields.Selection(related='expense_line_ids.payment_mode', default='own_account', readonly=True, string="Paid By")
user_id = fields.Many2one('res.users', 'Manager', readonly=True, copy=False, states={'draft': [('readonly', False)]}, tracking=True)
total_amount = fields.Monetary('Total Amount', currency_field='currency_id', compute='_compute_amount', store=True)
company_id = fields.Many2one('res.company', string='Company', readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.company)
company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.company)
currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.company.currency_id)
attachment_number = fields.Integer(compute='_compute_attachment_number', string='Number of Attachments')
journal_id = fields.Many2one('account.journal', string='Expense Journal', states={'done': [('readonly', True)], 'post': [('readonly', True)]}, default=_default_journal_id, domain="[('company_id', '=', company_id)]", help="The journal used when the expense is done.")
@@ -673,6 +673,10 @@ class HrExpenseSheet(models.Model):
is_multiple_currency = fields.Boolean("Handle lines with different currencies", compute='_compute_is_multiple_currency')
can_reset = fields.Boolean('Can Reset', compute='_compute_can_reset')
_sql_constraints = [
('journal_id_required_posted', "CHECK((state IN ('post', 'done') AND journal_id IS NOT NULL) OR (state NOT IN ('post', 'done')))", 'The journal must be set on posted expense'),
]
@api.depends('expense_line_ids.total_amount_company')
def _compute_amount(self):
for sheet in self:
+1 -1
View File
@@ -45,7 +45,7 @@ class TestExpenseCommon(TestAccountNoChartCommon):
'name': 'Purchase Journal - Test',
'code': 'HRTPJ',
'type': 'purchase',
'company_id': cls.env.user.company_id.id,
'company_id': cls.env.company.id,
})
cls.expense_sheet = cls.env['hr.expense.sheet'].create({
'name': 'Expense for Johnny Employee',