def setUp(self): self.user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******') self.product = f.ProductFactory(name='product1', price=2, description='the best you can have!') # Move products to the warehouse self.product.move(get_lost_and_found(), get_storage(), quantity=5) self.product.move(get_lost_and_found(), get_storage(), quantity=10)
def create_stock_and_set_price(sender, instance, *args, **kwargs): composite = instance.composite composite_price = 0 for cps in composite.product_sets.all(): composite_price += (cps.product.price.amount * cps.quantity) composite.price = composite_price composite.save() from bazaar.warehouse.locations import get_storage from bazaar.warehouse.models import Stock location = get_storage() stock, created = Stock.objects.get_or_create(product=composite, location=location) if created: product_quantity = api.get_storage_quantity(instance.product) product_price = api.get_storage_price(instance.product) composite_quantity = product_quantity // instance.quantity composite_price = product_price * instance.quantity stock.quantity = composite_quantity stock.unit_price = composite_price stock.save() else: quantities = [] unit_price = 0 for ps in composite.product_sets.all(): product_quantity = api.get_storage_quantity(ps.product) product_price = api.get_storage_price(ps.product) quantities.append(product_quantity // ps.quantity) unit_price = unit_price + (product_price.amount * ps.quantity) if stock.unit_price != unit_price: stock.unit_price = unit_price if min(quantities) != api.get_storage_quantity(composite): stock.quantity = min(quantities) stock.save()
def test_cost_sort(self): """ Test that sort by cost works correctly """ product2 = f.ProductFactory(name='product2', price=1, description='the best you can have!') api.move(get_lost_and_found(), get_storage(), product2, 1, 5000) self.client.login(username=self.user.username, password='******') response = self.client.get("/products/?&order_by=purchase_price") self.assertEqual(response.status_code, status.HTTP_200_OK) products = response.context_data['product_list'] self.assertEqual(products.count(), 2) self.assertEqual(products[0].name, 'product1')
def get_queryset(self): qs = super(ProductListView, self).get_queryset() location_storage = get_storage() qs = qs.extra( select=SortedDict([ ("stock", "SELECT SUM(quantity) " "FROM warehouse_stock " "WHERE warehouse_stock.product_id = goods_product.id " "AND warehouse_stock.location_id = %s"), ("purchase_price", "SELECT SUM(unit_price) " "FROM warehouse_stock " "WHERE warehouse_stock.product_id = goods_product.id " "AND warehouse_stock.location_id = %s"), ]), select_params=( location_storage.id, location_storage.id, ) ) return qs