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

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