<?php
namespace App\Controller\Manager;
use App\Entity\Paper;
use League\Csv\Writer;
use App\Services\PaperSearch;
use App\Repository\PaperRepository;
use App\Form\Manager\PaperFilterType;
use App\Repository\PaperProductRepository;
use App\Services\ExportManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
#[Route('/manager/stocks', name: 'stock_')]
class StockController extends AbstractController
{
protected $paperProductRepository;
public function __construct(PaperProductRepository $paperProductRepository)
{
$this->paperProductRepository = $paperProductRepository;
}
#[Route('/', name: 'index')]
public function index(PaperRepository $paperRepository, PaperSearch $paperSearch): Response
{
$pagination = $paperRepository->findStocks(200);
$papers = $pagination->getItems();
$count = 0;
$price = 0;
$quantity = 0;
foreach($paperRepository->findBy(['archived' => false]) as $paper) {
$price += $paper->getPrice();
$quantity += $paper->getQuantity();
$count++;
}
$form = $this->createForm(PaperFilterType::class, null, [
'papers' => $paperSearch->getPapers()
]);
return $this->render('manager/stock/index.html.twig', [
'pagination' => $pagination,
'papers' => $papers,
'price' => $price,
'quantity' => $quantity,
'count' => $count,
'paper_products' => count($this->paperProductRepository->findAllDeliveredAndNotUsed()),
'form' => $form->createView()
]);
}
#[Route('/{id}/detail', name: 'detail', methods: ['GET'])]
public function detail(Paper $paper): Response
{
$pagination = $this->paperProductRepository->findDetail($paper);
return $this->renderForm('manager/stock/detail.html.twig', [
'pagination' => $pagination,
'paper' => $paper,
'paper_products' => $pagination->getItems()
]);
}
#[Route('/export', name: 'export')]
public function export(ExportManager $exportManager, ParameterBagInterface $params, PaperRepository $paperRepository, PaperProductRepository $paperProductRepository, Session $session): Response
{
$path = $params->get('kernel.project_dir') . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'stock';
$file = $exportManager->exportStock($path, $paperRepository, $paperProductRepository, $session);
$response = $this->file($file);
$response->deleteFileAfterSend();
return $response;
}
}