def test_create_order_with_gift_card_partial_use( checkout_with_item, gift_card_used, customer_user, shipping_method ): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() price_without_gift_card = calculations.checkout_total(checkout) gift_card_balance_before_order = gift_card_used.current_balance_amount checkout.gift_cards.add(gift_card_used) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data( checkout=checkout, tracking_code="tracking_code", discounts=None ), user=customer_user, redirect_url="https://www.example.com", ) gift_card_used.refresh_from_db() expected_old_balance = ( price_without_gift_card.gross.amount + gift_card_used.current_balance_amount ) assert order.gift_cards.count() > 0 assert order.total == zero_taxed_money() assert gift_card_balance_before_order == expected_old_balance
def test_create_order_with_gift_card_partial_use(checkout_with_item, gift_card_used, customer_user, shipping_method): checkout = checkout_with_item checkout.user = customer_user checkout.billing_address = customer_user.default_billing_address checkout.shipping_address = customer_user.default_billing_address checkout.shipping_method = shipping_method checkout.save() checkout_total = checkout.get_total() price_without_gift_card = TaxedMoney(net=checkout_total, gross=checkout_total) gift_card_balance_before_order = gift_card_used.current_balance checkout.gift_cards.add(gift_card_used) checkout.save() order = create_order( checkout=checkout, order_data=prepare_order_data(checkout=checkout, tracking_code="tracking_code", discounts=None), user=customer_user, ) gift_card_used.refresh_from_db() assert order.gift_cards.count() > 0 assert order.total == zero_taxed_money() assert (gift_card_balance_before_order == ( price_without_gift_card + gift_card_used.current_balance).gross.amount)
def test_calculate_order_shipping_for_order_without_shipping( vatlayer, order_line, shipping_zone, site_settings): manager = get_plugins_manager( plugins=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"]) order = order_line.order order.shipping_method = None order.save() price = manager.calculate_order_shipping(order) assert price == zero_taxed_money(order.currency)
def test_view_order_shipping_remove(admin_client, draft_order): url = reverse("dashboard:order-shipping-remove", kwargs={"order_pk": draft_order.pk}) data = {"csrfmiddlewaretoken": "hello"} response = admin_client.post(url, data) assert response.status_code == 302 redirect_url = reverse("dashboard:order-details", kwargs={"order_pk": draft_order.pk}) assert get_redirect_location(response) == redirect_url draft_order.refresh_from_db() assert not draft_order.shipping_method assert not draft_order.shipping_method_name assert draft_order.shipping_price == zero_taxed_money()
def test_get_checkout_context(checkout_with_voucher): line_price = TaxedMoney(net=Money("30.00", "USD"), gross=Money("30.00", "USD")) expected_data = { "checkout": checkout_with_voucher, "checkout_are_taxes_handled": taxes_are_enabled(), "checkout_lines": [(checkout_with_voucher.lines.first(), line_price)], "checkout_shipping_price": zero_taxed_money(), "checkout_subtotal": line_price, "checkout_total": line_price - checkout_with_voucher.discount_amount, "shipping_required": checkout_with_voucher.is_shipping_required(), "total_with_shipping": TaxedMoneyRange(start=line_price, stop=line_price), } data = get_checkout_context(checkout_with_voucher, discounts=None) assert data == expected_data
def calculate_order_shipping(self, order: "Order", previous_value: TaxedMoney) -> TaxedMoney: if self._skip_plugin(previous_value): return previous_value if not charge_taxes_on_shipping(): return previous_value if not _validate_order(order): return zero_taxed_money(order.total.currency) taxes_data = get_order_tax_data(order, self.config, False) tax_lines = taxes_data.get("TransactionTaxes", []) if not tax_lines: return previous_value currency = order.currency return self._calculate_checkout_shipping(currency, tax_lines, previous_value)
def calculate_order_line_total( self, order: "Order", order_line: "OrderLine", variant: "ProductVariant", product: "Product", previous_value: TaxedMoney, ) -> TaxedMoney: if self._skip_plugin(previous_value): return previous_value if not product.charge_taxes: return previous_value if not _validate_order(order): return zero_taxed_money(order.total.currency) taxes_data = self._get_order_tax_data(order, previous_value) return self._calculate_line_total_price(taxes_data, order_line.id, previous_value)
def calculate_checkout_total( self, checkout: "Checkout", lines: Iterable["CheckoutLine"], discounts: Iterable[DiscountInfo], previous_value: TaxedMoney, ) -> TaxedMoney: if self._skip_plugin(previous_value): logger.debug("Skip Plugin in Calculate Checkout Total") return previous_value checkout_total = previous_value if not _validate_checkout(checkout, lines): logger.debug("Checkout Invalid in Calculate Checkout Total") return checkout_total response = get_checkout_tax_data(checkout, discounts, self.config) if not response or "Errors found" in response["Status"]: return checkout_total if len(response["TransactionTaxes"]) == 0: raise TaxError("ATE did not return TransactionTaxes") currency = checkout.currency tax = Money(Decimal(response.get("TotalTaxAmount", 0.0)), currency) net = checkout_total.net total_gross = net + tax taxed_total = quantize_price(TaxedMoney(net=net, gross=total_gross), currency) total = self._append_prices_of_not_taxed_lines( taxed_total, lines, discounts, ) voucher_value = checkout.discount if voucher_value: total -= voucher_value return max(total, zero_taxed_money(total.currency))
def calculate_checkout_total( self, checkout_info: "CheckoutInfo", lines: Iterable["CheckoutLineInfo"], address: Optional["Address"], discounts: Iterable[DiscountInfo], previous_value: TaxedMoney, ) -> TaxedMoney: if self._skip_plugin(previous_value): logger.debug("Skip Plugin in Calculate Checkout Total") return previous_value checkout_total = previous_value if not _validate_checkout(checkout_info, lines): logger.debug("Checkout Invalid in Calculate Checkout Total") return checkout_total checkout = checkout_info.checkout response = get_checkout_tax_data(checkout_info, lines, discounts, self.config) if not response or "Errors found" in response["Status"]: return checkout_total currency = checkout.currency tax = Money(Decimal(response.get("TotalTaxAmount", 0.0)), currency) net = checkout_total.net gross = net + tax taxed_total = quantize_price(TaxedMoney(net=net, gross=gross), currency) total = self._append_prices_of_not_taxed_lines( taxed_total, lines, checkout_info.channel, discounts, ) voucher_value = checkout.discount if voucher_value: total -= voucher_value return max(total, zero_taxed_money(total.currency))
def calculate_order_line_total( self, order: "Order", order_line: "OrderLine", variant: "ProductVariant", product: "Product", previous_value: OrderTaxedPricesData, ) -> OrderTaxedPricesData: if self._skip_plugin(previous_value): return previous_value if not product.charge_taxes: return previous_value if not _validate_order(order): zero_money = zero_taxed_money(order.currency) return OrderTaxedPricesData(price_with_discounts=zero_money, undiscounted_price=zero_money) taxes_data = self._get_order_tax_data(order, previous_value) return self._calculate_order_line_total_price(taxes_data, order, order_line.id, previous_value)