<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class DefaultController extends AbstractController
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
public function index(): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products?access_token='.$apiToken.'&orderby=price'
);
$slug = array();
$i = 0;
$content = $response->toArray();
/*
foreach($content as $key => $value) {
if($value){
switch($value["id"]){
case 611: // Cromas Carmenere 2020 los vascos
$slug[0] = $value["slug"];
break;
case 612: // Cromas Cabernet Franc Gran Reserva 2019 Los Vascos
$slug[1] = $value["slug"];
break;
case 624: // Cromas Cabernet Sauvignon Gran Reserva 2018 Los Vascos
$slug[2] = $value["slug"];
break;
case 614: // Cromas Syrah Gran Reserva 2019 Los Vascos
$slug[3] = $value["slug"];
break;
case 615: // Chardonnay 2021 Los Vascos
$slug[4] = $value["slug"];
break;
case 616: // Cabernet Sauvignon 2019 Los Vascos
$slug[5] = $value["slug"];
break;
case 617: // Rosé 2022 Los Vascos
$slug[6] = $value["slug"];
break;
case 618: // Sauvignon Blanc 2021 Los Vascos
$slug[7] = $value["slug"];
break;
case 547: // Le Dix 2017
$slug[8] = $value["slug"];
break;
case 620: // Magnum Cromas Cabernet Sauvignon Gran Reserva 2018 Los Vascos
$slug[9] = $value["slug"];
break;
case 621: // Magnum Cromas Carmenere 2020 Los Vascos
$slug[10] = $value["slug"];
break;
}
$i++;
}
}*/
$parameters["slug"] = $content;
} catch (Exception $e) {
dump("f");
}
return $this->render('home.html.twig',$parameters);
}
public function contacto(): Response
{
return $this->render('contacto.html.twig');
}
public function vinos(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$valmin = $request->get('valmin');
$valmax = $request->get('valmax');
$tags = $request->get('etiquetas');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tags.'&price_min='.$valmin.'&price_max='.$valmax.'&orderby=price'
);
$content = $response->toArray();
$parameters["products"] = $content;
$i = 0;
foreach ($parameters["products"] as $data) {
if($data["tags"] != null ){
$tag = explode(",", $data['tags']);
foreach ($tag as $t){
if($t == "linea1"){
$data["linea"] = 1;
$parameters["products"][$i] = $data;
}
if($t == "linea2"){
$data["linea"] = 2;
$parameters["products"][$i] = $data;
}
if($t == "linea3"){
$data["linea"] = 3;
$parameters["products"][$i] = $data;
}
if($t == "Mendoza"){
$data["linea"] = 4;
$parameters["products"][$i] = $data;
}
}
}
$i++;
}
$parameters["valmin"] = $valmin;
$parameters["valmax"] = $valmax;
$parameters["tags"] = $tags;
} catch (Exception $e) {
dump("f");
}
return $this->render('vinos.html.twig', $parameters);
}
public function producto(Request $request, $slug): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$tag = $request->query->get('tag');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/slug/'.$slug.'?access_token='.$apiToken
);
$response2 = $this->client->request(
'GET',
$apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tag
);
$content = $response->toArray();
$content2 = $response2->toArray();
$tags = explode(",", $content['tags']);
$parameters["product"] = $content;
$parameters["products"] = $content2;
$parameters["tags"] = $tags;
//$parameters["metadata"]= $this->metadata($content);
} catch (Exception $e) {
dump("f");
}
return $this->render('producto.html.twig', $parameters);
}
public function productoId(Request $request, $id): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/'.$id.'?access_token='.$apiToken
);
$response2 = $this->client->request(
'GET',
$apiBaseUrl.'marketplace/products?access_token='.$apiToken
);
$content = $response->toArray();
$content2 = $response2->toArray();
$tags = explode(",", $content['tags']);
$parameters["product"] = $content;
$parameters["products"] = $content2;
$parameters["tags"] = $tags;
//$parameters["metadata"]= $this->metadata($content);
} catch (Exception $e) {
dump("f");
}
return $this->render('producto.html.twig', $parameters);
}
public function addToCart(Request $request)
{
$productObject["product_id"] = intval($request->get('product_id'));
$productObject["variant_id"] = intval($request->get('variant_id'));
$productObject["quantity_products"] = intval($request->get('quantity_products'));
if($request->getSession()->get('cartId') == null){
$productObject["cart_id"] = null;
}
else {
$productObject["cart_id"] = intval($request->getSession()->get('cartId'));
}
$product = json_encode($productObject);
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$response = $this->client->request(
'POST',
$apiBaseUrl.'products/shopping-cart?access_token='.$apiToken,
[
'body' => $product,
]
);
$content = $response->toArray()[0];
$request->getSession()->set('cartId',$content['cart_id']);
return $this->redirectToRoute('carrito');
}
public function currentItems(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->getSession()->get('cartId');
if ($cartId != null){
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
);
$cart = $response->toArray();
if(count($cart) == 1){
$cart = $cart[0];
}
$total_tours_apalta = 0;
foreach($cart["bookings"] as $booking){
if($booking['tour_subcategoria'] == "Apalta"){
$total_tours_apalta++;
}
}
$cart['total_tours_apalta'] = $total_tours_apalta;
return new JsonResponse($cart);
}else{
return new JsonResponse(null);
}
}
public function deleteFromCart(Request $request,$saleId)
{
$code = $request->get('code');
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->getSession()->get('cartId');
if ($cartId != null)
{
$response = $this->client->request(
'DELETE',
$apiBaseUrl.'products/shopping-cart/sale/'.strval($saleId).'?access_token='.$apiToken
);
$cart = $response->toArray();
if($cart[0]['products_sales']==[] && $cart[0]['bookings']==[]){
$request->getSession()->remove('cartId');
}
}
return $this->redirectToRoute('carrito', array('code'=>$code));
}
public function checkProductVariant(Request $request)
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$variantName = $request->query->get('variantName');
$productId = $request->query->get('productId');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/check/variant?variantName='.$variantName.'&productId='.$productId.'&access_token='.$apiToken
);
$content = $response->toArray();
$parameters["product"] = $content;
} catch (Exception $e) {
dump("f");
}
return new JsonResponse($content);
}
public function carrito(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->getSession()->get('cartId');
if ($cartId != null){
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
);
$cart = $response->toArray();
if(count($cart) == 1){
$cart = $cart[0];
}
$parameters["cart"] = $cart;
$parameters["products"] = $cart['products_sales'];
$parameters["code"] = $request->get('coupon_code');
}else{
return $this->redirectToRoute('vinos');
}
return $this->render('carrito.html.twig', $parameters);
}
public function comunas(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$regionId = $request->get('regionId');
$response = $this->client->request(
'GET',
$apiBaseUrl.'api-comunas/'.$regionId.'?access_token='.$apiToken
);
$cart = $response->toArray();
return new JsonResponse($cart);
}
public function checkout(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$shippingResponse = $this->client->request(
'GET',
$apiBaseUrl.'shipping-methods?access_token='.$apiToken
);
$shippingMethods = $shippingResponse->toArray();
$cartId = $request->getSession()->get('cartId');
if($cartId == null){
return $this->redirectToRoute('index');
}
if ($request->isMethod('POST')) {
$customerObject["formaPago"] = 7;
$customerObject["currency"] = "CLP";
$customerObject["method_shipping_id"] = intval($request->get('method_shipping_id'));
$customerObject["costumerName"] = $request->get('costumerName');
$customerObject["costumerPhone"] = $request->get('costumerPhone');
$customerObject["costumerEmail"] = $request->get('costumerEmail');
$customerObject["shippingName"] = $request->get('shippingName');
$customerObject["shippingRut"] = $request->get('shippingRut');
$customerObject["billingName"] = $request->get('shippingName');
$customerObject["billingRut"] = $request->get('shippingRut');
if($request->get('method_region_id')){
$customerObject["shippingRegion"] = $request->get('method_region_id');
}else{
$customerObject["shippingRegion"] = 13;
}
if($request->get('shippingCity')){
$customerObject["shippingCity"] = $request->get('shippingCity');
$customerObject["billingCity"] = $request->get('shippingCity');
}else{
$customerObject["billingCity"] = "Santiago";
$customerObject["shippingCity"] = "Santiago";
}
if($request->get('shippingCommune')){
$customerObject["shippingCommune"] = $request->get('shippingCommune');
$customerObject["billingCommune"] = $request->get('shippingCommune');
}else{
$customerObject["billingCommune"] = "Santiago";
$customerObject["shippingCommune"] = "Santiago";
}
if($request->get('shippingAddress')){
$customerObject["billingAddress"] = $request->get('shippingAddress');
$customerObject["shippingAddress"] = $request->get('shippingAddress');
}else{
$customerObject["billingAddress"] = "Santiago";
$customerObject["shippingAddress"] = "Santiago";
}
$customerObject["costo-envio"] = $request->get('costo-envio');
$customer = json_encode($customerObject);
$response = $this->client->request(
'POST',
$apiBaseUrl.'products/shopping-cart/checkout/'.$cartId.'?access_token='.$apiToken,
[
'body' => $customer,
]
);
$response = $response->toArray();
$response = $response[0];
$cartId = $response['cart_id'];
$cartCode = $response['cart_code'];
$price = $response['converted_total_price'];
$pagoBase64 = base64_encode($cartId."@;".$cartCode."@;".$price);
//$request->getSession()->remove('cartId');
//$webpayURL = 'https://webpay-colchagua.tourpay.cl/sdk/tbk-normal.php?data='.$pagoBase64;
//$urlBase = $response['test_url'];
$urlBase = $response['payment_url'] ?? null;
$webpayURL = null;
if (isset($response['payment_url_mall'])) {
$webpayURL = $response['payment_url_mall'];
return $this->redirect($webpayURL);
}
}
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/shopping-cart/'.$cartId.'?access_token='.$apiToken
);
$cart = $response->toArray();
if(count($cart) == 1){
$cart = $cart[0];
}
$parameters["cart"] = $cart;
$parameters["shippingMethods"] = $shippingMethods;
return $this->render('checkout.html.twig', $parameters);
}
public function pagado(Request $request, $id): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/shopping-cart/'.$id.'?access_token='.$apiToken
);
$cart = $response->toArray();
$parameters["cart"] = $cart[0];
$request->getSession()->remove('cartId');
return $this->render('pago/pagado.html.twig', $parameters);
}
public function rechazo(Request $request, $id): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->getSession()->get('cartId');
$request->getSession()->remove('cartId');
return $this->render('pago/rechazo.html.twig');
}
public function error(Request $request, $id, $id1): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->getSession()->get('cartId');
$request->getSession()->remove('cartId');
return $this->render('pago/error.html.twig');
}
public function changeQuantityShoppingCart(Request $request)
{
$apiToken = $this->getParameter('api_token');
$apiUrl = $this->getParameter('api_url');
$locale = $request->getLocale();
$variant_Id = $request->query->get('variantId');
$product_Id = $request->query->get('productId');
$quantity_products = $request->query->get('quantity_products');
$cart_Id = $request->query->get('cartId');
$discounted_price = $request->query->get('discountedPrice');
if($discounted_price == 0){
$discounted_price = null;
}
$response = $this->client->request(
'GET',
$apiUrl.'products/shopping-cart/change/quantity?variant_Id='.$variant_Id.'&product_Id='.$product_Id.'&quantity_products='.$quantity_products.'&cart_Id='.$cart_Id.'&discounted_price='.$discounted_price.'&access_token='.$apiToken
);
return new JsonResponse($response->getContent());
}
public function validarCupon($cartId,$code): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/applicate-coupon/'.$cartId.'/'.$code.'?access_token='.$apiToken
);
$statuCodeP = $response->getStatusCode();
if($statuCodeP == 200 ){
$parametros['producto'] = $response->toArray();
}else{
$parametros['producto'] = [];
}
return new JsonResponse($parametros);
}
public function productosTag(Request $request, $tag): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products?access_token='.$apiToken.'&tag='.$tag.'&orderby=name'
);
$content = $response->toArray();
$parameters["products"] = $content;
$i = 0;
foreach ($parameters["products"] as $data) {
if($data["tags"] != null ){
$tags = explode(",", $data['tags']);
foreach ($tags as $t){
if($t == $tag){
$data["linea"] = 1;
$parameters["products"][$i] = $data;
}
}
}
$i++;
}
$parameters["tag"] = $tag;
} catch (Exception $e) {
dump("f");
}
return $this->render('vinos.html.twig', $parameters);
}
public function nosotros(): Response
{
return $this->render('nosotros.html.twig');
}
public function tyc(): Response
{
return $this->render('TyC.html.twig');
}
public function infoPractica(): Response
{
return $this->render('infoPractica.html.twig');
}
public function calculateDynamicShipping(Request $request): Response
{
$apiToken = $this->getParameter('api_token');
$apiBaseUrl = $this->getParameter('api_url');
$cartId = $request->query->get('cartId');
$shippingMethod = $request->query->get('shippingMethod');
$regionId = $request->query->get('regionId')??0;
try {
$response = $this->client->request(
'GET',
$apiBaseUrl.'products/shopping-cart/calculate-dynamic-shipping/'.$cartId.'/'.$shippingMethod.'/'.$regionId.'?access_token='.$apiToken
);
$content_array = $response->toArray();
$parameters["shipping_value"] = $content_array;
} catch (Exception $e) {
}
return new JsonResponse($content_array);
}
}