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.

79 lines
2.4KB

  1. <?php
  2. namespace common\logic\User\CreditHistory\Service;
  3. use common\logic\AbstractBuilder;
  4. use common\logic\Order\Order\Model\Order;
  5. use common\logic\Order\Order\Service\OrderSolver;
  6. use common\logic\Producer\Producer\Model\Producer;
  7. use common\logic\User\CreditHistory\Event\CreditHistoryCreateEvent;
  8. use common\logic\User\CreditHistory\Model\CreditHistory;
  9. use common\logic\User\User\Model\User;
  10. use yii\base\Event;
  11. class CreditHistoryBuilder extends AbstractBuilder
  12. {
  13. protected CreditHistorySolver $creditHistorySolver;
  14. protected OrderSolver $orderSolver;
  15. public function loadDependencies(): void
  16. {
  17. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  18. $this->orderSolver = $this->loadService(OrderSolver::class);
  19. }
  20. public function instanciateCreditHistory(
  21. string $type,
  22. float $amount,
  23. Producer $producer,
  24. User $user,
  25. User $userAction,
  26. string $meanPayment = null,
  27. Order $order = null
  28. ): CreditHistory
  29. {
  30. $creditHistory = new CreditHistory;
  31. $creditHistory->type = $type;
  32. $creditHistory->amount = round($amount, 2);
  33. $creditHistory->populateProducer($producer);
  34. $creditHistory->populateUser($user);
  35. $creditHistory->populateUserAction($userAction);
  36. if($order) {
  37. $creditHistory->populateOrder($order);
  38. }
  39. if($meanPayment) {
  40. $creditHistory->mean_payment = $meanPayment;
  41. }
  42. return $creditHistory;
  43. }
  44. // saveCreditHistory
  45. public function createCreditHistory(
  46. string $type,
  47. float $amount,
  48. Producer $producer,
  49. User $user,
  50. User $userAction,
  51. string $meanPayment = null,
  52. Order $order = null
  53. ): ?CreditHistory
  54. {
  55. if ($amount > -0.01 && $amount < 0.01) {
  56. return null;
  57. }
  58. $creditHistory = $this->instanciateCreditHistory($type, $amount, $producer, $user, $userAction, $meanPayment, $order);
  59. $creditHistory->setComment($creditHistory->getComment() . $this->orderSolver->getCreditHistoryComment($creditHistory));
  60. $this->saveCreate($creditHistory);
  61. $creditHistoryCreateEvent = new CreditHistoryCreateEvent();
  62. $creditHistoryCreateEvent->creditHistory = $creditHistory;
  63. $creditHistory->trigger(CreditHistory::EVENT_CREATE, $creditHistoryCreateEvent);
  64. return $creditHistory;
  65. }
  66. }