|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?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']) ?? 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
- {
- $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;
- }
-
- }
|