<?php
namespace App\Controller;
use App\Entity\Shop;
use App\Entity\ShopSetting;
use App\Entity\Stock;
use App\Form\ShopType;
use App\Repository\ManufacturerRepository;
use App\Repository\OptionRepository;
use App\Repository\OrderRepository;
use App\Repository\ShopSettingRepository;
use App\Repository\SettingRepository;
use App\Repository\ShopRepository;
use App\Repository\StockRepository;
use App\Repository\UserRepository;
use Knp\Component\Pager\PaginatorInterface;
use Knp\Snappy\Pdf;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/{_locale<%app.supported_locales%>}/shop')]
class ShopController extends AbstractController
{
#[Route('/', name: 'app_shop_index', methods: ['GET'])]
public function index(ShopRepository $shopRepository, SettingRepository $settingRepository): Response
{
$marketplaces=array();
$mps=$settingRepository->getMarketplaces();
foreach($mps as $mp) {
$marketplaces[$mp->id]=$mp->name;
}
return $this->render('shop/index.html.twig', [
'shops' => $shopRepository->findAll(),
'marketplaces'=>$marketplaces
]);
}
#[Route('/new', name: 'app_shop_new', methods: ['GET', 'POST'])]
public function new(Request $request, SettingRepository $settingRepository, ShopRepository $shopRepository, ShopSettingRepository $shopSettingRepository, OptionRepository $optionRepository): Response
{
$marketplaces=$settingRepository->getMarketplaces();
$shop = new Shop();
/*$settings=$settingRepository->findBy(array('target'=>'Shop'));
foreach($settings as $setting) {
if(!$shopSettingRepository->findOneBy(array('shop'=>$shop,'setting'=>$setting))) {
$shopSetting = new ShopSetting();
$shopSetting->setShop($shop);
$shopSetting->setSetting($setting);
$shopSetting->setValue($setting->getDefaultValue());
$shopSettingRepository->add($shopSetting);
}
}
*/
$form = $this->createForm(ShopType::class, $shop, ['marketplaces'=>$marketplaces]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$mp=$settingRepository->getMarketplaceById($shop->getWttId());
$shop->setWttName($mp->name);
foreach ($form['options']->getData()->getValues() as $o) {
$option = $optionRepository->find($o->getId());
if ($option) {
$option->addShop($shop);
}
}
$shopRepository->add($shop);
return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('shop/new.html.twig', [
'shop' => $shop,
'form' => $form,
]);
}
#[Route('/{id}/users', name: 'app_shop_users', methods: ['GET'])]
public function users(Shop $shop, UserRepository $userRepository, PaginatorInterface $paginator, Request $request): Response
{
$query=$userRepository->findAllDql($shop);
$users = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
30 /*limit per page*/
);
return $this->render('shop/users.html.twig', [
'shop' => $shop,
'users'=>$users
]);
}
#[Route('/{id}', name: 'app_shop_show', methods: ['GET'])]
public function show(Shop $shop): Response
{
return $this->render('shop/show.html.twig', [
'shop' => $shop,
]);
}
#[Route('/{id}/edit', name: 'app_shop_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Shop $shop, ShopRepository $shopRepository, StockRepository $stockRepository, SettingRepository $settingRepository, ShopSettingRepository $shopSettingRepository, ManufacturerRepository $manufacturerRepository, OptionRepository $optionRepository): Response
{
$settings=$settingRepository->findBy(array('target'=>'Shop'));
$marketplaces=$settingRepository->getMarketplaces();
foreach($settings as $setting) {
if(!$shopSettingRepository->findOneBy(array('shop'=>$shop,'setting'=>$setting))) {
$shopSetting = new ShopSetting();
$shopSetting->setShop($shop);
$shopSetting->setSetting($setting);
$shopSetting->setValue($setting->getDefaultValue());
$shopSetting->setUrl($setting->getDefaultUrl());
$shopSettingRepository->add($shopSetting);
}
}
$user = $this->getUser();
$stocks = $stockRepository->findByShop($shop, $request);
$stockController=new StockController();
$list=$stockController->list($stocks, $manufacturerRepository);
$form = $this->createForm(ShopType::class, $shop, ['marketplaces'=>$marketplaces]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$mp=$settingRepository->getMarketplaceById($shop->getWttId());
$shop->setWttName($mp->name);
foreach ($form['options']->getData()->getValues() as $o) {
$option = $optionRepository->find($o->getId());
if ($option) {
$option->addShop($shop);
}
}
$shopRepository->add($shop);
return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('shop/edit.html.twig', [
'shop' => $shop,
'form' => $form,
/*'stocks' => $list['stocks'],
'manufacturers' => $list['manufacturers'],
'models' => $list['models'],
'colors' => $list['colors'],
'grades' => $list['grades'],
'categories' => $list['categories'],
'shops' => $list['shops']*/
]);
}
#[Route('/{id}', name: 'app_shop_delete', methods: ['POST'])]
public function delete(Request $request, Shop $shop, ShopRepository $shopRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$shop->getId(), $request->request->get('_token'))) {
$shopRepository->remove($shop);
}
return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/{id}/stock', name: 'app_shop_stock', methods: ['GET'])]
public function stock(Request $request, Shop $shop, StockRepository $stockRepository, ManufacturerRepository $manufacturerRepository): Response
{
$stocks = $stockRepository->findByShop($shop, $request);
$stockController=new StockController();
$list=$stockController->list($stocks, $manufacturerRepository);
return $this->render('shop/stock.html.twig', [
'shop' => $shop,
'stocks' => $list['stocks'],
'manufacturers' => $list['manufacturers'],
'models' => $list['models'],
'colors' => $list['colors'],
'grades' => $list['grades'],
'categories' => $list['categories'],
'shops' => $list['shops']
]);
}
/**
* @Route("/{id}/pdf", name="app_shop_stock_pdf", methods={"GET"})
*/
public function stockPdf(Shop $shop, StockRepository $stockRepository, ParameterBagInterface $parameterBag, Request $request, ): Response
{
$filename='bak2-stock-'.date('Y-m-d').'-'.$shop->getId().".pdf";
$filePath=$parameterBag->get('kernel.project_dir').'/public/uploads/stocks/'.$filename;
if(file_exists($filePath)) {
unlink($filePath);
}
$stocks = $stockRepository->findByShop($shop, $request);
$template='stock/pdf.html.twig';
$knpSnappyPdf=new Pdf($parameterBag->get('app.wkhtmltopdf'));
$knpSnappyPdf->setTemporaryFolder($parameterBag->get('kernel.project_dir')."/tmp");
$knpSnappyPdf->setOptions([
'enable-local-file-access' => true,
'keep-relative-links' => true,
'disable-smart-shrinking' => true,
'margin-top'=>"20mm",
'margin-right'=>"20mm",
'margin-bottom'=>"20mm",
'margin-left'=>"20mm",
]);
$html=$this->renderView(
$template,
array(
'shop'=>$shop,
'stocks' => $stocks,
)
);
$knpSnappyPdf->generateFromHtml(
$html,
$filePath
);
return $this->redirect('/uploads/stocks/'.$filename);
}
#[Route('/{id}/orders', name: 'app_shop_orders', methods: ['GET'])]
public function orders(Request $request, Shop $shop, ShopRepository $shopRepository, OrderRepository $orderRepository, PaginatorInterface $paginator): Response
{
/*$orders=array();
foreach($users as $user) {
$orders = array_merge($orders , $orderRepository->findByUser($user, $request));
}*/
//$orders=$orderRepository->findBy(['shop'=>$shop->getId()]);
$query=$orderRepository->findByDql(['shop'=>$shop->getId()]);
$orders = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
30 /*limit per page*/
);
return $this->render('shop/orders.html.twig', [
'shop'=>$shop,
'orders' => $orders,
]);
}
#[Route('/{id}/switch', name: 'app_shop_switch', methods: ['GET'])]
public function changeShop(Request $request, Shop $shop, ShopRepository $shopRepository, UserRepository $userRepository): Response
{
$user=$this->getUser();
$user->setShop($shop);
$userRepository->add($user);
return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/{id}/{shop}', name: 'app_shop_public', methods: ['GET'])]
public function public(Request $request, Shop $shop, ShopRepository $shopRepository): Response
{
return "ee";
}
}