<?php
namespace App\Manager;
use App\Core\CoreBundle\Manager\BaseManager;
use App\Core\CoreBundle\Traits\Updatable;
use App\Entity\Config;
use App\Entity\Media;
use App\Entity\Univers;
use App\Entity\User;
use App\Service\Convert;
class ArticleManager extends BaseManager
{
use Updatable;
protected Convert $converter;
public function setConverter(Convert $converter)
{
$this->converter = $converter;
}
public function convert($data)
{
return $data;
}
public function getMetiIds()
{
$r = [];
$sql = "SELECT * FROM app_article";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[] = $item['meti_id'];
}
return $r;
}
public function getMetiIdsByBdd()
{
$r = [];
$sql = "SELECT * FROM app_article AS a LEFT JOIN app_univers AS u ON a.univer_id=u.id";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[$item['bdd']][] = $item['meti_id'];
}
return $r;
}
public function convertImportArticle()
{
$r = [];
$sql = "SELECT * FROM app_article";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[$item['meti_id']] = $item['id'];
}
return $r;
}
public function convertImportArticleCodeMarque()
{
$r = [];
$sql = "SELECT * FROM app_article";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[$item['meti_id']] = [
'id' => $item['id'],
'stock' => $item['stock'],
'stockReel' => $item['stock_reel'],
];
}
return $r;
}
public function convertImportArticleEAN()
{
$r = [];
$sql = "SELECT * FROM app_article";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[$item['code_ean']] = $item['id'];
}
$sql = "SELECT * FROM app_ean";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$items = $stmt->executeQuery()->fetchAllAssociative();
foreach ($items as $item) {
$r[$item['code_ean']] = $item['article_id'];
}
return $r;
}
public function getArticlesAPIByUniversSQL($universId, $magasinId, $codeTarif, $request, $client, $commandeCourante)
{
$r = [];
$items = [];
$now = time();
/** @var Univers $univers */
$univers = $this->container->get('app.manager.univers')->find($universId);
$ssFamilleAccess = $univers ? $univers->getSsFamille() : false;
// gestion des images par defaut
/** @var Config $config */
$config = $this->container->get('app.manager.config')->find(1);
$imageDefault = ($config->getImageDefault() instanceof Media) ? $config->getImageDefault()->getName() : null;
$userId = $this->container->get('session')->get('user');
/** @var User $user */
$user = $this->container->get('app.manager.user')->find($userId);
$favorisA = $this->container->get('app.manager.favoris')->getFavorisByUserAndMagasin($userId, $magasinId);
$types = $this->container->get('app.manager.magasin')->find($this->container->get('session')->get('magasin'))->getTypeNames();
$commande = null;
if (isset($request['panier']) and $request['panier'] == 'true') {
$commande = $this->container->get('session')->get('commandeCourante');
}
// @todo => faire le système d'eanscan par session qui se reset apres la requete
if ($this->container->get('session')->get('eanscan')) { // cas de la recherche par scan
$articles = $this->getRepository()->getArticlesAPIByUniversSQL($universId, $request, $favorisA, $commande, $this->container->get('session')->get('eanscan'));
$familles = [];
foreach ($articles as $article) {
$article['eanscan'] = true;
$items[] = $article;
$familles[] = $article['nf_id'];
}
$articles = $this->getRepository()->getArticlesAPIByUniversSQL($universId, $request, $favorisA, $commande, null, array_unique($familles));
foreach ($articles as $article) {
$items[] = $article;
}
$this->container->get('session')->remove('eanscan');
} else {
$items = $this->getRepository()->getArticlesAPIByUniversSQL($universId, $request, $favorisA, $commande);
}
// Afin de recuperer l'historique
$magasinArticles = $this->container->get('app.manager.magasin_article')->getByMagasinAndUnivers($magasinId, $universId);
// dans le cas d'un univers stock import frais, on recupere les fraisCommandeArticle
$fraisCommandeArticle = null;
if ($univers->getFrais()) {
$fraisCommandeArticle = $this->container->get('app.manager.frais_commande_article')->findForCatalogue($universId);
}
// permet d'enlever un niveau au tableau (pour retirer la clé de la commande)
$commandeCourante = array_shift($commandeCourante);
$articlesPresent = [];
$commandeArticlesU = [];
foreach ($items as $item) {
if (!in_array((int)$item['a_id'], $articlesPresent)) { // evite les doublons du aux jointures
if ( // permet de filter par type que peut voir le magasin
!$item['a_type_name'] or
in_array($item['a_type_name'], $types)
) {
$pcb = $item['a_pcb'];
$tarifU = $item['a_prix_' . $codeTarif];
if ($tarifU != 0) {
$tarifT = round($pcb * $tarifU, 2);
$favoris = in_array($item['a_id'], $favorisA);
$quantite = 0;
$quantiteReel = 0;
$quantiteCommande = 0;
$quantiteDepassement = 0;
$tarifHT = 0;
$caId = null;
if (!empty($commandeCourante) and array_key_exists($item['a_id'], $commandeCourante)) {
$caId = $commandeCourante[$item['a_id']]['ca_id'];
$quantite = $commandeCourante[$item['a_id']]['ca_quantite_commande'];
$quantiteReel = $commandeCourante[$item['a_id']]['ca_quantite'];
$quantiteCommande = $commandeCourante[$item['a_id']]['ca_quantite_commande'];
$quantiteDepassement = $commandeCourante[$item['a_id']]['ca_quantite_depassement'];
$tarifHT = $commandeCourante[$item['a_id']]['ca_tarif_ht'];
}
$nouveauteApp = $item['a_nouveaute_app'];
if ($nouveauteApp) {
if (
strtotime($item['a_nouveaute_app_debut']) <= $now &&
strtotime($item['a_nouveaute_app_fin']) >= $now
) {
$nouveauteApp = true;
} else {
$nouveauteApp = false;
}
}
$promoApp = $item['a_promo_app'];
if ($promoApp) {
if (
strtotime($item['a_promo_app_debut']) <= $now &&
strtotime($item['a_promo_app_fin']) >= $now
) {
$promoApp = true;
} else {
$promoApp = false;
}
}
$repartitionStock = ($fraisCommandeArticle) ? $this->calculRepartitionStock($item['a_pcb'], $item['a_stock'], $quantiteCommande) : null;
$repartitionArrivage = ($fraisCommandeArticle) ? $this->calculRepartitionArrivage(
$item['a_pcb'],
$item['a_stock'],
(int)$item['a_id'],
$quantiteCommande,
$fraisCommandeArticle
) : null;
$repartitionReste = ($fraisCommandeArticle) ? $this->calculRepartitionReste(
$item['a_pcb'],
$item['a_stock'],
(int)$item['a_id'],
$quantiteCommande,
$fraisCommandeArticle
) : null;
if(
!array_key_exists('showArrivage', $request) or
$request['showArrivage'] != 'true' or
(is_array($fraisCommandeArticle) and array_key_exists((int)$item['a_id'],$fraisCommandeArticle))
) {
// gestion de l'image
if($item['a_image']) {
$image = $item['a_image'];
} elseif ($item['a_image_perso']) {
$image = $item['a_image_perso'];
} else {
$image = $imageDefault;
}
$r[] = [
'user' => $userId,
'id' => (int)$item['a_id'],
'reference' => $item['a_meti_id'],
'referenceF' => $item['a_meti_id'] . ' | ' . $item['a_ean'] . ' | ' . $item['a_pcb'],
'ean' => $item['a_ean'],
'article' => $item['a_name'],
'nomenclature' => $item['nd_name'] . ' | ' . $item['nr_name'] . ' | ' . $item['nf_name'] . ' | ' . $item['nsf_name'],
'departement' => $item['nd_name'],
'rayon' => $item['nr_name'],
'famille' => $item['nf_name'],
'ssFamille' => $item['nsf_name'],
'showSF' => $ssFamilleAccess,
'departementId' => $item['nd_id'],
'rayonId' => $item['nr_id'],
'familleId' => $item['nf_id'],
'favoris' => $favoris,
'pcb' => $item['a_pcb'],
'tarifU' => $this->converter->mf($tarifU),
'tarifT' => $this->converter->mf($tarifT),
'stock' => $item['a_stock'],
'arrivage' => is_array($fraisCommandeArticle) and array_key_exists((int)$item['a_id'],$fraisCommandeArticle),
'quantite_arrivage' => $this->calculArrivage($item['a_pcb'],(int)$item['a_id'], $fraisCommandeArticle),
'stock_reel' => $item['a_stock_reel'],
'stock_pcb' => ($item['a_pcb']) ? $item['a_stock'] / $item['a_pcb'] : 0,
'stock_reel_pcb' => ($item['a_pcb']) ? $item['a_stock_reel'] / $item['a_pcb'] : 0,
//'arrivage' => $this->calculArrivage($item, $fraisCommandeArticle),
'quantite' => $quantite,
'quantite_reel' => $quantiteReel,
'quantite_commande' => $quantiteCommande,
'quantite_depassement' => $quantiteDepassement,
'ca_tarifHT' => $this->converter->mf($tarifHT),
'ca_id' => $caId,
'nouveaute' => ($item['a_nouveaute'] or $nouveauteApp),
'promo' => ($item['a_promo'] or $promoApp),
'promo_content' => $item['a_promo_app_content'],
'promo_ancien_prix' => $item['a_promo_app_ancien_prix'],
'top' => ($item['a_top'] or $item['a_top_app']),
'content' => $item['a_content'],
'image' => $image,
'avenir' => $item['a_avenir'],
'avenir_date' => $this->convertDate($item['a_avenir_date']),
'historique' => (array_key_exists($item['a_id'], $magasinArticles)) ? $magasinArticles[$item['a_id']] : [],
'historiqueF' => (array_key_exists($item['a_id'], $magasinArticles)) ? $this->formatHistoriqueForExcel($magasinArticles[$item['a_id']]) : [],
'labelF' => $this->formatLabelForExcel($item),
'eanscan' => (array_key_exists('eanscan', $item)),
'repartition_stock' => $repartitionStock,
'repartition_arrivage' => $repartitionArrivage,
'repartition_reste' => $repartitionReste,
'dlc_date' => $this->convertDate($item['a_dlc_date']),
'canSeeStock' => $user->getCanSeeStock(),
];
$articlesPresent[] = (int)$item['a_id'];
}
}
}
}
}
if (empty($request)) {
$this->container->get('session')->set('countArticleTotal', sizeof($r));
$this->container->get('session')->set('countArticleFiltered', sizeof($r));
} else {
$this->container->get('session')->set('countArticleFiltered', sizeof($r));
}
return $r;
}
public function calculRepartitionStock($pcb, $stock, $quantiteCommande)
{
$stockPcb = ($pcb) ? $stock / $pcb : 0;
return ($quantiteCommande >= $stockPcb) ? $stockPcb : $quantiteCommande;
}
public function calculRepartitionArrivage($pcb, $stock, $id, $quantiteCommande, $fraisCommandeArticle)
{
$stockPcb = ($pcb) ? $stock / $pcb : 0;
$arrivage = $this->calculArrivage($pcb, $id, $fraisCommandeArticle);
if ($quantiteCommande <= $stockPcb) {
return 0;
} else {
$quantiteArrivage = $quantiteCommande - $stockPcb;
if ($quantiteArrivage <= $arrivage) {
return $quantiteArrivage;
} else {
return $arrivage;
}
}
}
public function calculRepartitionReste($pcb, $stock, $id, $quantiteCommande, $fraisCommandeArticle)
{
$stockPcb = ($pcb) ? $stock / $pcb : 0;
$arrivage = $this->calculArrivage($pcb, $id, $fraisCommandeArticle);
$stockArrivage = $stockPcb + $arrivage;
if ($quantiteCommande <= $stockArrivage) {
return 0;
} else {
return $quantiteCommande - $stockArrivage;
}
}
public function calculArrivage($pcb, $id, $fraisCommandeArticle)
{
return ($pcb and $fraisCommandeArticle and array_key_exists($id, $fraisCommandeArticle)) ? $fraisCommandeArticle[$id] : 0;
}
public function convertDate($date)
{
if ($date != '') {
$date = explode('-', $date);
return $date[2] . '/' . $date[1] . '/' . $date[0];
}
return '';
}
public function getNomenclatureAPIByUniversSQL($universId)
{
$ret = [];
$items = $this->getRepository()->getNomenclatureAPIByUniversSQL($universId);
foreach ($items as $item) {
$d = $item['departementId'];
$r = $item['rayonId'];
$f = $item['familleId'];
$sf = $item['ssfamilleId'];
if (!array_key_exists($d, $ret)) {
$ret[$d] = [];
$ret[$d]['id'] = $d;
$ret[$d]['name'] = $item['departement'];
$ret[$d]['rayons'] = [];
}
if (!array_key_exists($r, $ret[$d]['rayons'])) {
$ret[$d]['rayons'][$r] = [];
$ret[$d]['rayons'][$r]['id'] = $r;
$ret[$d]['rayons'][$r]['name'] = $item['rayon'];
$ret[$d]['rayons'][$r]['familles'] = [];
}
if (!array_key_exists($f, $ret[$d]['rayons'][$r]['familles'])) {
$ret[$d]['rayons'][$r]['familles'][$f] = [];
$ret[$d]['rayons'][$r]['familles'][$f]['id'] = $f;
$ret[$d]['rayons'][$r]['familles'][$f]['name'] = $item['famille'];
$ret[$d]['rayons'][$r]['familles'][$f]['ssFamilles'] = [];
}
if (!array_key_exists($sf, $ret[$d]['rayons'][$r]['familles'][$f]['ssFamilles'])) {
$ret[$d]['rayons'][$r]['familles'][$f]['ssFamilles'][$sf] = [];
$ret[$d]['rayons'][$r]['familles'][$f]['ssFamilles'][$sf]['id'] = $sf;
$ret[$d]['rayons'][$r]['familles'][$f]['ssFamilles'][$sf]['name'] = $item['ssFamille'];
}
}
return $ret;
}
public function getArticlesForMobile($request, $codeTarif, $image, $removeA = [], $commandecourante)
{
$r = [];
$commandecourante = array_shift($commandecourante);
$articles = $this->getRepository()->getArticlesAPIByUniversSQL($request['univers'], $request);
foreach ($articles as $article) {
if (empty($removeA) or !in_array($article['a_id'], $removeA)) {
$data = $article;
$data['a_prix'] = $article['a_prix_' . $codeTarif];
for ($i = 1; $i <= 6; $i++) {
unset($data['a_prix_' . $i]);
}
$data['a_image'] = ($data['a_image']) ?? $image;
$data['ca_quantite'] = 0;
$data['ca_tarif_ht'] = 0;
$data['ca_id'] = null;
if ($commandecourante and array_key_exists($article['a_id'], $commandecourante)) {
$data['ca_quantite'] = (int)$commandecourante[$article['a_id']]['ca_quantite'];
$data['ca_tarif_ht'] = $data['ca_quantite'] * $data['a_pcb'] * $data['a_prix'];
$data['ca_id'] = (int)$commandecourante[$article['a_id']]['ca_id'];
}
$r[] = $data;
}
}
return $r;
}
private function formatHistoriqueForExcel(mixed $a_id)
{
$r = [];
foreach ($a_id as $value) {
foreach ($value as $day => $quantite) {
$r[] = $day . ' -' . $quantite;
}
}
return implode(' | ', $r);
}
private function formatLabelForExcel(mixed $item)
{
return $item['a_name'] .' | '.$this->convertDate($item['a_dlc_date']);
}
}