Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CreditHistoryBuilder.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. class CreditHistoryBuilder
  10. {
  11. protected CreditHistoryFactory $creditHistoryFactory;
  12. protected UserMerchantStore $userMerchantStore;
  13. protected UserMerchantBuilder $userMerchantBuilder;
  14. public function __construct(
  15. CreditHistoryFactory $creditHistoryFactory,
  16. UserMerchantStore $userMerchantStore,
  17. UserMerchantBuilder $userMerchantBuilder
  18. ) {
  19. $this->creditHistoryFactory = $creditHistoryFactory;
  20. $this->userMerchantStore = $userMerchantStore;
  21. $this->userMerchantBuilder = $userMerchantBuilder;
  22. }
  23. public function create(
  24. string $type,
  25. UserMerchantInterface $userMerchant,
  26. array $params = []
  27. ): CreditHistoryInterface {
  28. $creditHistory = $this->creditHistoryFactory->create($type, $userMerchant);
  29. $amount = isset($params['amount']) ?? null ;
  30. $meanPayment = isset($params['meanPayment']) ?? null ;
  31. $reference = isset($params['reference']) ?? null ;
  32. $comment = isset($params['comment']) ?? null ;
  33. $orderPayment = isset($params['orderPayment']) ?? null ;
  34. $orderRefund = isset($params['orderRefund']) ?? null ;
  35. $creditHistory->setType($type);
  36. $creditHistory->setUserMerchant($userMerchant);
  37. $creditHistory->setAmount($amount);
  38. $creditHistory->setMeanPayment($meanPayment);
  39. $creditHistory->setReference($reference);
  40. $creditHistory->setComment($comment);
  41. $creditHistory->setOrderPayment($orderPayment);
  42. $creditHistory->setOrderRefund($orderRefund);
  43. return $creditHistory;
  44. }
  45. // saveCreditHistory
  46. public function save(CreditHistoryInterface $creditHistory): bool
  47. {
  48. if ($creditHistory) {
  49. $userMerchant = $creditHistory->getUserMerchant();
  50. $isCreditActive = $this->userMerchantStore->isCreditActive($userMerchant);
  51. if ($isCreditActive) {
  52. if ($creditHistory->getType() == CreditHistoryModel::TYPE_CREDIT) {
  53. $userMerchantAmount = $userMerchant->getCredit() + $creditHistory->getAmountInherited();
  54. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  55. return true;
  56. } elseif ($creditHistory->getType() == CreditHistoryModel::TYPE_DEBIT) {
  57. if ($this->userMerchantStore->isCreditSufficientToPay(
  58. $userMerchant,
  59. $creditHistory->getAmountInherited()
  60. )) {
  61. $userMerchantAmount = $userMerchant->getCredit() - $creditHistory->getAmountInherited();
  62. $this->userMerchantBuilder->updateCredit($userMerchant, $creditHistory, $userMerchantAmount);
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. }