Hi,
Please refer to the code below:
Python:
from odoo import models, fields
from datetime import timedelta
class ProjectTask(models.Model):
_inherit = 'project.task'
timesheet_start_date = fields.Date(string="Timesheet Start Date")
timesheet_end_date = fields.Date(string="Timesheet End Date")
daily_hours = fields.Float(string="Hours per Day", default=8.0)
def action_create_timesheets(self):
"""
Automatically generates timesheet entries for each working day
(Monday to Friday) between the 'timesheet_start_date' and
'timesheet_end_date' fields defined on the task.
Each day will be filled with the number of hours defined in
'daily_hours' field (default 8.0). Timesheet entries are
created for the current user (must be linked to an employee).
This method is triggered manually via a button on the task form.
Raises:
Skips any task that has missing start or end date.
Does not check for existing timesheet duplication.
"""
timesheet_model = self.env['account.analytic.line']
for task in self:
if not (task.timesheet_start_date and task.timesheet_end_date):
continue
current_date = task.timesheet_start_date
while current_date <= task.timesheet_end_date:
if current_date.weekday() < 5: # Monday to Friday only
timesheet_model.create({
'name': f'Timesheet for {current_date}',
'project_id': task.project_id.id,
'task_id': task.id,
'unit_amount': task.daily_hours,
'date': current_date,
'employee_id': self.env.user.employee_id.id,
'user_id': self.env.uid,
})
current_date += timedelta(days=1)
XML:
<record id="view_task_form2" model="ir.ui.view">
<field name="name">project.task.form.timesheet.autofill</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<xpath expr="//form//header" position="inside">
<button name="action_create_timesheets"
string="Create Timesheets"
type="object"
class="oe_highlight"/>
</xpath>
<xpath expr="//form/sheet/notebook/page[@name='page_timesheets']"
position="after">
<page string="Timesheet Autofill">
<group>
<field name="timesheet_start_date"/>
<field name="timesheet_end_date"/>
<field name="daily_hours"/>
</group>
</page>
</xpath>
</field>
</record>
Result:

When the button is clicked, timesheets are automatically created based on the provided data.

Hope it helps.