src/Model/Cart.php line 405

Open in your IDE?
  1. <?php
  2. namespace App\Model;
  3. use App\Env;
  4. use App\DTO\AppDTO;
  5. use App\DTO\CartDTO;
  6. use App\Entity\Prod;
  7. use App\Model\ProdColor;
  8. use App\Service\Auth\Auth;
  9. use App\ValueObject\Money;
  10. use App\Entity\CartUnsaved;
  11. use App\Entity\Cart as EntityCart;
  12. use App\Repository\CartRepository;
  13. use App\Repository\ProdRepository;
  14. use App\Service\Discount\NumDiscount;
  15. use App\Service\Discount\SumDiscount;
  16. use App\Service\Discount\UserDiscount;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use App\Repository\CartUnsavedRepository;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Session\Session;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. class Cart
  24. {
  25.     protected EntityManagerInterface $em;
  26.     protected AppDTO $app;
  27.     protected Auth $Auth;
  28.     protected SessionInterface $Session;
  29.     protected ProdColor $prodColor;
  30.     protected RequestStack $rs;
  31.     protected $cart;
  32.     protected $prods_limited = [];
  33.     protected $order_id;
  34.     protected NumDiscount $NumDiscount;
  35.     protected SumDiscount $SumDiscount;
  36.     protected UserDiscount $UserDiscount;
  37.     //Repository
  38.     protected ProdRepository $Prods;
  39.     protected CartUnsavedRepository $CartUnsaved;
  40.     protected CartRepository $Carts;
  41.     public function __construct(EntityManagerInterface $emAppDTO $appAuth $AuthNumDiscount $NumDiscountSumDiscount $SumDiscountUserDiscount $UserDiscountProdColor $prodColorRequestStack $rsSecurity $security)
  42.     {
  43.         $this->em $em;
  44.         $this->app $app;
  45.         $this->Auth $Auth;
  46.         $this->Auth->setUser($security->getUser());
  47.         $this->prodColor $prodColor;
  48.         $this->rs $rs;
  49.         $this->Session $rs->getSession();
  50.         $this->Prods $this->em->getRepository(Prod::class);
  51.         $this->CartUnsaved $this->em->getRepository(CartUnsaved::class);
  52.         $this->Carts $this->em->getRepository(EntityCart::class);
  53.         $this->NumDiscount $NumDiscount;
  54.         $this->SumDiscount $SumDiscount;
  55.         $this->UserDiscount $UserDiscount;
  56.         $this->cart $this->Session->get('cart');
  57.         
  58.         if (empty($this->cart)) {
  59.             $this->cart = [];
  60.         }
  61.         if (empty($this->cart)) {
  62.             $this->load_session();
  63.         }
  64.         $this->recount();
  65.         $this->flush();
  66.     }
  67.     public function getCart(): array
  68.     {
  69.         $deleted 0;
  70.         foreach ($this->cart as $k => $v) {
  71.             $prod $this->Prods->find($v['id']);
  72.             if ($prod == null) {
  73.                 $deleted 1;
  74.                 unset ($this->cart[$k]);
  75.             }
  76.         }
  77.         if ($deleted) {
  78.             $this->save_session();
  79.             $this->flush();
  80.         }
  81.         return $this->cart;
  82.     }
  83.     public function isFreeDelivery(float $free_delivery_min): bool
  84.     {
  85.         $freedelivery false;
  86.         if ($this->getAmount() >= $free_delivery_min && !$this->Auth->isOpt()) {
  87.             $freedelivery true;
  88.         }
  89.         return $freedelivery;
  90.     }
  91.     public function loadFromOrder(int $order_id)
  92.     {
  93.         $this->order_id $order_id;
  94.         /** @var \App\Entity\Cart[] $prods */
  95.         $prods $this->em->createQuery("SELECT c FROM App\Entity\Cart c WHERE c.order_id = ".$order_id)->getResult();
  96.                 
  97.         foreach($prods as $prod) {
  98.             $this->cart[$this->getCartId($prod->getProd()->getId(), 0)] = [
  99.                 'cid' => $prod->getId(),
  100.                 'id' => $prod->getProd()->getId(),
  101.                 'var' => $prod->getVar(),
  102.                 'num' => $prod->getNum(),
  103.                 'baseprice' => (new Money($prod->getPrice()))->getAmount(),
  104.                 'price' => 0,
  105.                 'skidka' => $prod->getSkidka(),
  106.                 'numdiscount' => $prod->getNumdiscount(),
  107.                 'userdiscount' => $prod->getUserdiscount(),
  108.             ];
  109.         }
  110.         $this->flush();
  111.     }
  112.     public function getCartId(int $prod_idint $var 0, array $chars = [], int $user_id 0) {
  113.         $id $prod_id."_".$var;
  114.             
  115.         if (!empty($chars)) {
  116.             ksort($chars);
  117.             $id .= "_".json_encode($chars);                
  118.         }
  119.             
  120.         if ($user_id) {
  121.             $id .= "_".$user_id;
  122.         }
  123.             
  124.         return md5($id);
  125.     }
  126.     public function addItem($id 0$var 0$num 1$price 0$skidka 0$numdiscount = [], $weight 0){
  127.         $cart_id $this->getCartId($id$var);
  128.             
  129.         if(isset($this->cart[$cart_id])){
  130.             $this->cart[$cart_id]['num'] += $num;
  131.             if($this->cart[$cart_id]['skidka'] == 0){
  132.                 $this->cart[$cart_id]['numdiscount'] = $this->NumDiscount->getDiscount($num$numdiscount);
  133.                 $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  134.             }else{
  135.                 $this->cart[$cart_id]['numdiscount'] = 0;
  136.                 $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  137.             }
  138.         }else{
  139. //            array_unshift($this->cart, array($cart_id => array('id' => intval($id), 'var' => intval($var), 'num' => intval($num), 'baseprice' => floatval($price), 'price' => floatval($price), 'skidka' => floatval($skidka), 'weight' => floatval($weight))));
  140.             $this->cart[$cart_id] = array('id' => intval($id), 'var' => intval($var), 'num' => intval($num), 'baseprice' => floatval($price), 'price' => floatval($price), 'skidka' => floatval($skidka), 'weight' => floatval($weight));
  141.     
  142.             if($this->cart[$cart_id]['skidka'] == 0){
  143.                 $this->cart[$cart_id]['numdiscount'] = $this->NumDiscount->getDiscount($num$numdiscount);
  144.                 $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  145.             }else{
  146.                 $this->cart[$cart_id]['numdiscount'] = 0;
  147.                 $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  148.             }
  149.         }
  150.         $this->recount();
  151.         $this->save_session();
  152.         $this->flush();
  153.     }
  154.     public function updateItem($cart_id ''$num 0$numdiscount = []){
  155.         if($num == 0){
  156.             $prod $this->cart[$cart_id]['id'];
  157.             $prodvar $this->cart[$cart_id]['var'];
  158.             unset($this->cart[$cart_id]);
  159.         }else{
  160.             $this->cart[$cart_id]['num'] = intval($num);
  161.             if($this->order_id == 0) {
  162.                 if($this->cart[$cart_id]['skidka'] == 0){
  163.                     $this->cart[$cart_id]['numdiscount'] = $this->NumDiscount->getDiscount($num$numdiscount);
  164.                     $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  165.                 }else{
  166.                     $this->cart[$cart_id]['numdiscount'] = 0;
  167.                     $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  168.                 }
  169.             } else {
  170.                 if($this->cart[$cart_id]['skidka'] == 0){
  171.                     $this->cart[$cart_id]['numdiscount'] = $this->NumDiscount->getDiscount($num$numdiscount);
  172.                     $this->cart[$cart_id]['userdiscount'] = $this->UserDiscount->getValue();
  173.                 }else{
  174.                     $this->cart[$cart_id]['numdiscount'] = 0;
  175.                     $this->cart[$cart_id]['userdiscount'] = 0;
  176.                 }
  177.             }
  178.                 
  179.             $this->recount();
  180.             $prod intval($this->cart[$cart_id]['id']);
  181.             $prodvar intval($this->cart[$cart_id]['var']);
  182.         }
  183.         $this->save_session();
  184.         $this->flush();
  185.     }
  186.     public function deleteItem(string $cart_id)
  187.     {
  188.         $prod $this->cart[$cart_id]['id'];
  189.         $prodvar $this->cart[$cart_id]['var'];
  190.         unset($this->cart[$cart_id]);
  191.         $this->recount();
  192.         $this->save_session();
  193.         $this->flush();
  194.     }
  195.     public function deleteAll()
  196.     {
  197.         $this->cart = [];
  198.         $this->delete_session();
  199.         $this->flush();
  200.     }    
  201.     public function cart_id($prod 0$var 0$chars = array(), $user_id 0)
  202.     {
  203.         $this->getCartId($prod$var$chars$user_id);        
  204.     }
  205.                 
  206.     public function getAmount(): float
  207.     {
  208.         $amount 0;
  209.         foreach($this->cart as $k => $v)
  210.             $amount += round($v['price'], Env::price_precission()) * $v['num'];
  211.             
  212.         return $amount;
  213.     }
  214.     public function getBaseAmount(): float 
  215.     {
  216.         $amount 0;
  217.         foreach($this->cart as $k => $v)
  218.             $amount += round($v['baseprice'], Env::price_precission()) * $v['num'];
  219.             
  220.         return $amount;
  221.     }
  222.     public function getAmountWithoutDiscount()
  223.     {
  224.         $amount 0;
  225.             
  226.         foreach ($this->cart as $k => $v) {
  227.             $amount += round($v['baseprice'], Env::price_precission()) * $v['num'];
  228.         }
  229.             
  230.         return $amount;
  231.     }
  232.     public function getAmountDelivery()
  233.     {
  234.         return (float)@$_SESSION['_sf2_attributes']['checkout']['delivery_cost'];
  235.     }
  236.     public function getAmountWithDelivery()
  237.     {
  238.         if (($this->getAmount() < $this->app->sett->get('free_delivery_amount')) || ($this->Auth->isOpt())) {
  239.             return $this->getAmount() + $this->getAmountDelivery();
  240.         } else {
  241.             return $this->getAmount();
  242.         }
  243.     }
  244.     public function userLogin($user_id)
  245.     {
  246.         $this->load_session($user_id);
  247.         $discount $this->UserDiscount->getValue();
  248.         if ($discount >= 0) {
  249.             foreach ($this->cart as $k => $v) {
  250.                 $prod $this->Prods->find((int)$v['id']);
  251.                 $numdiscount $prod->getNumdiscount();
  252.         
  253.                 if($v['var'] == 2){
  254.                     $numdiscount $prod->getNumdiscount2();
  255.                 }
  256.     
  257.                 if($v['var'] == 3){
  258.                     $numdiscount $prod->getNumdiscount3();
  259.                 }
  260.                 
  261.                 $this->cart[$k]['userdiscount'] = $discount;
  262.                 if($this->Auth->isOpt()) {
  263.                     $this->cart[$k]['numdiscount'] = $this->NumDiscount->getDiscount((int) $this->cart[$k]['num'], $numdiscount);
  264.                 }
  265.             }
  266.         }
  267.         $this->recount();
  268.         $this->save_session();
  269.         $this->em->createQuery("DELETE App\Entity\CartUnsaved c WHERE c.user_id = '".$this->Auth->guestId()."'")->getResult();
  270.         $this->flush();
  271.     }
  272.         
  273.     public function setUserDiscount($discount){
  274.         if($discount){
  275.             foreach($this->cart as $k => $v){
  276.                 $this->cart[$k]['userdiscount'] = $discount;
  277.             }
  278.         }
  279.         $this->recount();
  280.         $this->save_session();
  281.         $this->flush();
  282.     }
  283.         
  284.     protected function recount()
  285.     {
  286.         foreach ($this->cart as $k => $v) {
  287.             if (!isset($v['userdiscount'])) $this->cart[$k]['userdiscount'] = 0;
  288.             if (!isset($v['numdiscount'])) $this->cart[$k]['numdiscount'] = 0;
  289.             if (!isset($v['skidka'])) $this->cart[$k]['skidka'] = 0;
  290.             
  291.             if ($this->order_id == 0) {
  292.                 $prod $this->Prods->find((int) $v['id']);
  293.                 if ($prod) {
  294.                     $skidka $prod->getSkidka();
  295.             
  296.                     if ($v['var'] == 2) {
  297.                         $skidka $prod->getSkidka2();
  298.                     }
  299.     
  300.                     if ($v['var'] == 3) {
  301.                         $skidka $prod->getSkidka3();
  302.                     }
  303.                 
  304.                     $this->cart[$k]['skidka'] = $skidka;
  305.                 }                
  306.             }
  307.             
  308.             $this->cart[$k]['baseprice'] = (new Money($v['baseprice']))->getAmount();
  309.             if ($v['skidka']) {
  310.                 $this->cart[$k]['price'] = (new Money(($v['baseprice']) * (100 $v['skidka']) / 100))->getAmount();
  311.             } else {
  312.                 $this->cart[$k]['price'] = (new Money((($v['baseprice']) * (100 $v['userdiscount']) * (100 $v['numdiscount']) / 100 100)))->getAmount();
  313.             }                
  314.         }    
  315.     }
  316.         
  317.     public function getProdNum()
  318.     {
  319.         return (empty($this->cart)) ? count($this->cart);
  320.     }
  321.     public function getPackNum()
  322.     {
  323.         $num 0;
  324.         foreach($this->cart as $k => $v)
  325.             $num += $v['num'];
  326.         return $num;
  327.     }
  328.     public function getWeight()
  329.     {
  330.         $weight 0;
  331.         foreach($this->cart as $k => $v)
  332.         {
  333.             $n $v['num'] ?? 0;
  334.             $w $v['weight'] ?? 0;
  335.             $weight += $n $w;
  336.         }
  337.             
  338.         return round($weight 10002);
  339.     }
  340.     protected function load_session()
  341.     {
  342.         $uid $this->Auth->getUserId() ? $this->Auth->getUserId(): $this->Auth->guestId();
  343.         $unsaved_cart $this->CartUnsaved->findOneBy(["user_id" => $uid]);
  344.         if ($unsaved_cart) {
  345.             $cart json_decode($unsaved_cart->getCart(), true);    
  346.             foreach ($cart as $k => $v) {
  347.                 $prod $this->Prods->find((int) $v['id']);
  348.                 
  349.                 $v['baseprice'] = $prod->getPrice();
  350.                 if ($v['var'] == 2) { 
  351.                     $v['baseprice'] = $prod->getPrice2();
  352.                 } elseif ($v['var'] == 3) {
  353.                     $v['baseprice'] = $prod->getPrice3();
  354.                 }
  355.                 
  356.                 if (!isset($this->cart[$k])) {
  357.                     $this->cart[$k] = $v;
  358.                 }                
  359.             }
  360.         }
  361.         $this->flush();
  362.     }
  363.     private function flush()
  364.     {
  365.         $this->Session->set('cart'$this->cart);
  366.     }
  367.     public function save_session()
  368.     {
  369.         $uid $this->Auth->getUserId() ? $this->Auth->getUserId(): $this->Auth->guestId();
  370.         $cart json_encode($this->cartJSON_UNESCAPED_UNICODE);
  371.         $unsaved_cart $this->CartUnsaved->findOneBy(["user_id" => $uid]);
  372.         if ($unsaved_cart) {
  373.             $unsaved_cart->setCart($cart);
  374.         } else {
  375.             $unsaved_cart = new CartUnsaved();
  376.             $unsaved_cart->setUserId($uid);
  377.             $unsaved_cart->setCart($cart);
  378.             $this->em->persist($unsaved_cart);
  379.         }
  380.         $this->em->flush();
  381.     }
  382.     public function delete_session()
  383.     {
  384.         $uid $this->Auth->getUserId() ? $this->Auth->getUserId(): $this->Auth->guestId();
  385.         $unsaved_cart $this->CartUnsaved->findOneBy(["user_id" => $uid]);
  386.         if ($unsaved_cart) {
  387.             $this->em->remove($unsaved_cart);
  388.             $this->em->flush();
  389.         }
  390.     }
  391.         
  392.     public function saveCart(int $order_id) {
  393.         // if ($this->Auth->isAuth() || !$this->Auth->isOpt()) {
  394.         //     $userdiscount = $this->UserDiscount->calculateUserDiscount(new Model_Discount, new Model_Order(), new Model_User(), Auth::userid());
  395.         // } else {
  396.         //     $userdiscount = 0;
  397.         // }
  398.         
  399.         foreach ($this->cart as $k => $v) {
  400.             $prod $this->Prods->find((int) $v['id']);
  401.             $price $prod->getPrice();
  402.             $num $prod->getNum();
  403.             
  404.             if ($v['skidka']) {
  405.                 $v['numdiscount'] = 0;
  406.             }
  407.                 
  408.             $Cart = new EntityCart();
  409.             $Cart->setOrderId($order_id);
  410.             $Cart->setProd($prod);
  411.             $Cart->setVar((int) $v['var']);
  412.             $Cart->setNum((int) $v['num']);
  413.             $Cart->setPrice(round($priceEnv::price_precission()));
  414.             $Cart->setSkidka((int) $v['skidka']);
  415.             $Cart->setNumdiscount((int) $v['numdiscount']);
  416.             $Cart->setUserdiscount((int) $v['userdiscount']);
  417.             //$Cart->setSumdiscount((int) $v['sumdiscount']);
  418.             $this->em->persist($Cart);                
  419.         }
  420.         $this->em->flush();
  421.         $this->flush();
  422.     }
  423.     //Удалить товары с нулевыми остатками из цветов
  424.     public function deleteNullFromColors()
  425.     {
  426.         foreach ($this->cart as $k => $v) {
  427.             $prod $this->Prods->find((int) $v['id']);
  428.             $prod_num $prod->getNum();
  429.             if ($v['var'] == 2$prod_num $prod->getNum2();
  430.             if ($v['var'] == 3$prod_num $prod->getNum3();
  431.             if ($prod->getId() == $v['id'] && $prod_num && $prod_num $v['num']) {
  432.                 $this->prods_limited[] = $v['id'];
  433.                 $this->cart[$k]['num'] = $prod_num;
  434.             }
  435.             if ($prod->getId() == $v['id'] && $prod_num <= 0) {
  436.                 $this->prodColor->deleteColor($prod->getId());
  437.             }
  438.         }
  439.         $this->flush();
  440.     }
  441.     public function save_cart_admin($order_id) {
  442.         $this->em->createQuery("DELETE App\Entity\Cart c WHERE c.order = ".$order_id)->getResult();
  443.         
  444.         foreach ($this->cart as $v) {
  445.             $prod $this->Prods->find((int) $v['id']);
  446.             if ($v['skidka']) {
  447.                 $v['numdiscount'] = 0;
  448.             }
  449.             $Cart = new EntityCart();
  450.             $Cart->setOrderId($order_id);
  451.             $Cart->setProd($prod);
  452.             $Cart->setVar((int) $v['var']);
  453.             $Cart->setNum((int) $v['num']);
  454.             $Cart->setPrice(round($v['baseprice'], Env::price_precission()));
  455.             $Cart->setSkidka((int) $v['skidka']);
  456.             $Cart->setNumdiscount((int) $v['numdiscount']);
  457.             $Cart->setUserdiscount((int) $v['userdiscount']);
  458.             $Cart->setSumdiscount((int) $v['sumdiscount']);
  459.             $this->em->persist($Cart);
  460.         }
  461.         $this->em->flush();
  462.         $this->flush();
  463.     }
  464.     
  465.     public function prods_limited() {
  466.         foreach ($this->cart as $k => $v) {
  467.             $prod $this->Prods->find((int) $v['id']);
  468.             $prod_num $prod->getNum();
  469.             if ($v['var'] == 2$prod_num $prod->getNum2();
  470.             if ($v['var'] == 3$prod_num $prod->getNum3();
  471.         
  472.             if ($prod->getId() == $v['id'] && $prod_num && $prod_num $v['num']) {
  473.                 $this->prods_limited[] = $v['id'];
  474.                 $this->cart[$k]['num'] = $prod_num;
  475.             }
  476.             if ($prod->getId() == $v['id'] && $prod_num <= 0) {
  477.                 unset($this->cart[$k]);
  478.             }
  479.         }
  480.         $this->flush();
  481.         $this->save_session();
  482.         if (!empty($this->prods_limited)) return true;
  483.         else return false;
  484.     }
  485.     public function getProdsLimited (): array
  486.     {
  487.         return $this->prods_limited;
  488.     }
  489. }