def stockable(self): provider = IStock(self.context) raw = provider.stockable() result = [] if raw is None: result.append({ 'title': self.context.Title(), 'key': self.context.UID(), 'value': provider.stock() }) else: for items in raw: stockable = { 'objs': items, 'title': [], 'key': [], 'value': provider.variationStock(items) } for item in items: stockable['key'].append(item.UID()) stockable['title'].append(item.Title()) stockable['key'] = '_'.join(stockable['key']) stockable['title'] = ', '.join(stockable['title']) result.append(stockable) return result
def store(self): provider = IStock(self.context) stockable = self.stockable() for item in stockable: try: value = int(self.request.form.get(item['key'], 0)) if not item.has_key('objs'): provider.setStock(value) else: provider.setVariationStock(value, item['objs']) except: self.errors[item['key']] = _(u'There was an error when saving this stock entry') return len(self.errors) == 0
def products(self): if not hasattr(self, 'cart'): self.cart = IShoppingCart(self.context) order = None if self.request.SESSION.get(ORDER_SESSION_KEY, None) is not None: registry = IOrderRegistry(self.context) order = registry.getOrder(self.request.SESSION.get(ORDER_SESSION_KEY, 0)) if order is not None: increaseStockFromOrder(registry, order) products = self.cart.getProducts() uid_catalog = getToolByName(self.context, 'uid_catalog') for product in products: provider = IStock(uid_catalog(UID=product['uid'])[0].getObject()) if not len(product['variations']): product['stock'] = provider.stock() else: product['stock'] = provider.variationStock([uid_catalog(UID=v['uid'])[0].getObject() for v in product['variations']]) product['outofstock'] = product['amount'] > product['stock'] product['checkout_allowed'] = not product['outofstock'] or provider.outOfStockCheckoutAllowed() if order is not None: decreaseStockFromOrder(registry, order) return products
def decreaseStockFromOrder(registry, order): if not hasattr(order, 'stock_decreased'): order.stock_decreased = PersistentDict() uid_catalog = getToolByName(registry.context, 'uid_catalog') for product in order.products: stock = IStock(uid_catalog(UID=product[0])[0].getObject()) if len(product[5]): # we have variations variations = [v[0] for v in product[5]] stock.setVariationStock(stock.variationStock(variations)-int(product[3])+order.stock_decreased.get(product[0], 0), variations) else: stock.setStock(stock.stock()-int(product[3])+order.stock_decreased.get(product[0], 0)) order.stock_decreased[product[0]] = int(product[3])