forked from OCA/timesheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] project_timesheet_time_control: Refactor with mixin
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
1 parent
a5fd0ac
commit d52cee6
Showing
8 changed files
with
12 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |