Browse Source

Produits tournants

feature/rotating_product
Guillaume Bourgeois 4 months ago
parent
commit
a4463de564
20 changed files with 222 additions and 79 deletions
  1. +12
    -4
      backend/controllers/SubscriptionController.php
  2. +6
    -4
      backend/views/subscription/index.php
  3. +2
    -2
      common/config/main.php
  4. +26
    -13
      common/forms/SubscriptionForm.php
  5. +26
    -0
      console/migrations/m240709_093857_alter_column_product_subscription_id_product.php
  6. +15
    -4
      domain/Order/Order/OrderBuilder.php
  7. +1
    -1
      domain/Product/Rotating/Rotating.php
  8. +13
    -11
      domain/Product/Rotating/RotatingManager.php
  9. +6
    -0
      domain/Product/Rotating/RotatingResolver.php
  10. +39
    -18
      domain/Subscription/ProductSubscription/ProductSubscription.php
  11. +3
    -9
      domain/Subscription/ProductSubscription/ProductSubscriptionBuilder.php
  12. +39
    -0
      domain/Subscription/ProductSubscription/ProductSubscriptionManager.php
  13. +8
    -2
      domain/Subscription/ProductSubscription/ProductSubscriptionModule.php
  14. +10
    -1
      domain/Subscription/ProductSubscription/ProductSubscriptionRepository.php
  15. +7
    -0
      domain/Subscription/ProductSubscription/ProductSubscriptionRepositoryQuery.php
  16. +2
    -2
      domain/Subscription/Subscription/Subscription.php
  17. +1
    -1
      domain/Subscription/Subscription/SubscriptionRepository.php
  18. +1
    -2
      domain/Subscription/Subscription/SubscriptionSearch.php
  19. +2
    -2
      domain/Subscription/Subscription/SubscriptionSolver.php
  20. +3
    -3
      producer/controllers/SubscriptionController.php

+ 12
- 4
backend/controllers/SubscriptionController.php View File

@@ -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() : ''
]);
}


+ 6
- 4
backend/views/subscription/index.php View File

@@ -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']).'&nbsp;'. $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']).'&nbsp;'. $productModule->getSolver()->strUnit($productSubscription->getProduct(), 'wording_short').')<br />' ;
}
elseif($productSubscription->getRotating()) {
$html .= Html::encode($productSubscription->getRotating()->getName()).' ('.($productSubscription->getQuantity()).'&nbsp;p.)<br />' ;
}
else {
$html .= 'Produit non défini<br />' ;

+ 2
- 2
common/config/main.php View File

@@ -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

+ 26
- 13
common/forms/SubscriptionForm.php View File

@@ -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
);
}
}

+ 26
- 0
console/migrations/m240709_093857_alter_column_product_subscription_id_product.php View File

@@ -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');
}
}

+ 15
- 4
domain/Order/Order/OrderBuilder.php View File

@@ -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;

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

@@ -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'],

+ 13
- 11
domain/Product/Rotating/RotatingManager.php View File

@@ -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);
}
}
}
}

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

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

+ 39
- 18
domain/Subscription/ProductSubscription/ProductSubscription.php View File

@@ -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
- 9
domain/Subscription/ProductSubscription/ProductSubscriptionBuilder.php View File

@@ -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;
}

+ 39
- 0
domain/Subscription/ProductSubscription/ProductSubscriptionManager.php View File

@@ -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;
}
}

+ 8
- 2
domain/Subscription/ProductSubscription/ProductSubscriptionModule.php View File

@@ -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();
}
}

+ 10
- 1
domain/Subscription/ProductSubscription/ProductSubscriptionRepository.php View File

@@ -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();
}
}

+ 7
- 0
domain/Subscription/ProductSubscription/ProductSubscriptionRepositoryQuery.php View File

@@ -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;
}
}

+ 2
- 2
domain/Subscription/Subscription/Subscription.php View File

@@ -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()

+ 1
- 1
domain/Subscription/Subscription/SubscriptionRepository.php View File

@@ -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'
];

+ 1
- 2
domain/Subscription/Subscription/SubscriptionSearch.php View File

@@ -140,8 +140,7 @@ class SubscriptionSearch extends Subscription
]);
$query->andFilterWhere(['like', 'product.name', $this->product_name]) ;

return $dataProvider;
}

+ 2
- 2
domain/Subscription/Subscription/SubscriptionSolver.php View File

@@ -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']) . '&nbsp'. $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']) . '&nbsp'. $this->productSolver->strUnit($productSubscription->getProduct(), 'wording_short') . '</strong> ' . Html::encode($productSubscription->getProduct()->name). '<br />' ;
}
else {
$html .= 'Produit non défini<br />' ;

+ 3
- 3
producer/controllers/SubscriptionController.php View File

@@ -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;
}
}
}

Loading…
Cancel
Save