Parcourir la source

Refactoring services #885

refactoring
Guillaume il y a 1 an
Parent
révision
97f1c09d43
9 fichiers modifiés avec 84 ajouts et 75 suppressions
  1. +7
    -2
      common/logic/Config/TaxRate/TaxRateRepository.php
  2. +23
    -26
      common/logic/Distribution/Distribution/DistributionBuilder.php
  3. +9
    -8
      common/logic/Distribution/Distribution/DistributionRepository.php
  4. +2
    -1
      common/logic/Distribution/Distribution/DistributionSolver.php
  5. +4
    -4
      common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php
  6. +3
    -3
      common/logic/Distribution/PointSaleDistribution/PointSaleDistributionRepository.php
  7. +9
    -29
      common/logic/Distribution/ProductDistribution/ProductDistributionRepository.php
  8. +26
    -1
      common/logic/Order/Order/OrderRepository.php
  9. +1
    -1
      producer/controllers/OrderController.php

+ 7
- 2
common/logic/Config/TaxRate/TaxRateRepository.php Voir le fichier

@@ -17,11 +17,16 @@ class TaxRateRepository extends BaseService implements RepositoryInterface
] ;
}

public function findTaxRates(): array
{
return TaxRate::find()->all();
}

// getTaxRateArray
public function getAsArray(): array
public function findTaxRatesAsArray(): array
{
$taxRateArrayReturn = [];
$taxRateArray = TaxRate::find()->all();
$taxRateArray = $this->findTaxRates();

foreach($taxRateArray as $taxRate) {
$taxRateArrayReturn[$taxRate->id] = $taxRate;

+ 23
- 26
common/logic/Distribution/Distribution/DistributionBuilder.php Voir le fichier

@@ -10,15 +10,12 @@ use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionReposit
use common\logic\Distribution\ProductDistribution\ProductDistribution;
use common\logic\Distribution\ProductDistribution\ProductDistributionBuilder;
use common\logic\Distribution\ProductDistribution\ProductDistributionRepository;
use common\logic\Order\Order\Order;
use common\logic\Order\Order\OrderRepository;
use common\logic\Order\ProductOrder\ProductOrderBuilder;
use common\logic\PointSale\PointSale\PointSale;
use common\logic\Producer\Producer\Producer;
use common\logic\Product\Product\Product;
use common\logic\Product\Product\ProductRepository;
use common\logic\Subscription\Subscription\Subscription;
use common\logic\User\UserProducer\UserProducer;
use common\logic\User\UserProducer\UserProducerRepository;

class DistributionBuilder extends BaseService implements BuilderInterface
@@ -64,8 +61,8 @@ class DistributionBuilder extends BaseService implements BuilderInterface
$distribution = $this->instanciateDistribution($producer, $date, $delivery);
$distribution->save();

$this->initPointSaleDistribution($distribution);
$this->initProductDistribution($distribution);
$this->createPointSaleDistributions($distribution);
$this->createProductDistributions($distribution);

return $distribution;
}
@@ -81,32 +78,32 @@ class DistributionBuilder extends BaseService implements BuilderInterface
return $distribution;
}

public function initPointSaleDistribution(Distribution $distribution): void
public function createPointSaleDistributions(Distribution $distribution): void
{
$countPointSaleDistribution = $this->pointSaleDistributionRepository->countByDistribution($distribution);
if (!$countPointSaleDistribution) {
$this->pointSaleDistributionBuilder->createAll($distribution, true);
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true);
}
}

public function initProductDistribution(Distribution $distribution): void
public function createProductDistributions(Distribution $distribution): void
{
$productArray = $this->productRepository->get();
$countProductDistribution = $this->productDistributionRepository->countByDistribution($distribution);

if (!$countProductDistribution) {
foreach ($productArray as $product) {
$this->linkProduct($distribution, $product);
$this->addProduct($distribution, $product);
}
}
}

/**
* Lie un produit à une distribution.
* Ajoute un produit à une distribution.
*/
public function linkProduct(Distribution $distribution, Product $product): ProductDistribution
public function addProduct(Distribution $distribution, Product $product): ProductDistribution
{
$productDistribution = $this->productDistributionBuilder->createIfNotExist($distribution, $product);
$productDistribution = $this->productDistributionBuilder->createProductDistributionIfNotExist($distribution, $product);
$this->updateOrderProductPrices($distribution, $product);

return $productDistribution;
@@ -115,24 +112,24 @@ class DistributionBuilder extends BaseService implements BuilderInterface
/**
* Lie un produit aux distributions futures.
*/
public function linkProductIncomingDistributions(Product $product): void
public function addProductIncomingDistributions(Product $product): void
{
$distributionArray = $this->distributionRepository->getIncoming();

foreach ($distributionArray as $distribution) {
$this->linkProduct($distribution, $product);
$this->addProduct($distribution, $product);
}
}

/**
* Lie un point de vente à une distribution
* Ajoute un point de vente à une distribution
*/
public function linkPointSale(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
public function addPointSale(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
{
$pointSaleDistribution = $this->pointSaleDistributionBuilder->createIfNotExist($distribution, $pointSale);
$dayString = $this->distributionSolver->getDayAsString($distribution);
$pointSaleDistribution = $this->pointSaleDistributionBuilder->createPointSaleDistributionIfNotExist($distribution, $pointSale);

$dayString = $this->distributionSolver->getDayAsString($distribution);
$pointSaleDistribution->delivery = 0;

$deliveryDay = 'delivery_' . $dayString;
if ($pointSale->$deliveryDay) {
$pointSaleDistribution->delivery = 1;
@@ -144,14 +141,13 @@ class DistributionBuilder extends BaseService implements BuilderInterface
}

/**
* Lie un point de vente aux distributions futures.
* Ajoute un point de vente aux distributions futures.
*/
public function linkPointSaleIncomingDistributions(PointSale $pointSale): void
public function addPointSaleIncomingDistributions(PointSale $pointSale): void
{
$distributionArray = $this->distributionRepository->getIncoming();

foreach ($distributionArray as $distribution) {
$this->linkPointSale($distribution, $pointSale);
$this->addPointSale($distribution, $pointSale);
}
}

@@ -187,15 +183,16 @@ class DistributionBuilder extends BaseService implements BuilderInterface
/**
* Active ou désactive la distribution.
*/
public function active(Distribution $distribution, bool $active = true): void
public function updateActive(Distribution $distribution, bool $active = true): void
{
$this->pointSaleDistributionBuilder->createAll($distribution, true);
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true);

$distribution->active = (int) $active;
$distribution->save();

if ($active) {
Subscription::addAll($distribution->date);
// @TODO : gérer avec les événements
//Subscription::addAll($distribution->date);
}
}
}

+ 9
- 8
common/logic/Distribution/Distribution/DistributionRepository.php Voir le fichier

@@ -26,14 +26,14 @@ class DistributionRepository extends BaseService implements RepositoryInterface
];
}

public function getOneById(int $id): ?Distribution
public function findOneDistributionById(int $id): ?Distribution
{
return Distribution::searchOne([
'id' => $id
]);
}

public function getOne(Producer $producer, string $date): ?Distribution
public function findOneDistribution(Producer $producer, string $date): ?Distribution
{
$paramsDistribution = [
'date' => $date,
@@ -43,7 +43,7 @@ class DistributionRepository extends BaseService implements RepositoryInterface
return Distribution::searchOne($paramsDistribution);
}

public function getOneByDate(string $date): ?Distribution
public function findOneDistributionByDate(string $date): ?Distribution
{
return Distribution::searchOne([
'distribution.date' => $date
@@ -53,7 +53,7 @@ class DistributionRepository extends BaseService implements RepositoryInterface
/**
* Récupère les distributions futures.
*/
public function getIncoming(): array
public function findDistributionsIncoming(): array
{
$distributionsArray = Distribution::find()
->where('date > \'' . date('Y-m-d') . '\'')
@@ -66,12 +66,13 @@ class DistributionRepository extends BaseService implements RepositoryInterface

return $distributionsArray;
}
public function isDateAvailable(Producer $producer, string $date = null): bool

// isDateAvailable
public function isDistributionDateAvailable(Producer $producer, string $date = null): bool
{
if($date && strlen($date)) {
$distribution = $this->getOne($producer, $date);
if($distribution && $this->distributionSolver->isAvailable($distribution)) {
$distribution = $this->findOneDistribution($producer, $date);
if($distribution && $this->distributionSolver->isDistributionAvailable($distribution)) {
return true;
}
}

+ 2
- 1
common/logic/Distribution/Distribution/DistributionSolver.php Voir le fichier

@@ -47,7 +47,8 @@ class DistributionSolver extends BaseService implements SolverInterface
return false;
}

public function isAvailable(Distribution $distribution): bool
// isAvailable
public function isDistributionAvailable(Distribution $distribution): bool
{
$producer = $distribution->producer;
$dateDistribution = $distribution->date;

+ 4
- 4
common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php Voir le fichier

@@ -49,7 +49,7 @@ class PointSaleDistributionBuilder extends BaseService implements BuilderInterfa

public function createPointSaleDistributionIfNotExist(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
{
$pointSaleDistribution = $this->pointSaleDistributionRepository->getOne($distribution, $pointSale);
$pointSaleDistribution = $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distribution, $pointSale);

if (!$pointSaleDistribution) {
$pointSaleDistribution = $this->createPointSaleDistribution($distribution, $pointSale);
@@ -62,12 +62,12 @@ class PointSaleDistributionBuilder extends BaseService implements BuilderInterfa
* Crée les PointSaleDistribution pour une distribution donnée.
*/
// setAll
public function createAllPointSaleDistribution(Distribution $distribution, bool $delivery): void
public function createAllPointSaleDistributions(Distribution $distribution, bool $delivery): void
{
$pointSaleArray = $this->pointSaleRepository->getByDistribution($distribution);
$pointSaleArray = $this->pointSaleRepository->findPointSaleDistributionsByDistribution($distribution);
foreach ($pointSaleArray as $pointSale) {
if (!$pointSale->pointSaleDistribution) {
$this->create($distribution, $pointSale);
$this->createPointSaleDistribution($distribution, $pointSale);
}
}


+ 3
- 3
common/logic/Distribution/PointSaleDistribution/PointSaleDistributionRepository.php Voir le fichier

@@ -19,7 +19,7 @@ class PointSaleDistributionRepository extends BaseService implements RepositoryI
] ;
}

public function getOne(Distribution $distribution, PointSale $pointSale): ?PointSaleDistribution
public function findOnePointSaleDistribution(Distribution $distribution, PointSale $pointSale): ?PointSaleDistribution
{
return PointSaleDistribution::searchOne([
'id_distribution' => $distribution->id,
@@ -27,14 +27,14 @@ class PointSaleDistributionRepository extends BaseService implements RepositoryI
]);
}

public function getByDistribution(Distribution $distribution)
public function findPointSaleDistributionsByDistribution(Distribution $distribution): array
{
return PointSaleDistribution::searchAll([
'id_distribution' => $distribution->id
]) ;
}

public function countByDistribution(Distribution $distribution): int
public function countPointSaleDistributionsByDistribution(Distribution $distribution): int
{
return PointSaleDistribution::searchCount([
'id_distribution' => $distribution->id

+ 9
- 29
common/logic/Distribution/ProductDistribution/ProductDistributionRepository.php Voir le fichier

@@ -20,14 +20,7 @@ class ProductDistributionRepository extends BaseService implements RepositoryInt
] ;
}

public function countByDistribution(Distribution $distribution): int
{
return ProductDistribution::searchCount([
'id_distribution' => $distribution->id
]);
}

public function getOne(Distribution $distribution, Product $product): ?ProductDistribution
public function findOneProductDistribution(Distribution $distribution, Product $product): ?ProductDistribution
{
return ProductDistribution::searchOne([
'id_distribution' => $distribution->id,
@@ -35,30 +28,17 @@ class ProductDistributionRepository extends BaseService implements RepositoryInt
]);
}

// @TODO : à déplacer dans OrderRepository
// searchByDistribution
public static function getByDistribution(Distribution $distribution): array
public function findProductDistributionsByDistribution(Distribution $distribution): array
{
$productDistributionArray = ProductDistribution::searchAll([
return ProductDistribution::searchAll([
'id_distribution' => $distribution->id
]) ;
}

$orderArray = Order::searchAll([
'distribution.id' => $distribution->id
]) ;

foreach ($productDistributionArray as $productDistribution) {
if (isset($productDistribution->product)) {
$arrayProductsDistribution[$productDistribution->id_product] = [
'active' => (int) $productDistribution->active,
'unavailable' => (int) $productDistribution->product->unavailable,
'quantity_max' => $productDistribution->quantity_max,
'quantity_order' => Order::getProductQuantity($productDistribution->id_product, $orderArray),
'quantity_remaining' => $productDistribution->quantity_max - Order::getProductQuantity($productDistribution->id_product, $orders)
];
}
}

return $productDistributionArray;
public function countProductDistributionsByDistribution(Distribution $distribution): int
{
return ProductDistribution::searchCount([
'id_distribution' => $distribution->id
]);
}
}

+ 26
- 1
common/logic/Order/Order/OrderRepository.php Voir le fichier

@@ -4,6 +4,8 @@ namespace common\logic\Order\Order;

use common\logic\BaseService;
use common\logic\Distribution\Distribution\Distribution;
use common\logic\Distribution\ProductDistribution\ProductDistribution;
use common\logic\Distribution\ProductDistribution\ProductDistributionRepository;
use common\logic\Order\ProductOrder\ProductOrder;
use common\logic\Order\ProductOrder\ProductOrderRepository;
use common\logic\Producer\Producer\Producer;
@@ -18,12 +20,14 @@ class OrderRepository extends BaseService implements RepositoryInterface
protected ProductOrderRepository $productOrderRepository;
protected ProducerRepository $producerRepository;
protected OrderSolver $orderSolver;
protected ProductDistributionRepository $productDistributionRepository;

public function __construct()
{
$this->productOrderRepository = $this->loadService(ProductOrderRepository::class);
$this->producerRepository = $this->loadService(ProducerRepository::class);
$this->orderSolver = $this->loadService(OrderSolver::class);
$this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class);
}

public function defaultOptionsSearch(): array
@@ -81,7 +85,8 @@ class OrderRepository extends BaseService implements RepositoryInterface
return false;
}

public function findByDistribution(Distribution $distribution, string $conditionAppend = '')
// findByDistribution
public function findOrdersByDistribution(Distribution $distribution, string $conditionAppend = '')
{
return Order::searchAll([
'distribution.date' => $distribution->date,
@@ -242,5 +247,25 @@ class OrderRepository extends BaseService implements RepositoryInterface
->one();
}

// searchByDistribution
public function findProductDistributionsByDistribution(Distribution $distribution): array
{
$orderArray = $this->findOrdersByDistribution($distribution);
$productDistributionArray = $this->productDistributionRepository->findProductDistributionsByDistribution();

foreach ($productDistributionArray as $productDistribution) {
if (isset($productDistribution->product)) {
$productDistributionArray[$productDistribution->id_product] = [
'active' => (int) $productDistribution->active,
'unavailable' => (int) $productDistribution->product->unavailable,
'quantity_max' => $productDistribution->quantity_max,
'quantity_order' => $this->orderSolver->getProductQuantity($productDistribution->product, $orderArray),
'quantity_remaining' => $productDistribution->quantity_max - $this->orderSolver->getProductQuantity($productDistribution->product, $orders)
];
}
}

return $productDistributionArray;
}

}

+ 1
- 1
producer/controllers/OrderController.php Voir le fichier

@@ -77,7 +77,7 @@ class OrderController extends ProducerBaseController
$params['order'] = $order;
}

if ($this->getDistributionManager()->isDateAvailable($producer, $date)) {
if ($this->getDistributionManager()->isDistributionDateAvailable($producer, $date)) {
$params['date'] = $date;
}


Chargement…
Annuler
Enregistrer