src/Front/Modules/Cart/Service/CartService.php line 345

  1. <?php
  2. namespace App\Front\Modules\Cart\Service;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Form\Form;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use App\Admin\Modules\OnlineService\Service\CreatorPriceListService as AdminCreatorPriceListService;
  7. use App\Admin\Modules\OnlineService\Entity\OnlineService;
  8. use App\Admin\Modules\OnlineService\Entity\OnlineServiceCreatorCategoryItem;
  9. use App\Modules\User\Service\UserService;
  10. use App\Front\Modules\OnlineService\Service\OnlineServiceService;
  11. use App\Front\Modules\OnlineService\Service\OnlineServiceRepositoryService;
  12. class CartService
  13. {
  14.     const ITEM_TYPE_ONLINE_SERVICE 1;
  15.     const ITEM_TYPE_CREATOR_ONLINE_SERVICE 2;
  16.     private $_requestStack;
  17.     private $_doctrine;
  18.     private $_adminCreatorPriceListService;
  19.     private $_userService;
  20.     private $_onlineServiceService;
  21.     private $_onlineServiceRepositoryService;
  22.     private $_sessionKey 'cart';
  23.     
  24.     public function __construct(
  25.             RequestStack $requestStack,
  26.             ManagerRegistry $doctrine,
  27.             AdminCreatorPriceListService $adminCreatorPriceListService,
  28.             UserService $userService,
  29.             OnlineServiceService $onlineServiceService,
  30.             OnlineServiceRepositoryService $onlineServiceRepositoryService)
  31.     {
  32.         $this->_requestStack $requestStack;
  33.         $this->_doctrine $doctrine;
  34.         $this->_adminCreatorPriceListService $adminCreatorPriceListService;
  35.         $this->_userService $userService;
  36.         $this->_onlineServiceService $onlineServiceService;
  37.         $this->_onlineServiceRepositoryService $onlineServiceRepositoryService;
  38.     }
  39.     
  40.     private static $_cartDetails;
  41.     public function getDetails()
  42.     {
  43.         if (self::$_cartDetails === null)
  44.         {
  45.             $data array_merge($this->_getData(), array(
  46.                 'online_services' => array(),
  47.                 'creator_online_services' => array(),
  48.                 'contract_preview_step_active' => false,
  49.                 'vat' => array(),
  50.                 'total_quantity' => 0,
  51.                 'total_items_price' => 0,
  52.                 'total_items_gross_price' => 0,
  53.                 'total_gross_price' => 0
  54.             ));
  55.             if ($data['items'])
  56.             {
  57.                 foreach($data['items'] as $cartItemId => $cartItemData)
  58.                 {
  59.                     switch($cartItemData['type'])
  60.                     {
  61.                         case self::ITEM_TYPE_ONLINE_SERVICE:
  62.                             $service $this->_onlineServiceRepositoryService->find($cartItemData['id']);
  63.                             if (!$service)
  64.                                 continue 2;
  65.                             
  66.                             $cartItemData['service_data'] = $this->_onlineServiceService->getDetails($service);
  67.                             if (!isset($data['online_services'][$service->getCategory()->getId()]))
  68.                             {
  69.                                 $data['online_services'][$service->getCategory()->getId()] = array(
  70.                                     'entity' => $service->getCategory(),
  71.                                     'services' => array(),
  72.                                     'total_price' => 0,
  73.                                     'total_gross_price' => 0
  74.                                 );
  75.                             }
  76.                             
  77.                             $data['online_services'][$service->getCategory()->getId()]['services'][$cartItemId] = $cartItemData;
  78.                             $data['online_services'][$service->getCategory()->getId()]['total_price'] += $cartItemData['service_data']['price'];
  79.                             $data['online_services'][$service->getCategory()->getId()]['total_gross_price'] += $cartItemData['service_data']['gross_price'];
  80.                             $data['total_quantity'] += $cartItemData['quantity'];
  81.                             $data['total_items_price'] += $cartItemData['service_data']['price'];
  82.                             $data['total_items_gross_price'] += $cartItemData['service_data']['gross_price'];
  83.                             
  84.                             $data['items'][$cartItemId] = &$data['online_services'][$service->getCategory()->getId()]['services'][$cartItemId];
  85.                             
  86.                             if (!in_array($cartItemData['service_data']['vat'], $data['vat']))
  87.                                 $data['vat'][] = $cartItemData['service_data']['vat'];
  88.                             break;
  89.                         case self::ITEM_TYPE_CREATOR_ONLINE_SERVICE:
  90.                             $item $this->_doctrine->getRepository(OnlineServiceCreatorCategoryItem::class)->find($cartItemData['id']);
  91.                             if (!$item)
  92.                                 continue 2;
  93.                             $priceListItemData $this->_adminCreatorPriceListService->getItemDetails($item->getPriceListItem());
  94.                             $cartItemData array_merge($cartItemData, array(
  95.                                     'entity' => $item,
  96.                                     'vat' => $priceListItemData['vat'],
  97.                                     'gross_price' => $priceListItemData['gross_price']
  98.                                 ));
  99.                             if (!isset($data['creator_online_services'][$item->getCategory()->getId()]))
  100.                             {
  101.                                 $data['creator_online_services'][$item->getCategory()->getId()] = array(
  102.                                     'entity' => $item->getCategory(),
  103.                                     'items' => array(),
  104.                                     'total_price' => 0,
  105.                                     'total_gross_price' => 0
  106.                                 );
  107.                             }
  108.                             $data['creator_online_services'][$item->getCategory()->getId()]['items'][$cartItemId] = $cartItemData;
  109.                             $data['creator_online_services'][$item->getCategory()->getId()]['total_price'] += $item->getPriceListItem()->getPrice();
  110.                             $data['creator_online_services'][$item->getCategory()->getId()]['total_gross_price'] += $cartItemData['gross_price'];
  111.                             $data['total_quantity'] += $cartItemData['quantity'];
  112.                             $data['total_items_price'] += $item->getPriceListItem()->getPrice();
  113.                             $data['total_items_gross_price'] += $cartItemData['gross_price'];
  114.                             
  115.                             $data['items'][$cartItemId] = &$data['creator_online_services'][$item->getCategory()->getId()]['items'][$cartItemId];
  116.                             
  117.                             if (!in_array($cartItemData['vat'], $data['vat']))
  118.                                 $data['vat'][] = $cartItemData['vat'];
  119.                             break;
  120.                         default:
  121.                             throw new \Exception('Nieznany typ produktu.');
  122.                     }
  123.                 }
  124.                 if ($data['creator_online_services'])
  125.                     $data['contract_preview_step_active'] = true;
  126.                     
  127.                 $data['total_gross_price'] = $data['total_items_gross_price'];
  128.                 
  129.                 // voucher
  130.                 
  131.                 if ($data['voucher'])
  132.                 {
  133.                     if ($data['voucher']['is_percentage'])
  134.                     {
  135.                         $data['voucher']['amount'] = round($data['voucher']['value'] / 100 $data['total_gross_price'], 2);
  136.                         $data['total_gross_price'] -= $data['voucher']['amount'];
  137.                     }
  138.                     else
  139.                     {
  140.                         $data['voucher']['amount'] = $data['voucher']['value'];
  141.                         $data['total_gross_price'] = max($data['total_gross_price'] - $data['voucher']['amount'], 0);
  142.                     }
  143.                 }
  144.             }
  145.             
  146.             self::$_cartDetails $data;
  147.         }
  148.         return self::$_cartDetails;
  149.     }
  150.     
  151.     public function addItem(OnlineService|OnlineServiceCreatorCategoryItem $item$quantity 1, &$totalQuantity null)
  152.     {
  153.         $cartData $this->_getData();
  154.         if ($item instanceof OnlineService)
  155.             $itemType self::ITEM_TYPE_ONLINE_SERVICE;
  156.         elseif ($item instanceof OnlineServiceCreatorCategoryItem)
  157.             $itemType self::ITEM_TYPE_CREATOR_ONLINE_SERVICE;
  158.         else
  159.             throw new \Exception('Nieznay typ produktu.');
  160.         $itemId $itemType '_' $item->getId();
  161.         
  162.         if (isset($cartData['items'][$itemId]))
  163.         {
  164.             if (in_array($itemType, [self::ITEM_TYPE_ONLINE_SERVICEself::ITEM_TYPE_CREATOR_ONLINE_SERVICE]))
  165.             {
  166.                 $cartData['items'][$itemId]['quantity'] = 1;
  167.                 $totalQuantity 1;
  168.             }
  169.             else
  170.             {
  171.                 $cartData['items'][$itemId]['quantity'] += $quantity;
  172.                 $totalQuantity $cartData['items'][$itemId]['quantity'];
  173.             }
  174.             $this->_setData($cartData);
  175.             return true;
  176.         }
  177.         
  178.         $itemData = array(
  179.             'id' => $item->getId(),
  180.             'type' => $itemType,
  181.             'quantity' => $quantity
  182.         );
  183.         $cartData['items'][$itemId] = $itemData;
  184.         $totalQuantity $quantity;
  185.  
  186.         $this->_setData($cartData);
  187.         
  188.         return true;
  189.     }
  190.     
  191.     public function deleteItem($id)
  192.     {
  193.         $cartData $this->_getData();
  194.         
  195.         unset($cartData['items'][$id]);
  196.         
  197.         $this->_setData($cartData);
  198.     }
  199.     
  200.     public function updateCartFromForm(Form $form)
  201.     {
  202.         $formType $form->getConfig()->getType()->getInnerType();
  203.         
  204.         $cartData $this->_getData();
  205.         // pozycje w koszyku
  206.         foreach(array_keys($cartData['items']) as $itemId)
  207.         {
  208.             $itemExists false;
  209.             
  210.             $idParts explode('_'$itemId);
  211.             
  212.             if (count($idParts) == 2)
  213.             {
  214.                 switch($idParts[0])
  215.                 {
  216.                     case self::ITEM_TYPE_ONLINE_SERVICE:
  217.                         $itemExists in_array($itemIdarray_keys($form['onlineServices']->getData()));
  218.                         break;
  219.                     case self::ITEM_TYPE_CREATOR_ONLINE_SERVICE:
  220.                         $itemExists in_array($itemIdarray_keys($form['creatorOnlineServices']->getData()));
  221.                         break;
  222.                 }
  223.             }
  224.             
  225.             if (!$itemExists)
  226.                 unset($cartData['items'][$itemId]);
  227.         }
  228.         $this->_setData($cartData);
  229.         $formType->cartData $this->getDetails();
  230.     }
  231.     
  232.     public function setAccountData(Form $form)
  233.     {
  234.         $cartData $this->_getData();
  235.         
  236.         $cartData['account_data'] = array(
  237.             'email' => $form['email']->getData(),
  238.             'password' => $form['password']->getData()
  239.         );
  240.         
  241.         $this->_setData($cartData);        
  242.     }
  243.     
  244.     public function setClientData(Form $form)
  245.     {
  246.         $cartData $this->_getData();
  247.         
  248.         // dane klienta
  249.         
  250.         $cartData['client_data'] = $form->getData();
  251.         
  252.         unset($cartData['client_data']['delivery']);
  253.         $this->_setData($cartData);
  254.     }
  255.     
  256.     public function setPaymentData(Form $form)
  257.     {
  258.         $data $this->_getData();
  259.         $data['payment_data'] = $form->getData();
  260.         
  261.         $this->_setData($data);
  262.     }
  263.     public function setVoucher(?string $id null, ?string $number null, ?float $value null, ?bool $isPercentageValue null)
  264.     {
  265.         $cartData $this->_getData();
  266.         
  267.         $cartData['voucher'] = $id ? array(
  268.             'id' => $id,
  269.             'number' => $number,
  270.             'is_percentage' => $isPercentageValue,
  271.             'value' => $value
  272.         ) : null;
  273.         
  274.         $this->_setData($cartData);
  275.     }
  276.     
  277.     public function setUserPanelCartView(bool $set)
  278.     {
  279.         $data $this->_getData();
  280.         
  281.         $data['is_user_panel_cart'] = $set;
  282.         
  283.         $this->_setData($data);
  284.     }
  285.     
  286.     public function isValid()
  287.     {
  288.         $data $this->getDetails();
  289.         if (!$this->_userService->getLoggedUser(false) && empty($data['account_data']))
  290.             return false;
  291.         
  292.         if (empty($data['items']) || empty($data['client_data']))
  293.             return false;
  294.         
  295.         return true;
  296.     }
  297.     
  298.     public function clear()
  299.     {
  300.         $oldData $this->_getData();
  301.         
  302.         $this->_setData(null);
  303.         
  304.         $data $this->_getData();
  305.         $data['is_user_panel_cart'] = $oldData['is_user_panel_cart'];
  306.         
  307.         $this->_setData($data);
  308.     }
  309.     
  310.     private function _getData()
  311.     {
  312.         $data $this->_requestStack->getSession()->get($this->_sessionKey);
  313.         
  314.         if (!$data)
  315.         {
  316.             $data = array(
  317.                 'items' => array(),
  318.                 'account_data' => null,
  319.                 'client_data' => null,
  320.                 'voucher' => null,
  321.                 'is_user_panel_cart' => false
  322.             );
  323.         }
  324.         
  325.         return $data;
  326.     }
  327.     
  328.     private function _setData($data)
  329.     {
  330.         self::$_cartDetails null;
  331.         $this->_requestStack->getSession()->set($this->_sessionKey$data);
  332.     }
  333. }