Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
727 Lượt xem

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:

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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,

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ