src/Controller/CalculatorController.php line 234

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BusinessCardOption;
  4. use App\Entity\Calculator;
  5. use App\Entity\CalculatorProductBusinessCard;
  6. use App\Entity\Imposition;
  7. use App\Entity\Machine;
  8. use App\Entity\MachinePaperSizeMatrix;
  9. use App\Entity\PaperSize;
  10. use App\Entity\Product;
  11. use App\Entity\Task;
  12. use App\Form\CalculatorProductBusinessCardType;
  13. use App\Form\CalculatorSettingsType;
  14. use App\Form\CalculatorType;
  15. use App\Repository\CalculatorProductBusinessCardRepository;
  16. use App\Repository\CalculatorRepository;
  17. use App\Repository\ImpositionRepository;
  18. use App\Repository\MachinePaperSizeMatrixRepository;
  19. use App\Repository\MachineRepository;
  20. use App\Repository\PaperSizeRepository;
  21. use App\Repository\ProductRepository;
  22. use App\Repository\TaskRepository;
  23. use App\Service\BusinessCardCalculator;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Knp\Component\Pager\PaginatorInterface;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  33. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  34. use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
  35. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  36. use Symfony\Component\Serializer\Serializer;
  37. /**
  38.  * @Route("/admin/calculator")
  39.  */
  40. class CalculatorController extends AbstractController
  41. {
  42.     /**
  43.      * @Route("/", name="calculator_index")
  44.      */
  45.     public function index(CalculatorRepository $calculatorRepository,
  46.                           PaginatorInterface $paginator,Request $request,
  47.                           EntityManagerInterface $em
  48.     ): Response
  49.     {
  50.         //$queryParams = $this->get_params_from_search_query($request);
  51.         $name trim($request->query->get('name'));
  52.         $query $calculatorRepository->findBySearchQuery($name);
  53.         //$product = $query->getResult();
  54.         //$products = $productRepository->findAll();
  55.         $pagination $paginator->paginate(
  56.             $query/* query NOT result */
  57.             $request->query->getInt('page'1), /*page number*/
  58.             20/*limit per page*/
  59.             [
  60.                 'defaultSortFieldName'      => 'c.createdAt',
  61.                 'defaultSortDirection' => 'desc'
  62.             ]
  63.         );
  64.         return $this->render('calculator/index.html.twig', [
  65.             'pagination' => $pagination
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/new", name="calculator_new", methods={"GET", "POST"})
  70.      */
  71.     public function new(Request $requestCalculatorRepository $calculatorRepository): Response
  72.     {
  73.         $calculator = new Calculator();
  74.         $form $this->createForm(CalculatorType::class, $calculator);
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             $calculatorRepository->add($calculatortrue);
  78.             return $this->redirectToRoute('app_calculator_index', [], Response::HTTP_SEE_OTHER);
  79.         }
  80.         return $this->renderForm('calculator/new.html.twig', [
  81.             'calculator' => $calculator,
  82.             'form' => $form,
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/get_printing_types", name="calculator_get_printing_types", methods={"GET"}, options={"expose"=true})
  87.      */
  88.     public function getPrintingTypes(Request $requestMachineRepository $machineRepository): Response
  89.     {
  90.         $query $request->query->get('q''');
  91.         $machines $machineRepository->findPrintingTypesByNameOrCode($query);
  92.         $results = [];
  93.         foreach ($machines as $machine) {
  94.             /* @var $machine Machine */
  95.             $results['results'][] = [
  96.                 'id' => htmlspecialchars($machine->getId(), \ENT_COMPAT | \ENT_HTML5),
  97.                 'text' => htmlspecialchars('[' $machine->getCode() . '] ' $machine->getName(),
  98.                     \ENT_COMPAT | \ENT_HTML5)
  99.             ];
  100.         }
  101.         return $this->json($results);
  102.     }
  103.     /**
  104.      * @Route("/get_machines", name="calculator_get_machines", methods={"GET"}, options={"expose"=true})
  105.      */
  106.     public function getMachines(Request $requestMachineRepository $machineRepository): Response
  107.     {
  108.         $query $request->query->get('q''');
  109.         $printingTypeId $request->query->get('machine_id''');
  110.         $parentMachine $machineRepository->find($printingTypeId);
  111.         $machines $machineRepository->findMachinesByNameOrCode($query$parentMachine);
  112.         $results = [];
  113.         foreach ($machines as $machine) {
  114.             /* @var Machine $machine */
  115.             $results['results'][] = [
  116.                 'id' => htmlspecialchars($machine->getId(), \ENT_COMPAT | \ENT_HTML5),
  117.                 'text' => htmlspecialchars($machine->getCalculatorDisplayName(),
  118.                     \ENT_COMPAT | \ENT_HTML5)
  119.             ];
  120.         }
  121.         return $this->json($results);
  122.     }
  123.     /**
  124.      * @Route("/get_machine_paper", name="calculator_get_machine_paper", methods={"GET"}, options={"expose"=true})
  125.      */public function get_machine_papers(Request $requestMachineRepository $machineRepository): Response
  126.     {
  127.         $results = [];
  128.         $machineId $request->query->get('machine_id''');
  129.         $machine $machineRepository->find($machineId);
  130.         $paperSizes $machine->getPaperSizes()->toArray();
  131.         foreach ($paperSizes as $paperSize){
  132.             /* @var PaperSize $paperSize */
  133.             $results['results'][] = [
  134.                 'id' => htmlspecialchars($paperSize->getId(), \ENT_COMPAT | \ENT_HTML5),
  135.                 'text' => htmlspecialchars($paperSize->getCalculatorDisplayName(),
  136.                     \ENT_COMPAT | \ENT_HTML5)
  137.             ];
  138.         }
  139.         return $this->json($results200,
  140.             [],
  141.             [
  142.                 'groups' => ['machine']
  143.             ]);
  144.     }
  145.     /**
  146.      * @Route("/get_imposition", name="calculator_get_imposition", methods={"GET"}, options={"expose"=true})
  147.      */
  148.     public function get_imposition(Request $requestImpositionRepository $impositionRepository): Response
  149.     {
  150.         $results = [];
  151.         $query $request->query->get('q''');
  152.         $finishedSizeX $request->query->get('finished_size_x');
  153.         $finishedSizeY $request->query->get('finished_size_y');
  154.         $impositions $impositionRepository->findBy([
  155.             'impositionAcross' => $finishedSizeX,
  156.             'impositionDown' => $finishedSizeY,
  157.         ]);
  158.         foreach ($impositions as $imposition){
  159.             /* @var Imposition $imposition */
  160.             $results['results'][] = [
  161.                 'id' => htmlspecialchars($imposition->getId(), \ENT_COMPAT | \ENT_HTML5),
  162.                 'text' => htmlspecialchars($imposition->getCalculatorDisplayName(),
  163.                     \ENT_COMPAT | \ENT_HTML5)
  164.             ];
  165.         }
  166.         return $this->json($results);
  167.     }
  168.     /**
  169.      * @Route("/get_paper_type", name="calculator_get_paper_type", methods={"GET"}, options={"expose"=true})
  170.      */
  171.     public function get_paper_type(Request $requestPaperSizeRepository $paperSizeRepositoryProductRepository $productRepository): Response
  172.     {
  173.         $results = [];
  174.         $paperSizeId $request->query->get('paper_size_id');
  175.         $query $request->query->get('q''');
  176.         $paperSize $paperSizeRepository->find($paperSizeId);
  177.         if ($paperSize){
  178.             $products $productRepository->findPapersBySize($query$paperSize->getWidth(), $paperSize->getHeight());
  179.             foreach ($products as $product){
  180.                 /* @var Product $product */
  181.                 $results['results'][] = [
  182.                     'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
  183.                     'text' => htmlspecialchars($product->getCalculatorPaperDisplayName(),
  184.                         \ENT_COMPAT | \ENT_HTML5)
  185.                 ];
  186.             }
  187.         }
  188.         return $this->json($results);
  189.     }
  190.     /**
  191.      * @Route("/get_task", methods="GET", name="calculator_get_task", options={"expose"=true})
  192.      */
  193.     public function get_task(Request $requestTaskRepository $taskRepository): JsonResponse
  194.     {
  195.         $query $request->query->get('q''');
  196.         $tasks $taskRepository->findByNameOrCode($query);
  197.         $results = [];
  198.         foreach ($tasks as $task) {
  199.             /* @var $task Task */
  200.             $results['results'][] = [
  201.                 'id' => htmlspecialchars($task->getId(), \ENT_COMPAT | \ENT_HTML5),
  202.                 'text' => htmlspecialchars($task->getCalculatorDisplayName(),
  203.                     \ENT_COMPAT | \ENT_HTML5),
  204.                 'name'=> $task->getName(),
  205.                 'code'=> $task->getCode(),
  206.                 'invoice_description' => $task->getInvoiceDescription(),
  207.                 'technical_notes' => $task->getTechnicalNotes()
  208.             ];
  209.         }
  210.         return $this->json($results);
  211.     }
  212.     /**
  213.      * @Route("/get_side_click_labels", name="get_side_click_labels", methods={"GET"}, options={"expose"=true})
  214.      */
  215.     public function get_side_click_labels(Request $requestMachinePaperSizeMatrixRepository $machinePaperSizeMatrixRepositoryRepository
  216.     ): Response
  217.     {
  218.         $results = [];
  219.         $paperSizeId $request->query->get('paper_size_id');
  220.         $machineId $request->query->get('machine_id');
  221.         $sideTwo $request->query->get('side_two');
  222.         /** @var  $machinePaperSizeMatrix MachinePaperSizeMatrix*/
  223.         $machinePaperSizeMatrix $machinePaperSizeMatrixRepositoryRepository->findOneBy([
  224.             'paperSize' => $paperSizeId,
  225.             'machine' => $machineId
  226.         ]);
  227.         if ($machinePaperSizeMatrix){
  228.             if (!empty($sideTwo) && $sideTwo === 'true'){
  229.                 $results['results'][] = [
  230.                     'id' => 'none',
  231.                     'text' => 'none'
  232.                 ];
  233.             }
  234.             $results['results'][] = [
  235.                 'id' => htmlspecialchars($machinePaperSizeMatrix->getColourClickLabel(), \ENT_COMPAT | \ENT_HTML5),
  236.                 'text' => htmlspecialchars($machinePaperSizeMatrix->getColourClickLabel(),
  237.                     \ENT_COMPAT | \ENT_HTML5)
  238.             ];
  239.             $results['results'][] = [
  240.                 'id' => htmlspecialchars($machinePaperSizeMatrix->getBlackOnlyClickLabel(), \ENT_COMPAT | \ENT_HTML5),
  241.                 'text' => htmlspecialchars($machinePaperSizeMatrix->getBlackOnlyClickLabel(),
  242.                     \ENT_COMPAT | \ENT_HTML5)
  243.             ];
  244.         }
  245.         return $this->json($results);
  246.     }
  247.     /**
  248.      * @Route("/{id}", name="app_calculator_show", methods={"GET"})
  249.      */
  250.     public function show(Calculator $calculator): Response
  251.     {
  252.         return $this->render('calculator/show.html.twig', [
  253.             'calculator' => $calculator,
  254.         ]);
  255.     }
  256.     /**
  257.      * @Route("/{id}/edit", name="calculator_edit", methods={"GET", "POST"})
  258.      */
  259.     public function edit(Request $requestCalculator $calculatorCalculatorRepository $calculatorRepository): Response
  260.     {
  261.         $form $this->createForm(CalculatorType::class, $calculator);
  262.         $form->handleRequest($request);
  263.         if ($form->isSubmitted() && $form->isValid()) {
  264.             //$calculatorRepository->add($calculator, true);
  265.             return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
  266.         }
  267.         $template $calculator->getTemplate();
  268.         return $this->renderForm(empty($template)?'calculator/edit.html.twig':'calculator/'.$template, [
  269.             'calculator' => $calculator,
  270.             'form' => $form,
  271.         ]);
  272.     }
  273.     /**
  274.      * @Route("/{id}", name="calculator_delete", methods={"POST"})
  275.      */
  276.     public function delete(Request $requestCalculator $calculatorCalculatorRepository $calculatorRepository): Response
  277.     {
  278.         if ($this->isCsrfTokenValid('delete'.$calculator->getId(), $request->request->get('_token'))) {
  279.             $calculatorRepository->remove($calculatortrue);
  280.         }
  281.         return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
  282.     }
  283.     /**
  284.      * @Route("/{id}/settings", name="calculator_settings", methods={"GET", "POST"})
  285.      */
  286.     public function settings(Request $requestCalculator $calculatorCalculatorRepository $calculatorRepository): Response
  287.     {
  288.         $form $this->createForm(CalculatorSettingsType::class, $calculator);
  289.         $form->handleRequest($request);
  290.         if ($form->isSubmitted() && $form->isValid()) {
  291.             $calculatorRepository->add($calculatortrue);
  292.             $this->addFlash('success''Calculator updated.');
  293.             return $this->redirectToRoute('calculator_settings', ['id' => $calculator->getId()], Response::HTTP_SEE_OTHER);
  294.         }
  295.         $template $calculator->getTemplate();
  296.         return $this->renderForm('calculator/settings.html.twig', [
  297.             'calculator' => $calculator,
  298.             'form' => $form,
  299.         ]);
  300.     }
  301.     /**
  302.      * @Route("/{id}/calculate", name="calculate", methods={"POST"})
  303.      */
  304.     public function calculate(Request $requestCalculator $calculatorBusinessCardCalculator $calculatorHelper
  305.                                           ): Response
  306.     {
  307.         $result = [
  308.             'result' => 'Error',
  309.             'html' => '<p>Error</>'
  310.         ];
  311.         //Get all data
  312.         $data $request->request->all();
  313.         //Business card
  314.         if ($calculator->getId() == ){
  315.             $result $calculatorHelper->getPricing($data$calculator);
  316.             return $this->json([
  317.                 'result' => $result,
  318.                 'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
  319.                     'pricing' => $result
  320.                 ])
  321.             ]);
  322.         }elseif($calculator->getId() == ){
  323.             $result $calculatorHelper->getOptionPricing($data$calculator);
  324.             return $this->json([
  325.                 'result' => $result,
  326.                 'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
  327.                     'pricing' => $result
  328.                 ])
  329.             ]);
  330.         }
  331.         return $this->json($result);
  332.     }
  333.     /**
  334.      * @Route("/{id}/show", name="calculator_show", methods={"GET", "POST"})
  335.      */
  336.     public function showCalculator(Request $requestCalculator $calculator,
  337.     CalculatorRepository $calculatorRepositoryBusinessCardCalculator $calculatorHelper,
  338.     CalculatorProductBusinessCardRepository $bcRepository
  339.     ): Response
  340.     {
  341.         $form $this->createForm(CalculatorType::class, $calculator);
  342.         //Business card
  343.         if ($calculator->getId() == ){
  344.             $calculatorProductBusinessCard = new CalculatorProductBusinessCard();
  345.             $calculatorProductBusinessCard->setCalculator($calculator);
  346.             $form $this->createForm(CalculatorProductBusinessCardType::class, $calculatorProductBusinessCard,['testing' => true]);
  347.         }
  348.         $form->handleRequest($request);
  349.         if ($form->isSubmitted() ) {
  350.             $formData $form->getData();
  351.             //get product code.
  352.             /*$formData->getPrintingType();*/
  353.             //Business card prefix
  354.             $productCode $calculatorHelper->getProductCode($formData);
  355.             if ($form->getClickedButton() && 'saveAndAddBtn' === $form->getClickedButton()->getName()) {
  356.                 //Save as new cal
  357.                 /* @var $calculatorProductBusinessCard CalculatorProductBusinessCard */
  358.                 $calculatorProductBusinessCard = new CalculatorProductBusinessCard();
  359.                 $calculatorProductBusinessCard $form->getData();
  360.                 $calculatorProductBusinessCard->setName('test');
  361.                 $calculatorProductBusinessCard->setCode('test '.time());
  362.                 $bcRepository->add($calculatorProductBusinessCardtrue);
  363.                 /*try{
  364.                     $calculatorHelper->denormalizeBusinessCardProduct($calculatorProductBusinessCard, $formData);
  365.                 }catch (\Exception $exception){
  366.                     return $this->json(['result' => 'error']);
  367.                 }*/
  368.                 $this->addFlash('success''Calculator: '.$calculatorProductBusinessCard->getCode().' created.');
  369.                 return $this->redirectToRoute('calculator_product_business_card_show',
  370.                     ['id' => $calculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
  371.             }
  372.             //Calculate Only
  373.             if ($form->get('calculateBtnClicked')->getData() == '1'){
  374.                 $result $calculatorHelper->getOptionPricing($form->getData(), $calculator);
  375.                 return $this->json([
  376.                     'result' => $result,
  377.                     'productCode' => $productCode,
  378.                     'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
  379.                         'pricing' => $result
  380.                     ])
  381.                 ]);
  382.             }
  383.             return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
  384.         }
  385.         $template $calculator->getTemplate();
  386.         return $this->renderForm(empty($template)?'calculator/edit.html.twig':$template, [
  387.             'calculator' => $calculator,
  388.             'form' => $form,
  389.         ]);
  390.     }
  391.     /**
  392.      * @Route("/show/calculator_product_business_card/{product_id}",
  393.      *     name="calculator_show_calculator_product_business_card", methods={"GET", "POST"})
  394.      *@ParamConverter("product", options={"id" = "product_id"})
  395.      */
  396.     function showCalculatorProductBusinessCard(Request $request,
  397.         CalculatorProductBusinessCard $product): Response
  398.     {
  399.         $form $this->createForm(CalculatorProductBusinessCardType::class, $product);
  400.         $calculator $product->getCalculator();
  401.         return $this->renderForm('calculator/calculator_product_business_card.html.twig', [
  402.             'calculator' => $calculator,
  403.             'product' => $product,
  404.             'form' => $form,
  405.         ]);
  406.     }
  407.     /**
  408.      * @Route("/show/calculator_product_business_card_test/{product_id}",
  409.      *      name="calculator_show_calculator_product_business_card_test", methods={"GET", "POST"})
  410.      * @ParamConverter("product", options={"id" = "product_id"})
  411.      * @param CalculatorProductBusinessCard $product
  412.      * @param BusinessCardCalculator $calculatorHelper
  413.      * @return Response
  414.      */
  415.     function showCalculatorProductBusinessCardTest(
  416.        CalculatorProductBusinessCard $productBusinessCardCalculator $calculatorHelper): Response
  417.     {
  418.         $calculator $product->getCalculator();
  419.         $result $calculatorHelper->getOptionPricing($product$calculator);
  420.         return $this->render('calculator/business_card_calculator_result_test.html.twig', [
  421.             'pricing' => $result
  422.         ]);
  423.     }
  424.     /**
  425.      * @Route("/calculator_product_business_card/create",name="create_calculator_product_business_card", methods={"POST"})
  426.      * @param Request $request
  427.      * @return JsonResponse
  428.      */
  429.     function createCalculatorProductBusinessCard(Request $request): JsonResponse
  430.     {
  431.         $data $request->request->all();
  432.         return $this->json(['result' => 'test']);
  433.     }
  434.     /**
  435.      * @Route("/calculator_product_business_card/save/{product_id}",
  436.      *      name="save_calculator_product_business_card", methods={"POST"})
  437.      * @ParamConverter("product", options={"id" = "product_id"})
  438.      * @param Request $request
  439.      * @param CalculatorProductBusinessCard $product
  440.      * @param BusinessCardCalculator $businessCardCalculator
  441.      * @param EntityManagerInterface $entityManager
  442.      * @return JsonResponse
  443.      */
  444.     function saveCalculatorBusinessCard(Request $requestCalculatorProductBusinessCard $product,
  445.             BusinessCardCalculator $businessCardCalculatorEntityManagerInterface $entityManager
  446.         ): JsonResponse
  447.     {
  448.         //Get all data
  449.         $data $request->request->all();
  450.         $product $businessCardCalculator->denormalizeBusinessCardProduct($product$data);
  451.         return $this->json(['result' => 'saved']);
  452.     }
  453. }