|
- <?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;
- use Lc\CaracoleBundle\Solver\Credit\CreditHistorySolver;
- use Lc\CaracoleBundle\Solver\User\UserMerchantSolver;
-
- class CreditHistoryBuilder
- {
- protected CreditHistoryFactory $creditHistoryFactory;
- protected CreditHistorySolver $creditHistorySolver;
- protected UserMerchantSolver $userMerchantSolver;
- protected UserMerchantStore $userMerchantStore;
- protected UserMerchantBuilder $userMerchantBuilder;
-
- public function __construct(
- CreditHistoryFactory $creditHistoryFactory,
- CreditHistorySolver $creditHistorySolver,
- UserMerchantStore $userMerchantStore,
- UserMerchantBuilder $userMerchantBuilder,
- UserMerchantSolver $userMerchantSolver
- ) {
- $this->creditHistoryFactory = $creditHistoryFactory;
- $this->creditHistorySolver = $creditHistorySolver;
- $this->userMerchantStore = $userMerchantStore;
- $this->userMerchantBuilder = $userMerchantBuilder;
- $this->userMerchantSolver = $userMerchantSolver;
- }
-
- public function create(
- string $type,
- UserMerchantInterface $userMerchant,
- array $params = []
- ): CreditHistoryInterface {
- $creditHistory = $this->creditHistoryFactory->create($type, $userMerchant);
-
- $amount = isset($params['amount']) ? $params['amount'] : null ;
- $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
- $reference = isset($params['reference']) ? $params['reference'] : null ;
- $comment = isset($params['comment']) ? $params['comment'] : null ;
- $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
- $orderRefund = isset($params['orderRefund']) ? $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);
-
- $this->save($creditHistory);
-
- return $creditHistory;
- }
-
- // saveCreditHistory
- public function save(CreditHistoryInterface $creditHistory): bool
- {
- $amount = $this->creditHistorySolver->getAmountInherited($creditHistory);
- $userMerchant = $creditHistory->getUserMerchant();
- $isCreditActive = $this->userMerchantSolver->isCreditActive($userMerchant);
-
- if ($isCreditActive) {
- if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
- $userMerchantAmount = $userMerchant->getCredit() + $amount;
- $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
- return true;
- } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
- if ($this->userMerchantSolver->isCreditSufficientToPay(
- $userMerchant,
- $amount
- )) {
- $userMerchantAmount = $userMerchant->getCredit() - $amount;
- $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
- return true;
- } else {
- return false;
- }
- }
- }
-
- return false;
- }
-
- }
|