Browse Source

Produits tournants : initialisation des distributions

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
cf94583968
10 changed files with 222 additions and 2 deletions
  1. +1
    -1
      common/components/BusinessLogic.php
  2. +7
    -0
      domain/Distribution/Distribution/Distribution.php
  3. +5
    -0
      domain/Distribution/Distribution/DistributionRepository.php
  4. +25
    -0
      domain/Distribution/Distribution/DistributionResolver.php
  5. +18
    -0
      domain/Distribution/DistributionRotating/DistributionRotatingManager.php
  6. +11
    -0
      domain/Distribution/DistributionRotating/DistributionRotatingRepository.php
  7. +15
    -0
      domain/Distribution/DistributionRotating/DistributionRotatingRepositoryQuery.php
  8. +32
    -0
      domain/Product/Rotating/RotatingManager.php
  9. +7
    -1
      domain/Product/Rotating/RotatingModule.php
  10. +101
    -0
      domain/Product/Rotating/RotatingResolver.php

+ 1
- 1
common/components/BusinessLogic.php View File

@@ -44,6 +44,7 @@ class BusinessLogic
$this->getSharedPointSaleModule(),
$this->getProductModule(),
$this->getRotatingProductModule(),
$this->getDistributionRotatingModule(),
$this->getRotatingModule(),
$this->getProductOrderModule(),
$this->getOrderStatusModule(),
@@ -53,7 +54,6 @@ class BusinessLogic
$this->getDeliveryNoteModule(),
$this->getDocumentModule(),
$this->getSubscriptionModule(),
$this->getDistributionRotatingModule(),
$this->getDistributionModule(),
$this->getProducerModule(),
$this->getOrderModule(),

+ 7
- 0
domain/Distribution/Distribution/Distribution.php View File

@@ -80,6 +80,13 @@ class Distribution extends ActiveRecordCommon
];
}

/* Getters / Setters */

public function getDate(): \DateTime
{
return new \DateTime($this->date);
}

/*
* Relations
*/

+ 5
- 0
domain/Distribution/Distribution/DistributionRepository.php View File

@@ -49,6 +49,11 @@ class DistributionRepository extends AbstractRepository
return $query->findOne();
}

public function findOneDistributionByDateTime(\DateTime $dateTime, bool $active = null): ?Distribution
{
return $this->findOneDistribution($dateTime->format('Y-m-d'), $active);
}

public function findOneFirstDistribution(): ?Distribution
{
return $this->createDefaultQuery()

+ 25
- 0
domain/Distribution/Distribution/DistributionResolver.php View File

@@ -0,0 +1,25 @@
<?php

namespace domain\Distribution\Distribution;

use domain\_\AbstractResolver;

class DistributionResolver extends AbstractResolver
{
protected DistributionRepository $distributionRepository;

public function loadDependencies(): void
{
$this->distributionRepository = $this->loadService(DistributionRepository::class);
}

public function getDateLastDistributionIncoming(): ?\DateTime
{
$lastDistribution = $this->distributionRepository->findOneLastDistribution();
if($lastDistribution) {
return $lastDistribution->getDate();
}

return null;
}
}

+ 18
- 0
domain/Distribution/DistributionRotating/DistributionRotatingManager.php View File

@@ -9,10 +9,12 @@ use domain\Product\RotatingProduct\RotatingProduct;
class DistributionRotatingManager extends AbstractManager
{
protected DistributionRotatingBuilder $distributionRotatingBuilder;
protected DistributionRotatingRepository $distributionRotatingRepository;

public function loadDependencies(): void
{
$this->distributionRotatingBuilder = $this->loadService(DistributionRotatingBuilder::class);
$this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
}

public function createDistributionRotating(Distribution $distribution, RotatingProduct $rotatingProduct): DistributionRotating
@@ -20,7 +22,23 @@ class DistributionRotatingManager extends AbstractManager
$distributionRotating = $this->distributionRotatingBuilder->instanciateDistributionRotating();
$distributionRotating
->setDistribution($distribution)
->setRotating($rotatingProduct->getRotating())
->setRotatingProduct($rotatingProduct);

return $distributionRotating;
}

public function createDistributionRotatingIfNotExist(Distribution $distribution, RotatingProduct $rotatingProduct): DistributionRotating
{
$distributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($distribution, $rotatingProduct->getRotating());
if($distributionRotating) {
$distributionRotating->setRotatingProduct($rotatingProduct);
$distributionRotating->save();

return $distributionRotating;
}
else {
return $this->createDistributionRotating($distribution, $rotatingProduct);
}
}
}

+ 11
- 0
domain/Distribution/DistributionRotating/DistributionRotatingRepository.php View File

@@ -3,6 +3,9 @@
namespace domain\Distribution\DistributionRotating;

use domain\_\AbstractRepository;
use domain\Distribution\Distribution\Distribution;
use domain\Product\Rotating\Rotating;
use domain\Product\RotatingProduct\RotatingProduct;

class DistributionRotatingRepository extends AbstractRepository
{
@@ -22,4 +25,12 @@ class DistributionRotatingRepository extends AbstractRepository
self::ATTRIBUTE_ID_PRODUCER => ''
];
}

public function findOneDistributionRotating(Distribution $distribution, Rotating $rotating): ?DistributionRotating
{
return $this->createQuery()
->filterByDistribution($distribution)
->filterByRotating($rotating)
->findOne();
}
}

+ 15
- 0
domain/Distribution/DistributionRotating/DistributionRotatingRepositoryQuery.php View File

@@ -3,6 +3,9 @@
namespace domain\Distribution\DistributionRotating;

use domain\_\AbstractRepositoryQuery;
use domain\Distribution\Distribution\Distribution;
use domain\Product\Rotating\Rotating;
use domain\Product\RotatingProduct\RotatingProduct;

class DistributionRotatingRepositoryQuery extends AbstractRepositoryQuery
{
@@ -12,4 +15,16 @@ class DistributionRotatingRepositoryQuery extends AbstractRepositoryQuery
{
$this->loadDefinition(DistributionRotatingDefinition::class);
}

public function filterByDistribution(Distribution $distribution): self
{
$this->andWhere(['distribution_rotating.id_distribution' => $distribution->id]);
return $this;
}

public function filterByRotating(Rotating $rotating): self
{
$this->andWhere(['distribution_rotating.id_rotating' => $rotating->getId()]);
return $this;
}
}

+ 32
- 0
domain/Product/Rotating/RotatingManager.php View File

@@ -2,22 +2,35 @@

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
@@ -44,6 +57,25 @@ class RotatingManager extends AbstractManager
}
}

/**
* @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);

+ 7
- 1
domain/Product/Rotating/RotatingModule.php View File

@@ -10,8 +10,9 @@ class RotatingModule extends AbstractModule
{
return [
RotatingDefinition::class,
RotatingBuilder::class,
RotatingRepository::class,
RotatingBuilder::class,
RotatingResolver::class,
RotatingManager::class
];
}
@@ -31,6 +32,11 @@ class RotatingModule extends AbstractModule
return RotatingRepository::getInstance();
}

public function getResolver(): RotatingResolver
{
return RotatingResolver::getInstance();
}

public function getManager(): RotatingManager
{
return RotatingManager::getInstance();

+ 101
- 0
domain/Product/Rotating/RotatingResolver.php View File

@@ -0,0 +1,101 @@
<?php

namespace domain\Product\Rotating;

use domain\_\AbstractResolver;
use domain\Distribution\Distribution\Distribution;
use domain\Distribution\Distribution\DistributionRepository;
use domain\Distribution\DistributionRotating\DistributionRotatingRepository;
use domain\Product\RotatingProduct\RotatingProduct;
use domain\Product\RotatingProduct\RotatingProductRepository;
use yii\base\ErrorException;

class RotatingResolver extends AbstractResolver
{
protected DistributionRepository $distributionRepository;
protected DistributionRotatingRepository $distributionRotatingRepository;

public function loadDependencies(): void
{
$this->distributionRepository = $this->loadService(DistributionRepository::class);
$this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
}

public function getPreviousDistribution(Rotating $rotating, \DateTime $date): ?Distribution
{
$date = $date->modify('previous '.$rotating->getDayAsString());
return $this->distributionRepository->findOneDistributionByDateTime($date);
}

public function getNextDistribution(Rotating $rotating, \DateTime $date): ?Distribution
{
$date = $date->modify('next '.$rotating->getDayAsString());
return $this->distributionRepository->findOneDistributionByDateTime($date);
}

/**
* @throws ErrorException
*/
public function deductNextRotatingProductOfDistribution(Rotating $rotating, Distribution $distribution): RotatingProduct
{
$date = clone $distribution->getDate();
$previousDistribution = $this->getPreviousDistribution($rotating, $date);
$previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating);
if($previousDistributionRotating) {
$rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct();
return $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
}

return $this->getFirstRotatingProduct($rotating);
}

/**
* @throws ErrorException
*/
public function getNextRotatingProduct(Rotating $rotating, RotatingProduct $currentRotatingProduct = null): RotatingProduct
{
if(is_null($currentRotatingProduct)) {
return $this->getFirstRotatingProduct($rotating);
}

$rotatingProductReturn = null;
$nextIsTheOne = false;
foreach($rotating->getRotatingProducts() as $rotatingProduct) {
if($nextIsTheOne) {
return $rotatingProduct;
}
if($rotatingProduct->getId() == $currentRotatingProduct->getId()) {
$nextIsTheOne = true;
}
}

if($nextIsTheOne) {
$rotatingProductReturn = $this->getFirstRotatingProduct($rotating);
}

if($rotatingProductReturn) {
return $rotatingProductReturn;
}
else {
throw new ErrorException("Le produit tournant suivant n'a pas été trouvé");
}
}

/**
* @throws ErrorException
*/
public function getFirstRotatingProduct(Rotating $rotating): RotatingProduct
{
$rotatingProductReturn = null;
foreach($rotating->getRotatingProducts() as $rotatingProduct) {
$rotatingProductReturn = $rotatingProduct;
}

if($rotatingProductReturn) {
return $rotatingProductReturn;
}
else {
throw new ErrorException("Le produit tournant doit contenir au moins un produit");
}
}
}

Loading…
Cancel
Save