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.

74 lines
2.1KB

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