Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions product_main_seller/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

===================
Product Main Vendor
===================
Expand All @@ -17,7 +13,7 @@ Product Main Vendor
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github
Expand Down
1 change: 1 addition & 0 deletions product_main_seller/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"depends": ["purchase"],
"maintainers": ["legalsylvain", "quentinDupont"],
"data": [
"data/ir_cron.xml",
"views/view_product_product.xml",
"views/view_product_template.xml",
],
Expand Down
12 changes: 12 additions & 0 deletions product_main_seller/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<odoo noupdate="1">
<record id="ir_cron_recompute_main_seller_id" model="ir.cron">
<field name="name">Recompute Main Vendor</field>
<field name="model_id" ref="product.model_product_supplierinfo" />
<field name="state">code</field>
<field name="code">model._cron_recompute_main_seller_id()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="active">True</field>
</record>
</odoo>
45 changes: 45 additions & 0 deletions product_main_seller/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,48 @@ def pre_init_hook(env):
WHERE pt.id = first_supplierinfos.product_tmpl_id;
"""
)
cr.execute(
"""
ALTER TABLE product_product
ADD COLUMN IF NOT EXISTS main_seller_id integer;
"""
)
cr.execute(
"""
WITH ranked_supplierinfos AS (
SELECT
p.id AS product_id,
psi.partner_id,
ROW_NUMBER() OVER (
PARTITION BY p.id
ORDER BY
CASE
WHEN psi.product_id = p.id THEN 0
ELSE 1
END,
psi.sequence,
psi.min_qty DESC,
psi.price,
psi.id
) AS row_number
FROM product_product p
JOIN product_supplierinfo psi
ON psi.product_tmpl_id = p.product_tmpl_id
AND (
psi.product_id = p.id
OR psi.product_id IS NULL
)
JOIN res_partner rp
ON rp.id = psi.partner_id
AND rp.active
WHERE
(psi.date_start IS NULL OR psi.date_start <= CURRENT_DATE)
AND (psi.date_end IS NULL OR psi.date_end >= CURRENT_DATE)
)
UPDATE product_product p
SET main_seller_id = ranked_supplierinfos.partner_id
FROM ranked_supplierinfos
WHERE ranked_supplierinfos.product_id = p.id
AND ranked_supplierinfos.row_number = 1;
"""
)
2 changes: 2 additions & 0 deletions product_main_seller/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from . import product_template
from . import product_product
from . import product_supplierinfo
42 changes: 42 additions & 0 deletions product_main_seller/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2026 Tecnativa - Andrii Kompaniiets
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class ProductProduct(models.Model):
_inherit = "product.product"

main_seller_id = fields.Many2one(
comodel_name="res.partner",
string="Main Vendor",
help="Put your supplier info in first position to set as main vendor",
compute="_compute_main_seller_id",
store=True,
)

@api.depends(
"variant_seller_ids.sequence",
"variant_seller_ids.partner_id.active",
"variant_seller_ids.date_start",
"variant_seller_ids.date_end",
)
def _compute_main_seller_id(self):
for product in self:
if product.variant_seller_ids:
product.main_seller_id = fields.first(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing here the filter for only this variant to have priority.

product.variant_seller_ids.filtered(
lambda seller, p=product: seller.partner_id.active
and (
not seller.date_start
or seller.date_start <= fields.Date.today()
)
and (
not seller.date_end
or seller.date_end >= fields.Date.today()
)
and (not seller.product_id or seller.product_id == p)
)
).partner_id
else:
product.main_seller_id = False
14 changes: 14 additions & 0 deletions product_main_seller/models/product_supplierinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2026 Tecnativa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models


class ProductSupplierinfo(models.Model):
_inherit = "product.supplierinfo"

@api.model
def _cron_recompute_main_seller_id(self):
products = self.env["product.product"].search([])
templates = self.env["product.template"].search([])
products._compute_main_seller_id()
templates._compute_main_seller_id()
15 changes: 14 additions & 1 deletion product_main_seller/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ class ProductTemplate(models.Model):
store=True,
)

@api.depends("variant_seller_ids.sequence", "variant_seller_ids.partner_id.active")
@api.depends(
"variant_seller_ids.sequence",
"variant_seller_ids.partner_id.active",
"variant_seller_ids.date_start",
"variant_seller_ids.date_end",
)
def _compute_main_seller_id(self):
for template in self:
if template.variant_seller_ids:
template.main_seller_id = fields.first(
template.variant_seller_ids.filtered(
lambda seller: seller.partner_id.active
and (
not seller.date_start
or seller.date_start <= fields.Date.today()
)
and (
not seller.date_end
or seller.date_end >= fields.Date.today()
)
)
).partner_id
else:
Expand Down
24 changes: 9 additions & 15 deletions product_main_seller/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Product Main Vendor</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -360,21 +360,16 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="product-main-vendor">
<h1 class="title">Product Main Vendor</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="product-main-vendor">
<h1>Product Main Vendor</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:c353b8931ca2140d6d80974f00d4e5e073b737283dc2770fe718a8842cd6bd4e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/purchase-workflow/tree/18.0/product_main_seller"><img alt="OCA/purchase-workflow" src="https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/purchase-workflow-18-0/purchase-workflow-18-0-product_main_seller"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/purchase-workflow/tree/18.0/product_main_seller"><img alt="OCA/purchase-workflow" src="https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/purchase-workflow-18-0/purchase-workflow-18-0-product_main_seller"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module extends the Odoo Product module to compute and display the
main Vendor of each products. The main vendor is the first vendor in the
vendors list.</p>
Expand All @@ -392,29 +387,29 @@ <h1>Product Main Vendor</h1>
</ul>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/purchase-workflow/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/purchase-workflow/issues/new?body=module:%20product_main_seller%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h2><a class="toc-backref" href="#toc-entry-2">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-3">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<ul class="simple">
<li>GRAP</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-4">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<ul class="simple">
<li>Quentin Dupont (<a class="reference external" href="mailto:quentin.dupont&#64;grap.coop">quentin.dupont&#64;grap.coop</a>)</li>
</ul>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand All @@ -429,6 +424,5 @@ <h3><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
3 changes: 1 addition & 2 deletions product_main_seller/views/view_product_product.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2022 - Today: GRAP (http://www.grap.coop)
@author: Quentin DUPONT (quentin.dupont@grap.coop)
Copyright 2026 - Tecnativa
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>
Expand Down
Loading