def test_shipment_with_unshippable_products(): shop = get_default_shop() supplier = get_default_supplier() product = create_product( "unshippable", shop=shop, supplier=supplier, default_price=5.55) product.shipping_mode = ShippingMode.NOT_SHIPPED product.save() order = _get_order(shop, supplier, stocked=False) initial_product_line_count = order.lines.products().count() add_product_to_order(order, supplier, product, quantity=4, taxless_base_unit_price=3) order.cache_prices() order.check_all_verified() order.save() assert order.shipments.count() == 0 assert order.can_create_shipment() assert not order.can_set_complete() order.create_shipment_of_all_products(supplier=supplier) assert (order.lines.products().count() == initial_product_line_count + 1) assert order.shipments.count() == 1 assert ShipmentProduct.objects.filter(shipment__order_id=order.id).count() == initial_product_line_count assert order.shipping_status == ShippingStatus.FULLY_SHIPPED assert order.can_set_complete()
def test_complex_order_tax(include_taxes): tax = get_default_tax() quantities = [44, 23, 65] product = get_default_product() supplier = get_default_supplier() shop = get_default_shop() shop.prices_include_tax = include_taxes shop.save() order = create_empty_order(shop=shop) order.full_clean() order.save() pricing_context = get_pricing_module().get_context_from_data( shop=shop, customer=order.customer or AnonymousContact(), ) total_price = Decimal("0") price = Decimal("50") for quantity in quantities: total_price += quantity * price add_product_to_order(order, supplier, product, quantity, price, tax.rate, pricing_context) order.cache_prices() order.save() currency = "EUR" summary = order.get_tax_summary()[0] assert summary.tax_rate == tax.rate assert summary.based_on == Money(total_price, currency) assert summary.tax_amount == Money(total_price * tax.rate, currency) assert summary.taxful == summary.based_on + summary.tax_amount assert order.get_total_tax_amount() == Money(total_price * tax.rate, currency)
def test_computing_simple_product_relations(rf): shop = get_default_shop() supplier = get_default_supplier() product = create_product("simple-test-product", shop) related_product = create_product("simple-related-product", shop) quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] for quantity in quantities: order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=6, shop=shop) add_product_to_order(order, supplier, related_product, quantity=quantity, taxless_base_unit_price=6) assert ProductCrossSell.objects.count() == 0 add_bought_with_relations_for_product(product.pk) assert ProductCrossSell.objects.count() == 1 cross_sell_product = ProductCrossSell.objects.filter( product1=product).first() assert cross_sell_product.product2 == related_product assert cross_sell_product.weight == sum(quantities) add_bought_with_relations_for_product(related_product.id) assert ProductCrossSell.objects.count() == 2 cross_sell_product = ProductCrossSell.objects.filter( product1=related_product).first() assert cross_sell_product.product2 == product assert cross_sell_product.weight == len(quantities)
def _create_order(shop, customer): p1 = factories.create_product("test", shop=shop, supplier=factories.get_default_supplier()) order = factories.create_order_with_product(factories.get_default_product(), factories.get_default_supplier(), 2, 10, shop=shop) factories.add_product_to_order(order, factories.get_default_supplier(), p1, 2, 5) order.customer = customer order.save() return order
def _add_product_to_order(order, sku, quantity, shop, supplier): product = create_product( sku=sku, shop=shop, supplier=supplier, default_price=3.33, ) add_product_to_order(order, supplier, product, quantity=quantity, taxless_base_unit_price=1)
def _get_order(shop, supplier, stocked=False): order = create_empty_order(shop=shop) order.full_clean() order.save() for product_data in _get_product_data(stocked): quantity = product_data.pop("quantity") product = create_product( sku=product_data.pop("sku"), shop=shop, supplier=supplier, default_price=3.33, **product_data) add_product_to_order(order, supplier, product, quantity=quantity, taxless_base_unit_price=1) order.cache_prices() order.check_all_verified() order.save() return order
def _get_order(shop, supplier, has_price): order = create_empty_order(shop=shop) order.full_clean() order.save() for product_data in _get_product_data(has_price): quantity = product_data.pop("quantity") tax_rate = product_data.pop("tax_rate") product = create_product( sku=product_data.pop("sku"), shop=shop, supplier=supplier, **product_data) add_product_to_order( order, supplier, product, quantity=quantity, taxless_base_unit_price=product_data["default_price"], tax_rate=tax_rate) order.cache_prices() order.check_all_verified() order.save() return order
def test_order_with_only_unshippable_products(): shop = get_default_shop() supplier = get_default_supplier() order = create_empty_order(shop=shop) order.full_clean() order.save() product = create_product( "unshippable", shop=shop, supplier=supplier, default_price=5.55) product.shipping_mode = ShippingMode.NOT_SHIPPED product.save() add_product_to_order(order, supplier, product, quantity=4, taxless_base_unit_price=3) order.cache_prices() order.check_all_verified() order.save() assert not order.can_create_shipment() assert order.can_set_complete()
def test_rounding_with_taxes(prices): shop = get_default_shop() supplier = get_default_supplier() order = create_empty_order(shop=shop) order.save() product = create_product("test_sku", shop=shop, supplier=supplier) tax_rate = Decimal("0.22222") for x, price in enumerate(prices): add_product_to_order(order, supplier, product, quantity=Decimal("2.22"), taxless_base_unit_price=Decimal(price), tax_rate=tax_rate) order.cache_prices() for x, order_line in enumerate(order.lines.all().order_by("ordering")): # Check that total prices calculated from priceful parts still matches assert _get_taxless_price(order_line) == order_line.taxless_price assert _get_taxful_price(order_line) == order_line.taxful_price assert order_line.price == ( order_line.base_unit_price * order_line.quantity - order_line.discount_amount)
def test_product_relations_max_quantity(rf): shop = get_default_shop() supplier = get_default_supplier() product = create_product("simple-test-product", shop) quantities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] for i, quantity in enumerate(quantities): order = create_order_with_product(product, supplier, quantity=1, taxless_base_unit_price=6, shop=shop) add_product_to_order(order, supplier, create_product("product-%s" % i, shop), quantity=quantity, taxless_base_unit_price=6) assert ProductCrossSell.objects.count() == 0 add_bought_with_relations_for_product(product.pk, max_quantity=5) assert ProductCrossSell.objects.count() == 5 # Test that ordering is ok assert not ProductCrossSell.objects.filter(weight=1).exists() assert ProductCrossSell.objects.filter(weight=11).exists()
def test_get_best_selling_products(admin_user): shop1 = get_default_shop() shop2 = get_shop(True) person1 = create_random_person() person1.user = admin_user person1.save() supplier = create_simple_supplier("supplier1") client = _get_client(admin_user) # list best selling products response = client.get("/api/wshop/front/shop_products/best_selling/", {"shop": shop2.pk, "limit": 20}) assert response.status_code == status.HTTP_200_OK products = json.loads(response.content.decode("utf-8")) assert len(products["results"]) == 0 # THIS IS IMPORTANT! cache.clear() products = [create_product("Standard-%d" % x, supplier=supplier, shop=shop2) for x in range(10)] # create 1 product with 4 variations parent_product = create_product("ParentProduct1", supplier=supplier, shop=shop2) children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop2) for x in range(4)] for child in children: child.link_to_parent(parent_product) best_selling = defaultdict(int) # create orders with standard products for p_index in range(len(products)): order = create_empty_order(shop=shop2) order.save() qty = (len(products)-p_index) add_product_to_order(order, supplier, products[p_index], qty, Decimal(1.0)) order.create_shipment_of_all_products() order.status = OrderStatus.objects.get_default_complete() order.save(update_fields=("status",)) best_selling[products[p_index].id] = qty # create orders with variation products - the parent product is counted instead of its children for p_index in range(2): variation = random.choice(children) qty = 5 order = create_empty_order(shop=shop2) order.save() add_product_to_order(order, supplier, variation, qty, Decimal(1.0)) order.create_shipment_of_all_products() order.status = OrderStatus.objects.get_default_complete() order.save(update_fields=("status",)) best_selling[parent_product.id] = best_selling[parent_product.id] + qty # get the top 100 best selling products response = client.get("/api/wshop/front/shop_products/best_selling/", {"shop": shop2.pk, "limit": 100}) assert response.status_code == status.HTTP_200_OK products = json.loads(response.content.decode("utf-8")) assert len(products["results"]) == len(best_selling) # as we added less then 100, this must be true assert products["next"] is None # check the if all IDS are part of best selling for ix in range(len(products)): assert products["results"][ix]["product_id"] in best_selling.keys() # get the top 5 best selling products (we should get paginated results) response = client.get("/api/wshop/front/shop_products/best_selling/", {"shop": shop2.pk, "limit": 5}) assert response.status_code == status.HTTP_200_OK products = json.loads(response.content.decode("utf-8")) assert len(products["results"]) == 5 assert products["count"] == len(best_selling) assert products["next"] is not None sorted_best_selling_ids = [prod[0] for prod in sorted(best_selling.items(), key=lambda prod: -prod[1])][:5] # check the if all the 5 best sellers are part of best selling for ix in range(len(products)): assert products["results"][ix]["product_id"] in sorted_best_selling_ids