Skip to Content
Menu
This question has been flagged
6 Replies
5647 Views

Hello guys i have inherited the account.analytic.line model and added a selection field on whether the duration affects the sla or not i want to compute the totals for the time that affects only sla i.e when the selection is yes the duration is computed if no it is not computed how do i go about it.Any assistance will be highly appreciated.

this is the link to my view https://ibb.co/eDr2Te

Avatar
Discard
Best Answer

Hi,

code of Ibrahim  optimized

@api.multi
@api.depends('timesheet_ids', 'timesheet_ids.sla', 'timesheet_ids.duration')

def _compute_total_duration(self):

    for record in self:

    record.total_duration = sum(record.timesheet_ids.filtered(lambda rec: rec.sla == 'yes').mapped('duration'))

Regards

Avatar
Discard

Hi Ibrahim,

thank you ^^,

bye

Author

Thanks Cyril and Ibrahim.THe code is running Perfectly

Hi,

I recommand you to store the value for better performance, y adding strore=True in the definition of your field. To avoid ir.rule test, and better performance, you can add compute_sudo=True too.

Bye

Best Answer

add dependency to total compute field

ex:

api.dependency('your sla filed from line',....etc fields)

def _amount_total()

Avatar
Discard
Best Answer

Hi, 
you want to calculate the Total of the time that affects only sla. 
Considering that : 
your O2M field is :  timesheet_ids

the total field is : total_duration

the affects SLA :  sla

the Duration : duration

then : 

add (compute='_compute_total_duration', store=True) to total_duration

and 

@api.multi
@api.depends('timesheet_ids', 'timesheet_ids.sla', 'timesheet_ids.duration')
def _compute_total_duration(self):
    for record in self:
        total = False
        // Filter only on record with sla = Yes
        timesheets = record.timesheet_ids.filtered(lambda rec: rec.sla == 'yes')
        if timesheets:
            for timesheet in timesheets:
                total += timesheet.duration     // here, you may want to check how to add durations based on your field's signature
        record.total_duration = total                    
Avatar
Discard

Hi,

I think you forgot something in your code :

total += timesheet

should be

total += timesheet.duration

Bye

second error, :

total = False

should be not before but in for record in self:

I agree Cyril. I have updated my answer

Thank you

Hi,

I add @api.muti in your code

bye

Related Posts Replies Views Activity
0
Oct 17
5634
2
Jul 24
411
1
Jan 24
421
2
Apr 24
1932
1
Jul 22
892