@@ -41,6 +41,7 @@ namespace backend\controllers; | |||
use domain\Feature\Feature\Feature; | |||
use domain\Product\Rotating\Rotating; | |||
use yii\filters\AccessControl; | |||
use yii\helpers\Html; | |||
use yii\web\NotFoundHttpException; | |||
use yii\web\Response; | |||
@@ -89,11 +90,12 @@ class RotatingController extends BackendController | |||
$rotatingModel->getDay(), | |||
$rotatingModel->getSelectedProductsIds() | |||
); | |||
$this->setFlash('success', "Produit tournant ajouté"); | |||
$this->setFlash('success', "Produit tournant <strong>".Html::encode($rotating->getName())."</strong> ajouté"); | |||
return $this->redirectAfterSave('rotating', $rotating->getId()); | |||
} | |||
return $this->render('create', [ | |||
'rotatingModel' => $rotatingModel, | |||
'rotating' => $this->initFormModel($rotatingModel), | |||
'productsArray' => $this->findProducts() | |||
]); | |||
@@ -103,15 +105,17 @@ class RotatingController extends BackendController | |||
{ | |||
$rotating = $this->findRotating($id); | |||
if ($rotating->load(\Yii::$app->request->post()) && $rotating->validate() && $rotating->save()) { | |||
$this->getRotatingModule()->getManager()->manageRotatingProducts( | |||
if ($rotating->load(\Yii::$app->request->post()) && $rotating->validate()) { | |||
$this->getRotatingModule()->getManager()->updateRotating( | |||
$rotating, | |||
$rotating->getSelectedProductsIds() | |||
); | |||
$this->setFlash('success', "Produit tournant <strong>".Html::encode($rotating->getName())."</strong> modifié"); | |||
return $this->redirectAfterSave('rotating', $rotating->getId()); | |||
} | |||
return $this->render('update', [ | |||
'rotatingModel' => $rotating, | |||
'rotating' => $this->initFormModel($rotating), | |||
'productsArray' => $this->findProducts() | |||
]); | |||
@@ -147,81 +151,4 @@ class RotatingController extends BackendController | |||
$this->getRotatingModule()->getBuilder()->initSelectedProductsIds($rotating); | |||
return $rotating; | |||
} | |||
/*public function actionCreate() | |||
{ | |||
$accessoryModule = $this->getAccessoryModule(); | |||
$accessoryModel = $accessoryModule->getBuilder()->instanciateAccessory($this->getProducerCurrent()); | |||
if ($accessoryModel->load(\Yii::$app->request->post()) && $accessoryModel->validate()) { | |||
$accessory = $accessoryModule->getManager()->createAccessory( | |||
$this->getProducerCurrent(), | |||
$accessoryModel->getName(), | |||
$accessoryModel->getQuantity() | |||
); | |||
$this->afterSave($accessory, $accessoryModel->getSelectedProductsIds()); | |||
$this->setFlash('success', "Accessoire ajouté"); | |||
return $this->redirectAfterSave('accessory', $accessory->getId()); | |||
} | |||
return $this->render('create', [ | |||
'accessory' => $this->initFormModel($accessoryModel), | |||
'productsArray' => $this->findProducts() | |||
]); | |||
} | |||
public function actionUpdate(int $id) | |||
{ | |||
$accessory = $this->findAccessory($id); | |||
if ($accessory->load(\Yii::$app->request->post()) && $accessory->validate() && $accessory->save()) { | |||
$this->afterSave($accessory, $accessory->getSelectedProductsIds()); | |||
return $this->redirectAfterSave('accessory', $accessory->getId()); | |||
} | |||
return $this->render('update', [ | |||
'accessory' => $this->initFormModel($accessory), | |||
'productsArray' => $this->findProducts() | |||
]); | |||
} | |||
public function actionDelete(int $id): Response | |||
{ | |||
$accessory = $this->findAccessory($id); | |||
if($accessory->delete()) { | |||
$this->setFlash('success', "Accessoire supprimé"); | |||
} | |||
return $this->redirect('index'); | |||
} | |||
public function afterSave(Accessory $accessory, $selectedProductsIdsArray): void | |||
{ | |||
if(!is_array($selectedProductsIdsArray)) { | |||
$selectedProductsIdsArray = []; | |||
} | |||
$this->getAccessoryModule()->getManager()->manageProducts($accessory, $selectedProductsIdsArray); | |||
} | |||
public function findProducts(): array | |||
{ | |||
return $this->getProductModule()->getRepository()->findProducts(true); | |||
} | |||
protected function findAccessory($id): Accessory | |||
{ | |||
if (($accessory = $this->getAccessoryModule()->getRepository()->findOneAccessoryById($id)) !== null) { | |||
return $accessory; | |||
} else { | |||
throw new NotFoundHttpException("L'accessoire n'a pas été trouvé."); | |||
} | |||
} | |||
public function initFormModel(Accessory $accessoryModel) | |||
{ | |||
$this->getAccessoryModule()->getBuilder()->initSelectedProductsIds($accessoryModel); | |||
return $accessoryModel; | |||
} | |||
*/ | |||
} |
@@ -76,6 +76,9 @@ use yii\widgets\ActiveForm; | |||
</h3> | |||
</div> | |||
<div class="panel-body"> | |||
<div class="has-error"> | |||
<?= Html::error($rotatingModel,'selected_products_ids', ['class' => 'help-block']) ?> | |||
</div> | |||
<?= Html::activeCheckboxList($rotating, 'selected_products_ids', ArrayHelper::map($productsArray, 'id', function ($product, $defaultValue) { | |||
return Html::encode($product->name); | |||
}), ['encode' => false, 'class' => '']) ?> |
@@ -44,6 +44,7 @@ $this->addBreadcrumb('Ajouter') ; | |||
<div class="rotating-create"> | |||
<?= $this->render('_form', [ | |||
'rotatingModel' => $rotatingModel, | |||
'rotating' => $rotating, | |||
'productsArray' => $productsArray | |||
]) ?> |
@@ -45,6 +45,8 @@ use yii\grid\GridView; | |||
* @var View $this | |||
*/ | |||
$rotatingModule = $this->getRotatingModule(); | |||
$this->setTitle('Produits tournans'); | |||
$this->addBreadcrumb($this->getTitle()); | |||
$this->addButton(['label' => 'Nouveau produit tournant <span class="glyphicon glyphicon-plus"></span>', 'url' => 'rotating/create', 'class' => 'btn btn-primary']); | |||
@@ -71,6 +73,13 @@ $this->addButton(['label' => 'Nouveau produit tournant <span class="glyphicon gl | |||
}, $rotating->getRotatingProducts())); | |||
} | |||
], | |||
[ | |||
'label' => 'Distributions à venir', | |||
'format' => 'raw', | |||
'value' => function($rotating) use ($rotatingModule) { | |||
return $rotatingModule->getResolver()->getRotatingProductsOfIncomingDistributionsAsHtml($rotating); | |||
} | |||
], | |||
[ | |||
'class' => 'yii\grid\ActionColumn', | |||
'template' => '{update} {delete}', |
@@ -47,6 +47,7 @@ $this->addBreadcrumb('Modifier') ; | |||
<div class="rotating-update"> | |||
<?= $this->render('_form', [ | |||
'rotatingModel' => $rotatingModel, | |||
'rotating' => $rotating, | |||
'productsArray' => $productsArray | |||
]) ?> |
@@ -39,6 +39,7 @@ | |||
namespace domain\Distribution\Distribution; | |||
use common\components\ActiveRecordCommon; | |||
use domain\Distribution\DistributionRotating\DistributionRotating; | |||
use domain\Distribution\PointSaleDistribution\PointSaleDistribution; | |||
use domain\Distribution\ProductDistribution\ProductDistribution; | |||
use domain\Order\Order\Order; | |||
@@ -87,6 +88,11 @@ class Distribution extends ActiveRecordCommon | |||
return new \DateTime($this->date); | |||
} | |||
public function getDistributionRotatings(): array | |||
{ | |||
return $this->distributionRotatingsRelation; | |||
} | |||
/* | |||
* Relations | |||
*/ | |||
@@ -115,4 +121,9 @@ class Distribution extends ActiveRecordCommon | |||
{ | |||
return $this->hasMany(PointSaleDistribution::class, ['id_distribution' => 'id']); | |||
} | |||
public function getDistributionRotatingsRelation() | |||
{ | |||
return $this->hasMany(DistributionRotating::class, ['id_distribution' => 'id']); | |||
} | |||
} |
@@ -2,22 +2,30 @@ | |||
namespace domain\Product\Rotating\Event; | |||
use domain\Product\Rotating\Rotating; | |||
use domain\Product\Rotating\RotatingModule; | |||
use justcoded\yii2\eventlistener\observers\ActiveRecordObserver; | |||
use justcoded\yii2\eventlistener\observers\Observer; | |||
use yii\base\ErrorException; | |||
class RotatingObserver extends ActiveRecordObserver | |||
class RotatingObserver extends Observer | |||
{ | |||
public function inserted(\yii\db\AfterSaveEvent $event) | |||
public function events(): array | |||
{ | |||
//RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender); | |||
return [ | |||
RotatingSaveEvent::NAME => 'onRotatingSave' | |||
]; | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ | |||
public function updated(\yii\db\AfterSaveEvent $event) | |||
public function onRotatingSave(RotatingSaveEvent $event) | |||
{ | |||
//RotatingModule::getInstance()->getManager()->initializeRotatingInDistributionsIncoming($event->sender); | |||
$rotatingModule = RotatingModule::getInstance(); | |||
$rotating = $event->rotating; | |||
// Obligé de recharger le Rotating | |||
$rotating = $rotatingModule->getRepository()->findOneRotatingById($rotating->getId()); | |||
$rotatingModule->getManager()->initializeRotatingInDistributionsIncoming($rotating); | |||
} | |||
} |
@@ -0,0 +1,13 @@ | |||
<?php | |||
namespace domain\Product\Rotating\Event; | |||
use yii\base\Event; | |||
use domain\Product\Rotating\Rotating; | |||
class RotatingSaveEvent extends Event | |||
{ | |||
const NAME = 'rotating.save.event'; | |||
public Rotating $rotating; | |||
} |
@@ -22,9 +22,10 @@ class Rotating extends ActiveRecordCommon | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_producer', 'name', 'day'], 'required'], | |||
[['id_producer', 'name', 'day', 'selected_products_ids'], 'required'], | |||
[['name'], 'string', 'max' => 255], | |||
[['id_producer', 'day', 'status'], 'integer'], | |||
[['selected_products_ids'], 'verifySelectedProductsIds'], | |||
[['selected_products_ids'], 'safe'], | |||
]; | |||
} | |||
@@ -37,9 +38,17 @@ class Rotating extends ActiveRecordCommon | |||
'name' => 'Nom', | |||
'day' => 'Jour', | |||
'status' => 'Statut', | |||
'selected_products_ids' => "Produits" | |||
]; | |||
} | |||
public function verifySelectedProductsIds($attribute, $params) | |||
{ | |||
if(count($this->getSelectedProductsIds()) < 2) { | |||
$this->addError($attribute, "Vous devez sélectionner au moins deux produits."); | |||
} | |||
} | |||
/* Getters / Setters */ | |||
public function getId(): ?int |
@@ -2,7 +2,6 @@ | |||
namespace domain\Product\Rotating; | |||
use common\components\Date; | |||
use domain\_\AbstractManager; | |||
use domain\Distribution\Distribution\Distribution; | |||
use domain\Distribution\Distribution\DistributionRepository; | |||
@@ -11,6 +10,7 @@ use domain\Distribution\DistributionRotating\DistributionRotatingManager; | |||
use domain\Producer\Producer\Producer; | |||
use domain\Product\Product\ProductManager; | |||
use domain\Product\Product\ProductRepository; | |||
use domain\Product\Rotating\Event\RotatingSaveEvent; | |||
use domain\Product\RotatingProduct\RotatingProductManager; | |||
use yii\base\ErrorException; | |||
@@ -42,21 +42,37 @@ class RotatingManager extends AbstractManager | |||
$this->productManager = $this->loadService(ProductManager::class); | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ | |||
public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating | |||
{ | |||
$rotating = $this->rotatingBuilder->instanciateRotating($producer); | |||
$rotating->setName($name); | |||
$rotating->setDay($day); | |||
$rotating->save(); | |||
$this->afterSaveRotating($rotating, $productsIdsArray); | |||
return $rotating; | |||
} | |||
public function updateRotating(Rotating $rotating, array $productsIdsArray): bool | |||
{ | |||
$return = $rotating->save(); | |||
$this->afterSaveRotating($rotating, $productsIdsArray); | |||
return $return; | |||
} | |||
public function afterSaveRotating(Rotating $rotating, array $productsIdsArray) | |||
{ | |||
$this->manageRotatingProducts($rotating, $productsIdsArray); | |||
return $rotating; | |||
$rotating->triggerEvent(RotatingSaveEvent::class, [ | |||
'rotating' => $rotating | |||
]); | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ | |||
public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void | |||
{ | |||
$this->deleteAllRotatingProducts($rotating); | |||
@@ -67,13 +83,6 @@ 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); | |||
} | |||
/** | |||
@@ -85,9 +94,13 @@ class RotatingManager extends AbstractManager | |||
$distribution = $this->rotatingResolver->getNextDistribution($rotating, $date); | |||
$dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming(); | |||
while($distribution && $dateLastDistributionIncoming && $date < $dateLastDistributionIncoming) { | |||
$this->initializeRotatingInDistribution($rotating, $distribution); | |||
$distribution = $this->rotatingResolver->getNextDistribution($rotating, $date); | |||
if($dateLastDistributionIncoming) { | |||
while($date < $dateLastDistributionIncoming) { | |||
if($distribution) { | |||
$this->initializeRotatingInDistribution($rotating, $distribution); | |||
} | |||
$distribution = $this->rotatingResolver->getNextDistribution($rotating, $date); | |||
} | |||
} | |||
} | |||
@@ -10,6 +10,7 @@ use domain\Product\Product\Product; | |||
use domain\Product\RotatingProduct\RotatingProduct; | |||
use domain\Product\RotatingProduct\RotatingProductRepository; | |||
use yii\base\ErrorException; | |||
use yii\helpers\Html; | |||
class RotatingResolver extends AbstractResolver | |||
{ | |||
@@ -124,4 +125,28 @@ class RotatingResolver extends AbstractResolver | |||
{ | |||
return (bool) $this->getRotatingOfProductInDistribution($product, $distribution); | |||
} | |||
public function getRotatingProductsOfIncomingDistributionsAsHtml(Rotating $rotating): string | |||
{ | |||
$html = ''; | |||
$countDistributionsDisplayed = 0; | |||
$numberDistributionsDisplayed = count($rotating->getRotatingProducts()); | |||
$incomingDistributionsArray = $this->distributionRepository->findDistributionsIncoming(); | |||
foreach($incomingDistributionsArray as $distribution) { | |||
foreach($distribution->getDistributionRotatings() as $distributionRotating) { | |||
if($distributionRotating->getRotating()->equal($rotating)) { | |||
$html .= $distribution->getDate()->format('d/m/Y'). ' : ' | |||
.Html::encode($distributionRotating->getRotatingProduct()->getProduct()->name).'<br />'; | |||
$countDistributionsDisplayed ++; | |||
} | |||
} | |||
if($countDistributionsDisplayed >= $numberDistributionsDisplayed) { | |||
break; | |||
} | |||
} | |||
return $html; | |||
} | |||
} |