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.

91 lines
3.5KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use common\components\Date;
  4. use domain\_\AbstractManager;
  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\ProductRepository;
  10. use domain\Product\RotatingProduct\RotatingProductManager;
  11. use yii\base\ErrorException;
  12. class RotatingManager extends AbstractManager
  13. {
  14. protected RotatingBuilder $rotatingBuilder;
  15. protected ProductRepository $productRepository;
  16. protected RotatingProductManager $rotatingProductManager;
  17. protected DistributionRepository $distributionRepository;
  18. protected DistributionRotatingManager $distributionRotatingManager;
  19. protected RotatingResolver $rotatingResolver;
  20. protected DistributionResolver $distributionResolver;
  21. public function loadDependencies(): void
  22. {
  23. $this->rotatingBuilder = $this->loadService(RotatingBuilder::class);
  24. $this->productRepository = $this->loadService(ProductRepository::class);
  25. $this->rotatingProductManager = $this->loadService(RotatingProductManager::class);
  26. $this->distributionRepository = $this->loadService(DistributionRepository::class);
  27. $this->distributionRotatingManager = $this->loadService(DistributionRotatingManager::class);
  28. $this->rotatingResolver = $this->loadService(RotatingResolver::class);
  29. $this->distributionResolver = $this->loadService(DistributionResolver::class);
  30. }
  31. public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
  32. {
  33. $rotating = $this->rotatingBuilder->instanciateRotating($producer);
  34. $rotating->setName($name);
  35. $rotating->setDay($day);
  36. $rotating->save();
  37. $this->manageRotatingProducts($rotating, $productsIdsArray);
  38. return $rotating;
  39. }
  40. public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
  41. {
  42. $this->deleteAllRotatingProducts($rotating);
  43. foreach($productsIdsArray as $productId) {
  44. $product = $this->productRepository->findOneProductById($productId);
  45. if($product) {
  46. $this->rotatingProductManager->createRotatingProduct($rotating, $product);
  47. }
  48. }
  49. }
  50. /**
  51. * @throws ErrorException
  52. */
  53. public function initializeRotatingInDistributionsIncoming(Rotating $rotating)
  54. {
  55. $date = new \DateTime();
  56. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  57. $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();
  58. while($distribution && $date < $dateLastDistributionIncoming) {
  59. $rotatingProduct = $this->rotatingResolver->deductNextRotatingProductOfDistribution($rotating, $distribution);
  60. $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
  61. $distribution,
  62. $rotatingProduct
  63. );
  64. $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
  65. }
  66. }
  67. public function deleteRotating(Rotating $rotating): bool
  68. {
  69. $this->deleteAllRotatingProducts($rotating);
  70. return $rotating->delete();
  71. }
  72. public function deleteAllRotatingProducts(Rotating $rotating): void
  73. {
  74. foreach($rotating->getRotatingProducts() as $rotatingProduct) {
  75. $rotatingProduct->delete();
  76. }
  77. }
  78. }