Assign operating workstreams to company context
This commit is contained in:
parent
462e91f00c
commit
315ab7497a
@ -14,6 +14,7 @@ class OperatingSystemSetup:
|
||||
self.env = env
|
||||
self.Partner = env["res.partner"].sudo()
|
||||
self.Project = env["project.project"].sudo()
|
||||
self.Invoice = env["account.move"].sudo()
|
||||
|
||||
def run(self):
|
||||
self._promote_existing_customers()
|
||||
@ -23,7 +24,7 @@ class OperatingSystemSetup:
|
||||
self._ensure_followup_cron_active()
|
||||
|
||||
def _promote_existing_customers(self):
|
||||
invoice_partners = self.env["account.move"].sudo().search(
|
||||
invoice_partners = self.Invoice.search(
|
||||
[
|
||||
("move_type", "=", "out_invoice"),
|
||||
("state", "=", "posted"),
|
||||
@ -166,10 +167,12 @@ class OperatingSystemSetup:
|
||||
limit=1,
|
||||
)
|
||||
if not project:
|
||||
company = self._project_company(partner, is_internal)
|
||||
project = self.Project.create(
|
||||
{
|
||||
"name": project_name,
|
||||
"partner_id": partner.id if not is_internal else False,
|
||||
"company_id": company.id if company else False,
|
||||
"allow_timesheets": True,
|
||||
"allow_milestones": True,
|
||||
"mcs_delivery_type": delivery_type,
|
||||
@ -186,10 +189,13 @@ class OperatingSystemSetup:
|
||||
return project
|
||||
|
||||
values = {}
|
||||
company = self._project_company(partner, is_internal)
|
||||
if not project.partner_id and not is_internal:
|
||||
values["partner_id"] = partner.id
|
||||
if project.partner_id and project.partner_id != partner and not is_internal:
|
||||
values["partner_id"] = partner.id
|
||||
if company and not project.company_id:
|
||||
values["company_id"] = company.id
|
||||
if not project.mcs_delivery_type:
|
||||
values["mcs_delivery_type"] = delivery_type
|
||||
if not project.mcs_initiative_type:
|
||||
@ -213,6 +219,29 @@ class OperatingSystemSetup:
|
||||
}
|
||||
return mapping.get(delivery_type, "client")
|
||||
|
||||
def _project_company(self, partner, is_internal=False):
|
||||
if is_internal:
|
||||
return self.env.company
|
||||
invoice = self.Invoice.search(
|
||||
[
|
||||
("move_type", "=", "out_invoice"),
|
||||
("state", "=", "posted"),
|
||||
("commercial_partner_id", "=", partner.commercial_partner_id.id),
|
||||
("company_id", "!=", False),
|
||||
],
|
||||
order="invoice_date desc, id desc",
|
||||
limit=1,
|
||||
)
|
||||
if invoice:
|
||||
return invoice.company_id
|
||||
if partner.currency_id:
|
||||
company = self.env["res.company"].sudo().search(
|
||||
[("currency_id", "=", partner.currency_id.id)], limit=1
|
||||
)
|
||||
if company:
|
||||
return company
|
||||
return self.env.company
|
||||
|
||||
def _ensure_followup_cron_active(self):
|
||||
cron = self.env.ref(
|
||||
"mcs_operating_system.ir_cron_mcs_schedule_missing_followups",
|
||||
@ -241,7 +270,7 @@ class OperatingSystemSetup:
|
||||
]
|
||||
):
|
||||
has_invoice = bool(
|
||||
self.env["account.move"].sudo().search_count(
|
||||
self.Invoice.search_count(
|
||||
[
|
||||
("move_type", "=", "out_invoice"),
|
||||
("state", "=", "posted"),
|
||||
|
||||
@ -200,7 +200,7 @@ class McsOperatingDashboard(models.Model):
|
||||
|
||||
active_projects = Project.search(
|
||||
[("active", "=", True)]
|
||||
+ self._company_domain(Project, company_ids, include_shared=True)
|
||||
+ self._company_domain(Project, company_ids, include_shared=False)
|
||||
)
|
||||
client_projects = active_projects.filtered(
|
||||
lambda project: project.mcs_delivery_type
|
||||
@ -266,7 +266,7 @@ class McsOperatingDashboard(models.Model):
|
||||
)
|
||||
active_tasks = Task.search(
|
||||
[("active", "=", True)]
|
||||
+ self._company_domain(Task, company_ids, include_shared=True)
|
||||
+ self._company_domain(Task, company_ids, include_shared=False)
|
||||
)
|
||||
|
||||
monthly_target = sum(active_projects.mapped("mcs_monthly_revenue_target"))
|
||||
@ -702,7 +702,7 @@ class McsOperatingDashboard(models.Model):
|
||||
"view_mode": "tree,form,kanban",
|
||||
"views": self._action_views("tree", "form", "kanban"),
|
||||
"domain": [("active", "=", True), ("mcs_monthly_revenue_target", ">", 0)]
|
||||
+ self._company_domain(Project, company_ids, include_shared=True),
|
||||
+ self._company_domain(Project, company_ids, include_shared=False),
|
||||
},
|
||||
"contracted_projects": {
|
||||
"type": "ir.actions.act_window",
|
||||
@ -711,7 +711,7 @@ class McsOperatingDashboard(models.Model):
|
||||
"view_mode": "tree,form,kanban",
|
||||
"views": self._action_views("tree", "form", "kanban"),
|
||||
"domain": [("active", "=", True), ("mcs_contracted_revenue", ">", 0)]
|
||||
+ self._company_domain(Project, company_ids, include_shared=True),
|
||||
+ self._company_domain(Project, company_ids, include_shared=False),
|
||||
},
|
||||
"pipeline": {
|
||||
"type": "ir.actions.act_window",
|
||||
@ -763,7 +763,7 @@ class McsOperatingDashboard(models.Model):
|
||||
("active", "=", True),
|
||||
("mcs_portfolio_health", "in", ["at_risk", "blocked"]),
|
||||
]
|
||||
+ self._company_domain(Project, company_ids, include_shared=True),
|
||||
+ self._company_domain(Project, company_ids, include_shared=False),
|
||||
},
|
||||
"ceo_actions": {
|
||||
"type": "ir.actions.act_window",
|
||||
@ -780,7 +780,7 @@ class McsOperatingDashboard(models.Model):
|
||||
"view_mode": "tree,form,kanban",
|
||||
"views": self._action_views("tree", "form", "kanban"),
|
||||
"domain": [("mcs_delivery_type", "in", ["internal_product", "internal_r_and_d"])]
|
||||
+ self._company_domain(Project, company_ids, include_shared=True),
|
||||
+ self._company_domain(Project, company_ids, include_shared=False),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user