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; } }