@@ -0,0 +1,37 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Builder\Merchant; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\CaracoleBundle\Resolver\MerchantResolver; | |||
use Lc\SovBundle\Component\CookieComponent; | |||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | |||
use Symfony\Component\HttpFoundation\Cookie; | |||
class MerchantBuilder | |||
{ | |||
protected ParameterBagInterface $parameterBag; | |||
protected CookieComponent $cookieComponent; | |||
public function __construct( | |||
ParameterBagInterface $parameterBag, | |||
CookieComponent $cookieComponent | |||
) { | |||
$this->parameterBag = $parameterBag; | |||
$this->cookieComponent = $cookieComponent; | |||
} | |||
public function setCookieMerchantCurrent($response, MerchantInterface $merchant) | |||
{ | |||
$response->headers->setCookie( | |||
Cookie::create( | |||
$this->parameterBag->get('app.cookie_name_merchant_current'), | |||
$merchant->getId(), | |||
0, | |||
'/', | |||
$this->cookieComponent->getCookieDomain() | |||
) | |||
); | |||
} | |||
} |
@@ -6,15 +6,10 @@ use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\CaracoleBundle\Factory\User\UserPointSaleFactory; | |||
use Lc\CaracoleBundle\Model\PointSale\PointSaleInterface; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
use Lc\SovBundle\Builder\User\UserBuilder as SovUserBuilder; | |||
class UserBuilder | |||
class UserBuilder extends SovUserBuilder | |||
{ | |||
protected EntityManagerInterface $entityManager; | |||
public function __construct(EntityManagerInterface $entityManager) | |||
{ | |||
$this->entityManager = $entityManager; | |||
} | |||
public function linkToPointSale(UserInterface $user, PointSaleInterface $pointSale) | |||
{ |
@@ -0,0 +1,77 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Builder\User; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\CaracoleBundle\Factory\User\VisitorFactory; | |||
use Lc\CaracoleBundle\Model\User\VisitorInterface; | |||
use Lc\SovBundle\Component\CookieComponent; | |||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | |||
use Symfony\Component\HttpFoundation\Cookie; | |||
class VisitorBuilder | |||
{ | |||
protected CookieComponent $cookieComponent; | |||
protected ParameterBagInterface $parameterBag; | |||
protected EntityManagerInterface $entityManager; | |||
public function __construct( | |||
EntityManagerInterface $entityManager, | |||
CookieComponent $cookieComponent, | |||
ParameterBagInterface $parameterBag | |||
) { | |||
$this->entityManager = $entityManager; | |||
$this->cookieComponent = $cookieComponent; | |||
$this->parameterBag = $parameterBag; | |||
} | |||
// addVisitor | |||
public function create(string $cookie, string $ip) | |||
{ | |||
$visitorFactory = new VisitorFactory(); | |||
$visitor = $visitorFactory->create($cookie, $ip); | |||
$this->entityManager->persist($visitor); | |||
$this->entityManager->flush(); | |||
} | |||
public function update(VisitorInterface $visitor) | |||
{ | |||
$totalVisit = $visitor->getTotalVisit() + 1; | |||
$visitor->setTotalVisit($totalVisit); | |||
$visitor->setLastAccess(new \DateTime()); | |||
$this->entityManager->persist($visitor); | |||
$this->entityManager->flush(); | |||
} | |||
// setCookieVisitor | |||
public function setCookie($response, $cookie) | |||
{ | |||
$response->headers->setCookie( | |||
Cookie::create( | |||
$this->parameterBag->get('app.cookie_name_visitor'), | |||
$this->cookieComponent->cryptCookie($cookie), | |||
new \DateTime('+2 months'), | |||
'/', | |||
$this->cookieComponent->getCookieDomain() | |||
) | |||
); | |||
} | |||
// updateVisitorCookie | |||
public function updateCookie($response) | |||
{ | |||
$response->headers->setCookie( | |||
Cookie::create( | |||
$this->parameterBag->get('app.cookie_name_visitor'), | |||
$this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()), | |||
new \DateTime('+2 months'), | |||
'/', | |||
$this->cookieComponent->getCookieDomain() | |||
) | |||
); | |||
} | |||
} |
@@ -8,10 +8,15 @@ use Lc\SovBundle\Factory\AbstractFactory; | |||
class VisitorFactory extends AbstractFactory | |||
{ | |||
public function create(): VisitorInterface | |||
public function create(string $cookie, string $ip): VisitorInterface | |||
{ | |||
$visitor = new Visitor(); | |||
$visitor->setCookie($cookie); | |||
$visitor->setIp($ip); | |||
$visitor->setTotalVisit(1); | |||
$visitor->setLastAccess(new \DateTime()); | |||
return $visitor; | |||
} | |||
@@ -16,7 +16,6 @@ class TaxRateStore extends AbstractStore | |||
$this->merchantResolver = $merchantResolver; | |||
} | |||
public function getAsArray() | |||
{ | |||
$query = $this->query->create(); |
@@ -11,4 +11,11 @@ class MerchantRepositoryQuery extends AbstractRepositoryQuery | |||
{ | |||
parent::__construct($repository, 'r', $paginator); | |||
} | |||
public function filterByDevAlias(string $devAlias) | |||
{ | |||
return $this->andWhere('.devAlias LIKE :devAlias') | |||
->setParameter('devAlias', $devAlias); | |||
} | |||
} |
@@ -2,6 +2,7 @@ | |||
namespace Lc\CaracoleBundle\Repository\Merchant; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\SovBundle\Repository\AbstractStore; | |||
class MerchantStore extends AbstractStore | |||
@@ -12,4 +13,14 @@ class MerchantStore extends AbstractStore | |||
{ | |||
$this->query = $query; | |||
} | |||
public function getOneByDevAlias(string $devAlias): ?MerchantInterface | |||
{ | |||
$query = $this->query->create(); | |||
$query->filterByDevAlias($devAlias); | |||
return $query->findOne(); | |||
} | |||
} |
@@ -3,16 +3,19 @@ | |||
namespace Lc\CaracoleBundle\Repository\Product; | |||
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; | |||
use Lc\CaracoleBundle\Model\Product\ProductFamilyModel; | |||
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; | |||
use Lc\SovBundle\Repository\AbstractStore; | |||
class ProductFamilyStore extends AbstractStore | |||
{ | |||
protected ProductFamilyRepositoryQuery $query; | |||
protected PriceResolver $priceResolver; | |||
public function __construct(ProductFamilyRepositoryQuery $query) | |||
public function __construct(ProductFamilyRepositoryQuery $query, PriceResolver $priceResolver) | |||
{ | |||
$this->query = $query; | |||
$this->priceResolver = $priceResolver; | |||
} | |||
public function isOneProductAvailableAddCart(array $products): bool | |||
@@ -25,4 +28,88 @@ class ProductFamilyStore extends AbstractStore | |||
return false; | |||
} | |||
public function getMultiplyingFactor($productFamily) | |||
{ | |||
if ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) { | |||
if ($productFamily->getBuyingPrice() > 0) { | |||
return number_format( | |||
$this->priceResolver->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(), | |||
3 | |||
); | |||
} | |||
} elseif ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) { | |||
if ($productFamily->getBuyingPriceByRefUnit() > 0) { | |||
return number_format( | |||
$this->priceResolver->getPriceByRefUnitWithTax( | |||
$productFamily | |||
) / $productFamily->getBuyingPriceByRefUnit(), | |||
3 | |||
); | |||
} | |||
} | |||
} | |||
public function getCheapestProduct(ProductFamilyInterface $productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct( | |||
$productFamily, | |||
function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceWithTaxAndReduction( | |||
$a | |||
) > $priceResolver->getPriceWithTaxAndReduction($b); | |||
}, | |||
true | |||
); | |||
} | |||
public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct( | |||
$productFamily, | |||
function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceByRefUnitWithTaxAndReduction( | |||
$a | |||
) > $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); | |||
}, | |||
false | |||
); | |||
} | |||
public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct( | |||
$productFamily, | |||
function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceByRefUnitWithTaxAndReduction( | |||
$a | |||
) < $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); | |||
}, | |||
false | |||
); | |||
} | |||
private function getCheapestOrMostExpensiveProduct( | |||
ProductFamilyInterface $productFamily, | |||
$comparisonFunction, | |||
$returnSelfIfNotActiveProducts | |||
) { | |||
if ($productFamily->getActiveProducts()) { | |||
$products = $productFamily->getProductsOnline()->getValues(); | |||
if (count($products) > 0) { | |||
usort($products, $comparisonFunction); | |||
return $products[0]; | |||
} | |||
} else { | |||
return $productFamily->getOriginProduct(); | |||
} | |||
if ($returnSelfIfNotActiveProducts) { | |||
return $productFamily; | |||
} else { | |||
return false; | |||
} | |||
} | |||
} |
@@ -9,53 +9,9 @@ use Lc\SovBundle\Repository\AbstractStore; | |||
class ProductStore extends AbstractStore | |||
{ | |||
protected ProductRepositoryQuery $query; | |||
protected PriceResolver $priceResolver; | |||
public function __construct(ProductRepositoryQuery $query, PriceResolver $priceResolver) | |||
{ | |||
$this->query = $query; | |||
$this->priceResolver = $priceResolver; | |||
} | |||
public function getCheapestProduct($productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceWithTaxAndReduction($a) > $priceResolver->getPriceWithTaxAndReduction($b); | |||
}, true); | |||
} | |||
public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) > $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); | |||
}, false); | |||
} | |||
public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily) | |||
{ | |||
$priceResolver = $this->priceResolver; | |||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { | |||
return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) < $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); | |||
}, false); | |||
} | |||
private function getCheapestOrMostExpensiveProduct(ProductFamilyInterface $productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts) | |||
{ | |||
if ($productFamily->getActiveProducts()) { | |||
$products = $productFamily->getProductsOnline()->getValues(); | |||
if (count($products) > 0) { | |||
usort($products, $comparisonFunction); | |||
return $products[0]; | |||
} | |||
} else { | |||
return $productFamily->getOriginProduct(); | |||
} | |||
if ($returnSelfIfNotActiveProducts) { | |||
return $productFamily; | |||
} else { | |||
return false; | |||
} | |||
} | |||
} |
@@ -14,4 +14,11 @@ class SectionRepositoryQuery extends AbstractRepositoryQuery | |||
{ | |||
parent::__construct($repository, 'r', $paginator); | |||
} | |||
public function filterByDevAlias(string $devAlias) | |||
{ | |||
return $this | |||
->andWhere('.devAlias LIKE :devAlias') | |||
->setParameter('devAlias', $devAlias); | |||
} | |||
} |
@@ -2,14 +2,37 @@ | |||
namespace Lc\CaracoleBundle\Repository\Section; | |||
use Lc\CaracoleBundle\Model\Section\SectionInterface; | |||
use Lc\CaracoleBundle\Repository\MerchantStoreTrait; | |||
use Lc\SovBundle\Repository\AbstractStore; | |||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |||
class SectionStore extends AbstractStore | |||
{ | |||
use MerchantStoreTrait; | |||
protected SectionRepositoryQuery $query; | |||
public function __construct(SectionRepositoryQuery $query) | |||
{ | |||
$this->query = $query; | |||
} | |||
// getSection | |||
public function getOneByDevAlias(string $devAlias): ?SectionInterface | |||
{ | |||
$query = $this->query->create(); | |||
$query | |||
->filterByMerchant($this->merchant) | |||
->filterByDevAlias($devAlias); | |||
$section = $query->findOne(); | |||
if (!$section) { | |||
throw new NotFoundHttpException('La section ' . $devAlias . ' est introuvable'); | |||
} | |||
return $section; | |||
} | |||
} |
@@ -11,4 +11,11 @@ class VisitorRepositoryQuery extends AbstractRepositoryQuery | |||
{ | |||
parent::__construct($repository, 'r', $paginator); | |||
} | |||
public function filterByCookie(string $cookie) | |||
{ | |||
return $this | |||
->andWhere('.cookie LIKE :cookie') | |||
->setParameter('cookie', $cookie); | |||
} | |||
} |
@@ -12,4 +12,13 @@ class VisitorStore extends AbstractStore | |||
{ | |||
$this->query = $query; | |||
} | |||
public function getOneByCookie(string $cookie) | |||
{ | |||
$query = $this->query->create(); | |||
$query->filterByCookie($cookie); | |||
return $query->findOne(); | |||
} | |||
} |
@@ -13,21 +13,27 @@ use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\CaracoleBundle\Model\User\UserMerchantInterface; | |||
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository; | |||
use Lc\CaracoleBundle\Repository\Merchant\MerchantStore; | |||
use Lc\CaracoleBundle\Repository\User\UserMerchantRepository; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
use Lc\SovBundle\Resolver\UrlResolver; | |||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | |||
use Symfony\Component\HttpFoundation\RequestStack; | |||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |||
use Symfony\Component\Security\Core\Security; | |||
class MerchantResolver | |||
{ | |||
protected $requestStack; | |||
protected $em; | |||
protected $urlResolver; | |||
protected $security; | |||
protected $currentMerchant; | |||
protected $merchantRepository; | |||
protected $userMerrchantRepository; | |||
protected ?MerchantInterface $currentMerchant; | |||
protected RequestStack $requestStack; | |||
protected EntityManagerInterface $em; | |||
protected UrlResolver $urlResolver; | |||
protected Security $security; | |||
protected MerchantRepository $merchantRepository; | |||
protected UserMerchantRepository $userMerchantRepository; | |||
protected UrlGeneratorInterface $router; | |||
protected MerchantStore $merchantStore; | |||
protected ParameterBagInterface $parameterBag; | |||
public function __construct( | |||
EntityManagerInterface $entityManager, | |||
@@ -35,7 +41,10 @@ class MerchantResolver | |||
UrlResolver $urlResolver, | |||
Security $security, | |||
MerchantRepository $merchantRepository, | |||
UserMerchantRepository $userMerchantRepository | |||
UserMerchantRepository $userMerchantRepository, | |||
UrlGeneratorInterface $router, | |||
MerchantStore $merchantStore, | |||
ParameterBagInterface $parameterBag | |||
) { | |||
$this->requestStack = $requestStack; | |||
$this->em = $entityManager; | |||
@@ -43,20 +52,23 @@ class MerchantResolver | |||
$this->security = $security; | |||
$this->merchantRepository = $merchantRepository; | |||
$this->userMerchantRepository = $userMerchantRepository; | |||
$this->merchantStore = $merchantStore; | |||
$this->router = $router; | |||
$this->parameterBag = $parameterBag; | |||
} | |||
public function getCurrent(): MerchantInterface | |||
{ | |||
if ($this->currentMerchant) { | |||
if (isset($this->currentMerchant) && $this->currentMerchant) { | |||
return $this->currentMerchant; | |||
} | |||
$this->currentMerchant = false; | |||
$this->currentMerchant = null; | |||
$request = $this->requestStack->getCurrentRequest(); | |||
$merchants = $this->merchantRepository->findAll(); | |||
$isCli = php_sapi_name() === 'cli'; | |||
if ($request || $isCli) { | |||
if ($request || $isCli) { | |||
if ($isCli || $this->urlResolver->isServerLocalhost()) { | |||
foreach ($merchants as $merchant) { | |||
if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) { | |||
@@ -94,7 +106,6 @@ class MerchantResolver | |||
UserInterface $user = null, | |||
MerchantInterface $merchant = null | |||
): ?UserMerchantInterface { | |||
if ($user === null) { | |||
$user = $this->security->getUser(); | |||
} | |||
@@ -110,4 +121,35 @@ class MerchantResolver | |||
); | |||
} | |||
public function getUrl($merchant) | |||
{ | |||
if ($this->urlResolver->isServerLocalhost()) { | |||
return $this->router->generate('frontend_home', [], UrlGeneratorInterface::ABSOLUTE_URL); | |||
} else { | |||
return $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL); | |||
} | |||
} | |||
public function getMerchantUser(UserInterface $user = null) | |||
{ | |||
$merchants = $this->merchantStore->findAll(); | |||
if ($user) { | |||
return $user->getMerchant(); | |||
} else { | |||
$merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt( | |||
$this->parameterBag->get('app.cookie_name_merchant_current') | |||
); | |||
if ($merchantCurrentId) { | |||
foreach ($merchants as $merchant) { | |||
if ($merchant->getId() == $merchantCurrentId) { | |||
return $merchant; | |||
} | |||
} | |||
} | |||
} | |||
return false; | |||
} | |||
} |
@@ -130,6 +130,17 @@ class OpeningResolver | |||
return false; | |||
} | |||
public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à') | |||
{ | |||
// @TODO : à réécrire | |||
} | |||
public function getDateBeginNextSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à') | |||
{ | |||
// @TODO : à réécrire | |||
} | |||
public function getMessages(): array | |||
{ | |||
return $this->messages; |
@@ -11,7 +11,7 @@ class OrderShopPriceResolver | |||
{ | |||
use PriceResolverTrait; | |||
protected $orderProductPriceResolver; | |||
protected OrderProductPriceResolver $orderProductPriceResolver; | |||
public function __construct(OrderProductPriceResolver $orderProductPriceResolver) | |||
{ |
@@ -0,0 +1,43 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Resolver; | |||
use Lc\CaracoleBundle\Repository\User\VisitorStore; | |||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | |||
use Symfony\Component\HttpFoundation\RequestStack; | |||
class VisitorResolver | |||
{ | |||
protected array $visitor; | |||
protected RequestStack $requestStack; | |||
protected ParameterBagInterface $parameterBag; | |||
protected VisitorStore $visitorStore; | |||
public function __construct( | |||
RequestStack $requestStack, | |||
ParameterBagInterface $parameterBag, | |||
VisitorStore $visitorStore | |||
) { | |||
$this->requestStack = $requestStack; | |||
$this->parameterBag = $parameterBag; | |||
$this->visitorStore = $visitorStore; | |||
} | |||
public function getCurrent() | |||
{ | |||
$cookie = $this->requestStack->getCurrentRequest()->cookies->get( | |||
$this->parameterBag->get('app.cookie_name_visitor') | |||
); | |||
return $this->getVisitor($cookie); | |||
} | |||
private function getVisitor($cookie) | |||
{ | |||
if (!isset($this->visitor[$cookie])) { | |||
$this->visitor[$cookie] = $this->visitorStore->getOneByCookie($cookie); | |||
} | |||
return $this->visitor[$cookie]; | |||
} | |||
} |