Browse Source

Refactoring repositories #1084

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
5314dc10e8
34 changed files with 205 additions and 144 deletions
  1. +6
    -5
      backend/controllers/DistributionController.php
  2. +1
    -1
      backend/controllers/DocumentController.php
  3. +1
    -4
      backend/controllers/OrderController.php
  4. +4
    -3
      backend/controllers/UserController.php
  5. +1
    -1
      backend/views/user/_form.php
  6. +1
    -1
      backend/views/user/credit.php
  7. +1
    -1
      common/components/BusinessLogic.php
  8. +3
    -1
      common/logic/AbstractRepository.php
  9. +2
    -1
      common/logic/AbstractRepositoryQuery.php
  10. +1
    -1
      common/logic/Distribution/Distribution/Service/DistributionBuilder.php
  11. +34
    -7
      common/logic/Order/Order/Repository/OrderRepository.php
  12. +1
    -1
      common/logic/Order/Order/Service/OrderBuilder.php
  13. +3
    -1
      common/logic/PointSale/UserPointSale/Repository/UserPointSaleRepository.php
  14. +7
    -3
      common/logic/PointSale/UserPointSale/Repository/UserPointSaleRepositoryQuery.php
  15. +5
    -6
      common/logic/Producer/Producer/Repository/ProducerRepository.php
  16. +1
    -1
      common/logic/Producer/Producer/Repository/ProducerRepositoryQuery.php
  17. +2
    -3
      common/logic/Producer/ProducerPriceRange/Repository/ProducerPriceRangeRepository.php
  18. +2
    -1
      common/logic/Producer/ProducerPriceRange/Service/ProducerPriceRangeDefinition.php
  19. +1
    -1
      common/logic/Product/Product/Repository/ProductRepository.php
  20. +3
    -1
      common/logic/Product/Product/Repository/ProductRepositoryQuery.php
  21. +21
    -64
      common/logic/User/User/Repository/UserRepository.php
  22. +37
    -0
      common/logic/User/User/Repository/UserRepositoryQuery.php
  23. +8
    -4
      common/logic/User/UserGroup/Repository/UserGroupRepository.php
  24. +6
    -0
      common/logic/User/UserGroup/Repository/UserGroupRepositoryQuery.php
  25. +10
    -14
      common/logic/User/UserProducer/Repository/UserProducerRepository.php
  26. +19
    -0
      common/logic/User/UserProducer/Repository/UserProducerRepositoryQuery.php
  27. +2
    -2
      common/logic/User/UserProducer/Service/UserProducerBuilder.php
  28. +4
    -4
      common/logic/User/UserUserGroup/Repository/UserUserGroupRepository.php
  29. +7
    -0
      common/logic/User/UserUserGroup/Repository/UserUserGroupRepositoryQuery.php
  30. +2
    -2
      frontend/controllers/SiteController.php
  31. +1
    -0
      frontend/forms/SignupForm.php
  32. +2
    -2
      producer/controllers/CreditController.php
  33. +5
    -4
      producer/controllers/OrderController.php
  34. +1
    -4
      producer/controllers/SubscriptionController.php

+ 6
- 5
backend/controllers/DistributionController.php View File

{ {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$orderManager = $this->getOrderManager();


$user = $userManager->findOneUserById($idUser); $user = $userManager->findOneUserById($idUser);
$idFavoritePointSale = 0; $idFavoritePointSale = 0;


if($user) { if($user) {
$favoritePointSale = $userManager->getUserFavoritePointSale($user);
$favoritePointSale = $orderManager->getUserFavoritePointSale($user);
if ($favoritePointSale) { if ($favoritePointSale) {
$idFavoritePointSale = $favoritePointSale->id; $idFavoritePointSale = $favoritePointSale->id;
} }
if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) { if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {
$userProducer = null; $userProducer = null;
if ($order->id_user) { if ($order->id_user) {
$userProducer = $userProducerManager->findOneUserProducer($order->user, $this->getProducerCurrent());
$userProducer = $userProducerManager->findOneUserProducer($order->user);
} }
foreach ($order->productOrder as $productOrder) { foreach ($order->productOrder as $productOrder) {
$invoicePrice = $productManager->getPrice($productOrder->product, [ $invoicePrice = $productManager->getPrice($productOrder->product, [


// init invoice prices // init invoice prices
$user = $userManager->findOneUserById($deliveryNote->id_user); $user = $userManager->findOneUserById($deliveryNote->id_user);
$userProducer = $userProducerManager->findOneUserProducer($user, $producerCurrent);
$userProducer = $userProducerManager->findOneUserProducer($user);
$orderManager->updateOrderInvoicePrices($order, [ $orderManager->updateOrderInvoicePrices($order, [
'user' => $user, 'user' => $user,
'user_producer' => $userProducer, 'user_producer' => $userProducer,
if ($firstOrder->pointSale->id_user) { if ($firstOrder->pointSale->id_user) {
$deliveryNote->id_user = $firstOrder->pointSale->id_user; $deliveryNote->id_user = $firstOrder->pointSale->id_user;
$user = $userManager->findOneUserById($deliveryNote->id_user); $user = $userManager->findOneUserById($deliveryNote->id_user);
$userProducer = $userProducerManager->findOneUserProducer($user, $producerCurrent);
$userProducer = $userProducerManager->findOneUserProducer($user);
} else { } else {
$user = new User(); $user = new User();
$user->type = User::TYPE_LEGAL_PERSON; $user->type = User::TYPE_LEGAL_PERSON;


if (!isset($user) || !$user) { if (!isset($user) || !$user) {
$user = $userManager->findOneUserById($deliveryNote->id_user); $user = $userManager->findOneUserById($deliveryNote->id_user);
$userProducer = $userProducerManager->findOneUserProducer($user, $producerCurrent);
$userProducer = $userProducerManager->findOneUserProducer($user);
} }


// affectation du BL aux commandes // affectation du BL aux commandes

+ 1
- 1
backend/controllers/DocumentController.php View File

); );
} }


$userProducer = $userProducerManager->findOneUserProducer($document->user, $this->getProducerCurrent());
$userProducer = $userProducerManager->findOneUserProducer($document->user);
$pointSale = $pointSaleManager->findOnePointSaleByIdUser($document->user->id); $pointSale = $pointSaleManager->findOnePointSaleByIdUser($document->user->id);


$productsArray = yii\helpers\ArrayHelper::map( $productsArray = yii\helpers\ArrayHelper::map(

+ 1
- 4
backend/controllers/OrderController.php View File

if (isset($order->user) && $order->user) { if (isset($order->user) && $order->user) {
$user = $order->user; $user = $order->user;


$userProducer = $userProducerManager->findOneUserProducer(
$user,
$producerManager->findOneProducerById($order->distribution->id_producer)
);
$userProducer = $userProducerManager->findOneUserProducer($user);
} }


foreach ($products as $key => $dataProductOrder) { foreach ($products as $key => $dataProductOrder) {

+ 4
- 3
backend/controllers/UserController.php View File

$model->user_groups[] = $userUserGroup->id_user_group; $model->user_groups[] = $userUserGroup->id_user_group;
} }
} }
$userProducer = $userProducerManager->findOneUserProducer($model, $producerCurrent);
$userProducer = $userProducerManager->findOneUserProducer($model);
$model->product_price_percent = $userProducer->product_price_percent; $model->product_price_percent = $userProducer->product_price_percent;
} }


{ {
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$producerManager = $this->getProducerManager(); $producerManager = $this->getProducerManager();
$pointSaleManager = $this->getPointSaleManager();


$producerCurrent = $this->getProducerCurrent(); $producerCurrent = $this->getProducerCurrent();
$model = $userManager->instanciateUser(); $model = $userManager->instanciateUser();


$user = $userManager->findOneUserById($id); $user = $userManager->findOneUserById($id);
$producer = $this->getProducerCurrent(); $producer = $this->getProducerCurrent();
$userProducer = $userProducerManager->findOneUserProducer($user, $producer);
$userProducer = $userProducerManager->findOneUserProducer($user);


if ($userProducer) { if ($userProducer) {
$userProducer->active = 0; $userProducer->active = 0;


$user = $userManager->findOneUserById($idUser); $user = $userManager->findOneUserById($idUser);
$producerCurrent = $this->getproducerCurrent(); $producerCurrent = $this->getproducerCurrent();
$userProducer = $userProducerManager->findOneUserProducer($user,$producerCurrent);
$userProducer = $userProducerManager->findOneUserProducer($user);


if ($userProducer) { if ($userProducer) {
$userProducer->credit_active = $state; $userProducer->credit_active = $state;

+ 1
- 1
backend/views/user/_form.php View File



<div class="col-md-4"> <div class="col-md-4">


<?php if($pointSaleBillingArray && count($pointSaleBillingArray) > 0): ?>
<?php if(isset($pointSaleBillingArray) && $pointSaleBillingArray && count($pointSaleBillingArray) > 0): ?>
<h3>Facturation</h3> <h3>Facturation</h3>
<?php foreach($pointSaleBillingArray as $pointSale): ?> <?php foreach($pointSaleBillingArray as $pointSale): ?>
<a href="<?= Yii::$app->urlManager->createUrl(['point-sale/update', 'id' => $pointSale->id]) ?>"><?= $pointSale->name ?></a><br /> <a href="<?= Yii::$app->urlManager->createUrl(['point-sale/update', 'id' => $pointSale->id]) ?>"><?= $pointSale->name ?></a><br />

+ 1
- 1
backend/views/user/credit.php View File

</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<h2>Historique <span class="the-credit"><?= number_format($userManager->getCredit($user, $producer), 2); ?> €</span></h2>
<h2>Historique <span class="the-credit"><?= number_format($userManager->getCredit($user), 2); ?> €</span></h2>
<table class="table table-bordered"> <table class="table table-bordered">
<thead> <thead>
<tr> <tr>

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

$this->getCreditHistoryContainer(), $this->getCreditHistoryContainer(),
$this->getProducerPriceRangeContainer(), $this->getProducerPriceRangeContainer(),
$this->getUserProducerContainer(), $this->getUserProducerContainer(),
$this->getUserPointSaleContainer(),
$this->getUserContainer(), $this->getUserContainer(),
$this->getOrderSatusHistoryContainer(), $this->getOrderSatusHistoryContainer(),
$this->getPointSaleDistributionContainer(), $this->getPointSaleDistributionContainer(),
$this->getProductOrderContainer(), $this->getProductOrderContainer(),
$this->getProductPriceContainer(), $this->getProductPriceContainer(),
$this->getProductSubscriptionContainer(), $this->getProductSubscriptionContainer(),
$this->getUserPointSaleContainer(),
$this->getQuotationContainer(), $this->getQuotationContainer(),
$this->getInvoiceContainer(), $this->getInvoiceContainer(),
$this->getDeliveryNoteContainer(), $this->getDeliveryNoteContainer(),

+ 3
- 1
common/logic/AbstractRepository.php View File



public function createQuery() public function createQuery()
{ {
return $this->query->createQuery();
$this->query->createQuery();

return $this->query;
} }


public function createDefaultQuery(): RepositoryQueryInterface public function createDefaultQuery(): RepositoryQueryInterface

+ 2
- 1
common/logic/AbstractRepositoryQuery.php View File



public function filterById(int $id): self public function filterById(int $id): self
{ {
$this->query->andWhere(['id' => $id]);
$class = $this->definition->getEntityFqcn();
$this->query->andWhere([$class::tableName().'.id' => $id]);


return $this; return $this;
} }

+ 1
- 1
common/logic/Distribution/Distribution/Service/DistributionBuilder.php View File

$userProducer = null; $userProducer = null;
if (isset($order->user) && $order->user) { if (isset($order->user) && $order->user) {
$user = $order->user; $user = $order->user;
$userProducer = $this->userProducerRepository->findOneUserProducer($user, $distribution->producer);
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
} }


$this->productOrderBuilder->updateProductOrderPrice( $this->productOrderBuilder->updateProductOrderPrice(

+ 34
- 7
common/logic/Order/Order/Repository/OrderRepository.php View File



namespace common\logic\Order\Order\Repository; namespace common\logic\Order\Order\Repository;


use common\helpers\GlobalParam;
use common\helpers\MeanPayment; use common\helpers\MeanPayment;
use common\logic\AbstractRepository; use common\logic\AbstractRepository;
use common\logic\Distribution\Distribution\Model\Distribution; use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Repository\DistributionRepository; use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository; use common\logic\Distribution\ProductDistribution\Repository\ProductDistributionRepository;
use common\logic\Order\Order\Model\Order; use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Service\OrderBuilder;
use common\logic\Order\Order\Service\OrderSolver; use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Order\ProductOrder\Repository\ProductOrderRepository; use common\logic\Order\ProductOrder\Repository\ProductOrderRepository;
use common\logic\PointSale\PointSale\Model\PointSale; use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\PointSale\PointSale\Repository\PointSaleRepository; use common\logic\PointSale\PointSale\Repository\PointSaleRepository;
use common\logic\PointSale\UserPointSale\Repository\UserPointSaleRepository;
use common\logic\Producer\Producer\Model\Producer; use common\logic\Producer\Producer\Model\Producer;
use common\logic\Producer\Producer\Repository\ProducerRepository; use common\logic\Producer\Producer\Repository\ProducerRepository;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\Product\Service\ProductSolver; use common\logic\Product\Product\Service\ProductSolver;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer;
use common\logic\User\UserProducer\Repository\UserProducerRepository; use common\logic\User\UserProducer\Repository\UserProducerRepository;
use yii\helpers\Html; use yii\helpers\Html;


protected UserProducerRepository $userProducerRepository; protected UserProducerRepository $userProducerRepository;
protected DistributionRepository $distributionRepository; protected DistributionRepository $distributionRepository;
protected PointSaleRepository $pointSaleRepository; protected PointSaleRepository $pointSaleRepository;
protected UserPointSaleRepository $userPointSaleRepository;


public function loadDependencies(): void public function loadDependencies(): void
{ {
$this->userProducerRepository = $this->loadService(UserProducerRepository::class); $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
$this->distributionRepository = $this->loadService(DistributionRepository::class); $this->distributionRepository = $this->loadService(DistributionRepository::class);
$this->pointSaleRepository = $this->loadService(PointSaleRepository::class); $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
$this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class);
} }


public function getDefaultOptionsSearch(): array public function getDefaultOptionsSearch(): array
return 0; return 0;
} }
elseif ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER) { elseif ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER) {
$userProducer = $this->userProducerRepository->findOneUserProducer(
$order->user,
$distribution->producer
);
$userProducer = $this->userProducerRepository->findOneUserProducer($order->user);
if ($userProducer) { if ($userProducer) {
return $userProducer->credit_active; return $userProducer->credit_active;
} }


return 0; return 0;
} }

public function findOneOrderLastByUser(User $user)
{
return $this->createDefaultQuery()
->filterByUser($user)
->orderBy('order.id DESC')
->findOne();
}

/**
* Retourne le point de vente favoris d'un utilisateur : le point de vente auquel le client est lié,
* le point de vente de la dernière commande sinon.
*/
public function getUserFavoritePointSale(User $user): ?PointSale
{
$pointSale = null;
$arrayUserPointSale = $this->userPointSaleRepository->findUserPointSalesByUser($user);

if (count($arrayUserPointSale) == 1) {
$pointSale = $this->pointSaleRepository->findOnePointSaleById($arrayUserPointSale[0]->id_point_sale);
} else {
$lastOrder = $this->findOneOrderLastByUser($user);
if ($lastOrder) {
$pointSale = $this->pointSaleRepository->findOnePointSaleById($lastOrder->id_point_sale);
}
}

return $pointSale;
}
} }

+ 1
- 1
common/logic/Order/Order/Service/OrderBuilder.php View File

$order->auto_payment = 1; $order->auto_payment = 1;
} elseif ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER) { } elseif ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER) {


$userProducer = $this->userProducerRepository->findOneUserProducer($order->user, $subscription->producer);
$userProducer = $this->userProducerRepository->findOneUserProducer($order->user);


if ($userProducer) { if ($userProducer) {
$order->auto_payment = $userProducer->credit_active; $order->auto_payment = $userProducer->credit_active;

+ 3
- 1
common/logic/PointSale/UserPointSale/Repository/UserPointSaleRepository.php View File

self::WITH => [], self::WITH => [],
self::JOIN_WITH => [], self::JOIN_WITH => [],
self::ORDER_BY => '', self::ORDER_BY => '',
self::ATTRIBUTE_ID_PRODUCER => ''
self::ATTRIBUTE_ID_PRODUCER => 'point_sale.id_producer'
] ; ] ;
} }


public function findUserPointSalesByUser(User $user): array public function findUserPointSalesByUser(User $user): array
{ {
return $this->createDefaultQuery() return $this->createDefaultQuery()
->joinPointSale()
->filterByUser($user) ->filterByUser($user)
->find(); ->find();
} }
} }


return $this->createDefaultQuery() return $this->createDefaultQuery()
->joinPointSale()
->filterByUser($user) ->filterByUser($user)
->filterByPointSale($pointSale) ->filterByPointSale($pointSale)
->findOne(); ->findOne();

+ 7
- 3
common/logic/PointSale/UserPointSale/Repository/UserPointSaleRepositoryQuery.php View File

$this->loadDefinition(UserPointSaleDefinition::class); $this->loadDefinition(UserPointSaleDefinition::class);
} }


public function filterByUser(User $user): self
public function joinPointSale(): self
{ {
$this->andWhere(['id_user' => $user->id]);
$this->innerJoinWith('pointSale', true);
return $this;
}


public function filterByUser(User $user): self
{
$this->andWhere(['user_point_sale.id_user' => $user->id]);
return $this; return $this;
} }


public function filterByPointSale(PointSale $pointSale): self public function filterByPointSale(PointSale $pointSale): self
{ {
$this->andWhere(['id_point_sale' => $pointSale->id]); $this->andWhere(['id_point_sale' => $pointSale->id]);

return $this; return $this;
} }
} }

+ 5
- 6
common/logic/Producer/Producer/Repository/ProducerRepository.php View File



public function findOneProducerById(int $id) public function findOneProducerById(int $id)
{ {
return $this->createDefaultQuery()
return $this->createQuery()
->filterById($id) ->filterById($id)
->findOne(); ->findOne();
} }


public function findOneProducerBySlug(string $slug) public function findOneProducerBySlug(string $slug)
{ {
return $this->createDefaultQuery()
return $this->createQuery()
->filterBySlug($slug) ->filterBySlug($slug)
->findOne(); ->findOne();
} }


public function queryProducersActive() public function queryProducersActive()
{ {
return $this->createDefaultQuery()
->filterIsActive();
return $this->createQuery()->filterIsActive();
} }


/** /**
*/ */
public function findOneProducerDemoAccount() public function findOneProducerDemoAccount()
{ {
return $this->createDefaultQuery()
return $this->createQuery()
->filterIsDemoAccount() ->filterIsDemoAccount()
->findOne(); ->findOne();
} }
*/ */
public function populateProducerDropdown(): array public function populateProducerDropdown(): array
{ {
return $this->createDefaultQuery()
$producers = $this->createQuery()
->filterIsActive() ->filterIsActive()
->orderBy('postcode, city ASC') ->orderBy('postcode, city ASC')
->find(); ->find();

+ 1
- 1
common/logic/Producer/Producer/Repository/ProducerRepositoryQuery.php View File

$this->loadDefinition(ProducerDefinition::class); $this->loadDefinition(ProducerDefinition::class);
} }


public function filterBySlug(int $slug): self
public function filterBySlug(string $slug): self
{ {
$this->andWhere(['slug' => $slug]); $this->andWhere(['slug' => $slug]);
return $this; return $this;

+ 2
- 3
common/logic/Producer/ProducerPriceRange/Repository/ProducerPriceRangeRepository.php View File



public function queryProducerPriceRanges() public function queryProducerPriceRanges()
{ {
return $this->createDefaultQuery()
->find();
return $this->createDefaultQuery();
} }


public function findProducerPriceRanges() public function findProducerPriceRanges()
{ {
return $this->queryProducerPriceRanges()->all();
return $this->queryProducerPriceRanges()->find();
} }


public function getAmountToBeBilledByTurnover(float $turnover = null, $format = false) public function getAmountToBeBilledByTurnover(float $turnover = null, $format = false)

+ 2
- 1
common/logic/Producer/ProducerPriceRange/Service/ProducerPriceRangeDefinition.php View File

namespace common\logic\Producer\ProducerPriceRange\Service; namespace common\logic\Producer\ProducerPriceRange\Service;


use common\logic\AbstractDefinition; use common\logic\AbstractDefinition;
use common\logic\Producer\ProducerPriceRange\Model\ProducerPriceRange;


class ProducerPriceRangeDefinition extends AbstractDefinition class ProducerPriceRangeDefinition extends AbstractDefinition
{ {
public function getEntityFqcn(): string public function getEntityFqcn(): string
{ {
return ProducerPriceRangeDefinition::class;
return ProducerPriceRange::class;
} }
} }

+ 1
- 1
common/logic/Product/Product/Repository/ProductRepository.php View File

$priceArray = []; $priceArray = [];
$userProducer = null; $userProducer = null;
if ($user) { if ($user) {
$userProducer = $this->userProducerRepository->findOneUserProducer($user, GlobalParam::getCurrentProducer());
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
} }


// specific prices // specific prices

+ 3
- 1
common/logic/Product/Product/Repository/ProductRepositoryQuery.php View File



public function filterByProductCategory(ProductCategory $productCategory = null) : self public function filterByProductCategory(ProductCategory $productCategory = null) : self
{ {
$this->andWhere(['product.id_product_category' => $productCategory->id]);
if($productCategory) {
$this->andWhere(['product.id_product_category' => $productCategory->id]);
}
return $this; return $this;
} }
} }

+ 21
- 64
common/logic/User/User/Repository/UserRepository.php View File



use common\helpers\GlobalParam; use common\helpers\GlobalParam;
use common\logic\AbstractRepository; use common\logic\AbstractRepository;
use common\logic\Order\Order\Model\Order;
use common\logic\PointSale\PointSale\Model\PointSale; use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\PointSale\UserPointSale\Model\UserPointSale;
use common\logic\Producer\Producer\Model\Producer; use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\User\Service\UserSolver; use common\logic\User\User\Service\UserSolver;
use common\logic\User\UserGroup\Model\UserGroup;
use common\logic\User\UserProducer\Repository\UserProducerRepository; use common\logic\User\UserProducer\Repository\UserProducerRepository;
use common\logic\User\UserUserGroup\Model\UserUserGroup;
use yii\db\Query; use yii\db\Query;


class UserRepository extends AbstractRepository class UserRepository extends AbstractRepository


public function findOneUserById($id) public function findOneUserById($id)
{ {
return User::searchOne(['id' => $id]);
return $this->createDefaultQuery()
->filterById($id)
->findOne();
} }


public function findUsers(): array public function findUsers(): array
return $usersArrayDropdown; return $usersArrayDropdown;
} }


/**
* Retourne le point de vente favoris d'un utilisateur : le point de vente auquel le client est lié,
* le point de vente de la dernière commande sinon.
*
*/
public function getUserFavoritePointSale(User $user): ?PointSale
{
$arrayUserPointSale = UserPointSale::find()
->innerJoinWith('pointSale', true)
->where([
'user_point_sale.id_user' => $user->id,
'point_sale.id_producer' => GlobalParam::getCurrentProducerId()
])
->all();

if (count($arrayUserPointSale) == 1) {
$pointSale = PointSale::findOne(['id' => $arrayUserPointSale[0]->id_point_sale]);
} else {
$lastOrder = Order::find()->innerJoinWith('pointSale', true)->where([
'order.id_user' => $user->id,
'point_sale.id_producer' => GlobalParam::getCurrentProducerId()
])
->orderBy('order.id DESC')
->one();

if ($lastOrder) {
$pointSale = PointSale::findOne(['id' => $lastOrder->id_point_sale]);
}
}

if (isset($pointSale)) {
return $pointSale;
}

return null;
}

/** /**
* Retourne le crédit de l'utilisateur pour un producteur donné. * Retourne le crédit de l'utilisateur pour un producteur donné.
* *
*/ */
public function getCredit(User $user, Producer $producer): float
public function getCredit(User $user): float
{ {
$userProducer = $this->userProducerRepository->findOneUserProducer($user, $producer);
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
return $userProducer ? $userProducer->credit : 0; return $userProducer ? $userProducer->credit : 0;
} }


* @param array $params * @param array $params
* @return Query * @return Query
*/ */
// findBy
public function queryUsersBy(array $params = []) public function queryUsersBy(array $params = [])
{ {
if (!isset($params['id_producer'])) { if (!isset($params['id_producer'])) {
/** /**
* Finds user by password reset token * Finds user by password reset token
*/ */
// getByPasswordResetToken
public function findOneUserByPasswordResetToken(string $token) public function findOneUserByPasswordResetToken(string $token)
{ {
if (!$this->userSolver->isPasswordResetTokenValid($token)) { if (!$this->userSolver->isPasswordResetTokenValid($token)) {
return null; return null;
} }


return User::findOne([
'password_reset_token' => $token,
]);
return $this->createDefaultQuery()
->filterByPasswordResetToken($token)
->findOne();
} }


/** /**
* Recherche un utilisateur via son adresse email. * Recherche un utilisateur via son adresse email.
*/ */
// getOneByEmail
public function findOneUserByEmail(string $email): ?User public function findOneUserByEmail(string $email): ?User
{ {
return User::searchOne(['email' => $email]);
return $this->createDefaultQuery()
->filterByEmail($email)
->findOne();
} }


// getOneByUsername
public function findOneUserByUsername(string $username): ?User public function findOneUserByUsername(string $username): ?User
{ {
return User::searchOne(['username' => $username]);
return $this->createDefaultQuery()
->filterByUsername($username)
->findOne();
} }


public function findUsersByProducer(Producer $producer) public function findUsersByProducer(Producer $producer)
{ {
return User::find()
->where([
'id_producer' => $producer->id,
'status' => User::STATUS_PRODUCER
])
->all();
return $this->createDefaultQuery()
->isStatusProducer()
->filterByProducer($producer)
->find();
} }


public function findUsersByStatus(string $status) public function findUsersByStatus(string $status)
{ {
return User::find()
->where([
'user.status' => $status
])
->all();
return $this->createDefaultQuery()
->filterByStatus($status)
->find();
} }
} }

+ 37
- 0
common/logic/User/User/Repository/UserRepositoryQuery.php View File

namespace common\logic\User\User\Repository; namespace common\logic\User\User\Repository;


use common\logic\AbstractRepositoryQuery; use common\logic\AbstractRepositoryQuery;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\User\Model\User;
use common\logic\User\User\Service\UserDefinition; use common\logic\User\User\Service\UserDefinition;


class UserRepositoryQuery extends AbstractRepositoryQuery class UserRepositoryQuery extends AbstractRepositoryQuery
{ {
$this->loadDefinition(UserDefinition::class); $this->loadDefinition(UserDefinition::class);
} }

public function filterByPasswordResetToken(string $token): self
{
$this->andWhere(['password_reset_token' => $token]);
return $this;
}

public function filterByEmail(string $email): self
{
$this->andWhere(['email' => $email]);
return $this;
}

public function filterByUsername(string $username): self
{
$this->andWhere(['username' => $username]);
return $this;
}

public function filterByProducer(Producer $producer): self
{
$this->andWhere(['user.id_producer' => $producer->id]);
return $this;
}

public function filterByStatus(string $status): self
{
$this->andWhere(['user.status' => $status]);
return $this;
}

public function isStatusProducer(): self
{
return $this->filterByStatus(User::STATUS_PRODUCER);
}
} }

+ 8
- 4
common/logic/User/UserGroup/Repository/UserGroupRepository.php View File

self::WITH => [], self::WITH => [],
self::JOIN_WITH => [], self::JOIN_WITH => [],
self::ORDER_BY => '', self::ORDER_BY => '',
self::ATTRIBUTE_ID_PRODUCER => ''
self::ATTRIBUTE_ID_PRODUCER => 'user_group.id_producer'
]; ];
} }


public function findOneUserGroupById(int $id) public function findOneUserGroupById(int $id)
{ {
return UserGroup::searchOne(['id' => $id]);
return $this->createDefaultQuery()
->filterById($id)
->findOne();
} }


public function findOneUserGroupByName(string $name) public function findOneUserGroupByName(string $name)
{ {
return UserGroup::searchOne(['name' => $name]);
return $this->createDefaultQuery()
->filterByName($name)
->findOne();
} }


public function findUserGroups() public function findUserGroups()
{ {
return UserGroup::find()->where('id_producer = ' . GlobalParam::getCurrentProducerId())->all();
return $this->createDefaultQuery()->find();
} }


public function populateUserGroupDropdownList(): array public function populateUserGroupDropdownList(): array

+ 6
- 0
common/logic/User/UserGroup/Repository/UserGroupRepositoryQuery.php View File

{ {
$this->loadDefinition(UserGroupDefinition::class); $this->loadDefinition(UserGroupDefinition::class);
} }

public function filterByName(string $name): self
{
$this->andWhere(['name' => $name]);
return $this;
}
} }

+ 10
- 14
common/logic/User/UserProducer/Repository/UserProducerRepository.php View File

public function getDefaultOptionsSearch(): array public function getDefaultOptionsSearch(): array
{ {
return [ return [
self::WITH => [],
self::WITH => ['producer'],
self::JOIN_WITH => [], self::JOIN_WITH => [],
self::ORDER_BY => '', self::ORDER_BY => '',
self::ATTRIBUTE_ID_PRODUCER => 'user_producer.id_producer' self::ATTRIBUTE_ID_PRODUCER => 'user_producer.id_producer'
]; ];
} }


public function findOneUserProducer(User $user, Producer $producer)
public function findOneUserProducer(User $user)
{ {
return UserProducer::searchOne([
'id_user' => $user->id,
'user_producer.id_producer' => $producer->id
]);
return $this->createDefaultQuery()
->filterByUser($user)
->findOne();
} }


public function findUserProducersByUser(User $user, bool $active = true, bool $bookmark = true) public function findUserProducersByUser(User $user, bool $active = true, bool $bookmark = true)
{ {
return UserProducer::find()
->with(['producer'])
->where([
'id_user' => $user->id,
'active' => $active,
'bookmark' => $bookmark
])
->all();
return $this->createDefaultQuery()
->filterByUser($user)
->filterByActive($active)
->filterByBookmark($bookmark)
->find();
} }
} }

+ 19
- 0
common/logic/User/UserProducer/Repository/UserProducerRepositoryQuery.php View File

namespace common\logic\User\UserProducer\Repository; namespace common\logic\User\UserProducer\Repository;


use common\logic\AbstractRepositoryQuery; use common\logic\AbstractRepositoryQuery;
use common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer; use common\logic\User\UserProducer\Model\UserProducer;
use common\logic\User\UserProducer\Service\UserProducerDefinition; use common\logic\User\UserProducer\Service\UserProducerDefinition;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
{ {
$this->loadDefinition(UserProducerDefinition::class); $this->loadDefinition(UserProducerDefinition::class);
} }

public function filterByUser(User $user): self
{
$this->andWhere(['id_user' => $user->id]);
return $this;
}

public function filterByActive(bool $active): self
{
$this->andWhere(['active' => $active]);
return $this;
}

public function filterByBookmark(bool $bookmark): self
{
$this->andWhere(['bookmark' => $bookmark]);
return $this;
}
} }

+ 2
- 2
common/logic/User/UserProducer/Service/UserProducerBuilder.php View File



public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer public function createUserProducerIfNotExist(User $user, Producer $producer, int $bookmark = 1): UserProducer
{ {
return $this->userProducerRepository->findOneUserProducer($user, $producer)
return $this->userProducerRepository->findOneUserProducer($user)
?? $this->createUserProducer($user, $producer, $bookmark); ?? $this->createUserProducer($user, $producer, $bookmark);
} }


public function updateCredit(CreditHistory $creditHistory): void public function updateCredit(CreditHistory $creditHistory): void
{ {
$userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user, $creditHistory->producer);
$userProducer = $this->userProducerRepository->findOneUserProducer($creditHistory->user);


if ($userProducer) { if ($userProducer) {
$oldCredit = $userProducer->getCredit(); $oldCredit = $userProducer->getCredit();

+ 4
- 4
common/logic/User/UserUserGroup/Repository/UserUserGroupRepository.php View File

self::JOIN_WITH => [], self::JOIN_WITH => [],
self::ORDER_BY => '', self::ORDER_BY => '',
self::ATTRIBUTE_ID_PRODUCER => '' self::ATTRIBUTE_ID_PRODUCER => ''
] ;
];
} }


public function findUserUserGroupsByUser(User $user) public function findUserUserGroupsByUser(User $user)
{ {
return UserUserGroup::searchAll([
'id_user' => $user->id
]);
return $this->createDefaultQuery()
->filterByUser($user)
->find();
} }
} }

+ 7
- 0
common/logic/User/UserUserGroup/Repository/UserUserGroupRepositoryQuery.php View File

namespace common\logic\User\UserUserGroup\Repository; namespace common\logic\User\UserUserGroup\Repository;


use common\logic\AbstractRepositoryQuery; use common\logic\AbstractRepositoryQuery;
use common\logic\User\User\Model\User;
use common\logic\User\UserUserGroup\Model\UserUserGroup; use common\logic\User\UserUserGroup\Model\UserUserGroup;
use common\logic\User\UserUserGroup\Service\UserUserGroupDefinition; use common\logic\User\UserUserGroup\Service\UserUserGroupDefinition;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
{ {
$this->loadDefinition(UserUserGroupDefinition::class); $this->loadDefinition(UserUserGroupDefinition::class);
} }

public function filterByUser(User $user): self
{
$this->andWhere(['id_user' => $user->id]);
return $this;
}
} }

+ 2
- 2
frontend/controllers/SiteController.php View File

public function getDataProviderPrices() public function getDataProviderPrices()
{ {
return new ActiveDataProvider([ return new ActiveDataProvider([
'query' => $this->getProducerPriceRangeManager()->queryProducerPriceRanges(),
'query' => $this->getProducerPriceRangeManager()->queryProducerPriceRanges()->query(),
'pagination' => [ 'pagination' => [
'pageSize' => 100, 'pageSize' => 100,
], ],
public function actionProducers() public function actionProducers()
{ {
$dataProviderProducers = new ActiveDataProvider([ $dataProviderProducers = new ActiveDataProvider([
'query' => $this->getProducerManager()->queryProducersActive(),
'query' => $this->getProducerManager()->queryProducersActive()->query(),
'pagination' => [ 'pagination' => [
'pageSize' => 100, 'pageSize' => 100,
], ],

+ 1
- 0
frontend/forms/SignupForm.php View File



if ($user->save() && $producer) { if ($user->save() && $producer) {
// Liaison User / Producer // Liaison User / Producer
\Yii::$app->logic->setProducerContext($producer);
$producerManager->addUser($user, $producer); $producerManager->addUser($user, $producer);


// Envoi d'un email de bienvenue à l'utilisateur // Envoi d'un email de bienvenue à l'utilisateur

+ 2
- 2
producer/controllers/CreditController.php View File

$searchModel = new CreditHistorySearch(); $searchModel = new CreditHistorySearch();
$searchModel->id_user = GlobalParam::getCurrentUserId(); $searchModel->id_user = GlobalParam::getCurrentUserId();
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams); $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
$userProducer = $this->getUserProducerManager()->findOneUserProducer(GlobalParam::getCurrentUser(), $producer);
$userProducer = $this->getUserProducerManager()->findOneUserProducer($this->getUserCurrent());


if (strlen($returnPayment)) { if (strlen($returnPayment)) {
if ($returnPayment == 'success') { if ($returnPayment == 'success') {
] ]
]); ]);
} else { } else {
$userProducer = $this->getUserProducerManager()->findOneUserProducer($user, $producer);
$userProducer = $this->getUserProducerManager()->findOneUserProducer($user);


Mailjet::sendMail([ Mailjet::sendMail([
'from_email' => $producerManager->getEmailOpendistrib($producer), 'from_email' => $producerManager->getEmailOpendistrib($producer),

+ 5
- 4
producer/controllers/OrderController.php View File

$errors = []; $errors = [];


if ($order->validate() && count($productsArray) && !$errorDate && !$errorPointSale) { if ($order->validate() && count($productsArray) && !$errorDate && !$errorPointSale) {
$userProducer = $userProducerManager->findOneUserProducer($user, $producer);
$userProducer = $userProducerManager->findOneUserProducer($user);
$pointSale = $pointSaleManager->findOnePointSaleById($order->id_point_sale); $pointSale = $pointSaleManager->findOnePointSaleById($order->id_point_sale);


$order->comment_point_sale = ($pointSale && strlen($pointSaleManager->getComment($pointSale))) ? $order->comment_point_sale = ($pointSale && strlen($pointSaleManager->getComment($pointSale))) ?
$credit = $producerManager->getConfig('credit'); $credit = $producerManager->getConfig('credit');
$creditLimit = $producerManager->getConfig('credit_limit'); $creditLimit = $producerManager->getConfig('credit_limit');
$creditFunctioning = $producerManager->getPointSaleCreditFunctioning($pointSale); $creditFunctioning = $producerManager->getPointSaleCreditFunctioning($pointSale);
$creditUser = $userManager->getCredit($user, $producer);
$creditUser = $userManager->getCredit($user);
$order = $orderManager->findOneOrderById($order->id); $order = $orderManager->findOneOrderById($order->id);
$orderManager->initOrder($order); $orderManager->initOrder($order);
$amountRemaining = $orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING); $amountRemaining = $orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING);
$user = GlobalParam::getCurrentUser(); $user = GlobalParam::getCurrentUser();
if($user) { if($user) {
$userProducer = $userProducerManager->findOneUserProducer($user, $producer);
$userProducer = $userProducerManager->findOneUserProducer($user);
if (!$userProducer) { if (!$userProducer) {
$userProducer = $producerManager->addUser($user, $producer); $userProducer = $producerManager->addUser($user, $producer);
} }
$pointSaleManager = $this->getPointSaleManager(); $pointSaleManager = $this->getPointSaleManager();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$producerManager = $this->getProducerManager(); $producerManager = $this->getProducerManager();
$orderManager = $this->getOrderManager();


$user = GlobalParam::getCurrentUser(); $user = GlobalParam::getCurrentUser();
$pointsSaleArray = PointSale::find(); $pointsSaleArray = PointSale::find();


$favoritePointSale = false; $favoritePointSale = false;
if ($user) { if ($user) {
$favoritePointSale = $userManager->getUserFavoritePointSale($user);
$favoritePointSale = $orderManager->getUserFavoritePointSale($user);
} }


if ($favoritePointSale) { if ($favoritePointSale) {

+ 1
- 4
producer/controllers/SubscriptionController.php View File

$productManager = $this->getProductManager(); $productManager = $this->getProductManager();
$subscriptionManager = $this->getSubscriptionManager(); $subscriptionManager = $this->getSubscriptionManager();
$user = GlobalParam::getCurrentUser(); $user = GlobalParam::getCurrentUser();
$userProducer = $this->getUserProducerManager()->findOneUserProducer(
GlobalParam::getCurrentUser(),
GlobalParam::getCurrentProducer()
);
$userProducer = $this->getUserProducerManager()->findOneUserProducer($this->getUserCurrent());
$pointSale = null; $pointSale = null;


if ($idSubscription > 0) { if ($idSubscription > 0) {

Loading…
Cancel
Save