vendor/bluue/adelya-bundle/src/EventSubscriber/DiscountCouponSubscriber.php line 99

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\AdelyaBundle\EventSubscriber;
  9. use Bluue\AdelyaBundle\Services\Api;
  10. use Bluue\CashRegisterBundle\Entity\ReceiptLine;
  11. use Bluue\CashRegisterBundle\Event\DiscountCouponEvent;
  12. use Bluue\CashRegisterBundle\Repository\ReceiptLineRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use NumberFormatter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class DiscountCouponSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var Api
  22.      */
  23.     private Api $api;
  24.     /**
  25.      * @var TranslatorInterface
  26.      */
  27.     private TranslatorInterface $tr;
  28.     /**
  29.      * @var RequestStack
  30.      */
  31.     private RequestStack $requestStack;
  32.     /**
  33.      * @var ReceiptLineRepository
  34.      */
  35.     private ReceiptLineRepository $receiptLineRepo;
  36.     /**
  37.      * @param Api $api
  38.      * @param TranslatorInterface $tr
  39.      * @param RequestStack $requestStack
  40.      * @param ReceiptLineRepository $receiptLineRepo
  41.      */
  42.     public function __construct(
  43.         Api $api,
  44.         TranslatorInterface $tr,
  45.         RequestStack $requestStack,
  46.         ReceiptLineRepository $receiptLineRepo
  47.     ) {
  48.         $this->api $api;
  49.         $this->tr $tr;
  50.         $this->requestStack $requestStack;
  51.         $this->receiptLineRepo $receiptLineRepo;
  52.     }
  53.     /**
  54.      * @return array
  55.      */
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             DiscountCouponEvent::LIST_COUPONS => 'listCoupons',
  60.             DiscountCouponEvent::CHECK_COUPON => 'checkCoupon',
  61.             DiscountCouponEvent::APPLY_COUPON => 'applyCoupon'
  62.         ];
  63.     }
  64.     /**
  65.      * @param DiscountCouponEvent $event
  66.      * @return void
  67.      */
  68.     public function listCoupons(DiscountCouponEvent $event): void
  69.     {
  70.         $receipt $event->getReceipt();
  71.         if (!($customer $receipt->getCustomer())) {
  72.             return;
  73.         }
  74.         $options $customer->getOptions();
  75.         $memberId $options['adelya_bundle']['id'] ?? null;
  76.         if ($memberId) {
  77.             $event->addList($this->api->listCoupons($memberId));
  78.         }
  79.     }
  80.     /**
  81.      * @param DiscountCouponEvent $event
  82.      * @return void
  83.      */
  84.     public function checkCoupon(DiscountCouponEvent $event): void
  85.     {
  86.         if ($coupon $this->api->checkCoupon($event->getCode())) {
  87.             $event->addList([$coupon]);
  88.         }
  89.     }
  90.     /**
  91.      * @param DiscountCouponEvent $event
  92.      * @return void
  93.      */
  94.     public function applyCoupon(DiscountCouponEvent $event): void
  95.     {
  96.         if ($event->getBundle() !== 'adelya' || !$coupon $this->api->getCoupon($event->getId())) {
  97.             return;
  98.         }
  99.         $coupon current($coupon);
  100.         $discountType 'percent';
  101.         $amount = (float) $coupon['fvalue'];
  102.         if ($campaign $this->api->getCampaign($coupon['campaign'] ?? '')) {
  103.             $campaign current($campaign);
  104.             $discountType $campaign['couponUnit'] ?? $discountType;
  105.             $amount $amount ?: (float) $campaign['couponValue'];
  106.         }
  107.         if (!$amount || !in_array($discountType, ['percent''discount''addCA'])) {
  108.             return;
  109.         }
  110.         $receipt $event->getReceipt();
  111.         if ($discountType == 'percent') {
  112.             /** @var ReceiptLine $line */
  113.             foreach ($receipt->getLines() as $line) {
  114.                 if ((int) $line->getQuantity() < || !$line->isEditable()) {
  115.                     continue;
  116.                 }
  117.                 $newPercentage = (float) $line->getDiscountPercentage() + $amount;
  118.                 $line->setDiscountPercentage((string) $newPercentage);
  119.                 $line->setTotalDiscountUntaxed(
  120.                     number_format(
  121.                         $newPercentage * (float) $line->getTotalAmountUntaxed(true) / 100,
  122.                         6,
  123.                         '.',
  124.                         ''
  125.                     )
  126.                 );
  127.                 $line->setTotalDiscount(
  128.                     number_format(
  129.                         $newPercentage * (float) $line->getTotalAmount(true) / 100,
  130.                         6,
  131.                         '.',
  132.                         ''
  133.                     )
  134.                 );
  135.                 $this->receiptLineRepo->calc($line);
  136.             }
  137.             $receiptLine = (new ReceiptLine())
  138.                 ->setName($this->tr->trans('Discount coupon', [], 'AdelyaBundle') . ' ' $coupon['trackCode'])
  139.                 ->setQuantity('-1')
  140.                 ->setUnitPriceUntaxed('0')
  141.                 ->setUnitPrice('0')
  142.                 ->setOptions([
  143.                     'discountCoupon' => $coupon['id'],
  144.                     'trackcode' => $coupon['trackCode'],
  145.                     'adelya_bundle' => true
  146.                 ])
  147.             ;
  148.         } else {
  149.             $numberFormatter NumberFormatter::create(
  150.                 $this->requestStack->getSession()->get('_locale''fr'),
  151.                 NumberFormatter::CURRENCY
  152.             );
  153.             $discountValue $numberFormatter->formatCurrency($amount$receipt->getCurrency()->getIsoCode());
  154.             $amount = (string) ($amount * -1);
  155.             $receiptLine = (new ReceiptLine())
  156.                 ->setName($this->tr->trans('value discount', ['value' => $discountValue], 'AdelyaBundle'))
  157.                 ->setQuantity('1')
  158.                 ->setUnitPriceUntaxed($amount)
  159.                 ->setUnitPrice($amount)
  160.                 ->setOptions([
  161.                     'discountCoupon' => $coupon['id'],
  162.                     'trackcode' => $coupon['trackCode'],
  163.                     'adelya_bundle' => true
  164.                 ])
  165.             ;
  166.         }
  167.         $receipt->addLine($receiptLine);
  168.         $this->receiptLineRepo->calc($receiptLine);
  169.     }
  170. }