You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeliveryNoteBuilder.php 4.6KB

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