@@ -45,7 +45,6 @@ use common\helpers\Price; | |||
use common\logic\Distribution\Distribution\Model\Distribution; | |||
use common\logic\Document\DeliveryNote\Model\DeliveryNote; | |||
use common\logic\Order\Order\Model\Order; | |||
use common\logic\PointSale\PointSale\Model\PointSale; | |||
use common\logic\Producer\Producer\Model\Producer; | |||
use common\logic\Product\Product\Model\Product; | |||
use DateTime; | |||
@@ -690,29 +689,22 @@ class DistributionController extends BackendController | |||
/** | |||
* 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) | |||
{ | |||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||
$distributionModule = $this-> getDistributionModule(); | |||
$distribution = null; | |||
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']; | |||
} | |||
@@ -720,92 +712,22 @@ class DistributionController extends BackendController | |||
} | |||
/** | |||
* 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; | |||
$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']; | |||
} | |||
/** | |||
* 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)) { | |||
return Ajax::responseSuccess('Les abonnements ont bien été importés.'); | |||
@@ -816,8 +738,6 @@ class DistributionController extends BackendController | |||
/** | |||
* Synchronise les commandes avec la plateforme Tiller pour une date donnée. | |||
* | |||
* @param string $date | |||
*/ | |||
public function actionAjaxProcessSynchroTiller(string $date) | |||
{ |
@@ -209,7 +209,8 @@ class SubscriptionController extends BackendController | |||
$messageOrdersDeleted = ''; | |||
if ($model->date_end) { | |||
$countOrdersDeleted = $orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription, true); | |||
$countOrdersDeleted = $subscriptionModule->getOrderManager() | |||
->deleteOrdersIncomingDistributionsFromSubscription($subscription, true); | |||
if ($countOrdersDeleted) { | |||
$messageOrdersDeleted = '<br />' . $countOrdersDeleted . ' commandes supprimées'; | |||
} | |||
@@ -241,7 +242,7 @@ class SubscriptionController extends BackendController | |||
$orderModule = $this->getOrderModule(); | |||
$subscription = $subscriptionModule->findOneSubscriptionById($id); | |||
$orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
$subscriptionModule->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
$subscriptionModule->deleteSubscription($subscription); | |||
$this->setFlash('success', 'Abonnement supprimé'); | |||
@@ -260,7 +261,8 @@ class SubscriptionController extends BackendController | |||
if ($generate) { | |||
if ($update) { | |||
$orderModule->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
$subscriptionModule->getOrderManager() | |||
->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
} | |||
foreach ($matchedDistributionsArray as $distribution) { | |||
$orderModule->createOrderFromSubscription($subscription, $distribution->date); |
@@ -15,6 +15,7 @@ use common\logic\Distribution\ProductDistribution\Service\ProductDistributionBui | |||
use common\logic\Order\Order\Repository\OrderRepository; | |||
use common\logic\Order\ProductOrder\Service\ProductOrderBuilder; | |||
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\Repository\ProductRepository; | |||
use common\logic\Product\Product\Service\ProductSolver; | |||
@@ -33,6 +34,7 @@ class DistributionBuilder extends AbstractBuilder | |||
protected UserProducerRepository $userProducerRepository; | |||
protected ProductOrderBuilder $productOrderBuilder; | |||
protected ProductSolver $productSolver; | |||
protected PointSaleRepository $pointSaleRepository; | |||
public function loadDependencies(): void | |||
{ | |||
@@ -47,6 +49,7 @@ class DistributionBuilder extends AbstractBuilder | |||
$this->userProducerRepository = $this->loadService(UserProducerRepository::class); | |||
$this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class); | |||
$this->productSolver = $this->loadService(ProductSolver::class); | |||
$this->pointSaleRepository = $this->loadService(PointSaleRepository::class); | |||
} | |||
public function instanciateDistribution(string $date, bool $delivery = true): Distribution | |||
@@ -199,9 +202,29 @@ class DistributionBuilder extends AbstractBuilder | |||
} | |||
} | |||
public function activeDistributionByDate(\DateTime $date): void | |||
public function activeDistributionByDate(\DateTime $date, bool $active = true): void | |||
{ | |||
$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); | |||
} | |||
} | |||
} |
@@ -117,91 +117,6 @@ class OrderBuilder extends AbstractBuilder | |||
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 | |||
{ | |||
$productsAdd = false; | |||
@@ -234,7 +149,7 @@ class OrderBuilder extends AbstractBuilder | |||
$this->initOrderAutoPaymentFromSubscription($order, $subscription); | |||
$this->addProductOrdersFromSubscription($order, $subscription); | |||
$this->saveUpdate($order); | |||
$this->update($order); | |||
} | |||
public function initOrderBaseFromSubscription(Order $order, Subscription $subscription): void |
@@ -147,4 +147,19 @@ class PointSaleRepository extends AbstractRepository | |||
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; | |||
} | |||
} |
@@ -4,7 +4,7 @@ namespace common\logic\Subscription\Subscription\Event; | |||
use common\logic\Distribution\Distribution\Event\DistributionActiveEvent; | |||
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; | |||
class DistributionObserver extends Observer | |||
@@ -19,8 +19,8 @@ class DistributionObserver extends Observer | |||
public static function onDistributionActive(DistributionActiveEvent $event): void | |||
{ | |||
$distribution = $event->distribution; | |||
$orderModule = OrderModule::getInstance(); | |||
$subscriptionModule = SubscriptionModule::getInstance(); | |||
$orderModule->createAllOrdersFromSubscriptions($distribution->date); | |||
$subscriptionModule->getOrderManager()->createAllOrdersFromSubscriptions($distribution->date); | |||
} | |||
} |
@@ -4,6 +4,7 @@ namespace common\logic\Subscription\Subscription\Module; | |||
use common\logic\AbstractModule; | |||
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\SubscriptionDefinition; | |||
use common\logic\Subscription\Subscription\Service\SubscriptionSolver; | |||
@@ -13,6 +14,7 @@ use common\logic\Subscription\Subscription\Service\SubscriptionSolver; | |||
* @mixin SubscriptionSolver | |||
* @mixin SubscriptionRepository | |||
* @mixin SubscriptionBuilder | |||
* @mixin OrderManager | |||
*/ | |||
class SubscriptionModule extends AbstractModule | |||
{ | |||
@@ -22,7 +24,8 @@ class SubscriptionModule extends AbstractModule | |||
SubscriptionDefinition::class, | |||
SubscriptionSolver::class, | |||
SubscriptionRepository::class, | |||
SubscriptionBuilder::class | |||
SubscriptionBuilder::class, | |||
OrderManager::class, | |||
]; | |||
} | |||
@@ -45,4 +48,9 @@ class SubscriptionModule extends AbstractModule | |||
{ | |||
return SubscriptionBuilder::getInstance(); | |||
} | |||
public function getOrderManager(): OrderManager | |||
{ | |||
return OrderManager::getInstance(); | |||
} | |||
} |
@@ -0,0 +1,117 @@ | |||
<?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; | |||
} | |||
} |
@@ -109,8 +109,9 @@ class SubscriptionController extends ProducerBaseController | |||
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) { | |||
$this->setFlash('success', 'Abonnement modifié'); | |||
@@ -229,7 +230,8 @@ class SubscriptionController extends ProducerBaseController | |||
$subscriptionModule->deleteSubscription($subscription); | |||
// @TODO : gérer via événements | |||
$this->getOrderModule()->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
$this->getSubscriptionModule()->getOrderManager() | |||
->deleteOrdersIncomingDistributionsFromSubscription($subscription); | |||
$this->setFlash('success', 'Abonnement supprimé'); | |||