Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DeliveryNoteBuilder.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace common\logic\Document\DeliveryNote\Service;
  3. use common\logic\Distribution\Distribution\Model\Distribution;
  4. use common\logic\Document\DeliveryNote\Event\DeliveryNoteCreateEvent;
  5. use common\logic\Document\DeliveryNote\Model\DeliveryNote;
  6. use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository;
  7. use common\logic\Document\Document\Service\DocumentBuilder;
  8. use common\logic\Order\Order\Model\Order;
  9. use common\logic\Order\Order\Service\OrderSolver;
  10. use common\logic\PointSale\PointSale\Model\PointSale;
  11. use common\logic\User\User\Repository\UserRepository;
  12. use common\logic\User\User\Service\UserSolver;
  13. use yii\base\ErrorException;
  14. class DeliveryNoteBuilder extends DocumentBuilder
  15. {
  16. protected UserSolver $userSolver;
  17. protected UserRepository $userRepository;
  18. protected DeliveryNoteRepository $deliveryNoteRepository;
  19. protected OrderSolver $orderSolver;
  20. public function loadDependencies(): void
  21. {
  22. parent::loadDependencies();
  23. $this->userSolver = $this->loadService(UserSolver::class);
  24. $this->userRepository = $this->loadService(UserRepository::class);
  25. $this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
  26. $this->orderSolver = $this->loadService(OrderSolver::class);
  27. }
  28. public function instanciateDeliveryNote(): DeliveryNote
  29. {
  30. $deliveryNote = new DeliveryNote();
  31. $this->initDocumentProducer($deliveryNote);
  32. $this->initTaxCalculationMethod($deliveryNote);
  33. return $deliveryNote;
  34. }
  35. public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution, array $idOrders): DeliveryNote
  36. {
  37. $deliveryNote = $this->instanciateDeliveryNote();
  38. $this->initDeliveryNoteNameByPointSale($deliveryNote, $distribution, $pointSale);
  39. $this->initDeliveryNoteUserFromPointSale($deliveryNote, $pointSale);
  40. $this->initDeliveryNoteAddress($deliveryNote);
  41. $deliveryNote->save();
  42. $this->triggerDeliveryNoteCreateEvent($deliveryNote, $idOrders);
  43. return $deliveryNote;
  44. }
  45. public function createDeliveryNoteForOrder(Order $order): DeliveryNote
  46. {
  47. $deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteByOrder($order);
  48. $deliveryNote = $this->deleteDeliveryNoteIfStatusDraft($deliveryNote);
  49. if(!$deliveryNote) {
  50. $deliveryNote = $this->instanciateDeliveryNote();
  51. $this->initDeliveryNoteNameByOrder($deliveryNote, $order);
  52. $this->initDeliveryNoteUserFromOrder($deliveryNote, $order);
  53. $this->initDeliveryNoteAddress($deliveryNote);
  54. $deliveryNote->save();
  55. $this->triggerDeliveryNoteCreateEvent($deliveryNote, [$order->id]);
  56. }
  57. return $deliveryNote;
  58. }
  59. public function triggerDeliveryNoteCreateEvent(DeliveryNote $deliveryNote, array $idOrders)
  60. {
  61. $deliveryNoteCreateEvent = new DeliveryNoteCreateEvent();
  62. $deliveryNoteCreateEvent->deliveryNote = $deliveryNote;
  63. $deliveryNoteCreateEvent->idOrders = $idOrders;
  64. $deliveryNote->trigger(DeliveryNote::EVENT_CREATE, $deliveryNoteCreateEvent);
  65. }
  66. public function initDeliveryNoteUserFromPointSale(DeliveryNote $deliveryNote, PointSale $pointSale)
  67. {
  68. $deliveryNote->id_user = $pointSale->id_user;
  69. }
  70. public function initDeliveryNoteUserFromOrder(DeliveryNote $deliveryNote, Order $order)
  71. {
  72. $deliveryNote->id_user = $order->id_user;
  73. }
  74. public function initDeliveryNoteNameByPointSale(DeliveryNote $deliveryNote, Distribution $distribution, PointSale $pointSale)
  75. {
  76. $deliveryNote->name = 'Bon de livraison ' . $pointSale->name .
  77. ' (' . date('d/m/Y', strtotime($distribution->date)) . ')';
  78. }
  79. public function initDeliveryNoteNameByOrder(DeliveryNote $deliveryNote, Order $order)
  80. {
  81. $deliveryNote->name = 'Bon de livraison ' . $this->orderSolver->getOrderUsername($order) .
  82. ' (' . date('d/m/Y', strtotime($order->distribution->date)) . ')';
  83. }
  84. public function initDeliveryNoteAddress(DeliveryNote $deliveryNote)
  85. {
  86. $user = $this->userRepository->findOneUserById($deliveryNote->id_user);
  87. if($user) {
  88. $deliveryNote->address = $this->userSolver->getFullAddress($user);
  89. }
  90. else {
  91. throw new ErrorException("L'utilisateur du bon de livraison n'est pas défini.");
  92. }
  93. }
  94. public function deleteDeliveryNoteIfStatusDraft(DeliveryNote $deliveryNote = null): ?DeliveryNote
  95. {
  96. if ($deliveryNote && $this->documentSolver->isStatusDraft($deliveryNote)) {
  97. $deliveryNote->delete();
  98. $deliveryNote = null;
  99. }
  100. return $deliveryNote;
  101. }
  102. }