Browse Source

Refactoring services #885

refactoring
Guillaume 1 year ago
parent
commit
ab29441494
6 changed files with 43 additions and 29 deletions
  1. +18
    -0
      common/logic/BaseBuilder.php
  2. +4
    -3
      common/logic/Config/TaxRate/TaxRateBuilder.php
  3. +8
    -6
      common/logic/Distribution/Distribution/DistributionBuilder.php
  4. +3
    -5
      common/logic/Distribution/Distribution/DistributionSolver.php
  5. +7
    -12
      common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php
  6. +3
    -3
      common/logic/Distribution/ProductDistribution/ProductDistributionBuilder.php

+ 18
- 0
common/logic/BaseBuilder.php View File

<?php

namespace common\logic;

use yii\db\ActiveRecord;

class BaseBuilder extends BaseService
{
public function create(ActiveRecord $model): void
{
$model->save();
}

public function update(ActiveRecord $model): void
{
$model->save();
}
}

+ 4
- 3
common/logic/Config/TaxRate/TaxRateBuilder.php View File



namespace common\logic\Config\TaxRate; namespace common\logic\Config\TaxRate;


use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface; use common\logic\BuilderInterface;


class TaxRateBuilder extends BaseService implements BuilderInterface
class TaxRateBuilder extends BaseBuilder implements BuilderInterface
{ {
public function instanciateTaxRate(): TaxRate public function instanciateTaxRate(): TaxRate
{ {
public function createTaxRate(): TaxRate public function createTaxRate(): TaxRate
{ {
$taxRate = $this->instanciateTaxRate(); $taxRate = $this->instanciateTaxRate();
$taxRate->save();

$this->create($taxRate);


return $taxRate; return $taxRate;
} }

+ 8
- 6
common/logic/Distribution/Distribution/DistributionBuilder.php View File



namespace common\logic\Distribution\Distribution; namespace common\logic\Distribution\Distribution;


use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface; use common\logic\BuilderInterface;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistribution; use common\logic\Distribution\PointSaleDistribution\PointSaleDistribution;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionBuilder; use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionBuilder;
use common\logic\Product\Product\ProductRepository; use common\logic\Product\Product\ProductRepository;
use common\logic\User\UserProducer\UserProducerRepository; use common\logic\User\UserProducer\UserProducerRepository;


class DistributionBuilder extends BaseService implements BuilderInterface
class DistributionBuilder extends BaseBuilder implements BuilderInterface
{ {
protected DistributionRepository $distributionRepository; protected DistributionRepository $distributionRepository;
protected DistributionSolver $distributionSolver; protected DistributionSolver $distributionSolver;
public function createDistribution(Producer $producer, string $date, bool $delivery = true): Distribution public function createDistribution(Producer $producer, string $date, bool $delivery = true): Distribution
{ {
$distribution = $this->instanciateDistribution($producer, $date, $delivery); $distribution = $this->instanciateDistribution($producer, $date, $delivery);
$distribution->save();

$this->create($distribution);


$this->createPointSaleDistributions($distribution); $this->createPointSaleDistributions($distribution);
$this->createProductDistributions($distribution); $this->createProductDistributions($distribution);
$pointSaleDistribution->delivery = 1; $pointSaleDistribution->delivery = 1;
} }


$pointSaleDistribution->save();
$this->update($pointSaleDistribution);


return $pointSaleDistribution; return $pointSaleDistribution;
} }
$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->getOne($user, $distribution->producer);
$userProducer = $this->userProducerRepository->findOneUserProducer($user, $distribution->producer);
} }


$this->productOrderBuilder->updatePrice( $this->productOrderBuilder->updatePrice(
$this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true); $this->pointSaleDistributionBuilder->createAllPointSaleDistributions($distribution, true);


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

$this->update($distribution);


if ($active) { if ($active) {
// @TODO : gérer avec les événements // @TODO : gérer avec les événements

+ 3
- 5
common/logic/Distribution/Distribution/DistributionSolver.php View File

$dateToday = date('Y-m-d'); $dateToday = date('Y-m-d');


foreach ($distributionsArray as $keyDistribution => $distribution) { foreach ($distributionsArray as $keyDistribution => $distribution) {
if(!$this->isAvailable($distribution)) {
if(!$this->isDistributionAvailable($distribution)) {
unset($distributionsArray[$keyDistribution]); unset($distributionsArray[$keyDistribution]);
} }
} }


public function isPointSaleActive(Distribution $distribution, PointSale $pointSale): bool public function isPointSaleActive(Distribution $distribution, PointSale $pointSale): bool
{ {
$pointSaleActive = false;

if ($distribution->pointSaleDistribution) { if ($distribution->pointSaleDistribution) {
foreach ($distribution->pointSaleDistribution as $pointSaleDistribution) { foreach ($distribution->pointSaleDistribution as $pointSaleDistribution) {
if ($pointSaleDistribution->id_point_sale == $pointSale->id && $pointSaleDistribution->delivery) { if ($pointSaleDistribution->id_point_sale == $pointSale->id && $pointSaleDistribution->delivery) {
$pointSaleActive = true;
return true;
} }
} }
} }


return $pointSaleActive;
return false;
} }


} }

+ 7
- 12
common/logic/Distribution/PointSaleDistribution/PointSaleDistributionBuilder.php View File



namespace common\logic\Distribution\PointSaleDistribution; namespace common\logic\Distribution\PointSaleDistribution;


use common\helpers\GlobalParam;
use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface; use common\logic\BuilderInterface;
use common\logic\Distribution\Distribution\Distribution; use common\logic\Distribution\Distribution\Distribution;
use common\logic\Distribution\Distribution\DistributionRepository; use common\logic\Distribution\Distribution\DistributionRepository;
use common\logic\PointSale\PointSale\PointSale; use common\logic\PointSale\PointSale\PointSale;
use common\logic\PointSale\PointSale\PointSaleRepository; use common\logic\PointSale\PointSale\PointSaleRepository;


class PointSaleDistributionBuilder extends BaseService implements BuilderInterface
class PointSaleDistributionBuilder extends BaseBuilder implements BuilderInterface
{ {
protected PointSaleDistributionRepository $pointSaleDistributionRepository; protected PointSaleDistributionRepository $pointSaleDistributionRepository;
protected PointSaleRepository $pointSaleRepository; protected PointSaleRepository $pointSaleRepository;
public function createPointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution public function createPointSaleDistribution(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
{ {
$pointSaleDistribution = $this->instanciatePointSaleDistribution($distribution, $pointSale); $pointSaleDistribution = $this->instanciatePointSaleDistribution($distribution, $pointSale);
$pointSaleDistribution->save();

$this->create($pointSaleDistribution);


return $pointSaleDistribution; return $pointSaleDistribution;
} }


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

if (!$pointSaleDistribution) {
$pointSaleDistribution = $this->createPointSaleDistribution($distribution, $pointSale);
}

return $pointSaleDistribution;
return $this->pointSaleDistributionRepository->findOnePointSaleDistribution($distribution, $pointSale)
?? $this->createPointSaleDistribution($distribution, $pointSale);
} }


/** /**
$pointSaleDistribution->delivery = 0; $pointSaleDistribution->delivery = 0;
} }


$pointSaleDistribution->save();
$this->update($pointSaleDistribution);
} }
} }

+ 3
- 3
common/logic/Distribution/ProductDistribution/ProductDistributionBuilder.php View File



namespace common\logic\Distribution\ProductDistribution; namespace common\logic\Distribution\ProductDistribution;


use common\logic\BaseService;
use common\logic\BaseBuilder;
use common\logic\BuilderInterface; use common\logic\BuilderInterface;
use common\logic\Distribution\Distribution\Distribution; use common\logic\Distribution\Distribution\Distribution;
use common\logic\Distribution\Distribution\DistributionSolver; use common\logic\Distribution\Distribution\DistributionSolver;
use common\logic\Product\Product\Product; use common\logic\Product\Product\Product;
use common\logic\Product\Product\ProductSolver; use common\logic\Product\Product\ProductSolver;


class ProductDistributionBuilder extends BaseService implements BuilderInterface
class ProductDistributionBuilder extends BaseBuilder implements BuilderInterface
{ {
protected ProductDistributionRepository $productDistributionRepository; protected ProductDistributionRepository $productDistributionRepository;
protected DistributionSolver $distributionSolver; protected DistributionSolver $distributionSolver;
->initActive($productDistribution) ->initActive($productDistribution)
->initQuantityMax($productDistribution); ->initQuantityMax($productDistribution);


$productDistribution->save();
$this->create($productDistribution);


return $productDistribution; return $productDistribution;
} }

Loading…
Cancel
Save