Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/AppBundle/Entity/WarehouseProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,12 @@ public function setQuantity($quantity)
return $this;
}

/**
* Decrease quantity by number to minimum 0
* @param $number
*/
public function decreaseQuantityBy($number)
{
$this->quantity = max($this->quantity - $number, 0);
}
}
33 changes: 33 additions & 0 deletions src/AppBundle/Facade/OrderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace AppBundle\Facade;
use AppBundle\Entity\Address;
use AppBundle\Entity\Cart;
use AppBundle\Entity\CartItem;
use AppBundle\Entity\Order;
use AppBundle\Entity\User;
use AppBundle\Entity\Warehouse;
use AppBundle\FormType\VO\OrderVO;
use AppBundle\Repository\AddressRepository;
use AppBundle\Repository\OrderRepository;
Expand All @@ -19,6 +21,8 @@
*/
class OrderFacade {

const DEFAULT_STOCK = 1;

/** @var OrderRepository */
private $orderRepository;

Expand All @@ -43,9 +47,15 @@ class OrderFacade {
/** @var CartItemFacade */
private $cartItemFacade;

/** @var WarehouseProductFacade */
private $warehouseProductFacade;

/** @var Swift_Mailer */
private $swiftMailer;

/** @var CartItem */
private $cartItem;

public function __construct(
EntityManager $entityManager,
OrderRepository $orderRepository,
Expand All @@ -55,6 +65,8 @@ public function __construct(
CartFacade $cartFacade,
AddressRepository $addressRepository,
CartItemFacade $cartItemFacade,
WarehouseProductFacade $warehouseProductFacade,
CartItem $cartItem,
Swift_Mailer $swiftMailer
) {
$this->entityManager = $entityManager;
Expand All @@ -65,6 +77,8 @@ public function __construct(
$this->cartFacade = $cartFacade;
$this->addressRepository = $addressRepository;
$this->cartItemFacade = $cartItemFacade;
$this->warehouseProductFacade = $warehouseProductFacade;
$this->cartItem = $cartItem;
$this->swiftMailer = $swiftMailer;
}

Expand Down Expand Up @@ -102,6 +116,8 @@ public function create(OrderVO $orderVO)

$order->setDeliveryWarehouse($warehouse);
} else {
$warehouse = $this->warehouseRepository->find(static::DEFAULT_STOCK);

if ($orderVO->getAddressId() === 0) {
$addressVO = $orderVO->getDelivery();

Expand All @@ -127,6 +143,8 @@ public function create(OrderVO $orderVO)
$this->entityManager->flush($order->getDeliveryAddress());
}

$this->decreaseStockItems($warehouse, $cart);

$this->entityManager->persist($order);
$this->entityManager->flush($order);

Expand All @@ -137,6 +155,21 @@ public function create(OrderVO $orderVO)
return $order;
}

public function decreaseStockItems(Warehouse $warehouse, Cart $cart)
{
$cartItems = $this->cartItemFacade->findByCart($cart);

foreach ($cartItems as $cartItem) {
$warehouseProduct = $this->warehouseProductFacade->findByProductAndWarehouse($cartItem->getProduct(), $warehouse);

$orderedCount = $cartItem->getQuantity();

$warehouseProduct->decreaseQuantityBy($orderedCount);

$this->entityManager->flush($warehouseProduct);
}
}

public function sendOrderEmail($order)
{
$cart = $order->getCart();
Expand Down
8 changes: 7 additions & 1 deletion src/AppBundle/Facade/WarehouseProductFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AppBundle\Entity\Product;
use AppBundle\Entity\Warehouse;
use AppBundle\Entity\WarehouseProduct;
use AppBundle\Repository\WarehouseProductRepository;

class WarehouseProductFacade
Expand All @@ -21,8 +22,13 @@ public function findByProduct(Product $product) {
]);
}

/**
* @param Product $product
* @param Warehouse $warehouse
* @return WarehouseProduct
*/
public function findByProductAndWarehouse(Product $product, Warehouse $warehouse) {
return $this->warehouseProductRepository->findBy([
return $this->warehouseProductRepository->findOneBy([
"product" => $product,
"warehouse" => $warehouse,
]);
Expand Down