|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Reduction;
-
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCreditModel;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Repository\AbstractStore;
-
- class ReductionCreditStore extends AbstractStore
- {
- protected ReductionCreditRepositoryQuery $query;
- protected OrderShopStore $orderShopStore;
-
- public function __construct(
- ReductionCreditRepositoryQuery $query,
- OrderShopStore $orderShopStore
- ) {
- $this->query = $query;
- $this->orderShopStore = $orderShopStore;
- }
-
- // isReductionCreditAllowAddToOrder
- public function isAllowAddToOrder(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
- {
- $user = $orderShop->getUser();
-
- // appartient à l'utilisateur
- if (!$reductionCredit->getUsers()->contains($user)) {
- // @TODO : gérer les addFlash dans le controleur
- //$this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
- return false;
- }
-
- // n'a pas été utilisé
- if ($reductionCredit->getType() == ReductionCredit::TYPE_CREDIT) {
- if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
- //$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
- } else {
- if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit) > 0) {
- //$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
- return false;
- }
- }
-
- return true;
- }
-
- // findReductionCreditsByUser
- public function getByTypeAndUser(string $type = ReductionCreditModel::TYPE_CREDIT, UserInterface $user)
- {
- $query = $this->query->create();
-
- $query
- ->filterStatusOnline()
- ->filterByType($type)
- ->filterByUser($user);
-
- return $query->find();
- }
-
- public function getReductionCreditsAvailableByUser(UserInterface $user)
- {
- $reductionCredits = $this->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user);
-
- $reductionCreditsArray = [];
- foreach ($reductionCredits as $reductionCredit) {
- if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
- $reductionCreditsArray[] = $reductionCredit;
- }
- }
-
- return $reductionCreditsArray;
- }
-
- public function getReductionGiftsAvailableByUser($user)
- {
- $reductionGifts = $this->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user);
-
- $reductionGiftsArray = [];
- foreach ($reductionGifts as $reductionGift) {
- if (!$this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
- $reductionGiftsArray[] = $reductionGift;
- }
- }
-
- return $reductionGiftsArray;
- }
-
- public function isReductionGiftUsed(ReductionCreditInterface $reductionGift)
- {
- if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionGift)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isReductionCreditUsed(ReductionCreditInterface $reductionCredit, UserInterface $user = null)
- {
- if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isReductionCreditAddedToOrder(
- OrderShopInterface $orderShop,
- ReductionCreditInterface $reductionCredit
- ) {
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
- return true;
- }
- }
-
- return false;
- }
- }
|