No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

CreditHistoryBuilder.php 3.8KB

hace 3 años
hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\Credit;
  3. use Lc\CaracoleBundle\Builder\User\UserMerchantBuilder;
  4. use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory;
  5. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  6. use Lc\CaracoleBundle\Model\Credit\CreditHistoryModel;
  7. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  8. use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
  9. use Lc\CaracoleBundle\Solver\Credit\CreditHistorySolver;
  10. use Lc\CaracoleBundle\Solver\User\UserMerchantSolver;
  11. class CreditHistoryBuilder
  12. {
  13. protected CreditHistoryFactory $creditHistoryFactory;
  14. protected CreditHistorySolver $creditHistorySolver;
  15. protected UserMerchantSolver $userMerchantSolver;
  16. protected UserMerchantStore $userMerchantStore;
  17. protected UserMerchantBuilder $userMerchantBuilder;
  18. public function __construct(
  19. CreditHistoryFactory $creditHistoryFactory,
  20. CreditHistorySolver $creditHistorySolver,
  21. UserMerchantStore $userMerchantStore,
  22. UserMerchantBuilder $userMerchantBuilder,
  23. UserMerchantSolver $userMerchantSolver
  24. ) {
  25. $this->creditHistoryFactory = $creditHistoryFactory;
  26. $this->creditHistorySolver = $creditHistorySolver;
  27. $this->userMerchantStore = $userMerchantStore;
  28. $this->userMerchantBuilder = $userMerchantBuilder;
  29. $this->userMerchantSolver = $userMerchantSolver;
  30. }
  31. public function create(
  32. string $type,
  33. UserMerchantInterface $userMerchant,
  34. array $params = []
  35. ): CreditHistoryInterface {
  36. $creditHistory = $this->creditHistoryFactory->create($type, $userMerchant);
  37. $amount = isset($params['amount']) ? $params['amount'] : null ;
  38. $meanPayment = isset($params['meanPayment']) ? $params['meanPayment'] : null ;
  39. $reference = isset($params['reference']) ? $params['reference'] : null ;
  40. $comment = isset($params['comment']) ? $params['comment'] : null ;
  41. $orderPayment = isset($params['orderPayment']) ? $params['orderPayment'] : null ;
  42. $orderRefund = isset($params['orderRefund']) ? $params['orderRefund'] : null ;
  43. $creditHistory->setType($type);
  44. $creditHistory->setUserMerchant($userMerchant);
  45. $creditHistory->setAmount($amount);
  46. $creditHistory->setMeanPayment($meanPayment);
  47. $creditHistory->setReference($reference);
  48. $creditHistory->setComment($comment);
  49. $creditHistory->setOrderPayment($orderPayment);
  50. $creditHistory->setOrderRefund($orderRefund);
  51. $this->save($creditHistory);
  52. return $creditHistory;
  53. }
  54. // saveCreditHistory
  55. public function save(CreditHistoryInterface $creditHistory): bool
  56. {
  57. $amount = $this->creditHistorySolver->getAmountInherited($creditHistory);
  58. $userMerchant = $creditHistory->getUserMerchant();
  59. $isCreditActive = $this->userMerchantSolver->isCreditActive($userMerchant);
  60. if ($isCreditActive) {
  61. if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
  62. $userMerchantAmount = $userMerchant->getCredit() + $amount;
  63. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  64. return true;
  65. } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
  66. if ($this->userMerchantSolver->isCreditSufficientToPay(
  67. $userMerchant,
  68. $amount
  69. )) {
  70. $userMerchantAmount = $userMerchant->getCredit() - $amount;
  71. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. }