namespace Lc\ShopBundle\Controller\Backend; | namespace Lc\ShopBundle\Controller\Backend; | ||||
use App\Entity\OrderShop; | |||||
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; | use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; | ||||
use Lc\ShopBundle\Context\OrderPaymentInterface; | use Lc\ShopBundle\Context\OrderPaymentInterface; | ||||
use Lc\ShopBundle\Context\OrderProductInterface; | use Lc\ShopBundle\Context\OrderProductInterface; | ||||
use Lc\ShopBundle\Form\Backend\Order\OrderReductionCartType; | use Lc\ShopBundle\Form\Backend\Order\OrderReductionCartType; | ||||
use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType; | use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType; | ||||
use Lc\ShopBundle\Form\Backend\Order\OrderStatusType; | use Lc\ShopBundle\Form\Backend\Order\OrderStatusType; | ||||
use Lc\ShopBundle\Model\OrderPayment; | |||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||||
use Symfony\Component\HttpFoundation\Response; | use Symfony\Component\HttpFoundation\Response; |
namespace Lc\ShopBundle\Controller ; | namespace Lc\ShopBundle\Controller ; | ||||
use Lc\ShopBundle\Services\CityUtils; | use Lc\ShopBundle\Services\CityUtils; | ||||
use Lc\ShopBundle\Services\Utils; | |||||
use Symfony\Component\HttpFoundation\Request; | use Symfony\Component\HttpFoundation\Request; | ||||
use Symfony\Component\HttpFoundation\JsonResponse; | use Symfony\Component\HttpFoundation\JsonResponse; | ||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||||
{ | { | ||||
protected $cities ; | protected $cities ; | ||||
public function __construct(CityUtils $cities) | |||||
public function __construct(Utils $cities) | |||||
{ | { | ||||
$this->cities = $cities ; | $this->cities = $cities ; | ||||
} | } | ||||
} | } | ||||
$result = array_merge( | $result = array_merge( | ||||
json_decode($this->cities->callApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))), | |||||
json_decode($this->cities->callApi('get', 'communes', array_merge($data, ['codeRegion' => 44]))) | |||||
json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))), | |||||
json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44]))) | |||||
); | ); | ||||
$return = [] ; | $return = [] ; |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
class CityUtils | |||||
{ | |||||
function callApi($method, $url, $data = false) | |||||
{ | |||||
$url = 'https://geo.api.gouv.fr/'.$url ; | |||||
$curl = curl_init(); | |||||
switch ($method) | |||||
{ | |||||
case "POST": | |||||
curl_setopt($curl, CURLOPT_POST, 1); | |||||
if ($data) | |||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |||||
break; | |||||
case "PUT": | |||||
curl_setopt($curl, CURLOPT_PUT, 1); | |||||
break; | |||||
default: | |||||
if ($data) | |||||
$url = sprintf("%s?%s", $url, http_build_query($data)); | |||||
} | |||||
// Optional Authentication: | |||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |||||
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |||||
curl_setopt($curl, CURLOPT_URL, $url); | |||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |||||
$result = curl_exec($curl); | |||||
curl_close($curl); | |||||
return $result; | |||||
} | |||||
public function getZipByCity($city) | |||||
{ | |||||
$zip = null ; | |||||
$returnCitiesSearchZip = json_decode($this->callApi('get', 'communes', ['nom' => $city, 'fields' => 'nom,codesPostaux'])) ; | |||||
if($returnCitiesSearchZip) { | |||||
foreach($returnCitiesSearchZip as $citySearchZip) { | |||||
if(strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) { | |||||
$zip = $citySearchZip->codesPostaux[0] ; | |||||
} | |||||
} | |||||
} | |||||
return $zip ; | |||||
} | |||||
} |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
class DateUtils | |||||
{ | |||||
public function date($format, $timestamp) | |||||
{ | |||||
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8'); | |||||
return strftime($format, $timestamp) ; | |||||
} | |||||
public function getNextDay($day) | |||||
{ | |||||
return new \DateTime('next '.$day) ; | |||||
} | |||||
public function getNextDayByNumber($number) | |||||
{ | |||||
return $this->getNextDay($this->getDayByNumber($number, 'en')) ; | |||||
} | |||||
public function getDayByNumber($number, $lang = 'fr') | |||||
{ | |||||
if($lang == 'fr') { | |||||
$daysArray = [ | |||||
1 => 'Lundi', | |||||
2 => 'Mardi', | |||||
3 => 'Mercredi', | |||||
4 => 'Jeudi', | |||||
5 => 'Vendredi', | |||||
6 => 'Samedi', | |||||
7 => 'Dimanche' | |||||
] ; | |||||
} | |||||
else { | |||||
$daysArray = [ | |||||
1 => 'Monday', | |||||
2 => 'Tuesday', | |||||
3 => 'Wednesday', | |||||
4 => 'Thursday', | |||||
5 => 'Friday', | |||||
6 => 'Saturday', | |||||
7 => 'Sunday', | |||||
] ; | |||||
} | |||||
if(isset($daysArray[$number])) { | |||||
return $daysArray[$number] ; | |||||
} | |||||
return '' ; | |||||
} | |||||
} |
<?php | <?php | ||||
namespace Lc\ShopBundle\Services; | |||||
namespace Lc\ShopBundle\Services\Order; | |||||
use App\Entity\OrderProductReductionCatalog; | use App\Entity\OrderProductReductionCatalog; | ||||
use App\Entity\OrderShop; | use App\Entity\OrderShop; |
<?php | <?php | ||||
namespace Lc\ShopBundle\Services; | |||||
use App\Entity\OrderProductReductionCatalog; | |||||
use App\Entity\OrderShop; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\DocumentInterface; | |||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||||
use Lc\ShopBundle\Context\OrderPaymentInterface; | |||||
namespace Lc\ShopBundle\Services\Order; | |||||
use Lc\ShopBundle\Context\OrderReductionCartInterface; | use Lc\ShopBundle\Context\OrderReductionCartInterface; | ||||
use Lc\ShopBundle\Context\OrderProductInterface; | |||||
use Lc\ShopBundle\Context\OrderReductionCreditInterface; | use Lc\ShopBundle\Context\OrderReductionCreditInterface; | ||||
use Lc\ShopBundle\Context\OrderShopInterface; | use Lc\ShopBundle\Context\OrderShopInterface; | ||||
use Lc\ShopBundle\Context\OrderStatusHistoryInterface; | |||||
use Lc\ShopBundle\Context\OrderStatusInterface; | |||||
use Lc\ShopBundle\Context\ProductFamilyUtilsInterface; | |||||
use Lc\ShopBundle\Context\ReductionCartInterface; | use Lc\ShopBundle\Context\ReductionCartInterface; | ||||
use Lc\ShopBundle\Context\ReductionCreditInterface; | use Lc\ShopBundle\Context\ReductionCreditInterface; | ||||
use Lc\ShopBundle\Context\UserInterface; | |||||
use Lc\ShopBundle\Model\Document; | |||||
use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType; | |||||
use Lc\ShopBundle\Model\ReductionCart; | |||||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | |||||
use Symfony\Component\Security\Core\Security; | |||||
trait OrderUtilsReductionTrait | trait OrderUtilsReductionTrait | ||||
{ | { |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\PointSaleInterface; | |||||
use Lc\ShopBundle\Context\UserInterface; | |||||
use Lc\ShopBundle\Context\UserPointSaleInterface; | |||||
class PointSaleUtils | |||||
{ | |||||
protected $em ; | |||||
public function __construct(EntityManagerInterface $em) | |||||
{ | |||||
$this->em = $em ; | |||||
} | |||||
public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale) | |||||
{ | |||||
foreach($user->getUserPointSales() as $userPointSale) { | |||||
if($userPointSale->getPointSale()->getId() == $pointSale->getId()) { | |||||
return true ; | |||||
} | |||||
} | |||||
return false ; | |||||
} | |||||
public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale) | |||||
{ | |||||
if(!$this->isUserLinkedToPointSale($user, $pointSale)) { | |||||
$userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName(); | |||||
$userPointSale = new $userPointSaleClass ; | |||||
$userPointSale->setUser($user) ; | |||||
$userPointSale->setPointSale($pointSale) ; | |||||
$this->em->persist($userPointSale); | |||||
$this->em->flush() ; | |||||
} | |||||
} | |||||
} |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
class Price | |||||
{ | |||||
protected $price ; | |||||
protected $taxRate ; | |||||
public function __construct($price, $taxRate) | |||||
{ | |||||
$this->price = $price ; | |||||
$this->taxRate = $taxRate ; | |||||
} | |||||
public function withTax() | |||||
{ | |||||
return self::getPriceWithTax( | |||||
$this->price, | |||||
$this->taxRate | |||||
) ; | |||||
} | |||||
public function withoutTax() | |||||
{ | |||||
return $this->price ; | |||||
} | |||||
/* Static */ | |||||
public static function priceTwoDecimals($number) | |||||
{ | |||||
return number_format(( ($number * 100)) / 100, 2) ; | |||||
} | |||||
public static function getPriceWithoutTax($priceWithTax, $taxRate) | |||||
{ | |||||
return floatval($priceWithTax) / ($taxRate + 1); | |||||
} | |||||
public static function getPriceWithTax($priceWithoutTax, $taxRate) | |||||
{ | |||||
return self::priceTwoDecimals(floatval($priceWithoutTax) * ($taxRate + 1)) ; | |||||
} | |||||
} |
namespace Lc\ShopBundle\Services ; | namespace Lc\ShopBundle\Services ; | ||||
use Lc\ShopBundle\Price\Services\ProductPriceUtils; | |||||
use Lc\ShopBundle\Services\Price\ProductPriceUtils; | |||||
class ProductFamilyUtils | class ProductFamilyUtils | ||||
{ | { |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||||
use Lc\ShopBundle\Context\TaxRateInterface; | |||||
class TaxRateUtils | |||||
{ | |||||
protected $em ; | |||||
protected $merchantUtils ; | |||||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils) | |||||
{ | |||||
$this->em = $em ; | |||||
$this->merchantUtils = $merchantUtils ; | |||||
} | |||||
public function getTaxRatesList() | |||||
{ | |||||
$taxRatesList =array(); | |||||
$taxRates = $this->em->getRepository(TaxRateInterface::class)->findAll(); | |||||
foreach ($taxRates as $taxRate){ | |||||
$taxRatesList[$taxRate->getId()]['title'] = $taxRate->getTitle(); | |||||
$taxRatesList[$taxRate->getId()]['value'] = $taxRate->getValue(); | |||||
} | |||||
$taxRatesList['default']['title'] = $this->merchantUtils->getMerchantCurrentTaxRate()->getTitle(); | |||||
$taxRatesList['default']['value'] = $this->merchantUtils->getMerchantCurrentTaxRate()->getValue(); | |||||
return $taxRatesList; | |||||
} | |||||
} |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\UnitInterface; | |||||
class UnitUtils | |||||
{ | |||||
protected $em ; | |||||
protected $merchantUtils ; | |||||
public function __construct(EntityManagerInterface $em) | |||||
{ | |||||
$this->em = $em ; | |||||
} | |||||
public function getUnitsList() | |||||
{ | |||||
$unitsList =array(); | |||||
$units = $this->em->getRepository(UnitInterface::class)->findAll(); | |||||
foreach ($units as $unit){ | |||||
$unitsList[$unit->getId()]['unit'] = $unit->getUnit(); | |||||
$unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit(); | |||||
$unitsList[$unit->getId()]['wording'] = $unit->getWording(); | |||||
$unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort(); | |||||
$unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient(); | |||||
$unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId(); | |||||
} | |||||
return $unitsList; | |||||
} | |||||
} | |||||
use Doctrine\ORM\EntityManagerInterface; | use Doctrine\ORM\EntityManagerInterface; | ||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | use Lc\ShopBundle\Context\MerchantUtilsInterface; | ||||
use Lc\ShopBundle\Context\PageInterface; | use Lc\ShopBundle\Context\PageInterface; | ||||
use Lc\ShopBundle\Context\PointSaleInterface; | |||||
use Lc\ShopBundle\Context\TaxRateInterface; | use Lc\ShopBundle\Context\TaxRateInterface; | ||||
use Lc\ShopBundle\Context\UnitInterface; | use Lc\ShopBundle\Context\UnitInterface; | ||||
use Lc\ShopBundle\Context\UserInterface; | |||||
use Lc\ShopBundle\Context\UserPointSaleInterface; | |||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||||
use Symfony\Component\HttpFoundation\ParameterBag; | use Symfony\Component\HttpFoundation\ParameterBag; | ||||
{ | { | ||||
protected $em ; | protected $em ; | ||||
protected $parameterBag ; | protected $parameterBag ; | ||||
protected $merchantUtils ; | |||||
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag) | public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag) | ||||
{ | { | ||||
$this->parameterBag = $parameterBag ; | $this->parameterBag = $parameterBag ; | ||||
} | } | ||||
// @todo : À supprimer et passer dans DateUtils (gérer du coup le cas du modèle DeliverySlot qui dépend de cette fonction) | |||||
public static function getDayByNumber($number, $lang = 'fr') | |||||
{ | |||||
if($lang == 'fr') { | |||||
$daysArray = [ | |||||
1 => 'Lundi', | |||||
2 => 'Mardi', | |||||
3 => 'Mercredi', | |||||
4 => 'Jeudi', | |||||
5 => 'Vendredi', | |||||
6 => 'Samedi', | |||||
7 => 'Dimanche' | |||||
] ; | |||||
} | |||||
else { | |||||
$daysArray = [ | |||||
1 => 'Monday', | |||||
2 => 'Tuesday', | |||||
3 => 'Wednesday', | |||||
4 => 'Thursday', | |||||
5 => 'Friday', | |||||
6 => 'Saturday', | |||||
7 => 'Sunday', | |||||
] ; | |||||
} | |||||
if(isset($daysArray[$number])) { | |||||
return $daysArray[$number] ; | |||||
} | |||||
return '' ; | |||||
} | |||||
public function getElementByDevAlias($devAlias, $class = PageInterface::class) | public function getElementByDevAlias($devAlias, $class = PageInterface::class) | ||||
{ | { | ||||
return $slugify->slugify($string) ; | return $slugify->slugify($string) ; | ||||
} | } | ||||
public function getUnitsList() | |||||
{ | |||||
$unitsList =array(); | |||||
$units = $this->em->getRepository(UnitInterface::class)->findAll(); | |||||
foreach ($units as $unit){ | |||||
$unitsList[$unit->getId()]['unit'] = $unit->getUnit(); | |||||
$unitsList[$unit->getId()]['wordingUnit'] = $unit->getWordingUnit(); | |||||
$unitsList[$unit->getId()]['wording'] = $unit->getWording(); | |||||
$unitsList[$unit->getId()]['wordingShort'] = $unit->getWordingShort(); | |||||
$unitsList[$unit->getId()]['coefficient'] = $unit->getCoefficient(); | |||||
$unitsList[$unit->getId()]['unitReference'] = $unit->getUnitReference()->getId(); | |||||
} | |||||
return $unitsList; | |||||
} | |||||
public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale) | |||||
{ | |||||
foreach($user->getUserPointSales() as $userPointSale) { | |||||
if($userPointSale->getPointSale()->getId() == $pointSale->getId()) { | |||||
return true ; | |||||
} | |||||
} | |||||
return false ; | |||||
} | |||||
public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale) | |||||
{ | |||||
if(!$this->isUserLinkedToPointSale($user, $pointSale)) { | |||||
$userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName(); | |||||
$userPointSale = new $userPointSaleClass ; | |||||
$userPointSale->setUser($user) ; | |||||
$userPointSale->setPointSale($pointSale) ; | |||||
$this->em->persist($userPointSale); | |||||
$this->em->flush() ; | |||||
} | |||||
} | |||||
function callCitiesApi($method, $url, $data = false) | |||||
{ | |||||
$url = 'https://geo.api.gouv.fr/'.$url ; | |||||
$curl = curl_init(); | |||||
switch ($method) | |||||
{ | |||||
case "POST": | |||||
curl_setopt($curl, CURLOPT_POST, 1); | |||||
if ($data) | |||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |||||
break; | |||||
case "PUT": | |||||
curl_setopt($curl, CURLOPT_PUT, 1); | |||||
break; | |||||
default: | |||||
if ($data) | |||||
$url = sprintf("%s?%s", $url, http_build_query($data)); | |||||
} | |||||
// Optional Authentication: | |||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |||||
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |||||
curl_setopt($curl, CURLOPT_URL, $url); | |||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |||||
$result = curl_exec($curl); | |||||
curl_close($curl); | |||||
return $result; | |||||
} | |||||
public function getZipByCity($city) | |||||
{ | |||||
$zip = null ; | |||||
$returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', ['nom' => $city, 'fields' => 'nom,codesPostaux'])) ; | |||||
if($returnCitiesSearchZip) { | |||||
foreach($returnCitiesSearchZip as $citySearchZip) { | |||||
if(strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) { | |||||
$zip = $citySearchZip->codesPostaux[0] ; | |||||
} | |||||
} | |||||
} | |||||
return $zip ; | |||||
} | |||||
public function date($format, $timestamp) | |||||
{ | |||||
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8'); | |||||
return strftime($format, $timestamp) ; | |||||
} | |||||
public function getNextDay($day) | |||||
{ | |||||
return new \DateTime('next '.$day) ; | |||||
} | |||||
public function getNextDayByNumber($number) | |||||
{ | |||||
return $this->getNextDay($this->getDayByNumber($number, 'en')) ; | |||||
} | |||||
public function getDayByNumber($number, $lang = 'fr') | |||||
{ | |||||
if($lang == 'fr') { | |||||
$daysArray = [ | |||||
1 => 'Lundi', | |||||
2 => 'Mardi', | |||||
3 => 'Mercredi', | |||||
4 => 'Jeudi', | |||||
5 => 'Vendredi', | |||||
6 => 'Samedi', | |||||
7 => 'Dimanche' | |||||
] ; | |||||
} | |||||
else { | |||||
$daysArray = [ | |||||
1 => 'Monday', | |||||
2 => 'Tuesday', | |||||
3 => 'Wednesday', | |||||
4 => 'Thursday', | |||||
5 => 'Friday', | |||||
6 => 'Saturday', | |||||
7 => 'Sunday', | |||||
] ; | |||||
} | |||||
if(isset($daysArray[$number])) { | |||||
return $daysArray[$number] ; | |||||
} | |||||
return '' ; | |||||
} | |||||
} | } |