From 38df5fb1fbaaed3c46325e0fa48e6d46b218eb71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 13 Jan 2016 21:02:12 +0100 Subject: [PATCH] Write the right cost to the right product in recompute_cost_price As some products can be skipped if they are services, the zip loop can be shifted. So it is replaced by a dictionary which have also the advantage to group the write of the same cost. Also if all the products are services, the call to write is empty which is not allowed. issue5218 review22681002 --- product.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/product.py b/product.py index 7138a029ea..17e0d6496f 100644 --- a/product.py +++ b/product.py @@ -2,6 +2,7 @@ # this repository contains the full copyright notices and license terms. import datetime from decimal import Decimal +from collections import defaultdict from sql import Literal, Null from sql.aggregate import Max @@ -188,28 +189,30 @@ class Product(object, StockMixin): pool = Pool() Template = pool.get('product.template') - costs = [] - for product in products: - if product.type == 'service': - continue - costs.append(getattr(product, 'recompute_cost_price_%s' % - product.cost_price_method)()) - if hasattr(cls, 'cost_price'): digits = cls.cost_price.digits write = cls.write - records = products + record = lambda p: p else: digits = Template.cost_price.digits write = Template.write - records = [p.template for p in products] + record = lambda p: p.template - costs = [c.quantize( - Decimal(str(10.0 ** -digits[1]))) for c in costs] + costs = defaultdict(list) + for product in products: + if product.type == 'service': + continue + cost = getattr(product, + 'recompute_cost_price_%s' % product.cost_price_method)() + cost = cost.quantize(Decimal(str(10.0 ** -digits[1]))) + costs[cost].append(record(product)) + + if not costs: + return to_write = [] - for record, cost in zip(records, costs): - to_write.append([record]) + for cost, records in costs.iteritems(): + to_write.append(records) to_write.append({'cost_price': cost}) # Enforce check access for account_stock*