productDistributionRepository = $this->loadService(ProductDistributionRepository::class); $this->distributionSolver = $this->loadService(DistributionSolver::class); $this->productSolver = $this->loadService(ProductSolver::class); } public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution { $productDistribution = new ProductDistribution(); $productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution); $productDistribution->populateFieldObject('id_product', 'product', $product); return $productDistribution; } public function createProductDistribution(Distribution $distribution, Product $product): ProductDistribution { $productDistribution = $this->instanciateProductDistribution($distribution, $product); $this->initProductDistribution($productDistribution); $this->saveCreate($productDistribution); return $productDistribution; } public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution { return $this->productDistributionRepository->findOneProductDistribution($distribution, $product) ?? $this->createProductDistribution($distribution, $product); } public function updateProductDistribution(ProductDistribution $productDistribution): ProductDistribution { $this->initProductDistribution($productDistribution); $this->saveUpdate($productDistribution); return $productDistribution; } public function initProductDistribution(ProductDistribution $productDistribution): ProductDistribution { $this->initActive($productDistribution); $this->initQuantityMax($productDistribution); return $productDistribution; } public function initActive(ProductDistribution $productDistribution): ProductDistribution { $productDistribution->active = (int) $this->productSolver->isProductActiveByDay( $productDistribution->product, $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) ); return $productDistribution; } public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution { $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay( $productDistribution->product, $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) ); return $productDistribution; } }