|
- <?php
-
- namespace Lc\CaracoleBundle\Builder\Credit;
-
- use Lc\CaracoleBundle\Builder\User\UserMerchantBuilder;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
- use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
- use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
-
- class CreditHistoryBuilder
- {
- protected UserMerchantStore $userMerchantStore;
- protected UserMerchantBuilder $userMerchantBuilder;
-
- public function __construct(
- UserMerchantStore $userMerchantStore,
- UserMerchantBuilder $userMerchantBuilder
- ) {
- $this->userMerchantStore = $userMerchantStore;
- $this->userMerchantBuilder = $userMerchantBuilder;
- }
-
- // 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;
- }
-
- }
|