Skip to Content
Menú
This question has been flagged
3 Respostes
182 Vistes

Hello,

I often work on projects for an entire month. I use the Timesheet app that is connected with my Project and my Sales Order to make an invoice. 


Is it possible to add a start date (eg. 1st of the month),then an end date, so that the timesheets will be filled in with 8 hours a day, each working day of that period? 


I have seen some extra plugins that uses calendar view and a start and end date, but then the total amount of hours are calculated and not the working hours. 


I think that this is not a really specific topic and that more people will find this usefull. 


Thanks 

Avatar
Descartar
Best Answer

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.

Avatar
Descartar
Autor Best Answer

Wow, thanks for the quick reply. This custom module will not work in Odoo Online, but I will defenitely test it on our system. 

Thanks

Avatar
Descartar
Best Answer

Hii,

Use a custom module:

Define a form (wizard) to input:

Start Date

End Date

Employee

Project/Task

Logic to fill working days only:

from datetime import timedelta

from odoo import models, fields, api

import datetime


class AutoTimesheetWizard(models.TransientModel):

    _name = 'auto.timesheet.wizard'


    employee_id = fields.Many2one('hr.employee', required=True)

    project_id = fields.Many2one('project.project', required=True)

    task_id = fields.Many2one('project.task', required=False)

    start_date = fields.Date(required=True)

    end_date = fields.Date(required=True)


    def action_fill_timesheets(self):

        user = self.employee_id.user_id

        date = self.start_date

        while date <= self.end_date:

            if date.weekday() < 5: # Monday to Friday (0-4)

                self.env['account.analytic.line'].create({

                    'name': 'Auto-filled timesheet',

                    'project_id': user.id ,

                    'unit_amount': 8,

                    'date': date,

                })

            date += timedelta(days=1)


i hope it is use full

Avatar
Descartar
Related Posts Respostes Vistes Activitat
2
de jul. 25
960
1
de maig 25
723
0
de maig 25
846
2
d’abr. 25
774
0
d’abr. 25
837