summaryrefslogtreecommitdiff
path: root/addons/project/models/analytic_account.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/project/models/analytic_account.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/project/models/analytic_account.py')
-rw-r--r--addons/project/models/analytic_account.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/addons/project/models/analytic_account.py b/addons/project/models/analytic_account.py
new file mode 100644
index 00000000..64fa95d4
--- /dev/null
+++ b/addons/project/models/analytic_account.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+
+
+class AccountAnalyticAccount(models.Model):
+ _inherit = 'account.analytic.account'
+ _description = 'Analytic Account'
+
+ project_ids = fields.One2many('project.project', 'analytic_account_id', string='Projects')
+ project_count = fields.Integer("Project Count", compute='_compute_project_count')
+
+ @api.depends('project_ids')
+ def _compute_project_count(self):
+ project_data = self.env['project.project'].read_group([('analytic_account_id', 'in', self.ids)], ['analytic_account_id'], ['analytic_account_id'])
+ mapping = {m['analytic_account_id'][0]: m['analytic_account_id_count'] for m in project_data}
+ for account in self:
+ account.project_count = mapping.get(account.id, 0)
+
+ @api.constrains('company_id')
+ def _check_company_id(self):
+ for record in self:
+ if record.company_id and not all(record.company_id == c for c in record.project_ids.mapped('company_id')):
+ raise UserError(_('You cannot change the company of an analytical account if it is related to a project.'))
+
+ def unlink(self):
+ projects = self.env['project.project'].search([('analytic_account_id', 'in', self.ids)])
+ has_tasks = self.env['project.task'].search_count([('project_id', 'in', projects.ids)])
+ if has_tasks:
+ raise UserError(_('Please remove existing tasks in the project linked to the accounts you want to delete.'))
+ return super(AccountAnalyticAccount, self).unlink()
+
+ def action_view_projects(self):
+ kanban_view_id = self.env.ref('project.view_project_kanban').id
+ result = {
+ "type": "ir.actions.act_window",
+ "res_model": "project.project",
+ "views": [[kanban_view_id, "kanban"], [False, "form"]],
+ "domain": [['analytic_account_id', '=', self.id]],
+ "context": {"create": False},
+ "name": "Projects",
+ }
+ if len(self.project_ids) == 1:
+ result['views'] = [(False, "form")]
+ result['res_id'] = self.project_ids.id
+ return result