This is another interesting use of Automated Actions. Credit to Ray Carnes who posted this on the Odoo help forum.
Something similar could probably be achieved with development, but if you are using Odoo Online that isn’t an option!
First a brief explanation of the standard functionality in Odoo
In the Purchase tab for Products there is a list of vendors:
On the left-hand side you will see “grab handles” that allow you to re-order the list by “drag and drop”.
Technical note: this uses an integer field with the
This is important because if you are using “Buy to Order” or automatic reordering, Odoo will create RFQs for the first vendor in the list.
However, you might want Odoo to propose the vendor with the lowest price. This is fairly simple to setup, requiring three steps:
- Create an Automated Action
- Disable the “grab handle”
- Reset the sequence on the existing supplier price records
1. Automated Action
Start by enabling developer mode and navigating to Settings / Technical / Automated Actions. For basic information, read the page about Automated Actions first.
The Model is “Product Template” and Action to Do is “Execute Python Code”
record['seller_ids'] = record.seller_ids.sorted(key = lambda s: s.price)
I’m not a Python programmer, but it seems to be possible to add more fields to the sort:
I’m not a Python programmer, but a bit of quick research reveals that you can add a second field to the sort:
if record.seller_ids: record['seller_ids'] = record.seller_ids.sorted(key = lambda s: (s.price,s.min_qty))
2. Disable the “grab handle”
This is a very small change, making the sequence number invisible.
We do this by creating an Extension View
You could use Odoo Studio in Odoo Enterprise (if it’s installed) by clicking on “Invisible” in the Field Properties – this will also create an Extension View.
Otherwise, start by enabling developer mode and navigating to Settings / Technical / Views. Click on “Create”
View Name: product.supplierinfo.tree.view.remove_drag_handle
View Type: Tree
View inheritance mode: Extension View
<data> <field name="sequence" position="attributes"> <attribute name="invisible">1</attribute> </field> </data>
Note that this will affect both the Product Form View and the Vendor Price Lists (on the Purchase / Configuration menu):
3. Remove sequence on existing records
It’s also necessary to reset the sequence on the existing supplier price records. If this not done, these records will not be re-sequenced by the Automated Action.
Start by selecting all records on the Vendor Price List and click on Action and Export:
The following dialogue (pop up) is displayed:
Ensure that you select “I want to update data (import-compatible export)” and select the “Sequence” field (the user interface is slightly different in Odoo 13 compared with Odoo 12).
Then click on “Export” and close the dialogue.
In Excel update the sequence to 1 for all records and save the file.
Now click on “Import”
Click on “Load File” and select the file you just updated.
The following preview is displayed:
Check that you have the Field “External ID” shown. That’s important so that records are updated. If it’s not present, Odoo will create new records!
You should also check that the Sequence is 1 for all records
You can click on “Test” if you want to check what you are uploading. Then click on “Import”.
Now Odoo should automatically re-sequence the list so that the Vendor with the lowest price is selected.