src/Application/Cart/Cart.php line 178

Open in your IDE?
  1. <?php
  2. namespace App\Application\Cart;
  3. use App\Admin\Document\Client;
  4. use App\Admin\Document\Counterparty;
  5. use App\Admin\Document\Product;
  6. use App\Admin\Document\PromoCode;
  7. use App\Admin\Document\PropertyValue;
  8. use App\Admin\Document\UserAccount;
  9. use AvenueAdminBundle\Authentication\Storage\Session;
  10. use Doctrine\ODM\MongoDB\DocumentManager;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. class Cart extends AbstractCart
  13. {
  14.     /**
  15.      * @var SessionInterface
  16.      */
  17.     private $session;
  18.     /**
  19.      * @var DocumentManager
  20.      */
  21.     private $documentManager;
  22.     /**
  23.      * @var Session
  24.      */
  25.     private $storage;
  26.     /**
  27.      * @param SessionInterface $session
  28.      * @param DocumentManager $documentManager
  29.      */
  30.     public function __construct(SessionInterface $sessionDocumentManager $documentManagerSession $storage)
  31.     {
  32.         $this->session $session;
  33.         $this->documentManager $documentManager;
  34.         $this->storage $storage;
  35.     }
  36.     /**
  37.      * @return UserAccount
  38.      */
  39.     private function getIdentity()
  40.     {
  41.         return $this->storage->read();
  42.     }
  43.     public function getCounterpartyId() {
  44.          return $this->getCounterparty() ? $this->getCounterparty()->getId() : null;
  45.     }
  46.     /**
  47.      * @var Counterparty
  48.      */
  49.     private $counterparty;
  50.     /**
  51.      * @return Counterparty
  52.      */
  53.     public function getCounterparty() {
  54.         if (!isset($this->counterparty)) {
  55.             $userAccount $this->getIdentity();
  56.             if (!$userAccount) {
  57.                 return null;
  58.             }
  59.             $id $this->session->get('cart-counterparty'null);
  60.             if ($id) {
  61.                 $this->counterparty $this->documentManager->getRepository(Counterparty::class)->findOneBy([
  62.                     '_id' => $id,
  63.                     'client' => $userAccount->getClient()->getId()
  64.                 ]);
  65.             }
  66.             if (!$this->counterparty) {
  67.                 $this->counterparty false;
  68.             }
  69.         }
  70.         return $this->counterparty;
  71.     }
  72.     public function getCounterpartyName() {
  73.         $counterparty $this->getCounterparty();
  74.         if ($counterparty) {
  75.             return $counterparty->getName();
  76.         }
  77.         return null;
  78.     }
  79.     public function getValidPrice(Product $product) {
  80.         $counterparty $this->getCounterparty();
  81.         if ($counterparty) {
  82.             return $counterparty->getSellerProductPrice($product);
  83.         }
  84.         return $product->getPrice();
  85.     }
  86.     public function isValid() {
  87.         $counterparty $this->getCounterparty();
  88.         if ($counterparty) {
  89.             foreach ($this->getItems() as $item) {
  90.                 if (!$counterparty->getSellerProductPrice($item->product)) {
  91.                     return false;
  92.                 }
  93.             }
  94.         }
  95.         return true;
  96.     }
  97.     public function hasRetailCart(?UserAccount $userAccount ) {
  98.         $counterpartyId $this->getCounterpartyId();
  99.         if ($counterpartyId) {
  100.             foreach ($userAccount->getCartItemsByCounterparty(null) as $productId => $a) {
  101.                 if ($productId && is_array($a)) {
  102.                     $product $this->findProductById($productId);
  103.                     if ($product) {
  104.                         return true;
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         return false;
  110.     }
  111.     public function loadRetail(UserAccount $userAccount) {
  112.         $items $userAccount->getCartItemsByCounterparty(null);
  113.         $this->setSessionItems($items);
  114.     }
  115.     public function setCounterparty(Counterparty $counterparty null, array $sessionItems) {
  116.         $this->counterparty $counterparty;
  117.         $this->session->set('cart-counterparty'$counterparty $counterparty->getId() : null);
  118.         $this->setSessionItems($sessionItems);
  119.         if ($counterparty && $counterparty->getUseDealerPromoCode()) {
  120.             $this->setPromoCode(PromoCode::getDealerPromoCode());
  121.         } elseif ($this->getPromoCode() && ($this->getPromoCode()->isDealer() || !$this->getShowPromoCode())) {
  122.             $this->clearPromoCode();
  123.         }
  124.     }
  125.     public function getSessionItems(): array {
  126.         return $this->session->get('cart-items', []);
  127.     }
  128.     private function setSessionItems($items) {
  129.         $this->session->set('cart-items'$items);
  130.     }
  131.     private function getCalcSessionItems(): array {
  132.         return $this->session->get('calc-items', []);
  133.     }
  134.     private function setCalcSessionItems($items) {
  135.         $this->session->set('calc-items'$items);
  136.     }
  137.     private function getDecorCalcSessionItems(): array {
  138.         return $this->session->get('decor-calc-items', []);
  139.     }
  140.     private function setDecorCalcSessionItems($items) {
  141.         $this->session->set('decor-calc-items'$items);
  142.     }
  143.     public function getGlueCount() {
  144.         return $this->session->get('glue-count'0);
  145.     }
  146.     public function getDecorGlueCount() {
  147.         return $this->session->get('glue-count-decor'0);
  148.     }
  149.     private function setGlueCount($value$decor) {
  150.         return $this->session->set('glue-count' . ($decor '-decor' ''), $value);
  151.     }
  152.     public function getCompareList(): array {
  153.         return $this->session->get('cart-compare-list', []);
  154.     }
  155.     private function setCompareList($list) {
  156.         $this->session->set('cart-compare-list'$list);
  157.     }
  158.     private function getCartItem($productId$items) {
  159.         if (isset($items[$productId])) {
  160.             $cartItem $items[$productId];
  161.         } else {
  162.             $cartItem = ['quantity' => 0];
  163.         }
  164.         return $cartItem;
  165.     }
  166.     public function add($productId$quantity$replace false)
  167.     {
  168.         $items $this->getSessionItems();
  169.         $cartItem $this->getCartItem($productId$items);
  170.         if ($replace) {
  171.             $cartItem['quantity'] = $quantity;
  172.         } else {
  173.             $cartItem['quantity'] += $quantity;
  174.         }
  175.         $items[$productId] = $cartItem;
  176.         $this->setSessionItems($items);
  177.         return $cartItem['quantity'];
  178.     }
  179.     public function setNotInStock($productId$maxQuantity)
  180.     {
  181.         $items $this->getSessionItems();
  182.         $cartItem $this->getCartItem($productId$items);
  183.         $cartItem['notInStock'] = true;
  184.         $cartItem['maxQuantity'] = $maxQuantity;
  185.         $items[$productId] = $cartItem;
  186.         $this->setSessionItems($items);
  187.     }
  188.     public function clearNotInStock($productId)
  189.     {
  190.         $items $this->getSessionItems();
  191.         $cartItem $this->getCartItem($productId$items);
  192.         unset($cartItem['notInStock']);
  193.         unset($cartItem['maxQuantity']);
  194.         $items[$productId] = $cartItem;
  195.         $this->setSessionItems($items);
  196.     }
  197.     public function put($productId$quantity)
  198.     {
  199.         return $this->add($productId$quantitytrue);
  200.     }
  201.     /**
  202.      * @param string $productId
  203.      */
  204.     public function remove($productId)
  205.     {
  206.         $items $this->getSessionItems();
  207.         if (isset($items[$productId])) {
  208.             unset($items[$productId]);
  209.         }
  210.         $this->setSessionItems($items);
  211.     }
  212.     public function has($productId) {
  213.         return $this->getQuantity($productId) > 0;
  214.     }
  215.     public function getQuantity($productId) {
  216.         $items $this->getSessionItems();
  217.         if (isset($items[$productId])) {
  218.             return $items[$productId]['quantity'];
  219.         }
  220.         return 0;
  221.     }
  222.     /**
  223.      * @return CartItem[]
  224.      */
  225.     public function getItems($sessionItems null): array
  226.     {
  227.         if (!isset($sessionItems)) {
  228.             $sessionItems $this->getSessionItems();
  229.         }
  230.         $result = [];
  231.         foreach ($sessionItems as $productId => $item) {
  232.             $product $this->findProductById($productId);
  233.             if (!$product) {
  234.                 continue;
  235.             }
  236.             $result[] = new CartItem($this$product$item);
  237.         }
  238.         return $result;
  239.     }
  240.     public function clear() {
  241.         $this->setSessionItems([]);
  242.     }
  243.     /**
  244.      * @param string $productId
  245.      * @return null|Product
  246.      */
  247.     private function findProductById($productId)
  248.     {
  249.         return $this->documentManager->getRepository(Product::class)->findOneBy(['active' => true'_id' => $productId]);
  250.     }
  251.     public function getDiscountAmount() {
  252.         $result 0;
  253.         foreach ($this->getItems() as $item) {
  254.             $result += $item->getDiscountAmount();
  255.         }
  256.         return $result;
  257.     }
  258.     public function getAmountWithDiscount() {
  259.         return $this->getAmount() - $this->getDiscountAmount();
  260.     }
  261.     public function getVat() {
  262.         if ($this->getCounterparty()) {
  263.             return $this->getCounterparty()->getVat();
  264.         }
  265.         return Counterparty::getVatByType('cosca');
  266.     }
  267.     public static function calcVat($amount$vat) {
  268.         return round($amount $vat / (100 $vat), 2);
  269.     }
  270.     public function getVatAmount() {
  271.         return self::calcVat($this->getAmountWithDiscount(), $this->getVat());
  272.     }
  273.     private $promoCode false;
  274.     public function clearPromoCode(): void
  275.     {
  276.         $this->promoCode false;
  277.         $this->session->set('cart-promo-map'null);
  278.     }
  279.     public function setPromoCode(PromoCode $promoCode)
  280.     {
  281.         if (!$this->getCounterparty() || $this->getCounterparty()->getShowPromoCode()
  282.             || $this->getCounterparty()->getUseDealerPromoCode() && $promoCode->isDealer()
  283.         ) {
  284.             $this->promoCode $promoCode;
  285.             $a $this->session->get('cart-promo-map', []);
  286.             $a[$this->getCounterpartyId()] = $this->promoCode->getCode();
  287.             $this->session->set('cart-promo-map'$a);
  288.             return true;
  289.         }
  290.         return false;
  291.     }
  292.     /**
  293.      * @param Counterparty $counterparty
  294.      * @return string|null
  295.      */
  296.     private function getCartPromoCode($counterparty) {
  297.         $a $this->session->get('cart-promo-map', []);
  298.         return $a[$counterparty $counterparty->getId() : ''] ?? null;
  299.     }
  300.     /**
  301.      * @return PromoCode
  302.      */
  303.     public function getPromoCode()
  304.     {
  305.         if ($this->promoCode === false) {
  306.             $this->promoCode $this->getCounterpartyPromoCode($this->getCounterparty());
  307.         }
  308.         return $this->promoCode;
  309.     }
  310.     /**
  311.      * @return PromoCode
  312.      */
  313.     public function getCounterpartyPromoCode($counterparty)
  314.     {
  315.         $code $this->getCartPromoCode($counterparty);
  316.         if ($code) {
  317.             if ($code == PromoCode::DEALER_CODE) {
  318.                 return PromoCode::getDealerPromoCode();
  319.             }
  320.             return $this->documentManager->getRepository(PromoCode::class)->findOneBy(['code' => $code]);
  321.         }
  322.         return null;
  323.     }
  324.     public function getShowPromoCode() {
  325.         return !$this->getCounterparty() || $this->getCounterparty()->getShowPromoCode();
  326.     }
  327.     public function addInCompareList($id) {
  328.         $list $this->getCompareList();
  329.         $list[] = $id;
  330.         $this->setCompareList($list);
  331.     }
  332.     public function clearCompareList($id) {
  333.         $this->setCompareList([]);
  334.     }
  335.     public function inCompareList($id) {
  336.         $list $this->getCompareList();
  337.         return in_array($id$list);
  338.     }
  339.     public function addToCalc($productId$quantity 1$replace false)
  340.     {
  341.         $items $this->getCalcSessionItems();
  342.         $cartItem $this->getCartItem($productId$items);
  343.         if ($replace) {
  344.             $cartItem['quantity'] = $quantity;
  345.         } else {
  346.             $cartItem['quantity'] += $quantity;
  347.         }
  348.         $items[$productId] = $cartItem;
  349.         $this->setCalcSessionItems($items);
  350.         $this->updateGlueCount(false);
  351.         return $cartItem['quantity'];
  352.     }
  353.     public function clearCalc() {
  354.         $this->setCalcSessionItems([]);
  355.         $this->updateGlueCount(false);
  356.     }
  357.     public function addToDecorCalc($productId)
  358.     {
  359.         $items $this->getDecorCalcSessionItems();
  360.         $cartItem $this->getCartItem($productId$items);
  361.         $cartItem['quantity'] = 1;
  362.         $items[$productId] = $cartItem;
  363.         $this->setDecorCalcSessionItems($items);
  364.         $this->updateGlueCount(true);
  365.         return $cartItem['quantity'];
  366.     }
  367.     public function clearDecorCalc() {
  368.         $this->setDecorCalcSessionItems([]);
  369.         $this->updateGlueCount(true);
  370.     }
  371.     public function removeFromCalc($productId)
  372.     {
  373.         $items $this->getCalcSessionItems();
  374.         unset($items[$productId]);
  375.         $this->setCalcSessionItems($items);
  376.         $this->updateGlueCount(false);
  377.     }
  378.     public function removeFromDecorCalc($productId)
  379.     {
  380.         $items $this->getDecorCalcSessionItems();
  381.         unset($items[$productId]);
  382.         $this->setDecorCalcSessionItems($items);
  383.         $this->updateGlueCount(true);
  384.     }
  385.     /**
  386.      * @return CartItem[]
  387.      */
  388.     public function getCalcItems(): array
  389.     {
  390.         return $this->getItems($this->getCalcSessionItems());
  391.     }
  392.     /**
  393.      * @return CartItem[]
  394.      */
  395.     public function getDecorCalcItems(): array
  396.     {
  397.         return $this->getItems($this->getDecorCalcSessionItems());
  398.     }
  399.     public function hasCalcItem($productId) {
  400.         $items $this->getCalcSessionItems();
  401.         return isset($items[$productId]);
  402.     }
  403.     public function hasDecorCalcItem($productId) {
  404.         $items $this->getDecorCalcSessionItems();
  405.         return isset($items[$productId]);
  406.     }
  407.     public function getCalcPerimeter() {
  408.         return $this->session->get('calc-perimeter'0);
  409.     }
  410.     public  function setCalcPerimeter($value) {
  411.         $this->session->set('calc-perimeter'$value);
  412.         $this->updateGlueCount(true);
  413.     }
  414.     public function getCalcOpeningWidth() {
  415.         return $this->session->get('calc-opening-width'0);
  416.     }
  417.     public  function setCalcOpeningWidth($value) {
  418.         $this->session->set('calc-opening-width'$value);
  419.         $this->updateGlueCount(true);
  420.     }
  421.     private function updateGlueCount($decor) {
  422.         $glueCount 0;
  423.         if ($decor) {
  424.             $items $this->getDecorCalcSessionItems();
  425.         }
  426.         foreach ($decor $this->getDecorCalcItems() : $this->getCalcItems() as $item) {
  427.             $len $item->product->getLengthForGlue() ?: 1;
  428.             if ($decor) {
  429.                 $s $this->getCalcPerimeter();
  430.                 if ($item->product->isPlinthCalculator()) {
  431.                     $s -= $this->getCalcOpeningWidth();
  432.                 }
  433.                 $quantity ceil($s $len);
  434.                 $sessionItem $this->getCartItem($item->product->getId(), $items);
  435.                 $sessionItem['quantity'] = $quantity;
  436.                 $items[$item->product->getId()] = $sessionItem;
  437.             } else {
  438.                 $quantity $item->quantity;
  439.             }
  440.             $mountingSurface $item->product->getMountingSurface() ?: 2;
  441.             $glueCount += ceil($mountingSurface $quantity $len 27);
  442.         }
  443.         $this->setGlueCount($glueCount$decor);
  444.         if ($decor) {
  445.             $this->setDecorCalcSessionItems($items);
  446.         }
  447.     }
  448. }