From 77d406b249a828995f41e5f67c68d5578546212b Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Wed, 24 May 2017 08:47:02 +0200 Subject: [PATCH] Apply filter on locations for variants quantities It was only applied on export of template quantities. --- .../models/prestashop_backend/common.py | 26 ++++++++++++++++++ .../models/product_product/common.py | 27 ++++++++++++++----- .../models/product_template/common.py | 27 ++----------------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/connector_prestashop/models/prestashop_backend/common.py b/connector_prestashop/models/prestashop_backend/common.py index 643a40f1f..c8932f19b 100644 --- a/connector_prestashop/models/prestashop_backend/common.py +++ b/connector_prestashop/models/prestashop_backend/common.py @@ -302,6 +302,32 @@ def import_record(self, model_name, ext_id): import_record(session, model_name, self.id, ext_id) return True + @api.multi + def _get_locations_for_stock_quantities(self): + root_location = (self.stock_location_id or + self.warehouse_id.lot_stock_id) + locations = self.env['stock.location'].search([ + ('id', 'child_of', root_location.id), + ('prestashop_synchronized', '=', True), + ('usage', '=', 'internal'), + ]) + # if we choosed a location but none where flagged + # 'prestashop_synchronized', consider we want all of them in the tree + if not locations: + locations = self.env['stock.location'].search([ + ('id', 'child_of', root_location.id), + ('usage', '=', 'internal'), + ]) + if not locations: + # we must not pass an empty location or we would have the + # stock for every warehouse, which is the last thing we + # expect + raise exceptions.UserError( + _('No internal location found to compute the product ' + 'quantity.') + ) + return locations + class PrestashopShopGroup(models.Model): _name = 'prestashop.shop.group' diff --git a/connector_prestashop/models/product_product/common.py b/connector_prestashop/models/product_product/common.py index a3ba4b272..b50d373bf 100644 --- a/connector_prestashop/models/product_product/common.py +++ b/connector_prestashop/models/product_product/common.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from collections import defaultdict from openerp import api, fields, models from openerp.addons.decimal_precision import decimal_precision as dp @@ -162,14 +163,28 @@ class PrestashopProductCombination(models.Model): @api.multi def recompute_prestashop_qty(self): - for product_binding in self: - if product_binding.quantity != product_binding.qty_available: - product_binding.quantity = product_binding.qty_available + # group products by backend + backends = defaultdict(self.browse) + for product in self: + backends[product.backend_id] |= product + + for backend, products in backends.iteritems(): + products._recompute_prestashop_qty_backend(backend) return True - @api.model - def _prestashop_qty(self, product): - return product.qty_available + @api.multi + def _recompute_prestashop_qty_backend(self, backend): + locations = backend._get_locations_for_stock_quantities() + self_loc = self.with_context(location=locations.ids, + compute_child=False) + for product_binding in self_loc: + new_qty = product_binding._prestashop_qty() + if product_binding.quantity != new_qty: + product_binding.quantity = new_qty + return True + + def _prestashop_qty(self): + return self.qty_available class ProductAttribute(models.Model): diff --git a/connector_prestashop/models/product_template/common.py b/connector_prestashop/models/product_template/common.py index 1f8c6bde2..ac24146e9 100644 --- a/connector_prestashop/models/product_template/common.py +++ b/connector_prestashop/models/product_template/common.py @@ -3,7 +3,7 @@ from collections import defaultdict -from openerp import _, exceptions, api, fields, models +from openerp import api, fields, models from openerp.addons.decimal_precision import decimal_precision as dp from ...unit.backend_adapter import GenericAdapter @@ -111,8 +111,6 @@ class PrestashopProductTemplate(models.Model): digits_compute=dp.get_precision('Product Price'), ) - RECOMPUTE_QTY_STEP = 1000 - @api.multi def recompute_prestashop_qty(self): # group products by backend @@ -126,28 +124,7 @@ def recompute_prestashop_qty(self): @api.multi def _recompute_prestashop_qty_backend(self, backend): - root_location = (backend.stock_location_id or - backend.warehouse_id.lot_stock_id) - locations = self.env['stock.location'].search([ - ('id', 'child_of', root_location.id), - ('prestashop_synchronized', '=', True), - ('usage', '=', 'internal'), - ]) - # if we choosed a location but none where flagged - # 'prestashop_synchronized', consider we want all of them in the tree - if not locations: - locations = self.env['stock.location'].search([ - ('id', 'child_of', root_location.id), - ('usage', '=', 'internal'), - ]) - if not locations: - # we must not pass an empty location or we would have the - # stock for every warehouse, which is the last thing we - # expect - raise exceptions.UserError( - _('No internal location found to compute the product ' - 'quantity.') - ) + locations = backend._get_locations_for_stock_quantities() self_loc = self.with_context(location=locations.ids, compute_child=False) for product in self_loc: