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.

144 lines
5.5KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use common\components\Date;
  4. use domain\_\AbstractManager;
  5. use domain\Distribution\Distribution\Distribution;
  6. use domain\Distribution\Distribution\DistributionRepository;
  7. use domain\Distribution\Distribution\DistributionResolver;
  8. use domain\Distribution\DistributionRotating\DistributionRotatingManager;
  9. use domain\Producer\Producer\Producer;
  10. use domain\Product\Product\ProductManager;
  11. use domain\Product\Product\ProductRepository;
  12. use domain\Product\RotatingProduct\RotatingProductManager;
  13. use yii\base\ErrorException;
  14. class RotatingManager extends AbstractManager
  15. {
  16. protected RotatingBuilder $rotatingBuilder;
  17. protected ProductRepository $productRepository;
  18. protected RotatingProductManager $rotatingProductManager;
  19. protected DistributionRepository $distributionRepository;
  20. protected DistributionRotatingManager $distributionRotatingManager;
  21. protected RotatingResolver $rotatingResolver;
  22. protected DistributionResolver $distributionResolver;
  23. protected RotatingRepository $rotatingRepository;
  24. protected ProductManager $productManager;
  25. /**
  26. * @throws ErrorException
  27. */
  28. public function loadDependencies(): void
  29. {
  30. $this->rotatingBuilder = $this->loadService(RotatingBuilder::class);
  31. $this->productRepository = $this->loadService(ProductRepository::class);
  32. $this->rotatingProductManager = $this->loadService(RotatingProductManager::class);
  33. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  34. $this->distributionRotatingManager = $this->loadService(DistributionRotatingManager::class);
  35. $this->rotatingResolver = $this->loadService(RotatingResolver::class);
  36. $this->distributionResolver = $this->loadService(DistributionResolver::class);
  37. $this->rotatingRepository = $this->loadService(RotatingRepository::class);
  38. $this->productManager = $this->loadService(ProductManager::class);
  39. }
  40. public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
  41. {
  42. $rotating = $this->rotatingBuilder->instanciateRotating($producer);
  43. $rotating->setName($name);
  44. $rotating->setDay($day);
  45. $rotating->save();
  46. $this->manageRotatingProducts($rotating, $productsIdsArray);
  47. return $rotating;
  48. }
  49. /**
  50. * @throws ErrorException
  51. */
  52. public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
  53. {
  54. $this->deleteAllRotatingProducts($rotating);
  55. foreach($productsIdsArray as $productId) {
  56. $product = $this->productRepository->findOneProductById($productId);
  57. if($product) {
  58. $this->rotatingProductManager->createRotatingProduct($rotating, $product);
  59. }
  60. }
  61. // Rechargement obligatoire car rotatingProductsRelation a changé juste au-dessus
  62. // Possible de gérer ça via unset($rotating->rotatingProductsRelation) ?
  63. $rotating = $this->rotatingRepository->findOneRotatingById($rotating->getId());
  64. // @TODO : gérer via événement
  65. $this->initializeRotatingInDistributionsIncoming($rotating);
  66. }
  67. /**
  68. * @throws ErrorException
  69. */
  70. public function initializeRotatingInDistributionsIncoming(Rotating $rotating)
  71. {
  72. $date = new \DateTime();
  73. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  74. $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();
  75. while($distribution && $dateLastDistributionIncoming && $date < $dateLastDistributionIncoming) {
  76. $this->initializeRotatingInDistribution($rotating, $distribution);
  77. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  78. }
  79. }
  80. /**
  81. * @throws ErrorException
  82. */
  83. public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution)
  84. {
  85. $rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution);
  86. $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
  87. $distribution,
  88. $rotatingProductOfDistribution
  89. );
  90. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  91. if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) {
  92. $this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution);
  93. }
  94. else {
  95. $this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution);
  96. }
  97. }
  98. }
  99. /**
  100. * @throws ErrorException
  101. */
  102. public function initializeAllRotatingsInDistribution(Distribution $distribution)
  103. {
  104. $rotatingsArray = $this->rotatingRepository->findRotatings();
  105. foreach($rotatingsArray as $rotating) {
  106. $this->initializeRotatingInDistribution($rotating, $distribution);
  107. }
  108. }
  109. public function deleteRotating(Rotating $rotating): bool
  110. {
  111. $this->deleteAllRotatingProducts($rotating);
  112. return $rotating->delete();
  113. }
  114. public function deleteAllRotatingProducts(Rotating $rotating): void
  115. {
  116. foreach($rotating->getDistributionRotatings() as $distributionRotating) {
  117. $distributionRotating->delete();
  118. }
  119. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  120. $rotatingProduct->delete();
  121. }
  122. }
  123. }