Browse Source

[Administration] Refactoring DistributionController #1342

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
5696919be8
9 changed files with 192 additions and 190 deletions
  1. +12
    -92
      backend/controllers/DistributionController.php
  2. +5
    -3
      backend/controllers/SubscriptionController.php
  3. +25
    -2
      common/logic/Distribution/Distribution/Service/DistributionBuilder.php
  4. +1
    -86
      common/logic/Order/Order/Service/OrderBuilder.php
  5. +15
    -0
      common/logic/PointSale/PointSale/Repository/PointSaleRepository.php
  6. +3
    -3
      common/logic/Subscription/Subscription/Event/DistributionObserver.php
  7. +9
    -1
      common/logic/Subscription/Subscription/Module/SubscriptionModule.php
  8. +117
    -0
      common/logic/Subscription/Subscription/Service/OrderManager.php
  9. +5
    -3
      producer/controllers/SubscriptionController.php

+ 12
- 92
backend/controllers/DistributionController.php View File

use common\logic\Distribution\Distribution\Model\Distribution; use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Document\DeliveryNote\Model\DeliveryNote; use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Order\Order\Model\Order; use common\logic\Order\Order\Model\Order;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\Producer\Producer\Model\Producer; use common\logic\Producer\Producer\Model\Producer;
use common\logic\Product\Product\Model\Product; use common\logic\Product\Product\Model\Product;
use DateTime; use DateTime;


/** /**
* Active/désactive un jour de distribution. * Active/désactive un jour de distribution.
*
* @param integer $idDistribution
* @param string $date
* @param boolean $active
* @return array
*/ */
public function actionAjaxProcessActiveDistribution(int $idDistribution = 0, string $date = '', bool $active = false) public function actionAjaxProcessActiveDistribution(int $idDistribution = 0, string $date = '', bool $active = false)
{ {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$distributionModule = $this-> getDistributionModule(); $distributionModule = $this-> getDistributionModule();
$distribution = null;


if ($idDistribution) { if ($idDistribution) {
$distribution = $distributionModule->findOneDistributionById($idDistribution);
$distribution = $distributionModule->getRepository()->findOneDistributionById($idDistribution);
} }

$format = 'Y-m-d';
$dateObject = DateTime::createFromFormat($format, $date);
if ($dateObject && $dateObject->format($format) === $date) {
$distribution = $distributionModule->createDistributionIfNotExist($date);
if ($distributionModule->getSolver()->validateDistributionDate($date)) {
$distribution = $distributionModule->getBuilder()->createDistributionIfNotExist($date);
} }


if (isset($distribution) && $distribution) {
$distributionModule->activeDistribution($distribution, $active);
if ($distribution) {
$distributionModule->getBuilder()->activeDistribution($distribution, $active);
return ['success']; return ['success'];
} }


} }


/** /**
* Change l'état d'une semaine de production (activé, désactivé).
*
* @param string $date
* @param integer $active
* Change l'état d'une semaine de distribution (activé, désactivé).
*/ */
public function actionAjaxProcessActiveWeekDistribution($date, $active)
public function actionAjaxProcessActiveWeekDistribution(string $date, int $active)
{ {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

$week = sprintf('%02d', date('W', strtotime($date)));
$start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
$dateMonday = date('Y-m-d', strtotime('Monday', $start));
$dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
$dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
$dateThursday = date('Y-m-d', strtotime('Thursday', $start));
$dateFriday = date('Y-m-d', strtotime('Friday', $start));
$dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
$dateSunday = date('Y-m-d', strtotime('Sunday', $start));

$pointsSaleArray = PointSale::searchAll(['status' => 1]);

$activeMonday = false;
$activeTuesday = false;
$activeWednesday = false;
$activeThursday = false;
$activeFriday = false;
$activeSaturday = false;
$activeSunday = false;

foreach ($pointsSaleArray as $pointSale) {
if ($pointSale->delivery_monday) {
$activeMonday = true;
}
if ($pointSale->delivery_tuesday) {
$activeTuesday = true;
}
if ($pointSale->delivery_wednesday) {
$activeWednesday = true;
}
if ($pointSale->delivery_thursday) {
$activeThursday = true;
}
if ($pointSale->delivery_friday) {
$activeFriday = true;
}
if ($pointSale->delivery_saturday) {
$activeSaturday = true;
}
if ($pointSale->delivery_sunday) {
$activeSunday = true;
}
}

if ($activeMonday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateMonday, $active);
}
if ($activeTuesday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateTuesday, $active);
}
if ($activeWednesday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateWednesday, $active);
}
if ($activeThursday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateThursday, $active);
}
if ($activeFriday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateFriday, $active);
}
if ($activeSaturday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateSaturday, $active);
}
if ($activeSunday || !$active) {
$this->actionAjaxProcessActiveDistribution(0, $dateSunday, $active);
}

$this->getDistributionModule()->getBuilder()->activeDistributionsOfWeek($date, $active);
return ['success']; return ['success'];
} }


/** /**
* Ajoute les commandes récurrentes pour une date donnée. * Ajoute les commandes récurrentes pour une date donnée.
*
* @param string $date
*/ */
public function actionAjaxProcessAddSubscriptions($date)
public function actionAjaxProcessAddSubscriptions(string $date)
{ {
$ordersArray = $this->getOrderModule()->createAllOrdersFromSubscriptions($date, true);
$ordersArray = $this->getSubscriptionModule()->getOrderManager()
->createAllOrdersFromSubscriptions($date, true);


if($ordersArray && count($ordersArray)) { if($ordersArray && count($ordersArray)) {
return Ajax::responseSuccess('Les abonnements ont bien été importés.'); return Ajax::responseSuccess('Les abonnements ont bien été importés.');


/** /**
* Synchronise les commandes avec la plateforme Tiller pour une date donnée. * Synchronise les commandes avec la plateforme Tiller pour une date donnée.
*
* @param string $date
*/ */
public function actionAjaxProcessSynchroTiller(string $date) public function actionAjaxProcessSynchroTiller(string $date)
{ {

+ 5
- 3
backend/controllers/SubscriptionController.php View File

$messageOrdersDeleted = ''; $messageOrdersDeleted = '';


if ($model->date_end) { if ($model->date_end) {
$countOrdersDeleted = $orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription, true);
$countOrdersDeleted = $subscriptionModule->getOrderManager()
->deleteOrdersIncomingDistributionsFromSubscription($subscription, true);
if ($countOrdersDeleted) { if ($countOrdersDeleted) {
$messageOrdersDeleted = '<br />' . $countOrdersDeleted . ' commandes supprimées'; $messageOrdersDeleted = '<br />' . $countOrdersDeleted . ' commandes supprimées';
} }
$orderModule = $this->getOrderModule(); $orderModule = $this->getOrderModule();


$subscription = $subscriptionModule->findOneSubscriptionById($id); $subscription = $subscriptionModule->findOneSubscriptionById($id);
$orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription);
$subscriptionModule->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription);
$subscriptionModule->deleteSubscription($subscription); $subscriptionModule->deleteSubscription($subscription);


$this->setFlash('success', 'Abonnement supprimé'); $this->setFlash('success', 'Abonnement supprimé');


if ($generate) { if ($generate) {
if ($update) { if ($update) {
$orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription);
$subscriptionModule->getOrderManager()
->deleteOrdersIncomingDistributionsFromSubscription($subscription);
} }
foreach ($matchedDistributionsArray as $distribution) { foreach ($matchedDistributionsArray as $distribution) {
$orderModule->createOrderFromSubscription($subscription, $distribution->date); $orderModule->createOrderFromSubscription($subscription, $distribution->date);

+ 25
- 2
common/logic/Distribution/Distribution/Service/DistributionBuilder.php View File

use common\logic\Order\Order\Repository\OrderRepository; use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\ProductOrder\Service\ProductOrderBuilder; use common\logic\Order\ProductOrder\Service\ProductOrderBuilder;
use common\logic\PointSale\PointSale\Model\PointSale; use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\PointSale\PointSale\Repository\PointSaleRepository;
use common\logic\Product\Product\Model\Product; use common\logic\Product\Product\Model\Product;
use common\logic\Product\Product\Repository\ProductRepository; use common\logic\Product\Product\Repository\ProductRepository;
use common\logic\Product\Product\Service\ProductSolver; use common\logic\Product\Product\Service\ProductSolver;
protected UserProducerRepository $userProducerRepository; protected UserProducerRepository $userProducerRepository;
protected ProductOrderBuilder $productOrderBuilder; protected ProductOrderBuilder $productOrderBuilder;
protected ProductSolver $productSolver; protected ProductSolver $productSolver;
protected PointSaleRepository $pointSaleRepository;


public function loadDependencies(): void public function loadDependencies(): void
{ {
$this->userProducerRepository = $this->loadService(UserProducerRepository::class); $this->userProducerRepository = $this->loadService(UserProducerRepository::class);
$this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class); $this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class);
$this->productSolver = $this->loadService(ProductSolver::class); $this->productSolver = $this->loadService(ProductSolver::class);
$this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
} }


public function instanciateDistribution(string $date, bool $delivery = true): Distribution public function instanciateDistribution(string $date, bool $delivery = true): Distribution
} }
} }


public function activeDistributionByDate(\DateTime $date): void
public function activeDistributionByDate(\DateTime $date, bool $active = true): void
{ {
$distribution = $this->createDistributionIfNotExist($date->format('Y-m-d')); $distribution = $this->createDistributionIfNotExist($date->format('Y-m-d'));
$this->activeDistribution($distribution);
$this->activeDistribution($distribution, $active);
}

public function activeDistributionByDayWeek(string $date, string $dayWeek, bool $active = true)
{
$week = sprintf('%02d', date('W', strtotime($date)));
$start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
$dateDay = new \DateTime(date('Y-m-d', strtotime($dayWeek, $start)));
$activeDay = $this->pointSaleRepository->isDayOfWeekWithDelivery($dayWeek);
if ($activeDay || !$active) {
$this->activeDistributionByDate($dateDay, $active);
}
}

public function activeDistributionsOfWeek(string $date, bool $active = true): void
{
$daysWeekArray = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

foreach($daysWeekArray as $dayWeek) {
$this->activeDistributionByDayWeek($date, $dayWeek, $active);
}
} }
} }

+ 1
- 86
common/logic/Order/Order/Service/OrderBuilder.php View File

return $order; return $order;
} }


/**
* Ajoute l'abonnement' pour une date donnée.
*/
public function createOrderFromSubscription(Subscription $subscription, string $date, bool $force = false): ?Order
{
$now = date('Y-m-d');
$distributionDate = date('Y-m-d', strtotime($date));
$distribution = $this->distributionRepository->findOneDistribution($distributionDate);

if ($distribution
&& $distribution->active
&& ($distributionDate > $now || $force)
&& count($subscription->productSubscription)
&& $subscription->pointSale) {

$order = $this->createOrder($distribution);
$this->updateOrderFromSubscription($order, $subscription);

return $order;
}

return null;
}

/**
* Ajoute les commandes pour une date donnée à partir des abonnements.
*/
public function createAllOrdersFromSubscriptions(string $date, bool $force = false): array
{
$ordersSubscriptionsArray = [];
$distribution = $this->distributionRepository->findOneDistribution(date('Y-m-d', strtotime($date)));

if ($distribution) {
$orderArray = $this->orderRepository->findOrdersByDistribution($distribution);
$subscriptionArray = $this->subscriptionRepository->findSubscriptionsByDate($date);
foreach ($subscriptionArray as $subscription) {
if (!$this->subscriptionSolver->hasOrderAlreadyExist($subscription, $orderArray)) {
$order = $this->createOrderFromSubscription($subscription, $date, $force);
if ($order) {
$ordersSubscriptionsArray[] = $order;
}
}
}
}

return $ordersSubscriptionsArray;
}

public function updateOrdersIncomingDistributionsFromSubscription(Subscription $subscription, $update = false): array
{
$orderArray = [];
$matchedDistributionsArray = $this->distributionRepository->findDistributionsIncomingMatchWithSubscrtiption($subscription, true);

if ($update) {
$this->deleteOrdersIncomingDistributionsFromSubscription($subscription);
}

if (count($matchedDistributionsArray)) {
foreach ($matchedDistributionsArray as $distribution) {
$orderArray[] = $this->createOrderFromSubscription($subscription, $distribution->date);
}
}

return $orderArray;
}

public function deleteOrdersIncomingDistributionsFromSubscription(Subscription $subscription, bool $deleteAfterDateEnd = false): int
{
$ordersArray = $this->orderRepository->findOrdersIncomingBySubscription($subscription, !$deleteAfterDateEnd);
$countOrdersDeleted = 0;

if ($ordersArray && count($ordersArray)) {
foreach ($ordersArray as $order) {
if ($this->distributionSolver->isDistributionAvailable($order->distribution)) {
$theOrder = $this->orderRepository->findOneOrderById($order->id);
$this->initOrder($theOrder);
$this->deleteOrder($order, true);
$countOrdersDeleted++;
}
}
}

return $countOrdersDeleted;
}

public function addProductOrdersFromSubscription(Order $order, Subscription $subscription): bool public function addProductOrdersFromSubscription(Order $order, Subscription $subscription): bool
{ {
$productsAdd = false; $productsAdd = false;
$this->initOrderAutoPaymentFromSubscription($order, $subscription); $this->initOrderAutoPaymentFromSubscription($order, $subscription);
$this->addProductOrdersFromSubscription($order, $subscription); $this->addProductOrdersFromSubscription($order, $subscription);


$this->saveUpdate($order);
$this->update($order);
} }


public function initOrderBaseFromSubscription(Order $order, Subscription $subscription): void public function initOrderBaseFromSubscription(Order $order, Subscription $subscription): void

+ 15
- 0
common/logic/PointSale/PointSale/Repository/PointSaleRepository.php View File



return count($results); return count($results);
} }

public function isDayOfWeekWithDelivery(string $dayWeek): bool
{
$activeDay = false;
$pointsSaleArray = $this->findPointSales();
$fieldDelivery = 'delivery_'.strtolower($dayWeek);

foreach ($pointsSaleArray as $pointSale) {
if ($pointSale->$fieldDelivery) {
$activeDay = true;
}
}

return $activeDay;
}
} }

+ 3
- 3
common/logic/Subscription/Subscription/Event/DistributionObserver.php View File



use common\logic\Distribution\Distribution\Event\DistributionActiveEvent; use common\logic\Distribution\Distribution\Event\DistributionActiveEvent;
use common\logic\Distribution\Distribution\Model\Distribution; use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Order\Order\Module\OrderModule;
use common\logic\Subscription\Subscription\Module\SubscriptionModule;
use justcoded\yii2\eventlistener\observers\Observer; use justcoded\yii2\eventlistener\observers\Observer;


class DistributionObserver extends Observer class DistributionObserver extends Observer
public static function onDistributionActive(DistributionActiveEvent $event): void public static function onDistributionActive(DistributionActiveEvent $event): void
{ {
$distribution = $event->distribution; $distribution = $event->distribution;
$orderModule = OrderModule::getInstance();
$subscriptionModule = SubscriptionModule::getInstance();


$orderModule->createAllOrdersFromSubscriptions($distribution->date);
$subscriptionModule->getOrderManager()->createAllOrdersFromSubscriptions($distribution->date);
} }
} }

+ 9
- 1
common/logic/Subscription/Subscription/Module/SubscriptionModule.php View File



use common\logic\AbstractModule; use common\logic\AbstractModule;
use common\logic\Subscription\Subscription\Repository\SubscriptionRepository; use common\logic\Subscription\Subscription\Repository\SubscriptionRepository;
use common\logic\Subscription\Subscription\Service\OrderManager;
use common\logic\Subscription\Subscription\Service\SubscriptionBuilder; use common\logic\Subscription\Subscription\Service\SubscriptionBuilder;
use common\logic\Subscription\Subscription\Service\SubscriptionDefinition; use common\logic\Subscription\Subscription\Service\SubscriptionDefinition;
use common\logic\Subscription\Subscription\Service\SubscriptionSolver; use common\logic\Subscription\Subscription\Service\SubscriptionSolver;
* @mixin SubscriptionSolver * @mixin SubscriptionSolver
* @mixin SubscriptionRepository * @mixin SubscriptionRepository
* @mixin SubscriptionBuilder * @mixin SubscriptionBuilder
* @mixin OrderManager
*/ */
class SubscriptionModule extends AbstractModule class SubscriptionModule extends AbstractModule
{ {
SubscriptionDefinition::class, SubscriptionDefinition::class,
SubscriptionSolver::class, SubscriptionSolver::class,
SubscriptionRepository::class, SubscriptionRepository::class,
SubscriptionBuilder::class
SubscriptionBuilder::class,
OrderManager::class,
]; ];
} }


{ {
return SubscriptionBuilder::getInstance(); return SubscriptionBuilder::getInstance();
} }

public function getOrderManager(): OrderManager
{
return OrderManager::getInstance();
}
} }

+ 117
- 0
common/logic/Subscription/Subscription/Service/OrderManager.php View File

<?php

namespace common\logic\Subscription\Subscription\Service;

use common\logic\AbstractManager;
use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\Distribution\Service\DistributionSolver;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\Order\Service\OrderBuilder;
use common\logic\Subscription\Subscription\Model\Subscription;
use common\logic\Subscription\Subscription\Repository\SubscriptionRepository;

class OrderManager extends AbstractManager
{
protected DistributionSolver $distributionSolver;
protected DistributionRepository $distributionRepository;
protected OrderRepository $orderRepository;
protected OrderBuilder $orderBuilder;
protected SubscriptionRepository $subscriptionRepository;
protected SubscriptionSolver $subscriptionSolver;

public function loadDependencies(): void
{
$this->distributionSolver = $this->loadService(DistributionSolver::class);
$this->distributionRepository = $this->loadService(DistributionRepository::class);
$this->orderRepository = $this->loadService(OrderRepository::class);
$this->orderBuilder = $this->loadService(OrderBuilder::class);
$this->subscriptionRepository = $this->loadService(SubscriptionRepository::class);
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
}

/**
* Ajoute l'abonnement' pour une date donnée.
*/
public function createOrderFromSubscription(Subscription $subscription, string $date, bool $force = false): ?Order
{
$now = date('Y-m-d');
$distributionDate = date('Y-m-d', strtotime($date));
$distribution = $this->distributionRepository->findOneDistribution($distributionDate);

if ($distribution
&& $distribution->active
&& ($distributionDate > $now || $force)
&& count($subscription->productSubscription)
&& $subscription->pointSale) {

$order = $this->orderBuilder->createOrder($distribution);
$this->orderBuilder->updateOrderFromSubscription($order, $subscription);

return $order;
}

return null;
}

/**
* Ajoute les commandes pour une date donnée à partir des abonnements.
*/
public function createAllOrdersFromSubscriptions(string $date, bool $force = false): array
{
$ordersSubscriptionsArray = [];
$distribution = $this->distributionRepository->findOneDistribution(date('Y-m-d', strtotime($date)));

if ($distribution) {
$orderArray = $this->orderRepository->findOrdersByDistribution($distribution);
$subscriptionArray = $this->subscriptionRepository->findSubscriptionsByDate($date);
foreach ($subscriptionArray as $subscription) {
if (!$this->subscriptionSolver->hasOrderAlreadyExist($subscription, $orderArray)) {
$order = $this->createOrderFromSubscription($subscription, $date, $force);
if ($order) {
$ordersSubscriptionsArray[] = $order;
}
}
}
}

return $ordersSubscriptionsArray;
}

public function updateOrdersIncomingDistributionsFromSubscription(Subscription $subscription, $update = false): array
{
$orderArray = [];
$matchedDistributionsArray = $this->distributionRepository->findDistributionsIncomingMatchWithSubscrtiption($subscription, true);

if ($update) {
$this->deleteOrdersIncomingDistributionsFromSubscription($subscription);
}

if (count($matchedDistributionsArray)) {
foreach ($matchedDistributionsArray as $distribution) {
$orderArray[] = $this->createOrderFromSubscription($subscription, $distribution->date);
}
}

return $orderArray;
}

public function deleteOrdersIncomingDistributionsFromSubscription(Subscription $subscription, bool $deleteAfterDateEnd = false): int
{
$ordersArray = $this->orderRepository->findOrdersIncomingBySubscription($subscription, !$deleteAfterDateEnd);
$countOrdersDeleted = 0;

if ($ordersArray && count($ordersArray)) {
foreach ($ordersArray as $order) {
if ($this->distributionSolver->isDistributionAvailable($order->distribution)) {
$theOrder = $this->orderRepository->findOneOrderById($order->id);
$this->orderBuilder->initOrder($theOrder);
$this->orderBuilder->deleteOrder($order, true);
$countOrdersDeleted++;
}
}
}

return $countOrdersDeleted;
}
}

+ 5
- 3
producer/controllers/SubscriptionController.php View File



if ($model->load($posts) && $model->validate() && $model->save()) { if ($model->load($posts) && $model->validate() && $model->save()) {


$subscription = $subscriptionModule->findOneSubscriptionById($model->id);
$this->getOrderModule()->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate);
$subscription = $subscriptionModule->getRepository()->findOneSubscriptionById($model->id);
$subscriptionModule->getOrderManager()
->updateOrdersIncomingDistributionsFromSubscription($subscription, $isUpdate);


if ($isUpdate) { if ($isUpdate) {
$this->setFlash('success', 'Abonnement modifié'); $this->setFlash('success', 'Abonnement modifié');
$subscriptionModule->deleteSubscription($subscription); $subscriptionModule->deleteSubscription($subscription);


// @TODO : gérer via événements // @TODO : gérer via événements
$this->getOrderModule()->deleteOrdersIncomingDistributionsFromSubscription($subscription);
$this->getSubscriptionModule()->getOrderManager()
->deleteOrdersIncomingDistributionsFromSubscription($subscription);


$this->setFlash('success', 'Abonnement supprimé'); $this->setFlash('success', 'Abonnement supprimé');



Loading…
Cancel
Save