|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
-
- namespace common\logic\Document\DeliveryNote\Service;
-
- use common\logic\Distribution\Distribution\Model\Distribution;
- use common\logic\Document\DeliveryNote\Event\DeliveryNoteCreateEvent;
- use common\logic\Document\DeliveryNote\Model\DeliveryNote;
- use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository;
- use common\logic\Document\Document\Service\DocumentBuilder;
- use common\logic\Order\Order\Model\Order;
- use common\logic\Order\Order\Service\OrderSolver;
- use common\logic\PointSale\PointSale\Model\PointSale;
- use common\logic\User\User\Repository\UserRepository;
- use common\logic\User\User\Service\UserSolver;
- use yii\base\ErrorException;
-
- class DeliveryNoteBuilder extends DocumentBuilder
- {
- protected UserSolver $userSolver;
- protected UserRepository $userRepository;
- protected DeliveryNoteRepository $deliveryNoteRepository;
- protected OrderSolver $orderSolver;
-
- public function loadDependencies(): void
- {
- parent::loadDependencies();
- $this->userSolver = $this->loadService(UserSolver::class);
- $this->userRepository = $this->loadService(UserRepository::class);
- $this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
- $this->orderSolver = $this->loadService(OrderSolver::class);
- }
-
- public function instanciateDeliveryNote(): DeliveryNote
- {
- $deliveryNote = new DeliveryNote();
-
- $this->initDocumentProducer($deliveryNote);
- $this->initTaxCalculationMethod($deliveryNote);
-
- return $deliveryNote;
- }
-
- public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution, array $idOrders): DeliveryNote
- {
- $deliveryNote = $this->instanciateDeliveryNote();
- $this->initDeliveryNoteNameByPointSale($deliveryNote, $distribution, $pointSale);
- $this->initDeliveryNoteUserFromPointSale($deliveryNote, $pointSale);
- $this->initDeliveryNoteAddress($deliveryNote);
- $deliveryNote->save();
-
- $this->triggerDeliveryNoteCreateEvent($deliveryNote, $idOrders);
-
- return $deliveryNote;
- }
-
- public function createDeliveryNoteForOrder(Order $order): DeliveryNote
- {
- $deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteByOrder($order);
- $deliveryNote = $this->deleteDeliveryNoteIfStatusDraft($deliveryNote);
-
- if(!$deliveryNote) {
- $deliveryNote = $this->instanciateDeliveryNote();
- $this->initDeliveryNoteNameByOrder($deliveryNote, $order);
- $this->initDeliveryNoteUserFromOrder($deliveryNote, $order);
- $this->initDeliveryNoteAddress($deliveryNote);
- $deliveryNote->save();
-
- $this->triggerDeliveryNoteCreateEvent($deliveryNote, [$order->id]);
- }
-
- return $deliveryNote;
- }
-
- public function triggerDeliveryNoteCreateEvent(DeliveryNote $deliveryNote, array $idOrders)
- {
- $deliveryNoteCreateEvent = new DeliveryNoteCreateEvent();
- $deliveryNoteCreateEvent->deliveryNote = $deliveryNote;
- $deliveryNoteCreateEvent->idOrders = $idOrders;
- $deliveryNote->trigger(DeliveryNote::EVENT_CREATE, $deliveryNoteCreateEvent);
- }
-
- public function initDeliveryNoteUserFromPointSale(DeliveryNote $deliveryNote, PointSale $pointSale)
- {
- $deliveryNote->id_user = $pointSale->id_user;
- }
-
- public function initDeliveryNoteUserFromOrder(DeliveryNote $deliveryNote, Order $order)
- {
- $deliveryNote->id_user = $order->id_user;
- }
-
- public function initDeliveryNoteNameByPointSale(DeliveryNote $deliveryNote, Distribution $distribution, PointSale $pointSale)
- {
- $deliveryNote->name = 'Bon de livraison ' . $pointSale->name .
- ' (' . date('d/m/Y', strtotime($distribution->date)) . ')';
- }
-
- public function initDeliveryNoteNameByOrder(DeliveryNote $deliveryNote, Order $order)
- {
- $deliveryNote->name = 'Bon de livraison ' . $this->orderSolver->getOrderUsername($order) .
- ' (' . date('d/m/Y', strtotime($order->distribution->date)) . ')';
- }
-
- public function initDeliveryNoteAddress(DeliveryNote $deliveryNote)
- {
- $user = $this->userRepository->findOneUserById($deliveryNote->id_user);
- if($user) {
- $deliveryNote->address = $this->userSolver->getFullAddress($user);
- }
- else {
- throw new ErrorException("L'utilisateur du bon de livraison n'est pas défini.");
- }
- }
-
- public function deleteDeliveryNoteIfStatusDraft(DeliveryNote $deliveryNote = null): ?DeliveryNote
- {
- if ($deliveryNote && $this->documentSolver->isStatusDraft($deliveryNote)) {
- $deliveryNote->delete();
- $deliveryNote = null;
- }
-
- return $deliveryNote;
- }
- }
|