|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
-
- namespace common\logic\Distribution\ProductDistribution\Service;
-
- use common\logic\AbstractBuilder;
- use common\logic\Distribution\Distribution\Model\Distribution;
- use common\logic\Distribution\Distribution\Service\DistributionSolver;
- use common\logic\Distribution\ProductDistribution\Model\ProductDistribution;
- use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository;
- use common\logic\Product\Product\Model\Product;
- use common\logic\Product\Product\Service\ProductSolver;
-
- class ProductDistributionBuilder extends AbstractBuilder
- {
- protected ProductDistributionRepository $productDistributionRepository;
- protected DistributionSolver $distributionSolver;
- protected ProductSolver $productSolver;
-
- public function loadDependencies(): void
- {
- $this->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;
- }
-
- }
|