|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?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;
-
- public function __construct()
- {
- $this->creditHistorySolver = $this->loadService(CreditHistorySolver::class);
- }
-
- public function instanciate(
- 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->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);
- }
-
- if($meanPayment) {
- $creditHistory->mean_payment = $meanPayment;
- }
-
- return $creditHistory;
- }
-
- // saveCreditHistory
- public function create(
- 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->instanciate($type, $amount, $producer, $user, $userAction, $meanPayment, $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
- // @TODO : à gérer avec les événements
- //$this->userProducerBuilder->updateCredit($creditHistory);
-
- return $creditHistory;
- }
- }
|