|
- <?php
-
- namespace Lc\ShopBundle\Services\Order;
-
- use Lc\ShopBundle\Context\OrderReductionCartInterface;
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\ReductionCartInterface;
- use Lc\ShopBundle\Context\ReductionCreditInterface;
- use Lc\ShopBundle\Model\ReductionCredit;
- use function Matrix\trace;
-
- trait OrderUtilsReductionTrait
- {
- public function getReductionCartByCode($code){
- $reductionCartRepository = $this->em->getRepository(ReductionCartInterface::class);
-
- $reductionCarts = $reductionCartRepository->findByCode($code);
- $findReductionCart = null;
- foreach ($reductionCarts as $reductionCart) {
- if ($reductionCart && in_array($code, $reductionCart->getCodes())) {
- $findReductionCart = $reductionCart;
- }
- }
- return $findReductionCart;
- }
-
- public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2)
- {
- return (!$orderProductReductionCatalog1 && !$orderProductReductionCatalog2)
- || ($orderProductReductionCatalog1
- && $orderProductReductionCatalog2
- && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit()
- && (string)$orderProductReductionCatalog1->getValue() == (string)$orderProductReductionCatalog2->getValue()
- && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate());
- }
-
-
- public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog)
- {
- $text = '';
-
- if ($orderProductReductionCatalog) {
- if ($orderProductReductionCatalog->getUnit() == 'amount') {
- $text .= '- ' . $orderProductReductionCatalog->getValue() . ' €';
- }
-
- if ($orderProductReductionCatalog->getUnit() == 'percent') {
- $text .= '- ' . $orderProductReductionCatalog->getValue() . ' %';
- }
- }
-
- return $text;
- }
-
-
- public function createOrderReductionCart(OrderShopInterface $orderShop, ReductionCartInterface $reductionCart, $code = null)
- {
- $orderReductionCartClass = $this->em->getClassMetadata(OrderReductionCartInterface::class);
- $orderReductionCart = new $orderReductionCartClass->name;
-
- $orderReductionCart->setOrderShop($orderShop);
- $orderReductionCart->setReductionCart($reductionCart);
-
- $orderReductionCart->setTitle($reductionCart->getTitle());
- $orderReductionCart->setValue($reductionCart->getValue());
- $orderReductionCart->setUnit($reductionCart->getUnit());
- $orderReductionCart->setCodeUsed($code);
- $orderReductionCart->setBehaviorTaxRate($reductionCart->getBehaviorTaxRate());
- $orderReductionCart->setFreeShipping($reductionCart->getFreeShipping());
- $orderReductionCart->setAppliedTo($reductionCart->getAppliedTo());
- $orderReductionCart->setType($reductionCart->getType());
-
- $orderShop->addOrderReductionCart($orderReductionCart) ;
-
- if($this->isOrderShopPositiveAmount($orderShop)
- && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {
-
- $this->em->persist($orderReductionCart);
- $this->em->flush();
-
- return $orderReductionCart ;
- }
- else {
- $orderShop->removeOrderReductionCart($orderReductionCart) ;
- return false;
- }
- }
-
- public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
- {
- $user = $orderShop->getUser() ;
-
- // appartient à l'utilisateur
- if(!$reductionCredit->getUsers()->contains($user)) {
- $this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
- return false ;
- }
-
- // n'a pas été utilisé
- if($reductionCredit->getType()== ReductionCredit::TYPE_CREDIT){
- if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
- $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
- }else{
- if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit) > 0) {
- $this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
-
- }
-
- return true;
- }
-
- public function getReductionCartRemainingQuantity($reductionCart) :float
- {
- return $reductionCart->getAvailableQuantity() - $this->orderShopRepo->countValidOrderWithReductionCart($reductionCart);
- }
-
- public function getReductionCartUsedQuantityPerUser($reductionCart, $user) :float
- {
- return $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
- }
- public function getReductionCartUsedQuantity($reductionCart) :float
- {
- return $this->orderShopRepo->countValidOrderWithReductionCart($reductionCart);
- }
- public function getReductionCartRemainingQuantityPerUser($reductionCart, $user) :float
- {
- if ($reductionCart->getAvailableQuantityPerUser()) {
- return $reductionCart->getAvailableQuantityPerUser() - $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
- }
-
- return false;
- }
-
-
- public function createOrderReductionCredit(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
- {
- $orderReductionCreditClass = $this->em->getClassMetadata(OrderReductionCreditInterface::class);
- $orderReductionCredit = new $orderReductionCreditClass->name;
-
- $orderReductionCredit->setOrderShop($orderShop);
- $orderReductionCredit->setReductionCredit($reductionCredit);
-
- $orderReductionCredit->setTitle($reductionCredit->getTitle());
- $orderReductionCredit->setValue($reductionCredit->getValue());
- $orderReductionCredit->setUnit($reductionCredit->getUnit());
- $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate());
- $orderReductionCredit->setType($reductionCredit->getType());
-
- $orderShop->addOrderReductionCredit($orderReductionCredit) ;
-
- if($this->isOrderShopPositiveAmount($orderShop)
- && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {
-
- $this->em->persist($orderReductionCredit);
- $this->em->flush();
-
- return $orderReductionCredit;
- }
- else {
- $orderShop->removeOrderReductionCredit($orderReductionCredit) ;
-
- return false;
- }
- }
-
-
- public function getReductionCartsAvailableByUser($user)
- {
- $reductionCartRepository = $this->em->getRepository(ReductionCartInterface::class) ;
- return $reductionCartRepository->findAllAvailableForUser($user);
- }
-
- public function getReductionCreditsAvailableByUser($user)
- {
- $reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;
-
- $reductionCreditsArray = [] ;
- foreach($reductionCredits as $reductionCredit) {
- if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
- $reductionCreditsArray[] = $reductionCredit ;
- }
- }
-
- return $reductionCreditsArray ;
- }
-
- public function getReductionGiftsAvailableByUser($user)
- {
- $reductionGifts = $this->reductionCreditRepo->findReductionGiftToUseByUser($user) ;
-
- $reductionGiftsArray = [] ;
- foreach($reductionGifts as $reductionGift) {
- if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
- $reductionGiftsArray[] = $reductionGift ;
- }
- }
-
- return $reductionGiftsArray ;
- }
-
- public function isReductionGiftUsed($reductionGift){
- if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionGift)) {
- return true;
- }else{
- return false;
- }
- }
-
- public function isReductionCreditUsed($reductionCredit, $user = false){
- if($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
- return true;
- }else{
- return false;
- }
- }
-
- public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
- {
- foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- return true ;
- }
- }
-
- return false ;
- }
-
- }
|