@@ -69,18 +69,13 @@ class DistributionBuilder extends BaseService implements BuilderInterface | |||
public function createDistributionIfNotExist(Producer $producer, string $date, int $delivery = 1) | |||
{ | |||
$distribution = $this->distributionRepository->getOne($producer, $date); | |||
if (!$distribution) { | |||
$distribution = $this->createDistribution($producer, $date, 1); | |||
} | |||
return $distribution; | |||
return $this->distributionRepository->findOneDistribution($producer, $date) | |||
?? $this->createDistribution($producer, $date, $delivery); | |||
} | |||
public function createPointSaleDistributions(Distribution $distribution): void | |||
{ | |||
$countPointSaleDistribution = $this->pointSaleDistributionRepository->countByDistribution($distribution); | |||
$countPointSaleDistribution = $this->pointSaleDistributionRepository->countPointSaleDistributionByDistribution($distribution); | |||
if (!$countPointSaleDistribution) { | |||
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true); | |||
} | |||
@@ -88,8 +83,8 @@ class DistributionBuilder extends BaseService implements BuilderInterface | |||
public function createProductDistributions(Distribution $distribution): void | |||
{ | |||
$productArray = $this->productRepository->get(); | |||
$countProductDistribution = $this->productDistributionRepository->countByDistribution($distribution); | |||
$productArray = $this->productRepository->findProducts(); | |||
$countProductDistribution = $this->productDistributionRepository->countProductDistributionByDistribution($distribution); | |||
if (!$countProductDistribution) { | |||
foreach ($productArray as $product) { | |||
@@ -114,7 +109,7 @@ class DistributionBuilder extends BaseService implements BuilderInterface | |||
*/ | |||
public function addProductIncomingDistributions(Product $product): void | |||
{ | |||
$distributionArray = $this->distributionRepository->getIncoming(); | |||
$distributionArray = $this->distributionRepository->findDistributionsIncoming(); | |||
foreach ($distributionArray as $distribution) { | |||
$this->addProduct($distribution, $product); | |||
} | |||
@@ -127,7 +122,7 @@ class DistributionBuilder extends BaseService implements BuilderInterface | |||
{ | |||
$pointSaleDistribution = $this->pointSaleDistributionBuilder->createPointSaleDistributionIfNotExist($distribution, $pointSale); | |||
$dayString = $this->distributionSolver->getDayAsString($distribution); | |||
$dayString = $this->distributionSolver->getDistributionDayAsString($distribution); | |||
$pointSaleDistribution->delivery = 0; | |||
$deliveryDay = 'delivery_' . $dayString; |
@@ -10,7 +10,7 @@ use common\logic\SolverInterface; | |||
class DistributionSolver extends BaseService implements SolverInterface | |||
{ | |||
public function getDayAsString(Distribution $distribution): string | |||
public function getDistributionDayAsString(Distribution $distribution): string | |||
{ | |||
$dayDistribution = date('N', strtotime($distribution->date)); | |||
@@ -7,27 +7,26 @@ use common\logic\BuilderInterface; | |||
use common\logic\Distribution\Distribution\Distribution; | |||
use common\logic\Distribution\Distribution\DistributionSolver; | |||
use common\logic\Product\Product\Product; | |||
use common\logic\Product\Product\ProductSolver; | |||
class ProductDistributionBuilder extends BaseService implements BuilderInterface | |||
{ | |||
protected ProductDistributionRepository $productDistributionRepository; | |||
protected DistributionSolver $distributionSolver; | |||
protected ProductSolver $productSolver; | |||
public function __construct() | |||
{ | |||
$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->populateRelation('distribution', $distribution); | |||
$productDistribution->id_distribution = $distribution->id; | |||
$productDistribution->populateRelation('product', $product); | |||
$productDistribution->id_product = $product->id; | |||
$productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution); | |||
$productDistribution->populateFieldObject('id_product', 'product', $product); | |||
return $productDistribution; | |||
} | |||
@@ -47,37 +46,26 @@ class ProductDistributionBuilder extends BaseService implements BuilderInterface | |||
public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution | |||
{ | |||
$productDistribution = $this->productDistributionRepository->getOne($distribution, $product); | |||
if(!$productDistribution) { | |||
$productDistribution = $this->createProductDistribution($distribution, $product); | |||
} | |||
return $productDistribution; | |||
return $this->productDistributionRepository->findOneProductDistribution($distribution, $product) | |||
?? $this->createProductDistribution($distribution, $product); | |||
} | |||
public function initActive(ProductDistribution $productDistribution): ProductDistribution | |||
{ | |||
$day = $this->distributionSolver->getDayAsString($productDistribution->distribution); | |||
$productDistribution->active = 0; | |||
if ($productDistribution->product->active && $productDistribution->product->$day) { | |||
$productDistribution->active = 1; | |||
} | |||
$productDistribution->active = $this->productSolver->isProductActiveByDay( | |||
$productDistribution->product, | |||
$this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) | |||
); | |||
return $productDistribution; | |||
} | |||
public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution | |||
{ | |||
$product = $productDistribution->product; | |||
$day = $this->distributionSolver->getDayAsString($productDistribution->distribution); | |||
$productDistribution->quantity_max = $product->quantity_max; | |||
$fieldQuantityMax = 'quantity_max_' . $day; | |||
if (isset($product->$fieldQuantityMax) && $product->$fieldQuantityMax > 0) { | |||
$productDistribution->quantity_max = $product->$fieldQuantityMax; | |||
} | |||
$productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay( | |||
$productDistribution->product, | |||
$this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) | |||
); | |||
return $productDistribution; | |||
} |
@@ -183,4 +183,31 @@ class ProductSolver extends BaseService implements SolverInterface | |||
return $product->name; | |||
} | |||
public function isProductActiveByDay(Product $product, string $day): bool | |||
{ | |||
return $product->active && $product->$day; | |||
} | |||
public function getProductFieldNameQuantityMax(string $day): string | |||
{ | |||
return 'quantity_max_'.$day; | |||
} | |||
public function isProductQuantityMaxDefinedByDay(Product $product, string $day): bool | |||
{ | |||
$fieldQuantityMax = $this->getProductFieldNameQuantityMax($day); | |||
return isset($product->$fieldQuantityMax) && $product->$fieldQuantityMax > 0; | |||
} | |||
public function getProductQuantityMaxByDay(Product $product, string $day): ?float | |||
{ | |||
$quantityMax = $product->quantity_max; | |||
if($this->isProductQuantityMaxDefinedByDay($product, $day)) { | |||
$fieldQuantityMax = $this->getProductFieldNameQuantityMax($day); | |||
$quantityMax = $product->$fieldQuantityMax; | |||
} | |||
return $quantityMax; | |||
} | |||
} |