vendor/bluue/adelya-bundle/src/EventSubscriber/CustomerFidelitySubscriber.php line 107

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\CustomerFidelityEvent;
  12. use Bluue\CashRegisterBundle\Repository\ReceiptLineRepository;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class CustomerFidelitySubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var Api
  19.      */
  20.     private Api $api;
  21.     /**
  22.      * @var TranslatorInterface
  23.      */
  24.     private TranslatorInterface $tr;
  25.     /**
  26.      * @var ReceiptLineRepository
  27.      */
  28.     private ReceiptLineRepository $receiptLineRepo;
  29.     /**
  30.      * @param Api $api
  31.      * @param TranslatorInterface $tr
  32.      * @param ReceiptLineRepository $receiptLineRepo
  33.      */
  34.     public function __construct(Api $apiTranslatorInterface $trReceiptLineRepository $receiptLineRepo)
  35.     {
  36.         $this->api $api;
  37.         $this->tr $tr;
  38.         $this->receiptLineRepo $receiptLineRepo;
  39.     }
  40.     /**
  41.      * @return array
  42.      */
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             CustomerFidelityEvent::CHECK_CARD => 'checkCard',
  47.             CustomerFidelityEvent::GET_INFOS => 'getInfos',
  48.             CustomerFidelityEvent::USE_CREDITS => 'useCredits'
  49.         ];
  50.     }
  51.     /**
  52.      * @param CustomerFidelityEvent $event
  53.      * @return void
  54.      */
  55.     public function checkCard(CustomerFidelityEvent $event): void
  56.     {
  57.         $infos $event->getInfos();
  58.         if (!empty($infos['message']) || empty($infos['cardNumber'])) {
  59.             return;
  60.         }
  61.         $cardNumber $infos['cardNumber'];
  62.         $customerInfos $this->api->getCustomer($event->getCustomer());
  63.         if (!empty($customerInfos['cardNumber']) && $customerInfos['cardNumber'] == $cardNumber) {
  64.             return;
  65.         }
  66.         $fidelityCard $this->api->getFidelityCard($cardNumber);
  67.         if (!empty($fidelityCard['FidelityCard']) && !empty($fidelityCard['FidelityCard']['status'])) {
  68.             switch ($fidelityCard['FidelityCard']['status']) {
  69.                 case 'DISABLED':
  70.                     $infos['message'] = $this->tr->trans('Card disabled', [], 'AdelyaBundle');
  71.                     break;
  72.                 case 'USED':
  73.                 case 'ENABLED':
  74.                 case 'SOLD':
  75.                     $infos['message'] = $this->tr->trans('Card already used', [], 'AdelyaBundle');
  76.                     break;
  77.                 case 'EXPIRED':
  78.                     $infos['message'] = $this->tr->trans('Card expired', [], 'AdelyaBundle');
  79.                     break;
  80.                 case 'DELETED':
  81.                     $infos['message'] = $this->tr->trans('Card deleted', [], 'AdelyaBundle');
  82.                     break;
  83.             }
  84.         } else {
  85.             $infos['message'] = $this->tr->trans('Unknown card', [], 'AdelyaBundle');
  86.         }
  87.         $event->setInfos($infos);
  88.     }
  89.     /**
  90.      * @param CustomerFidelityEvent $event
  91.      * @return void
  92.      */
  93.     public function getInfos(CustomerFidelityEvent $event): void
  94.     {
  95.         if (!empty($event->getInfos())) {
  96.             return;
  97.         }
  98.         if ($customerInfos $this->api->getCustomer($event->getCustomer())) {
  99.             $event->setInfos([
  100.                 'points' => $customerInfos['CompactFidelityMember']['nbPoint'] ?? 0,
  101.                 'credits' => $customerInfos['CompactFidelityMember']['nbCredit'] ?? 0,
  102.                 'bundle' => 'adelya'
  103.             ]);
  104.         }
  105.     }
  106.     /**
  107.      * @param CustomerFidelityEvent $event
  108.      * @return void
  109.      */
  110.     public function useCredits(CustomerFidelityEvent $event): void
  111.     {
  112.         $receipt $event->getReceipt();
  113.         $totalAmount = (float) $receipt->getTotalAmount();
  114.         $infos $event->getInfos();
  115.         $amount $infos['credits'];
  116.         if ($infos['bundle'] !== 'adelya' || !$amount || $totalAmount <= 0) {
  117.             return;
  118.         }
  119.         if ($amount $totalAmount) {
  120.             $amount $totalAmount;
  121.         }
  122.         $amount = (string) ($amount * -1);
  123.         $receiptLine = (new ReceiptLine())
  124.             ->setName($this->tr->trans('Fidelity points', [], 'AdelyaBundle'))
  125.             ->setQuantity('1')
  126.             ->setUnitPriceUntaxed($amount)
  127.             ->setUnitPrice($amount)
  128.             ->setOptions(['fidelityPoints' => true'adelya_bundle' => true])
  129.         ;
  130.         $receipt->addLine($receiptLine);
  131.         $this->receiptLineRepo->calc($receiptLine);
  132.     }
  133. }