def decrease_stock(self, quantity, branch, type, object_id, cost_center=None): """When receiving a product, update the stock reference for the sold item this on a specific |branch|. Returns the stock item that was decreased. :param quantity: amount to decrease :param branch: a |branch| :param type: the type of the stock increase. One of the StockTransactionHistory.types :param object_id: the id of the object responsible for the transaction :param cost_center: the |costcenter| to which this stock decrease is related, if any """ # FIXME: Put this back once 1.6 is released #assert isinstance(type, int) if quantity <= 0: raise ValueError(_(u"quantity must be a positive number")) if branch is None: raise ValueError(u"branch cannot be None") stock_item = self.get_stock_item(branch) if stock_item is None or quantity > stock_item.quantity: raise StockError( _('Quantity to sell is greater than the available stock.')) old_quantity = stock_item.quantity stock_item.quantity -= quantity # We emptied the entire stock in all branches, we need to change # the status of the sellable to unavailable as we cannot sell # it anymore if not self.store.find(ProductStockItem, storable=self).sum(ProductStockItem.quantity): sellable = self.product.sellable if sellable: sellable.set_unavailable() stock_transaction = StockTransactionHistory( product_stock_item=stock_item, quantity=-quantity, stock_cost=stock_item.stock_cost, responsible=get_current_user(self.store), type=type, object_id=object_id, store=self.store) if cost_center is not None: cost_center.add_stock_transaction(stock_transaction) ProductStockUpdateEvent.emit(self.product, branch, old_quantity, stock_item.quantity) return stock_item
def increase_stock(self, quantity, branch, type, object_id, unit_cost=None, batch=None): """When receiving a product, update the stock reference for this new item on a specific |branch|. :param quantity: amount to increase :param branch: |branch| that stores the stock :param type: the type of the stock increase. One of the StockTransactionHistory.types :param object_id: the id of the object responsible for the transaction :param unit_cost: unit cost of the new stock or `None` :param batch: The batch of the storable. Should be not ``None`` if self.is_batch is ``True`` """ assert isinstance(type, int) if quantity <= 0: raise ValueError(_(u"quantity must be a positive number")) if branch is None: raise ValueError(u"branch cannot be None") stock_item = self.get_stock_item(branch, batch) # If the stock_item is missing create a new one if stock_item is None: store = self.store stock_item = ProductStockItem(store=store, storable=self, batch=batch, branch=store.fetch(branch)) # Unit cost must be updated here as # 1) we need the stock item which might not exist # 2) it needs to be updated before we change the quantity of the # stock item if unit_cost is not None: stock_item.update_cost(quantity, unit_cost) old_quantity = stock_item.quantity stock_item.quantity += quantity StockTransactionHistory(product_stock_item=stock_item, quantity=quantity, stock_cost=stock_item.stock_cost, responsible=get_current_user(self.store), type=type, object_id=object_id, store=self.store) ProductStockUpdateEvent.emit(self.product, branch, old_quantity, stock_item.quantity)
def decrease_stock(self, quantity, branch, type, object_id, cost_center=None, batch=None): """When receiving a product, update the stock reference for the sold item this on a specific |branch|. Returns the stock item that was decreased. :param quantity: amount to decrease :param branch: a |branch| :param type: the type of the stock increase. One of the StockTransactionHistory.types :param object_id: the id of the object responsible for the transaction :param cost_center: the |costcenter| to which this stock decrease is related, if any :param batch: The batch of the storable. Should be not ``None`` if self.is_batch is ``True`` """ # FIXME: Put this back once 1.6 is released # assert isinstance(type, int) if quantity <= 0: raise ValueError(_(u"quantity must be a positive number")) if branch is None: raise ValueError(u"branch cannot be None") stock_item = self.get_stock_item(branch, batch) if stock_item is None or quantity > stock_item.quantity: raise StockError( _('Quantity to sell is greater than the available stock.')) old_quantity = stock_item.quantity stock_item.quantity -= quantity stock_transaction = StockTransactionHistory( product_stock_item=stock_item, quantity=-quantity, stock_cost=stock_item.stock_cost, responsible=get_current_user(self.store), type=type, object_id=object_id, store=self.store) if cost_center is not None: cost_center.add_stock_transaction(stock_transaction) ProductStockUpdateEvent.emit(self.product, branch, old_quantity, stock_item.quantity) return stock_item
def increase_stock(self, quantity, branch, type, object_id, unit_cost=None): """When receiving a product, update the stock reference for this new item on a specific |branch|. :param quantity: amount to increase :param branch: |branch| that stores the stock :param type: the type of the stock increase. One of the StockTransactionHistory.types :param object_id: the id of the object responsible for the transaction :param unit_cost: unit cost of the new stock or `None` """ assert isinstance(type, int) if quantity <= 0: raise ValueError(_(u"quantity must be a positive number")) if branch is None: raise ValueError(u"branch cannot be None") stock_item = self.get_stock_item(branch) # If the stock_item is missing create a new one if stock_item is None: store = self.store stock_item = ProductStockItem( storable=self, branch=store.fetch(branch), store=store) # Unit cost must be updated here as # 1) we need the stock item which might not exist # 2) it needs to be updated before we change the quantity of the # stock item if unit_cost is not None: stock_item.update_cost(quantity, unit_cost) old_quantity = stock_item.quantity stock_item.quantity += quantity # If previously lacked quantity change the status of the sellable if not old_quantity: sellable = self.product.sellable if sellable: sellable.can_sell() StockTransactionHistory(product_stock_item=stock_item, quantity=quantity, stock_cost=stock_item.stock_cost, responsible=get_current_user(self.store), type=type, object_id=object_id, store=self.store) ProductStockUpdateEvent.emit(self.product, branch, old_quantity, stock_item.quantity)