選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CreditHistoryBuilder.php 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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']) ?? null ;
  38. $meanPayment = isset($params['meanPayment']) ?? null ;
  39. $reference = isset($params['reference']) ?? null ;
  40. $comment = isset($params['comment']) ?? null ;
  41. $orderPayment = isset($params['orderPayment']) ?? null ;
  42. $orderRefund = isset($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. return $creditHistory;
  52. }
  53. // saveCreditHistory
  54. public function save(CreditHistoryInterface $creditHistory): bool
  55. {
  56. $amount = $this->creditHistorySolver->getAmountInherited($creditHistory);
  57. $userMerchant = $creditHistory->getUserMerchant();
  58. $isCreditActive = $this->userMerchantSolver->isCreditActive($userMerchant);
  59. if ($isCreditActive) {
  60. if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
  61. $userMerchantAmount = $userMerchant->getCredit() + $amount;
  62. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  63. return true;
  64. } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
  65. if ($this->userMerchantSolver->isCreditSufficientToPay(
  66. $userMerchant,
  67. $amount
  68. )) {
  69. $userMerchantAmount = $userMerchant->getCredit() - $amount;
  70. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. }