Skip to Content
मेन्यू
This question has been flagged
3 Replies
1897 Views

Hi, thanks all for support.


I'm creating with odoo 16 ce a new View to show the task list, accessable only for supervisors, filtering only by task and timesheet added by workers for whom he is supervisors. I have to show hours logged and remaining hours for each task. 

The best I could accomplish was this, what do I have to change to reach my goal?

The field x_remaining_hours has been added to account.analytic.line model as calculated with this "Calculate" field:


sum = 0
for record in self:
​sum = sum + record.task_id.project_id.remaining_hours


Still not working... my code below (tags stripped 'cause I'm not able to use the code tag, sorry)

tree editable="bottom" string="Timesheet Activities" sample="1"
field name="date"/
field name="employee_id"/
field name="project_id" required="1" options="{'no_create_edit': True, 'no_open': 1}"/
field name="task_id" optional="show" options="{'no_create_edit': True, 'no_open': True}" widget="task_with_hours" context="{'default_project_id': project_id}"/
field name="name" optional="show" required="0"/
field name="unit_amount" optional="show" widget="timesheet_uom" sum="Total" decoration-danger="unit_amount > 24 or unit_amount < 0"/
field name="x_remaining_hours" optional="show" widget="remaining_hours" sum="Total" decoration-danger="unit_amount > 24 or unit_amount < 0"/

field name="company_id" invisible="1"/
field name="user_id" invisible="1"/
/tree
 

Avatar
Discard
Best Answer

Hii Andrea,
By looking on your problem seems that you want to add your field 'x_remaining_hours' to the 'account.analytic.line' to achieve that you have to inherit the tree view by xpath, 
We cannot directly define the field the value like above code if not from its model.
class AccountAnalyticLine(models.Model): 
_inherit = 'account.analytic.line'

x_remaining_hours = fields.Float(string='Remaining Hours', compute='_compute_remaining_hours', store=True)

@api.depends('task_id.project_id.remaining_hours')
def _compute_remaining_hours(self):
for record in self:
record.x_remaining_hours = record.task_id.project_id.remaining_hours

Take a look for inheriting tree view: https://www.odoo.com/forum/help-1/how-to-inherit-list-tree-view-163626
I hope this information is helpful to you.
Feel free for further assistance on contact@geminatecs.com
Thank you ,
Geminate Consultancy Services 
w : www.geminatecs.com

Avatar
Discard
Author Best Answer

Hi Shubham, thanks a lot for your improvements to the code.

Currently, I'm not able to add the remaining_hours field to the account.analytic.line model, how do I accomplish this? I was trying to add the x_remaining_hours with the code above but unsuccessfully... Can you give me an hint on this?

Thanks again, really appreciated!

Avatar
Discard

you need to define a computed field in a custom module. Here's an example of how you can accomplish this:
class AccountAnalyticLine(models.Model):
_inherit = 'account.analytic.line'

x_remaining_hours = fields.Float(string='Remaining Hours', compute='_compute_remaining_hours', store=True)

@api.depends('task_id.project_id.remaining_hours')
def _compute_remaining_hours(self):
for record in self:
record.x_remaining_hours = record.task_id.project_id.remaining_hours

After completing these steps, you should be able to add the x_remaining_hours field to your tree view as mentioned in your previous code snippet. The compute method _compute_remaining_hours will calculate the remaining hours based on the remaining_hours field of the associated project.

Best Answer

you need to make a few changes to your code. Here's an updated version of your code with the necessary modifications:

tree editable="bottom" string="Timesheet Activities" sample="1">
field name="date"/>
field name="employee_id"/>
field name="project_id" required="1" options="{'no_create_edit': True, 'no_open': 1}"/>
field name="task_id" optional="show" options="{'no_create_edit': True, 'no_open': True}" widget="task_with_hours" context="{'default_project_id': project_id}"/>
field name="name" optional="show" required="0"/>
field name="unit_amount" optional="show" widget="timesheet_uom" sum="Total" decoration-danger="unit_amount > 24 or unit_amount < 0"/>
field name="task_id.project_id.remaining_hours" optional="show" widget="float_time" sum="Total Remaining Hours" decoration-danger="unit_amount > 24 or unit_amount < 0"/>
field name="company_id" invisible="1"/>
field name="user_id" invisible="1"/>
/tree>


  1. Added the field task_id.project_id.remaining_hours directly in the view: Instead of calculating the remaining hours in the Python code, you can directly access the remaining_hours field of the project_id related to the task_id using the dot notation.

  2. Changed the widget for the x_remaining_hours field: In your code, you had widget="remaining_hours", but there is no built-in Odoo widget called "remaining_hours". I assumed you wanted to display the remaining hours as a float value, so I changed the widget to "float_time". You can modify it according to your requirements.

Make sure you have the necessary fields (project_id.remaining_hours) available in the account.analytic.line model and that the field is correctly calculated in the model definition.

Avatar
Discard
Author

Hi Shubham, thanks a lot for your improvements to the code.

Currently, I'm not able to add the remaining_hours field to the account.analytic.line model, how do I accomplish this? I was trying to add the x_remaining_hours with the code above but unsuccessfully... Can you give me an hint on this?

Thanks again, really appreciated!