|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
-
- namespace common\logic\User\CreditHistory;
-
- use common\logic\BaseService;
- use common\logic\BuilderInterface;
- use common\logic\Order\Order\Order;
- use common\logic\Producer\Producer\Producer;
- use common\logic\User\User\User;
- use common\logic\User\UserProducer\UserProducerBuilder;
-
- class CreditHistoryBuilder extends BaseService implements BuilderInterface
- {
- protected CreditHistorySolver $creditHistorySolver;
- protected UserProducerBuilder $userProducerBuilder;
-
- public function __construct()
- {
- $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
- $this->userProducerBuilder = $this->loadService(UserProducerBuilder::class);
- }
-
- public function instanciate(string $type, float $amount, Producer $producer, User $user, User $userAction, Order $order = null): CreditHistory
- {
- $creditHistory = new CreditHistory;
-
- $creditHistory->type = $type;
- $creditHistory->amount = round($amount, 2);
- $creditHistory->id_producer = $producer->id;
- $creditHistory->populateRelation('producer', $producer);
- $creditHistory->id_user = $user->id;
- $creditHistory->populateRelation('user', $user);
- $creditHistory->id_user_action = $userAction->id;
- $creditHistory->populateRelation('userAction', $userAction);
-
- if($order) {
- $creditHistory->id_order = $order->id;
- $creditHistory->populateRelation('order', $order);
- }
-
- return $creditHistory;
- }
-
- // saveCreditHistory
- public function create(string $type, float $amount, Producer $producer, User $user, User $userAction, Order $order = null): ?CreditHistory
- {
- if ($amount > -0.01 && $amount < 0.01) {
- return null;
- }
-
- $creditHistory = $this->instanciate($type, $amount, $producer, $user, $userAction, $order);
-
- // Initialisation du commentaire avant sauvegarde
- $creditHistory->setComment($creditHistory->getComment() . $this->creditHistorySolver->getStrComment($creditHistory));
-
- $creditHistory->save();
-
- // Mise à jour du crédit au niveau de UserProducer
- $this->userProducerBuilder->updateCredit($creditHistory);
-
- return $creditHistory;
- }
- }
|