Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

100 lines
3.8KB

  1. <?php
  2. namespace domain\Order\Order;
  3. use domain\Document\DeliveryNote\DeliveryNote;
  4. use domain\Document\DeliveryNote\DeliveryNoteBuilder;
  5. use domain\Document\DeliveryNote\DeliveryNoteRepository;
  6. use domain\Document\Document\DocumentManager;
  7. use domain\Document\Document\DocumentSolver;
  8. use domain\_\AbstractManager;
  9. use yii\base\Exception;
  10. class OrderDocumentManager extends AbstractManager
  11. {
  12. protected OrderSolver $orderSolver;
  13. protected OrderRepository $orderRepository;
  14. protected DocumentSolver $documentSolver;
  15. protected DeliveryNoteRepository $deliveryNoteRepository;
  16. protected DeliveryNoteBuilder $deliveryNoteBuilder;
  17. protected DocumentManager $documentManager;
  18. public function loadDependencies(): void
  19. {
  20. $this->orderSolver = $this->loadService(OrderSolver::class);
  21. $this->orderRepository = $this->loadService(OrderRepository::class);
  22. $this->documentSolver = $this->loadService(DocumentSolver::class);
  23. $this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
  24. $this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class);
  25. $this->documentManager = $this->loadService(DocumentManager::class);
  26. }
  27. public function generateDeliveryNoteForPointSale(array $idOrders = []): ?DeliveryNote
  28. {
  29. reset($idOrders);
  30. $firstOrder = $this->orderRepository->findOneOrderById((int)$idOrders[key($idOrders)]);
  31. $ordersArray = Order::searchAll(['id' => $idOrders,]);
  32. $deliveryNote = $this->deliveryNoteRepository->getOneDeliveryNoteExistingFromOrders($ordersArray);
  33. $deliveryNote = $this->deliveryNoteBuilder->deleteDeliveryNoteIfStatusDraft($deliveryNote);
  34. if ($deliveryNote && $this->documentSolver->isStatusValid($deliveryNote)) {
  35. throw new Exception("Vous ne pouvez pas modifier un bon de livraison déjà validé.");
  36. }
  37. if(!$deliveryNote && $firstOrder && !$firstOrder->pointSale->id_user) {
  38. throw new Exception("Vous devez définir un contact de facturation pour ce point de vente.");
  39. }
  40. if ($firstOrder) {
  41. if (!$deliveryNote) {
  42. $deliveryNote = $this->deliveryNoteBuilder->createDeliveryNoteForPointSale(
  43. $firstOrder->pointSale,
  44. $firstOrder->distribution,
  45. $idOrders
  46. );
  47. }
  48. return $deliveryNote;
  49. }
  50. return null;
  51. }
  52. public function generateDeliveryNotesForUser(array $idOrders = []): void
  53. {
  54. foreach($idOrders as $idOrder) {
  55. $order = $this->orderRepository->findOneOrderById($idOrder);
  56. $this->generateDeliveryNoteForUser($order);
  57. }
  58. }
  59. public function generateDeliveryNoteForUser(Order $order = null): ?DeliveryNote
  60. {
  61. if ($order && $this->orderSolver->isOrderFromProducer($order) && $order->id_user) {
  62. return $this->deliveryNoteBuilder->createDeliveryNoteForOrder($order);
  63. }
  64. return null;
  65. }
  66. public function validateDeliveryNotes(array $idOrders = []): bool
  67. {
  68. if (count($idOrders) > 0) {
  69. $oneDeliveryNoteValidated = false;
  70. foreach ($idOrders as $idOrder) {
  71. $order = $this->orderRepository->findOneOrderById($idOrder);
  72. if ($this->orderSolver->isOrderFromProducer($order)) {
  73. $deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteById((int) $order->id_delivery_note);
  74. if($deliveryNote) {
  75. $this->documentManager->validateDocument($deliveryNote);
  76. $oneDeliveryNoteValidated = true;
  77. }
  78. }
  79. }
  80. return $oneDeliveryNoteValidated;
  81. }
  82. return false;
  83. }
  84. }