<?php
/**
* @author Léo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\AdelyaBundle\EventSubscriber;
use Bluue\AdelyaBundle\Services\Api;
use Bluue\CashRegisterBundle\Entity\ReceiptLine;
use Bluue\CashRegisterBundle\Event\CustomerFidelityEvent;
use Bluue\CashRegisterBundle\Repository\ReceiptLineRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CustomerFidelitySubscriber implements EventSubscriberInterface
{
/**
* @var Api
*/
private Api $api;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var ReceiptLineRepository
*/
private ReceiptLineRepository $receiptLineRepo;
/**
* @param Api $api
* @param TranslatorInterface $tr
* @param ReceiptLineRepository $receiptLineRepo
*/
public function __construct(Api $api, TranslatorInterface $tr, ReceiptLineRepository $receiptLineRepo)
{
$this->api = $api;
$this->tr = $tr;
$this->receiptLineRepo = $receiptLineRepo;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
CustomerFidelityEvent::CHECK_CARD => 'checkCard',
CustomerFidelityEvent::GET_INFOS => 'getInfos',
CustomerFidelityEvent::USE_CREDITS => 'useCredits'
];
}
/**
* @param CustomerFidelityEvent $event
* @return void
*/
public function checkCard(CustomerFidelityEvent $event): void
{
$infos = $event->getInfos();
if (!empty($infos['message']) || empty($infos['cardNumber'])) {
return;
}
$cardNumber = $infos['cardNumber'];
$customerInfos = $this->api->getCustomer($event->getCustomer());
if (!empty($customerInfos['cardNumber']) && $customerInfos['cardNumber'] == $cardNumber) {
return;
}
$fidelityCard = $this->api->getFidelityCard($cardNumber);
if (!empty($fidelityCard['FidelityCard']) && !empty($fidelityCard['FidelityCard']['status'])) {
switch ($fidelityCard['FidelityCard']['status']) {
case 'DISABLED':
$infos['message'] = $this->tr->trans('Card disabled', [], 'AdelyaBundle');
break;
case 'USED':
case 'ENABLED':
case 'SOLD':
$infos['message'] = $this->tr->trans('Card already used', [], 'AdelyaBundle');
break;
case 'EXPIRED':
$infos['message'] = $this->tr->trans('Card expired', [], 'AdelyaBundle');
break;
case 'DELETED':
$infos['message'] = $this->tr->trans('Card deleted', [], 'AdelyaBundle');
break;
}
} else {
$infos['message'] = $this->tr->trans('Unknown card', [], 'AdelyaBundle');
}
$event->setInfos($infos);
}
/**
* @param CustomerFidelityEvent $event
* @return void
*/
public function getInfos(CustomerFidelityEvent $event): void
{
if (!empty($event->getInfos())) {
return;
}
if ($customerInfos = $this->api->getCustomer($event->getCustomer())) {
$event->setInfos([
'points' => $customerInfos['CompactFidelityMember']['nbPoint'] ?? 0,
'credits' => $customerInfos['CompactFidelityMember']['nbCredit'] ?? 0,
'bundle' => 'adelya'
]);
}
}
/**
* @param CustomerFidelityEvent $event
* @return void
*/
public function useCredits(CustomerFidelityEvent $event): void
{
$receipt = $event->getReceipt();
$totalAmount = (float) $receipt->getTotalAmount();
$infos = $event->getInfos();
$amount = $infos['credits'];
if ($infos['bundle'] !== 'adelya' || !$amount || $totalAmount <= 0) {
return;
}
if ($amount > $totalAmount) {
$amount = $totalAmount;
}
$amount = (string) ($amount * -1);
$receiptLine = (new ReceiptLine())
->setName($this->tr->trans('Fidelity points', [], 'AdelyaBundle'))
->setQuantity('1')
->setUnitPriceUntaxed($amount)
->setUnitPrice($amount)
->setOptions(['fidelityPoints' => true, 'adelya_bundle' => true])
;
$receipt->addLine($receiptLine);
$this->receiptLineRepo->calc($receiptLine);
}
}