Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
746 Vizualizări

I would like to have a computed field that shows the number of days an order is overdue.

I have created a field called x_studio_days_overdue that is an integer.

I would like to compute this field by subtracting two other fields and I would like to show the result as a number of days.

x_studio_shipping_date - scheduled_date = x_studio_days_overdue

How would I write the code to do this in the Compute section of the x_studio_days_overdue field?



Here is the code that ultimately worked:

Imagine profil
Abandonează
Cel mai bun răspuns

Hii Daniel Murphy,


This code ensures the field x_studio_days_overdue shows the correct number of overdue days as an integer by subtracting scheduled_date from x_studio_shipping_date. If either date is missing, it defaults to 0.

from datetime import date

@api.depends('x_studio_shipping_date', 'scheduled_date')
def _compute_days_overdue(self):
for record in self:
if record.x_studio_shipping_date and record.scheduled_date:
shipping_date = fields.Date.from_string(record.x_studio_shipping_date)
scheduled_date = fields.Date.from_string(record.scheduled_date)
record.x_studio_days_overdue = (shipping_date - scheduled_date).days
else:
record.x_studio_days_overdue = 0

Let me know if you need further clarification!

Thanks,

Imagine profil
Abandonează
Cel mai bun răspuns

What do you need that you can't get from the Scheduled Date?

Imagine profil
Abandonează
Autor Cel mai bun răspuns

I would like to know the number of days between when the order was scheduled to ship and when it actually shipped.  I want to be able to run reports off of this number so that I can track on-time delivery performance.

Imagine profil
Abandonează