Do not browse records at a datetime before they were created

If user tries to get the cost price of a product before it was created, it
should return None instead of raising an access error.

issue7334
review49281002
This commit is contained in:
Cédric Krier committed 2018-04-20 18:36:25 +02:00
1 parent fd622d7953
commit df8c27aba6
2 files changed
+20 -18

No files matched your search

+12 -11
View File
@@ -378,24 +378,25 @@ class Location(DeactivableMixin, ModelSQL, ModelView):
if not any(map(valid_context, ['product', 'product_template'])):
return cost_values
def get_record():
if trans_context.get('product') is not None:
return Product(trans_context['product'])
else:
return Template(trans_context['product_template'])
context = {}
if 'stock_date_end' in trans_context:
# Use the last cost_price of the day
context['_datetime'] = datetime.datetime.combine(
trans_context['stock_date_end'], datetime.time.max)
with Transaction().set_context(context):
if trans_context.get('product') is not None:
product = Product(trans_context['product'])
cost_price = product.cost_price
else:
template = Template(trans_context['product_template'])
cost_price = template.cost_price
# The date could be before the product creation
# or the template may have more than one product
if not isinstance(cost_price, Decimal):
record = get_record()
if record.create_date > context['_datetime']:
return cost_values
with Transaction().set_context(context):
cost_price = get_record().cost_price
# The template may have more than one product
if cost_price is not None:
for location in locations:
cost_values[location.id] = (
Decimal(str(location.quantity)) * cost_price)
+8 -7
View File
@@ -136,21 +136,22 @@ class Product(StockMixin, object):
@classmethod
def get_cost_value(cls, products, name):
cost_values = {}
cost_values = {p.id: None for p in products}
context = {}
trans_context = Transaction().context
if trans_context.get('stock_date_end'):
# Use the last cost_price of the day
context['_datetime'] = datetime.datetime.combine(
trans_context['stock_date_end'], datetime.time.max)
# The date could be before the product creation
products = [p for p in products
if p.create_date <= context['_datetime']]
with Transaction().set_context(context):
for product in cls.browse(products):
# The date could be before the product creation
if not isinstance(product.cost_price, Decimal):
cost_values[product.id] = None
else:
cost_values[product.id] = (Decimal(str(product.quantity))
* product.cost_price)
# The product may not have a cost price
if product.cost_price is not None:
cost_values[product.id] = (
Decimal(str(product.quantity)) * product.cost_price)
return cost_values
@classmethod