Browse Source

Produits tournants

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
cebbe010b4
9 changed files with 82 additions and 27 deletions
  1. +1
    -1
      common/components/ActiveRecordCommon.php
  2. +25
    -12
      common/components/Date.php
  3. +4
    -4
      domain/Distribution/Distribution/DistributionResolver.php
  4. +1
    -1
      domain/Distribution/DistributionRotating/DistributionRotating.php
  5. +6
    -1
      domain/Distribution/DistributionRotating/DistributionRotatingManager.php
  6. +6
    -2
      domain/Product/Rotating/Event/RotatingObserver.php
  7. +13
    -2
      domain/Product/Rotating/Rotating.php
  8. +20
    -1
      domain/Product/Rotating/RotatingManager.php
  9. +6
    -3
      domain/Product/Rotating/RotatingResolver.php

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

@@ -51,7 +51,7 @@ class ActiveRecordCommon extends \yii\db\ActiveRecord

public function populateFieldObject($fieldIdentifier, $fieldObject, $object)
{
$this->{$fieldIdentifier} = $object->id;
$this->{$fieldIdentifier} = $object ? $object->id : null;
$this->populateRelation($fieldObject, $object);
}


+ 25
- 12
common/components/Date.php View File

@@ -4,22 +4,35 @@ namespace common\components;

class Date
{
public static function getDaysOfWeekArray(): array
public static function getDaysOfWeekArray(string $lang = 'fr'): array
{
return [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
6 => 'Samedi',
7 => 'Dimanche'
];
if($lang == 'fr') {
return [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
6 => 'Samedi',
7 => 'Dimanche'
];
}
else {
return [
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
];
}
}

public static function getDayOfWeekStringByNumber(int $dayOfWeekNumber): string
public static function getDayOfWeekStringByNumber(int $dayOfWeekNumber, string $lang = 'fr'): string
{
$daysOfWeekArray = self::getDaysOfWeekArray();
$daysOfWeekArray = self::getDaysOfWeekArray($lang);
return $daysOfWeekArray[$dayOfWeekNumber];
}
}

+ 4
- 4
domain/Distribution/Distribution/DistributionResolver.php View File

@@ -15,11 +15,11 @@ class DistributionResolver extends AbstractResolver

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

return null;
}
}
}

+ 1
- 1
domain/Distribution/DistributionRotating/DistributionRotating.php View File

@@ -63,7 +63,7 @@ class DistributionRotating extends ActiveRecordCommon
return $this->rotatingProductRelation;
}

public function setRotatingProduct(RotatingProduct $rotatingProduct): self
public function setRotatingProduct(?RotatingProduct $rotatingProduct): self
{
$this->populateFieldObject('id_rotating_product', 'rotatingProductRelation', $rotatingProduct);
return $this;

+ 6
- 1
domain/Distribution/DistributionRotating/DistributionRotatingManager.php View File

@@ -24,13 +24,18 @@ class DistributionRotatingManager extends AbstractManager
->setDistribution($distribution)
->setRotating($rotatingProduct->getRotating())
->setRotatingProduct($rotatingProduct);
$distributionRotating->save();

return $distributionRotating;
}

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

if($distributionRotating) {
$distributionRotating->setRotatingProduct($rotatingProduct);
$distributionRotating->save();

+ 6
- 2
domain/Product/Rotating/Event/RotatingObserver.php View File

@@ -4,16 +4,20 @@ namespace domain\Product\Rotating\Event;

use domain\Product\Rotating\RotatingModule;
use justcoded\yii2\eventlistener\observers\ActiveRecordObserver;
use yii\base\ErrorException;

class RotatingObserver extends ActiveRecordObserver
{
public function inserted(\yii\db\AfterSaveEvent $event)
{
RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender);
//RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender);
}

/**
* @throws ErrorException
*/
public function updated(\yii\db\AfterSaveEvent $event)
{
RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender);
//RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender);
}
}

+ 13
- 2
domain/Product/Rotating/Rotating.php View File

@@ -4,6 +4,7 @@ namespace domain\Product\Rotating;

use common\components\ActiveRecordCommon;
use common\components\Date;
use domain\Distribution\DistributionRotating\DistributionRotating;
use domain\Producer\Producer\Producer;
use domain\Product\RotatingProduct\RotatingProduct;
use yii\db\ActiveQuery;
@@ -101,6 +102,11 @@ class Rotating extends ActiveRecordCommon
return $this;
}

public function getDistributionRotatings(): array
{
return $this->distributionRotatingsRelation;
}

public function getRotatingProducts(): array
{
return $this->rotatingProductsRelation;
@@ -118,10 +124,15 @@ class Rotating extends ActiveRecordCommon
return $this->hasMany(RotatingProduct::class, ['id_rotating' => 'id']);
}

public function getDistributionRotatingsRelation()
{
return $this->hasMany(DistributionRotating::class, ['id_rotating' => 'id']);
}

/* Méthodes */

public function getDayAsString(): string
public function getDayAsString(string $lang = 'fr'): string
{
return Date::getDayOfWeekStringByNumber($this->getDay());
return Date::getDayOfWeekStringByNumber($this->getDay(), $lang);
}
}

+ 20
- 1
domain/Product/Rotating/RotatingManager.php View File

@@ -21,6 +21,8 @@ class RotatingManager extends AbstractManager
protected DistributionRepository $distributionRepository;
protected DistributionRotatingManager $distributionRotatingManager;
protected RotatingResolver $rotatingResolver;
protected DistributionResolver $distributionResolver;
protected RotatingRepository $rotatingRepository;

/**
* @throws ErrorException
@@ -33,6 +35,8 @@ class RotatingManager extends AbstractManager
$this->distributionRepository = $this->loadService(DistributionRepository::class);
$this->distributionRotatingManager = $this->loadService(DistributionRotatingManager::class);
$this->rotatingResolver = $this->loadService(RotatingResolver::class);
$this->distributionResolver = $this->loadService(DistributionResolver::class);
$this->rotatingRepository = $this->loadService(RotatingRepository::class);
}

public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
@@ -47,6 +51,9 @@ class RotatingManager extends AbstractManager
return $rotating;
}

/**
* @throws ErrorException
*/
public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
{
$this->deleteAllRotatingProducts($rotating);
@@ -57,6 +64,13 @@ class RotatingManager extends AbstractManager
$this->rotatingProductManager->createRotatingProduct($rotating, $product);
}
}


// Rechargement obligatoire car rotatingProductsRelation a changé juste au-dessus
// Possible de gérer ça via unset($rotating->rotatingProductsRelation) ?
$rotating = $this->rotatingRepository->findOneRotatingById($rotating->getId());
// @TODO : gérer via événement
$this->initializeRotatingInDistributionsIncoming($rotating);
}

/**
@@ -68,10 +82,11 @@ class RotatingManager extends AbstractManager
$distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
$dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();

while($distribution && $date < $dateLastDistributionIncoming) {
while($distribution && $dateLastDistributionIncoming && $date < $dateLastDistributionIncoming) {
$this->initializeRotatingInDistribution($rotating, $distribution);
$distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
}
die();
}

/**
@@ -94,6 +109,10 @@ class RotatingManager extends AbstractManager

public function deleteAllRotatingProducts(Rotating $rotating): void
{
foreach($rotating->getDistributionRotatings() as $distributionRotating) {
$distributionRotating->delete();
}

foreach($rotating->getRotatingProducts() as $rotatingProduct) {
$rotatingProduct->delete();
}

+ 6
- 3
domain/Product/Rotating/RotatingResolver.php View File

@@ -23,13 +23,13 @@ class RotatingResolver extends AbstractResolver

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

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

@@ -41,9 +41,11 @@ class RotatingResolver extends AbstractResolver
$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);
$nextRotatingProduct = $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
return $nextRotatingProduct;
}

return $this->getFirstRotatingProduct($rotating);
@@ -64,6 +66,7 @@ class RotatingResolver extends AbstractResolver
if($nextIsTheOne) {
return $rotatingProduct;
}

if($rotatingProduct->getId() == $currentRotatingProduct->getId()) {
$nextIsTheOne = true;
}

Loading…
Cancel
Save