|
- <?php
-
- namespace common\logic\Distribution\PointSaleDistribution;
-
- use common\helpers\GlobalParam;
- use common\logic\BaseService;
- use common\logic\BuilderInterface;
- use common\logic\Distribution\Distribution\Distribution;
- use common\logic\Distribution\Distribution\DistributionRepository;
- use common\logic\PointSale\PointSale\PointSale;
- use common\logic\PointSale\PointSale\PointSaleRepository;
-
- class PointSaleDistributionBuilder extends BaseService implements BuilderInterface
- {
- protected PointSaleDistributionRepository $pointSaleDistributionRepository;
- protected PointSaleRepository $pointSaleRepository;
- protected DistributionRepository $distributionRepository;
-
- public function __construct()
- {
- $this->pointSaleDistributionRepository = $this->loadService(PointSaleDistributionRepository::class);
- $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
- $this->distributionRepository = $this->loadService(DistributionRepository::class);
- }
-
- /**
- * Instancie un PointSaleDistribution.
- */
- public function instanciate(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
- {
- $pointSaleDistribution = new PointSaleDistribution();
-
- $pointSaleDistribution->id_distribution = $distribution->id;
- $pointSaleDistribution->id_point_sale = $pointSale->id;
-
- return $pointSaleDistribution;
- }
-
- /**
- * Crée un PointSaleDistribution.
- */
- public function create(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
- {
- $pointSaleDistribution = $this->instanciate($distribution, $pointSale);
- $pointSaleDistribution->save();
-
- return $pointSaleDistribution;
- }
-
- public function createIfNotExist(Distribution $distribution, PointSale $pointSale): PointSaleDistribution
- {
- $pointSaleDistribution = $this->pointSaleDistributionRepository->getOne($distribution, $pointSale);
-
- if (!$pointSaleDistribution) {
- $pointSaleDistribution = $this->create($distribution, $pointSale);
- }
-
- return $pointSaleDistribution;
- }
-
- /**
- * Crée les PointSaleDistribution pour une distribution donnée.
- */
- // setAll
- public function createAll(Distribution $distribution, bool $delivery): void
- {
- $pointSaleArray = $this->pointSaleRepository->getByDistribution($distribution);
- foreach ($pointSaleArray as $pointSale) {
- if (!$pointSale->pointSaleDistribution) {
- $this->create($distribution, $pointSale);
- }
- }
-
- $pointSaleDistributionArray = $this->pointSaleDistributionRepository->getByDistribution($distribution);
- foreach ($pointSaleDistributionArray as $pointSaleDistribution) {
- $this->initDelivery($pointSaleDistribution, $delivery);
- }
- }
-
- public function initDelivery(PointSaleDistribution $pointSaleDistribution, bool $delivery)
- {
- $day = date('N', strtotime($pointSaleDistribution->distribution->date));
- $pointSale = $pointSaleDistribution->pointSale;
-
- if ($delivery &&
- (($day == 1 && $pointSale->delivery_monday) ||
- ($day == 2 && $pointSale->delivery_tuesday) ||
- ($day == 3 && $pointSale->delivery_wednesday) ||
- ($day == 4 && $pointSale->delivery_thursday) ||
- ($day == 5 && $pointSale->delivery_friday) ||
- ($day == 6 && $pointSale->delivery_saturday) ||
- ($day == 7 && $pointSale->delivery_sunday)
- )) {
- $pointSaleDistribution->delivery = 1;
- } else {
- $pointSaleDistribution->delivery = 0;
- }
-
- $pointSaleDistribution->save();
- }
- }
|