src/Services/Helpers/LeadHelper.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Services\Helpers;
  3. use App\Entity\CoreBundle\Lead;
  4. use App\Entity\CoreBundle\LeadHistory;
  5. use App\Factory\Leads\LeadFactory;
  6. use App\Services\Authorization;
  7. use App\Services\Currency;
  8. use App\Services\DeleteEntities;
  9. use App\Services\Exporter;
  10. use App\Services\myMailer;
  11. use App\Services\RESTApiService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Psr\Container\ContainerInterface;
  15. use Symfony\Component\Validator\Validator\ValidatorInterface;
  16. class LeadHelper
  17. {
  18.     private Currency $currency;
  19.     private EntityManagerInterface $em;
  20.     private UserHelper $userHelper;
  21.     private Logger $logger;
  22.     private $twig;
  23.     public function __construct(EntityManagerInterface $emCurrency $currencyUserHelper $userHelperLogger $logger,
  24.                                 ContainerInterface $container)
  25.     {
  26.         $this->currency $currency;
  27.         $this->em $em;
  28.         $this->userHelper $userHelper;
  29.         $this->logger $logger;
  30.         $this->twig $container->get('twig');
  31.     }
  32.     
  33.     
  34.     public function isAllLeadsHaveSameStatus(array $leads): array
  35.     {
  36.         $allLeadsHaveSameStatus true;
  37.         $firstLeadStatusId = -1;
  38.         $counter 0;
  39.         foreach ($leads as $lead)
  40.         {
  41.             if($counter == 0)
  42.             {
  43.                 $firstLeadStatusId $lead->getStatus()->getId();
  44.                 $counter++;
  45.             }
  46.             else
  47.             {
  48.                 if($lead->getStatus()->getId() != $firstLeadStatusId)
  49.                 {
  50.                     $allLeadsHaveSameStatus false;
  51.                     break;
  52.                 }
  53.             }
  54.         }
  55.         return [ "allLeadsHaveSameStatus" => $allLeadsHaveSameStatus ,
  56.             "statusId" => $firstLeadStatusId];
  57.     }
  58.     public function calculateLeadTotalPrice(Lead $lead): array
  59.     {
  60.         //(1).1st PC price
  61.         $mainUpSellPrice $lead->getMainUpSellPrice();
  62.         $mainUpSellPriceInSystem $lead->getMainUpSellPriceInSystem();
  63.         $mainUpSellPriceWithCurrency $this->currency->priceFormat($mainUpSellPrice $lead->getOffer()->getCountry()->getCurrencySymbol());
  64.         $totalNumberOfAllPurchasedItems 1;
  65.         //(2).UpSells Price
  66.         $upSellPrice 0;
  67.         $upSellPriceInSystem 0;
  68.         if(count($lead->getExtraUpSells()) > 0)
  69.         {
  70.             foreach ($lead->getExtraUpSells() as $upSell)
  71.             {
  72.                 $upSellPrice += $upSell->getTotalPriceInCountry();
  73.                 $upSellPriceInSystem += $upSell->getTotalPriceInSystem();
  74.                 $totalNumberOfAllPurchasedItems += $upSell->getNumberOfPieces();
  75.             }
  76.         }
  77.         $upSellPriceWithCurrency $this->currency->priceFormat($upSellPrice $lead->getOffer()->getCountry()->getCurrencySymbol());
  78.         //(3).Cross Sells Price
  79.         $crossSellsPrice 0//1
  80.         $crossSellsPriceInSystem 0;
  81.         foreach ($lead->getCrossSells() as $crossSell)
  82.         {
  83.             $crossSellsPrice += $crossSell->getUnitPriceInCountry() *  $crossSell->getNumberOfPieces();
  84.             $crossSellsPriceInSystem += $crossSell->getUnitPriceInSystem() *  $crossSell->getNumberOfPieces();
  85.             $totalNumberOfAllPurchasedItems += $crossSell->getNumberOfPieces();
  86.         }
  87.         $crossSellsPriceWithCurrency $this->currency->priceFormat($crossSellsPrice $lead->getOffer()->getCountry()->getCurrencySymbol());
  88.         //(4).Gifts Price
  89.         $giftsPrice 0;
  90.         $giftsPriceInSystem 0;
  91.         foreach ($lead->getGifts() as $gift)
  92.         {
  93.             $giftsPrice += $gift->getTotalPriceInCountry();
  94.             $giftsPriceInSystem += $gift->getTotalPriceInSystem();
  95.         }
  96.         $giftsPriceWithCurrency $this->currency->priceFormat($giftsPrice $lead->getOffer()->getCountry()->getCurrencySymbol());
  97.         //(5). Taxes + TotalCost
  98.         $subTotal $mainUpSellPrice $upSellPrice $crossSellsPrice $giftsPrice;
  99.         $subTotalInSystem $mainUpSellPriceInSystem $upSellPriceInSystem $crossSellsPriceInSystem $giftsPriceInSystem;
  100.         //Delivery In Progress = 8 / Success Delivery = 9 / Not Paid = 10 / Returned = 11 ------> From Database
  101.         //View
  102.         if(($lead->getStatus()->getId() == || $lead->getStatus()->getId() == || $lead->getStatus()->getId() == 10 ||
  103.                 $lead->getStatus()->getId() == 11) && ($lead->getTotalCostInSystem() > 0))
  104.         {
  105.             $allTaxesPrice $lead->getTotalTaxes();
  106.             $allTaxesPriceInSystem $lead->getTotalTaxesInSystem();
  107.             $totalCost $lead->getTotalCost();
  108.             $deliveryFees $lead->getDeliveryFees();
  109.             $totalCostInSystem $lead->getTotalCostInSystem();
  110.             $totalCostWithCurrency $this->currency->priceFormat($totalCost $lead->getOffer()->getCountry()->getCurrencySymbol());
  111.         }
  112.         else
  113.         {
  114.             $allTaxesPrice 0;
  115.             $allTaxesPriceInSystem 0;
  116.             foreach ($lead->getOffer()->getCountry()->getTaxes() as $tax)
  117.             {
  118.                 if($tax->getOperator() == 'percentage')
  119.                 {
  120.                     $allTaxesPrice += ($tax->getValue() / 100) * $subTotal;
  121.                     $allTaxesPriceInSystem += ($tax->getValueInSystem() / 100) * $subTotal;
  122.                 }
  123.                 else
  124.                 {
  125.                     $allTaxesPrice += $tax->getValue();
  126.                     $allTaxesPriceInSystem += $tax->getValueInSystem();
  127.                 }
  128.             }
  129.             $deliveryFees 0;
  130.             if($lead->getDeliveryType() == "Free")
  131.             {
  132.                 if ($totalNumberOfAllPurchasedItems >= $lead->getNumberOfPCsForFreeDelivery())
  133.                     $deliveryFees 0;
  134.                 else
  135.                 {
  136.                     if($lead->getCity())
  137.                         $deliveryFees $lead->getCity()->getDeliveryFees();
  138.                 }
  139.             }
  140.             else
  141.             {
  142.                 if($lead->getCity())
  143.                     $deliveryFees $lead->getCity()->getDeliveryFees();
  144.             }
  145.             //$deliveryFees is city currency so can only be added here
  146.             $totalCost $subTotal $allTaxesPrice $deliveryFees;
  147.             $totalCostInSystem $subTotalInSystem $allTaxesPriceInSystem;
  148.             $totalCostWithCurrency $this->currency->priceFormat($totalCost $lead->getOffer()->getCountry()->getCurrencySymbol());
  149.         }
  150.         $taxesPriceWithCurrency $this->currency->priceFormat($allTaxesPrice $lead->getOffer()->getCountry()->getCurrencySymbol());
  151.         return ['totalCost' => $totalCost 'totalCostWithCurrency' => $totalCostWithCurrency ,
  152.             'totalTaxes' => $allTaxesPrice 'totalTaxesPriceWithCurrency' => $taxesPriceWithCurrency ,
  153.             'crossSellsPrice' => $crossSellsPriceWithCurrency 'mainUpSellPrice' => $mainUpSellPriceWithCurrency ,
  154.             'upSellsPrice' => $upSellPriceWithCurrency  'giftsPrice' => $giftsPriceWithCurrency ,
  155.             'allTaxesPriceInSystem' => $allTaxesPriceInSystem ,'totalCostInSystem' => $totalCostInSystem ,
  156.             'deliveryFees' => $deliveryFees ];
  157.     }
  158.     public function validateLeadForLoggedInOperator(Lead $lead $params = []): array
  159.     {
  160.         $loggedInUser $this->userHelper->getUser();
  161.         $userType $this->userHelper->getLoggedUserEntity();
  162.         if($userType != 'User')
  163.         {
  164.             $getLog array_key_exists('getLog' $params) ? $params['getLog'] : NULL;
  165.             $callingFunction array_key_exists('callingFunction' $params) ? $params['callingFunction'] : '';
  166.             $failExit false;
  167.             if ($lead->getAssignedOperator() == NULL || $lead->getAssignedOperator()->getId() != $loggedInUser->getId())
  168.             {
  169.                 $failExit true;
  170.                 $message "Operator(" $loggedInUser->getUsername().") Not Assigned to this lead (" $callingFunction ")";
  171.                 $this->logger->createLeadLog('error', ['updatedLead' => $lead,'message' => $message]);
  172.                 $data['errorType'] = "unAssigned";
  173.                 $data["errorTitle"] = "You have been unassigned to this lead !";
  174.             }
  175.             else if($lead->getOperatorPlannedCall() != NULL)
  176.             {
  177.                 $failExit true;
  178.                 $message "Operator(" $loggedInUser->getUsername().") Can't edit Lead in Planned Call List (" $callingFunction .")";
  179.                 $this->logger->createLeadLog('error', ['updatedLead' => $lead,'message' => $message]);
  180.                 $data['errorType'] = "leadInPlannedCallList";
  181.                 $data["errorTitle"] = "This lead in planned call list !";
  182.             }
  183.             if($failExit == true)
  184.             {
  185.                 if($getLog != NULL && $getLog == true)
  186.                 {
  187.                     $logs $this->em->getRepository(Lead::class)->getLeadLogAndLeadComments($lead->getId());
  188.                     $data['logHtml'] = $this->twig->render('CoreBundle/Lead/parts/infoPopupLog.html.twig', ['leadLog' => $logs]);
  189.                     //$data['logHtml'] = $this->renderView(
  190.                 }
  191.                 $data["status"] = "error";
  192.                 $data["errorMessage"] = "Please Refresh The Page";
  193.                 return ['valid' => false 'data' => $data];
  194.             }
  195.         }
  196.         return ['valid' => true];
  197.     }
  198.     public function copyLead(Lead $lead): Lead
  199.     {
  200.         $newLead = new Lead();
  201.         $newLead->setLanguage($lead->getLanguage());
  202.         $newLead->setAddress($lead->getAddress());
  203.         $newLead->setName($lead->getName());
  204.         $newLead->setPhone($lead->getPhone());
  205.         $newLead->setLanguage($lead->getLanguage());
  206.         $newLead->setInformation($lead->getInformation());
  207.         $newLead->setEmail($lead->getEmail());
  208.         $newLead->setCity($lead->getCity());
  209.         $newLead->setLeadSource($lead->getLeadSource());
  210.         $newLead->setStatus($lead->getStatus());
  211.         $newLead->setAssignedOperator($lead->getAssignedOperator());
  212.         return $newLead;
  213.     }
  214.     public function isPlanDeliveryDateStringValid($dateString): bool
  215.     {
  216.         $valid = (bool)strtotime($dateString); //check string
  217.         if(!$valid)
  218.             return false;
  219.         $dateConverted strtotime($dateString);
  220.         $oldTime strtotime("-1 day"time());
  221.         if($dateConverted $oldTime)
  222.             return false;
  223.         return true;
  224.     }
  225.     public function createLead(array $params = []): Lead
  226.     {
  227.         LeadFactory::setDependencies($this->em$this->userHelper);
  228.         $lead LeadFactory::create($params);
  229.         $this->em->persist($lead);
  230.         return $lead;
  231.     }
  232.     public function createNewLeadHistory(Lead $lead): void
  233.     {
  234.         //Create New History
  235.         $result $this->em->getRepository(Lead::class)->isLeadWasSentFromExistedSource($lead->getPhone(), $lead->getEmail());
  236.         if ($result)
  237.         {
  238.             foreach ($result as $matchedLead)
  239.             {
  240.                 if ($matchedLead->getId() != $lead->getId()) //not the same ids
  241.                 {
  242.                     //Create History
  243.                     $leadHistory = new LeadHistory();
  244.                     $leadHistory->setNewLead($lead);
  245.                     $leadHistory->setFoundLead($matchedLead);
  246.                     $this->em->persist($leadHistory);
  247.                 }
  248.             }
  249.         }
  250.     }
  251.     public function getNotFoundHashes($orderHashes): array
  252.     {
  253.         if($orderHashes == NULL || count($orderHashes) == 0)
  254.             return [];
  255.         //Check For In Valid Hashes
  256.         $hashesNotFound = [];
  257.         $leadHashes $this->em->getRepository(Lead::class)->getLeadHashesByHashes($orderHashes);
  258.         if(count($leadHashes) != count($orderHashes))
  259.         {
  260.             for ($i 0$i count($orderHashes); $i++)
  261.             {
  262.                 $found false;
  263.                 foreach ($leadHashes as $leadHash)
  264.                 {
  265.                     $orderHashes[$i] = trim(preg_replace('/\s+/'''$orderHashes[$i]));
  266.                     if ($orderHashes[$i] == $leadHash['hash'])
  267.                     {
  268.                         $found true;
  269.                         break;
  270.                     }
  271.                 }
  272.                 if(!$found)
  273.                     array_push($hashesNotFound $orderHashes[$i]);
  274.             }
  275.         }
  276.         return $hashesNotFound;
  277.     }
  278. }