class BackendController extends CommonController | class BackendController extends CommonController | ||||
{ | { | ||||
public function beforeAction($event) | |||||
{ | |||||
// Contexte producteur | |||||
$this->getLogic()->setProducerContext($this->getProducerCurrent()); | |||||
return parent::beforeAction($event); | |||||
} | |||||
/** | /** | ||||
* Vérifie si l'utilisateur a au moins saisi un produit et un point de vente. | * Vérifie si l'utilisateur a au moins saisi un produit et un point de vente. | ||||
* Si ce n'est pas le cas : redirection vers le tableau de bord. | * Si ce n'est pas le cas : redirection vers le tableau de bord. |
use common\logic\Order\OrderStatusHistory\OrderStatusHistoryContainer; | use common\logic\Order\OrderStatusHistory\OrderStatusHistoryContainer; | ||||
use common\logic\Order\ProductOrder\ProductOrderContainer; | use common\logic\Order\ProductOrder\ProductOrderContainer; | ||||
use common\logic\PointSale\PointSale\PointSaleContainer; | use common\logic\PointSale\PointSale\PointSaleContainer; | ||||
use common\logic\Producer\Producer\Producer; | |||||
use common\logic\Producer\Producer\ProducerContainer; | use common\logic\Producer\Producer\ProducerContainer; | ||||
use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeContainer; | use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeContainer; | ||||
use common\logic\Product\Product\ProductContainer; | use common\logic\Product\Product\ProductContainer; | ||||
]; | ]; | ||||
} | } | ||||
public function setProducerContext(Producer $producer) | |||||
{ | |||||
foreach($this->getContainers() as $container) { | |||||
foreach($container->getServices() as $serviceClass) { | |||||
$instanceService = $serviceClass::getInstance(); | |||||
$instanceService->setProducerContext($producer); | |||||
} | |||||
} | |||||
} | |||||
/* | /* | ||||
* Hiérarchie des apps | * Hiérarchie des apps | ||||
*/ | */ |
abstract class AbstractService extends AbstractSingleton implements ServiceInterface | abstract class AbstractService extends AbstractSingleton implements ServiceInterface | ||||
{ | { | ||||
use ProducerContextTrait; | |||||
protected function getHierarchy(): array | protected function getHierarchy(): array | ||||
{ | { | ||||
return [ | return [ |
namespace common\logic\Distribution\Distribution; | namespace common\logic\Distribution\Distribution; | ||||
use common\logic\AbstractBuilder; | use common\logic\AbstractBuilder; | ||||
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\Distribution\PointSaleDistribution\PointSaleDistributionRepository; | use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionRepository; | ||||
$this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class); | $this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class); | ||||
} | } | ||||
public function instanciateDistribution(Producer $producer, string $date, bool $delivery = true): Distribution | |||||
public function instanciateDistribution(string $date, bool $delivery = true): Distribution | |||||
{ | { | ||||
$distribution = new Distribution(); | $distribution = new Distribution(); | ||||
$distribution->id_producer = $producer->id; | |||||
$distribution->populateProducer($this->getProducerContext()); | |||||
$distribution->date = $date; | $distribution->date = $date; | ||||
$distribution->delivery = (int) $delivery; | $distribution->delivery = (int) $delivery; | ||||
} | } | ||||
// initDistribution | // initDistribution | ||||
public function createDistribution(Producer $producer, string $date, bool $delivery = true): Distribution | |||||
public function createDistribution(string $date, bool $delivery = true): Distribution | |||||
{ | { | ||||
$distribution = $this->instanciateDistribution($producer, $date, $delivery); | |||||
$distribution = $this->instanciateDistribution($date, $delivery); | |||||
$this->saveCreate($distribution); | $this->saveCreate($distribution); | ||||
return $distribution; | return $distribution; | ||||
} | } | ||||
public function createDistributionIfNotExist(Producer $producer, string $date, int $delivery = 1) | |||||
public function createDistributionIfNotExist(string $date, int $delivery = 1) | |||||
{ | { | ||||
return $this->distributionRepository->findOneDistribution($producer, $date) | |||||
?? $this->createDistribution($producer, $date, $delivery); | |||||
return $this->distributionRepository->findOneDistribution($date) | |||||
?? $this->createDistribution($date, $delivery); | |||||
} | } | ||||
public function createPointSaleDistributions(Distribution $distribution): void | public function createPointSaleDistributions(Distribution $distribution): void |
]); | ]); | ||||
} | } | ||||
public function findOneDistribution(Producer $producer, string $date, bool $active = null): ?Distribution | |||||
public function findOneDistribution(string $date, bool $active = null): ?Distribution | |||||
{ | { | ||||
$paramsDistribution = [ | $paramsDistribution = [ | ||||
'date' => $date, | 'date' => $date, | ||||
'distribution.id_producer' => $producer->id | |||||
'distribution.id_producer' => $this->getProducerContext() | |||||
]; | ]; | ||||
if(!is_null($active)) { | if(!is_null($active)) { |
<?php | |||||
namespace common\logic; | |||||
use common\logic\Producer\Producer\Producer; | |||||
use yii\base\ErrorException; | |||||
trait ProducerContextTrait | |||||
{ | |||||
protected ?Producer $producerContext = null; | |||||
public function setProducerContext(Producer $producer): void | |||||
{ | |||||
$this->producerContext = $producer; | |||||
} | |||||
public function getProducerContext(): Producer | |||||
{ | |||||
if(is_null($this->producerContext)) { | |||||
throw new ErrorException("Le contexte producteur n''est pas défini."); | |||||
} | |||||
return $this->producerContext; | |||||
} | |||||
} |