src/Controller/LeadController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Entity\CustomerGarage;
  5. use App\Entity\Lead;
  6. use App\Entity\MangoCall;
  7. use App\Repository\CustomerGarageRepository;
  8. use App\Repository\CustomerRepository;
  9. use App\Repository\LeadRepository;
  10. use App\Repository\MangoCallRepository;
  11. use App\Service\Api;
  12. use App\Service\CreateLead;
  13. use App\Service\DTO\LeadDTO;
  14. use App\Service\Helper\PhoneHelper;
  15. use App\Service\Helper\RequestHelper;
  16. use DateTime;
  17. use Exception;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  22. /**
  23.  * @Route("/lead")
  24.  */
  25. class LeadController extends BaseController
  26. {
  27.     /**
  28.      * @Route("/list", methods="GET")
  29.      * @throws Exception
  30.      */
  31.     public function leadsList(Request $requestLeadRepository $leadRepository): JsonResponse
  32.     {
  33.         return new JsonResponse(LeadDTO::getLeadsList(...$leadRepository->getList((new DateTime($request->query->get('from')))->setTime(00), (new DateTime($request->query->get('to')))->setTime(2359,59), $request->query->get('phone'))));
  34.     }
  35.     /**
  36.      * @Route("/{lead}/data", methods="GET")
  37.      * @throws Exception
  38.      */
  39.     public function data(Request $requestLead $leadLeadRepository $leadRepository): JsonResponse
  40.     {
  41.         return new JsonResponse(LeadDTO::getLead($lead));
  42.     }
  43.     /**
  44.      * @Route("/create_from_call", methods="POST")
  45.      * @throws Exception
  46.      * @throws TransportExceptionInterface
  47.      */
  48.     public function createLeadFromCall(
  49.         Request $request,
  50.         CustomerGarageRepository $customerGarageRepository,
  51.         CustomerRepository $customerRepository,
  52.         MangoCallRepository $mangoCallRepository,
  53.         Api $apiService,
  54.         CreateLead $createLeadService
  55.     ): JsonResponse
  56.     {
  57.         $phone PhoneHelper::getNormalizePhone(RequestHelper::get($request'phone'));
  58.         $carBrand RequestHelper::get($request'carBrand');
  59.         $carModel RequestHelper::get($request'carModel');
  60.         $carVin RequestHelper::get($request'carVin');
  61.         $mangoCallId RequestHelper::getInt($request'currentCallId');
  62.         /** @var MangoCall $mangoCall */
  63.         $mangoCall $mangoCallRepository->find($mangoCallId);
  64.         $customer $customerRepository->findOneBy(['phone' => $phone]);
  65.         $customer $customer ?: $customerRepository->addCustomer($phone);
  66.         $garageId RequestHelper::getInt($request'garageId');
  67.         if($garageId) {
  68.             /** @var CustomerGarage $customerGarage */
  69.             $customerGarage $customerGarageRepository->find($garageId);
  70.             if($carBrand || $carModel || $carVin) {
  71.                 $customerGarageRepository->update(
  72.                     $customerGarage
  73.                         ->setCarBrand($carBrand)
  74.                         ->setCarModel($carModel)
  75.                         ->setCarVin($carVin)
  76.                 );
  77.             }
  78.         } else {
  79.             $customerGarageRepository->save(
  80.                 new CustomerGarage($carBrand$carModel$carVin$customer)
  81.             );
  82.         }
  83.         $lead $createLeadService->executeCreateLead(
  84.             $customer,
  85.             RequestHelper::get($request'customerFirstName'),
  86.             RequestHelper::get($request'customerFatherName'),
  87.             RequestHelper::get($request'customerSureName'),
  88.             RequestHelper::get($request'town'),
  89.             $carBrand,
  90.             $carModel,
  91.             $carVin,
  92.             RequestHelper::get($request'comment'),
  93.             $this->findUserByCookie($request->cookies->get('user')),
  94.             $mangoCall
  95.         );
  96.         if($mangoCall) {
  97.             $apiService->updateMangoClient($lead->getCustomer());
  98.             $mangoCallRepository->update($mangoCall->setLead($lead));
  99.         }
  100.         return new JsonResponse(['id' => $lead->getId(), 'deprecatedOrderId'=> $lead->getDeprecatedOrderId()]);
  101.     }
  102. }