<?php
// src/Controller/DefaultController.php
namespace App\Controller;
use App\Entity\HistoryUpdate;
use App\Entity\Products;
use App\Entity\Tickets;
use App\Entity\StocksHistory;
use App\Service\HttpService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\PointsOfSale;
use Doctrine\ORM\EntityManagerInterface;
class DefaultController extends AbstractController
{
private $httpService;
private $em;
public function __construct(HttpService $httpService,EntityManagerInterface $em)
{
$this->httpService = $httpService;
$this->em = $em;
}
/**
* @Route("/")
*/
public function index(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$message = $this->httpService->getHappyMessage();
$user = $this->getUser();
return $this->render('default/index.html.twig', [
'message' => $message,
'user'=> $user
]);
}
/**
* @Route("/home")
*/
public function home(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser();
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
'user'=> $user
]);
}
/**
* @Route("/history", name="update_history")
*/
public function history(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdates = $this->em->getRepository('App\Entity\HistoryUpdate')->findBy(array(), array('id' => 'DESC'), 30, 0);
//var_dump($historyUpdate);die;
return $this->render('default/history.html.twig', [
'historyUpdates' => $historyUpdates,
]);
}
/**
* @Route("/ticket-history/{PointsOfSale}/{annee}", name="ticket_history")
*/
public function tickets($PointsOfSale= null, $annee= '2022'): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$pointsOfSale =null;
if($PointsOfSale){
$pointsOfSale = $this->em->getRepository('App\Entity\PointsOfSale')->findOneBy([
'idCashmag' => $PointsOfSale
]);
}
$allPointOfSales = $this->em->getRepository('App\Entity\PointsOfSale')->findAll();
$allHistoryUpdate = $this->em->getRepository('App\Entity\HistoryUpdate')->findBy([
'entityName' => "TicketHistory"
]);
$alereadyDone =[];
foreach($allHistoryUpdate as $HistoryUpdate){
$comment = json_decode($HistoryUpdate->getComment());
if($comment){
if(isset($comment->mois) AND $comment->idPointofSale == $PointsOfSale){
$alereadyDone[]=$comment->mois;
}
}
}
//var_dump($alereadyDone); die;
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
//$historyUpdates = $this->em->getRepository('App\Entity\HistoryUpdate')->findBy(array(), array('id' => 'DESC'), 30, 0);
//var_dump($historyUpdate);die;
return $this->render('default/ticket.html.twig', [
'allPointOfSales' => $allPointOfSales,
'pointsOfSale' => $pointsOfSale,
'alereadyDone' => $alereadyDone
]);
}
/**
* @Route("/product-stock-history", name="point_vente_stock_history")
*/
public function pointVenteStockHistory(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$allPointOfSales = $this->em->getRepository('App\Entity\PointsOfSale')->findAll();
$allHistoryUpdate = $this->em->getRepository('App\Entity\HistoryUpdate')->findBy([
'entityName' => "StocksHistory"
]);
$alereadyDone =[];
$dateMAJ = [];
foreach($allHistoryUpdate as $HistoryUpdate){
$comment = json_decode($HistoryUpdate->getComment());
if($comment){
$alereadyDone[]=$comment->idPointofSale;
$dateMAJ[$comment->idPointofSale] = $HistoryUpdate->getEndDate();
}
}
return $this->render('default/point-vente.html.twig', [
'allPointOfSales' => $allPointOfSales,
'alereadyDone' => $alereadyDone,
'dateMAJ'=> $dateMAJ
]);
}
/**
* @Route("/point-de-vente", name="point_de_vente")
*/
public function pointDeVente(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdate = new HistoryUpdate();
$historyUpdate->setEntityName('PointsOfSale');
$historyUpdate->setComment('Import Manuelle');
$historyUpdate->setStartDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
try{
//do stuff here
$reponse = $this->httpService->getListPointOfSales();
foreach($reponse['list'] as $pointDeVente){
$pointsOfSale = $this->em->getRepository('App\Entity\PointsOfSale')->findOneBy([
'idCashmag' => $pointDeVente['id']
]);
if($pointsOfSale == null){
$pointsOfSale = new PointsOfSale();
$pointsOfSale->setIdCashmag($pointDeVente['id']);
$pointsOfSale->setIdCountry($pointDeVente['id_country']);
$pointsOfSale->setName($pointDeVente['name']);
$pointsOfSale->setNameWebshop($pointDeVente['name_webshop']);
$pointsOfSale->setAddress($pointDeVente['address']);
$pointsOfSale->setCity($pointDeVente['city']);
$pointsOfSale->setCompanyIdentification($pointDeVente['company_identification']);
$pointsOfSale->setEnableCredit($pointDeVente['enable_credit']);
$pointsOfSale->setEnableOrder($pointDeVente['enable_order']);
$pointsOfSale->setHasAlcool($pointDeVente['has_alcool']);
$pointsOfSale->setLegalForm($pointDeVente['legal_form']);
$pointsOfSale->setPhone($pointDeVente['phone']);
$pointsOfSale->setPhone2($pointDeVente['phone_2']);
$pointsOfSale->setPostal($pointDeVente['postal']);
$pointsOfSale->setWebshopDisplayMode($pointDeVente['webshop_display_mode']);
$pointsOfSale->setWebsite($pointDeVente['website']);
$this->em->persist($pointsOfSale);
$this->em->flush();
}
}
$historyUpdate->setEndDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
}
catch(\Exception $e){
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$historyUpdate->setComment($e->getMessage());
$this->em->persist($historyUpdate);
$this->em->flush();
}
$this->addFlash('success', 'Liste des points de vente mise à jour!');
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
]);
}
/**
* @Route("/remplis-produits", name="fill_products")
*/
public function products(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdate = new HistoryUpdate();
$historyUpdate->setEntityName('Products');
$historyUpdate->setComment('Import Manuelle');
$historyUpdate->setStartDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
//$historyUpdate->getId();
try{
//do stuff here
$allPointOfSales = $this->em->getRepository('App\Entity\PointsOfSale')->findAll();
foreach($allPointOfSales as $pointOfSales){
$idPointofSale = $pointOfSales->getIdCashmag();
$listproducts = $this->httpService->getListproductsByPointOfSales($idPointofSale);
//var_dump( $listproducts); die;
foreach($listproducts['list'] as $product){
$Products = $this->em->getRepository('App\Entity\Products')->findOneBy([
'idCashmag' => $product['id']
]);
if($Products == null){
$dobReconverted = null;//\DateTime::createFromFormat('Y-m-d H:i:s', $product['date']);
$Products = new Products();
$Products->setIdCashmag($product['id']);
$Products->setShortName(mb_convert_encoding($product['short_name'], 'HTML-ENTITIES', 'UTF-8'));
$Products->setName(mb_convert_encoding($product['name'], 'HTML-ENTITIES', 'UTF-8'));
$Products->setOrderName($product['order_name']);
$Products->setVolume($product['volume']);
$Products->setWeight($product['weight']);
$Products->setPrices(json_encode($product['prices']));
$Products->setCostPrice($product['cost_price']);
$Products->setMinimumStock($product['minimum_stock']);
$Products->setEnableComment($product['enable_comment']);
$Products->setExpiration($dobReconverted);// erreur changer entity en bool et ajouter date
$Products->setFidelityPoints($product['fidelity_points']);
$Products->setPoints($product['points']);
$Products->setOptionalSelection($product['optional_selection']);
$Products->setDescription($product['description']);
$Products->setBlocked($product['blocked']);
$Products->setFreePrice($product['free_price']);
$Products->setProductType($product['product_type']);
$Products->setBarcodes(json_encode($product['barcodes']));
$Products->setArchive($product['archive']);
$this->em->persist($Products);
$this->em->flush();
}
}
}
$historyUpdate->setEndDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
}
catch(\Exception $e){
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$historyUpdate->setComment($e->getMessage());
$this->em->persist($historyUpdate);
$this->em->flush();
}
$this->addFlash('success', 'Liste des Produits mise à jour!');
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
]);
}
/**
* @Route("/stock-history-produits", name="fill_stock_history_products")
*/
public function productsStockHistory(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdate = new HistoryUpdate();
$historyUpdate->setEntityName('StocksHistory');
$historyUpdate->setComment('Import Manuelle');
$historyUpdate->setStartDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
//$historyUpdate->getId();
try{
$allPointOfSales = $this->em->getRepository('App\Entity\PointsOfSale')->findAll();
foreach($allPointOfSales as $pointOfSales){
$idPointofSale = $pointOfSales->getIdCashmag();
$listproducts = $this->httpService->getListproductsByPointOfSales($idPointofSale);
//var_dump( $listproducts); die;
foreach($listproducts['list'] as $product){
$Products = $this->em->getRepository('App\Entity\Products')->findOneBy([
'idCashmag' => $product['id']
]);
if($Products == null){
$dobReconverted = null;//\DateTime::createFromFormat('Y-m-d H:i:s', $product['date']);
$Products = new Products();
$Products->setIdCashmag($product['id']);
$Products->setShortName(mb_convert_encoding($product['short_name'], 'HTML-ENTITIES', 'UTF-8'));
$Products->setName(mb_convert_encoding($product['name'], 'HTML-ENTITIES', 'UTF-8'));
$Products->setOrderName($product['order_name']);
$Products->setVolume($product['volume']);
$Products->setWeight($product['weight']);
$Products->setPrices(json_encode($product['prices']));
$Products->setCostPrice($product['cost_price']);
$Products->setMinimumStock($product['minimum_stock']);
$Products->setEnableComment($product['enable_comment']);
$Products->setExpiration($dobReconverted);// erreur changer entity en bool et ajouter date
$Products->setFidelityPoints($product['fidelity_points']);
$Products->setPoints($product['points']);
$Products->setOptionalSelection($product['optional_selection']);
$Products->setDescription($product['description']);
$Products->setBlocked($product['blocked']);
$Products->setFreePrice($product['free_price']);
$Products->setProductType($product['product_type']);
$Products->setBarcodes(json_encode($product['barcodes']));
$Products->setArchive($product['archive']);
$this->em->persist($Products);
$this->em->flush();
}
///////////////////remplissage stock Hitory///////////////////
//$listHistoryproducts = $this->httpService->getStockHystoryProductsByPointOfSales(180192, 9102645);
$listHistoryproducts = $this->httpService->getStockHystoryProductsByPointOfSales($idPointofSale, $product['id']);
$listHistoryproducts = $listHistoryproducts["list"];
usort($listHistoryproducts, function($a, $b) {
return $a['date'] <=> $b['date'];//by date ASC
});
//var_dump($listHistoryproducts); die;
foreach($listHistoryproducts as $historyproduct){
$existe = $this->em->getRepository('App\Entity\StocksHistory')->findOneBy([
'idCashmag' => $historyproduct['id']
]);
if($existe == null){
$stocksHistory = new StocksHistory();
$stocksHistory->setIdCashmag($historyproduct['id']);
$stocksHistory->setIdPointOfSale($idPointofSale);
$stocksHistory->setIdProduct($historyproduct['id_product']);//id produit cashmag
$stocksHistory->setNewQuantityDecimal($historyproduct['new_quantity_decimal']);
$stocksHistory->setQuantityDecimal($historyproduct['quantity_decimal']);
$stocksHistory->setParts($historyproduct['parts']);
$stocksHistory->setArchive($historyproduct['archive']);
$stocksHistory->setType($historyproduct['type']);
$stocksHistory->setDate(\DateTime::createFromFormat('Y-m-d H:i:s', $historyproduct['date']));
$stocksHistory->setComment($historyproduct['comment']);
if($historyproduct['stock_date'] != "0000-00-00 00:00:00"){
$stocksHistory->setStockDate(\DateTime::createFromFormat('Y-m-d H:i:s', $historyproduct['stock_date']));
}
if (array_key_exists('type_text', $historyproduct))
{
$stocksHistory->setTypeText($historyproduct['type_text']);
}
$this->em->persist($stocksHistory);
}
}
$this->em->flush();
}
}
$historyUpdate->setEndDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
}
catch(\Exception $e){
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$historyUpdate->setComment($e->getMessage());
$this->em->persist($historyUpdate);
$this->em->flush();
}
$this->addFlash('success', 'Liste Historique des Produits par point de vente mise à jour!');
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
]);
}
/**
* @Route("/stock-history-point-of-sale", name="fill_stock_history_point_of_sale")
*/
public function pointOfSaleStockHistory(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdate = new HistoryUpdate();
$historyUpdate->setEntityName('StocksHistory');
$historyUpdate->setComment('Import Manuelle');
$historyUpdate->setStartDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
//$historyUpdate->getId();
try{
$allPointOfSales = $this->em->getRepository('App\Entity\PointsOfSale')->findAll();
foreach($allPointOfSales as $pointOfSales){
$idPointofSale = $pointOfSales->getIdCashmag();
$limit = 5000;
$offset = 0;
$i = 1;
do{
///////////////////remplissage stock Hitory///////////////////
//$listHistoryproducts = $this->httpService->getStockHystoryProductsByPointOfSales(180192, 9102645);
$listHistoryproducts = $this->httpService->getStockHystoryByPointOfSales($idPointofSale, $limit, $offset);
//var_dump($listHistoryproducts); die;
$listHistoryproducts = $listHistoryproducts["list"];
usort($listHistoryproducts, function($a, $b) {
return $a['date'] <=> $b['date'];//by date ASC
});
//var_dump($listHistoryproducts); die;
foreach($listHistoryproducts as $historyproduct){
$existe = $this->em->getRepository('App\Entity\StocksHistory')->findOneBy([
'idCashmag' => $historyproduct['id']
]);
if($existe == null){
$stocksHistory = new StocksHistory();
$stocksHistory->setIdCashmag($historyproduct['id']);
$stocksHistory->setIdPointOfSale($idPointofSale);
$stocksHistory->setIdProduct($historyproduct['id_product']);//id produit cashmag
$stocksHistory->setNewQuantityDecimal($historyproduct['new_quantity_decimal']);
$stocksHistory->setQuantityDecimal($historyproduct['quantity_decimal']);
$stocksHistory->setParts($historyproduct['parts']);
$stocksHistory->setArchive($historyproduct['archive']);
$stocksHistory->setType($historyproduct['type']);
$stocksHistory->setDate(\DateTime::createFromFormat('Y-m-d H:i:s', $historyproduct['date']));
$stocksHistory->setComment($historyproduct['comment']);
if($historyproduct['stock_date'] != "0000-00-00 00:00:00"){
$stocksHistory->setStockDate(\DateTime::createFromFormat('Y-m-d H:i:s', $historyproduct['stock_date']));
}
if (array_key_exists('type_text', $historyproduct))
{
$stocksHistory->setTypeText($historyproduct['type_text']);
}
$this->em->persist($stocksHistory);
}
}
$this->em->flush();
$i++;
$offset = $limit * $i;
}while (count($listHistoryproducts) > 0);
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$history= [];
$history["Offset"] = $offset;
$history["idPointofSale"] = $idPointofSale;
$history["type"] = "MANUAL";
$historyUpdate->setComment(json_encode($history));
$this->em->persist($historyUpdate);
$this->em->flush();
$this->addFlash('success', 'Liste Historique des Produits pour point de vente '.$idPointofSale.' mise à jour!');
return $this->render('default/index.html.twig', [
'message' => 'Liste Historique des Produits pour point de vente '.$idPointofSale.' mise à jour!',
]);
}
$historyUpdate->setEndDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
}
catch(\Exception $e){
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$historyUpdate->setComment($e->getMessage());
$this->em->persist($historyUpdate);
$this->em->flush();
}
$this->addFlash('success', 'Liste Historique des Produits par point de vente mise à jour!');
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
]);
}
/**
* @Route("/history-ticket/{{annee}}/{{mois}}/{{idPointofSale}}", name="fill_tickets_history")
*/
public function ticketskHistory($annee, $mois, $idPointofSale): Response
{
ini_set("memory_limit", "-1");
set_time_limit(0);
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$historyUpdate = new HistoryUpdate();
$historyUpdate->setEntityName('TicketHistory');
$historyUpdate->setComment('Import Manuelle');
$historyUpdate->setStartDate(new \DateTime());
$this->em->persist($historyUpdate);
$this->em->flush();
//$historyUpdate->getId();
try{
$dateDebutText ="$annee-$mois-01 00:00:00";
$dateDebut = \DateTime::createFromFormat('Y-m-d H:i:s', $dateDebutText);
$dateFin = \DateTime::createFromFormat('Y-m-d H:i:s', $dateDebutText)->modify('+1 month')->modify('-1 seconds');
$PointOfSale = $this->em->getRepository('App\Entity\PointsOfSale')->findOneBy([
'idCashmag' => $idPointofSale
]);
$limit = 1000;
$offset = 0;
$i = 1;
do{
$listtickets = $this->httpService->getTicketsHistoryByPointOfSales($idPointofSale, $dateDebut, $dateFin, $limit, $offset);
//var_dump( $listtickets); die;
foreach($listtickets['list'] as $ticket){
$Tickets = $this->em->getRepository('App\Entity\Tickets')->findOneBy([
'idCashmag' => $ticket['id']
]);
if($Tickets == null){
$dobReconverted = null;//\DateTime::createFromFormat('Y-m-d H:i:s', $ticket['date']);
$Tickets = new Tickets();
$Tickets->setIdCashmag($ticket['id']);
$Tickets->setComment(mb_convert_encoding($ticket['comment'], 'HTML-ENTITIES', 'UTF-8'));
if($ticket['date'] != "0000-00-00 00:00:00"){
$Tickets->setDate(\DateTime::createFromFormat('Y-m-d H:i:s', $ticket['date']));
}
$Tickets->setRNote(mb_convert_encoding($ticket['r_note'], 'HTML-ENTITIES', 'UTF-8'));
$Tickets->setRTicket(mb_convert_encoding($ticket['r_ticket'], 'HTML-ENTITIES', 'UTF-8'));
$Tickets->setRInvoice(mb_convert_encoding($ticket['r_invoice'], 'HTML-ENTITIES', 'UTF-8'));
$Tickets->setPayments(json_encode($ticket['payments']));//json
$Tickets->setDetails(json_encode($ticket['details']));//json
//$Tickets->setIdTaxes($ticket['cost_price']);
$Tickets->setIdPointOfSale($idPointofSale);
$Tickets->setPointValue($ticket['point_value']);
$Tickets->setPictureLink($ticket['picture_link']);
$Tickets->setPictureBase64($ticket['picture_base64']);
$Tickets->setShortSignature($ticket['short_signature']);
$Tickets->setIdOrder($ticket['id_order']);
$Tickets->setReversal($ticket['reversal']);
$Tickets->setReverse($ticket['reverse']);
$Tickets->setTotalCa($ticket['total_ca']);
$Tickets->setTotalHt($ticket['total_ht']);
$Tickets->setTotalTtc($ticket['total_ttc']);
$Tickets->setNLines($ticket['n_lines']);
$Tickets->setTypeDocument($ticket['type_document']);
$Tickets->setTypeOperation($ticket['type_operation']);
$Tickets->setSoftwareVersion($ticket['software_version']);
$Tickets->setNTable($ticket['n_table']);
$Tickets->setNPeople($ticket['n_people']);
$Tickets->setNInvoice($ticket['n_invoice']);
$Tickets->setNTicket($ticket['n_ticket']);
$Tickets->setNNote($ticket['n_note']);
$Tickets->setOperatorData(json_encode($ticket['operator']));
$Tickets->setClientData(json_encode($ticket['client']));//json
$Tickets->setSaleMethodeData(json_encode($ticket['sale_method']));//json
$Tickets->setServiceData(json_encode($ticket['service']));//json
$Tickets->setAttributesData(json_encode($ticket['attributes']));//json
$Tickets->setTaxesData(json_encode($ticket['taxes']));//json
$Tickets->setCodeDevice($ticket['code_device']);
$Tickets->setCreditAmount($ticket['credit_amount']);
$Tickets->setReference($ticket['reference']);
$Tickets->setUrl($ticket['url']);
$Tickets->setDateText($ticket['date_text']);
$Tickets->setCurrencyCode($ticket['currency_code']);
$this->em->persist($Tickets);
}
}
$this->em->flush();
$i++;
$offset = $limit * $i;
}while (count($listtickets['list']) > 0);
$historyUpdate->setEndDate(new \DateTime());
$history= [];
$history["Offset"] = $offset;
$history["mois"] = "$annee-$mois";
$history["idPointofSale"] = $idPointofSale;
$history["type"] = "MANUAL";
$historyUpdate->setComment(json_encode($history));
$this->em->persist($historyUpdate);
$this->em->flush();
$pointOfSaleName = $PointOfSale->getName();
$this->addFlash('success', "Liste Historique des Ticket point de vente $pointOfSaleName date $annee-$mois mise à jour!");
$message = $this->httpService->getHappyMessage();
return $this->render('default/index.html.twig', [
'message' => $message,
]);
}
catch(\Exception $e){
//error_log($e->getMessage());
$historyUpdate->setEndDate(new \DateTime());
$historyUpdate->setComment($e->getMessage());
$this->em->persist($historyUpdate);
$this->em->flush();
}
}
/**
* @Route("/profile")
*/
public function profile(): Response
{
$number = random_int(0, 100);
return $this->render('default/index.html.twig', [
'number' => $number,
]);
}
}