Skip to content

Commit

Permalink
[IMP] project_timesheet_time_control: Refactor with mixin
Browse files Browse the repository at this point in the history
Models related to timesheet time controls now inherit from a mixin that adds most needed logic automatically.

This requires the changes introduced in OCA/project#596.
  • Loading branch information
yajo authored and carolinafernandez-tecnativa committed Sep 25, 2023
1 parent 9273575 commit d0abab8
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 185 deletions.
2 changes: 1 addition & 1 deletion crm_timesheet/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
'name': "CRM Timesheet",
'category': 'Customer Relationship Management',
'version': '12.0.2.0.0',
'version': '12.0.2.1.0',
'depends': [
'crm',
'project_timesheet_time_control'
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/crm_timesheet.pot
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ msgstr ""
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -41,32 +36,15 @@ msgid "Project"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/cs_CZ.po
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ msgstr "Analytická linka"
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -47,32 +42,15 @@ msgid "Project"
msgstr "Projekt"

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ msgstr "Kostenstellenbuchung"
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -44,32 +39,15 @@ msgid "Project"
msgstr "Projekt"

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ msgstr "Línea analítica"
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -47,32 +42,15 @@ msgid "Project"
msgstr "Proyecto"

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ msgstr "Ligne analytique"
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -46,32 +41,15 @@ msgid "Project"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
22 changes: 0 additions & 22 deletions crm_timesheet/i18n/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ msgstr "Riga analitica"
msgid "Helper to quickly switch between timesheet lines"
msgstr ""

#. module: crm_timesheet
#: model:ir.model.fields,help:crm_timesheet.field_crm_lead__show_time_control
msgid "Indicate which time control button to show, if any."
msgstr ""

#. module: crm_timesheet
#: model:ir.model,name:crm_timesheet.model_crm_lead
#: model:ir.model.fields,field_description:crm_timesheet.field_account_analytic_line__lead_id
Expand All @@ -47,32 +42,15 @@ msgid "Project"
msgstr "Progetto"

#. module: crm_timesheet
#: model:ir.model.fields,field_description:crm_timesheet.field_crm_lead__show_time_control
msgid "Show Time Control"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Start"
msgstr ""

#. module: crm_timesheet
#: code:addons/crm_timesheet/models/crm_lead.py:58
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_kanban_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_tree_view_oppor
#: model_terms:ir.ui.view,arch_db:crm_timesheet.view_crm_lead_kanban
#, python-format
msgid "Start work"
msgstr ""

#. module: crm_timesheet
#: selection:crm.lead,show_time_control:0
msgid "Stop"
msgstr ""

#. module: crm_timesheet
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_leads
#: model_terms:ir.ui.view,arch_db:crm_timesheet.crm_case_form_view_oppor
Expand Down
63 changes: 11 additions & 52 deletions crm_timesheet/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,34 @@
# Copyright 2017 David Vidal <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo import api, fields, models


class CrmLead(models.Model):
_inherit = 'crm.lead'
_name = 'crm.lead'
_inherit = ['crm.lead', "hr.timesheet.time_control.mixin"]

project_id = fields.Many2one(
comodel_name='project.project',
string="Project",
)
show_time_control = fields.Selection(
selection=[("start", "Start"), ("stop", "Stop")],
compute="_compute_show_time_control",
help="Indicate which time control button to show, if any.",
)
timesheet_ids = fields.One2many(
comodel_name='account.analytic.line',
inverse_name='lead_id',
string="Timesheet",
)

@api.model
def _timesheet_running_domain(self):
"""Domain to find running timesheet lines."""
return self.env["account.analytic.line"]._running_domain() + [
("lead_id", "in", self.ids),
]
def _relation_with_timesheet_line(self):
return "lead_id"

@api.depends("timesheet_ids.employee_id", "timesheet_ids.unit_amount")
def _compute_show_time_control(self):
"""Decide which time control button to show, if any."""
grouped = self.env["account.analytic.line"].read_group(
domain=self._timesheet_running_domain(),
fields=["id"],
groupby=["lead_id"],
)
lines_per_lead = {group["lead_id"][0]: group["lead_id_count"]
for group in grouped}
button_per_lines = {0: "start", 1: "stop"}
for lead in self:
lead.show_time_control = button_per_lines.get(
lines_per_lead.get(lead.id, 0),
False,
)
return super()._compute_show_time_control()

def button_start_work(self):
"""Create a new record starting now, with a running timer."""
return {
"context": {
"default_project_id": self.project_id.id,
"default_lead_id": self.id,
},
"name": _("Start work"),
"res_model": "hr.timesheet.switch",
"target": "new",
"type": "ir.actions.act_window",
"view_mode": "form",
"view_type": "form",
}

@api.multi
def button_end_work(self):
running_lines = self.env["account.analytic.line"].search(
self._timesheet_running_domain(),
)
if not running_lines:
raise UserError(
_("No running timer found in lead/opportunity %s. "
"Refresh the page and check again.") % self.display_name,
)
return running_lines.button_end_work()
result = super().button_start_work()
result["context"].update({
"default_project_id": self.project_id.id,
})
return result

0 comments on commit d0abab8

Please sign in to comment.