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.

159 lines
5.9KB

  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. if($rotating->getDay() == $distribution->getDate()->format('N')) {
  98. $rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution);
  99. $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
  100. $distribution,
  101. $rotatingProductOfDistribution
  102. );
  103. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  104. if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) {
  105. $this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution);
  106. }
  107. else {
  108. $this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution);
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * @throws ErrorException
  115. */
  116. public function initializeAllRotatingsInDistribution(Distribution $distribution)
  117. {
  118. $rotatingsArray = $this->rotatingRepository->findRotatings();
  119. foreach($rotatingsArray as $rotating) {
  120. $this->initializeRotatingInDistribution($rotating, $distribution);
  121. }
  122. }
  123. public function deleteRotating(Rotating $rotating): bool
  124. {
  125. $this->deleteAllRotatingProducts($rotating);
  126. return $rotating->delete();
  127. }
  128. public function deleteAllRotatingProducts(Rotating $rotating): void
  129. {
  130. foreach($rotating->getDistributionRotatings() as $distributionRotating) {
  131. $distributionRotating->delete();
  132. }
  133. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  134. $rotatingProduct->delete();
  135. }
  136. }
  137. }