<?php
namespace App\Controller;
use App\Entity\Affectation;
use App\Entity\Payment;
use App\Entity\Piece;
use App\Entity\PieceLine;
use App\Form\PieceLineSearchType;
use App\Form\PieceSearchType;
use App\Form\PieceType;
use App\Repository\ConfigurationRepository;
use App\Repository\PieceLineRepository;
use App\Repository\PieceRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Spipu\Html2Pdf\Html2Pdf;
/**
* @Route("/piece")
*/
class PieceController extends AbstractController
{
/**
* @Route("/", name="piece_index", methods={"GET|POST"})
* @Security("is_granted('ROLE_USER')")
*/
public function index(PieceRepository $pieceRepository, Request $request, PaginatorInterface $paginator): Response
{
$form = $this->createForm(PieceSearchType::class,null,['responsable'=>$this->getUser()]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$url = $this->buildSearchUrl($request->request->all());
if (!empty($url)) {
return $this->redirectToRoute('piece_index', $url);
}
}
if($request->query->get('payment') == 'create'){
$pieces = $pieceRepository->findAll();
$type = Payment::TYPE_OLD;
$date = new \DateTime('now');
$em = $this->getDoctrine()->getManager();
foreach ($pieces as $piece){
if($piece->getPayer() and $piece->getAmountTTC() > 0){
if(!is_null($piece->getModePaiement())){
$type = $piece->getModePaiement();
}
$payment = New Payment();
$payment->setUser($piece->getUser());
$payment->setDate($piece->getDate());
$payment->setClient($piece->getClient());
$payment->setAmount($piece->getAmountTTC());
$payment->setType($type);
$affectation = new Affectation();
$affectation->setPayment($payment);
$affectation->setAmount($piece->getAmountTTC());
$affectation->setPiece($piece);
$payment->addAffectation($affectation);
$em->persist($payment);
$em->flush();
}
$piece->setUpdatedAt($date);
$em->flush();
}
}
$pieces = $paginator->paginate(
$pieceRepository->MyFindAll($request->query->all(),$this->getUser()), /* query NOT result */
$request->query->getInt('page', 1)/* page number */, 20/* limit per page */
);
$totaux = $pieceRepository->getSumOperations($request->query->all(),$this->getUser());
return $this->render('piece/index.html.twig', [
'pieces' => $pieces,
'totaux' => $totaux,
'form' => $form->createView(),
]);
}
/**
* @Route("/ligne-vente", name="ligne_vente_index", methods={"GET|POST"})
* @Security("is_granted('ROLE_SUPER_ADMIN')")
*/
public function ligneVenteIndex(PieceLineRepository $pieceLineRepository, Request $request, PaginatorInterface $paginator): Response
{
$form = $this->createForm(PieceLineSearchType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$url = $this->buildSearchUrlPieceLine($request->request->all());
if (!empty($url)) {
return $this->redirectToRoute('ligne_vente_index', $url);
}
}
$pieceLines = $paginator->paginate(
$pieceLineRepository->MyFindAll($request->query->all()), /* query NOT result */
$request->query->getInt('page', 1)/* page number */, 20/* limit per page */
);
$totaux = $pieceLineRepository->getSumOperations($request->query->all());
return $this->render('piece/ligne_vente_index.html.twig', [
'pieceLines' => $pieceLines,
'totaux' => $totaux,
'form' => $form->createView(),
]);
}
/**
* @Route("/new", name="piece_new", methods={"GET","POST"})
* @Security("is_granted('ROLE_USER')")
*/
public function new(Request $request, ConfigurationRepository $configurationRepository, PieceRepository $pieceRepository): Response
{
$piece = new Piece();
$pieceLine = new PieceLine();
if (count($piece->getPieceLines()) === 0) {
$piece->addPieceLine($pieceLine);
}
$config = $configurationRepository->find(1);
$array_tva = $config->getTvaValues();
$products = new ArrayCollection();
$form = $this->createForm(PieceType::class, $piece, ['config' => $array_tva,'responsable'=>$this->getUser(),'type'=>'add','products'=>$products]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$piece->setUser($this->getUser());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($piece);
$entityManager->flush();
foreach ($piece->getPieceLines() as $piece_line) {
$lot = $piece_line->getLot();
$lot->setStock($lot->getStock() - $piece_line->getQty());
$entityManager->flush();
}
$data = $request->request->all();
if($data['piece']['modePaiement'] != ''){
$payment = new Payment();
$payment->setClient($piece->getClient());
$payment->setDate($piece->getDate());
$payment->setAmount($piece->getAmountTTC());
$payment->setType($data['piece']['modePaiement']);
$affectation = new Affectation();
$affectation->setPiece($piece);
$affectation->setAmount($piece->getAmountTTC());
$affectation->setPayment($payment);
$payment->addAffectation($affectation);
$entityManager->persist($payment);
$entityManager->flush();
}
return $this->redirectToRoute('piece_show',['id'=>$piece->getId()]);
}
return $this->render('piece/new.html.twig', [
'piece' => $piece,
'numero' => $pieceRepository->getMaxNumeroPiece()['last_numero'] + 1,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}/delete-confirmation", name="piece_delete_confirmation", methods={"GET"})
* @Security("is_granted('ROLE_SUPER_ADMIN')")
*/
public function deleteConfirmation(Piece $piece): Response
{
return $this->render('piece/_delete_confirmation.html.twig', [
'piece' => $piece,
]);
}
/**
* @Route("/delete-validation", name="piece_delete_validation", methods={"GET"})
* @Security("is_granted('ROLE_SUPER_ADMIN')")
*/
public function deleteValidation(): Response
{
return $this->render('piece/_delete_validation.html.twig', [
]);
}
/**
* @Route("/{id}/impression", name="piece_print", methods={"GET"})
* @Security("is_granted('ROLE_USER')")
*/
public function imprimerPieceAction(Piece $piece, ConfigurationRepository $configurationRepository)
{
if (
($this->getUser()->isResponsable() && $piece->getClient()->getUser() != $this->getUser()) or
($this->getUser()->isResponsable() && !$this->getUser()->canViewInvoice($piece))
) {
$this->addFlash("danger", "Accès interdit");
return $this->redirectToRoute('accueil_back');
}
$configuration = $configurationRepository->find(1);
if ($configuration->getTvaActive()) {
$nb_article_page = 15;
} else {
$nb_article_page = 16;
}
$nb_ligne_piece = count($piece->getPieceLines());
$nb_total_page = ceil($nb_ligne_piece / $nb_article_page);
$array_ligne_piece = [];
$compt = 1;
$nb_page = 1;
foreach ($piece->getPieceLines() as $ligne):
$array_ligne_piece[$nb_page][] = $ligne;
if ($compt % $nb_article_page == 0) {
$nb_page++;
}
$compt++;
endforeach;
if ($configuration->getTvaActive()) {
$html = $this->renderView('piece/imprimer_piece_tva.html.twig', [
'piece' => $piece,
'array_ligne_piece' => $array_ligne_piece,
'nb_total_page' => $nb_total_page,
'num' => $piece->getNumero(),
'config' => $configuration,
'server' => 'https://' . $_SERVER['HTTP_HOST'],
]);
} else {
$html = $this->renderView('piece/imprimer_piece.html.twig', [
'piece' => $piece,
'array_ligne_piece' => $array_ligne_piece,
'nb_total_page' => $nb_total_page,
'num' => $piece->getNumero(),
'config' => $configuration,
'server' => 'https://' . $_SERVER['HTTP_HOST'],
]);
}
$html2pdf = new Html2Pdf('P', 'A4', 'fr');
$html2pdf->writeHTML($html);
$html2pdf->Output();
}
/**
* @Route("/{id}", name="piece_show", methods={"GET"})
* @Security("is_granted('ROLE_USER')")
*/
public function show(Piece $piece, ConfigurationRepository $configurationRepository): Response
{
if (
($this->getUser()->isResponsable() && $piece->getClient()->getUser() != $this->getUser()) or
($this->getUser()->isResponsable() && !$this->getUser()->canViewInvoice($piece))
) {
$this->addFlash("danger", "Accès interdit");
return $this->redirectToRoute('accueil_back');
}
return $this->render('piece/show.html.twig', [
'piece' => $piece,
'config' => $configurationRepository->find(1),
'server' => 'https://' . $_SERVER['HTTP_HOST'],
'modes' => array_flip(Payment::paymentTypeListWithoutGift())
]);
}
/**
* @Route("/{id}/edit", name="piece_edit", methods={"GET","POST"})
* @Security("is_granted('ROLE_USER')")
*/
public function edit(Request $request, Piece $piece, PieceRepository $pieceRepository, ConfigurationRepository $configurationRepository): Response
{
if (
($this->getUser()->isResponsable() && $this->getParameter('param_title') == 'IRON-APP') or
($this->getUser()->isResponsable() && $piece->getClient()->getUser() != $this->getUser()) or
($this->getUser()->isResponsable() && !$this->getUser()->canViewInvoice($piece))
) {
$this->addFlash("danger", "Accès interdit");
return $this->redirectToRoute('accueil_back');
}
$actuel_piece = $pieceRepository->find($request->attributes->get('id'));
$array_actuel_ligne_piece = [];
foreach ($actuel_piece->getPieceLines() as $piece_line) {
$array_actuel_ligne_piece[$piece_line->getId()]['product'] = $piece_line->getProduct();
$array_actuel_ligne_piece[$piece_line->getId()]['lot'] = $piece_line->getLot();
$array_actuel_ligne_piece[$piece_line->getId()]['qte'] = $piece_line->getQty();
}
$config = $configurationRepository->find(1);
$array_tva = $config->getTvaValues();
$products = new ArrayCollection();
foreach($piece->getPieceLines() as $line){
if($products->contains($line->getProduct()) === false){
$products->add($line->getProduct());
}
}
$form = $this->createForm(PieceType::class, $piece, ['config' => $array_tva,'responsable'=>$this->getUser(),'type'=>'edit','products'=>$products]);
$form->handleRequest($request);
$array_edit_ligne_piece = [];
$array_new_ligne_piece = [];
if ($form->isSubmitted() && $form->isValid()) {
$piece->setUser($this->getUser());
$entityManager = $this->getDoctrine()->getManager();
$comp = 0;
foreach ($piece->getPieceLines() as $piece_line) {
if ($piece_line->getId()) {
$array_edit_ligne_piece[$piece_line->getId()]['product'] = $piece_line->getProduct();
$array_edit_ligne_piece[$piece_line->getId()]['lot'] = $piece_line->getLot();
$array_edit_ligne_piece[$piece_line->getId()]['qte'] = $piece_line->getQty();
} else {
$array_new_ligne_piece[$comp]['product'] = $piece_line->getProduct();
$array_new_ligne_piece[$comp]['lot'] = $piece_line->getLot();
$array_new_ligne_piece[$comp]['qte'] = $piece_line->getQty();
$comp++;
}
}
$entityManager->flush();
$array_delete_ligne_piece=[];
foreach ($array_actuel_ligne_piece as $id_ligne=>$piece_line) {
if(!isset($array_edit_ligne_piece[$id_ligne])){
$array_delete_ligne_piece[$id_ligne]=$array_actuel_ligne_piece[$id_ligne];
}
}
//stock new article
foreach ($array_new_ligne_piece as $piece_line) {
$product = $piece_line['product'];
$lot = $piece_line['lot'];
$lot->setStock($lot->getStock() - $piece_line['qte']);
$entityManager->flush();
}
//stock edit article
foreach ($array_edit_ligne_piece as $id_ligne=>$piece_line) {
$product_act = $array_actuel_ligne_piece[$id_ligne]['product'];
$lot_act = $array_actuel_ligne_piece[$id_ligne]['lot'];
$lot_act->setStock($lot_act->getStock() + $array_actuel_ligne_piece[$id_ligne]['qte']);
$entityManager->flush();
$product_edit = $piece_line['product'];
$lot_edit = $piece_line['lot'];
$lot_edit->setStock($lot_edit->getStock() - $piece_line['qte']);
$entityManager->flush();
}
//stock delete article
foreach ($array_delete_ligne_piece as $piece_line) {
$product = $piece_line['product'];
$lot = $piece_line['lot'];
$lot->setStock($lot->getStock() + $piece_line['qte']);
$entityManager->flush();
}
return $this->redirectToRoute('piece_show', ['id' => $piece->getId()]);
}
return $this->render('piece/edit.html.twig', [
'piece' => $piece,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}/delete", name="piece_delete", methods={"DELETE"})
* @Security("is_granted('ROLE_SUPER_ADMIN')")
*/
public function delete(Request $request, Piece $piece): Response
{
if ($this->isCsrfTokenValid('delete' . $piece->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
foreach ($piece->getPieceLines() as $piece_line) {
$product = $piece_line->getProduct();
$product->setStock($product->getStock() + $piece_line->getQty());
$entityManager->flush();
}
$entityManager->remove($piece);
$entityManager->flush();
}
return $this->redirectToRoute('piece_index');
}
private function buildSearchUrlPieceLine($data)
{$url = [];
foreach ($data as $k => $v) {
if (isset($data['piece_line_search']['code']) && !empty($data['piece_line_search']['code'])) {
$url['code'] = $data['piece_line_search']['code'];
}
if (isset($data['piece_line_search']['product']) && !empty($data['piece_line_search']['product'])) {
$url['product'] = $data['piece_line_search']['product'];
}
if (isset($data['piece_line_search']['dateDu']) && !empty($data['piece_line_search']['dateDu'])) {
$url['dateDu'] = $data['piece_line_search']['dateDu'];
}
if (isset($data['piece_line_search']['dateAu']) && !empty($data['piece_line_search']['dateAu'])) {
$url['dateAu'] = $data['piece_line_search']['dateAu'];
}
if (isset($data['piece_line_search']['priceHTMin']) && !empty($data['piece_line_search']['priceHTMin'])) {
$url['priceHTMin'] = $data['piece_line_search']['priceHTMin'];
}
if (isset($data['piece_line_search']['priceHTMax']) && !empty($data['piece_line_search']['priceHTMax'])) {
$url['priceHTMax'] = $data['piece_line_search']['priceHTMax'];
}
if (isset($data['piece_line_search']['totalHTMin']) && !empty($data['piece_line_search']['totalHTMin'])) {
$url['totalHTMin'] = $data['piece_line_search']['totalHTMin'];
}
if (isset($data['piece_line_search']['totalHTMax']) && !empty($data['piece_line_search']['totalHTMax'])) {
$url['totalHTMax'] = $data['piece_line_search']['totalHTMax'];
}
if (isset($data['piece_line_search']['rabaisMin']) && !empty($data['piece_line_search']['rabaisMin'])) {
$url['rabaisMin'] = $data['piece_line_search']['rabaisMin'];
}
if (isset($data['piece_line_search']['rabaisMax']) && !empty($data['piece_line_search']['rabaisMax'])) {
$url['rabaisMax'] = $data['piece_line_search']['rabaisMax'];
}
if (isset($data['piece_line_search']['amountRabaisMin']) && !empty($data['piece_line_search']['amountRabaisMin'])) {
$url['amountRabaisMin'] = $data['piece_line_search']['amountRabaisMin'];
}
if (isset($data['piece_line_search']['amountRabaisMax']) && !empty($data['piece_line_search']['amountRabaisMax'])) {
$url['amountRabaisMax'] = $data['piece_line_search']['amountRabaisMax'];
}
if (isset($data['piece_line_search']['priceBuyHTMin']) && !empty($data['piece_line_search']['priceBuyHTMin'])) {
$url['priceBuyHTMin'] = $data['piece_line_search']['priceBuyHTMin'];
}
if (isset($data['piece_line_search']['priceBuyHTMax']) && !empty($data['piece_line_search']['priceBuyHTMax'])) {
$url['priceBuyHTMax'] = $data['piece_line_search']['priceBuyHTMax'];
}
if (isset($data['piece_line_search']['margeMin']) && !empty($data['piece_line_search']['margeMin'])) {
$url['margeMin'] = $data['piece_line_search']['margeMin'];
}
if (isset($data['piece_line_search']['margeMax']) && !empty($data['piece_line_search']['margeMax'])) {
$url['margeMax'] = $data['piece_line_search']['margeMax'];
}
}
return $url;
}
private function buildSearchUrl($data)
{
$url = [];
foreach ($data as $k => $v) {
if (isset($data['piece_search']['user']) && !empty($data['piece_search']['user'])) {
$url['user'] = $data['piece_search']['user'];
}
if (isset($data['piece_search']['client']) && !empty($data['piece_search']['client'])) {
$url['client'] = $data['piece_search']['client'];
}
if (isset($data['piece_search']['code']) && !empty($data['piece_search']['code'])) {
$url['code'] = $data['piece_search']['code'];
}
if (isset($data['piece_search']['amountHTMin']) && !empty($data['piece_search']['amountHTMin'])) {
$url['amountHTMin'] = $data['piece_search']['amountHTMin'];
}
if (isset($data['piece_search']['amountHTMax']) && !empty($data['piece_search']['amountHTMax'])) {
$url['amountHTMax'] = $data['piece_search']['amountHTMax'];
}
if (isset($data['piece_search']['amountTVAMin']) && !empty($data['piece_search']['amountTVAMin'])) {
$url['amountTVAMin'] = $data['piece_search']['amountTVAMin'];
}
if (isset($data['piece_search']['amountTVAMax']) && !empty($data['piece_search']['amountTVAMax'])) {
$url['amountTVAMax'] = $data['piece_search']['amountTVAMax'];
}
if (isset($data['piece_search']['amountDiscountMin']) && !empty($data['piece_search']['amountDiscountMin'])) {
$url['amountDiscountMin'] = $data['piece_search']['amountDiscountMin'];
}
if (isset($data['piece_search']['amountDiscountMax']) && !empty($data['piece_search']['amountDiscountMax'])) {
$url['amountDiscountMax'] = $data['piece_search']['amountDiscountMax'];
}
if (isset($data['piece_search']['discountMin']) && !empty($data['piece_search']['discountMin'])) {
$url['discountMin'] = $data['piece_search']['discountMin'];
}
if (isset($data['piece_search']['discountMax']) && !empty($data['piece_search']['discountMax'])) {
$url['discountMax'] = $data['piece_search']['discountMax'];
}
if (isset($data['piece_search']['amountTTCMin']) && !empty($data['piece_search']['amountTTCMin'])) {
$url['amountTTCMin'] = $data['piece_search']['amountTTCMin'];
}
if (isset($data['piece_search']['amountTTCMax']) && !empty($data['piece_search']['amountTTCMax'])) {
$url['amountTTCMax'] = $data['piece_search']['amountTTCMax'];
}
if (isset($data['piece_search']['dateDu']) && !empty($data['piece_search']['dateDu'])) {
$url['dateDu'] = $data['piece_search']['dateDu'];
}
if (isset($data['piece_search']['dateAu']) && !empty($data['piece_search']['dateAu'])) {
$url['dateAu'] = $data['piece_search']['dateAu'];
}
if (isset($data['piece_search']['soldeMin']) && $data['piece_search']['soldeMin'] != '') {
$url['soldeMin'] = $data['piece_search']['soldeMin'];
}
if (isset($data['piece_search']['soldeMax']) && $data['piece_search']['soldeMax'] != '') {
$url['soldeMax'] = $data['piece_search']['soldeMax'];
}
/*if (isset($data['piece_search']['payer']) && !empty($data['piece_search']['payer'])) {
$url['payer'] = $data['piece_search']['payer'];
}
if (isset($data['piece_search']['modePaiement']) && !empty($data['piece_search']['modePaiement'])) {
$url['modePaiement'] = $data['piece_search']['modePaiement'];
}*/
}
return $url;
}
}