diff --git a/location.py b/location.py index 443031b900..ddcb2a206e 100644 --- a/location.py +++ b/location.py @@ -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) diff --git a/product.py b/product.py index d2cd38fd7a..e40d2319fb 100644 --- a/product.py +++ b/product.py @@ -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