<?php
namespace App\Controller;
use RuntimeException;
use App\Entity\PUnite;
use App\Entity\PStatut;
use App\Entity\UArticle;
use App\Entity\PArticleNiveau;
use Doctrine\Persistence\ManagerRegistry;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
#[Route('/article')]
class ArticleController extends AbstractController
{
private $em;
public function __construct(ManagerRegistry $doctrine)
{
$this->em = $doctrine->getManager();
}
#[Route('/', name: 'article_index')]
public function index(): Response
{
$niveaux = $this->em->getRepository(PArticleNiveau::class)->findBy(['parent' => null]);
$unites = $this->em->getRepository(PUnite::class)->findAll();
return $this->render('article/index.html.twig' , [
'niveaux' => $niveaux,
'unites' => $unites
]);
}
#[Route('/list', name: 'article_list')]
public function list(Request $request): Response
{
$params = $request->query;
// dd($params);
$where = $totalRows = $sqlRequest = "";
$filtre = "where article.active = 1 ";
// dd($params->get('columns')[0]);
if (!empty($params->get('columns')[0]['search']['value'])) {
$niveaux = $this->em->getRepository(PArticleNiveau::class)->find($params->get('columns')[0]['search']['value']);
$arrayOfIds = ApiController::pluck($niveaux);
$filtre .= " and niv.id in (" . implode(",",$arrayOfIds) . ") ";
}
if (!empty($params->get('columns')[1]['search']['value'])) {
if($params->get('columns')[1]['search']['value'] == "nv") {
$filtre .= " and pstatut.id = 1 ";
} else if($params->get('columns')[1]['search']['value'] == "ni") {
$filtre .= " and pstatut.id = 2 ";
}
}
$columns = array(
array( 'db' => 'article.titre','dt' => 0),
array( 'db' => 'UPPER(unite.designation)','dt' => 1),
array( 'db' => 'LOWER(niv.designation)','dt' => 2),
array( 'db' => 'UPPER(niv.id)','dt' => 3),
array( 'db' => 'UPPER(niv.niveau)','dt' => 5),
array( 'db' => 'article.id','dt' => 6),
array( 'db' => 'pstatut.designation','dt' => 7),
array( 'db' => 'article.active','dt' => 8),
);
$sql = "SELECT " . implode(", ", DatatablesController::Pluck($columns, 'db')) . "
FROM uarticle article
left join punite unite on unite.id = article.unite_id
inner join pstatut on pstatut.id = article.statut_id
inner join particle_niveau niv on niv.id = article.niveau_id
$filtre "
;
$totalRows .= $sql;
$sqlRequest .= $sql;
$stmt = $this->em->getConnection()->prepare($sql);
$newstmt = $stmt->executeQuery();
$totalRecords = count($newstmt->fetchAll());
$my_columns = DatatablesController::Pluck($columns, 'db');
// search
$where = DatatablesController::Search($request, $columns);
if (isset($where) && $where != '') {
$sqlRequest .= $where;
}
$sqlRequest .= DatatablesController::Order($request, $columns);
$stmt = $this->em->getConnection()->prepare($sqlRequest);
$resultSet = $stmt->executeQuery();
$result = $resultSet->fetchAll();
$data = array();
$i = 1;
foreach ($result as $key => $row) {
$nestedData = array();
$currentNiveau = $row['UPPER(niv.id)'];
$arrayOfNiveaux = [];
for ($i=$row['UPPER(niv.niveau)']; $i > 0; $i--) {
$niveau = $this->em->getRepository(PArticleNiveau::class)->find($currentNiveau);
array_push($arrayOfNiveaux, [$niveau->getDesignation(), $niveau->getStatut()->getDesignation(), $niveau->getId()]);
if($niveau->getParent()) {
$currentNiveau = $niveau->getParent()->getId();
} else {
break;
}
}
$arrayOfNiveaux = array_reverse($arrayOfNiveaux);
$diffrence = 6 - count($arrayOfNiveaux);
if($diffrence > 0){
for($i = 1; $i <= $diffrence; $i++){
array_push($arrayOfNiveaux, ["-", "-", "-"]);
}
}
$cd = $row['id'];
$check = "";
if($row['designation'] == "Intégrer" or $row['designation'] == "Updated"){
$check = "checked disabled='disabled'";
}
$nestedData[] = "<input type ='checkbox' class='check_article' id ='$cd' $check>";
$nestedData[] = $cd;
$currentClass = "";
foreach ($arrayOfNiveaux as $niveau) {
if($niveau[1] == "En Cours") {
// $nestedData[] = "<span class='danger' id='$niveau[2]'>".$niveau[0]."</span>";
if(strlen($niveau[0]) >= 20)
{
$nestedData[] = "<span class='danger' id='$niveau[2]'>".mb_substr($niveau[0],0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$niveau[0].'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='danger' id='$niveau[2]'>".$niveau[0]."</span>";
}
$currentClass = "danger";
} else if($niveau[1] == "Valider") {
if(strlen($niveau[0]) >= 20)
{
$nestedData[] = "<span class='success' >".mb_substr($niveau[0],0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$niveau[0].'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='success' >".$niveau[0]."</span>";
}
$currentClass = "success";
}else if($niveau[1] == "Intégrer") {
// $nestedData[] = "<span class='' >".$niveau[0]."</span>";
if(strlen($niveau[0]) >= 20)
{
$nestedData[] = "<span class='' >".mb_substr($niveau[0],0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$niveau[0].'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='' >".$niveau[0]."</span>";
}
$currentClass = "";
}else {
if(strlen($niveau[0]) >= 20)
{
$nestedData[] = "<span class='$currentClass' >".mb_substr($niveau[0],0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$niveau[0].'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='$currentClass' >".$niveau[0]."</span>";
}
// $nestedData[] = "<span class='$currentClass' >".$niveau[0]."</span>";
}
}
foreach (array_values($row) as $key => $value) {
if($key < 2) {
if($row["designation"] == "En Cours"){
// $nestedData[] = "<span class='dangerArticle' id='$cd'>".$value."</span>";
if(strlen($value) >= 20)
{
$nestedData[] = "<span class='dangerArticle' id='$cd' >".mb_substr($value,0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$value.'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='dangerArticle' id='$cd'>".$value."</span>";
}
} else if($row["designation"] == "Valider") {
// $nestedData[] = "<span class='successArticle' id='$cd'>".$value."</span>";
if(strlen($value) >= 20)
{
$nestedData[] = "<span class='successArticle' id='$cd' >".mb_substr($value,0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$value.'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='successArticle' id='$cd'>".$value."</span>";
}
}
else if($row["designation"] == "Updated") {
// $nestedData[] = "<span class='successArticle' id='$cd'>".$value."</span>";
if(strlen($value) >= 20)
{
$nestedData[] = "<span class='UpdatedArticle' id='$cd' >".mb_substr($value,0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$value.'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span class='UpdatedArticle' id='$cd'>".$value."</span>";
}
} else {
if(strlen($value) >= 20)
{
$nestedData[] = "<span id='$cd' >".mb_substr($value,0,20).'<span style="position: absolute; font-weight:bold; cursor:pointer" class="hint--top" aria-label="'.$value.'"> '. ' ...' .'</span></span>';
}
else {
$nestedData[] = "<span id='$cd'>".$value."</span>";
}
// $nestedData[] = "<span id='$cd'>".$value."</span>";
}
}
}
$nestedData [] = $row['active'] == 1 ? 'oui' : 'non';
$html = '<div class="btn-group dropdown-btn-action ">
<button type="button" class="btn btn-secondary-outline btn-sm dropdown-toggle btn-dropdown" id="dropdown_table" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-ellipsis-h"></i>
</button>
<div class="dropdown-menu dropdown-menu-right">
<button type="button" class="dropdown-item getArticleDetails" id="'.$cd.'">
Details
</button> ';
if ($this->isGranted('ROLE_CREATEUR')){
$html .= '<button type="button" class="dropdown-item getArticleUpdate" id="'.$cd.'">
Modifier
</button>';
}
if ($row['active'] == 1){
$html .= '<button type="button" class="dropdown-item switchenable" id="'.$cd.'">
Désactiver
</button>';
} else {
$html .= '<button type="button" class="dropdown-item switchenable" id="'.$cd.'">
Active
</button>';
}
$html .= '</div>
</div>';
$nestedData [] = $html;
// $nestedData["DT_RowId"] = $cd;
// $nestedData["DT_RowClass"] = $cd;
$data[] = $nestedData;
$i++;
}
$json_data = array(
"draw" => intval($params->get('draw')),
"recordsTotal" => intval($totalRecords),
"recordsFiltered" => intval($totalRecords),
"data" => $data
);
// die;
// $json_data = mb_convert_encoding($json_data, "UTF-8", "auto");
// dd($json_data);
$json = json_encode($json_data);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(json_last_error_msg());
}
// dd("amine");
return new Response(json_encode($json_data));
}
#[Route('/niveau/details/{niveau}', name: 'article_niveau_details')]
public function niveauDetails(PArticleNiveau $niveau): Response
{
return new JsonResponse([
'niveau' => $niveau->getNiveau(),
'designation' => $niveau->getDesignation(),
'cca1' => $niveau->getCCA1(),
'cca2' => $niveau->getCCA2(),
'cca3' => $niveau->getCCA3(),
'cca4' => $niveau->getCCA4(),
]);
}
#[Route('/niveau/update/{niveau}', name: 'article_niveau_update')]
public function niveauUpdate(PArticleNiveau $niveau, Request $request): Response
{
$niveau->setDesignation($request->get("designation"));
$niveau->setStatut($this->em->getRepository(PStatut::class)->find(2));
if($niveau->getParent()){
$parentNiveau = $niveau->getParent();
if($request->get('cca1') == "" && $request->get('cca2') == "" && $request->get('cca3') == "" && $request->get('cca4') == "") {
$niveau->setCCA1($parentNiveau->getCCA1());
$niveau->setCCA2($parentNiveau->getCCA2());
$niveau->setCCA3($parentNiveau->getCCA3());
$niveau->setCCA4($parentNiveau->getCCA4());
} else {
$niveau->setCCA1($request->get('cca1') != "" ? $request->get('cca1') : null);
$niveau->setCCA2($request->get('cca2') != "" ? $request->get('cca2') : null);
$niveau->setCCA3($request->get('cca3') != "" ? $request->get('cca3') : null);
$niveau->setCCA4($request->get('cca4') != "" ? $request->get('cca4') : null);
if($niveau->getNiveaux()) {
$niveauChilds = $niveau->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
}
}
}
}
}
}
}
}
}
}
}
} else {
$niveau->setCCA1($request->get('cca1') != "" ? $request->get('cca1') : null);
$niveau->setCCA2($request->get('cca2') != "" ? $request->get('cca2') : null);
$niveau->setCCA3($request->get('cca3') != "" ? $request->get('cca3') : null);
$niveau->setCCA4($request->get('cca4') != "" ? $request->get('cca4') : null);
if($niveau->getNiveaux()) {
$niveauChilds = $niveau->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
if($niveauChild->getNiveaux()) {
$niveauChilds = $niveauChild->getNiveaux();
foreach ($niveauChilds as $niveauChild) {
$niveauChild->setCCA1($niveau->getCCA1());
$niveauChild->setCCA2($niveau->getCCA2());
$niveauChild->setCCA3($niveau->getCCA3());
$niveauChild->setCCA4($niveau->getCCA4());
}
}
}
}
}
}
}
}
}
}
}
$this->em->flush();
return new JsonResponse("Bien Enregistre", 200);
}
#[Route('/article/details/{article}', name: 'article_article_details')]
public function articleDetails(UArticle $article): Response
{
$html = $this->render("article/pages/form_valide.html.twig", [
'article' => $article
])->getContent();
return new JsonResponse([
"html" => $html,
'title' => $article->getTitre()
]);
}
#[Route('/article/siwtchactive/{article}', name: 'article_article_siwtchactive')]
public function siwtchactive(UArticle $article, HttpClientInterface $client): Response
{
$article->setActive(!$article->getActive());
$response = $client->request('POST', $this->getParameter("api")."/api/article/siwtchactive/".$article->getId(), [
'body' => [
'active' => $article->getActive(),
],
]);
if($response->getContent() == "good") {
$this->em->flush();
return new JsonResponse('Bien enregister!');
}
return new JsonResponse('Something went wrong!', 500);
}
#[Route('/article/valide', name: 'article_article_valide')]
public function articleValidate(Request $request): Response
{
$idArticles = json_decode($request->get('articles'));
// dd($idArticles);
foreach($idArticles as $idArticle) {
$article = $this->em->getRepository(UArticle::class)->find($idArticle);
if($article->getStatut()->getDesignation() == "En Cours") {
$article->setStatut($this->em->getRepository(PStatut::class)->find(2));
$currentNiveau = $article->getNiveau()->getId();
for ($j= $article->getNiveau()->getNiveau(); $j > 0; $j--) {
$niveau = $this->em->getRepository(PArticleNiveau::class)->find($currentNiveau);
if($niveau->getStatut()->getDesignation() == "En Cours"){
$niveau->setStatut($this->em->getRepository(PStatut::class)->find(2));
}
if($niveau->getParent()) {
$currentNiveau = $niveau->getParent()->getId();
} else {
break;
}
}
}
}
$this->em->flush();
return new JsonResponse("Bien Enregistre", 200);
}
#[Route('/niveau/add', name: 'article_niveau_add')]
public function niveauAdd(Request $request): Response
{
// dd($request);
$niveau = new PArticleNiveau();
if($request->get("parent") != "null"){
$parentNiveau = $this->em->getRepository(PArticleNiveau::class)->find($request->get("parent"));
$niveau->setParent($parentNiveau);
if($request->get('cca1') == "" && $request->get('cca2') == "" && $request->get('cca3') == "" && $request->get('cca4') == "") {
$niveau->setCCA1($parentNiveau->getCCA1());
$niveau->setCCA2($parentNiveau->getCCA2());
$niveau->setCCA3($parentNiveau->getCCA3());
$niveau->setCCA4($parentNiveau->getCCA4());
} else {
$niveau->setCCA1($request->get('cca1') != "" ? $request->get('cca1') : null);
$niveau->setCCA2($request->get('cca2') != "" ? $request->get('cca2') : null);
$niveau->setCCA3($request->get('cca3') != "" ? $request->get('cca3') : null);
$niveau->setCCA4($request->get('cca4') != "" ? $request->get('cca4') : null);
}
} else {
$niveau->setCCA1($request->get('cca1') != "" ? $request->get('cca1') : null);
$niveau->setCCA2($request->get('cca2') != "" ? $request->get('cca2') : null);
$niveau->setCCA3($request->get('cca3') != "" ? $request->get('cca3') : null);
$niveau->setCCA4($request->get('cca4') != "" ? $request->get('cca4') : null);
}
$niveau->setNiveau($request->get('niveau'));
$niveau->setDesignation($request->get('designation'));
$niveau->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$this->em->persist($niveau);
$this->em->flush();
return new JsonResponse(['id' => $niveau->getId(), "designation" => $niveau->getDesignation()], 200);
}
#[Route('/article/add', name: 'article_article_add')]
public function articleAdd(Request $request): Response
{
// dd($request);
$article = $this->em->getRepository(UArticle::class)->findOneBy(['titre' => trim($request->get("titre"))]);
// dd($article, trim($request->get("titre")));
if($article) {
return new JsonResponse("Article déja existe",500);
}
$article = new UArticle();
$article->setTitre(
$request->get("titre") != "" ? $request->get("titre") : null
);
$article->setPoid(
$request->get("poid") != "" ? $request->get("poid") : null
);
$article->setUnite(
$request->get("unite") != "" ? $this->em->getRepository(PUnite::class)->find($request->get("unite")) : null
);
$article->setLongeur(
$request->get("longeur") != "" ? $request->get("longeur") : null
);
$article->setLargeur(
$request->get("largeur") != "" ? $request->get("largeur") : null
);
$article->setHauteur(
$request->get("hauteur") != "" ? $request->get("hauteur") : null
);
$article->setSurface(
$request->get("surface") != "" ? $request->get("surface") : null
);
$article->setVolume(
$request->get("volume") != "" ? $request->get("volume") : null
);
$article->setPrixVente(
$request->get("prix_vente") != "" ? $request->get("prix_vente") : null
);
$article->setPrixVenteMin(
$request->get("prix_vente_min") != "" ? $request->get("prix_vente_min") : null
);
$article->setPrixVenteMax(
$request->get("prix_vente_max") != "" ? $request->get("prix_vente_max") : null
);
if($request->get("prix_vente_max") >= 0 && $request->get("prix_vente_min") >= 0) {
if(($request->get("prix_vente_max") + $request->get("prix_vente_min")) == 0) {
$article->setPrixVenteMoyenne(0);
} else {
$article->setPrixVenteMoyenne(($request->get("prix_vente_max") + $request->get("prix_vente_min")) / 2);
}
}
$article->setPrixAchat(
$request->get("prix_achat") != "" ? $request->get("prix_achat") : null
);
$article->setPrixAchatMin(
$request->get("prix_achat_min") != "" ? $request->get("prix_achat_min") : null
);
$article->setPrixAchatMax(
$request->get("prix_achat_max") != "" ? $request->get("prix_achat_max") : null
);
if($request->get("prix_achat_max") >= 0 && $request->get("prix_achat_min") >= 0) {
if(($request->get("prix_achat_max") + $request->get("prix_achat_min")) == 0) {
$article->setPrixAchatMoyenne(0);
} else {
$article->setPrixAchatMoyenne(($request->get("prix_achat_max") + $request->get("prix_achat_min")) / 2);
}
}
$article->setAutreInformation(
$request->get("autre_infromation") != "" ? $request->get("autre_infromation") : null
);
$article->setDescriptionDetail(
$request->get("desicription_detail") != "" ? $request->get("desicription_detail") : null
);
$article->setGererEnStock(
$request->get("gerer_en_stock") ? true : false
);
$article->setVerificationStock(
$request->get("verification_stock") ? true : false
);
$article->setCodeBarre(
$request->get("code_barre") != "" ? $request->get("code_barre") : null
);
$article->setTva(
$request->get("tva") != "" ? $request->get("tva") : null
);
$article->setDosage(
$request->get("dosage") != "" ? $request->get("dosage") : null
);
$article->setDci(
$request->get("dci") != "" ? $request->get("dci") : null
);
$article->setRemise(
$request->get("remise") != "" ? $request->get("remise") : null
);
$article->setPrixReference(
$request->get("prix_reference") != "" ? $request->get("prix_reference") : null
);
$article->setTaille(
$request->get("taille") != "" ? $request->get("taille") : null
);
$article->setMatiere(
$request->get("matiere") != "" ? $request->get("matiere") : null
);
$article->setConditionnement(
$request->get("conditionnement") != "" ? $request->get("conditionnement") : null
);
$article->setMarque(
$request->get("marque") != "" ? $request->get("marque") : null
);
$article->setMA(
$request->get("ma") != "" ? $request->get("ma") : null
);
$article->setRefInterne(
$request->get("reference_interne") != "" ? $request->get("reference_interne") : null
);
$article->setSNS(
$request->get("sns") != "" ? $request->get("sns") : null
);
$article->setNiveau(
$this->em->getRepository(PArticleNiveau::class)->find($request->get('niveau'))
);
$article->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$article->setActive(true);
$article->setQteStock($request->get("QTE_STOCK") != "" ? $request->get("QTE_STOCK") : null);
$article->setFonCode($request->get("FON_CODE") != "" ? $request->get("FON_CODE") : null);
$article->setSpecialiteProduit($request->get("SPECIALITE_PRODUIT") != "" ? $request->get("SPECIALITE_PRODUIT") : null);
$article->setSpeCode($request->get("SPE_CODE") != "" ? $request->get("SPE_CODE") : null);
$article->setAncienCode($request->get("ANCIEN_CODE") != "" ? $request->get("ANCIEN_CODE") : null);
$article->setCodeCat($request->get("CODE_CAT") != "" ? $request->get("CODE_CAT") : null);
$article->setClasseProduit($request->get("CLASSE_PRODUIT") != "" ? $request->get("CLASSE_PRODUIT") : null);
$article->setSegmentsProduit($request->get("SEGMENTS_PRODUIT") != "" ? $request->get("SEGMENTS_PRODUIT") : null);
$article->setSousSegmentsProduit($request->get("SOUS-SEGMENT_PRODUIT") != "" ? $request->get("SOUS-SEGMENT_PRODUIT") : null);
$article->setReference($request->get("REFERENCE") != "" ? $request->get("REFERENCE") : null);
$article->setVoieAdministration($request->get("VOIE_ADMINISTRATION") != "" ? $request->get("VOIE_ADMINISTRATION") : null);
$article->setPresentation($request->get("PRESENTATION") != "" ? $request->get("PRESENTATION") : null);
$article->setFabricant($request->get("FABRICANT") != "" ? $request->get("FABRICANT") : null);
$article->setFournisseur($request->get("FOURNISSEUR") != "" ? $request->get("FOURNISSEUR") : null);
$article->setCodeDci($request->get("CODE_DCI") != "" ? $request->get("CODE_DCI") : null);
$article->setEan($request->get("EAN") != "" ? $request->get("EAN") : null);
$article->setAtc($request->get("ATC") != "" ? $request->get("ATC") : null);
$article->setCodeImm($request->get("CODE_IMM") != "" ? $request->get("CODE_IMM") : null);
$article->setEanAccess($request->get("EAN_ACCESS") != "" ? $request->get("EAN_ACCESS") : null);
$article->setPrixVentePh($request->get("PRIX_VENTE_PH") != "" ? $request->get("PRIX_VENTE_PH") : null);
$article->setPrixVentePpv($request->get("PRIX_VENTE_PPV") != "" ? $request->get("PRIX_VENTE_PPV") : null);
$article->setPrixVentePpc($request->get("PRIX_VENTE_PPC") != "" ? $request->get("PRIX_VENTE_PPC") : null);
$article->setCleReference($request->get("CLE_REFERENCE") != "" ? $request->get("CLE_REFERENCE") : null);
$this->em->persist($article);
$this->em->flush();
$article->setCode('ART'. str_pad($article->getId(), 6, '0', STR_PAD_LEFT));
$this->em->flush();
return new JsonResponse("Bien Enregistre",200);
}
#[Route('/exporter', name: 'article_article_exporter')]
public function exporter(Request $request): Response
{
/** @var UArticle[] */
$articles = $this->em->getRepository(UArticle::class)->findAll();
$array = [];
foreach ($articles as $article) {
$arrayOfNiveaux = [];
if($article->getNiveau()) {
$currentNiveau = $article->getNiveau()->getId();
for ($j= $article->getNiveau()->getNiveau(); $j > 0; $j--) {
$niveau = $this->em->getRepository(PArticleNiveau::class)->find($currentNiveau);
array_push($arrayOfNiveaux, [$niveau->getDesignation(), $niveau->getId()]);
if($niveau->getParent()) {
$currentNiveau = $niveau->getParent()->getId();
} else {
break;
}
}
}
// dump($arrayOfNiveaux);
$arrayOfNiveaux = array_reverse($arrayOfNiveaux);
// dd($arrayOfNiveaux);
$diffrence = 6 - count($arrayOfNiveaux);
if($diffrence > 0){
for($k = 1; $k <= $diffrence; $k++){
array_push($arrayOfNiveaux, ["", ""]);
}
}
array_push($array, [
'id' => $article->getId(),
'code' => $article->getCode(),
'unite_id' => $article->getUnite()? $article->getUnite()->getId() : "",
'unite' => $article->getUnite()? $article->getUnite()->getDesignation() : "",
'id_niveau_affecte' =>$article->getNiveau() ? $article->getNiveau()->getId() : "",
'id_dossier' =>$arrayOfNiveaux[0][1],
'dossier' =>$arrayOfNiveaux[0][0],
'id_sous_dossier' =>$arrayOfNiveaux[1][1],
'sous_dossier' =>$arrayOfNiveaux[1][0],
'id_group' =>$arrayOfNiveaux[2][1],
'group' =>$arrayOfNiveaux[2][0],
'id_categorie' =>$arrayOfNiveaux[3][1],
'categorie' =>$arrayOfNiveaux[3][0],
'id_type' =>$arrayOfNiveaux[4][1],
'type' =>$arrayOfNiveaux[4][0],
'id_famille' =>$arrayOfNiveaux[5][1],
'famille' =>$arrayOfNiveaux[5][0],
'titre' =>$article->getTitre(),
'description' =>$article->getDescription(),
'poid' =>$article->getPoid(),
'longeur' =>$article->getLongeur(),
'largeur' =>$article->getLargeur(),
'hauteur' =>$article->getHauteur(),
'surface' =>$article->getSurface(),
'volume' =>$article->getVolume(),
'prixe_vente' =>$article->getPrixVente(),
'prix_vente_min' =>$article->getPrixVenteMin(),
'prix_vente_max' =>$article->getPrixVenteMax(),
'prix_vente_moynne' =>$article->getPrixVenteMoyenne(),
'prix_achat' =>$article->getPrixAchat(),
'prix_achat_min' =>$article->getPrixAchatMin(),
'prix_achat_max' =>$article->getPrixAchatMax(),
'prix_achat_moyenne' =>$article->getPrixAchatMoyenne(),
'autre_information' =>$article->getAutreInformation(),
'description_detail' =>$article->getDescriptionDetail(),
'gerer_en_stock' =>$article->getGererEnStock() ? "oui" : "non",
'verification_stock' =>$article->getVerificationStock() ? "oui" : "non",
'code_barre' =>$article->getCodeBarre(),
'tva' =>$article->getTva(),
'dosage' =>$article->getDosage(),
'dci' =>$article->getDci(),
'remise' =>$article->getRemise(),
'prix_reference' =>$article->getPrixReference(),
'taille' =>$article->getTaille(),
'matiere' =>$article->getMatiere(),
'conditionnement' =>$article->getConditionnement(),
'notation_technique' =>$article->getNotationTechnique(),
'marque' =>$article->getMarque(),
'ma' =>$article->getMA(),
'famille_article' =>$article->getFamille(),
'categorie_article' =>$article->getCategorie(),
'ref_interne' =>$article->getRefInterne(),
'sns' =>$article->getSNS(),
'statut' => $article->getStatut()->getDesignation(),
'active' => $article->getActive() ? 'oui' : 'non'
]);
}
return new JsonResponse($array);
}
// public function exporter(Request $request): Response
// {
// $articles = $this->em->getRepository(UArticle::class)->findAll();
// $spreadsheet = new Spreadsheet();
// $sheet = $spreadsheet->getActiveSheet();
// $sheet->setCellValue('A1', 'id');
// $sheet->setCellValue('B1', 'code');
// $sheet->setCellValue('C1', 'unite_id');
// $sheet->setCellValue('D1', 'unite');
// $sheet->setCellValue('E1', 'id_niveau_affecte');
// $sheet->setCellValue('F1', 'id');
// $sheet->setCellValue('G1', 'Dossier');
// $sheet->setCellValue('H1', 'id');
// $sheet->setCellValue('I1', 'Sous dossier');
// $sheet->setCellValue('J1', 'id');
// $sheet->setCellValue('K1', 'Group');
// $sheet->setCellValue('L1', 'id');
// $sheet->setCellValue('M1', 'Catégorie');
// $sheet->setCellValue('N1', 'id');
// $sheet->setCellValue('O1', 'Type');
// $sheet->setCellValue('P1', 'id');
// $sheet->setCellValue('Q1', 'Famille');
// $sheet->setCellValue('R1', 'Titre');
// $sheet->setCellValue('S1', 'Description');
// $sheet->setCellValue('T1', 'Poid');
// $sheet->setCellValue('U1', 'Longeur');
// $sheet->setCellValue('V1', 'Largeur');
// $sheet->setCellValue('W1', 'Hauteur');
// $sheet->setCellValue('X1', 'Surface');
// $sheet->setCellValue('Y1', 'Volume');
// $sheet->setCellValue('Z1', 'Prix vente');
// $sheet->setCellValue('AA1', 'Prix vente min');
// $sheet->setCellValue('AB1', 'Prix vente max');
// $sheet->setCellValue('AC1', 'Prix vente moyenne');
// $sheet->setCellValue('AD1', 'Prix Achat');
// $sheet->setCellValue('AE1', 'Prix Achat min');
// $sheet->setCellValue('AF1', 'Prix Achat max');
// $sheet->setCellValue('AG1', 'Prix Achat moyenne');
// $sheet->setCellValue('AH1', 'Autre information');
// $sheet->setCellValue('AI1', 'Description detail');
// $sheet->setCellValue('AJ1', 'Gerer en stock');
// $sheet->setCellValue('AK1', 'Verification stcok');
// $sheet->setCellValue('AL1', 'Code Barre');
// $sheet->setCellValue('AM1', 'TVA');
// $sheet->setCellValue('AN1', 'Dosage');
// $sheet->setCellValue('AO1', 'DCI');
// $sheet->setCellValue('AP1', 'Remise');
// $sheet->setCellValue('AQ1', 'Prix reference');
// $sheet->setCellValue('AR1', 'Taille');
// $sheet->setCellValue('AS1', 'Matiere');
// $sheet->setCellValue('AT1', 'Conditionnement');
// $sheet->setCellValue('AU1', 'Notation technique');
// $sheet->setCellValue('AV1', 'Marque');
// $sheet->setCellValue('AW1', 'M/A');
// $sheet->setCellValue('AX1', 'Reference Interne');
// $sheet->setCellValue('AY1', 'S/NS');
// $sheet->setCellValue('AZ1', 'Statut');
// $i = 2;
// foreach ($articles as $article) {
// $sheet->setCellValue('A'.$i, $article->getId());
// $sheet->setCellValue('B'.$i, $article->getCode());
// $sheet->setCellValue('C'.$i, $article->getUnite()? $article->getUnite()->getId() : "");
// $sheet->setCellValue('D'.$i, $article->getUnite() ? $article->getUnite()->getDesignation() : "");
// $sheet->setCellValue('E'.$i, $article->getNiveau() ? $article->getNiveau()->getId() : "");
// $arrayOfNiveaux = [];
// if($article->getNiveau()) {
// $currentNiveau = $article->getNiveau()->getId();
// for ($j= $article->getNiveau()->getNiveau(); $j > 0; $j--) {
// $niveau = $this->em->getRepository(PArticleNiveau::class)->find($currentNiveau);
// array_push($arrayOfNiveaux, [$niveau->getDesignation(), $niveau->getId()]);
// if($niveau->getParent()) {
// $currentNiveau = $niveau->getParent()->getId();
// } else {
// break;
// }
// }
// }
// // dump($arrayOfNiveaux);
// $arrayOfNiveaux = array_reverse($arrayOfNiveaux);
// // dd($arrayOfNiveaux);
// $diffrence = 6 - count($arrayOfNiveaux);
// if($diffrence > 0){
// for($k = 1; $k <= $diffrence; $k++){
// array_push($arrayOfNiveaux, ["", ""]);
// }
// }
// $sheet->setCellValue('F'.$i, $arrayOfNiveaux[0][1]);
// $sheet->setCellValue('G'.$i, $arrayOfNiveaux[0][0]);
// $sheet->setCellValue('H'.$i, $arrayOfNiveaux[1][1]);
// $sheet->setCellValue('I'.$i, $arrayOfNiveaux[1][0]);
// $sheet->setCellValue('J'.$i, $arrayOfNiveaux[2][1]);
// $sheet->setCellValue('K'.$i, $arrayOfNiveaux[2][0]);
// $sheet->setCellValue('L'.$i, $arrayOfNiveaux[3][1]);
// $sheet->setCellValue('M'.$i, $arrayOfNiveaux[3][0]);
// $sheet->setCellValue('N'.$i, $arrayOfNiveaux[4][1]);
// $sheet->setCellValue('O'.$i, $arrayOfNiveaux[4][0]);
// $sheet->setCellValue('P'.$i, $arrayOfNiveaux[5][1]);
// $sheet->setCellValue('Q'.$i, $arrayOfNiveaux[5][0]);
// $sheet->setCellValue('R'.$i, $article->getTitre());
// $sheet->setCellValue('S'.$i, $article->getDescription());
// $sheet->setCellValue('T'.$i, $article->getPoid());
// $sheet->setCellValue('U'.$i, $article->getLongeur());
// $sheet->setCellValue('V'.$i, $article->getLargeur());
// $sheet->setCellValue('W'.$i, $article->getHauteur());
// $sheet->setCellValue('X'.$i, $article->getSurface());
// $sheet->setCellValue('Y'.$i, $article->getVolume());
// $sheet->setCellValue('Z'.$i, $article->getPrixVente());
// $sheet->setCellValue('AA'.$i, $article->getPrixVenteMin());
// $sheet->setCellValue('AB'.$i, $article->getPrixVenteMax());
// $sheet->setCellValue('AC'.$i, $article->getPrixVenteMoyenne());
// $sheet->setCellValue('AD'.$i, $article->getPrixAchat());
// $sheet->setCellValue('AE'.$i, $article->getPrixAchatMin());
// $sheet->setCellValue('AF'.$i, $article->getPrixAchatMax());
// $sheet->setCellValue('AG'.$i, $article->getPrixAchatMoyenne());
// $sheet->setCellValue('AH'.$i, $article->getAutreInformation());
// $sheet->setCellValue('AI'.$i, $article->getDescriptionDetail());
// $sheet->setCellValue('AJ'.$i, $article->getGererEnStock() ? "oui" : "non");
// $sheet->setCellValue('AK'.$i, $article->getVerificationStock() ? "oui" : "non");
// $sheet->setCellValue('AL'.$i, $article->getCodeBarre());
// $sheet->setCellValue('AM'.$i, $article->getTva());
// $sheet->setCellValue('AN'.$i, $article->getDosage());
// $sheet->setCellValue('AO'.$i, $article->getDci());
// $sheet->setCellValue('AP'.$i, $article->getRemise());
// $sheet->setCellValue('AQ'.$i, $article->getPrixReference());
// $sheet->setCellValue('AR'.$i, $article->getTaille());
// $sheet->setCellValue('AS'.$i, $article->getMatiere());
// $sheet->setCellValue('AT'.$i, $article->getConditionnement());
// $sheet->setCellValue('AU'.$i, $article->getNotationTechnique());
// $sheet->setCellValue('AV'.$i, $article->getMarque());
// $sheet->setCellValue('AW'.$i, $article->getMA());
// $sheet->setCellValue('AX'.$i, $article->getRefInterne());
// $sheet->setCellValue('AY'.$i, $article->getSNS());
// $sheet->setCellValue('AZ'.$i, $article->getStatut()->getDesignation());
// $i++;
// }
// $writer = new Xlsx($spreadsheet);
// $fileName = 'articles.xlsx';
// $temp_file = tempnam(sys_get_temp_dir(), $fileName);
// $writer->save($temp_file);
// return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
// return new JsonResponse(['id' => $niveau->getId(), "designation" => $niveau->getDesignation()], 200);
// }
#[Route('/article/import', name: 'article_article_import')]
public function articleImport(Request $request , SluggerInterface $slugger): Response
{
$file = $request->files->get('file');
// dd($file);
if(!$file){
return new JsonResponse('Prière d\'importer le fichier',500);
}
if($file->guessExtension() !== 'xlsx'){
return new JsonResponse('Prière d\'enregister un fichier xlsx', 500);
}
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'_'.$this->getUser()->getUsername().'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('artice_directory'),
$newFilename
);
} catch (FileException $e) {
throw new \Exception($e);
}
$reader = new reader();
$spreadsheet = $reader->load($this->getParameter('artice_directory').'/'.$newFilename);
$worksheet = $spreadsheet->getActiveSheet();
$spreadSheetArys = $worksheet->toArray();
unset($spreadSheetArys[0]);
$sheetCount = count($spreadSheetArys);
foreach ($spreadSheetArys as $sheet) {
if(trim($sheet[4]) == ""){
return new JsonResponse('Veuillez remplir Id niveau',500);
}
}
foreach ($spreadSheetArys as $sheet) {
// dd();
$article = $this->em->getRepository(UArticle::class)->findOneBy(['titre' => trim($request->get("titre"))]);
// dd($article, trim($request->get("titre")));
if(!$article) {
$article = new UArticle();
$article->setTitre(
$sheet[11]
);
$article->setUnite(
$sheet[2] > 0 ? $this->em->getRepository(PUnite::class)->find($sheet[2]) : null
);
$article->setDescription(
$sheet[12]
);
$article->setPoid($sheet[13]);
$article->setLongeur($sheet[14]);
$article->setLargeur($sheet[15]);
$article->setHauteur($sheet[16]);
$article->setSurface($sheet[17]);
$article->setVolume($sheet[18]);
$article->setPrixVente($sheet[19]);
$article->setPrixVenteMin($sheet[20]);
$article->setPrixVenteMax($sheet[21]);
if($sheet[21] >= 0 && $sheet[20] >= 0) {
if(($sheet[21] + $sheet[20]) == 0) {
$article->setPrixVenteMoyenne(0);
} else {
$article->setPrixVenteMoyenne(($sheet[21] + $sheet[20]) / 2);
}
}
$article->setPrixAchat($sheet[23]);
$article->setPrixAchatMin($sheet[24]);
$article->setPrixAchatMax($sheet[25]);
if($sheet[25] >= 0 && $sheet[24] >= 0) {
if(($sheet[25] + $sheet[24]) == 0) {
$article->setPrixAchatMoyenne(0);
} else {
$article->setPrixAchatMoyenne(($sheet[24] + $sheet[25]) / 2);
}
}
$article->setAutreInformation($sheet[27]);
$article->setDescriptionDetail($sheet[28]);
$article->setGererEnStock($sheet[29] == "oui" ? true : false);
$article->setVerificationStock($sheet[30] == "oui" ? true : false);
$article->setCodeBarre($sheet[31]);
$article->setTva($sheet[32]);
$article->setDosage($sheet[33]);
$article->setDci($sheet[34]);
$article->setRemise($sheet[35]);
$article->setPrixReference($sheet[36]);
$article->setTaille($sheet[37]);
$article->setMatiere($sheet[38]);
$article->setConditionnement($sheet[39]);
$article->setNotationTechnique($sheet[40]);
$article->setMarque($sheet[41]);
$article->setMA($sheet[42]);
$article->setRefInterne((float)$sheet[43]);
$article->setSNS($sheet[44]);
$article->setQteStock($sheet[45]);
$article->setFonCode($sheet[46]);
$article->setSpecialiteProduit($sheet[47]);
$article->setSpeCode($sheet[48]);
$article->setAncienCode($sheet[49]);
$article->setCodeCat($sheet[50]);
$article->setClasseProduit($sheet[51]);
$article->setSegmentsProduit($sheet[52]);
$article->setSousSegmentsProduit($sheet[53]);
$article->setReference($sheet[54]);
$article->setVoieAdministration($sheet[55]);
$article->setPresentation($sheet[56]);
$article->setFabricant($sheet[57]);
$article->setFournisseur($sheet[58]);
$article->setCodeDci($sheet[59]);
$article->setEan($sheet[60]);
$article->setAtc($sheet[61]);
$article->setCodeImm($sheet[62]);
$article->setEanAccess($sheet[63]);
$article->setPrixVentePh($sheet[64]);
$article->setPrixVentePpv($sheet[65]);
$article->setPrixVentePpc($sheet[66]);
$article->setCleReference($sheet[67]);
$article->setNiveau(
$sheet[4] > 0 ? $this->em->getRepository(PArticleNiveau::class)->find($sheet[4]) : null
);
$article->setStatut(
$this->em->getRepository(PStatut::class)->find(1)
);
$article->setActive(true);
$this->em->persist($article);
$this->em->flush();
$article->setCode('ART'. str_pad($article->getId(), 6, '0', STR_PAD_LEFT));
$this->em->flush();
}
}
return new JsonResponse("Total des articles crée est ".$sheetCount);
}
#[Route('/article/miseajour', name: 'article_article_miseajour')]
public function articleMiseAJour(Request $request , SluggerInterface $slugger): Response
{
$file = $request->files->get('file');
// dd($file);
if(!$file){
return new JsonResponse('Prière d\'importer le fichier',500);
}
if($file->guessExtension() !== 'xlsx'){
return new JsonResponse('Prière d\'enregister un fichier xlsx', 500);
}
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = $slugger->slug($originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'_'.$this->getUser()->getUsername().'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('artice_directory'),
$newFilename
);
} catch (FileException $e) {
throw new \Exception($e);
}
$reader = new reader();
$spreadsheet = $reader->load($this->getParameter('artice_directory').'/'.$newFilename);
$worksheet = $spreadsheet->getActiveSheet();
$spreadSheetArys = $worksheet->toArray();
unset($spreadSheetArys[0]);
$sheetCount = count($spreadSheetArys);
foreach ($spreadSheetArys as $sheet) {
// dd();
$article = $this->em->getRepository(UArticle::class)->find($sheet[0]);
if($article) {
$article->setTitre(
$sheet[11]
);
$article->setUnite(
$sheet[2] > 0 ? $this->em->getRepository(PUnite::class)->find($sheet[2]) : null
);
$article->setDescription(
$sheet[12]
);
$article->setPoid($sheet[13]);
$article->setLongeur($sheet[14]);
$article->setLargeur($sheet[15]);
$article->setHauteur($sheet[16]);
$article->setSurface($sheet[17]);
$article->setVolume($sheet[18]);
$article->setPrixVente($sheet[19]);
$article->setPrixVenteMin($sheet[20]);
$article->setPrixVenteMax($sheet[21]);
if($sheet[21] >= 0 && $sheet[20] >= 0) {
if(($sheet[21] + $sheet[20]) == 0) {
$article->setPrixVenteMoyenne(0);
} else {
$article->setPrixVenteMoyenne(($sheet[21] + $sheet[20]) / 2);
}
}
$article->setPrixAchat($sheet[23]);
$article->setPrixAchatMin($sheet[24]);
$article->setPrixAchatMax($sheet[25]);
if($sheet[25] >= 0 && $sheet[24] >= 0) {
if(($sheet[25] + $sheet[24]) == 0) {
$article->setPrixAchatMoyenne(0);
} else {
$article->setPrixAchatMoyenne(($sheet[24] + $sheet[25]) / 2);
}
}
$article->setAutreInformation($sheet[27]);
$article->setDescriptionDetail($sheet[28]);
$article->setGererEnStock($sheet[29] == "oui" ? true : false);
$article->setVerificationStock($sheet[30] == "oui" ? true : false);
$article->setCodeBarre($sheet[31]);
$article->setTva($sheet[32]);
$article->setDosage($sheet[33]);
$article->setDci($sheet[34]);
$article->setRemise($sheet[35]);
$article->setPrixReference($sheet[36]);
$article->setTaille($sheet[37]);
$article->setMatiere($sheet[38]);
$article->setConditionnement($sheet[39]);
$article->setMarque($sheet[40]);
$article->setMA($sheet[41]);
$article->setRefInterne($sheet[42]);
$article->setSNS($sheet[43]);
$article->setNiveau(
$sheet[4] > 0 ? $this->em->getRepository(PArticleNiveau::class)->find($sheet[4]) : null
);
}
$this->em->flush();
}
return new JsonResponse("Total des articles à jour est ".$sheetCount);
}
#[Route('/article/details/update/{article}', name: 'article_article_details_update')]
public function articleDetailsUpdate(UArticle $article): Response
{
$arrayOfNiveaux = [];
$currentNiveau = $article->getNiveau()->getId();
for ($j= $article->getNiveau()->getNiveau(); $j > 0; $j--) {
$niveau = $this->em->getRepository(PArticleNiveau::class)->find($currentNiveau);
array_push($arrayOfNiveaux, $niveau);
if($niveau->getParent()) {
$currentNiveau = $niveau->getParent()->getId();
} else {
break;
}
}
// dump($arrayOfNiveaux);
$arrayOfNiveaux = array_reverse($arrayOfNiveaux);
// dd($arrayOfNiveaux);
$diffrence = 6 - count($arrayOfNiveaux);
if($diffrence > 0){
for($k = 1; $k <= $diffrence; $k++){
array_push($arrayOfNiveaux, null);
}
}
$html = $this->render("article/pages/form_update.html.twig", [
'article' => $article,
'niveauxAffecte' => $arrayOfNiveaux,
'niveaux' => $this->em->getRepository(PArticleNiveau::class)->findBy(['parent' => null]),
'unites' => $this->em->getRepository(PUnite::class)->findAll()
])->getContent();
return new JsonResponse([
"html" => $html,
'title' => $article->getTitre()
]);
}
#[Route('/article/update/{article}', name: 'article_article_details_update_save')]
public function articleUpdate(Request $request, UArticle $article): Response
{
$article->setTitre(
$request->get("titre") != "" ? $request->get("titre") : null
);
$article->setPoid(
$request->get("poid") != "" ? $request->get("poid") : null
);
$article->setUnite(
$request->get("unite") != "" ? $this->em->getRepository(PUnite::class)->find($request->get("unite")) : null
);
$article->setLongeur(
$request->get("longeur") != "" ? $request->get("longeur") : null
);
$article->setLargeur(
$request->get("largeur") != "" ? $request->get("largeur") : null
);
$article->setHauteur(
$request->get("hauteur") != "" ? $request->get("hauteur") : null
);
$article->setSurface(
$request->get("surface") != "" ? $request->get("surface") : null
);
$article->setVolume(
$request->get("volume") != "" ? $request->get("volume") : null
);
$article->setPrixVente(
$request->get("prix_vente") != "" ? $request->get("prix_vente") : null
);
$article->setPrixVenteMin(
$request->get("prix_vente_min") != "" ? $request->get("prix_vente_min") : null
);
$article->setPrixVenteMax(
$request->get("prix_vente_max") != "" ? $request->get("prix_vente_max") : null
);
if($request->get("prix_vente_max") >= 0 && $request->get("prix_vente_min") >= 0) {
if(($request->get("prix_vente_max") + $request->get("prix_vente_min")) == 0) {
$article->setPrixVenteMoyenne(0);
} else {
$article->setPrixVenteMoyenne(($request->get("prix_vente_max") + $request->get("prix_vente_min")) / 2);
}
}
$article->setPrixAchat(
$request->get("prix_achat") != "" ? $request->get("prix_achat") : null
);
$article->setPrixAchatMin(
$request->get("prix_achat_min") != "" ? $request->get("prix_achat_min") : null
);
$article->setPrixAchatMax(
$request->get("prix_achat_max") != "" ? $request->get("prix_achat_max") : null
);
if($request->get("prix_achat_max") >= 0 && $request->get("prix_achat_min") >= 0) {
if(($request->get("prix_achat_max") + $request->get("prix_achat_min")) == 0) {
$article->setPrixAchatMoyenne(0);
} else {
$article->setPrixAchatMoyenne(($request->get("prix_achat_max") + $request->get("prix_achat_min")) / 2);
}
}
$article->setAutreInformation(
$request->get("autre_infromation") != "" ? $request->get("autre_infromation") : null
);
$article->setDescriptionDetail(
$request->get("desicription_detail") != "" ? $request->get("desicription_detail") : null
);
$article->setGererEnStock(
$request->get("gerer_en_stock") ? true : false
);
$article->setVerificationStock(
$request->get("verification_stock") ? true : false
);
$article->setCodeBarre(
$request->get("code_barre") != "" ? $request->get("code_barre") : null
);
$article->setTva(
$request->get("tva") != "" ? $request->get("tva") : null
);
$article->setDosage(
$request->get("dosage") != "" ? $request->get("dosage") : null
);
$article->setDci(
$request->get("dci") != "" ? $request->get("dci") : null
);
$article->setRemise(
$request->get("remise") != "" ? $request->get("remise") : null
);
$article->setPrixReference(
$request->get("prix_reference") != "" ? $request->get("prix_reference") : null
);
$article->setTaille(
$request->get("taille") != "" ? $request->get("taille") : null
);
$article->setMatiere(
$request->get("matiere") != "" ? $request->get("matiere") : null
);
$article->setConditionnement(
$request->get("conditionnement") != "" ? $request->get("conditionnement") : null
);
$article->setMarque(
$request->get("marque") != "" ? $request->get("marque") : null
);
$article->setMA(
$request->get("ma") != "" ? $request->get("ma") : null
);
$article->setRefInterne(
$request->get("reference_interne") != "" ? $request->get("reference_interne") : null
);
$article->setSNS(
$request->get("sns") != "" ? $request->get("sns") : null
);
$article->setNiveau(
$this->em->getRepository(PArticleNiveau::class)->find($request->get('niveau'))
);
$article->setQteStock($request->get("QTE_STOCK") != "" ? $request->get("QTE_STOCK") : null);
$article->setFonCode($request->get("FON_CODE") != "" ? $request->get("FON_CODE") : null);
$article->setSpecialiteProduit($request->get("SPECIALITE_PRODUIT") != "" ? $request->get("SPECIALITE_PRODUIT") : null);
$article->setSpeCode($request->get("SPE_CODE") != "" ? $request->get("SPE_CODE") : null);
$article->setAncienCode($request->get("ANCIEN_CODE") != "" ? $request->get("ANCIEN_CODE") : null);
$article->setCodeCat($request->get("CODE_CAT") != "" ? $request->get("CODE_CAT") : null);
$article->setClasseProduit($request->get("CLASSE_PRODUIT") != "" ? $request->get("CLASSE_PRODUIT") : null);
$article->setSegmentsProduit($request->get("SEGMENTS_PRODUIT") != "" ? $request->get("SEGMENTS_PRODUIT") : null);
$article->setSousSegmentsProduit($request->get("SOUS-SEGMENT_PRODUIT") != "" ? $request->get("SOUS-SEGMENT_PRODUIT") : null);
$article->setReference($request->get("REFERENCE") != "" ? $request->get("REFERENCE") : null);
$article->setVoieAdministration($request->get("VOIE_ADMINISTRATION") != "" ? $request->get("VOIE_ADMINISTRATION") : null);
$article->setPresentation($request->get("PRESENTATION") != "" ? $request->get("PRESENTATION") : null);
$article->setFabricant($request->get("FABRICANT") != "" ? $request->get("FABRICANT") : null);
$article->setFournisseur($request->get("FOURNISSEUR") != "" ? $request->get("FOURNISSEUR") : null);
$article->setCodeDci($request->get("CODE_DCI") != "" ? $request->get("CODE_DCI") : null);
$article->setEan($request->get("EAN") != "" ? $request->get("EAN") : null);
$article->setAtc($request->get("ATC") != "" ? $request->get("ATC") : null);
$article->setCodeImm($request->get("CODE_IMM") != "" ? $request->get("CODE_IMM") : null);
$article->setEanAccess($request->get("EAN_ACCESS") != "" ? $request->get("EAN_ACCESS") : null);
$article->setPrixVentePh($request->get("PRIX_VENTE_PH") != "" ? $request->get("PRIX_VENTE_PH") : null);
$article->setPrixVentePpv($request->get("PRIX_VENTE_PPV") != "" ? $request->get("PRIX_VENTE_PPV") : null);
$article->setPrixVentePpc($request->get("PRIX_VENTE_PPC") != "" ? $request->get("PRIX_VENTE_PPC") : null);
$article->setCleReference($request->get("CLE_REFERENCE") != "" ? $request->get("CLE_REFERENCE") : null);
if($article->getStatut()->getDesignation() == "Intégrer") {
$article->setStatut(
$this->em->getRepository(PStatut::class)->find(4)
);
}
$this->em->flush();
return new JsonResponse("Bien Enregistre",200);
}
#[Route('/article/integere', name: 'article_article_integere')]
public function articleintegere(Request $request, HttpClientInterface $client): Response
{
$insert = 0;
$updated = 0;
/** @var UArticle[] */
$articles = $this->em->getRepository(UArticle::class)->findBy(['statut' => $this->em->getRepository(PStatut::class)->find(4)]);
// dd($articles);
foreach($articles as $article) {
// dd($this->getParameter("api")."/api/insert/article");
$response = $client->request('POST', $this->getParameter("api")."/api/insert/article", [
'body' => [
'id' => $article->getId(),
'unite' => $article->getUnite() ? $article->getUnite()->getId() : null,
'niveau' => $article->getNiveau()->getId(),
'code' => $article->getCode(),
'titre' => $article->getTitre(),
'description' => $article->getDescription(),
'poid' => $article->getPoid(),
'longeur' => $article->getLongeur(),
'largeur' => $article->getLargeur(),
'hauteur' => $article->getHauteur(),
'surface' => $article->getSurface(),
'famille_article' =>$article->getFamille(),
'categorie_article' =>$article->getCategorie(),
'volume' => $article->getVolume(),
'prix_vente' => $article->getPrixVente(),
'prix_vente_min' => $article->getPrixVenteMin(),
'prix_vente_max' => $article->getPrixVenteMax(),
'prix_vente_moyenne' => $article->getPrixVenteMoyenne(),
'prix_achat' => $article->getPrixAchat(),
'prix_achat_min' => $article->getPrixAchatMin(),
'prix_achat_max' => $article->getPrixAchatMax(),
'prix_achat_moyenne' => $article->getPrixAchatMoyenne(),
'active' => $article->getActive(),
'autre_information' => $article->getAutreInformation(),
'descirption_detail' => $article->getDescriptionDetail(),
'gerer_en_stock' => $article->getGererEnStock(),
'verification_stock' => $article->getVerificationStock(),
'code_barre' => $article->getCodeBarre(),
'tva' => $article->getTVA(),
'dosage' => $article->getDosage(),
'dci' => $article->getDCI(),
'remise' => $article->getRemise(),
'prix_reference' => $article->getPrixReference(),
'taille' => $article->getTaille(),
'matiere' => $article->getMatiere(),
'conditionnement' => $article->getConditionnement(),
'notation_technique' => $article->getNotationTechnique(),
'marque' => $article->getMarque(),
'ai' => $article->getAi(),
'av' => $article->getAv(),
'ma' => $article->getMa(),
'ref_interne' => $article->getRefInterne(),
'sns' => $article->getSns(),
'statut_id' => $article->getStatut()->getId(),
'etat_achat' => $article->getEtatAchat(),
'etat_vente' => $article->getEtatVente(),
'qte_stock' => $article->getQteStock(),
'fon_code' => $article->getFonCode(),
'specialite_produit' => $article->getSpecialiteProduit(),
'spe_code' => $article->getSpeCode(),
'ancien_code' => $article->getAncienCode(),
'code_cat' => $article->getCodeCat(),
'classe_produit' => $article->getClasseProduit(),
'segments_produit' => $article->getSegmentsProduit(),
'sous_segment_produit' => $article->getSousSegmentsProduit(),
'reference' => $article->getReference(),
'voie_administration' => $article->getVoieAdministration(),
'presentation' => $article->getPresentation(),
'fabricant' => $article->getFabricant(),
'fournisseur' => $article->getFournisseur(),
'code_dci' => $article->getCodeDci(),
'ean' => $article->getEan(),
'atc' => $article->getAtc(),
'code_imm' => $article->getCodeImm(),
'ean_access' => $article->getEanAccess(),
'prix_vente_ph' => $article->getPrixVentePh(),
'prix_vente_ppv' => $article->getPrixVentePpv(),
'prix_vente_ppc' => $article->getPrixVentePpc(),
'cle_reference' => $article->getCleReference(),
],
]);
if($response->getContent() == "good") {
$article->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
$updated++;
}
}
$idArticles = json_decode($request->get('articles'));
foreach($idArticles as $idArticle) {
$article = $this->em->getRepository(UArticle::class)->find($idArticle);
if($article->getStatut()->getDesignation() == "Valider"){
// dd($this->getParameter("api")."/api/insert/article");
$idNiveaux = ApiController::pluckParent($article->getNiveau());
// i reversed the array because [18051, 18050, X, Y, Z, A]
// imagine 18051 is the Level 6 and 18050 is the level 5 and both are new levels not inserted in ugouv.
// when u try to insert the first value means the level 6 he will try to find the parent id in ugouv but also the lvl 5 not inserted yet will persist a null on parent id
// thats why i reversed the array to start with the revent level to the last level to prevent that bug
$idNiveauxReversed = array_reverse($idNiveaux);
foreach($idNiveauxReversed as $idNiveau) {
$niveau = $this->em->getRepository(PArticleNiveau::class)->find($idNiveau);
if($niveau->getStatut()->getDesignation() == "Valider") {
$response = $client->request('POST', $this->getParameter("api")."/api/insert/niveau", [
'body' => [
'id' => $niveau->getId(),
'parent' => $niveau->getParent() ? $niveau->getParent()->getId() : '',
'niveau' => $niveau->getNiveau(),
'designation' => $niveau->getDesignation(),
'cca1' => $niveau->getCCA1(),
'cca2' => $niveau->getCCA2(),
'cca3' => $niveau->getCCA3(),
'cca4' => $niveau->getCCA4(),
],
]);
// dd($response->getContent());
if($response->getContent() == "good") {
$niveau->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
}
// dump($response->getContent());
}
}
// die;
$response = $client->request('POST', $this->getParameter("api")."/api/insert/article", [
'body' => [
'id' => $article->getId(),
'unite' => $article->getUnite() ? $article->getUnite()->getId() : null,
'niveau' => $article->getNiveau()->getId(),
'code' => $article->getCode(),
'titre' => $article->getTitre(),
'description' => $article->getDescription(),
'poid' => $article->getPoid(),
'longeur' => $article->getLongeur(),
'largeur' => $article->getLargeur(),
'hauteur' => $article->getHauteur(),
'surface' => $article->getSurface(),
'famille_article' =>$article->getFamille(),
'categorie_article' =>$article->getCategorie(),
'volume' => $article->getVolume(),
'prix_vente' => $article->getPrixVente(),
'prix_vente_min' => $article->getPrixVenteMin(),
'prix_vente_max' => $article->getPrixVenteMax(),
'prix_vente_moyenne' => $article->getPrixVenteMoyenne(),
'prix_achat' => $article->getPrixAchat(),
'prix_achat_min' => $article->getPrixAchatMin(),
'prix_achat_max' => $article->getPrixAchatMax(),
'prix_achat_moyenne' => $article->getPrixAchatMoyenne(),
'active' => $article->getActive(),
'autre_information' => $article->getAutreInformation(),
'descirption_detail' => $article->getDescriptionDetail(),
'gerer_en_stock' => $article->getGererEnStock(),
'verification_stock' => $article->getVerificationStock(),
'code_barre' => $article->getCodeBarre(),
'tva' => $article->getTVA(),
'dosage' => $article->getDosage(),
'dci' => $article->getDCI(),
'remise' => $article->getRemise(),
'prix_reference' => $article->getPrixReference(),
'taille' => $article->getTaille(),
'matiere' => $article->getMatiere(),
'conditionnement' => $article->getConditionnement(),
'notation_technique' => $article->getNotationTechnique(),
'marque' => $article->getMarque(),
'ai' => $article->getAi(),
'av' => $article->getAv(),
'ma' => $article->getMa(),
'ref_interne' => $article->getRefInterne(),
'sns' => $article->getSns(),
'statut_id' => $article->getStatut()->getId(),
'etat_achat' => $article->getEtatAchat(),
'etat_vente' => $article->getEtatVente(),
'cle_reference' => $article->getCleReference(),
],
]);
if($response->getContent() == "good") {
$article->setStatut(
$this->em->getRepository(PStatut::class)->find(3)
);
$insert++;
}
}
}
$this->em->flush();
return new JsonResponse(['insert' => $insert, 'updated' => $updated],200);
}
}