瀏覽代碼

[Administration] Refactoring DistributionController #1342

feature/souke
Guillaume Bourgeois 1 年之前
父節點
當前提交
6a2c56922e
共有 4 個檔案被更改,包括 150 行新增92 行删除
  1. +30
    -90
      backend/controllers/DistributionController.php
  2. +12
    -0
      common/logic/Distribution/Distribution/Service/DistributionSolver.php
  3. +10
    -2
      common/logic/Order/Order/Module/OrderModule.php
  4. +98
    -0
      common/logic/Order/Order/Service/OrderDocumentManager.php

+ 30
- 90
backend/controllers/DistributionController.php 查看文件

@@ -39,23 +39,19 @@
namespace backend\controllers;

use common\helpers\Ajax;
use common\helpers\CSV;
use common\helpers\GlobalParam;
use common\helpers\MeanPayment;
use common\helpers\Password;
use common\helpers\Price;
use common\helpers\Tiller;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\Document\Model\Document;
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 common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer;
use DateTime;
use kartik\mpdf\Pdf;
use yii\base\Exception;
use yii\filters\AccessControl;

class DistributionController extends BackendController
@@ -85,27 +81,22 @@ class DistributionController extends BackendController
];
}

public function actionIndex($date = '', $idOrderUpdate = 0)
public function actionIndex(string $date = '', int $idOrderUpdate = 0)
{
$this->checkProductsPointsSale();

$orderModule = $this->getOrderModule();

$format = 'Y-m-d';
$theDate = '';
$dateObject = DateTime::createFromFormat($format, $date);

if ($dateObject && $dateObject->format($format) === $date) {
$theDate = $date;
if(!$this->getDistributionModule()->getSolver()->validateDistributionDate($date)) {
$date = '';
}

$orderUpdate = null;
if ($idOrderUpdate) {
$orderUpdate = $orderModule->findOneOrderById($idOrderUpdate);
if($idOrderUpdate) {
$orderUpdate = $this->getOrderModule()->getRepository()
->findOneOrderById($idOrderUpdate);
}

return $this->render('index', [
'date' => $theDate,
'date' => $date,
'orderUpdate' => $orderUpdate
]);
}
@@ -120,16 +111,14 @@ class DistributionController extends BackendController
$userModule = $this->getUserModule();

$producer = $this->getProducerCurrent();
$format = 'Y-m-d';
$dateObject = DateTime::createFromFormat($format, $date);

$dateObject = DateTime::createFromFormat('Y-m-d', $date);
$json = ['distribution' => [], 'products' => []];
$json['means_payment'] = MeanPayment::getAll();
$json['producer'] = $this->buildAjaxInfosResponseProducer($producer);
$json['distributions'] = $this->buildAjaxInfosResponseDistributions($dateObject);
$json['units'] = Product::$unitsArray;

if ($dateObject && $dateObject->format($format) === $date) {
if ($distributionModule->getSolver()->validateDistributionDate($date)) {
$distribution = $distributionModule->createDistributionIfNotExist($date);
$ordersArray = $orderModule->findOrdersByDistribution($distribution, false);
$ordersArrayObject = $ordersArray;
@@ -927,45 +916,18 @@ class DistributionController extends BackendController
return $return;
}

public function actionAjaxGenerateDeliveryNote(int $idOrder)
{
$this->generateDeliveryNote($idOrder);
return Ajax::responseSuccess('Bon de livraison généré');
}

public function actionAjaxGenerateDeliveryNotePointSale($idOrders)
public function actionAjaxGenerateDeliveryNotePointSale(string $idOrders)
{
$orderModule = $this->getOrderModule();
$deliveryNoteModule = $this->getDeliveryNoteModule();

if (strlen($idOrders)) {
$idOrders = json_decode($idOrders, true);

if (is_array($idOrders) && count($idOrders) > 0) {
reset($idOrders);
$firstOrder = $orderModule->findOneOrderById((int)$idOrders[key($idOrders)]);
$ordersArray = Order::searchAll(['id' => $idOrders,]);
$deliveryNote = $deliveryNoteModule->getOneDeliveryNoteExistingFromOrders($ordersArray);
$isUpdate = (bool) $deliveryNote;

if ($deliveryNote && $deliveryNoteModule->isStatusValid($deliveryNote)) {
return Ajax::responseError('Vous ne pouvez pas modifier un bon de livraison déjà validé.');
}

if(!$deliveryNote && $firstOrder && !$firstOrder->pointSale->id_user) {
return Ajax::responseError("Vous devez définir un contact de facturation pour ce point de vente.");
}

if ($firstOrder) {
if (!$deliveryNote) {
$deliveryNoteModule->createDeliveryNoteForPointSale(
$firstOrder->pointSale,
$firstOrder->distribution,
$idOrders
);
if(is_array($idOrders)) {
try {
if($this->getOrderModule()->getDocumentManager()->generateDeliveryNoteForPointSale($idOrders)) {
return Ajax::responseSuccess("Bon de livraison généré");
}

return Ajax::responseSuccess('Bon de livraison ' . ($isUpdate ? 'modifié' : 'généré'));
}
catch(Exception $exception) {
return Ajax::responseError($exception->getMessage());
}
}
}
@@ -973,52 +935,30 @@ class DistributionController extends BackendController
return Ajax::responseError('Une erreur est survenue lors de la génération du bon de livraison.');
}

public function actionAjaxGenerateDeliveryNoteEachUser($idOrders)
public function actionAjaxGenerateDeliveryNote(int $idOrder)
{
$orderModule = $this->getOrderModule();
$order = $orderModule->getRepository()->findOneOrderById($idOrder);
$orderModule->getDocumentManager()->generateDeliveryNoteForUser($order);
return Ajax::responseSuccess('Bon de livraison généré');
}

public function actionAjaxGenerateDeliveryNoteEachUser(string $idOrders)
{
if (strlen($idOrders)) {
$idOrders = json_decode($idOrders, true);

if (is_array($idOrders) && count($idOrders) > 0) {
foreach ($idOrders as $idOrder) {
$this->generateDeliveryNote($idOrder);
}
}

$this->getOrderModule()->getDocumentManager()->generateDeliveryNotesForUser($idOrders);
return Ajax::responseSuccess('Bon(s) de livraison généré(s)');
}

return Ajax::responseError('Une erreur est survenue lors de la génération du bon de livraison.');
}

public function generateDeliveryNote(int $idOrder)
{
$orderModule = $this->getOrderModule();
$deliveryNoteModule = $this->getDeliveryNoteModule();

$order = $orderModule->findOneOrderById($idOrder);
if ($orderModule->isOrderFromProducer($order) && $order->id_user) {
$deliveryNoteModule->createDeliveryNoteForOrder($order);
}
}

public function actionAjaxValidateDeliveryNotes($idOrders)
public function actionAjaxValidateDeliveryNotes(string $idOrders)
{
$orderModule = $this->getOrderModule();
$deliveryNoteModule = $this->getDeliveryNoteModule();

if (strlen($idOrders)) {
$idOrders = json_decode($idOrders, true);
if (is_array($idOrders) && count($idOrders) > 0) {
foreach ($idOrders as $idOrder) {
$order = $orderModule->findOneOrderById($idOrder);
if ($orderModule->isOrderFromProducer($order)) {
$deliveryNote = $deliveryNoteModule->findOneDeliveryNoteById((int) $order->id_delivery_note);
if($deliveryNote) {
$deliveryNoteModule->validateDocument($deliveryNote);
}
}
}

if($this->getOrderModule()->getDocumentManager()->validateDeliveryNotes($idOrders)) {
return Ajax::responseSuccess('Bon(s) de livraison validé(s)');
}
}

+ 12
- 0
common/logic/Distribution/Distribution/Service/DistributionSolver.php 查看文件

@@ -11,6 +11,18 @@ use common\logic\SolverInterface;

class DistributionSolver extends AbstractService implements SolverInterface
{
public function validateDistributionDate(string $date = null): bool
{
$format = 'Y-m-d';
$dateObject = \DateTime::createFromFormat($format, $date);

if ($dateObject && $dateObject->format($format) === $date) {
return true;
}

return false;
}

public function getDistributionDayAsString(Distribution $distribution): string
{
$dayDistribution = date('N', strtotime($distribution->date));

+ 10
- 2
common/logic/Order/Order/Module/OrderModule.php 查看文件

@@ -6,6 +6,7 @@ use common\logic\AbstractModule;
use common\logic\Order\Order\Repository\OrderRepository;
use common\logic\Order\Order\Service\OrderBuilder;
use common\logic\Order\Order\Service\OrderDefinition;
use common\logic\Order\Order\Service\OrderDocumentManager;
use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Order\Order\Service\OrderManager;

@@ -14,6 +15,7 @@ use common\logic\Order\Order\Service\OrderManager;
* @mixin OrderSolver
* @mixin OrderRepository
* @mixin OrderBuilder
* @mixin OrderDocumentManager
*/
class OrderModule extends AbstractModule
{
@@ -24,7 +26,8 @@ class OrderModule extends AbstractModule
OrderSolver::class,
OrderRepository::class,
OrderBuilder::class,
OrderManager::class
OrderManager::class,
OrderDocumentManager::class,
];
}

@@ -48,8 +51,13 @@ class OrderModule extends AbstractModule
return OrderBuilder::getInstance();
}

public function getUtils(): OrderManager
public function getManager(): OrderManager
{
return OrderManager::getInstance();
}

public function getDocumentManager(): OrderDocumentManager
{
return OrderDocumentManager::getInstance();
}
}

+ 98
- 0
common/logic/Order/Order/Service/OrderDocumentManager.php 查看文件

@@ -0,0 +1,98 @@
<?php

namespace common\logic\Order\Order\Service;

use common\logic\AbstractManager;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository;
use common\logic\Document\DeliveryNote\Service\DeliveryNoteBuilder;
use common\logic\Document\Document\Service\DocumentSolver;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository;
use yii\base\Exception;

class OrderDocumentManager extends AbstractManager
{
protected OrderSolver $orderSolver;
protected OrderRepository $orderRepository;
protected DocumentSolver $documentSolver;
protected DeliveryNoteRepository $deliveryNoteRepository;
protected DeliveryNoteBuilder $deliveryNoteBuilder;

public function loadDependencies(): void
{
$this->orderSolver = $this->loadService(OrderSolver::class);
$this->orderRepository = $this->loadService(OrderRepository::class);
$this->documentSolver = $this->loadService(DocumentSolver::class);
$this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
$this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class);
}

public function generateDeliveryNoteForPointSale(array $idOrders = []): ?DeliveryNote
{
reset($idOrders);
$firstOrder = $this->orderRepository->findOneOrderById((int)$idOrders[key($idOrders)]);
$ordersArray = Order::searchAll(['id' => $idOrders,]);
$deliveryNote = $this->deliveryNoteRepository->getOneDeliveryNoteExistingFromOrders($ordersArray);

if ($deliveryNote && $this->documentSolver->isStatusValid($deliveryNote)) {
throw new Exception("Vous ne pouvez pas modifier un bon de livraison déjà validé.");
}

if(!$deliveryNote && $firstOrder && !$firstOrder->pointSale->id_user) {
throw new Exception("Vous devez définir un contact de facturation pour ce point de vente.");
}

if ($firstOrder) {
if (!$deliveryNote) {
$deliveryNote = $this->deliveryNoteBuilder->createDeliveryNoteForPointSale(
$firstOrder->pointSale,
$firstOrder->distribution,
$idOrders
);
}

return $deliveryNote;
}

return null;
}

public function generateDeliveryNotesForUser(array $idOrders = []): void
{
foreach($idOrders as $idOrder) {
$order = $this->orderRepository->findOneOrderById($idOrder);
$this->generateDeliveryNoteForUser($order);
}
}

public function generateDeliveryNoteForUser(Order $order = null): ?DeliveryNote
{
if ($order && $this->orderSolver->isOrderFromProducer($order) && $order->id_user) {
return $this->deliveryNoteBuilder->createDeliveryNoteForOrder($order);
}

return null;
}

public function validateDeliveryNotes(array $idOrders = []): bool
{
if (count($idOrders) > 0) {
$oneDeliveryNoteValidated = false;
foreach ($idOrders as $idOrder) {
$order = $this->orderRepository->findOneOrderById($idOrder);
if ($this->orderSolver->isOrderFromProducer($order)) {
$deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteById((int) $order->id_delivery_note);
if($deliveryNote) {
$this->deliveryNoteBuilder->validateDocument($deliveryNote);
$oneDeliveryNoteValidated = true;
}
}
}

return $oneDeliveryNoteValidated;
}

return false;
}
}

Loading…
取消
儲存