42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
import datetime
|
|
|
|
from odoo import fields, models
|
|
|
|
class ProjectTimesheetMatrixWizard(models.TransientModel):
|
|
_name = 'project.timesheet.matrix.wiz'
|
|
|
|
def _default_line_ids(self):
|
|
project = self.env["project.project"].browse(self.env.context.get("project_id"))
|
|
dates = [datetime.date.today() - datetime.timedelta(days=delta) for delta in range(0, 8)]
|
|
users = self.env["res.users"].search([("partner_id", "in", project.message_partner_ids.ids)])
|
|
|
|
dates.sort()
|
|
|
|
orm_date_min = fields.Date.to_string(min(dates))
|
|
orm_date_max = fields.Date.to_string(max(dates))
|
|
|
|
analytic_lines = self.env["account.analytic.line"].search([
|
|
("date", ">=", orm_date_min),
|
|
("date", "<=", orm_date_max),
|
|
("user_id", "in", users.ids),
|
|
])
|
|
|
|
data = []
|
|
|
|
for date in dates:
|
|
for user in users.sorted("name"):
|
|
filtered_analytic_lines = analytic_lines.filtered(lambda line: line.user_id == user and line.date == fields.Date.to_string(date))
|
|
if filtered_analytic_lines:
|
|
data += [(4, line_id) for line_id in filtered_analytic_lines.ids]
|
|
else:
|
|
data.append((0, 0, {
|
|
"date": fields.Date.to_string(date),
|
|
"project_id": project.id,
|
|
"user_id": user.id,
|
|
"name": "Timesheet from Matrix",
|
|
"unit_amount": 0.0,
|
|
}))
|
|
|
|
return data
|
|
|
|
line_ids = fields.Many2many('account.analytic.line', default=_default_line_ids) |