No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

126 líneas
4.8KB

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