Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

63 lines
2.2KB

  1. <?php
  2. namespace common\logic\User\CreditHistory;
  3. use common\logic\BaseService;
  4. use common\logic\BuilderInterface;
  5. use common\logic\Order\Order\Order;
  6. use common\logic\Producer\Producer\Producer;
  7. use common\logic\User\User\User;
  8. use common\logic\User\UserProducer\UserProducerBuilder;
  9. class CreditHistoryBuilder extends BaseService implements BuilderInterface
  10. {
  11. protected CreditHistorySolver $creditHistorySolver;
  12. protected UserProducerBuilder $userProducerBuilder;
  13. public function __construct()
  14. {
  15. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  16. $this->userProducerBuilder = $this->loadService(UserProducerBuilder::class);
  17. }
  18. public function instanciate(string $type, float $amount, Producer $producer, User $user, User $userAction, Order $order = null): CreditHistory
  19. {
  20. $creditHistory = new CreditHistory;
  21. $creditHistory->type = $type;
  22. $creditHistory->amount = round($amount, 2);
  23. $creditHistory->id_producer = $producer->id;
  24. $creditHistory->populateRelation('producer', $producer);
  25. $creditHistory->id_user = $user->id;
  26. $creditHistory->populateRelation('user', $user);
  27. $creditHistory->id_user_action = $userAction->id;
  28. $creditHistory->populateRelation('userAction', $userAction);
  29. if($order) {
  30. $creditHistory->id_order = $order->id;
  31. $creditHistory->populateRelation('order', $order);
  32. }
  33. return $creditHistory;
  34. }
  35. // saveCreditHistory
  36. public function create(string $type, float $amount, Producer $producer, User $user, User $userAction, Order $order = null): ?CreditHistory
  37. {
  38. if ($amount > -0.01 && $amount < 0.01) {
  39. return null;
  40. }
  41. $creditHistory = $this->instanciate($type, $amount, $producer, $user, $userAction, $order);
  42. // Initialisation du commentaire avant sauvegarde
  43. $creditHistory->setComment($creditHistory->getComment() . $this->creditHistorySolver->getStrComment($creditHistory));
  44. $creditHistory->save();
  45. // Mise à jour du crédit au niveau de UserProducer
  46. $this->userProducerBuilder->updateCredit($creditHistory);
  47. return $creditHistory;
  48. }
  49. }