Skip to content

Commit

Permalink
product_main_supplierinfo: replace '_get_sellers' by std '_get_filter…
Browse files Browse the repository at this point in the history
…ed_sellers'

Co-authored-by: Do Anh Duy <[email protected]>
  • Loading branch information
sebalix and xaviedoanhduy committed Dec 11, 2024
1 parent aa7eed0 commit d797363
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions product_main_supplierinfo/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,18 @@ class ProductProduct(models.Model):
@api.depends_context("company")
def _compute_main_seller_id(self):
for product in self:
sellers = product._get_sellers()
product.main_seller_id = fields.first(sellers)

def _get_sellers(self):
"""Returns all available sellers of a product based on some constraints.
product.main_seller_id = fields.first(
product._get_filtered_sellers(quantity=None).sorted("price")
)

They are ordered and filtered like it is done in the standard 'product' addon.
"""
self.ensure_one()
all_sellers = self._prepare_sellers(False).filtered(
lambda s: not s.company_id or s.company_id.id == self.env.company.id
)
today = fields.Date.context_today(self)
sellers = all_sellers.filtered(
lambda s: (
(s.product_id == self or not s.product_id)
and (
(s.date_start <= today if s.date_start else True)
and (s.date_end >= today if s.date_end else True)
)
def _get_filtered_sellers(
self, partner_id=False, quantity=0.0, date=None, uom_id=False, params=False
):
res = super()._get_filtered_sellers(partner_id, quantity, date, uom_id, params)
if not res:
sellers_filtered = self._prepare_sellers(params)
sellers_filtered = sellers_filtered.filtered(
lambda s: not s.company_id or s.company_id.id == self.env.company.id
)
)
if not sellers:
sellers = all_sellers.filtered(lambda s: (s.product_id == self))
if not sellers:
sellers = all_sellers.filtered(lambda s: not s.product_id)
return sellers.sorted("price")
res = sellers_filtered.filtered(lambda s: s.product_id == self)
return res

0 comments on commit d797363

Please sign in to comment.