Skip to content

Commit

Permalink
Apply filter on locations for variants quantities
Browse files Browse the repository at this point in the history
It was only applied on export of template quantities.
  • Loading branch information
guewen committed Aug 21, 2017
1 parent 22926c7 commit 77d406b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
26 changes: 26 additions & 0 deletions connector_prestashop/models/prestashop_backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
27 changes: 21 additions & 6 deletions connector_prestashop/models/product_product/common.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand Down
27 changes: 2 additions & 25 deletions connector_prestashop/models/product_template/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 77d406b

Please sign in to comment.