Skip to content

Commit e35c035

Browse files
[IMP] product_main_seller: Add main_seller_id to product.product product and add to depends date_start, date_end
1 parent 34fd6ea commit e35c035

10 files changed

Lines changed: 141 additions & 23 deletions

File tree

product_main_seller/README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
===================
62
Product Main Vendor
73
===================
@@ -17,7 +13,7 @@ Product Main Vendor
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
2218
:alt: License: AGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github

product_main_seller/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"depends": ["purchase"],
1414
"maintainers": ["legalsylvain", "quentinDupont"],
1515
"data": [
16+
"data/ir_cron.xml",
1617
"views/view_product_product.xml",
1718
"views/view_product_template.xml",
1819
],
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
<odoo noupdate="1">
3+
<record id="ir_cron_recompute_main_seller_id" model="ir.cron">
4+
<field name="name">Recompute Main Vendor</field>
5+
<field name="model_id" ref="product.model_product_supplierinfo" />
6+
<field name="state">code</field>
7+
<field name="code">model._cron_recompute_main_seller_id()</field>
8+
<field name="interval_number">1</field>
9+
<field name="interval_type">days</field>
10+
<field name="active">True</field>
11+
</record>
12+
</odoo>

product_main_seller/hooks.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,48 @@ def pre_init_hook(env):
3636
WHERE pt.id = first_supplierinfos.product_tmpl_id;
3737
"""
3838
)
39+
cr.execute(
40+
"""
41+
ALTER TABLE product_product
42+
ADD COLUMN IF NOT EXISTS main_seller_id integer;
43+
"""
44+
)
45+
cr.execute(
46+
"""
47+
WITH ranked_supplierinfos AS (
48+
SELECT
49+
p.id AS product_id,
50+
psi.partner_id,
51+
ROW_NUMBER() OVER (
52+
PARTITION BY p.id
53+
ORDER BY
54+
CASE
55+
WHEN psi.product_id = p.id THEN 0
56+
ELSE 1
57+
END,
58+
psi.sequence,
59+
psi.min_qty DESC,
60+
psi.price,
61+
psi.id
62+
) AS row_number
63+
FROM product_product p
64+
JOIN product_supplierinfo psi
65+
ON psi.product_tmpl_id = p.product_tmpl_id
66+
AND (
67+
psi.product_id = p.id
68+
OR psi.product_id IS NULL
69+
)
70+
JOIN res_partner rp
71+
ON rp.id = psi.partner_id
72+
AND rp.active
73+
WHERE
74+
(psi.date_start IS NULL OR psi.date_start <= CURRENT_DATE)
75+
AND (psi.date_end IS NULL OR psi.date_end >= CURRENT_DATE)
76+
)
77+
UPDATE product_product p
78+
SET main_seller_id = ranked_supplierinfos.partner_id
79+
FROM ranked_supplierinfos
80+
WHERE ranked_supplierinfos.product_id = p.id
81+
AND ranked_supplierinfos.row_number = 1;
82+
"""
83+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from . import product_template
2+
from . import product_product
3+
from . import product_supplierinfo
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Tecnativa - Andrii Kompaniiets
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import api, fields, models
5+
6+
7+
class ProductProduct(models.Model):
8+
_inherit = "product.product"
9+
10+
main_seller_id = fields.Many2one(
11+
comodel_name="res.partner",
12+
string="Main Vendor",
13+
help="Put your supplier info in first position to set as main vendor",
14+
compute="_compute_main_seller_id",
15+
store=True,
16+
)
17+
18+
@api.depends(
19+
"variant_seller_ids.sequence",
20+
"variant_seller_ids.partner_id.active",
21+
"variant_seller_ids.date_start",
22+
"variant_seller_ids.date_end",
23+
)
24+
def _compute_main_seller_id(self):
25+
for product in self:
26+
if product.variant_seller_ids:
27+
product.main_seller_id = fields.first(
28+
product.variant_seller_ids.filtered(
29+
lambda seller, p=product: seller.partner_id.active
30+
and (
31+
not seller.date_start
32+
or seller.date_start <= fields.Date.today()
33+
)
34+
and (
35+
not seller.date_end
36+
or seller.date_end >= fields.Date.today()
37+
)
38+
and (not seller.product_id or seller.product_id == p)
39+
)
40+
).partner_id
41+
else:
42+
product.main_seller_id = False
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2026 Tecnativa
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
from odoo import api, models
4+
5+
6+
class ProductSupplierinfo(models.Model):
7+
_inherit = "product.supplierinfo"
8+
9+
@api.model
10+
def _cron_recompute_main_seller_id(self):
11+
products = self.env["product.product"].search([])
12+
templates = self.env["product.template"].search([])
13+
products._compute_main_seller_id()
14+
templates._compute_main_seller_id()

product_main_seller/models/product_template.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@ class ProductTemplate(models.Model):
1616
store=True,
1717
)
1818

19-
@api.depends("variant_seller_ids.sequence", "variant_seller_ids.partner_id.active")
19+
@api.depends(
20+
"variant_seller_ids.sequence",
21+
"variant_seller_ids.partner_id.active",
22+
"variant_seller_ids.date_start",
23+
"variant_seller_ids.date_end",
24+
)
2025
def _compute_main_seller_id(self):
2126
for template in self:
2227
if template.variant_seller_ids:
2328
template.main_seller_id = fields.first(
2429
template.variant_seller_ids.filtered(
2530
lambda seller: seller.partner_id.active
31+
and (
32+
not seller.date_start
33+
or seller.date_start <= fields.Date.today()
34+
)
35+
and (
36+
not seller.date_end
37+
or seller.date_end >= fields.Date.today()
38+
)
2639
)
2740
).partner_id
2841
else:

product_main_seller/static/description/index.html

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Product Main Vendor</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="product-main-vendor">
364+
<h1 class="title">Product Main Vendor</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="product-main-vendor">
370-
<h1>Product Main Vendor</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375370
!! source digest: sha256:c353b8931ca2140d6d80974f00d4e5e073b737283dc2770fe718a8842cd6bd4e
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<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>
372+
<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>
378373
<p>This module extends the Odoo Product module to compute and display the
379374
main Vendor of each products. The main vendor is the first vendor in the
380375
vendors list.</p>
@@ -392,29 +387,29 @@ <h1>Product Main Vendor</h1>
392387
</ul>
393388
</div>
394389
<div class="section" id="bug-tracker">
395-
<h2><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h2>
390+
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
396391
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/purchase-workflow/issues">GitHub Issues</a>.
397392
In case of trouble, please check there if your issue has already been reported.
398393
If you spotted it first, help us to smash it by providing a detailed and welcomed
399394
<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>
400395
<p>Do not contact contributors directly about support or help with technical issues.</p>
401396
</div>
402397
<div class="section" id="credits">
403-
<h2><a class="toc-backref" href="#toc-entry-2">Credits</a></h2>
398+
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
404399
<div class="section" id="authors">
405-
<h3><a class="toc-backref" href="#toc-entry-3">Authors</a></h3>
400+
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
406401
<ul class="simple">
407402
<li>GRAP</li>
408403
</ul>
409404
</div>
410405
<div class="section" id="contributors">
411-
<h3><a class="toc-backref" href="#toc-entry-4">Contributors</a></h3>
406+
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
412407
<ul class="simple">
413408
<li>Quentin Dupont (<a class="reference external" href="mailto:quentin.dupont&#64;grap.coop">quentin.dupont&#64;grap.coop</a>)</li>
414409
</ul>
415410
</div>
416411
<div class="section" id="maintainers">
417-
<h3><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h3>
412+
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
418413
<p>This module is maintained by the OCA.</p>
419414
<a class="reference external image-reference" href="https://odoo-community.org">
420415
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -429,6 +424,5 @@ <h3><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h3>
429424
</div>
430425
</div>
431426
</div>
432-
</div>
433427
</body>
434428
</html>

product_main_seller/views/view_product_product.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
3-
Copyright (C) 2022 - Today: GRAP (http://www.grap.coop)
4-
@author: Quentin DUPONT (quentin.dupont@grap.coop)
3+
Copyright 2026 - Tecnativa
54
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
65
-->
76
<odoo>

0 commit comments

Comments
 (0)