<?php
namespace App\Controller;
use App\Entity\Agency;
use App\Entity\CarBuy;
use App\Entity\Client;
use App\Repository\ClientRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\CarSale;
// Include Dompdf required namespaces
use Dompdf\Dompdf;
use Dompdf\Options;
/**
* @Route("/agency")
*/
class AgencyController extends AbstractController
{
/**
* @Route("/sales/", name="agency_sales")
*/
public function indexSales(): Response
{
return $this->render('agency/index_sales.html.twig', [
'currentMenu' => 'agency',
]);
}
/**
* @Route("/purchases/", name="agency_purchases")
*/
public function indexPurchases(): Response
{
return $this->render('agency/index_purchases.html.twig', [
'currentMenu' => 'agency',
]);
}
/**
* @Route("/sales/{id}/forms/", name="agency_forms_sale", methods={"GET"})
*/
public function agencyFormsSale(Request $request, CarSale $carSale, $id): Response
{
$isCreated = false;
$procedureLocation = 'VILLA CARLOS PAZ';
$procedureDate = date('Y-m-d');
$procedureTime = date('H:i:s');
$businessPrice = $carSale->getCurrency().number_format($carSale->getPrice(), 0, '', '.');
$businessPayment = '';
$businessExpenses = '';
$businessDocumentation = 'CERTIF. DE TRANSFERENCIA, CONTRATO DE COMPRA VENTA Y CEDULA TITULAR. -';
$moneyAdvance = '';
$mandateSalePrice = '';
$mandateRetirementCost = '';
$mandateOthers = '';
$mandateIsShow = '';
$sellerAlternative = '';
$sellerName = '';
$sellerDni = '';
$sellerAddressName = '';
$sellerAddressNumber = '';
$sellerAddressFloor = '';
$sellerAddressApartment = '';
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findOneBy([ 'saleId' => $id ]);
$enabledNotification = false;
$status = 'no-ready';
//dump($agency); exit();
if ($agency != null) {
$isCreated = true;
$businessPrice = $agency->getBusinessPrice();
$businessPayment = $agency->getBusinessPayment();
$businessExpenses = $agency->getBusinessExpenses();
$businessDocumentation = $agency->getBusinessDocumentation();
$moneyAdvance = $agency->getMoneyAdvance();
$mandateSalePrice = $agency->getMandateSalePrice();
$mandateRetirementCost = $agency->getMandateRetirementCost();
$mandateOthers = $agency->getMandateOthers();
$mandateIsShow = $agency->getMandateIsShow();
$sellerAlternative = $agency->getSellerAlternative();
$sellerName = $agency->getSellerName();
$sellerDni = $agency->getSellerDni();
$sellerAddressName = $agency->getSellerAddressName();
$sellerAddressNumber = $agency->getSellerAddressNumber();
$sellerAddressFloor = $agency->getSellerAddressFloor();
$sellerAddressApartment = $agency->getSellerAddressApartment();
if ($agency->getEnabledNotification()) {
$enabledNotification = true;
}
$status = $agency->getStatus();
//$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findBy([ 'saleId' => $id ]);
}
//dump($carSale->getCarPayment()); exit();
//dump(json_decode($mandateSalePrice)); exit();
return $this->render('agency/document_sale.html.twig', [
'car_sale' => $carSale,
'procedure_location' => $procedureLocation,
'procedure_date' => $procedureDate,
'procedure_time' => $procedureTime,
'business_price' => $businessPrice,
'business_payment' => $businessPayment,
'business_expenses' => $businessExpenses,
'business_documentation' => $businessDocumentation,
'money_advance' => $moneyAdvance,
'seller_alternative' => $sellerAlternative,
'seller_name' => $sellerName,
'seller_dni' => $sellerDni,
'seller_address_name' => $sellerAddressName,
'seller_address_number' => $sellerAddressNumber,
'seller_address_floor' => $sellerAddressFloor,
'seller_address_apartment' => $sellerAddressApartment,
'isCreated' => $isCreated,
'enabledNotification' => $enabledNotification,
'status' => $status,
'mandate_sale_price' => json_decode($mandateSalePrice, true),
'mandate_retirement_cost' => json_decode($mandateRetirementCost, true),
'mandate_others' => json_decode($mandateOthers, true),
'mandate_is_show' => json_decode($mandateIsShow, true),
'form_number' => $agency ? $agency->getId() : '-',
'currentMenu' => 'agency',
]);
}
/**
* @Route("/purchases/{id}/forms/", name="agency_forms_purchase", methods={"GET"})
*/
public function agencyFormsPurchase(Request $request, CarBuy $carBuy, $id): Response
{
$isCreated = false;
$procedureLocation = 'VILLA CARLOS PAZ';
$procedureDate = date('Y-m-d');
$procedureTime = date('H:i:s');
$businessPrice = $carBuy->getCurrency().number_format($carBuy->getPrice(), 0, '', '.');
$businessPayment = '';
$businessExpenses = '';
$businessDocumentation = 'CERTIF. DE TRANSFERENCIA, CONTRATO DE COMPRA VENTA Y CEDULA TITULAR. -';
$mandateSalePrice = '';
$mandateRetirementCost = '';
$mandateOthers = '';
$mandateIsShow = '';
$sellerName = '';
$sellerDni = '';
$sellerAddressName = '';
$sellerAddressNumber = '';
$sellerAddressFloor = '';
$sellerAddressApartment = '';
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findOneBy([ 'saleId' => $id ]);
$enabledNotification = false;
$status = 'no-ready';
//dump($agency); exit();
if ($agency != null) {
$isCreated = true;
$businessPrice = $agency->getBusinessPrice();
$businessPayment = $agency->getBusinessPayment();
$businessExpenses = $agency->getBusinessExpenses();
$businessDocumentation = $agency->getBusinessDocumentation();
/*
$mandateSalePrice = $agency->getMandateSalePrice();
$mandateRetirementCost = $agency->getMandateRetirementCost();
$mandateOthers = $agency->getMandateOthers();
$mandateIsShow = $agency->getMandateIsShow();
$sellerAlternative = $agency->getSellerAlternative();
$sellerName = $agency->getSellerName();
$sellerDni = $agency->getSellerDni();
$sellerAddressName = $agency->getSellerAddressName();
$sellerAddressNumber = $agency->getSellerAddressNumber();
$sellerAddressFloor = $agency->getSellerAddressFloor();
$sellerAddressApartment = $agency->getSellerAddressApartment();
if ($agency->getEnabledNotification()) {
$enabledNotification = true;
}
*/
$status = $agency->getStatus();
//$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findBy([ 'saleId' => $id ]);
}
//dump($carBuy); exit();
//dump($carSale->getCar()); exit();
//dump(json_decode($mandateSalePrice)); exit();
$carSwap = $this->getDoctrine()->getRepository('App\Entity\Car')->findOneBy([ 'id' => $carBuy->getCarSwap() ]);
return $this->render('agency/document_purchase.html.twig', [
'car_sale' => $carBuy,
'car_buy' => $carBuy,
'car_swap' => $carSwap,
'procedure_location' => $procedureLocation,
'procedure_date' => $procedureDate,
'procedure_time' => $procedureTime,
'business_price' => $businessPrice,
'business_payment' => $businessPayment,
'business_expenses' => $businessExpenses,
'business_documentation' => $businessDocumentation,
'seller_name' => $sellerName,
'seller_dni' => $sellerDni,
'seller_address_name' => $sellerAddressName,
'seller_address_number' => $sellerAddressNumber,
'seller_address_floor' => $sellerAddressFloor,
'seller_address_apartment' => $sellerAddressApartment,
'isCreated' => $isCreated,
'enabledNotification' => $enabledNotification,
'status' => $status,
'mandate_sale_price' => json_decode($mandateSalePrice, true),
'mandate_retirement_cost' => json_decode($mandateRetirementCost, true),
'mandate_others' => json_decode($mandateOthers, true),
'mandate_is_show' => json_decode($mandateIsShow, true),
'currentMenu' => 'agency',
]);
}
/**
* @Route("/sales/{id}/save/", name="agency_save", methods="POST")
*/
public function agencySave(Request $request, $id)
{
$data = json_decode($request->getContent(), true);
$isPurchase = isset($data['is-purchase']) ? $data['is-purchase'] : false;
$aProcedure['location'] = $data['procedure-location'];
$aProcedure['date'] = new \DateTime($data['procedure-date'].' 00:00:01');
$aProcedure['time'] = new \DateTime($data['procedure-date'].' '.$data['procedure-time']);
$aBusiness['price'] = $data['business-price'];
$aBusiness['payment'] = $data['business-payment'];
$aBusiness['expenses'] = $data['business-expenses'];
$aBusiness['documentation'] = $data['business-documentation'];
//$moneyAdvance = $data['money-advance'];
if (!$isPurchase) {
$aMandate['sale-price'] = json_encode($data['mandate-sale-price']);
$aMandate['retirement-cost'] = json_encode($data['mandate-retirement-cost']);
$aMandate['others'] = json_encode($data['mandate-others']);
$aMandate['is-show'] = json_encode($data['mandate-is-show']);
$aSeller['alternative'] = $data['seller-alternative'];
$aSeller['name'] = $data['seller-name'];
$aSeller['dni'] = $data['seller-dni'];
$aSeller['address-name'] = $data['seller-address-name'];
$aSeller['address-number'] = $data['seller-address-number'];
$aSeller['address-floor'] = $data['seller-address-floor'];
$aSeller['address-apartment'] = $data['seller-address-apartment'];
$enabledNotification = $data['enabled-notification'] ?? false;
$status = $data['status'] ?? null;
}
$aCar['chassis-number'] = $data['car-chassis-number'];
$aCar['engine-number'] = $data['car-engine-number'];
$aCar['id'] = $data['car-id'];
$aCar['type'] = $data['car-type'];
$aCar['settlement'] = $data['car-settlement'];
$aBuyer['id'] = $data['buyer-id'];
$aBuyer['last-name'] = $data['buyer-last-name'];
$aBuyer['first-name'] = $data['buyer-first-name'];
$aBuyer['dni'] = $data['buyer-dni'];
$aBuyer['telephone'] = $data['buyer-telephone'];
$aBuyer['address'] = $data['buyer-address'];
$aBuyer['address-number'] = $data['buyer-address-number'];
$aBuyer['address-floor'] = $data['buyer-address-floor'];
$aBuyer['address-apartment'] = $data['buyer-address-apartment'];
$aBuyer['location'] = $data['buyer-location'];
$aBuyer['state'] = $data['buyer-state'];
/*dump($aProcedure);
dump($aBusiness);
dump($id);*/
/*---- Agency [BEGIN] ----*/
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findOneBy([ 'saleId' => $id ]);
if ($agency == null) {
$agency = new Agency();
$agency->setSaleId($id);
$agency->setCreatedAt(new \DateTime);
} else {
}
$agency->setProcedureLocation($aProcedure['location']);
$agency->setProcedureDate($aProcedure['date']);
$agency->setProcedureTime($aProcedure['time']);
$agency->setBusinessPrice($aBusiness['price']);
$agency->setBusinessPayment($aBusiness['payment']);
$agency->setBusinessExpenses($aBusiness['expenses']);
$agency->setBusinessDocumentation($aBusiness['documentation']);
//$agency->setMoneyAdvance($moneyAdvance);
if (!$isPurchase) {
$agency->setMandateSalePrice($aMandate['sale-price']);
$agency->setMandateRetirementCost($aMandate['retirement-cost']);
$agency->setMandateOthers($aMandate['others']);
$agency->setMandateIsShow($aMandate['is-show']);
$agency->setSellerAlternative($aSeller['alternative']);
$agency->setSellerName($aSeller['name']);
$agency->setSellerDNI($aSeller['dni']);
$agency->setSellerAddressName($aSeller['address-name']);
$agency->setSellerAddressNumber($aSeller['address-number']);
$agency->setSellerAddressFloor($aSeller['address-floor']);
$agency->setSellerAddressApartment($aSeller['address-apartment']);
$agency->setEnabledNotification($enabledNotification);
$agency->setStatus($status);
} else {
$agency->setSellerName('');
$agency->setSellerDNI('');
$agency->setSellerAddressName('');
$agency->setSellerAddressNumber('');
$agency->setSellerAddressFloor('');
$agency->setSellerAddressApartment('');
}
$agency->setUpdatedAt(new \DateTime);
$this->getDoctrine()->getManager()->persist($agency);
$this->getDoctrine()->getManager()->flush();
/*---- Agency [END] ----*/
/*---- Car [START] ----*/
$car = $this->getDoctrine()->getRepository('App\Entity\Car')->find($aCar['id']);
$car->setType($aCar['type']);
$car->setSettlement($aCar['settlement']);
$car->setChassisNumber($aCar['chassis-number']);
$car->setEngineNumber($aCar['engine-number']);
$this->getDoctrine()->getManager()->persist($car);
$this->getDoctrine()->getManager()->flush();
/*---- Car [END] ----*/
/*---- Buyer [BEGIN] ----*/
//dump($aBuyer); exit();
$buyer = $this->getDoctrine()->getRepository('App\Entity\Client')->find($aBuyer['id']);
$buyer->setLastName($aBuyer['last-name']);
$buyer->setFirstName($aBuyer['first-name']);
$buyer->setDni($aBuyer['dni']);
$buyer->setCellphone($aBuyer['telephone']);
$buyer->setAddress($aBuyer['address']);
$buyer->setAddressNumber($aBuyer['address-number']);
$buyer->setAddressFloor($aBuyer['address-floor']);
$buyer->setAddressApartment($aBuyer['address-apartment']);
$buyer->setLocation($aBuyer['location']);
$buyer->setState($aBuyer['state']);
$this->getDoctrine()->getManager()->persist($buyer);
$this->getDoctrine()->getManager()->flush();
/*---- Buyer [END] ----*/
//dump($agency);
//dump($car);
//exit();
return $this->json('ok');
}
/**
* @Route("/sales/{id}/pdf/", name="agency_pdf", methods="GET")
*/
public function agencyPdf(Request $request, CarSale $carSale, $id)
{
$procedureLocation = 'VILLA CARLOS PAZ';
$procedureDate = date('Y-m-d');
$procedureTime = date('H:i:s');
$businessPrice = $carSale->getCurrency().number_format($carSale->getPrice(), 0, '', '.');
$businessPayment = '';
$businessExpenses = '';
$businessDocumentation = 'CERTIF. DE TRANSFERENCIA, CONTRATO DE COMPRA VENTA Y CEDULA TITULAR. -';
$mandateSalePrice = '';
$mandateRetirementCost = '';
$mandateOthers = '';
$mandateIsShow = '';
$sellerName = '';
$sellerDni = '';
$sellerAddressName = '';
$sellerAddressNumber = '';
$sellerAddressFloor = '';
$sellerAddressApartment = '';
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findOneBy([ 'saleId' => $id ]);
//dump($agency); exit();
if ($agency != null) {
$isCreated = true;
$businessPrice = $agency->getBusinessPrice();
$businessPayment = $agency->getBusinessPayment();
$businessExpenses = $agency->getBusinessExpenses();
$businessDocumentation = $agency->getBusinessDocumentation();
$mandateSalePrice = $agency->getMandateSalePrice();
$mandateRetirementCost = $agency->getMandateRetirementCost();
$mandateOthers = $agency->getMandateOthers();
$mandateIsShow = $agency->getMandateIsShow();
$sellerAlternative = $agency->getSellerAlternative();
$sellerName = $agency->getSellerName();
$sellerDni = $agency->getSellerDni();
$sellerAddressName = $agency->getSellerAddressName();
$sellerAddressNumber = $agency->getSellerAddressNumber();
$sellerAddressFloor = $agency->getSellerAddressFloor();
$sellerAddressApartment = $agency->getSellerAddressApartment();
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findBy([ 'saleId' => $id ]);
}
// Configure Dompdf according to your needs
$pdfOptions = new Options();
//$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsHtml5ParserEnabled(true);
$pdfOptions->setIsRemoteEnabled(true);
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
//exit( $this->getParameter('kernel.project_dir') );
$dompdf->getOptions()->setChroot( $this->getParameter('kernel.project_dir').'\public' );
// Retrieve the HTML generated in our twig file
$html = $this->renderView('pdf/agency_sales.html.twig', [
'car_sale' => $carSale,
'procedure_location' => $procedureLocation,
'procedure_date' => $procedureDate,
'procedure_time' => $procedureTime,
'business_price' => $businessPrice,
'business_payment' => $businessPayment,
'business_expenses' => $businessExpenses,
'business_documentation' => $businessDocumentation,
'seller_alternative' => $sellerAlternative,
'seller_name' => $sellerName,
'seller_dni' => $sellerDni,
'seller_address_name' => $sellerAddressName,
'seller_address_number' => $sellerAddressNumber,
'seller_address_floor' => $sellerAddressFloor,
'seller_address_apartment' => $sellerAddressApartment,
/*'mandate_sale_price' => $mandateSalePrice,
'mandate_retirement_cost' => $mandateRetirementCost,
'mandate_others' => $mandateOthers,*/
'mandate_sale_price' => json_decode($mandateSalePrice, true),
'mandate_retirement_cost' => json_decode($mandateRetirementCost, true),
'mandate_others' => json_decode($mandateOthers, true),
'mandate_is_show' => json_decode($mandateIsShow, true),
'server_root' => $_SERVER["DOCUMENT_ROOT"],
]);
//dump($html); exit();
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
ob_get_clean();
// Output the generated PDF to Browser (inline view)
$dompdf->stream("/mypdf.pdf", [
"Attachment" => false,
]);
//exit(0);
}
/**
* @Route("/purchase/{id}/pdf/", name="agency_pdf_purchase", methods="GET")
*/
public function agencyPdfPurchase(Request $request, CarBuy $carBuy, $id)
{
$procedureLocation = 'VILLA CARLOS PAZ';
$procedureDate = date('Y-m-d');
$procedureTime = date('H:i:s');
$businessPrice = $carBuy->getCurrency().number_format($carBuy->getPrice(), 0, '', '.');
$businessPayment = '';
$businessExpenses = '';
$businessDocumentation = 'CERTIF. DE TRANSFERENCIA, CONTRATO DE COMPRA VENTA Y CEDULA TITULAR. -';
$mandateSalePrice = '';
$mandateRetirementCost = '';
$mandateOthers = '';
$mandateIsShow = '';
$sellerName = '';
$sellerDni = '';
$sellerAddressName = '';
$sellerAddressNumber = '';
$sellerAddressFloor = '';
$sellerAddressApartment = '';
$agency = $this->getDoctrine()->getRepository('App\Entity\Agency')->findOneBy([ 'saleId' => $id ]);
$enabledNotification = false;
$status = 'no-ready';
if ($agency != null) {
$isCreated = true;
$businessPrice = $agency->getBusinessPrice();
$businessPayment = $agency->getBusinessPayment();
$businessExpenses = $agency->getBusinessExpenses();
$businessDocumentation = $agency->getBusinessDocumentation();
$status = $agency->getStatus();
}
$carSwap = $this->getDoctrine()->getRepository('App\Entity\Car')->findOneBy([ 'id' => $carBuy->getCarSwap() ]);
// Configure Dompdf according to your needs
$pdfOptions = new Options();
//$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsHtml5ParserEnabled(true);
$pdfOptions->setIsRemoteEnabled(true);
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
//exit( $this->getParameter('kernel.project_dir') );
$dompdf->getOptions()->setChroot( $this->getParameter('kernel.project_dir').'\public' );
// Retrieve the HTML generated in our twig file
$html = $this->renderView('pdf/agency_purchases.html.twig', [
'car_sale' => $carBuy,
'car_swap' => $carSwap,
'procedure_location' => $procedureLocation,
'procedure_date' => $procedureDate,
'procedure_time' => $procedureTime,
'business_price' => $businessPrice,
'business_payment' => $businessPayment,
'business_expenses' => $businessExpenses,
'business_documentation' => $businessDocumentation,
'seller_name' => $sellerName,
'seller_dni' => $sellerDni,
'seller_address_name' => $sellerAddressName,
'seller_address_number' => $sellerAddressNumber,
'seller_address_floor' => $sellerAddressFloor,
'seller_address_apartment' => $sellerAddressApartment,
'isCreated' => $isCreated,
'enabledNotification' => $enabledNotification,
'status' => $status,
'mandate_sale_price' => json_decode($mandateSalePrice, true),
'mandate_retirement_cost' => json_decode($mandateRetirementCost, true),
'mandate_others' => json_decode($mandateOthers, true),
'mandate_is_show' => json_decode($mandateIsShow, true),
'server_root' => $_SERVER["DOCUMENT_ROOT"],
]);
//dump($html); exit();
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
ob_get_clean();
// Output the generated PDF to Browser (inline view)
$dompdf->stream("/Compra - Boleto.pdf", [
"Attachment" => false,
]);
//exit(0);
}
/**
* @Route("/settings/", name="agency_settings", methods={"GET"})
*/
public function agencySettings(): Response
{
$notificationDays = $this->getDoctrine()->getRepository(\App\Entity\Settings::class)->findOneBy(['name' => 'agency_notification_days'])->getValue();
return $this->render('agency/settings.html.twig', [
'notificationDays' => $notificationDays,
'currentMenu' => 'agency',
]);
}
/**
* @Route("/save/notificationDdays", name="agency_save_notification_days", methods={"POST"})
*/
public function saveNotificationDays(Request $request): Response
{
$setting = $this->getDoctrine()->getManager()->getRepository(\App\Entity\Settings::class)->findOneBy(['name' => 'agency_notification_days']);
$setting->setValue( $request->get('d') );
$this->getDoctrine()->getManager()->persist($setting);
$this->getDoctrine()->getManager()->flush();
return $this->json('1');
}
}