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

82 lines
2.4KB

  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. public function __construct()
  13. {
  14. $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
  15. }
  16. public function instanciate(
  17. string $type,
  18. float $amount,
  19. Producer $producer,
  20. User $user,
  21. User $userAction,
  22. string $meanPayment = null,
  23. Order $order = null
  24. ): CreditHistory
  25. {
  26. $creditHistory = new CreditHistory;
  27. $creditHistory->type = $type;
  28. $creditHistory->amount = round($amount, 2);
  29. $creditHistory->id_producer = $producer->id;
  30. $creditHistory->populateRelation('producer', $producer);
  31. $creditHistory->id_user = $user->id;
  32. $creditHistory->populateRelation('user', $user);
  33. $creditHistory->id_user_action = $userAction->id;
  34. $creditHistory->populateRelation('userAction', $userAction);
  35. if($order) {
  36. $creditHistory->id_order = $order->id;
  37. $creditHistory->populateRelation('order', $order);
  38. }
  39. if($meanPayment) {
  40. $creditHistory->mean_payment = $meanPayment;
  41. }
  42. return $creditHistory;
  43. }
  44. // saveCreditHistory
  45. public function create(
  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->instanciate($type, $amount, $producer, $user, $userAction, $meanPayment, $order);
  59. // Initialisation du commentaire avant sauvegarde
  60. $creditHistory->setComment($creditHistory->getComment() . $this->creditHistorySolver->getStrComment($creditHistory));
  61. $creditHistory->save();
  62. // Mise à jour du crédit au niveau de UserProducer
  63. // @TODO : à gérer avec les événements
  64. //$this->userProducerBuilder->updateCredit($creditHistory);
  65. return $creditHistory;
  66. }
  67. }