|
- <?php
-
- namespace domain\Product\Rotating;
-
- use common\components\Date;
- use domain\_\AbstractManager;
- use domain\Distribution\Distribution\DistributionRepository;
- use domain\Distribution\Distribution\DistributionResolver;
- use domain\Distribution\DistributionRotating\DistributionRotatingManager;
- use domain\Producer\Producer\Producer;
- use domain\Product\Product\ProductRepository;
- use domain\Product\RotatingProduct\RotatingProductManager;
- use yii\base\ErrorException;
-
- class RotatingManager extends AbstractManager
- {
- protected RotatingBuilder $rotatingBuilder;
- protected ProductRepository $productRepository;
- protected RotatingProductManager $rotatingProductManager;
- protected DistributionRepository $distributionRepository;
- protected DistributionRotatingManager $distributionRotatingManager;
- protected RotatingResolver $rotatingResolver;
- protected DistributionResolver $distributionResolver;
-
- public function loadDependencies(): void
- {
- $this->rotatingBuilder = $this->loadService(RotatingBuilder::class);
- $this->productRepository = $this->loadService(ProductRepository::class);
- $this->rotatingProductManager = $this->loadService(RotatingProductManager::class);
- $this->distributionRepository = $this->loadService(DistributionRepository::class);
- $this->distributionRotatingManager = $this->loadService(DistributionRotatingManager::class);
- $this->rotatingResolver = $this->loadService(RotatingResolver::class);
- $this->distributionResolver = $this->loadService(DistributionResolver::class);
- }
-
- public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
- {
- $rotating = $this->rotatingBuilder->instanciateRotating($producer);
- $rotating->setName($name);
- $rotating->setDay($day);
- $rotating->save();
-
- $this->manageRotatingProducts($rotating, $productsIdsArray);
-
- return $rotating;
- }
-
- public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
- {
- $this->deleteAllRotatingProducts($rotating);
-
- foreach($productsIdsArray as $productId) {
- $product = $this->productRepository->findOneProductById($productId);
- if($product) {
- $this->rotatingProductManager->createRotatingProduct($rotating, $product);
- }
- }
- }
-
- /**
- * @throws ErrorException
- */
- public function initializeRotatingInDistributionsIncoming(Rotating $rotating)
- {
- $date = new \DateTime();
- $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
- $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();
-
- while($distribution && $date < $dateLastDistributionIncoming) {
- $rotatingProduct = $this->rotatingResolver->deductNextRotatingProductOfDistribution($rotating, $distribution);
- $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
- $distribution,
- $rotatingProduct
- );
- $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
- }
- }
-
- public function deleteRotating(Rotating $rotating): bool
- {
- $this->deleteAllRotatingProducts($rotating);
- return $rotating->delete();
- }
-
- public function deleteAllRotatingProducts(Rotating $rotating): void
- {
- foreach($rotating->getRotatingProducts() as $rotatingProduct) {
- $rotatingProduct->delete();
- }
- }
- }
|