src/Repository/UserCartRepository.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Sales;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use App\Entity\User;
  7. use App\Entity\Customer;
  8. class UserCartRepository extends ServiceEntityRepository {
  9.     
  10.     public function __construct(ManagerRegistry $registry) {
  11.         parent::__construct($registry, \App\Entity\Usercart::class);
  12.     }
  13.     
  14.     /**
  15.      * Add a new line to the cart and returns the Id of that line (in case we need to set it as father id's on related products or similar.
  16.      * @param type $userId
  17.      * @param type $pbwId
  18.      * @param type $quantity
  19.      * @param type $volume
  20.      * @param type $domain
  21.      * @param type $relatedCartId
  22.      * @return object
  23.      */
  24.     public function addItem($userCart$userId$pbwId$volume$quantity$customer$domain null$relatedCartId null$productPromotion=null$bo=null$productBoCustomer=null$stock$overage) {
  25.         $entityManager $this->getEntityManager();
  26.         
  27.         $userCart->setUser($userId);
  28.         $userCart->setPbw($pbwId);
  29.         $userCart->setVolume($volume);
  30.         $userCart->setQuantity($quantity);
  31.         $userCart->setDomain($domain);
  32.         $userCart->setProductPromotion($productPromotion);
  33.         $userCart->setCustomer($customer);
  34.         $userCart->setProductBusinessOportunities($bo);
  35.         $userCart->setStock($stock);
  36.         $userCart->setOverage($overage);
  37.         $userCart->setProductBusinessOpportunityCustomer($productBoCustomer);
  38.         
  39.         if (!is_null($relatedCartId)) {
  40.             $relatedId $userCart->getRelatedCartId();
  41.             if (!is_null($relatedId)) {
  42.                 $relatedId[] = $relatedCartId;
  43.                 $userCart->setRelatedCartId($relatedId);
  44.             } else {
  45.                 $userCart->setRelatedCartId(array($relatedCartId));
  46.             }
  47.         }
  48.         $userRepository $this->getEntityManager()->getRepository(User::class);
  49.         $user $userRepository->find($userId);
  50.         if($user->getPartner()){
  51.             $partnerId=$user->getPartner()->getId();
  52.         }
  53.         elseif($user->getCustomer()){
  54.             $partnerId=$user->getCustomer()->getPartner()->getId();
  55.         }
  56.         $lastItemFromActualCart=$this->createQueryBuilder('uc')
  57.             ->where('uc.unregistrationdate is NULL and uc.sale is NULL')
  58.             ->andWhere('pbw.unregistrationdate is NULL')
  59.             ->andWhere('uc.cartReference is not NULL')
  60.             ->leftJoin('uc.pbw''pbw')
  61.             ->leftJoin('uc.customer''c')
  62.             ->leftJoin('c.partner''p')          
  63.             ->andWhere('p.id = :partner')          
  64.             ->setParameter('partner'$partnerId)
  65.             ->orderBy('uc.createdAt''DESC')
  66.             ->setMaxResults(1)
  67.             ->getQuery()
  68.             ->getOneOrNullResult();
  69.         if($lastItemFromActualCart) {
  70.             $userCart->setCartReference($lastItemFromActualCart->getCartReference());
  71.         }
  72.         else
  73.         {
  74.             $cartReference time() . '-' rand(0999999);
  75.             $userCart->setCartReference($cartReference);
  76.         }
  77.         $entityManager->persist($userCart);
  78.         $entityManager->flush();
  79.         return $userCart;
  80.     }
  81.     
  82.     
  83.     /**
  84.      * Add a related cart Id to the specific cart item.
  85.      * @param type $relatedCartId
  86.      * @return object
  87.      */
  88.     public function addRelatedId($cartItem$relatedCartId) {       
  89.         $entityManager $this->getEntityManager();
  90.         
  91.         $relatedId $cartItem->getRelatedCartId();
  92.         
  93.         if (!is_null($relatedId)) {
  94.             $relatedId[] = $relatedCartId;
  95.             $cartItem->setRelatedCartId($relatedId);
  96.         } else {
  97.             $cartItem->setRelatedCartId(array($relatedCartId));
  98.         }
  99.         
  100.         $entityManager->persist($cartItem);
  101.         $entityManager->flush();
  102.         
  103.         
  104.         return $this;
  105.     }
  106.     
  107.     
  108.     public function MyUserCartQuery($user) {
  109.     
  110.         $qb $this->createQueryBuilder('uc')
  111.             ->where('uc.user = :userId and uc.unregistrationdate is NULL and uc.sale is NULL')
  112.             ->andWhere('pbw.unregistrationdate is NULL')
  113.             ->leftJoin('uc.pbw''pbw')
  114.             ->setParameter('userId'$user->getId());
  115.         
  116.         return $qb;  
  117.     }
  118.     public function MyCustomerCartQuery($user) {
  119.     
  120.         if($user->getPartner()){
  121.             $qb $this->createQueryBuilder('uc')
  122.                 ->where('uc.unregistrationdate is NULL and uc.sale is NULL')
  123.                 ->leftJoin('uc.pbw''pbw')
  124.                 ->leftJoin('uc.customer''c')
  125.                 ->leftJoin('c.partner''p')
  126.                 ->andWhere('pbw.unregistrationdate is NULL')
  127.                 ->andWhere('p.id = :partner')      
  128.                 ->setParameter('partner'$user->getPartner()->getId());    
  129.         }
  130.         elseif($user->getCustomer()){
  131.             $qb $this->createQueryBuilder('uc')
  132.                 ->where('uc.unregistrationdate is NULL and uc.sale is NULL')
  133.                 ->leftJoin('uc.pbw''pbw')
  134.                 ->andWhere('pbw.unregistrationdate is NULL')
  135.                 ->andWhere('uc.customer = :customer')      
  136.                 ->setParameter('customer'$user->getCustomer()->getId());    
  137.         }
  138.         
  139.         return $qb;  
  140.     }
  141.     public function getNumberCustomerCarts($user) {
  142.                
  143.         if(!is_null($user->getPartner())){
  144.             $qb $this->createQueryBuilder('uc')
  145.                 ->select('DISTINCT IDENTITY(uc.customer)')      
  146.                 ->where('uc.user = :userId and uc.unregistrationdate is NULL and uc.sale is NULL')           
  147.                 ->join('uc.pbw''pbw')
  148.                 ->join('uc.customer''cus')
  149.                 ->andWhere('pbw.unregistrationdate is NULL ')
  150.                 ->andWhere('cus.partner = :userPartner')
  151.                 ->setParameter('userId'$user->getId())
  152.                 ->setParameter('userPartner'$user->getPartner()->getId())
  153.                 ->getQuery()
  154.                 ->getResult();
  155.             return sizeof($qb);    
  156.         }
  157.         elseif(!is_null($user->getCustomer())){
  158.             $qb $this->createQueryBuilder('uc')                  
  159.                 ->where('uc.unregistrationdate is NULL and uc.sale is NULL')           
  160.                 ->join('uc.pbw''pbw')
  161.                 ->andWhere('pbw.unregistrationdate is NULL ')
  162.                 ->andWhere('uc.customer = :customer')
  163.                 ->setParameter('customer'$user->getCustomer())
  164.                 ->getQuery()
  165.                 ->getResult();
  166.             return sizeof($qb);
  167.         }
  168.         else{
  169.             return 0;    
  170.         }   
  171.     }
  172.     public function MyCustomersCartsQuery($user) {
  173.              
  174.         $customerRepository $this->getEntityManager()->getRepository(Customer::class);
  175.         if(!is_null($user->getPartner())){
  176.             $qb $this->createQueryBuilder('uc')
  177.                 ->select('DISTINCT cus.id')      
  178.                 ->join('uc.pbw''pbw')
  179.                 ->join('uc.customer''cus')
  180.                 ->where('uc.unregistrationdate IS NULL')
  181.                 ->andWhere('uc.sale IS NULL')
  182.                 ->andWhere('pbw.unregistrationdate IS NULL')
  183.                 ->andWhere('cus.partner = :userPartner')
  184.                 ->setParameter('userPartner'$user->getPartner()->getId())
  185.                 ->getQuery()
  186.                 ->getResult();
  187.             
  188.             $customers=array();
  189.                 
  190.             foreach($qb as $customerId)
  191.             {
  192.                 array_push($customers$customerRepository->find($customerId));
  193.             }
  194.             
  195.             return $customers;
  196.         }
  197.     }
  198.     
  199.     public function MyCustomersCartQuery() {
  200.     
  201.         $qb $this->createQueryBuilder('uc')
  202.             ->where('uc.unregistrationdate is NULL and uc.sale is NULL');
  203.         
  204.         return $qb;  
  205.     }
  206.     
  207.     /**
  208.      * Method that checks if any related Id is unregistered on the PBW
  209.      * 
  210.      * @return boolean
  211.      */
  212.     public function checkRelatedId($cartItem) {
  213.                
  214.         if ($cartItem->getRelatedCartId()) {
  215.             foreach ($cartItem->getRelatedCartId() as $relatedId) {
  216.                 $relatedIdCart $this->find($relatedId);
  217.                 if ($relatedIdCart->getPbw()->getUnregistrationdate()) {
  218.                     return false;
  219.                 }              
  220.             }
  221.         }
  222.         return true;
  223.     }
  224. }
  225.