<?php
namespace App\Controller;
use App\Entity\BusinessCardOption;
use App\Entity\Calculator;
use App\Entity\CalculatorProductBusinessCard;
use App\Entity\Imposition;
use App\Entity\Machine;
use App\Entity\MachinePaperSizeMatrix;
use App\Entity\PaperSize;
use App\Entity\Product;
use App\Entity\Task;
use App\Form\CalculatorProductBusinessCardType;
use App\Form\CalculatorSettingsType;
use App\Form\CalculatorType;
use App\Repository\CalculatorProductBusinessCardRepository;
use App\Repository\CalculatorRepository;
use App\Repository\ImpositionRepository;
use App\Repository\MachinePaperSizeMatrixRepository;
use App\Repository\MachineRepository;
use App\Repository\PaperSizeRepository;
use App\Repository\ProductRepository;
use App\Repository\TaskRepository;
use App\Service\BusinessCardCalculator;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
/**
* @Route("/admin/calculator")
*/
class CalculatorController extends AbstractController
{
/**
* @Route("/", name="calculator_index")
*/
public function index(CalculatorRepository $calculatorRepository,
PaginatorInterface $paginator,Request $request,
EntityManagerInterface $em
): Response
{
//$queryParams = $this->get_params_from_search_query($request);
$name = trim($request->query->get('name'));
$query = $calculatorRepository->findBySearchQuery($name);
//$product = $query->getResult();
//$products = $productRepository->findAll();
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20, /*limit per page*/
[
'defaultSortFieldName' => 'c.createdAt',
'defaultSortDirection' => 'desc'
]
);
return $this->render('calculator/index.html.twig', [
'pagination' => $pagination
]);
}
/**
* @Route("/new", name="calculator_new", methods={"GET", "POST"})
*/
public function new(Request $request, CalculatorRepository $calculatorRepository): Response
{
$calculator = new Calculator();
$form = $this->createForm(CalculatorType::class, $calculator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$calculatorRepository->add($calculator, true);
return $this->redirectToRoute('app_calculator_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('calculator/new.html.twig', [
'calculator' => $calculator,
'form' => $form,
]);
}
/**
* @Route("/get_printing_types", name="calculator_get_printing_types", methods={"GET"}, options={"expose"=true})
*/
public function getPrintingTypes(Request $request, MachineRepository $machineRepository): Response
{
$query = $request->query->get('q', '');
$machines = $machineRepository->findPrintingTypesByNameOrCode($query);
$results = [];
foreach ($machines as $machine) {
/* @var $machine Machine */
$results['results'][] = [
'id' => htmlspecialchars($machine->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars('[' . $machine->getCode() . '] ' . $machine->getName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
return $this->json($results);
}
/**
* @Route("/get_machines", name="calculator_get_machines", methods={"GET"}, options={"expose"=true})
*/
public function getMachines(Request $request, MachineRepository $machineRepository): Response
{
$query = $request->query->get('q', '');
$printingTypeId = $request->query->get('machine_id', '');
$parentMachine = $machineRepository->find($printingTypeId);
$machines = $machineRepository->findMachinesByNameOrCode($query, $parentMachine);
$results = [];
foreach ($machines as $machine) {
/* @var Machine $machine */
$results['results'][] = [
'id' => htmlspecialchars($machine->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($machine->getCalculatorDisplayName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
return $this->json($results);
}
/**
* @Route("/get_machine_paper", name="calculator_get_machine_paper", methods={"GET"}, options={"expose"=true})
*/public function get_machine_papers(Request $request, MachineRepository $machineRepository): Response
{
$results = [];
$machineId = $request->query->get('machine_id', '');
$machine = $machineRepository->find($machineId);
$paperSizes = $machine->getPaperSizes()->toArray();
foreach ($paperSizes as $paperSize){
/* @var PaperSize $paperSize */
$results['results'][] = [
'id' => htmlspecialchars($paperSize->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($paperSize->getCalculatorDisplayName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
return $this->json($results, 200,
[],
[
'groups' => ['machine']
]);
}
/**
* @Route("/get_imposition", name="calculator_get_imposition", methods={"GET"}, options={"expose"=true})
*/
public function get_imposition(Request $request, ImpositionRepository $impositionRepository): Response
{
$results = [];
$query = $request->query->get('q', '');
$finishedSizeX = $request->query->get('finished_size_x');
$finishedSizeY = $request->query->get('finished_size_y');
$impositions = $impositionRepository->findBy([
'impositionAcross' => $finishedSizeX,
'impositionDown' => $finishedSizeY,
]);
foreach ($impositions as $imposition){
/* @var Imposition $imposition */
$results['results'][] = [
'id' => htmlspecialchars($imposition->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($imposition->getCalculatorDisplayName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
return $this->json($results);
}
/**
* @Route("/get_paper_type", name="calculator_get_paper_type", methods={"GET"}, options={"expose"=true})
*/
public function get_paper_type(Request $request, PaperSizeRepository $paperSizeRepository, ProductRepository $productRepository): Response
{
$results = [];
$paperSizeId = $request->query->get('paper_size_id');
$query = $request->query->get('q', '');
$paperSize = $paperSizeRepository->find($paperSizeId);
if ($paperSize){
$products = $productRepository->findPapersBySize($query, $paperSize->getWidth(), $paperSize->getHeight());
foreach ($products as $product){
/* @var Product $product */
$results['results'][] = [
'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($product->getCalculatorPaperDisplayName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
}
return $this->json($results);
}
/**
* @Route("/get_task", methods="GET", name="calculator_get_task", options={"expose"=true})
*/
public function get_task(Request $request, TaskRepository $taskRepository): JsonResponse
{
$query = $request->query->get('q', '');
$tasks = $taskRepository->findByNameOrCode($query);
$results = [];
foreach ($tasks as $task) {
/* @var $task Task */
$results['results'][] = [
'id' => htmlspecialchars($task->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($task->getCalculatorDisplayName(),
\ENT_COMPAT | \ENT_HTML5),
'name'=> $task->getName(),
'code'=> $task->getCode(),
'invoice_description' => $task->getInvoiceDescription(),
'technical_notes' => $task->getTechnicalNotes()
];
}
return $this->json($results);
}
/**
* @Route("/get_side_click_labels", name="get_side_click_labels", methods={"GET"}, options={"expose"=true})
*/
public function get_side_click_labels(Request $request, MachinePaperSizeMatrixRepository $machinePaperSizeMatrixRepositoryRepository
): Response
{
$results = [];
$paperSizeId = $request->query->get('paper_size_id');
$machineId = $request->query->get('machine_id');
$sideTwo = $request->query->get('side_two');
/** @var $machinePaperSizeMatrix MachinePaperSizeMatrix*/
$machinePaperSizeMatrix = $machinePaperSizeMatrixRepositoryRepository->findOneBy([
'paperSize' => $paperSizeId,
'machine' => $machineId
]);
if ($machinePaperSizeMatrix){
if (!empty($sideTwo) && $sideTwo === 'true'){
$results['results'][] = [
'id' => 'none',
'text' => 'none'
];
}
$results['results'][] = [
'id' => htmlspecialchars($machinePaperSizeMatrix->getColourClickLabel(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($machinePaperSizeMatrix->getColourClickLabel(),
\ENT_COMPAT | \ENT_HTML5)
];
$results['results'][] = [
'id' => htmlspecialchars($machinePaperSizeMatrix->getBlackOnlyClickLabel(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($machinePaperSizeMatrix->getBlackOnlyClickLabel(),
\ENT_COMPAT | \ENT_HTML5)
];
}
return $this->json($results);
}
/**
* @Route("/{id}", name="app_calculator_show", methods={"GET"})
*/
public function show(Calculator $calculator): Response
{
return $this->render('calculator/show.html.twig', [
'calculator' => $calculator,
]);
}
/**
* @Route("/{id}/edit", name="calculator_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Calculator $calculator, CalculatorRepository $calculatorRepository): Response
{
$form = $this->createForm(CalculatorType::class, $calculator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$calculatorRepository->add($calculator, true);
return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
}
$template = $calculator->getTemplate();
return $this->renderForm(empty($template)?'calculator/edit.html.twig':'calculator/'.$template, [
'calculator' => $calculator,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="calculator_delete", methods={"POST"})
*/
public function delete(Request $request, Calculator $calculator, CalculatorRepository $calculatorRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$calculator->getId(), $request->request->get('_token'))) {
$calculatorRepository->remove($calculator, true);
}
return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/{id}/settings", name="calculator_settings", methods={"GET", "POST"})
*/
public function settings(Request $request, Calculator $calculator, CalculatorRepository $calculatorRepository): Response
{
$form = $this->createForm(CalculatorSettingsType::class, $calculator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$calculatorRepository->add($calculator, true);
$this->addFlash('success', 'Calculator updated.');
return $this->redirectToRoute('calculator_settings', ['id' => $calculator->getId()], Response::HTTP_SEE_OTHER);
}
$template = $calculator->getTemplate();
return $this->renderForm('calculator/settings.html.twig', [
'calculator' => $calculator,
'form' => $form,
]);
}
/**
* @Route("/{id}/calculate", name="calculate", methods={"POST"})
*/
public function calculate(Request $request, Calculator $calculator, BusinessCardCalculator $calculatorHelper
): Response
{
$result = [
'result' => 'Error',
'html' => '<p>Error</>'
];
//Get all data
$data = $request->request->all();
//Business card
if ($calculator->getId() == 1 ){
$result = $calculatorHelper->getPricing($data, $calculator);
return $this->json([
'result' => $result,
'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
'pricing' => $result
])
]);
}elseif($calculator->getId() == 2 ){
$result = $calculatorHelper->getOptionPricing($data, $calculator);
return $this->json([
'result' => $result,
'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
'pricing' => $result
])
]);
}
return $this->json($result);
}
/**
* @Route("/{id}/show", name="calculator_show", methods={"GET", "POST"})
*/
public function showCalculator(Request $request, Calculator $calculator,
CalculatorRepository $calculatorRepository, BusinessCardCalculator $calculatorHelper,
CalculatorProductBusinessCardRepository $bcRepository
): Response
{
$form = $this->createForm(CalculatorType::class, $calculator);
//Business card
if ($calculator->getId() == 2 ){
$calculatorProductBusinessCard = new CalculatorProductBusinessCard();
$calculatorProductBusinessCard->setCalculator($calculator);
$form = $this->createForm(CalculatorProductBusinessCardType::class, $calculatorProductBusinessCard,['testing' => true]);
}
$form->handleRequest($request);
if ($form->isSubmitted() ) {
$formData = $form->getData();
//get product code.
/*$formData->getPrintingType();*/
//Business card prefix
$productCode = $calculatorHelper->getProductCode($formData);
if ($form->getClickedButton() && 'saveAndAddBtn' === $form->getClickedButton()->getName()) {
//Save as new cal
/* @var $calculatorProductBusinessCard CalculatorProductBusinessCard */
$calculatorProductBusinessCard = new CalculatorProductBusinessCard();
$calculatorProductBusinessCard = $form->getData();
$calculatorProductBusinessCard->setName('test');
$calculatorProductBusinessCard->setCode('test '.time());
$bcRepository->add($calculatorProductBusinessCard, true);
/*try{
$calculatorHelper->denormalizeBusinessCardProduct($calculatorProductBusinessCard, $formData);
}catch (\Exception $exception){
return $this->json(['result' => 'error']);
}*/
$this->addFlash('success', 'Calculator: '.$calculatorProductBusinessCard->getCode().' created.');
return $this->redirectToRoute('calculator_product_business_card_show',
['id' => $calculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
}
//Calculate Only
if ($form->get('calculateBtnClicked')->getData() == '1'){
$result = $calculatorHelper->getOptionPricing($form->getData(), $calculator);
return $this->json([
'result' => $result,
'productCode' => $productCode,
'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
'pricing' => $result
])
]);
}
return $this->redirectToRoute('calculator_index', [], Response::HTTP_SEE_OTHER);
}
$template = $calculator->getTemplate();
return $this->renderForm(empty($template)?'calculator/edit.html.twig':$template, [
'calculator' => $calculator,
'form' => $form,
]);
}
/**
* @Route("/show/calculator_product_business_card/{product_id}",
* name="calculator_show_calculator_product_business_card", methods={"GET", "POST"})
*@ParamConverter("product", options={"id" = "product_id"})
*/
function showCalculatorProductBusinessCard(Request $request,
CalculatorProductBusinessCard $product): Response
{
$form = $this->createForm(CalculatorProductBusinessCardType::class, $product);
$calculator = $product->getCalculator();
return $this->renderForm('calculator/calculator_product_business_card.html.twig', [
'calculator' => $calculator,
'product' => $product,
'form' => $form,
]);
}
/**
* @Route("/show/calculator_product_business_card_test/{product_id}",
* name="calculator_show_calculator_product_business_card_test", methods={"GET", "POST"})
* @ParamConverter("product", options={"id" = "product_id"})
* @param CalculatorProductBusinessCard $product
* @param BusinessCardCalculator $calculatorHelper
* @return Response
*/
function showCalculatorProductBusinessCardTest(
CalculatorProductBusinessCard $product, BusinessCardCalculator $calculatorHelper): Response
{
$calculator = $product->getCalculator();
$result = $calculatorHelper->getOptionPricing($product, $calculator);
return $this->render('calculator/business_card_calculator_result_test.html.twig', [
'pricing' => $result
]);
}
/**
* @Route("/calculator_product_business_card/create",name="create_calculator_product_business_card", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
function createCalculatorProductBusinessCard(Request $request): JsonResponse
{
$data = $request->request->all();
return $this->json(['result' => 'test']);
}
/**
* @Route("/calculator_product_business_card/save/{product_id}",
* name="save_calculator_product_business_card", methods={"POST"})
* @ParamConverter("product", options={"id" = "product_id"})
* @param Request $request
* @param CalculatorProductBusinessCard $product
* @param BusinessCardCalculator $businessCardCalculator
* @param EntityManagerInterface $entityManager
* @return JsonResponse
*/
function saveCalculatorBusinessCard(Request $request, CalculatorProductBusinessCard $product,
BusinessCardCalculator $businessCardCalculator, EntityManagerInterface $entityManager
): JsonResponse
{
//Get all data
$data = $request->request->all();
$product = $businessCardCalculator->denormalizeBusinessCardProduct($product, $data);
return $this->json(['result' => 'saved']);
}
}