def get_amount_stocked(self, user): """ Returns the amount of this product in the same location as the user. Caches the result for faster access later. """ amount = 0 track_inventory = Configuration.track_inventory() if not track_inventory: return 0 if self.part_type == "SERVICE" or not self.pk: return 0 cache_key = "product_%d_amount_stocked" % self.pk if cache.get(cache_key): return cache.get(cache_key) location = user.get_location() try: inventory = Inventory.objects.get(product=self, location=location) amount = inventory.amount_stocked except Inventory.DoesNotExist: pass cache.set(cache_key, amount) return amount
def track_inventory(self): if not Configuration.track_inventory(): return False if self.part_type == "SERVICE": return False return True
def sell(self, amount, location): """ Deduct product from inventory with specified location """ track_inventory = Configuration.track_inventory() if self.part_type == "SERVICE" or not track_inventory: return try: inventory = Inventory.objects.get(product=self, location=location) inventory.amount_stocked = inventory.amount_stocked - amount inventory.save() except Inventory.DoesNotExist: raise ValueError(_(u"Product %s not found in inventory.") % self.code)