<?php
namespace App\Controller;
use App\Entity\Company;
use App\Entity\CompanyGroup;
use App\Entity\Product;
use App\Form\Filter\AppGraphFilterType;
use App\Repository\CompanyRepository;
use App\Repository\OrderRepository;
use App\Repository\ProductRepository;
use App\Services\GalosoftGraphsFactory;
use App\Services\GalosoftMailer;
use App\Services\GalosoftMailerInterface;
use Gotenberg\Gotenberg;
use Gotenberg\Stream;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
class IndexController extends AbstractController
{
const DASHBOARD_COLUMN = 'dashboard_column';
const DASHBOARD_DATEFROM = 'dashboard_datefrom';
const DASHBOARD_DATETO = 'dashboard_dateto';
/**
* @Route("/", name="app_homepage")
*/
public function index(
Session $session,
CompanyRepository $companyRepository,
GalosoftGraphsFactory $galosoftGraphsFactory,
OrderRepository $orderRepository
): Response
{
$selectedColumn = $session->get(self::DASHBOARD_COLUMN, 3);
$dateFrom = $session->get(self::DASHBOARD_DATEFROM, new \DateTime('now-12months'));
$dateTo = $session->get(self::DASHBOARD_DATETO, new \DateTime('now'));
$qb = $companyRepository->createQueryBuilder('qc');
$qb->leftJoin('qc.companyFiles', 'companyFiles');
$qb->andWhere($qb->expr()->isInstanceOf('qc', CompanyGroup::class));
//$qb->andWhere($qb->expr()->neq('companyFiles.showOnWebsite', false));
$companies = $qb->getQuery()->getResult();
// CURRENT USER
$graphMyProduction = $galosoftGraphsFactory->create();
$gmp = $galosoftGraphsFactory->createLine();
if($this->isGranted('ROLE_COMPANY_ADMIN') || $this->isGranted('ROLE_COMPANY_USER')){
// company admin
$dataMyProduction = $orderRepository->ordersByCompanyGroupByDays($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
}elseif ($this->isGranted('ROLE_MERCHANT')){
// merchant
$dataMyProduction = $orderRepository->orderCountByDays($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
}
foreach ($dataMyProduction as $row) {
$gmp->createPoint($row['den'], $row['total']);
}
$graphMyProduction->addLine($gmp);
// BY Companies
$graphMyProductionByCompanies = $galosoftGraphsFactory->create();
$dataMyProduction = $orderRepository->orderCountByCompaniesByDays($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
$lines = [];
foreach ($dataMyProduction as $row) {
if(!isset($lines[$row['company_group_id']])){
$lines[$row['company_group_id']] = $galosoftGraphsFactory->createLine($row['company_label']);
}
$lines[$row['company_group_id']]->createPoint($row['den'], $row['total']);
}
foreach ($lines as $line) {
$graphMyProductionByCompanies->addLine($line);
}
// BY EMITENTS
$graphMyProductionByProducts = $galosoftGraphsFactory->create();
if($this->isGranted('ROLE_COMPANY_ADMIN') || $this->isGranted('ROLE_COMPANY_USER')){
// comapny admin
$dataMyProduction = $orderRepository->orderByCompanyGroupByProductsByDays($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
}elseif($this->isGranted('ROLE_MERCHANT')){
// merchant
$dataMyProduction = $orderRepository->orderCountByProductsByDays($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
}
$lines = [];
foreach ($dataMyProduction as $row) {
if(!isset($lines[$row['product_id']])){
$lines[$row['product_id']] = $galosoftGraphsFactory->createLine($row['product_label']);
}
$lines[$row['product_id']]->createPoint($row['den'], $row['total']);
}
foreach ($lines as $line) {
$graphMyProductionByProducts->addLine($line);
}
$graphMyProductionByNetworks = $galosoftGraphsFactory->create();
$dataOtherProduction = $orderRepository->orderByAffilCode($dateFrom, $dateTo, $this->getUser(), $selectedColumn);
$filter = $this->createForm(AppGraphFilterType::class);
return $this->render('index/emitents.html.twig', [
'companies' => $companies,
'graphMyProduction' => $graphMyProduction,
'graphMyProductionByCompanies' => $graphMyProductionByCompanies,
'graphMyProductionByProducts' => $graphMyProductionByProducts,
'graphMyProductionByNetworks' => $graphMyProductionByNetworks,
'tableOtherProduction' => $dataOtherProduction,
'selectedColumn' => $selectedColumn,
'dateFrom' => $dateFrom->format('j.n.Y'),
'dateTo' => $dateTo->format('j.n.Y'),
'form' => $filter->createView()
]);
}
/**
* @Route("/dashboard/settings", name="app_dashboard_settings")
*/
public function saveDashboardColumn(Request $request, Session $session){
$dashboardColumn = $request->request->get('dashboard_column');
$appGraphFilter = $request->request->get('app_graph_filter');
$session->set(self::DASHBOARD_COLUMN, $dashboardColumn);
$session->set(self::DASHBOARD_DATEFROM, \DateTime::createFromFormat('j.n.Y', $appGraphFilter['dateFrom']));
$session->set(self::DASHBOARD_DATETO, \DateTime::createFromFormat('j.n.Y', $appGraphFilter['dateTo']));
return $this->forward(IndexController::class. '::index');
}
/**
* @Route("/emitent/{emitent}", name="app_emitent_detail")
*/
public function emitentDetail(
Company $emitent,
ProductRepository $productRepository,
CompanyRepository $companyRepository
): Response
{
$companyIds = [$emitent->getId()];
$companies = $companyRepository->findBy(['companyGroup' => $emitent->getId()]);
foreach ($companies as $company) {
$companyIds[] = $company->getId();
}
$qbpr = $productRepository->createQueryBuilder('pr');
$qbpr
->andWhere($qbpr->expr()->gt('pr.webOrder', 0))
->andWhere($qbpr->expr()->isNotNull('pr.webOrder'))
->andWhere($qbpr->expr()->in('pr.company', $companyIds))
->addOrderBy('pr.webOrder', 'ASC');
$products = $qbpr->getQuery()->getResult();
return $this->render('index/emitent.html.twig', [
'emitent' => $emitent,
'products' => $products
]);
}
/**
* @Route("/kontakty", name="app_index_kontakty")
*/
public function kontakty(){
$bossUsers = $this->getUser()->getBosses();
return $this->render('index/contact.html.twig', [
'bossRelationFirstLine' => $bossUsers
]);
}
/**
* @Route("/eshop-bak", name="app_index_eshop")
*/
public function eshop(){
return $this->render('index/eshop.html.twig');
}
/**
* @Route("/basket-1", name="app_index_basket_1")
*/
public function basket1(){
return $this->render('index/basket1.html.twig');
}
/**
* @Route("/basket-2", name="app_index_basket_2")
*/
public function basket2(){
return $this->render('index/basket2.html.twig');
}
/**
* @Route("/basket-3", name="app_index_basket_3")
*/
public function basket3(){
return $this->render('index/basket3.html.twig');
}
/**
* @Route("/basket-4", name="app_index_basket_4")
*/
public function basket4(){
return $this->render('index/basket4.html.twig');
}
/**
* @Route("/basket-5", name="app_index_basket_5")
*/
public function basket5(){
return $this->render('basket/basket5.html.twig');
}
/**
* @Route("/kalendar-akci", name="app_index_calendar")
*/
public function calendar(){
return $this->render('index/calendar.html.twig');
}
/**
* @Route("/invoice", name="app_index_invoice")
*/
public function invoice(GalosoftMailerInterface $mailer){
$data = [
"id" => "123",
"variable" => "123",
"issued" => date("j.n.Y", strtotime("10.8.2023")),
"due" => date("j.n.Y", strtotime("12.8.2023")),
"name" => "ABC s.r.o.",
"surname" => "",
"street" => "Ulice 123",
"postal" => "123 00",
"town" => "Město",
"ic" => "123456789",
"dic" => "CZ123456789",
"totalPriceExclVat" => number_format(200, 2, ",", " "),
"totalPriceVat" => number_format(42, 2, ",", " "),
"totalPriceInclVat" => number_format(242, 2, ",", " "),
"items" => [
0 => [
"name" => "Zlatý slitek 1 Oz Argor Heraeus",
"count" => number_format(1,0, "", ""),
"vat" => number_format(21, 0, "", ""),
"priceExclVat" => number_format(100, 2, ",", " "),
"priceVat" => number_format(21, 2, ",", " "),
"priceInclVat" => number_format(121, 2, ",", " "),
"totalPriceExclVat" => number_format(100, 2, ",", " "),
"totalPriceVat" => number_format(21, 2, ",", " "),
"totalPriceInclVat" => number_format(121, 2, ",", " "),
],
1 => [
"name" => "Zlatý slitek 1 Oz Argor Heraeus",
"count" => number_format(1,0, "", ""),
"vat" => number_format(21, 0, "", ""),
"priceExclVat" => number_format(100, 2, ",", " "),
"priceVat" => number_format(21, 2, ",", " "),
"priceInclVat" => number_format(121, 2, ",", " "),
"totalPriceExclVat" => number_format(100, 2, ",", " "),
"totalPriceVat" => number_format(21, 2, ",", " "),
"totalPriceInclVat" => number_format(121, 2, ",", " "),
]
],
];
$urlPreffix = '../public/files/temp';
if (!is_dir($urlPreffix)) mkdir($urlPreffix);
$html = $this->render('index/invoice.html.twig', ["data" => $data])->getContent();
$request = Gotenberg::chromium("gotenberg:3000")
->html(Stream::string('my.html',$html));
$file = Gotenberg::save($request, $urlPreffix);
$mailer->send([
"template_id" => "d-ac95fa387b2f4ab486b4940fa53578ff",
"from" => array(
"name" => "EFS",
"email" => "info@efsgroup.cz"
),
"personalizations" => array(
array(
"dynamic_template_data" => $data,
"to" => array(
array(
"email" => "kohout@galosoft.cz"
),
)
)
),
"attachments" => [
[
//TODO - cesta k souboru bude /public/files/contracts/ID/HASH.pdf
//TODO - pristup k ni bude take ze zalozky dokumenty v detailu objednavky viz prejmenovani zalozky dle EFS-159
"content" => base64_encode(file_get_contents($urlPreffix."/".$file)),
"type" => "pdf",
"filename" => "Zálohová faktura č." . $data["id"] . ".pdf"
]
]
]);
return $this->render('index/invoice.html.twig', ["data" => $data]);
}
}