Skip to content

Commit

Permalink
Merge PR OCA#3319 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Sep 25, 2024
2 parents 9b4e05c + aaf433e commit 3a200bb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pricelist_cache/models/product_pricelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,20 @@ def _get_parent_pricelists(self):
globally, and based on another pricelist
"""
self.ensure_one()
today = str(date.today())
self.flush()
query = """
SELECT base_pricelist_id
FROM product_pricelist_item
WHERE applied_on = '3_global'
AND active = TRUE
AND base = 'pricelist'
AND base_pricelist_id IS NOT NULL
AND pricelist_id = %(pricelist_id)s
AND (date_end IS NULL OR date_end >= %(today)s)
AND (date_start IS NULL OR date_start <= %(today)s)
"""
self.env.cr.execute(query, {"pricelist_id": self.id})
self.env.cr.execute(query, {"pricelist_id": self.id, "today": today})
return self.browse([row[0] for row in self.env.cr.fetchall()])

def _is_factor_pricelist(self):
Expand Down
2 changes: 1 addition & 1 deletion pricelist_cache/models/product_pricelist_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _update_pricelist_cache(self, pricelist_id, product_prices):
Args:
- pricelist: a product.pricelist record
- product_prices: A dictionnary,
- product_prices: A dictionary,
with product.product id as keys, and prices as values
"""
product_ids = list(product_prices.keys())
Expand Down
6 changes: 6 additions & 0 deletions pricelist_cache/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,9 @@ def assert_cache_not_available(self, lists):

def assert_cache_not_computed(self, lists):
self.assertFalse(any(lists.mapped("is_pricelist_cache_computed")))

def assert_price_equal(self, pricelist, product, expected_price):
self.cache_model.cron_reset_pricelist_cache()
self.cache_model.invalidate_cache(["price"])
prices = self.cache_model.get_cached_prices_for_pricelist(pricelist, product)
self.assertEqual(prices.price, expected_price)
14 changes: 14 additions & 0 deletions pricelist_cache/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ def test_get_parent_lists_tree(self):
list5_parents_tree = list5._get_parent_list_tree()
expected_list5_tree = expected_list3_tree | list5
self.assertEqual(list5_parents_tree, expected_list5_tree)

def test_get_parent_pricelists(self):
# test that list2 finds the parent pricelist list1
self.assertEqual(self.list2._get_parent_pricelists(), self.list1)
# test that list1 is not returned when item is expired
item = self.list2.item_ids.filtered(
lambda item: (
item.applied_on == "3_global"
and item.base == "pricelist"
and item.base_pricelist_id
)
)
item.write({"date_end": "2021-03-14"})
self.assertFalse(self.list2._get_parent_pricelists())
21 changes: 21 additions & 0 deletions pricelist_cache/tests/test_pricelist_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,24 @@ def test_reset_cache(self):
self.cache_model.cron_reset_pricelist_cache()
new_max_cache_id = max(self.cache_model.search([]).ids)
self.assertEqual(new_max_cache_id, old_max_cache_id)

@freeze_time("2021-03-15")
def test_pricelist_price_item_expiry(self):
current_prices = self.cache_model.get_cached_prices_for_pricelist(
self.list1, self.p6
)
self.assert_price_equal(self.list1, self.p6, current_prices.price)
# expire the product specific pricelist item
item = self.env["product.pricelist.item"].search(
[
("product_id", "=", self.p6.id),
("pricelist_id", "=", self.list1.id),
]
)
item.write({"date_end": "2021-03-14"})
self.assert_price_equal(self.list1, self.p6, 100)
# next expire all of the pricelist items
items = self.list1.item_ids
items.write({"date_end": "2021-03-14"})
# expecting the product list price when all pricelist items are expired
self.assert_price_equal(self.list1, self.p6, self.p6.list_price)

0 comments on commit 3a200bb

Please sign in to comment.