Skip to content

Instantly share code, notes, and snippets.

@mdjaman
Created August 14, 2018 23:29
Show Gist options
  • Save mdjaman/986dd20da518382f86aceb8f169c1801 to your computer and use it in GitHub Desktop.
Save mdjaman/986dd20da518382f86aceb8f169c1801 to your computer and use it in GitHub Desktop.
Update storeProduct level after an order
<?php
/**
* This file is part of the Inventory project
* Copyright (c) 2016
* @author Marcel Djaman <marceldjaman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customer\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use MdjamanCommon\Entity\BaseEntity;
use MdjamanCommon\Traits\BlameableEntity;
/**
* OrderDetails
*
* @ORM\Table(name="order_details")
* @ORM\Entity(repositoryClass="Customer\Repository\OrderDetailsRepository")
*/
class OrderDetails extends BaseEntity implements
OrderDetailsInterface,
RentItemInterface
{
use RentItemTrait;
use BlameableEntity;
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=36, options={"fixed":true}, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
* @JMS\Groups({"list", "details"})
*/
protected $id;
/**
* @var integer
*
* @ORM\Column(name="qty", type="bigint", nullable=false)
* @JMS\Groups({"list", "details"})
*/
protected $qty;
/**
* @var string
*
* @ORM\Column(name="unit_price", type="decimal", precision=12, scale=3, nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $unitPrice;
/**
* @var \DateTime
*
* @ORM\Column(name="expiry_date", type="datetime", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $expiryDate;
/**
* @var \DateTime
*
* @ORM\Column(name="delivery_date", type="datetime", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $deliveryDate;
/**
* @var float
*
* @ORM\Column(name="percentage_discount", type="float", precision=4, scale=2, nullable=true)
* @JMS\Groups({"details"})
*/
protected $percentageDiscount;
/**
* @var string
*
* @ORM\Column(name="details", type="text", nullable=true)
* @JMS\Groups({"details"})
*/
protected $details;
/**
* @var string
*
* @ORM\Column(name="status", type="string", length=30, nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $status;
/**
* @var Order
*
* @ORM\ManyToOne(targetEntity="Order", inversedBy="orderDetails")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="order_id", referencedColumnName="id")
* })
* @JMS\Groups({"list", "details"})
* @JMS\MaxDepth(1)
*/
protected $order;
/**
* @var \Product\Entity\Product
*
* @ORM\ManyToOne(targetEntity="Product\Entity\Product")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* })
* @JMS\Groups({"list", "details"})
* @JMS\MaxDepth(1)
*/
protected $product;
/**
* @ORM\OneToMany(targetEntity="Tax\Entity\OrderDetailsTax", mappedBy="orderDetails", cascade={"all"})
* @JMS\Groups({"list", "details"})
* @JMS\MaxDepth(3)
*/
protected $taxes;
/**
* @ORM\OneToMany(targetEntity="Customer\Entity\DeliveryOrderLines", mappedBy="orderDetails", cascade={"all"})
* @JMS\Groups({"details"})
* @JMS\MaxDepth(3)
*/
protected $deliveryOrderLines;
/**
* OrderDetails constructor.
*/
public function __construct()
{
parent::__construct();
$this->taxes = new \Doctrine\Common\Collections\ArrayCollection();
$this->deliveryOrderLines = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __clone()
{
$this->id = null;
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set qty
*
* @param integer $qty
*
* @return OrderDetails
*/
public function setQty($qty)
{
$this->qty = $qty;
return $this;
}
/**
* Get qty
*
* @return integer
*/
public function getQty()
{
return $this->qty;
}
/**
* Set unitPrice
*
* @param string $unitPrice
*
* @return OrderDetails
*/
public function setUnitPrice($unitPrice)
{
$this->unitPrice = $unitPrice;
return $this;
}
/**
* Get unitPrice
*
* @return string
*/
public function getUnitPrice()
{
return $this->unitPrice;
}
/**
* @param \DateTime $expiryDate
* @return $this
*/
public function setExpiryDate(\DateTime $expiryDate = null)
{
$this->expiryDate = $expiryDate;
return $this;
}
/**
* @return \DateTime
*/
public function getExpiryDate()
{
return $this->expiryDate;
}
/**
* Set deliveryDate
*
* @param \DateTime $deliveryDate
*
* @return OrderDetails
*/
public function setDeliveryDate(\DateTime $deliveryDate)
{
$this->deliveryDate = $deliveryDate;
return $this;
}
/**
* Get deliveryDate
*
* @return \DateTime
*/
public function getDeliveryDate()
{
return $this->deliveryDate;
}
/**
* Set percentageDiscount
*
* @param float $percentageDiscount
*
* @return OrderDetails
*/
public function setPercentageDiscount($percentageDiscount)
{
$this->percentageDiscount = $percentageDiscount;
return $this;
}
/**
* Get percentageDiscount
*
* @return float
*/
public function getPercentageDiscount()
{
return $this->percentageDiscount;
}
/**
* Set details
*
* @param string $details
*
* @return OrderDetails
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* @return string
*/
public function getDetails()
{
return $this->details;
}
/**
* Set status
*
* @param string $status
*
* @return OrderDetails
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Set order
*
* @param Order $order
*
* @return OrderDetails
*/
public function setOrder(Order $order)
{
$this->order = $order;
return $this;
}
/**
* Get order
*
* @return Order
*/
public function getOrder()
{
return $this->order;
}
/**
* Set product
*
* @param \Product\Entity\Product $product
*
* @return OrderDetails
*/
public function setProduct(\Product\Entity\Product $product)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \Product\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* @param Collection $taxes
* @return $this
*/
public function addTaxes($taxes)
{
foreach ($taxes as $item) {
if (!$this->taxes->contains($item)) {
$this->taxes->add($item);
$item->setOrderDetails($this);
}
}
return $this;
}
/**
* @param mixed $taxes
* @return $this
*/
public function removeTaxes($taxes)
{
foreach ($taxes as $item) {
if ($this->deliveryOrderLines->contains($item)) {
$this->deliveryOrderLines->removeElement($item);
}
}
return $this;
}
/**
* Get taxes
*
* @return Collection
*/
public function getTaxes()
{
return $this->taxes;
}
/**
* @param mixed $deliveryOrderLines
* @return $this
*/
public function addDeliveryOrderLines($deliveryOrderLines)
{
foreach ($deliveryOrderLines as $item) {
if (!$this->deliveryOrderLines->contains($item)) {
$this->deliveryOrderLines->add($item);
$item->setOrderDetails($this);
}
}
return $this;
}
/**
* @param mixed $deliveryOrderLines
* @return $this
*/
public function removeDeliveryOrderLines($deliveryOrderLines)
{
foreach ($deliveryOrderLines as $item) {
if ($this->deliveryOrderLines->contains($item)) {
$this->deliveryOrderLines->removeElement($item);
}
}
return $this;
}
/**
* Get DeliveryOrderLines
*
* @return Collection
*/
public function getDeliveryOrderLines()
{
return $this->deliveryOrderLines;
}
}
<?php
/**
* This file is part of the Inventory project
* Copyright (c) 2016
* @author Marcel Djaman <marceldjaman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customer\Listener;
use Customer\Definitions;
use Customer\Entity\OrderDetailsInterface;
use Customer\Events;
use Store\Entity\StoreProductInterface;
use Store\Entity\StoreProductLevelInterface;
use Store\Repository\StoreProductLevelRepositoryInterface;
use Store\Service\StoreProductLevelServiceInterface;
use Store\Service\StoreProductServiceInterface;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
/**
* Class OrderDetailsTransactionListener
* @author Marcel Djaman <marceldjaman@gmail.com>
* @package Customer\Listener
*/
class OrderDetailsTransactionListener implements ListenerAggregateInterface
{
/**
* @var array
*/
protected $listeners = [];
/**
* @var StoreProductServiceInterface
*/
protected $storeProductService;
/**
* @var StoreProductLevelServiceInterface
*/
protected $storeProductLevelService;
/**
* @var string
*/
protected $message = 'Historique stock du produit %s du Point de vente %s mise à jour à la suite de la commande %s';
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 90)
{
$this->listeners[] = $events->attach(Events::ORDER_DETAILS_TRANSACTION, array($this, 'onOrderDetailsTransaction'), $priority);
}
/**
* @inheritDoc
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
/**
* @param EventInterface $event
*/
public function onOrderDetailsTransaction(EventInterface $event)
{
/* @var $orderDetails OrderDetailsInterface */
$orderDetails = $event->getParam('saved');
if (!$orderDetails instanceof OrderDetailsInterface) {
return;
}
$storeProductService = $this->getStoreProductService();
$storeProductLevelService = $this->getStoreProductLevelService();
try {
$order = $orderDetails->getOrder();
// if order have not been marked as delivered we don't remove product from stock
if ($order->getDeliveryStatus() !== Definitions::DELIVERY_STATUS_DELIVERED) {
return;
}
$product = $orderDetails->getProduct();
$store = $order->getStore();
$criteria = [
'store' => $store->getId(),
'product' => $product->getId(),
];
if ($orderDetails->getExpiryDate() instanceof \DateTime) {
$criteria['expiryDate'] = $orderDetails->getExpiryDate();
}
/* @var $storeProduct StoreProductInterface */
$storeProduct = $storeProductService->findOneBy($criteria);
if (!$storeProduct) {
$storeProduct = $storeProductService->createEntity();
$storeProduct->setStore($store);
$storeProduct->setProduct($product);
$storeProduct->setQty(0);
$storeProduct->setReorderLevel($product->getReorderLevel());
$storeProduct->setReorderQty($product->getReorderQty());
$storeProductService->saveStoreProduct($storeProduct);
if ($storeProduct->getId() === null) {
throw new \Exception('Something went wrong while persisting StoreProduct!');
}
}
/* @var $storeProductLevelRepository StoreProductLevelRepositoryInterface */
$storeProductLevelRepository = $storeProductLevelService->getRepository();
$lastStoreProductLevel = $storeProductLevelRepository->getLatestFromStoreProduct($storeProduct->getId());
$qty = $orderDetails->getQty();
if (null !== $lastStoreProductLevel) {
$qty = $lastStoreProductLevel->getQty() - $orderDetails->getQty();
}
$message = sprintf(_($this->message),
$product->getName(),
$store->getName(),
$order->getCode()
);
/* @var $storeProductLevel StoreProductLevelInterface */
$storeProductLevel = $storeProductLevelService->createEntity();
$storeProductLevel->setQty($qty);
$storeProductLevel->setTakingDate(new \DateTime());
$storeProductLevel->setDetails($message);
$storeProductLevel->setStoreProduct($storeProduct);
$storeProductLevelService->saveStoreProductLevel($storeProductLevel);
if ($storeProductLevel->getId() === null) {
throw new \Exception('Something went wrong while persisting StoreProductLevel!');
}
} catch (\Exception $ex) {
$msg = sprintf(
"%s:%d %s (%d) [%s]\n", $ex->getFile(), $ex->getLine(), $ex->getMessage(), $ex->getCode(), get_class($ex)
);
$storeProductLevelService->getLogger()->err($msg);
}
return;
}
/**
* @return StoreProductServiceInterface
*/
public function getStoreProductService()
{
return $this->storeProductService;
}
/**
* @param StoreProductServiceInterface $storeProductService
* @return $this
*/
public function setStoreProductService(StoreProductServiceInterface $storeProductService)
{
$this->storeProductService = $storeProductService;
return $this;
}
/**
* @return StoreProductLevelServiceInterface
*/
public function getStoreProductLevelService()
{
return $this->storeProductLevelService;
}
/**
* @param StoreProductLevelServiceInterface $storeProductLevelService
* @return $this
*/
public function setStoreProductLevelService(StoreProductLevelServiceInterface $storeProductLevelService)
{
$this->storeProductLevelService = $storeProductLevelService;
return $this;
}
}
<?php
/**
* This file is part of the Inventory project
* Copyright (c) 2016
* @author Marcel Djaman <marceldjaman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Store\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use MdjamanCommon\Entity\BaseEntity;
use MdjamanCommon\Traits\BlameableEntity;
use Product\Entity\Product;
/**
* StoreProduct
*
* @ORM\Table(name="store_product")
* @ORM\Entity(repositoryClass="Store\Repository\StoreProductRepository")
*/
class StoreProduct extends BaseEntity implements StoreProductInterface
{
use BlameableEntity;
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=36, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
* @JMS\Groups({"list", "details"})
*/
protected $id;
/**
* @var integer
*
* @ORM\Column(name="qty", type="bigint", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $qty;
/**
* @var integer
*
* @ORM\Column(name="virtual_qty", type="bigint", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $virtualQty;
/**
* @var \DateTime
*
* @ORM\Column(name="expiry_date", type="datetime", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $expiryDate;
/**
* @var integer
*
* @ORM\Column(name="reorder_level", type="bigint", nullable=true)
* @JMS\Groups({"details"})
*/
protected $reorderLevel;
/**
* @var integer
*
* @ORM\Column(name="reorder_qty", type="bigint", nullable=true)
* @JMS\Groups({"details"})
*/
protected $reorderQty;
/**
* @var float
*
* @ORM\Column(name="avg_monthly_usage", type="float", precision=12, scale=3, nullable=true)
* @JMS\Groups({"details"})
*/
protected $avgMonthlyUsage;
/**
* @var \Product\Entity\Product
*
* @ORM\ManyToOne(targetEntity="Product\Entity\Product", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* })
* @JMS\Groups({"list", "details"})
* @JMS\MaxDepth(3)
*/
protected $product;
/**
* @var \Store\Entity\Store
*
* @ORM\ManyToOne(targetEntity="Store\Entity\Store")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="store_id", referencedColumnName="id")
* })
* @JMS\Groups({"list", "details"})
* @JMS\MaxDepth(1)
*/
protected $store;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="Store\Entity\StoreProductLevel", mappedBy="storeProduct", cascade={"all"})
* @JMS\MaxDepth(2)
* @JMS\Groups({"details"})
*/
protected $levels;
/**
* @var string
*
* @ORM\Column(name="location", type="string", length=50, nullable=true)
* @JMS\Groups({"details"})
*/
protected $location;
/**
* StoreProduct constructor.
*/
public function __construct()
{
parent::__construct();
$this->levels = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set qty
*
* @param integer $qty
*
* @return StoreProduct
*/
public function setQty($qty)
{
$this->qty = $qty;
return $this;
}
/**
* Get qty
*
* @return integer
*/
public function getQty()
{
return $this->qty;
}
/**
* @return int
*/
public function getVirtualQty()
{
return $this->virtualQty;
}
/**
* @param int $virtualQty
* @return StoreProduct
*/
public function setVirtualQty($virtualQty)
{
$this->virtualQty = $virtualQty;
return $this;
}
/**
* @param \DateTime $expiryDate
* @return StoreProduct
*/
public function setExpiryDate(\DateTime $expiryDate = null)
{
$this->expiryDate = $expiryDate;
return $this;
}
/**
* @return \DateTime
*/
public function getExpiryDate()
{
return $this->expiryDate;
}
/**
* Set reorderLevel
*
* @param integer $reorderLevel
*
* @return StoreProduct
*/
public function setReorderLevel($reorderLevel)
{
$this->reorderLevel = $reorderLevel;
return $this;
}
/**
* Get reorderLevel
*
* @return integer
*/
public function getReorderLevel()
{
return $this->reorderLevel;
}
/**
* Set reorderQty
*
* @param integer $reorderQty
*
* @return StoreProduct
*/
public function setReorderQty($reorderQty)
{
$this->reorderQty = $reorderQty;
return $this;
}
/**
* Get reorderQty
*
* @return integer
*/
public function getReorderQty()
{
return $this->reorderQty;
}
/**
* Set avgMonthlyUsage
*
* @param float $avgMonthlyUsage
*
* @return StoreProduct
*/
public function setAvgMonthlyUsage($avgMonthlyUsage)
{
$this->avgMonthlyUsage = $avgMonthlyUsage;
return $this;
}
/**
* Get avgMonthlyUsage
*
* @return float
*/
public function getAvgMonthlyUsage()
{
return $this->avgMonthlyUsage;
}
/**
* Set product
*
* @param \Product\Entity\Product $product
*
* @return StoreProduct
*/
public function setProduct(Product $product)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \Product\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set store
*
* @param \Store\Entity\Store $store
*
* @return StoreProduct
*/
public function setStore(Store $store)
{
$this->store = $store;
return $this;
}
/**
* Get store
*
* @return \Store\Entity\Store
*/
public function getStore()
{
return $this->store;
}
/**
* @param StoreProductLevel $level
* @return $this
*/
public function addLevel(StoreProductLevel $level)
{
if (!$this->levels->contains($level)) {
$this->levels->add($level);
$level->setStoreProduct($this);
}
return $this;
}
/**
* @param StoreProductLevel $level
* @return $this
*/
public function removeLevel(StoreProductLevel $level)
{
if ($this->levels->contains($level)) {
$this->levels->removeElement($level);
}
return $this;
}
/**
* Get levels
*
* @return Collection
*/
public function getLevels()
{
return $this->levels;
}
/**
* @return string
*/
public function getLocation()
{
return $this->location;
}
/**
* @param string $location
* @return Product
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
}
<?php
/**
* This file is part of the Inventory project
* Copyright (c) 2016
* @author Marcel Djaman <marceldjaman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Store\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use MdjamanCommon\Traits\BlameableEntity;
/**
* StoreProductLevel
*
* @ORM\Table(name="store_product_level")
* @ORM\Entity(repositoryClass="Store\Repository\StoreProductLevelRepository")
*/
class StoreProductLevel implements StoreProductLevelInterface
{
use BlameableEntity;
/**
* @var string
*
* @ORM\Column(name="id", type="string", length=36, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
* @JMS\Groups({"list", "details"})
*/
protected $id;
/**
* @var integer
*
* @ORM\Column(name="qty", type="bigint", nullable=false)
* @JMS\Groups({"list", "details"})
*/
protected $qty;
/**
* @var integer
*
* @ORM\Column(name="virtual_qty", type="bigint", nullable=true)
* @JMS\Groups({"list", "details"})
*/
protected $virtualQty;
/**
* @var string
*
* @ORM\Column(name="details", type="text", nullable=true)
* @JMS\Groups({"details"})
*/
protected $details;
/**
* @var \DateTime
*
* @ORM\Column(name="taking_date", type="datetime", nullable=false)
* @JMS\Groups({"list", "details"})
*/
protected $takingDate;
/**
* @var \Store\Entity\StoreProduct
*
* @ORM\ManyToOne(targetEntity="Store\Entity\StoreProduct", cascade={"persist", "remove"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="store_product_id", referencedColumnName="id")
* })
* @JMS\Groups({"list", "details"})
*/
protected $storeProduct;
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set qty
*
* @param integer $qty
*
* @return StoreProductLevel
*/
public function setQty($qty)
{
$this->qty = $qty;
return $this;
}
/**
* Get qty
*
* @return integer
*/
public function getQty()
{
return $this->qty;
}
/**
* @return int
*/
public function getVirtualQty()
{
return $this->virtualQty;
}
/**
* @param int $virtualQty
* @return StoreProductLevel
*/
public function setVirtualQty($virtualQty)
{
$this->virtualQty = $virtualQty;
return $this;
}
/**
* Set details
*
* @param string $details
*
* @return StoreProductLevel
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* @return string
*/
public function getDetails()
{
return $this->details;
}
/**
* Set takingDate
*
* @param \DateTime $takingDate
*
* @return StoreProductLevel
*/
public function setTakingDate($takingDate)
{
$this->takingDate = $takingDate;
return $this;
}
/**
* Get takingDate
*
* @return \DateTime
*/
public function getTakingDate()
{
return $this->takingDate;
}
/**
* Set storeProduct
*
* @param \Store\Entity\StoreProduct $storeProduct
*
* @return StoreProductLevel
*/
public function setStoreProduct(StoreProduct $storeProduct)
{
$this->storeProduct = $storeProduct;
return $this;
}
/**
* Get storeProduct
*
* @return \Store\Entity\StoreProduct
*/
public function getStoreProduct()
{
return $this->storeProduct;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment