@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Context ; | |||
interface OrderUtilsInterface | |||
{ | |||
} |
@@ -3,19 +3,28 @@ | |||
namespace Lc\ShopBundle\Controller\Frontend ; | |||
use App\Form\Frontend\OrderProductsType; | |||
use CKSource\CKFinder\Response\JsonResponse; | |||
use App\Services\OrderUtils; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||
use Lc\ShopBundle\Context\OrderProductInterface; | |||
use Lc\ShopBundle\Context\OrderUtilsInterface; | |||
use Lc\ShopBundle\Context\ProductFamilyInterface; | |||
use Symfony\Component\HttpFoundation\JsonResponse; | |||
use Symfony\Component\HttpFoundation\Request; | |||
class CartController extends BaseController | |||
{ | |||
protected $orderUtils ; | |||
protected $productFamilyRepository ; | |||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils) | |||
protected $productFamily ; | |||
protected $orderProducts = [] ; | |||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils) | |||
{ | |||
parent::__construct($em, $merchantUtils); | |||
$this->orderUtils = $orderUtils ; | |||
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; | |||
} | |||
@@ -23,18 +32,24 @@ class CartController extends BaseController | |||
{ | |||
$return = [] ; | |||
$data = $request->request->all() ; | |||
$return = $data ; | |||
if(isset($data['order_products']['id_product_family'])) { | |||
$idProductFamily = $data['order_products']['id_product_family'] ; | |||
$productFamily = $this->productFamilyRepository->find($idProductFamily) ; | |||
if($productFamily) { | |||
$form = $this->createForm(OrderProductsType::class, ['id_product_family' => $productFamily->getId()]); | |||
$this->productFamily = $this->productFamilyRepository->find($idProductFamily) ; | |||
if($this->productFamily) { | |||
$form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]); | |||
$form->handleRequest($request); | |||
if ($form->isSubmitted() && $form->isValid()) { | |||
$orderShop = $this->orderUtils->getOrderShopCurrent() ; | |||
$data = $form->getData() ; | |||
foreach($data as $orderProduct) { | |||
if($orderProduct instanceof OrderProductInterface) { | |||
$this->orderUtils->addOrderProduct($orderShop, $orderProduct) ; | |||
if($orderProduct->getQuantity() > 0) { | |||
$this->orderProducts[] = $orderProduct ; | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,46 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Form\DataTransformer; | |||
use Lc\ShopBundle\Context\ProductInterface; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Symfony\Component\Form\DataTransformerInterface; | |||
use Symfony\Component\Form\Exception\TransformationFailedException; | |||
class ProductToIdTransformer implements DataTransformerInterface | |||
{ | |||
private $em ; | |||
public function __construct(EntityManagerInterface $em) | |||
{ | |||
$this->em = $em; | |||
} | |||
public function transform($product) | |||
{ | |||
if (null === $product) { | |||
return ''; | |||
} | |||
return $product->getId(); | |||
} | |||
public function reverseTransform($productId) | |||
{ | |||
if (!$productId) { | |||
return; | |||
} | |||
$product = $this->em->getRepository($this->em->getClassMetadata(ProductInterface::class)->getName())->find($productId); | |||
if (null === $product) { | |||
throw new TransformationFailedException(sprintf( | |||
'An issue with number "%s" does not exist!', | |||
$productId | |||
)); | |||
} | |||
return $product; | |||
} | |||
} |
@@ -8,7 +8,7 @@ use Lc\ShopBundle\Context\PriceInterface; | |||
/** | |||
* @ORM\MappedSuperclass() | |||
*/ | |||
abstract class OrderProduct extends AbstractEntity implements PriceInterface | |||
abstract class OrderProduct implements PriceInterface | |||
{ | |||
use PriceTrait ; | |||
@@ -19,7 +19,7 @@ abstract class OrderProduct extends AbstractEntity implements PriceInterface | |||
protected $orderShop; | |||
/** | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductInterface") | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductInterface", inversedBy="orderProducts")) | |||
*/ | |||
protected $product; | |||
@@ -37,10 +37,61 @@ abstract class OrderProduct extends AbstractEntity implements PriceInterface | |||
{ | |||
if($this->getTitle()) { | |||
return $this->getTitle(); | |||
}else{ | |||
} | |||
else{ | |||
return $this->getProduct()->getProductFamily()->getTitle(). ' - '.$this->getProduct()->getTitle(); | |||
} | |||
} | |||
public function getTitleOrderShop() | |||
{ | |||
$product = $this->getProduct() ; | |||
$productFamily = $product->getProductFamily() ; | |||
$titleProduct = $product->getTitleInherited() ; | |||
$titleProductFamily = $productFamily->getTitle() ; | |||
if(strlen($titleProduct) > 0 && strlen($titleProductFamily) > 0) { | |||
$title = $titleProductFamily.' - '.$titleProduct ; | |||
} | |||
else { | |||
$title = strlen($titleProduct) ? $titleProduct : $titleProductFamily ; | |||
} | |||
if($productFamily->hasProductsWithVariousWeight()) { | |||
$title .= ' - '.$product->getQuantityLabelInherited() ; | |||
} | |||
return $title ; | |||
} | |||
public function getTitleSummaryAfterAddCart() | |||
{ | |||
$title = '' ; | |||
$product = $this->getProduct() ; | |||
$productFamily = $product->getProductFamily() ; | |||
$titleProduct = $product->getTitleInherited() ; | |||
$titleProductFamily = $productFamily->getTitle() ; | |||
// multiple | |||
if($productFamily->getActiveProducts() && $productFamily->getBehaviorAddToCart() == 'multiple') { | |||
$title .= $titleProduct ; | |||
if($productFamily->hasProductsWithVariousWeight()) { | |||
$title .= ' - '.$product->getQuantityLabelInherited() ; | |||
} | |||
} | |||
// simple | |||
if($productFamily->getBehaviorAddToCart() == 'simple') { | |||
if($productFamily->getActiveProducts()) { | |||
$title .= $titleProduct ; | |||
if($productFamily->hasProductsWithVariousWeight()) { | |||
$title .= ' - '.$product->getQuantityLabelInherited() ; | |||
} | |||
} | |||
} | |||
return $title ; | |||
} | |||
public function getOrderShop(): ?OrderShop |
@@ -2,6 +2,7 @@ | |||
namespace Lc\ShopBundle\Model; | |||
use App\Entity\Visitor; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\Common\Collections\Collection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
@@ -10,7 +11,7 @@ use Lc\ShopBundle\Context\FilterMerchantInterface; | |||
/** | |||
* @ORM\MappedSuperclass() | |||
*/ | |||
abstract class OrderShop extends AbstractEntity implements FilterMerchantInterface | |||
abstract class OrderShop implements FilterMerchantInterface | |||
{ | |||
/** | |||
@@ -21,13 +22,16 @@ abstract class OrderShop extends AbstractEntity implements FilterMerchantInterfa | |||
/** | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserInterface", inversedBy="orders") | |||
* @ORM\JoinColumn(nullable=false) | |||
*/ | |||
protected $user; | |||
/** | |||
* @ORM\ManyToOne(targetEntity="App\Entity\Visitor", inversedBy="orders") | |||
*/ | |||
protected $visitor; | |||
/** | |||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\AddressInterface", inversedBy="order") | |||
* @ORM\JoinColumn(nullable=false) | |||
*/ | |||
protected $invoiceAddress; | |||
@@ -288,4 +292,16 @@ abstract class OrderShop extends AbstractEntity implements FilterMerchantInterfa | |||
return $this; | |||
} | |||
public function getVisitor(): ?Visitor | |||
{ | |||
return $this->visitor; | |||
} | |||
public function setVisitor(?Visitor $visitor): self | |||
{ | |||
$this->visitor = $visitor; | |||
return $this; | |||
} | |||
} |
@@ -2,8 +2,12 @@ | |||
namespace Lc\ShopBundle\Model; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\Common\Collections\Collection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
use Lc\ShopBundle\Context\OrderProductInterface; | |||
use Lc\ShopBundle\Context\PriceInterface; | |||
use Lc\ShopBundle\Context\ProductInterface; | |||
use Lc\ShopBundle\Context\ProductPropertyInterface; | |||
use Lc\ShopBundle\Context\SortableInterface; | |||
use Lc\ShopBundle\Services\Price; | |||
@@ -27,6 +31,16 @@ abstract class Product extends AbstractEntity implements SortableInterface, Prod | |||
*/ | |||
protected $title; | |||
/** | |||
* @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\OrderProductInterface", mappedBy="product", orphanRemoval=true, cascade={"remove"}) | |||
*/ | |||
protected $orderProducts ; | |||
public function __construct() | |||
{ | |||
$this->orderProducts = new ArrayCollection() ; | |||
} | |||
public function getPriceInherited() | |||
{ | |||
if($this->price) { | |||
@@ -117,4 +131,35 @@ abstract class Product extends AbstractEntity implements SortableInterface, Prod | |||
return $this; | |||
} | |||
/** | |||
* @return Collection|OrderProductInterface[] | |||
*/ | |||
public function getOrderProducts(): Collection | |||
{ | |||
return $this->orderProducts; | |||
} | |||
public function addOrderProduct(OrderProductInterface $orderProduct): self | |||
{ | |||
if (!$this->orderProducts->contains($orderProduct)) { | |||
$this->orderProducts[] = $orderProduct; | |||
$orderProduct->setProduct($this); | |||
} | |||
return $this; | |||
} | |||
public function removeOrderProduct(OrderProductInterface $orderProduct): self | |||
{ | |||
if ($this->orderProducts->contains($orderProduct)) { | |||
$this->orderProducts->removeElement($orderProduct); | |||
// set the owning side to null (unless already changed) | |||
if ($orderProduct->getProduct() === $this) { | |||
$orderProduct->setProduct(null); | |||
} | |||
} | |||
return $this; | |||
} | |||
} |
@@ -2,6 +2,9 @@ | |||
namespace Lc\ShopBundle\Model; | |||
use App\Entity\OrderShop; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\Common\Collections\Collection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
/** | |||
@@ -29,6 +32,16 @@ abstract class Visitor | |||
*/ | |||
protected $totalVisit; | |||
/** | |||
* @ORM\OneToMany(targetEntity="App\Entity\OrderShop", mappedBy="visitor") | |||
*/ | |||
protected $orders; | |||
public function __construct() | |||
{ | |||
$this->orders = new ArrayCollection(); | |||
} | |||
public function getCookie(): ?string | |||
{ | |||
return $this->cookie; | |||
@@ -76,4 +89,35 @@ abstract class Visitor | |||
return $this; | |||
} | |||
/** | |||
* @return Collection|OrderShop[] | |||
*/ | |||
public function getOrders(): Collection | |||
{ | |||
return $this->orders; | |||
} | |||
public function addOrder(OrderShop $order): self | |||
{ | |||
if (!$this->orders->contains($order)) { | |||
$this->orders[] = $order; | |||
$order->setVisitor($this); | |||
} | |||
return $this; | |||
} | |||
public function removeOrder(OrderShop $order): self | |||
{ | |||
if ($this->orders->contains($order)) { | |||
$this->orders->removeElement($order); | |||
// set the owning side to null (unless already changed) | |||
if ($order->getVisitor() === $this) { | |||
$order->setVisitor(null); | |||
} | |||
} | |||
return $this; | |||
} | |||
} |
@@ -8,21 +8,21 @@ lc_api: | |||
controller: Lc\ShopBundle\Controller\ApiController::getEntity | |||
lc_frontend_cart_add_product_family: | |||
path: /cart/add-product-family | |||
path: /lc/cart/add/product-family | |||
controller: Lc\ShopBundle\Controller\Frontend\CartController::addProductFamily | |||
lc_frontend_cart_add_product: | |||
path: /cart/add-product | |||
path: /lc/cart/add/product | |||
controller: Lc\ShopBundle\Controller\Frontend\CartController::addProduct | |||
lc_frontend_cart_edit_product: | |||
path: /cart/edit-product | |||
path: /lc/cart/edit/product | |||
controller: Lc\ShopBundle\Controller\Frontend\CartController::editProduct | |||
lc_frontend_cart_delete_product: | |||
path: /cart/delete-product | |||
path: /lc/cart/delete/product | |||
controller: Lc\ShopBundle\Controller\Frontend\CartController::deleteProduct | |||
lc_frontend_cart_summary: | |||
path: /cart/summary | |||
path: /lc/cart/summary | |||
controller: Lc\ShopBundle\Controller\Frontend\CartController::summary |
@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Services ; | |||
class OrderUtils | |||
{ | |||
} |