@@ -261,6 +261,13 @@ class DistributionController extends BackendController | |||
$jsonProduct['taxRate'] = $this->getProducerCurrent()->taxRate->getAttributes(); | |||
} | |||
if($this->getFeatureModule()->getChecker()->isEnabled(Feature::ALIAS_ROTATING_PRODUCT)) { | |||
if($this->getRotatingModule()->getResolver()->isRotatingOfProductInDistribution($product, $distribution)) { | |||
$rotating = $this->getRotatingModule()->getResolver()->getRotatingOfProductInDistribution($product, $distribution); | |||
$jsonProduct['rotating_name'] = $rotating->getName(); | |||
} | |||
} | |||
$jsonProductsArray[] = $jsonProduct; | |||
} | |||
@@ -117,7 +117,7 @@ $this->setPageTitle('Distributions') ; | |||
<template v-if="isOneProductMaximumQuantityExceeded()"><span class="glyphicon glyphicon-alert"></span> Quantités max dépassées<br /></template> | |||
<br /> | |||
<button class="btn btn-default" @click="showModalProducts = true">Configurer</button> | |||
<button class="btn btn-default" @click="showModalRotatings = true">Tournants</button> | |||
<!--<button class="btn btn-default" @click="showModalRotatings = true">Tournants</button>--> | |||
</span> | |||
</div> | |||
</div> | |||
@@ -143,7 +143,12 @@ $this->setPageTitle('Distributions') ; | |||
<button class="btn btn-danger" v-if="getProductDistribution(product).active == 0"><span class="glyphicon glyphicon-remove"></span></button> | |||
<button class="btn btn-default" v-else data-active-product="0" :data-id-product="product.id" @click="productActiveClick"><span class="glyphicon glyphicon-remove"></span></button> | |||
</td> | |||
<td>{{ product.name }}</td> | |||
<td> | |||
<div>{{ product.name }}</div> | |||
<div class="meta"> | |||
<span v-if="product.rotating_name" class="label label-default">{{ product.rotating_name }}</span> | |||
</div> | |||
</td> | |||
<td class="quantity-ordered"> | |||
{{ product.quantity_ordered ? product.quantity_ordered + ' '+ ((product.unit == 'piece') ? ' p.' : ' '+(product.unit == 'g' || product.unit == 'kg') ? 'kg' : 'litre(s)') : '∅' }} | |||
</td> |
@@ -49,6 +49,12 @@ class ActiveRecordCommon extends \yii\db\ActiveRecord | |||
public ?string $redirect_url = ''; | |||
public function equal($record): bool | |||
{ | |||
return $this->id == $record->id; | |||
} | |||
public function populateFieldObject($fieldIdentifier, $fieldObject, $object) | |||
{ | |||
$this->{$fieldIdentifier} = $object ? $object->id : null; |
@@ -209,7 +209,9 @@ return [ | |||
], | |||
Distribution::class => [ | |||
// Subscription : génération des commandes sur base des abonnements | |||
domain\Subscription\Subscription\Event\DistributionObserver::class | |||
domain\Subscription\Subscription\Event\DistributionObserver::class, | |||
// Initialisation de l'ensemble des produits tournants au moment de l'activation d'une distribution | |||
domain\Product\Rotating\Event\DistributionObserver::class | |||
], | |||
DeliveryNote::class => [ | |||
// Order : assignation du bon de livraison aux commandes | |||
@@ -234,8 +236,8 @@ return [ | |||
domain\Producer\Producer\Event\ProducerObserver::class | |||
], | |||
Rotating::class => [ | |||
// Initialisation des produits tournants dans les distributions à venir | |||
domain\Product\Rotating\Event\RotatingObserver::class | |||
// Initialisation du produit tournant dans les distributions à venir | |||
domain\Product\Rotating\Event\RotatingObserver::class, | |||
], | |||
], | |||
], |
@@ -92,10 +92,10 @@ class ProductDistributionBuilder extends AbstractBuilder | |||
return $productDistribution; | |||
} | |||
public function updateProductDistributionActive(ProductDistribution $productDistribution, int $active) | |||
public function updateProductDistributionActive(ProductDistribution $productDistribution, int $active): bool | |||
{ | |||
$productDistribution->active = $active; | |||
$this->update($productDistribution); | |||
return $this->update($productDistribution); | |||
} | |||
public function disableProductDistributionsIncomingByProduct(Product $product) |
@@ -3,16 +3,32 @@ | |||
namespace domain\Product\Product; | |||
use domain\_\AbstractManager; | |||
use domain\Distribution\Distribution\Distribution; | |||
use domain\Distribution\ProductDistribution\ProductDistributionBuilder; | |||
use domain\Product\Accessory\Accessory; | |||
use domain\Product\ProductAccessory\ProductAccessoryManager; | |||
class ProductManager extends AbstractManager | |||
{ | |||
protected ProductAccessoryManager $productAccessoryManager; | |||
protected ProductDistributionBuilder $productDistributionBuilder; | |||
public function loadDependencies(): void | |||
{ | |||
$this->productAccessoryManager = $this->loadService(ProductAccessoryManager::class); | |||
$this->productDistributionBuilder = $this->loadService(ProductDistributionBuilder::class); | |||
} | |||
public function activateProductInDistribution(Product $product, Distribution $distribution) | |||
{ | |||
$productDistribution = $this->productDistributionBuilder->createProductDistributionIfNotExist($distribution, $product); | |||
$this->productDistributionBuilder->updateProductDistributionActive($productDistribution, 1); | |||
} | |||
public function deactivateProductInDistribution(Product $product, Distribution $distribution) | |||
{ | |||
$productDistribution = $this->productDistributionBuilder->createProductDistributionIfNotExist($distribution, $product); | |||
$this->productDistributionBuilder->updateProductDistributionActive($productDistribution, 0); | |||
} | |||
public function needAccessory(Product $product, Accessory $accessory, int $quantity): void |
@@ -0,0 +1,27 @@ | |||
<?php | |||
namespace domain\Product\Rotating\Event; | |||
use domain\Distribution\Distribution\Distribution; | |||
use domain\Distribution\Distribution\Event\DistributionActiveEvent; | |||
use domain\Product\Rotating\RotatingModule; | |||
use justcoded\yii2\eventlistener\observers\Observer; | |||
use yii\base\ErrorException; | |||
class DistributionObserver extends Observer | |||
{ | |||
public function events() | |||
{ | |||
return [ | |||
Distribution::EVENT_ACTIVE => 'onDistributionActive' | |||
]; | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ | |||
public static function onDistributionActive(DistributionActiveEvent $event): void | |||
{ | |||
RotatingModule::getInstance()->getManager()->initializeAllRotatingsInDistribution($event->distribution); | |||
} | |||
} |
@@ -9,6 +9,7 @@ use domain\Distribution\Distribution\DistributionRepository; | |||
use domain\Distribution\Distribution\DistributionResolver; | |||
use domain\Distribution\DistributionRotating\DistributionRotatingManager; | |||
use domain\Producer\Producer\Producer; | |||
use domain\Product\Product\ProductManager; | |||
use domain\Product\Product\ProductRepository; | |||
use domain\Product\RotatingProduct\RotatingProductManager; | |||
use yii\base\ErrorException; | |||
@@ -23,6 +24,7 @@ class RotatingManager extends AbstractManager | |||
protected RotatingResolver $rotatingResolver; | |||
protected DistributionResolver $distributionResolver; | |||
protected RotatingRepository $rotatingRepository; | |||
protected ProductManager $productManager; | |||
/** | |||
* @throws ErrorException | |||
@@ -37,6 +39,7 @@ class RotatingManager extends AbstractManager | |||
$this->rotatingResolver = $this->loadService(RotatingResolver::class); | |||
$this->distributionResolver = $this->loadService(DistributionResolver::class); | |||
$this->rotatingRepository = $this->loadService(RotatingRepository::class); | |||
$this->productManager = $this->loadService(ProductManager::class); | |||
} | |||
public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating | |||
@@ -93,12 +96,33 @@ class RotatingManager extends AbstractManager | |||
*/ | |||
public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution) | |||
{ | |||
$rotatingProduct = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution); | |||
$rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution); | |||
$this->distributionRotatingManager->createDistributionRotatingIfNotExist( | |||
$distribution, | |||
$rotatingProduct | |||
$rotatingProductOfDistribution | |||
); | |||
foreach($rotating->getRotatingProducts() as $rotatingProduct) { | |||
if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) { | |||
$this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution); | |||
} | |||
else { | |||
$this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution); | |||
} | |||
} | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ | |||
public function initializeAllRotatingsInDistribution(Distribution $distribution) | |||
{ | |||
$rotatingsArray = $this->rotatingRepository->findRotatings(); | |||
foreach($rotatingsArray as $rotating) { | |||
$this->initializeRotatingInDistribution($rotating, $distribution); | |||
} | |||
} | |||
public function deleteRotating(Rotating $rotating): bool |
@@ -29,6 +29,12 @@ class RotatingRepository extends AbstractRepository | |||
return $this->createDefaultQuery(); | |||
} | |||
public function findRotatings(): array | |||
{ | |||
return $this->queryRotatings() | |||
->find(); | |||
} | |||
public function findOneRotatingById(int $id): ?Rotating | |||
{ | |||
return $this->createDefaultQuery() |
@@ -6,6 +6,7 @@ use domain\_\AbstractResolver; | |||
use domain\Distribution\Distribution\Distribution; | |||
use domain\Distribution\Distribution\DistributionRepository; | |||
use domain\Distribution\DistributionRotating\DistributionRotatingRepository; | |||
use domain\Product\Product\Product; | |||
use domain\Product\RotatingProduct\RotatingProduct; | |||
use domain\Product\RotatingProduct\RotatingProductRepository; | |||
use yii\base\ErrorException; | |||
@@ -42,12 +43,14 @@ class RotatingResolver extends AbstractResolver | |||
{ | |||
$date = clone $distribution->getDate(); | |||
$previousDistribution = $this->getPreviousDistribution($rotating, $date); | |||
$previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating); | |||
if($previousDistribution) { | |||
$previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating); | |||
if($previousDistributionRotating) { | |||
$rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct(); | |||
$nextRotatingProduct = $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution); | |||
return $nextRotatingProduct; | |||
if($previousDistributionRotating) { | |||
$rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct(); | |||
$nextRotatingProduct = $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution); | |||
return $nextRotatingProduct; | |||
} | |||
} | |||
return $this->getFirstRotatingProduct($rotating); | |||
@@ -104,4 +107,21 @@ class RotatingResolver extends AbstractResolver | |||
throw new ErrorException("Le produit tournant doit contenir au moins un produit"); | |||
} | |||
} | |||
public function getRotatingOfProductInDistribution(Product $product, Distribution $distribution): ?Rotating | |||
{ | |||
$distributionRotatingsArray = $this->distributionRotatingRepository->findDistributionRotatingsByDistribution($distribution); | |||
foreach($distributionRotatingsArray as $distributionRotating) { | |||
if($distributionRotating->getRotatingProduct()->getProduct()->equal($product)) { | |||
return $distributionRotating->getRotating(); | |||
} | |||
} | |||
return null; | |||
} | |||
public function isRotatingOfProductInDistribution(Product $product, Distribution $distribution): bool | |||
{ | |||
return (bool) $this->getRotatingOfProductInDistribution($product, $distribution); | |||
} | |||
} |
@@ -19,9 +19,9 @@ class DistributionObserver extends Observer | |||
public static function onDistributionActive(DistributionActiveEvent $event): void | |||
{ | |||
$distribution = $event->distribution; | |||
$orderModule = OrderModule::getInstance(); | |||
$orderModule->getManager()->createAllOrdersFromSubscriptions($event->distribution->date, $event->userAction); | |||
OrderModule::getInstance()->getManager()->createAllOrdersFromSubscriptions( | |||
$event->distribution->date, | |||
$event->userAction | |||
); | |||
} | |||
} |
@@ -955,15 +955,15 @@ class OrderController extends ProducerBaseController | |||
$productsArray = Product::find() | |||
->where([ | |||
'id_producer' => $producer->id, | |||
'product.status' => 1, | |||
]); | |||
]) | |||
->andWhere('product.status >= 0'); | |||
$productsArray = $productsArray->joinWith([ | |||
'productDistribution' => function ($query) use ( | |||
$distribution | |||
) { | |||
$query->andOnCondition( | |||
'product_distribution.id_distribution = ' . $distribution->id | |||
'product_distribution.id_distribution = ' . $distribution->id.' AND product_distribution.active = 1' | |||
); | |||
}, | |||
'productPrice' |