public function createDistributionIfNotExist(Producer $producer, string $date, int $delivery = 1) | public function createDistributionIfNotExist(Producer $producer, string $date, int $delivery = 1) | ||||
{ | { | ||||
$distribution = $this->distributionRepository->getOne($producer, $date); | return $this->distributionRepository->findOneDistribution($producer, $date) | ||||
?? $this->createDistribution($producer, $date, $delivery); | |||||
if (!$distribution) { | |||||
$distribution = $this->createDistribution($producer, $date, 1); | |||||
} | |||||
return $distribution; | |||||
} | } | ||||
public function createPointSaleDistributions(Distribution $distribution): void | public function createPointSaleDistributions(Distribution $distribution): void | ||||
{ | { | ||||
$countPointSaleDistribution = $this->pointSaleDistributionRepository->countByDistribution($distribution); | $countPointSaleDistribution = $this->pointSaleDistributionRepository->countPointSaleDistributionByDistribution($distribution); | ||||
if (!$countPointSaleDistribution) { | if (!$countPointSaleDistribution) { | ||||
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true); | $this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true); | ||||
} | } | ||||
public function createProductDistributions(Distribution $distribution): void | public function createProductDistributions(Distribution $distribution): void | ||||
{ | { | ||||
$productArray = $this->productRepository->get(); | $productArray = $this->productRepository->findProducts(); | ||||
$countProductDistribution = $this->productDistributionRepository->countByDistribution($distribution); | $countProductDistribution = $this->productDistributionRepository->countProductDistributionByDistribution($distribution); | ||||
if (!$countProductDistribution) { | if (!$countProductDistribution) { | ||||
foreach ($productArray as $product) { | foreach ($productArray as $product) { | ||||
*/ | */ | ||||
public function addProductIncomingDistributions(Product $product): void | public function addProductIncomingDistributions(Product $product): void | ||||
{ | { | ||||
$distributionArray = $this->distributionRepository->getIncoming(); | $distributionArray = $this->distributionRepository->findDistributionsIncoming(); | ||||
foreach ($distributionArray as $distribution) { | foreach ($distributionArray as $distribution) { | ||||
$this->addProduct($distribution, $product); | $this->addProduct($distribution, $product); | ||||
} | } | ||||
{ | { | ||||
$pointSaleDistribution = $this->pointSaleDistributionBuilder->createPointSaleDistributionIfNotExist($distribution, $pointSale); | $pointSaleDistribution = $this->pointSaleDistributionBuilder->createPointSaleDistributionIfNotExist($distribution, $pointSale); | ||||
$dayString = $this->distributionSolver->getDayAsString($distribution); | $dayString = $this->distributionSolver->getDistributionDayAsString($distribution); | ||||
$pointSaleDistribution->delivery = 0; | $pointSaleDistribution->delivery = 0; | ||||
$deliveryDay = 'delivery_' . $dayString; | $deliveryDay = 'delivery_' . $dayString; |
class DistributionSolver extends BaseService implements 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)); | $dayDistribution = date('N', strtotime($distribution->date)); | ||||
use common\logic\Distribution\Distribution\Distribution; | use common\logic\Distribution\Distribution\Distribution; | ||||
use common\logic\Distribution\Distribution\DistributionSolver; | use common\logic\Distribution\Distribution\DistributionSolver; | ||||
use common\logic\Product\Product\Product; | use common\logic\Product\Product\Product; | ||||
use common\logic\Product\Product\ProductSolver; | |||||
class ProductDistributionBuilder extends BaseService implements BuilderInterface | class ProductDistributionBuilder extends BaseService implements BuilderInterface | ||||
{ | { | ||||
protected ProductDistributionRepository $productDistributionRepository; | protected ProductDistributionRepository $productDistributionRepository; | ||||
protected DistributionSolver $distributionSolver; | protected DistributionSolver $distributionSolver; | ||||
protected ProductSolver $productSolver; | |||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class); | $this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class); | ||||
$this->distributionSolver = $this->loadService(DistributionSolver::class); | $this->distributionSolver = $this->loadService(DistributionSolver::class); | ||||
$this->productSolver = $this->loadService(ProductSolver::class); | |||||
} | } | ||||
public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution | public function instanciateProductDistribution(Distribution $distribution, Product $product): ProductDistribution | ||||
{ | { | ||||
$productDistribution = new ProductDistribution(); | $productDistribution = new ProductDistribution(); | ||||
$productDistribution->populateFieldObject('id_distribution', 'distribution', $distribution); | |||||
$productDistribution->populateRelation('distribution', $distribution); | $productDistribution->populateFieldObject('id_product', 'product', $product); | ||||
$productDistribution->id_distribution = $distribution->id; | |||||
$productDistribution->populateRelation('product', $product); | |||||
$productDistribution->id_product = $product->id; | |||||
return $productDistribution; | return $productDistribution; | ||||
} | } | ||||
public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution | public function createProductDistributionIfNotExist(Distribution $distribution, Product $product): ProductDistribution | ||||
{ | { | ||||
$productDistribution = $this->productDistributionRepository->getOne($distribution, $product); | return $this->productDistributionRepository->findOneProductDistribution($distribution, $product) | ||||
?? $this->createProductDistribution($distribution, $product); | |||||
if(!$productDistribution) { | |||||
$productDistribution = $this->createProductDistribution($distribution, $product); | |||||
} | |||||
return $productDistribution; | |||||
} | } | ||||
public function initActive(ProductDistribution $productDistribution): ProductDistribution | public function initActive(ProductDistribution $productDistribution): ProductDistribution | ||||
{ | { | ||||
$day = $this->distributionSolver->getDayAsString($productDistribution->distribution); | $productDistribution->active = $this->productSolver->isProductActiveByDay( | ||||
$productDistribution->product, | |||||
$productDistribution->active = 0; | $this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) | ||||
if ($productDistribution->product->active && $productDistribution->product->$day) { | ); | ||||
$productDistribution->active = 1; | |||||
} | |||||
return $productDistribution; | return $productDistribution; | ||||
} | } | ||||
public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution | public function initQuantityMax(ProductDistribution $productDistribution): ProductDistribution | ||||
{ | { | ||||
$product = $productDistribution->product; | $productDistribution->quantity_max = $this->productSolver->getProductQuantityMaxByDay( | ||||
$day = $this->distributionSolver->getDayAsString($productDistribution->distribution); | $productDistribution->product, | ||||
$this->distributionSolver->getDistributionDayAsString($productDistribution->distribution) | |||||
$productDistribution->quantity_max = $product->quantity_max; | ); | ||||
$fieldQuantityMax = 'quantity_max_' . $day; | |||||
if (isset($product->$fieldQuantityMax) && $product->$fieldQuantityMax > 0) { | |||||
$productDistribution->quantity_max = $product->$fieldQuantityMax; | |||||
} | |||||
return $productDistribution; | return $productDistribution; | ||||
} | } |
return $product->name; | 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; | |||||
} | |||||
} | } |