<?php
namespace App\Controller;
use App\Entity\Bodega;
use App\Entity\Categoria;
use App\Entity\Checkout;
use App\Entity\DocSiiVenta;
use App\Entity\DocSiiCompra;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\InputCall;
use App\Entity\SiiLog;
use App\Entity\User;
use App\Entity\Empresa;
use App\Entity\ListaPrecio;
use App\Entity\Producto;
use App\Entity\Stock;
use App\Entity\VersionProducto;
use App\Entity\WebhookMultivende;
/**
* Main controller.
* @Route("/main", name="mainController_")
*/
class MainController extends AbstractController
{
/**
* @Route("/dashboard", name="dashboard")
*/
public function index(): Response
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$empresaId = $user->getEmpresaId();
$webhookRepository = $em->getRepository(WebhookMultivende::class);
$inputDaysCalls = $webhookRepository->countByDays($em, 1, $empresaId);
$parameters['inputDaysCalls'] = count($inputDaysCalls);
return $this->render('main/index.html.twig', $parameters);
}
/**
* @Route("/report/getWeekData", name="getWeekData", options={"expose"=true})
*/
public function getWeekData(){
// Set up required variables
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$empresaId = $user->getEmpresaId();
$countMonthRepository = $em->getRepository(WebhookMultivende::class);
$labelA = '';
$labelB = '';
$results = $countMonthRepository->countWeekData($em, $empresaId);
$x = 0;
if(!$results){
$arrayLabel = [];
$arrayCount = [];
}
foreach($results as $data){
$arrayLabel[] = $data['monthDay'];
$arrayCount[] = $data['dayLb'];
$x++;
}
$labels = array_unique($arrayLabel);
$valores = array_count_values($arrayCount);
$arrayResponse['sucess'] = true;
$arrayResponse['labelA'] = array_values($labels);
$arrayResponse['labelB'] = array_values($valores);
$returnResponse = json_encode($arrayResponse);
return new JsonResponse($returnResponse);
// return $arrayResponse;
}
/**
* @Route("/report/getMonthData", name="getMonthData", options={"expose"=true})
*/
public function getMonthData(){
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$empresaId = $user->getEmpresaId();
$countMonthRepository = $em->getRepository(WebhookMultivende::class);
$labelA = '';
$labelB = '';
$arrayLabel = array();
$arrayCount = array();
$results = $countMonthRepository->countMonthData($em, $empresaId);
// dump($results);die;
$x = 0;
foreach($results as $data){
$arrayLabel[] = $data['FechaStr'];
$arrayCount[] = $data['month'];
$x++;
}
// dump($arrayLabel);die;
$labels = array_unique($arrayLabel);
$valores = array_count_values($arrayCount);
// dump($valores);die;
$arrayResponse['sucess'] = true;
$arrayResponse['labelA'] = array_values($labels);
$arrayResponse['labelB'] = array_values($valores);
$returnResponse = json_encode($arrayResponse);
return new JsonResponse($returnResponse);
// return $arrayResponse;
}
public function getDTDashboardAdmin(Request $request, $origin){
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$empresaId = $user->getEmpresaId();
$draw = 0;
$start = '';
$lenght = '';
$search = '';
$orders = [];
$columns = [];
if ($request->getMethod() == 'POST')
{
$draw = intval($request->request->get('draw'));
$start = $request->request->get('start'); //used for pagination
$length = $request->request->get('length'); // used for pagination
$search = $request->request->get('search'); //global search
$orders = $request->request->get('order'); //contains the required order column, as well as the sorting direction (ascending or descending)
$columns = $request->request->get('columns'); //columns as defined in the columnDefs
}
else // If the request is not a POST one, die hard
{
die;
}
foreach ($orders as $key => $order)
{
// Orders does not contain the name of the column, but its number,
// so add the name so we can handle it just like the $columns array
$orders[$key]['name'] = $columns[$order['column']]['name'];
}
$schemaManager = $em->getConnection()->getSchemaManager();
switch($origin){
case 'main':
$DBColumns = $schemaManager->listTableColumns('webhook_multivende');
break;
case 'product':
$DBColumns = $schemaManager->listTableColumns('version_producto');
break;
case 'priceList':
$DBColumns = $schemaManager->listTableColumns('lista_precio');
break;
case 'warehouse':
$DBColumns = $schemaManager->listTableColumns('bodega');
break;
case 'category':
$DBColumns = $schemaManager->listTableColumns('categoria');
break;
case 'stock':
$DBColumns = $schemaManager->listTableColumns('stock');
break;
case 'requestUser':
$DBColumns = $schemaManager->listTableColumns('user');
break;
case 'checkout':
$DBColumns = $schemaManager->listTableColumns('checkout');
break;
}
$DBColumnsName = [];
foreach($DBColumns as $column){
$DBColumnsName[] = $column->getName();
}
$useCondition = true;
switch($origin){
case 'main':
$inputCallRepository = $em->getRepository(WebhookMultivende::class);
$tableName ='webhook_multivende';
break;
case 'product':
$inputCallRepository = $em->getRepository(VersionProducto::class);
$tableName ='version_producto';
break;
case 'priceList':
$inputCallRepository = $em->getRepository(ListaPrecio::class);
$tableName ='lista_precio';
break;
case 'warehouse':
$inputCallRepository = $em->getRepository(Bodega::class);
$tableName ='bodega';
break;
case 'category':
$inputCallRepository = $em->getRepository(Categoria::class);
$tableName ='categoria';
break;
case 'stock':
$inputCallRepository = $em->getRepository(Stock::class);
$tableName ='stock';
break;
case 'requestUser':
$inputCallRepository = $em->getRepository(User::class);
$tableName ='user';
$useCondition = false;
break;
case 'checkout':
$inputCallRepository = $em->getRepository(Checkout::class);
$tableName ='checkout';
break;
}
if($useCondition){
switch($origin){
case 'main':
case 'product':
case 'priceList':
case 'warehouse':
case 'category':
case 'stock':
case 'checkout':
$otherConditions = $tableName.".empresa_id =".$empresaId ;
break;
}
}
else{
switch($origin){
default:
$otherConditions = '1 = 1';
break;
}
}
$results = $inputCallRepository->getRequiredDTData($tableName, $DBColumnsName, $start, $length, $orders, $search, $columns, $otherConditions);
// Returned objects are of type Town
$objects = $results["results"];
// Get total number of objects
$total_objects_count = $inputCallRepository->count2($tableName, $otherConditions);
// Get total number of results
$selected_objects_count = count($objects);
// Get total number of filtered data
$filtered_objects_count = $results["countResult"];
// Construct response
$response = '{
"draw": '.$draw.',
"recordsTotal": '.$total_objects_count.',
"recordsFiltered": '.$filtered_objects_count.',
"data": [';
$i = 0;
foreach ($objects as $key => $sucessPostData){
$response .= '["';
$j = 0;
$nbColumn = count($columns);
foreach ($columns as $key => $column){
// In all cases where something does not exist or went wrong, return -
$responseTemp = "-";
if(in_array($column['name'] , $DBColumnsName)){
$valSearch = array_search($column['name'], $DBColumnsName);
$DBColumnNameArray = explode('_', $DBColumnsName[$valSearch]);
if(count($DBColumnNameArray) <= 0){
$methodVal = ucfirst($DBColumnNameArray[0]);
}
else{
$upersArray = array_map('ucfirst', $DBColumnNameArray);
$methodVal = '';
foreach($upersArray as $uperArray){
$methodVal = $methodVal . $uperArray;
}
}
$methodVal = 'get'.$methodVal;
$temp = $sucessPostData->$methodVal();
// if(in_array('at', $DBColumnNameArray)){
// $responseTemp = $temp->format('Y-m-d H:i:s');
// }
if($temp instanceof \DateTime ){
$responseTemp = $temp->format('Y-m-d H:i:s');
}
else{
if(is_int($temp)){
$responseTemp = $temp;
}
else{
if(is_array($temp)){
$responseTemp = implode(' - ',$temp);
}
else{
if($temp == null){
$temp = '';
}
$firstChar = mb_substr($temp, 0, 1);
if($firstChar == '{'){
$resultTemp = json_decode($temp, true);
$responseTemp = implode(',',$resultTemp);
// dump(implode(',',$resultTemp));
}
else{
$responseTemp = $temp;
}
}
}
}
}
switch($origin){
case 'product':
switch($column['name']){
case 'producto_nombre':
case 'impuesto':
case 'concesionario':
$multivendeId = $sucessPostData->getProductoPadreMultivendeId();
$productoEntity = $em->getRepository(Producto::class)->findOneBy(array('productoMultivendeId' => $multivendeId));
switch($column['name']){
case 'producto_nombre':
$responseTemp = $productoEntity->getProductoNombre();
break;
case 'impuesto':
$responseTemp = $productoEntity->getImpuesto();
break;
case 'concesionario':
$responseTemp = $productoEntity->getConcesionario();
break;
}
break;
}
if($column['name'] == 'producto_short_description'){
// dump($responseTemp);
$nonSalt = htmlentities(str_replace(array("\r\n", "\n", "\r"), ' ', $responseTemp));
// $nonSalt = str_replace(["\n", "\r", "\t"], ' ', $responseTemp);
// dump($nonSalt);die;
$trimmed_description = strlen($responseTemp) > 20 ? substr($responseTemp, 0, 20) . "..." : $responseTemp;
$responseTemp = "<div title='".$nonSalt."'>". $trimmed_description."</div>";
}
break;
case 'checkout':
if($column['name'] == 'pdf_url'){
// $nonSalt = htmlentities(str_replace(array("\r\n", "\n", "\r"), ' ', $responseTemp));
$responseTemp = "<a href='".$responseTemp."' target='_blank'>PDF URL</a>";
}
break;
// case 'requestSii':
// if($column['name'] == 'empresa_id'){
// $empresaEntity = $em->getRepository(Empresa::class)->find($responseTemp);
// $responseTemp = "<a href='{{ path('assocEmpView')}}'>".$empresaEntity->getEmpresaRut()."</a>";
// }
// if($column['name'] == 'operacion_id'){
// switch($responseTemp){
// case 1:
// $responseTemp = 'VENTA';
// break;
// case 2:
// $responseTemp = 'COMPRA';
// break;
// }
// }
// break;
// case 'docCompra':
// case 'docVenta':
// if($column['name'] == 'razon_social'){
// $responseTemp = str_replace(' ', ' ', $responseTemp);
// }
// break;
}
$response .= $responseTemp;
if(++$j !== $nbColumn)
$response .='","';
}
$response .= '"]';
// Not on the last item
if(++$i !== $selected_objects_count)
$response .= ',';
}
$response .= ']}';
// dump($response);die;
$returnResponse = new JsonResponse();
$returnResponse->setJson($response);
return $returnResponse;
}
public function deleteElement(Request $request){
$responseJson = new JsonResponse();
$dataSend['empresa_rut'] = $request->get('rutEmpresa');
$requestForward = new Request();
$requestForward->initialize(
$requestForward->query->all(),
$requestForward->request->all(),
$requestForward->attributes->all(),
$requestForward->cookies->all(),
$requestForward->files->all(),
$requestForward->server->all(),
json_encode($dataSend)
);
$response = $this->forward('App\Controller\ApiSiiController::deleteEmpAssoc', [
'request' => $requestForward
]);
$responseJson->setJson($response->getContent());
return $responseJson;
}
}