Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shipment] Applying discounts and taxes for each shipment #12282

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Feature: Seeing aggregated discounts of an order
Then the order's items total should be "$280.00"
And the order's shipping promotion should be "-$5.00"
And the order's promotion total should be "-$20.00"
And there should be a shipping charge "DHL $10.00"
And there should be a shipping charge "$10.00" for "DHL" method
And the order's shipping total should be "$5.00"
And the order's total should be "$285.00"

Expand All @@ -38,6 +38,6 @@ Feature: Seeing aggregated discounts of an order
When I view the summary of the order "#00000006"
Then the order's items total should be "$810.00"
And the order's shipping promotion should be "-$5.00"
And there should be a shipping charge "DHL $10.00"
And there should be a shipping charge "$10.00" for "DHL" method
And the order's shipping total should be "$5.00"
And the order's total should be "$815.00"
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Feature: Seeing aggregated taxes of an order
Given the customer bought a single "Composite bow"
And the customer chose "DHL" shipping method to "United States" with "Offline" payment
When I view the summary of the order "#00000001"
Then there should be a shipping charge "DHL $10.00"
And there should be a shipping tax "$2.30"
Then there should be a shipping charge "$10.00" for "DHL" method
And there should be a shipping tax "$2.30" for "DHL" method
And the order's shipping total should be "$12.30"
And the order's tax total should be "$25.30"
And the order's total should be "$135.30"
Expand All @@ -37,8 +37,8 @@ Feature: Seeing aggregated taxes of an order
And the customer bought a "Claymore" and a "Bastard sword"
And the customer chose "DHL" shipping method to "United States" with "Offline" payment
When I view the summary of the order "#00000001"
Then there should be a shipping charge "DHL $10.00"
And there should be a shipping tax "$2.30"
Then there should be a shipping charge "$10.00" for "DHL" method
And there should be a shipping tax "$2.30" for "DHL" method
And the order's shipping total should be "$12.30"
And the order's tax total should be "$45.30"
And the order's total should be "$355.30"
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Feature: Seeing shipping fees of an order
When I view the summary of the order "#00000666"
Then the product named "Angel T-Shirt" should be in the items list
And the order's items total should be "$39.00"
And there should be a shipping charge "Free $0.00"
And there should be a shipping charge "$0.00" for "Free" method
And the order's shipping total should be "$0.00"
And the order's total should be "$39.00"

Expand All @@ -30,6 +30,6 @@ Feature: Seeing shipping fees of an order
When I view the summary of the order "#00000666"
Then the product named "Angel T-Shirt" should be in the items list
And the order's items total should be "$39.00"
And there should be a shipping charge "DHL $10.00"
And there should be a shipping charge "$10.00" for "DHL" method
And the order's shipping total should be "$10.00"
And the order's total should be "$49.00"
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Feature: Seeing taxes of an order
And the customer chose "DHL" shipping method to "United States" with "Offline" payment
When I view the summary of the order "#00000666"
Then the order's items total should be "$172.20"
And there should be a shipping charge "DHL $10.00"
And there should be a shipping tax "$2.30"
And there should be a shipping charge "$10.00" for "DHL" method
And there should be a shipping tax "$2.30" for "DHL" method
And the order's shipping total should be "$12.30"
And the order's tax total should be "$34.50"
And the order's total should be "$184.50"
Expand All @@ -35,8 +35,8 @@ Feature: Seeing taxes of an order
And the order "#00000666" is already paid
When I view the summary of the order "#00000666"
Then the order's items total should be "$172.20"
And there should be a shipping charge "DHL $10.00"
And there should be a shipping tax "$2.30"
And there should be a shipping charge "$10.00" for "DHL" method
And there should be a shipping tax "$2.30" for "DHL" method
And the order's shipping total should be "$12.30"
And the order's tax total should be "$34.50"
And the order's total should be "$184.50"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@managing_orders
Feature: Seeing shipping total with applied promotion and taxes
In order to get to know the cost of shipping
As an Administrator
I want to be able to see shipping total

Background:
Given the store operates on a single channel in "United States"
And the store has "VAT" tax rate of 23% for "Hogwart stuff" within the "US" zone
And the store has "Shipping VAT" tax rate of 23% for "Shipping Services" within the "US" zone
And the store has a product "Gryffindor scarf" priced at "$100.00"
And it belongs to "Hogwart stuff" tax category
And the store has "Owl post" shipping method with "$10.00" fee within the "US" zone
And shipping method "Owl post" belongs to "Shipping Services" tax category
And there is a promotion "50% shipping discount"
And it gives "50%" discount on shipping to every order
And the store allows paying offline
And there is a customer "fleur@delacour.com" that placed an order "#00000777"
And the customer bought a single "Gryffindor scarf"
And I am logged in as an administrator

@ui
Scenario: Seeing taxes of order items and shipping with applied promotion
Given the customer chose "Owl post" shipping method to "United States" with "Offline" payment
When I view the summary of the order "#00000777"
Then the order's items total should be "$123.00"
And there should be a shipping charge "$10.00" for "Owl post" method
And there should be a shipping tax "$1.15" for "Owl post" method
And the order's shipping total should be "$6.15"
And the order's tax total should be "$24.15"
And the order's total should be "$129.15"

@ui @domain
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this scenario should have @ui tag, because multiple shipments in Sylius could be used in background and displayed in admin panel, but there is no possibility to create more than one shipment during our checkout for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Domain test is really nice, but I would leave it with UI test as well. One cannot create multiple shipments in pure Sylius, but it does not take much to change it.

Scenario: Seeing taxes of order items and multiple shipments with applied promotion
Given the store has "Pigeon post" shipping method with "$16.00" fee within the "US" zone
And shipping method "Pigeon post" belongs to "Shipping Services" tax category
And the customer chose "Owl post" shipping method to "United States"
And the customer bought another "Gryffindor scarf" with separate "Pigeon post" shipment
And the customer chose "Offline" payment
When I view the summary of the order "#00000777"
Then the order's items total should be "$246.00"
And there should be a shipping charge "$10.00" for "Owl post" method
And there should be a shipping charge "$16.00" for "Pigeon post" method
And there should be a shipping tax "$1.15" for "Owl post" method
And there should be a shipping tax "$1.84" for "Pigeon post" method
And the order's shipping total should be "$15.99"
And the order's tax total should be "$48.99"
And the order's total should be "$261.99"
72 changes: 72 additions & 0 deletions src/Sylius/Behat/Context/Domain/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Behat\Behat\Context\Context;
use Doctrine\Persistence\ObjectManager;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand Down Expand Up @@ -90,6 +91,14 @@ public function iDeleteTheOrder(OrderInterface $order)
$this->orderRepository->remove($order);
}

/**
* @When I view the summary of the order :order
*/
public function iViewTheSummaryOfTheOrder(OrderInterface $order): void
{
$this->sharedStorage->set('order', $order);
}

/**
* @Then this order should not exist in the registry
*/
Expand Down Expand Up @@ -161,4 +170,67 @@ public function thisOrderShouldNotBeCancelled(OrderInterface $order)
{
Assert::notSame($order->getState(), OrderInterface::STATE_CANCELLED);
}
/**
* @Then /^(the order)'s items total should be ("[^"]+")$/
*/
public function theOrdersItemsTotalShouldBe(OrderInterface $order, int $itemsTotal): void
{
Assert::same($order->getItemsTotal(), $itemsTotal);
}

/**
* @Then /^there should be a shipping charge ("[^"]+") for "([^"]+)" method$/
*/
public function thereShouldBeAShippingChargeForMethod(int $shippingCharge, string $shippingMethodName): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');
foreach ($order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT) as $adjustment) {
if ($adjustment->getAmount() === $shippingCharge && $adjustment->getDetails()['shippingMethodName'] === $shippingMethodName) {
return;
}
}

throw new \DomainException('The given order has no shipping adjustment with proper amount and method');
}

/**
* @Then /^there should be a shipping tax ("[^"]+") for "([^"]+)" method$/
*/
public function thereShouldBeAShippingTaxForMethod(int $shippingTax, string $shippingMethodName): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');
foreach ($order->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) as $adjustment) {
if ($adjustment->getAmount() === $shippingTax && $adjustment->getDetails()['shippingMethodName'] === $shippingMethodName) {
return;
}
}

throw new \DomainException('The given order has no shipping adjustment with proper amount and method');
}

/**
* @Then /^(the order)'s shipping total should be ("[^"]+")$/
*/
public function theOrdersShippingTotalShouldBe(OrderInterface $order, int $shippingTotal): void
{
Assert::same($order->getShippingTotal(), $shippingTotal);
}

/**
* @Then /^(the order)'s tax total should be ("[^"]+")$/
*/
public function theOrdersTaxTotalShouldBe(OrderInterface $order, int $taxTotal): void
{
Assert::same($order->getTaxTotal(), $taxTotal);
}

/**
* @Then /^(the order)'s total should be ("[^"]+")$/
*/
public function theOrdersTotalShouldBe(OrderInterface $order, int $total): void
{
Assert::same($order->getTotal(), $total);
}
}
68 changes: 67 additions & 1 deletion src/Sylius/Behat/Context/Setup/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\PromotionCouponInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
Expand Down Expand Up @@ -62,6 +63,9 @@ final class OrderContext implements Context
/** @var FactoryInterface */
private $orderItemFactory;

/** @var FactoryInterface */
private $shipmentFactory;

/** @var StateMachineFactoryInterface */
private $stateMachineFactory;

Expand Down Expand Up @@ -95,6 +99,7 @@ public function __construct(
FactoryInterface $addressFactory,
FactoryInterface $customerFactory,
FactoryInterface $orderItemFactory,
FactoryInterface $shipmentFactory,
StateMachineFactoryInterface $stateMachineFactory,
RepositoryInterface $countryRepository,
RepositoryInterface $customerRepository,
Expand All @@ -110,6 +115,7 @@ public function __construct(
$this->addressFactory = $addressFactory;
$this->customerFactory = $customerFactory;
$this->orderItemFactory = $orderItemFactory;
$this->shipmentFactory = $shipmentFactory;
$this->stateMachineFactory = $stateMachineFactory;
$this->countryRepository = $countryRepository;
$this->customerRepository = $customerRepository;
Expand Down Expand Up @@ -314,6 +320,27 @@ public function theCustomerChoseShippingToWithPayment(
$this->objectManager->flush();
}

/**
* @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+")$/
*/
public function theCustomerChoseShippingTo(ShippingMethodInterface $shippingMethod, AddressInterface $address): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');

$order->setShippingAddress($address);
$order->setBillingAddress(clone $address);

$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_ADDRESS);

foreach ($order->getShipments() as $shipment) {
$shipment->setMethod($shippingMethod);
}
$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);

$this->objectManager->flush();
}

/**
* @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/
* @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/
Expand All @@ -326,6 +353,7 @@ public function theCustomerChoseShippingWithPayment(
$order = $this->sharedStorage->get('order');

$this->proceedSelectingShippingAndPaymentMethod($order, $shippingMethod, $paymentMethod);
$this->completeCheckout($order);

$this->objectManager->flush();
}
Expand Down Expand Up @@ -380,6 +408,27 @@ public function theCustomerBoughtSingleProduct(ProductInterface $product, ?Chann
$this->objectManager->flush();
}

/**
* @Given the customer bought another :product with separate :shippingMethod shipment
*/
public function theCustomerBoughtAnotherProductWithSeparateShipment(
ProductInterface $product,
ShippingMethodInterface $shippingMethod
): void {
$this->addProductVariantToOrder($this->variantResolver->getVariant($product), 1);

/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');

/** @var ShipmentInterface $shipment */
$shipment = $this->shipmentFactory->createNew();
$shipment->setMethod($shippingMethod);
$shipment->setOrder($order);
$order->addShipment($shipment);

$this->objectManager->flush();
}

/**
* @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/
* @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/
Expand Down Expand Up @@ -744,6 +793,18 @@ public function theOrderHasBeenPlacedInLocale(OrderInterface $order, string $loc
$this->objectManager->flush();
}

/**
* @Given the customer completed the order
*/
public function theCustomerCompletedTheOrder(): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');
$this->completeCheckout($order);

$this->objectManager->flush();
}

/**
* @param string $transition
*/
Expand Down Expand Up @@ -922,6 +983,12 @@ private function checkoutUsing(
$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_ADDRESS);

$this->proceedSelectingShippingAndPaymentMethod($order, $shippingMethod, $paymentMethod);
$this->completeCheckout($order);
}

private function completeCheckout(OrderInterface $order)
{
$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_COMPLETE);
}

private function createShippingPaymentMethodsAndAddress(): void
Expand Down Expand Up @@ -957,7 +1024,6 @@ private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order,
$payment->setMethod($paymentMethod);

$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
$this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_COMPLETE);
}

/**
Expand Down
Loading