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.

157 lines
5.8KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use domain\_\AbstractManager;
  4. use domain\Distribution\Distribution\Distribution;
  5. use domain\Distribution\Distribution\DistributionRepository;
  6. use domain\Distribution\Distribution\DistributionResolver;
  7. use domain\Distribution\DistributionRotating\DistributionRotatingManager;
  8. use domain\Producer\Producer\Producer;
  9. use domain\Product\Product\ProductManager;
  10. use domain\Product\Product\ProductRepository;
  11. use domain\Product\Rotating\Event\RotatingSaveEvent;
  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. /**
  41. * @throws ErrorException
  42. */
  43. public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
  44. {
  45. $rotating = $this->rotatingBuilder->instanciateRotating($producer);
  46. $rotating->setName($name);
  47. $rotating->setDay($day);
  48. $rotating->save();
  49. $this->afterSaveRotating($rotating, $productsIdsArray);
  50. return $rotating;
  51. }
  52. public function updateRotating(Rotating $rotating, array $productsIdsArray): bool
  53. {
  54. $return = $rotating->save();
  55. $this->afterSaveRotating($rotating, $productsIdsArray);
  56. return $return;
  57. }
  58. public function afterSaveRotating(Rotating $rotating, array $productsIdsArray)
  59. {
  60. $this->manageRotatingProducts($rotating, $productsIdsArray);
  61. $rotating->triggerEvent(RotatingSaveEvent::class, [
  62. 'rotating' => $rotating
  63. ]);
  64. }
  65. public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
  66. {
  67. $this->deleteAllRotatingProducts($rotating);
  68. foreach($productsIdsArray as $productId) {
  69. $product = $this->productRepository->findOneProductById($productId);
  70. if($product) {
  71. $this->rotatingProductManager->createRotatingProduct($rotating, $product);
  72. }
  73. }
  74. }
  75. /**
  76. * @throws ErrorException
  77. */
  78. public function initializeRotatingInDistributionsIncoming(Rotating $rotating)
  79. {
  80. $date = new \DateTime();
  81. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  82. $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();
  83. if($dateLastDistributionIncoming) {
  84. while($date < $dateLastDistributionIncoming) {
  85. if($distribution) {
  86. $this->initializeRotatingInDistribution($rotating, $distribution);
  87. }
  88. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  89. }
  90. }
  91. }
  92. /**
  93. * @throws ErrorException
  94. */
  95. public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution)
  96. {
  97. $rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution);
  98. $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
  99. $distribution,
  100. $rotatingProductOfDistribution
  101. );
  102. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  103. if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) {
  104. $this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution);
  105. }
  106. else {
  107. $this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution);
  108. }
  109. }
  110. }
  111. /**
  112. * @throws ErrorException
  113. */
  114. public function initializeAllRotatingsInDistribution(Distribution $distribution)
  115. {
  116. $rotatingsArray = $this->rotatingRepository->findRotatings();
  117. foreach($rotatingsArray as $rotating) {
  118. $this->initializeRotatingInDistribution($rotating, $distribution);
  119. }
  120. }
  121. public function deleteRotating(Rotating $rotating): bool
  122. {
  123. $this->deleteAllRotatingProducts($rotating);
  124. return $rotating->delete();
  125. }
  126. public function deleteAllRotatingProducts(Rotating $rotating): void
  127. {
  128. foreach($rotating->getDistributionRotatings() as $distributionRotating) {
  129. $distributionRotating->delete();
  130. }
  131. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  132. $rotatingProduct->delete();
  133. }
  134. }
  135. }