Sales Order Delivery Status (computed)

This is stolen from an Odoo Help answer, so credit is due to Paresh Wagh and David from dajac.

The requirement is to be able to see whether a sales order has been partially or fully delivered. This is important (amongst other reasons) if you will wait till an order is fully delivered before you invoice.

Computed Fields can be added in Odoo Studio or directly (by enabling “Developer” mode and going to Settings / Technical / Fields)

Start by adding a field (type = “char” )

Or if you used Odoo Studio, select the field you added and then click on “More”

  1. Stored
    • Set to TRUE so that it can be used in filters and grouping – note that the value of the computed field will only be changed if the value of one of the dependent fields changes.
    • You can set it to FALSE if you don’t need to use it in filters or grouping. This means Odoo will recalculate it when it is displayed.
  2. Dependencies
    • Enter a list of field names, separated by commas:
      • order_line.product_uom_qty, order_line.qty_delivered
  3. Compute (Python code)
for record in self:
    qty_sum = sum(line.product_uom_qty for line in record.order_line)
    del_sum = sum(line.qty_delivered for line in record.order_line)
    if del_sum == 0:
        record['x_studio_delivery_status'] = "New"
    elif qty_sum > del_sum:
        record['x_studio_delivery_status'] = "Partial"
    else:
        record['x_studio_delivery_status'] = "Done"
  • This is fairly simple Python code.
    • The first two lines calculate the total quantity ordered and delivered by reading all the sales order lines for this sales order.
    • Then check the status
      • If nothing has been delivered, status is “New”
      • If the delivery quantity is less than ordered, status is “Partial”
      • Otherwise (delivery quantity is greater than or equal to ordered quantity), status is “Done”.

It is simple code, but Python is very particular about how code is indented, and if you get it wrong then you will get rather unhelpful error messages.

If you are a Python programmer you can easily enhance the logic as required.

And if you are a Python programmer, please let me know how to exclude order lines for services and consumable items (because there is no “delivery”).

Other examples of Computed Fields

  1. Computed Field with count of POS orders
  2. Use Automated Actions to stop users archiving products
  3. Use a computed field to set field attributes

3 thoughts on “Sales Order Delivery Status (computed)

  1. Hello Friend,

    Thank you for the tip, helped me solve half of my problem.

    I will tell you how I made it work for Consumables and Services, first I created a line invisible float field called Real Quantity.

    Code:

    for line in self:
    if line.product_template_id.type != “product”:
    0
    else:
    line[‘x_studio_real_quantity’] = line.product_uom_qty

    Then I used your code but instead of using qty_sum = sum(line.product_uom_qty for line in record.order_line)

    I changed it for qty_sum = sum(line.x_studio_real_quantity for line in record.order_line)

    Also I changed the order of the if results.

    if del_sum == qty_sum:
    record[‘x_studio_status_deliver’] = “Done”
    elif del_sum == 0:
    record[‘x_studio_status_deliver’] = “To Do”
    else:
    record[‘x_studio_status_deliver’] = “Partial”

    Solved the issue right away.

    Like

  2. #The code that I used is as below, it throws an error,

    for line in self:
    if line.product_template_id.type != “product”:
    0
    else:
    line[‘x_studio_real_quantity’] = order_line.product_uom_qty

    for record in self:
    qty_sum = sum(line.x_studio_real_quantity for line in record.order_line)
    del_sum = sum(line.qty_delivered for line in record.order_line)
    if del_sum == qty_sum:
    record[‘x_studio_delivery_status’] = “Done”
    elif del_sum == 0:
    record[‘x_studio_delivery_status’] = “To Do”
    else:
    record[‘x_studio_delivery_status’] = “Partial”

    Like

Leave a comment