@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Context ; | |||
interface OrderUtilsInterface | |||
{ | |||
} |
@@ -3,19 +3,24 @@ | |||
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) | |||
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,23 +28,26 @@ 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()]); | |||
$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) ; | |||
} | |||
} | |||
} | |||
} | |||
} | |||
return new JsonResponse($return) ; | |||
return new JsonResponse($data) ; | |||
} | |||
public function addProduct() |
@@ -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 ; | |||
@@ -37,10 +37,31 @@ 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->getTitle() ; | |||
$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 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,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; | |||
} | |||
} |
@@ -0,0 +1,8 @@ | |||
<?php | |||
namespace Lc\ShopBundle\Services ; | |||
class OrderUtils | |||
{ | |||
} |