|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
-
- namespace Lc\CaracoleBundle\Builder\Credit;
-
- use Lc\CaracoleBundle\Builder\User\UserMerchantBuilder;
- use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
- use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
- use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
-
- class CreditHistoryBuilder
- {
- protected CreditHistoryFactory $creditHistoryFactory;
- protected UserMerchantStore $userMerchantStore;
- protected UserMerchantBuilder $userMerchantBuilder;
-
- public function __construct(
- CreditHistoryFactory $creditHistoryFactory,
- UserMerchantStore $userMerchantStore,
- UserMerchantBuilder $userMerchantBuilder
- ) {
- $this->creditHistoryFactory = $creditHistoryFactory;
- $this->userMerchantStore = $userMerchantStore;
- $this->userMerchantBuilder = $userMerchantBuilder;
- }
-
- public function create(
- string $type,
- UserMerchantInterface $userMerchant,
- array $params = []
- ): CreditHistoryInterface {
- $creditHistory = $this->creditHistoryFactory->create($type, $userMerchant);
-
- $amount = isset($params['amount']) ?? null ;
- $meanPayment = isset($params['meanPayment']) ?? null ;
- $reference = isset($params['reference']) ?? null ;
- $comment = isset($params['comment']) ?? null ;
- $orderPayment = isset($params['orderPayment']) ?? null ;
- $orderRefund = isset($params['orderRefund']) ?? null ;
-
- $creditHistory->setType($type);
- $creditHistory->setUserMerchant($userMerchant);
- $creditHistory->setAmount($amount);
- $creditHistory->setMeanPayment($meanPayment);
- $creditHistory->setReference($reference);
- $creditHistory->setComment($comment);
- $creditHistory->setOrderPayment($orderPayment);
- $creditHistory->setOrderRefund($orderRefund);
-
- return $creditHistory;
- }
-
- // saveCreditHistory
- public function save(CreditHistoryInterface $creditHistory): bool
- {
- if ($creditHistory) {
- $userMerchant = $creditHistory->getUserMerchant();
- $isCreditActive = $this->userMerchantStore->isCreditActive($userMerchant);
-
- if ($isCreditActive) {
- if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
- $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited();
- $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
- return true;
- } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
- if ($this->userMerchantStore->isCreditSufficientToPay(
- $userMerchant,
- $creditHistory->getAmountInherited()
- )) {
- $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited();
- $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
- return true;
- } else {
- return false;
- }
- }
- }
- }
-
- return false;
- }
-
- }
|