You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\Credit;
  3. use Lc\CaracoleBundle\Builder\User\UserMerchantBuilder;
  4. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  5. use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
  6. use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
  7. class CreditHistoryBuilder
  8. {
  9. protected UserMerchantStore $userMerchantStore;
  10. protected UserMerchantBuilder $userMerchantBuilder;
  11. public function __construct(
  12. UserMerchantStore $userMerchantStore,
  13. UserMerchantBuilder $userMerchantBuilder
  14. ) {
  15. $this->userMerchantStore = $userMerchantStore;
  16. $this->userMerchantBuilder = $userMerchantBuilder;
  17. }
  18. // saveCreditHistory
  19. public function save(CreditHistoryInterface $creditHistory) :bool
  20. {
  21. if ($creditHistory) {
  22. $userMerchant = $creditHistory->getUserMerchant();
  23. $isCreditActive = $this->userMerchantStore->isCreditActive($userMerchant);
  24. if ($isCreditActive) {
  25. if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
  26. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited();
  27. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  28. return true;
  29. } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
  30. if ($this->userMerchantStore->isCreditSufficientToPay($userMerchant, $creditHistory->getAmountInherited())) {
  31. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited();
  32. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. }
  39. }
  40. return false;
  41. }
  42. }