<?php
namespace App\Services;
use App\Entity\IctoMouvement;
use App\Entity\IctusPharmacie;
use App\Entity\User;
use App\Repository\IctoMouvementRepository;
use App\Repository\IctoParamgeneralRepository;
use App\Repository\IctoParampovRepository;
use App\Repository\IctoTypemouvementRepository;
use App\Repository\IctoValeurRepository;
use App\Repository\IctusPharmacieRepository;
use App\Repository\PaysRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\Util\StringUtil;
use Symfony\Component\Security\Core\Security;
class IctocoinService
{
private $pharmacieRepo;
private $typeMouvementRepo;
private $ictoParamPovRepo;
private $ictoParamGeneralRepo;
private $em;
private $security;
private $pays;
private $ictoValeurRepo;
private $ictoMouvementRepo;
public function __construct(
Security $security,
EntityManagerInterface $em,
IctusPharmacieRepository $pharmacieRepo,
IctoTypemouvementRepository $typeMouvementRepo,
IctoParampovRepository $ictoParampovRepo,
IctoParamgeneralRepository $ictoParamgeneralRepo,
PaysRepository $paysRepo,
IctoValeurRepository $ictoValeurRepo,
IctoMouvementRepository $ictoMouvementRepo
) {
$this->security = $security;
$this->em = $em;
$this->pharmacieRepo = $pharmacieRepo;
$this->typeMouvementRepo = $typeMouvementRepo;
$this->ictoParamPovRepo = $ictoParampovRepo;
$this->ictoParamGeneralRepo = $ictoParamgeneralRepo;
$this->ictoValeurRepo = $ictoValeurRepo;
$this->ictoMouvementRepo = $ictoMouvementRepo;
$wherePays = array(
"codepays" => "MG"
);
$this->pays = $paysRepo->findOneBy($wherePays);
}
public function debiter($pharmacie, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(2);
if (!($pharmacie instanceof IctusPharmacie)) {
$pharmacie = $this->pharmacieRepo->find($pharmacie);
}
$ictocoin->setPharmacie($pharmacie)->setMontant(-$montant)->setRaison($raison)->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function crediter($pharmacie, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(1);
if (!($pharmacie instanceof IctusPharmacie)) {
$pharmacie = $this->pharmacieRepo->find($pharmacie);
}
$ictocoin->setPharmacie($pharmacie)->setMontant($montant)->setRaison($raison)->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function getSolde($pharmacie)
{
$solde = array("reel" => 0, "du" => 0, "gain" => 0);
if (!($pharmacie instanceof IctusPharmacie)) {
$pharmacie = $this->pharmacieRepo->find($pharmacie);
}
$result = $this->ictoMouvementRepo->getSoldeByPharmacie($pharmacie)[0]["solde"];
if ($result < 0) {
$solde["reel"] = $result;
$solde["du"] = abs($result);
$solde["gain"] = 0;
} else if ($result > 0) {
$solde["reel"] = $result;
$solde["du"] = 0;
$solde["gain"] = $result;
}
return $solde;
}
public function getSoldeIctus()
{
$solde = array("reel" => 0, "du" => 0, "gain" => 0);
$result = $this->ictoMouvementRepo->getSoldeIctus()[0]["solde"];
if ($result > 0) {
$solde["reel"] = $result;
$solde["du"] = $result;
$solde["gain"] = 0;
} else if ($result < 0) {
$solde["reel"] = $result;
$solde["du"] = 0;
$solde["gain"] = abs($result);
}
return $solde;
}
//societe livraison
public function debiterSocieteLivraison($societelivraison, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(2);
$ictocoin->setPharmacie(null)
->setSocieteLivraison($societelivraison)
->setMontant(-$montant)
->setRaison($raison)
->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function crediterSocieteLivraison($societelivraison, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(1);
$ictocoin->setPharmacie(null)
->setSocieteLivraison($societelivraison)
->setMontant(-$montant)
->setRaison($raison)
->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function getSoldeSocieteLivraison($societeLivraison)
{
$solde = array("reel" => 0, "du" => 0, "gain" => 0);
$result = $this->ictoMouvementRepo->getSoldeSocieteLivraison($societeLivraison)[0]["solde"];
if ($result > 0) {
$solde["reel"] = $result;
$solde["du"] = $result;
$solde["gain"] = 0;
} else if ($result < 0) {
$solde["reel"] = $result;
$solde["du"] = 0;
$solde["gain"] = abs($result);
}
return $solde;
}
//Patient
public function debiterPatient($user, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(2);
if (!($user instanceof User)) {
$user = $this->pharmacieRepo->find($user);
}
$ictocoin->setUser($user)->setMontant(-$montant)->setRaison($raison)->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function crediterPatient($user, $montant, $raison)
{
$ictocoin = new IctoMouvement();
$typeMouvement = $this->typeMouvementRepo->find(1);
if (!($user instanceof User)) {
$user = $this->pharmacieRepo->find($user);
}
$ictocoin->setPharmacie($user)->setMontant($montant)->setRaison($raison)->setTypemouvement($typeMouvement);
$this->em->persist($ictocoin);
$this->em->flush();
if ($ictocoin->getId() !== null) {
return true;
}
return null;
}
public function getSoldePatient($user)
{
$solde = array("reel" => 0, "du" => 0, "gain" => 0);
if (!($user instanceof User)) {
$user = $this->pharmacieRepo->find($user);
}
$result = $this->ictoMouvementRepo->getSoldeByUser($user)[0]["solde"];
if ($result < 0) {
$solde["reel"] = $result;
$solde["du"] = abs($result);
$solde["gain"] = 0;
} else if ($result > 0) {
$solde["reel"] = $result;
$solde["du"] = 0;
$solde["gain"] = $result;
}
return $solde;
}
public function getMouvementSoldeSocieteLivraison($societeLivraison, $offset = 0, $limit = 10)
{
return $this->ictoMouvementRepo->getMouvementSocieteLivraison($societeLivraison, $offset, $limit);
}
public function getCountAllMouvementSocieteLivraison($societelivraison)
{
return $this->ictoMouvementRepo->getCountAllMouvementSocietelivraison($societelivraison)[0]["nb"];
}
public function getMouvementSolde($pharmacie, $offset = 0, $limit = 10, $mouvementAll, $dated = null, $datef = null)
{
return $this->ictoMouvementRepo->getMouvement($pharmacie, $offset, $limit, $mouvementAll, $dated, $datef);
}
public function getMouvementSoldeIctus($offset = 0, $limit = 10)
{
return $this->ictoMouvementRepo->getMouvementIctus($offset, $limit);
}
public function getCountAllMouvement($pharmacie, $mouvementAll = 0, $dateDebut = null, $dateFin = null)
{
return $this->ictoMouvementRepo->getCountAllMouvement($pharmacie, $mouvementAll, $dateDebut, $dateFin)[0]["nb"];
}
public function getCountAllMouvementIctus()
{
return $this->ictoMouvementRepo->getCountAllMouvementIctus()[0]["nb"];
}
public function montantIctocoin($pharmacie, $montantCommande, $pays = null)
{
$pourcentage = 0;
$valeurIctocoin = 0;
$pays = $pays ?? $this->pays;
if (!($pharmacie instanceof IctusPharmacie)) {
$pharmacie = $this->pharmacieRepo->find($pharmacie);
}
$findOffreSpecial = $this->ictoParamPovRepo->findOffreSpecial($pharmacie);
if ($findOffreSpecial) {
$result = $this->ictoParamPovRepo->getPourcentageByPharmacie($pharmacie, $montantCommande);
if ($result) {
$pourcentage = $result["0"]["pourcentage"];
}
} else {
$result = $this->ictoParamGeneralRepo->getPourcentageByMontant($montantCommande, $pays);
if ($result) {
$pourcentage = $result["0"]["pourcentage"];
}
}
$whereVI = array(
"currency" => $pays->getCurrency()
);
$getValeur = $this->ictoValeurRepo->findOneBy($whereVI);
if ($getValeur) {
$valeurIctocoin = $getValeur->getValeur();
}
return $montantCommande * $valeurIctocoin * $pourcentage / 100;
}
}