Pārlūkot izejas kodu

Refactoring repositories #1084

feature/souke
Guillaume Bourgeois pirms 1 gada
vecāks
revīzija
4806c28d07
9 mainītis faili ar 88 papildinājumiem un 31 dzēšanām
  1. +4
    -4
      backend/controllers/OrderController.php
  2. +19
    -0
      common/logic/AbstractRepository.php
  3. +24
    -0
      common/logic/AbstractRepositoryQuery.php
  4. +6
    -3
      common/logic/Config/TaxRate/Repository/TaxRateRepository.php
  5. +5
    -1
      common/logic/Config/TaxRate/Repository/TaxRateRepositoryQuery.php
  6. +9
    -21
      common/logic/Distribution/Distribution/Repository/DistributionRepository.php
  7. +14
    -0
      common/logic/Distribution/Distribution/Repository/DistributionRepositoryQuery.php
  8. +2
    -2
      common/logic/Order/Order/Service/OrderBuilder.php
  9. +5
    -0
      common/logic/ProducerContextTrait.php

+ 4
- 4
backend/controllers/OrderController.php Parādīt failu

@@ -423,7 +423,7 @@ class OrderController extends BackendController
$productManager = $this->getProductManager();
$productDistributionManager = $this->getProductDistributionManager();

$distribution = $distributionManager->findOneDistributionByDate($date);
$distribution = $distributionManager->findOneDistribution($date);
$selectedProductsArray = $productDistributionManager->findProductDistributionsByDistribution($distribution);
$productsArray = $productManager->findProducts();
$ordersArray = $orderManager->findOrdersByDate($date);
@@ -549,7 +549,7 @@ class OrderController extends BackendController
$data = [];
$filename = 'summary_' . $date;

$distribution = $distributionManager->findOneDistributionByDate($date);
$distribution = $distributionManager->findOneDistribution($date);
$selectedProductsArray = $productDistributionManager->findProductDistributionsByDistribution($distribution);

// head
@@ -608,7 +608,7 @@ class OrderController extends BackendController
$productDistributionManager = $this->getProductDistributionManager();

$data = [];
$distribution = $distributionManager->findOneDistributionByDate($date);
$distribution = $distributionManager->findOneDistribution($date);
$selectedProductsArray = $productDistributionManager->findProductDistributionsByDistribution($distribution);

// datas
@@ -782,7 +782,7 @@ class OrderController extends BackendController

$products = json_decode($products);
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale);
$distribution = $distributionManager->findOneDistributionByDate($date);
$distribution = $distributionManager->findOneDistribution($date);
$producerCurrent = $this->getProducerCurrent();

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date)

+ 19
- 0
common/logic/AbstractRepository.php Parādīt failu

@@ -4,5 +4,24 @@ namespace common\logic;

abstract class AbstractRepository extends AbstractService implements RepositoryInterface
{
public function createQuery()
{
$this->repositoryQuery->createQuery();

return $this->repositoryQuery;
}

public function createDefaultQuery()
{
$this->createQuery();

$defaultOptions = $this->getDefaultOptionsSearch();

// id producer
if(isset($defaultOptions['attribute_id_producer']) && $defaultOptions['attribute_id_producer']) {
$this->repositoryQuery->andWhere([$defaultOptions['attribute_id_producer'] => $this->getProducerContextId()]);
}

return $this->repositoryQuery;
}
}

+ 24
- 0
common/logic/AbstractRepositoryQuery.php Parādīt failu

@@ -2,7 +2,31 @@

namespace common\logic;

use yii\db\ActiveQuery;

class AbstractRepositoryQuery extends AbstractService implements RepositoryQueryInterface
{
protected ActiveQuery $query;

public function __call(string $name, $params): self
{
call_user_func_array([$this->query, $name], $params);

return $this;
}

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

public function findOne()
{
return $this->query->one();
}

public function filterById(int $id)
{
$this->query->andWhere(['id' => $id]);
}
}

+ 6
- 3
common/logic/Config/TaxRate/Repository/TaxRateRepository.php Parādīt failu

@@ -4,6 +4,7 @@ namespace common\logic\Config\TaxRate\Repository;

use common\logic\AbstractRepository;
use common\logic\Config\TaxRate\Model\TaxRate;
use yii\db\ActiveQuery;

class TaxRateRepository extends AbstractRepository
{
@@ -26,15 +27,17 @@ class TaxRateRepository extends AbstractRepository

public function findOneTaxRateById(int $id): ?TaxRate
{
return TaxRate::searchOne(['id' => $id]);
$query = $this->createQuery();
$query->filterById($id);
return $query->findOne();
}

public function findTaxRates(): array
{
return TaxRate::find()->all();
$query = $this->createQuery();
return $query->find();
}

// getTaxRateArray
public function findTaxRatesAsArray(): array
{
$taxRateArrayReturn = [];

+ 5
- 1
common/logic/Config/TaxRate/Repository/TaxRateRepositoryQuery.php Parādīt failu

@@ -3,8 +3,12 @@
namespace common\logic\Config\TaxRate\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Config\TaxRate\Model\TaxRate;

class TaxRateRepositoryQuery extends AbstractRepositoryQuery
{

public function createQuery()
{
$this->query = TaxRate::find();
}
}

+ 9
- 21
common/logic/Distribution/Distribution/Repository/DistributionRepository.php Parādīt failu

@@ -6,19 +6,18 @@ use common\helpers\GlobalParam;
use common\logic\AbstractRepository;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Service\DistributionSolver;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Subscription\Subscription\Model\Subscription;
use common\logic\Subscription\Subscription\Service\SubscriptionSolver;

class DistributionRepository extends AbstractRepository
{
protected DistributionRepositoryQuery $query;
protected DistributionRepositoryQuery $repositoryQuery;
protected DistributionSolver $distributionSolver;
protected SubscriptionSolver $subscriptionSolver;

public function loadDependencies(): void
{
$this->query = $this->loadService(DistributionRepositoryQuery::class);
$this->repositoryQuery = $this->loadService(DistributionRepositoryQuery::class);
$this->distributionSolver = $this->loadService(DistributionSolver::class);
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
}
@@ -35,30 +34,19 @@ class DistributionRepository extends AbstractRepository

public function findOneDistributionById(int $id): ?Distribution
{
return Distribution::searchOne([
'id' => $id
]);
return $this->createQuery()
->filterById($id)
->findOne();
}

public function findOneDistribution(string $date, bool $active = null): ?Distribution
{
$paramsDistribution = [
'date' => $date,
'distribution.id_producer' => $this->getProducerContext()->id
];

$query = $this->createDefaultQuery();
$query->filterByDate($date);
if(!is_null($active)) {
$paramsDistribution['active'] = $active;
$query->filterByActive($active);
}

return Distribution::searchOne($paramsDistribution);
}

public function findOneDistributionByDate(string $date): ?Distribution
{
return Distribution::searchOne([
'distribution.date' => $date
]);
return $query->findOne();
}

public function findOneFirstDistribution(): ?Distribution

+ 14
- 0
common/logic/Distribution/Distribution/Repository/DistributionRepositoryQuery.php Parādīt failu

@@ -3,8 +3,22 @@
namespace common\logic\Distribution\Distribution\Repository;

use common\logic\AbstractRepositoryQuery;
use common\logic\Distribution\Distribution\Model\Distribution;
use yii\db\ActiveQuery;

class DistributionRepositoryQuery extends AbstractRepositoryQuery
{
public function createQuery()
{
$this->query = Distribution::find();

return $this;
}

public function filterByDate(string $date)
{
$this->query->andWhere(['date' => $date]);

return $this;
}
}

+ 2
- 2
common/logic/Order/Order/Service/OrderBuilder.php Parādīt failu

@@ -112,7 +112,7 @@ class OrderBuilder extends AbstractBuilder
{
$now = date('Y-m-d');
$distributionDate = date('Y-m-d', strtotime($date));
$distribution = $this->distributionRepository->findOneDistributionByDate($distributionDate);
$distribution = $this->distributionRepository->findOneDistribution($distributionDate);

if ($distribution
&& $distribution->active
@@ -137,7 +137,7 @@ class OrderBuilder extends AbstractBuilder
public function createAllOrdersFromSubscriptions(string $date, bool $force = false): array
{
$orderArray = [];
$distribution = $this->distributionRepository->findOneDistributionByDate(date('Y-m-d', strtotime($date)));
$distribution = $this->distributionRepository->findOneDistribution(date('Y-m-d', strtotime($date)));

if ($distribution) {
$orderArray = $this->orderRepository->findOrdersByDistribution($distribution);

+ 5
- 0
common/logic/ProducerContextTrait.php Parādīt failu

@@ -24,4 +24,9 @@ trait ProducerContextTrait

return $this->producerContext;
}

public function getProducerContextId()
{
return $this->getProducerContext()->id;
}
}

Notiek ielāde…
Atcelt
Saglabāt