|
- <?php
-
- namespace common\logic\User\CreditHistory;
-
- use common\logic\AbstractBuilder;
- use common\logic\BuilderInterface;
- use common\logic\Order\Order\Order;
- use common\logic\Producer\Producer\Producer;
- use common\logic\User\User\User;
- use yii\base\Event;
- use yii\db\ActiveRecord;
-
- class CreditHistoryBuilder extends AbstractBuilder implements BuilderInterface
- {
- protected CreditHistorySolver $creditHistorySolver;
-
- public function __construct()
- {
- $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
- }
-
- public function instanciateCreditHistory(
- string $type,
- float $amount,
- Producer $producer,
- User $user,
- User $userAction,
- string $meanPayment = null,
- Order $order = null
- ): CreditHistory
- {
- $creditHistory = new CreditHistory;
-
- $creditHistory->type = $type;
- $creditHistory->amount = round($amount, 2);
- $creditHistory->populateProducer($producer);
- $creditHistory->populateUser($user);
- $creditHistory->populateUserAction($userAction);
-
- if($order) {
- $creditHistory->populateOrder($order);
- }
-
- if($meanPayment) {
- $creditHistory->mean_payment = $meanPayment;
- }
-
- return $creditHistory;
- }
-
- // saveCreditHistory
- public function createCreditHistory(
- string $type,
- float $amount,
- Producer $producer,
- User $user,
- User $userAction,
- string $meanPayment = null,
- Order $order = null
- ): ?CreditHistory
- {
- if ($amount > -0.01 && $amount < 0.01) {
- return null;
- }
-
- $creditHistory = $this->instanciateCreditHistory($type, $amount, $producer, $user, $userAction, $meanPayment, $order);
- $creditHistory->setComment($creditHistory->getComment() . $this->creditHistorySolver->getStrComment($creditHistory));
- $this->saveCreate($creditHistory);
-
- $creditHistory->trigger(CreditHistory::EVENT_CREATE, new Event(['creditHistory' => $creditHistory]));
-
- return $creditHistory;
- }
- }
|