<?php
namespace App\EventSubscriber\Boutique;
use App\Entity\Boutique\AvoirHistorique;
use App\Entity\Boutique\Facture;
use App\Entity\Boutique\ProduitStockBalance;
use App\Entity\User\CagnotteHistorique;
use App\Entity\User\PromotionHistorique;
use App\Manager\Boutique\ProduitStockBalanceManager;
use App\Manager\Boutique\WebserviceManager;
use App\Utils\Toolbox;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\Emails\CommandeEmail;
use App\Entity\Boutique\Produit;
use App\Entity\Boutique\ProduitDeclinaison;
use App\Entity\Boutique\Commande;
use App\Entity\Boutique\CommandeHistorique;
use App\Event\CommandeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
class CommandeSubscriber implements EventSubscriberInterface
{
private $em;
private $commandeEmail;
private $router;
private $webserviceManager;
/**
* @var ProduitStockBalanceManager
*/
private $produitStockBalanceManager;
public function __construct(
EntityManagerInterface $entityManager,
CommandeEmail $commandeEmail,
RouterInterface $router,
WebserviceManager $webserviceManager,
ProduitStockBalanceManager $produitStockBalanceManager
) {
$this->em = $entityManager;
$this->commandeEmail = $commandeEmail;
$this->router = $router;
$this->webserviceManager = $webserviceManager;
$this->produitStockBalanceManager = $produitStockBalanceManager;
}
public static function getSubscribedEvents()
{
return array(
CommandeEvent::COMMANDE_ENREGISTRER_SUCCESS => array('onCommandeEnregistrerSuccess', -10),
CommandeEvent::COMMANDE_ENREGISTRER_STATUT => array('onCommandeEnregistrerStatut', -10),
CommandeEvent::COMMANDE_ENREGISTRER_MODEPAIEMENT => array('onCommandeEnregistrerModepaiement', -10),
);
}
public function onCommandeEnregistrerSuccess(CommandeEvent $event)
{
}
public function onCommandeEnregistrerStatut(CommandeEvent $event)
{
$commande = $event->getCommande();
$eventUser = $event->getUser();
// Création Facture
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
$commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
is_null($commande->getFacture())
) {
$numero = strtoupper(Toolbox::generateRandomString(10));
$facture = new Facture();
$facture->setNumero($numero);
$this->em->persist($facture);
$this->em->flush();
$commande->setFacture($facture);
$this->em->persist($commande);
$this->em->flush();
}
// Création Facture avoir
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) &&
!is_null($commande->getFacture()) &&
is_null($commande->getFactureAvoir())
) {
$numero = strtoupper(Toolbox::generateRandomString(10));
$facture = new Facture();
$facture->setNumero($numero);
$facture->setIsAvoir(true);
$this->em->persist($facture);
$this->em->flush();
$commande->setFactureAvoir($facture);
$this->em->persist($commande);
$this->em->flush();
}
// MAJ des ebillets gere par le site
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
$commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
false === $commande->getCommandeAyantDroits()->isEmpty()
) {
$ebilletsEnRupture = [];
foreach ($commande->getCommandeAyantDroits() as $commandeAyantDroit) {
$commandeLigne = $commandeAyantDroit->getCommandeLigne();
$entite = $commandeLigne->getProduit();
if (!is_null($commandeLigne->getProduitDeclinaison())) {
$entite = $commandeLigne->getProduitDeclinaison();
}
$eanDisponibles = $entite->getProduitEansDisponibles();
// Si on a des ebillets dispo en stock
if (count($eanDisponibles)) {
$produitEan = $eanDisponibles[0];
$produitEan->setCommandeAyantDroit($commandeAyantDroit);
$produitEan->setUtilise(true);
$produitEan->setNom($commandeAyantDroit->getNom());
$produitEan->setPrenom($commandeAyantDroit->getPrenom());
$this->em->persist($produitEan);
$this->em->flush();
}
else {
$commandeAyantDroit->setEbilletManquant(true);
$this->em->persist($commandeAyantDroit);
$this->em->flush();
}
// Envoi mails notification pour ebillet (produit)
if (
is_null($commandeLigne->getProduitDeclinaison()) &&
!is_null($commandeLigne->getProduit()->getSeuilAlerteBillet()) &&
count($commandeLigne->getProduit()->getProduitEansDisponibles()) <= $commandeLigne->getProduit()->getSeuilAlerteBillet()
) {
$ebilletsEnRupture[$commandeLigne->getProduit()->getId().'_0'] = [
'produit' => $commandeLigne->getProduit(),
'declinaison' => null
];
}
// Envoi mails notification pour ebillet (declinaison)
if (
!is_null($commandeLigne->getProduitDeclinaison()) &&
!is_null($commandeLigne->getProduitDeclinaison()->getSeuilAlerteBillet()) &&
count($commandeLigne->getProduitDeclinaison()->getProduitEansDisponibles()) <= $commandeLigne->getProduitDeclinaison()->getSeuilAlerteBillet()
) {
$ebilletsEnRupture[$commandeLigne->getProduit()->getId().'_'.$commandeLigne->getProduitDeclinaison()->getId()] = [
'produit' => $commandeLigne->getProduit(),
'declinaison' => $commandeLigne->getProduitDeclinaison(),
];
}
}
foreach ($ebilletsEnRupture as $ebilletEnRupture) {
$this->commandeEmail->alerteStockEbillet($ebilletEnRupture['produit'], $ebilletEnRupture['declinaison']);
}
}
// MAJ de la passation ReducCE
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
$commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE &&
!empty($commande->getNumeroReducCe()) &&
false === $commande->getPassation()
) {
$response = $this->webserviceManager->passationCommande([
'CE_ID' => getenv('APP_PARTENAIRE_ID'),
'commandes_numero' => $commande->getNumeroReducCe()
]);
$xml = simplexml_load_string($response->PASSATION_COMMANDEResult->any);
if (isset($xml->NewDataSet->Commande->statut)) {
$statut = (string) $xml->NewDataSet->Commande->statut;
$error = (string) $xml->NewDataSet->Commande->message_erreur;
$commandes_numero = (string) $xml->NewDataSet->Commande->commandes_numero;
if ("true" == $statut) {
$commande->setPassation(true);
$this->em->persist($commande);
$this->em->flush();
}
else {
$this->webserviceManager->addLog(
'PASSATION_COMMANDEResult',
$commande->getId(),
$error
);
}
}
}
// Envoi mail de notification si la commande n'a pas deja ete dans ce statut et que le statut declenche un mail
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
in_array($commande->getStatut(), $commande->getStatutEmail())
) {
if ($commande->getStatut() == Commande::STATUT_ATTENTE_CHEQUE)
$this->commandeEmail->alerteCommandeAttentePaiementCheque($commande);
if ($commande->getStatut() == Commande::STATUT_ATTENTE_VIREMENT)
$this->commandeEmail->alerteCommandeAttentePaiementVirement($commande);
if ($commande->getStatut() == Commande::STATUT_ATTENTE_REAPPRO)
$this->commandeEmail->alerteCommandeAttenteReapprovisionnement($commande);
if ($commande->getStatut() == Commande::STATUT_PAIEMENT_ACCEPTE)
$this->commandeEmail->alerteCommandePaiementAccepte($commande);
if ($commande->getStatut() == Commande::STATUT_PAIEMENT_ERREUR)
$this->commandeEmail->alerteCommandePaiementErreur($commande);
if ($commande->getStatut() == Commande::STATUT_ENCOURS_PREPA)
$this->commandeEmail->alerteCommandeEnCoursPreparation($commande);
if (
$commande->getStatut() == Commande::STATUT_ENCOURS_LIVRAISON ||
$commande->getStatut() == Commande::STATUT_EXPEDIE_ENTREPRISE ||
$commande->getStatut() == Commande::STATUT_EXPEDIE_DOMICILE ||
$commande->getStatut() == Commande::STATUT_EXPEDIE_PARTIELLE_ENTREPRISE ||
$commande->getStatut() == Commande::STATUT_EXPEDIE_PARTIELLE_DOMICILE
) {
$this->commandeEmail->alerteCommandeEnCoursLivraison($commande);
}
if (
$commande->getStatut() == Commande::STATUT_LIVRAISON_ENTREPRISE ||
$commande->getStatut() == Commande::STATUT_LIVRAISON_PARTIELLE_ENTREPRISE
) {
$this->commandeEmail->alerteCommandeLivre($commande);
}
if (
$commande->getStatut() == Commande::STATUT_RETRAIT_AGENCE ||
$commande->getStatut() == Commande::STATUT_RETRAIT_AGENCE_PARTIEL
) {
$this->commandeEmail->alerteCommandeRetraitAgence($commande);
}
if ($commande->getStatut() == Commande::STATUT_ANNULEE)
$this->commandeEmail->alerteCommandeAnnulee($commande);
if ($commande->getStatut() == Commande::STATUT_REMBOURSE)
$this->commandeEmail->alerteCommandeRemboursement($commande);
if ($commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL)
$this->commandeEmail->alerteCommandeRemboursementPartiel($commande);
if ($commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL)
$this->commandeEmail->alerteCommandeRemboursementPartiel($commande);
if ($commande->getStatut() == Commande::STATUT_LIVRE)
$this->commandeEmail->alerteCommandeLivre($commande);
if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_BIENVENUE)
$this->commandeEmail->alerteCommercialBienvenue($commande);
if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_PARRAINAGE)
$this->commandeEmail->alerteCommercialParrainage($commande);
if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_EVALUATION)
$this->commandeEmail->alerteCommercialEvaluation($commande);
if ($commande->getStatut() == Commande::STATUT_COMMERCIAL_PROMOTION)
$this->commandeEmail->alerteCommercialPromotion($commande);
if ($commande->getStatut() == Commande::STATUT_PAIEMENT_PARTIEL_ACCEPTE)
$this->commandeEmail->alerteCommandePaiementPartielAccepte($commande);
if ($commande->getStatut() == Commande::STATUT_COMMANDE_ADMIN) {
$adminToken = $commande->generateAdminToken();
$commande->setAdminToken($adminToken);
$this->em->persist($commande);
$this->em->flush();
$this->commandeEmail->alerteCommandeAdmin($commande);
}
}
// MAJ des avoirs
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
in_array($commande->getStatut(), $commande->getDebitAvoir()) &&
false === $commande->checkStatutPaiementAttente() &&
true === $commande->getUtiliserAvoir() &&
!is_null($commande->getMontantAvoir())
) {
if ($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) {
$avoirHistoriques = $commande->getAvoirHistoriques();
foreach ($avoirHistoriques as $avoirHistorique) {
$avoir = $avoirHistorique->getAvoir();
$nouveauSolde = ($avoir->getReste() + $avoirHistorique->getMontant());
$avoir->setReste($nouveauSolde);
$this->em->persist($avoir);
$newAvoirHistorique = clone ($avoirHistorique);
$newAvoirHistorique->setOperation(AvoirHistorique::OPERATION_CREDIT);
$this->em->persist($newAvoirHistorique);
}
$this->em->flush();
}
else {
$avoirADebiter = $commande->getMontantAvoir();
foreach ($commande->getAvoirUtilises() as $avoir) {
if ($avoir->getReste() > 0) {
$avoirHistorique = new AvoirHistorique();
$avoirHistorique->setOperateur($commande->getUser());
$avoirHistorique->setAvoir($avoir);
$avoirHistorique->setCommande($commande);
$avoirHistorique->setOperation(AvoirHistorique::OPERATION_DEBIT);
$nouveauSolde = ($avoir->getReste() - $avoirADebiter);
if ($nouveauSolde >= 0) {
$avoirHistorique->setMontant($avoirADebiter);
$avoirADebiter = 0;
$avoir->setReste($nouveauSolde);
}
else {
$avoirHistorique->setMontant($avoir->getReste());
$avoirADebiter -= $avoir->getReste();
$avoir->setReste(0);
}
$avoir->addAvoirHistorique($avoirHistorique);
$this->em->persist($avoir);
$this->em->flush();
if (0 == $avoirADebiter) {
break;
}
}
}
}
}
// MAJ de la cagnotte CSE
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
in_array($commande->getStatut(), $commande->getDebitCagnotte()) &&
false === $commande->checkStatutPaiementAttente() &&
true === $commande->getUtiliserCagnotte() &&
!is_null($commande->getMontantCagnotte())
) {
$user = $commande->getUser();
if ($commande->getStatut() == Commande::STATUT_ANNULEE || $commande->getStatut() == Commande::STATUT_REMBOURSE) {
$nouveauMontantCagnotte = ($user->getCagnotte() + $commande->getMontantCagnotte());
$user->setCagnotte($nouveauMontantCagnotte);
$operation = CagnotteHistorique::OPERATION_CREDIT;
} else {
$nouveauMontantCagnotte = ($user->getCagnotte() - $commande->getMontantCagnotte());
$user->setCagnotte($nouveauMontantCagnotte);
$operation = CagnotteHistorique::OPERATION_DEBIT;
}
$cagnotteHistorique = new CagnotteHistorique();
$cagnotteHistorique->setOperation($operation);
$cagnotteHistorique->setUser($user);
$cagnotteHistorique->setMontant($commande->getMontantCagnotte());
$cagnotteHistorique->setOperateur($user);
$cagnotteHistorique->setCommande($commande);
if (false === $user->getGroups()->isEmpty()) {
$cagnotteHistorique->setGroup($user->getGroups()->first());
}
if (!is_null($commande->getPromotion())) {
$cagnotteHistorique->setPromotion($commande->getPromotion());
}
$user->addCagnotteHistorique($cagnotteHistorique);
$this->em->persist($user);
$this->em->flush();
}
// MAJ subventions
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
in_array($commande->getStatut(), $commande->getDebitSubvention()) &&
false === $commande->checkStatutPaiementAttente()
) {
if (
$commande->getStatut() == Commande::STATUT_ANNULEE ||
$commande->getStatut() == Commande::STATUT_REMBOURSE ||
$commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL ||
$commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL
) {
$operation = PromotionHistorique::OPERATION_CREDIT;
}
else {
$operation = PromotionHistorique::OPERATION_DEBIT;
}
$user = $commande->getUser();
foreach ($commande->getLignes() as $ligne) {
if (!is_null($ligne->getPromotion())) {
$dateUtilisationAt = $ligne->getCreatedAt();
if ($operation == PromotionHistorique::OPERATION_CREDIT) {
$dateUtilisationAt = new \DateTime();
}
$promotionHistorique = new PromotionHistorique();
$promotionHistorique->setUser($user);
$promotionHistorique->setOperateur($user);
$promotionHistorique->setCommande($commande);
$promotionHistorique->setCommandeLigne($ligne);
$promotionHistorique->setDateUtilisationAt($dateUtilisationAt);
$promotionHistorique->setPromotion($ligne->getPromotion());
$promotionHistorique->setOperation($operation);
if (false === $user->getGroups()->isEmpty()) {
$promotionHistorique->setGroup($user->getGroups()->first());
}
if (
($commande->getStatut() == Commande::STATUT_REMBOURSE_PARTIEL || $commande->getStatut() == Commande::STATUT_CREDIT_PARTIEL) &&
(!is_null($ligne->getQuantiteRetourne()) || !is_null($ligne->getMontantRembourse()))
) {
if (!is_null($ligne->getMontantRembourse())) {
$promotionHistorique->setMontant($ligne->getMontantRembourse());
}
if (!is_null($ligne->getQuantiteRetourne())) {
$promotionHistorique->setQuantite($ligne->getQuantiteRetourne());
}
$commande->addPromotionHistorique($promotionHistorique);
}
if (
$commande->getStatut() != Commande::STATUT_REMBOURSE_PARTIEL &&
$commande->getStatut() != Commande::STATUT_CREDIT_PARTIEL
) {
$promotionHistorique->setMontant($ligne->getMontantRemise() * $ligne->getQuantite());
$promotionHistorique->setQuantite($ligne->getQuantite());
$commande->addPromotionHistorique($promotionHistorique);
}
}
}
$this->em->persist($commande);
$this->em->flush();
}
// MAJ du stock si la commande n'a pas deja ete dans ce statut et que le statut declenche une MAJ du stock
if (
false === $commande->checkStatutHistorique($commande->getStatut()) &&
// false === $commande->checkStatutPaiementAttente() &&
in_array($commande->getStatut(), $commande->getStatutMajStock())
) {
$this->produitStockBalanceManager->updateStockByCommande($commande);
}
// Ajout dans table historique
$commandeHistorique = new CommandeHistorique();
$commandeHistorique->setCommande($commande);
$commandeHistorique->setUser($eventUser);
$commandeHistorique->setStatut($commande->getStatut());
$commande->addCommandeHistorique($commandeHistorique);
$this->em->persist($commande);
$this->em->flush();
}
public function onCommandeEnregistrerModepaiement(CommandeEvent $event)
{
}
}