@@ -95,7 +95,7 @@ class SubscriptionController extends BackendController | |||
$producerModule = $this->getProducerModule(); | |||
$productModule = $this->getProductModule(); | |||
$subscriptionManger = $this->getSubscriptionModule(); | |||
$distributionModule = $this-> getDistributionModule(); | |||
$distributionModule = $this->getDistributionModule(); | |||
$model = new SubscriptionForm(); | |||
$model->isAdmin = true; | |||
@@ -188,7 +188,12 @@ class SubscriptionController extends BackendController | |||
$arrayProductsSubscription = $productSubscriptionModule->findProductSubscriptionsBySubscription($subscription); | |||
foreach ($arrayProductsSubscription as $productSubscription) { | |||
$model->products['product_' . $productSubscription->id_product] = $productSubscription->quantity; | |||
if($productSubscription->getProduct()) { | |||
$model->products['product_' . $productSubscription->id_product] = $productSubscription->getQuantity(); | |||
} | |||
elseif($productSubscription->getRotating()) { | |||
$model->rotatings['rotating_' . $productSubscription->id_rotating] = $productSubscription->getQuantity(); | |||
} | |||
} | |||
} else { | |||
throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); | |||
@@ -292,6 +297,7 @@ class SubscriptionController extends BackendController | |||
{ | |||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||
$subscription = $idSubscription ? $this->getSubscriptionModule()->getRepository()->findOneSubscriptionById($idSubscription) : null; | |||
$rotatingModule = $this->getRotatingModule(); | |||
$unitModule = $this->getUnitModule(); | |||
@@ -306,8 +312,8 @@ class SubscriptionController extends BackendController | |||
}]); | |||
} | |||
// Produits | |||
$productsArray = $productsQuery->asArray()->orderBy('order ASC')->all(); | |||
foreach ($productsArray as &$theProduct) { | |||
$theProduct['wording_unit'] = $unitModule->getSolver()->strUnit($theProduct['unit']); | |||
@@ -322,8 +328,10 @@ class SubscriptionController extends BackendController | |||
$rotatingsArrayReturn = []; | |||
$rotatingsArray = $rotatingModule->getRepository()->findRotatings(); | |||
foreach($rotatingsArray as $rotating) { | |||
$productSubscription = $this->getProductSubscriptionModule()->getRepository() | |||
->findOneProductSubscriptionBySubscriptionAndRotating($subscription, $rotating); | |||
$rotatingsArrayReturn[] = array_merge($rotating->getAttributes(), [ | |||
'quantity' => '' | |||
'quantity' => $productSubscription ? $productSubscription->getQuantity() : '' | |||
]); | |||
} | |||
@@ -43,6 +43,7 @@ use domain\Product\Product\Product; | |||
use domain\Product\Product\ProductModule; | |||
use domain\Subscription\Subscription\Subscription; | |||
use domain\Subscription\Subscription\SubscriptionModule; | |||
use domain\Subscription\Subscription\SubscriptionRepository; | |||
use domain\User\User\UserModule; | |||
use yii\grid\GridView; | |||
use yii\helpers\ArrayHelper; | |||
@@ -55,8 +56,6 @@ $this->setTitle('Abonnements') ; | |||
$this->addBreadcrumb($this->getTitle()) ; | |||
$this->addButton(['label' => 'Nouvel abonnement <span class="glyphicon glyphicon-plus"></span>', 'url' => 'subscription/create', 'class' => 'btn btn-primary']) ; | |||
$subscriptionsArray = Subscription::searchAll() ; | |||
?> | |||
<div class="subscription-index"> | |||
<?= GridView::widget([ | |||
@@ -91,8 +90,11 @@ $subscriptionsArray = Subscription::searchAll() ; | |||
$html = '' ; | |||
foreach($model->productSubscription as $productSubscription) | |||
{ | |||
if(isset($productSubscription->product)) { | |||
$html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity * Product::$unitsArray[$productSubscription->product->unit]['coefficient']).' '. $productModule->getSolver()->strUnit($productSubscription->product, 'wording_short').')<br />' ; | |||
if($productSubscription->getProduct()) { | |||
$html .= Html::encode($productSubscription->getProduct()->name).' ('.($productSubscription->getQuantity() * Product::$unitsArray[$productSubscription->getProduct()->unit]['coefficient']).' '. $productModule->getSolver()->strUnit($productSubscription->getProduct(), 'wording_short').')<br />' ; | |||
} | |||
elseif($productSubscription->getRotating()) { | |||
$html .= Html::encode($productSubscription->getRotating()->getName()).' ('.($productSubscription->getQuantity()).' p.)<br />' ; | |||
} | |||
else { | |||
$html .= 'Produit non défini<br />' ; |
@@ -208,10 +208,10 @@ return [ | |||
domain\User\UserProducer\Event\PaymentObserver::class | |||
], | |||
Distribution::class => [ | |||
// Initialisation de l'ensemble des produits tournants au moment de l'activation d'une distribution | |||
domain\Product\Rotating\Event\DistributionObserver::class, | |||
// Subscription : génération des commandes sur base des abonnements | |||
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 |
@@ -40,7 +40,10 @@ namespace common\forms; | |||
use domain\Product\Product\Product; | |||
use domain\Product\Product\ProductModule; | |||
use domain\Product\Product\ProductRepository; | |||
use domain\Product\Rotating\RotatingRepository; | |||
use domain\Subscription\ProductSubscription\ProductSubscription; | |||
use domain\Subscription\ProductSubscription\ProductSubscriptionManager; | |||
use domain\Subscription\Subscription\Subscription; | |||
use domain\Subscription\Subscription\SubscriptionModule; | |||
use domain\User\User\UserModule; | |||
@@ -123,7 +126,7 @@ class SubscriptionForm extends Model | |||
$subscriptionModule = SubscriptionModule::getInstance(); | |||
if ($this->id) { | |||
$subscription = Subscription::searchOne(['id' => $this->id]) ; | |||
$subscription = Subscription::searchOne(['id' => $this->id]) ; | |||
$subscription->populateUpdatedBy($userModule->getCurrent()); | |||
} | |||
else { | |||
@@ -163,23 +166,33 @@ class SubscriptionForm extends Model | |||
$subscription->save(); | |||
// produits | |||
if ($this->id) { | |||
$productsSubscriptionsArray = ProductSubscription::findAll(['id_subscription' => $this->id]) ; | |||
ProductSubscription::deleteAll(['id_subscription' => $this->id]); | |||
ProductSubscription::deleteAll(['id_subscription' => $this->id]); | |||
} | |||
foreach ($this->products as $nameInput => $quantity) { | |||
if ($quantity) { | |||
$idProduct = (int) str_replace('product_', '', $nameInput); | |||
$product = Product::findOne($idProduct) ; | |||
$newProductSubscription = new ProductSubscription; | |||
$newProductSubscription->id_subscription = $subscription->id; | |||
$newProductSubscription->id_product = $idProduct; | |||
$newProductSubscription->quantity = $quantity / $productModule->getSolver()->getUnitCoefficient($product); | |||
$idProduct = (int) str_replace('product_', '', $nameInput); | |||
$product = ProductRepository::getInstance()->findOneProductById($idProduct) ; | |||
if ($quantity && $product) { | |||
ProductSubscriptionManager::getInstance()->createProductSubscription( | |||
$subscription, | |||
$product, | |||
null, | |||
$quantity / $productModule->getSolver()->getUnitCoefficient($product) | |||
); | |||
} | |||
} | |||
$newProductSubscription->save(); | |||
foreach ($this->rotatings as $nameInput => $quantity) { | |||
$idRotating = (int) str_replace('rotating_', '', $nameInput); | |||
$rotating = RotatingRepository::getInstance()->findOneRotatingById($idRotating) ; | |||
if ($quantity && $rotating) { | |||
ProductSubscriptionManager::getInstance()->createProductSubscription( | |||
$subscription, | |||
null, | |||
$rotating, | |||
$quantity | |||
); | |||
} | |||
} | |||
@@ -0,0 +1,26 @@ | |||
<?php | |||
use yii\db\Migration; | |||
use yii\db\Schema; | |||
/** | |||
* Class m240709_093857_alter_column_product_subscription_id_product | |||
*/ | |||
class m240709_093857_alter_column_product_subscription_id_product extends Migration | |||
{ | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function safeUp() | |||
{ | |||
$this->alterColumn('product_subscription', 'id_product', Schema::TYPE_INTEGER); | |||
} | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function safeDown() | |||
{ | |||
$this->alterColumn('product_subscription', 'id_product', Schema::TYPE_INTEGER.' NOT NULL'); | |||
} | |||
} |
@@ -34,6 +34,7 @@ use domain\Producer\Producer\ProducerSolver; | |||
use domain\Product\Product\Product; | |||
use domain\Product\Product\ProductRepository; | |||
use domain\Product\Product\ProductSolver; | |||
use domain\Product\Rotating\RotatingResolver; | |||
use domain\Subscription\Subscription\Subscription; | |||
use domain\Subscription\Subscription\SubscriptionBuilder; | |||
use domain\Subscription\Subscription\SubscriptionRepository; | |||
@@ -189,14 +190,24 @@ class OrderBuilder extends AbstractBuilder | |||
$user = $subscription->user ?? null; | |||
foreach ($subscription->productSubscription as $productSubscription) { | |||
if($productSubscription->getProduct()) { | |||
$product = $productSubscription->getProduct(); | |||
} | |||
elseif($productSubscription->getRotating()) { | |||
$product = RotatingResolver::getInstance()->getProductOfRotatingInDistribution( | |||
$productSubscription->getRotating(), | |||
$order->distribution | |||
); | |||
} | |||
$this->productOrderBuilder->createProductOrder( | |||
$order, | |||
$productSubscription->product, | |||
$productSubscription->quantity, | |||
(float) $this->productSolver->getPrice($productSubscription->product, [ | |||
$product, | |||
$productSubscription->getQuantity(), | |||
(float) $this->productSolver->getPrice($product, [ | |||
'user' => $user, | |||
'point_sale' => $subscription->pointSale, | |||
'quantity' => $productSubscription->quantity | |||
'quantity' => $productSubscription->getQuantity() | |||
]) | |||
); | |||
$productsAdd = true; |
@@ -22,7 +22,7 @@ class Rotating extends ActiveRecordCommon | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_producer', 'name', 'day', 'selected_products_ids'], 'required'], | |||
[['id_producer', 'name', 'day'], 'required'], | |||
[['name'], 'string', 'max' => 255], | |||
[['id_producer', 'day', 'status'], 'integer'], | |||
[['selected_products_ids'], 'verifySelectedProductsIds'], |
@@ -109,19 +109,21 @@ class RotatingManager extends AbstractManager | |||
*/ | |||
public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution) | |||
{ | |||
$rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution); | |||
if($rotating->getDay() == $distribution->getDate()->format('N')) { | |||
$rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution); | |||
$this->distributionRotatingManager->createDistributionRotatingIfNotExist( | |||
$distribution, | |||
$rotatingProductOfDistribution | |||
); | |||
$this->distributionRotatingManager->createDistributionRotatingIfNotExist( | |||
$distribution, | |||
$rotatingProductOfDistribution | |||
); | |||
foreach($rotating->getRotatingProducts() as $rotatingProduct) { | |||
if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) { | |||
$this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution); | |||
} | |||
else { | |||
$this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution); | |||
foreach($rotating->getRotatingProducts() as $rotatingProduct) { | |||
if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) { | |||
$this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution); | |||
} | |||
else { | |||
$this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution); | |||
} | |||
} | |||
} | |||
} |
@@ -57,6 +57,12 @@ class RotatingResolver extends AbstractResolver | |||
return $this->getFirstRotatingProduct($rotating); | |||
} | |||
public function getProductOfRotatingInDistribution(Rotating $rotating, Distribution $distribution): Product | |||
{ | |||
$rotatingProduct = $this->distributionRotatingRepository->findOneDistributionRotating($distribution, $rotating); | |||
return $rotatingProduct->getRotatingProduct()->getProduct(); | |||
} | |||
/** | |||
* @throws ErrorException | |||
*/ |
@@ -41,24 +41,16 @@ namespace domain\Subscription\ProductSubscription; | |||
use common\components\ActiveRecordCommon; | |||
use domain\Product\Product\Product; | |||
use domain\Product\Rotating\Rotating; | |||
use domain\Subscription\Subscription\Subscription; | |||
use yii\db\ActiveQuery; | |||
/** | |||
* This is the model class for table "commande_auto_produit". | |||
*/ | |||
class ProductSubscription extends ActiveRecordCommon | |||
{ | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public static function tableName() | |||
{ | |||
return 'product_subscription'; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
{ | |||
return [ | |||
@@ -68,9 +60,6 @@ class ProductSubscription extends ActiveRecordCommon | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
@@ -82,6 +71,10 @@ class ProductSubscription extends ActiveRecordCommon | |||
]; | |||
} | |||
/* Méthodes */ | |||
/* Getters / setters */ | |||
public function getRotating(): ?Rotating | |||
@@ -95,20 +88,48 @@ class ProductSubscription extends ActiveRecordCommon | |||
return $this; | |||
} | |||
/* Relations */ | |||
public function getSubscription(): Subscription | |||
{ | |||
return $this->subscriptionRelation; | |||
} | |||
public function getRotatingRelation(): ActiveQuery | |||
public function setSubscription(Subscription $subscription): self | |||
{ | |||
return $this->hasOne(Rotating::class, ['id' => 'id_rotating']); | |||
$this->populateFieldObject('id_subscription', 'subscriptionRelation', $subscription); | |||
return $this; | |||
} | |||
public function getProduct() | |||
public function getProduct(): ?Product | |||
{ | |||
return $this->hasOne(Product::class, ['id' => 'id_product']); | |||
return $this->productRelation; | |||
} | |||
public function populateProduct(Product $product): void | |||
public function setProduct(Product $product): self | |||
{ | |||
$this->populateFieldObject('id_product', 'product', $product); | |||
return $this; | |||
} | |||
public function getQuantity(): float | |||
{ | |||
return $this->quantity; | |||
} | |||
public function setQuantity(float $quantity): self | |||
{ | |||
$this->quantity = $quantity; | |||
return $this; | |||
} | |||
/* Relations */ | |||
public function getRotatingRelation(): ActiveQuery | |||
{ | |||
return $this->hasOne(Rotating::class, ['id' => 'id_rotating']); | |||
} | |||
public function getProductRelation(): ActiveQuery | |||
{ | |||
return $this->hasOne(Product::class, ['id' => 'id_product']); | |||
} | |||
} |
@@ -3,20 +3,14 @@ | |||
namespace domain\Subscription\ProductSubscription; | |||
use domain\_\AbstractBuilder; | |||
use domain\Subscription\Subscription\Subscription; | |||
class ProductSubscriptionBuilder extends AbstractBuilder | |||
{ | |||
public function instanciateProductSubscription(): ProductSubscription | |||
public function instanciateProductSubscription(Subscription $subscription): ProductSubscription | |||
{ | |||
$productSubscription = new ProductSubscription(); | |||
return $productSubscription; | |||
} | |||
public function createProductSubscription(): ProductSubscription | |||
{ | |||
$productSubscription = $this->instanciateProductSubscription(); | |||
$this->saveCreate($productSubscription); | |||
$productSubscription->setSubscription($subscription); | |||
return $productSubscription; | |||
} |
@@ -0,0 +1,39 @@ | |||
<?php | |||
namespace domain\Subscription\ProductSubscription; | |||
use domain\_\AbstractManager; | |||
use domain\Product\Product\Product; | |||
use domain\Product\Rotating\Rotating; | |||
use domain\Subscription\Subscription\Subscription; | |||
use yii\base\ErrorException; | |||
class ProductSubscriptionManager extends AbstractManager | |||
{ | |||
protected ProductSubscriptionBuilder $productSubscriptionBuilder; | |||
public function loadDependencies(): void | |||
{ | |||
$this->productSubscriptionBuilder = $this->loadService(ProductSubscriptionBuilder::class); | |||
} | |||
public function createProductSubscription(Subscription $subscription, Product $product = null, Rotating $rotating = null, float $quantity = null): ProductSubscription | |||
{ | |||
if(!$product && !$rotating) { | |||
throw new ErrorException("Il faut au moins un produit ou un produit tournant pour créer un ProductSubscription"); | |||
} | |||
$productSubscription = $this->productSubscriptionBuilder->instanciateProductSubscription($subscription); | |||
if($product) { | |||
$productSubscription->setProduct($product); | |||
} | |||
if($rotating) { | |||
$productSubscription->setRotating($rotating); | |||
} | |||
$productSubscription->setQuantity($quantity); | |||
$productSubscription->save(); | |||
return $productSubscription; | |||
} | |||
} |
@@ -11,7 +11,8 @@ class ProductSubscriptionModule extends AbstractModule | |||
return [ | |||
ProductSubscriptionDefinition::class, | |||
ProductSubscriptionRepository::class, | |||
ProductSubscriptionBuilder::class | |||
ProductSubscriptionBuilder::class, | |||
ProductSubscriptionManager::class, | |||
]; | |||
} | |||
@@ -25,8 +26,13 @@ class ProductSubscriptionModule extends AbstractModule | |||
return ProductSubscriptionRepository::getInstance(); | |||
} | |||
public function getFactory(): ProductSubscriptionBuilder | |||
public function getBuilder(): ProductSubscriptionBuilder | |||
{ | |||
return ProductSubscriptionBuilder::getInstance(); | |||
} | |||
public function getManager(): ProductSubscriptionManager | |||
{ | |||
return ProductSubscriptionManager::getInstance(); | |||
} | |||
} |
@@ -2,6 +2,7 @@ | |||
namespace domain\Subscription\ProductSubscription; | |||
use domain\Product\Rotating\Rotating; | |||
use domain\Subscription\Subscription\Subscription; | |||
use domain\_\AbstractRepository; | |||
@@ -20,7 +21,7 @@ class ProductSubscriptionRepository extends AbstractRepository | |||
public function getDefaultOptionsSearch(): array | |||
{ | |||
return [ | |||
self::WITH => ['product'], | |||
self::WITH => ['productRelation'], | |||
self::JOIN_WITH => [], | |||
self::ORDER_BY => '', | |||
self::ATTRIBUTE_ID_PRODUCER => '' | |||
@@ -33,4 +34,12 @@ class ProductSubscriptionRepository extends AbstractRepository | |||
->filterBySubscription($subscription) | |||
->find(); | |||
} | |||
public function findOneProductSubscriptionBySubscriptionAndRotating(Subscription $subscription, Rotating $rotating): ?ProductSubscription | |||
{ | |||
return $this->createDefaultQuery() | |||
->filterBySubscription($subscription) | |||
->filterByRotating($rotating) | |||
->findOne(); | |||
} | |||
} |
@@ -2,6 +2,7 @@ | |||
namespace domain\Subscription\ProductSubscription; | |||
use domain\Product\Rotating\Rotating; | |||
use domain\Subscription\Subscription\Subscription; | |||
use domain\_\AbstractRepositoryQuery; | |||
@@ -19,4 +20,10 @@ class ProductSubscriptionRepositoryQuery extends AbstractRepositoryQuery | |||
$this->andWhere(['id_subscription' => $subscription->id]); | |||
return $this; | |||
} | |||
public function filterByRotating(Rotating $rotating): self | |||
{ | |||
$this->andWhere(['id_rotating' => $rotating->getId()]); | |||
return $this; | |||
} | |||
} |
@@ -81,7 +81,7 @@ class Subscription extends ActiveRecordCommon | |||
return [ | |||
'id' => 'ID', | |||
'id_user' => 'Utilisateur', | |||
'id_producer' => 'Etablissement', | |||
'id_producer' => 'Producteur', | |||
'id_point_sale' => 'Point de vente', | |||
'date_begin' => 'Date de début', | |||
'date_end' => 'Date de fin', | |||
@@ -139,7 +139,7 @@ class Subscription extends ActiveRecordCommon | |||
public function getProductSubscription() | |||
{ | |||
return $this->hasMany(ProductSubscription::class, ['id_subscription' => 'id']) | |||
->with('product'); | |||
->with('productRelation'); | |||
} | |||
public function getCreatedBy() |
@@ -21,7 +21,7 @@ class SubscriptionRepository extends AbstractRepository | |||
{ | |||
return [ | |||
self::WITH => ['producer'], | |||
self::JOIN_WITH => ['user', 'productSubscription', 'productSubscription.product', 'pointSale'], | |||
self::JOIN_WITH => ['user', 'productSubscription', 'productSubscription.productRelation', 'pointSale'], | |||
self::ORDER_BY => 'user.name ASC', | |||
self::ATTRIBUTE_ID_PRODUCER => 'subscription.id_producer' | |||
]; |
@@ -140,8 +140,7 @@ class SubscriptionSearch extends Subscription | |||
]); | |||
$query->andFilterWhere(['like', 'product.name', $this->product_name]) ; | |||
return $dataProvider; | |||
} | |||
@@ -79,8 +79,8 @@ class SubscriptionSolver extends AbstractService implements SolverInterface | |||
$html = '' ; | |||
foreach($subscription->productSubscription as $productSubscription) | |||
{ | |||
if(isset($productSubscription->product) && $productSubscription->product) { | |||
$html .= '<strong>'.($productSubscription->quantity * Product::$unitsArray[$productSubscription->product->unit]['coefficient']) . ' '. $this->productSolver->strUnit($productSubscription->product, 'wording_short') . '</strong> ' . Html::encode($productSubscription->product->name). '<br />' ; | |||
if($productSubscription->getProduct()) { | |||
$html .= '<strong>'.($productSubscription->getQuantity() * Product::$unitsArray[$productSubscription->getProduct()->unit]['coefficient']) . ' '. $this->productSolver->strUnit($productSubscription->getProduct(), 'wording_short') . '</strong> ' . Html::encode($productSubscription->getProduct()->name). '<br />' ; | |||
} | |||
else { | |||
$html .= 'Produit non défini<br />' ; |
@@ -187,7 +187,7 @@ class SubscriptionController extends ProducerBaseController | |||
]); | |||
foreach ($arrayProductsSubscription as $productSubscription) { | |||
$model->products['product_' . $productSubscription->id_product] = $productSubscription->quantity; | |||
$model->products['product_' . $productSubscription->id_product] = $productSubscription->getQuantity(); | |||
} | |||
} else { | |||
throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); | |||
@@ -284,8 +284,8 @@ class SubscriptionController extends ProducerBaseController | |||
if (isset($arrayProductsSubscription) && count($arrayProductsSubscription)) { | |||
foreach ($arrayProductsSubscription as $productSubscription) { | |||
if ($product->id == $productSubscription->id_product) { | |||
$coefficientUnit = $productModule->getSolver()->getUnitCoefficient($productSubscription->product); | |||
$quantity = $productSubscription->quantity * $coefficientUnit; | |||
$coefficientUnit = $productModule->getSolver()->getUnitCoefficient($productSubscription->getProduct()); | |||
$quantity = $productSubscription->getQuantity() * $coefficientUnit; | |||
} | |||
} | |||
} |