Переглянути джерело

[Administration] Distributions : refactoring gestion des bons de livraisons #1195

feature/souke
Guillaume Bourgeois 1 рік тому
джерело
коміт
a36c39d547
9 змінених файлів з 180 додано та 109 видалено
  1. +19
    -93
      backend/controllers/DistributionController.php
  2. +4
    -0
      common/config/main.php
  3. +12
    -0
      common/logic/Document/DeliveryNote/Event/DeliveryNoteCreateEvent.php
  4. +2
    -0
      common/logic/Document/DeliveryNote/Model/DeliveryNote.php
  5. +26
    -0
      common/logic/Document/DeliveryNote/Repository/DeliveryNoteRepository.php
  6. +63
    -6
      common/logic/Document/DeliveryNote/Service/DeliveryNoteBuilder.php
  7. +24
    -0
      common/logic/Order/Order/Event/DeliveryNoteObserver.php
  8. +28
    -8
      common/logic/Order/Order/Service/OrderBuilder.php
  9. +2
    -2
      common/logic/Order/Order/Service/OrderSolver.php

+ 19
- 93
backend/controllers/DistributionController.php Переглянути файл

@@ -877,59 +877,27 @@ class DistributionController extends BackendController
$idOrders = json_decode($idOrders, true);

if (is_array($idOrders) && count($idOrders) > 0) {
// récupération première commande pour obtenir des infos
reset($idOrders);
$firstOrder = Order::searchOne([
'id' => (int)$idOrders[key($idOrders)]
]);

// deliveryNote existant
$deliveryNote = null;
$isUpdate = false;
$i = 0;
$ordersArray = Order::searchAll([
'id' => $idOrders,
]);
do {
$order = $ordersArray[$i];
if ($order->distribution->id_producer == GlobalParam::getCurrentProducerId() && $order->id_delivery_note > 0) {
$deliveryNote = DeliveryNote::searchOne([
'id' => $order->id_delivery_note
]);
$isUpdate = true;
}
$i++;
} while ($deliveryNote == null && isset($ordersArray[$i]));
$firstOrder = $orderManager->findOneOrderById((int)$idOrders[key($idOrders)]);
$ordersArray = Order::searchAll(['id' => $idOrders,]);
$deliveryNote = $deliveryNoteManager->getOneDeliveryNoteExistingFromOrders($ordersArray);
$isUpdate = (bool) $deliveryNote;

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

if ($firstOrder) {
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) {
if ($firstOrder->pointSale->id_user) {
$deliveryNote = $deliveryNoteManager->createDeliveryNoteForPointSale(
$firstOrder->pointSale,
$firstOrder->distribution
);
} else {
return Ajax::responseError("Vous devez définir un contact de facturation pour ce point de vente.");
}
} else {
// réinitialisation des order.id_delivery_note
Order::updateAll([
'id_delivery_note' => null
], [
'id_delivery_note' => $deliveryNote->id
]);
}

foreach ($idOrders as $idOrder) {
$order = $orderManager->findOneOrderById((int)$idOrder);
if($order) {
$orderManager->assignDeliveryNote($order, $deliveryNote);
}
$deliveryNoteManager->createDeliveryNoteForPointSale(
$firstOrder->pointSale,
$firstOrder->distribution,
$idOrders
);
}

return Ajax::responseSuccess('Bon de livraison ' . ($isUpdate ? 'modifié' : 'généré'));
@@ -959,53 +927,12 @@ class DistributionController extends BackendController

public function generateDeliveryNote(int $idOrder)
{
$userManager = $this->getUserManager();
$userProducerManager = $this->getUserProducerManager();
$orderManager = $this->getOrderManager();
$deliveryNoteManager = $this->getDeliveryNoteManager();
$order = $orderManager->findOneOrderById($idOrder);

if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId() && $order->id_user) {

$deliveryNote = null;
$idDeliveryNote = $order->id_delivery_note;
if ($idDeliveryNote) {
$deliveryNote = $deliveryNoteManager->findOneDeliveryNoteById($idDeliveryNote);
}

// on regénére le document si c'est un brouillon
if ($deliveryNote && $deliveryNoteManager->isStatusDraft($deliveryNote)) {
$deliveryNote->delete();
$deliveryNote = null;
}

if (!$deliveryNote) {
$deliveryNote = $deliveryNoteManager->instanciateDeliveryNote();
$deliveryNoteManager->initTaxCalculationMethod($deliveryNote);
$deliveryNote->id_producer = GlobalParam::getCurrentProducerId();
$deliveryNote->id_user = $order->id_user;
$deliveryNote->name = 'Bon de livraison ' . $orderManager->getOrderUsername($order) . ' (' . date(
'd/m/Y',
strtotime(
$order->distribution->date
)
) . ')';
$deliveryNote->address = $userManager->getFullAddress($order->user);
$deliveryNote->save();
}

if ($deliveryNote) {
$order->id_delivery_note = $deliveryNote->id;
$order->save();

$user = $userManager->findOneUserById($deliveryNote->id_user);
$userProducer = $userProducerManager->findOneUserProducer($user);
$orderManager->updateOrderInvoicePrices($order, [
'user' => $user,
'user_producer' => $userProducer,
'point_sale' => $order->pointSale
]);
}
$order = $orderManager->findOneOrderById($idOrder);
if ($orderManager->isOrderFromProducer($order) && $order->id_user) {
$deliveryNoteManager->createDeliveryNoteForOrder($order);
}
}

@@ -1016,12 +943,11 @@ class DistributionController extends BackendController

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

if (is_array($idOrders) && count($idOrders) > 0) {
foreach ($idOrders as $idOrder) {
$order = $orderManager->findOneOrderById($idOrder);
if ($order && $orderManager->isOrderFromProducer($order)) {
$deliveryNote = $deliveryNoteManager->findOneDeliveryNoteById((int)$order->id_delivery_note);
if ($orderManager->isOrderFromProducer($order)) {
$deliveryNote = $deliveryNoteManager->findOneDeliveryNoteById((int) $order->id_delivery_note);
if($deliveryNote) {
$deliveryNoteManager->validateDocument($deliveryNote);
}

+ 4
- 0
common/config/main.php Переглянути файл

@@ -38,6 +38,7 @@

use common\components\BusinessLogic;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Ticket\Ticket\Model\Ticket;
use common\logic\User\CreditHistory\Model\CreditHistory;

@@ -141,6 +142,9 @@ return [
Distribution::class => [
common\logic\Subscription\Subscription\Event\DistributionObserver::class
],
DeliveryNote::class => [
common\logic\Order\Order\Event\DeliveryNoteObserver::class
],
Ticket::class => [
common\logic\User\User\Event\TicketObserver::class,
],

+ 12
- 0
common/logic/Document/DeliveryNote/Event/DeliveryNoteCreateEvent.php Переглянути файл

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

namespace common\logic\Document\DeliveryNote\Event;

use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use yii\base\Event;

class DeliveryNoteCreateEvent extends Event
{
public DeliveryNote $deliveryNote;
public array $idOrders = [];
}

+ 2
- 0
common/logic/Document/DeliveryNote/Model/DeliveryNote.php Переглянути файл

@@ -42,6 +42,8 @@ use common\logic\Document\Document\Model\Document;

class DeliveryNote extends Document
{
const EVENT_CREATE = 'deliverynote.event.create';

/**
* @inheritdoc
*/

+ 26
- 0
common/logic/Document/DeliveryNote/Repository/DeliveryNoteRepository.php Переглянути файл

@@ -4,6 +4,7 @@ namespace common\logic\Document\DeliveryNote\Repository;

use common\logic\AbstractRepository;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Order\Order\Model\Order;

class DeliveryNoteRepository extends AbstractRepository
{
@@ -30,4 +31,29 @@ class DeliveryNoteRepository extends AbstractRepository
->filterById($id)
->findOne();
}

public function getOneDeliveryNoteExistingFromOrders(array $ordersArray): ?DeliveryNote
{
$i = 0;
$deliveryNote = null;
do {
$order = $ordersArray[$i];
if ($order->distribution->id_producer == $this->getProducerContextId() && $order->id_delivery_note > 0) {
$deliveryNote = $this->findOneDeliveryNoteById($order->id_delivery_note);
}
$i++;
} while ($deliveryNote == null && isset($ordersArray[$i]));

return $deliveryNote;
}

public function findOneDeliveryNoteByOrder(Order $order): ?DeliveryNote
{
$deliveryNoteExist = null;
if ($order->id_delivery_note) {
$deliveryNoteExist = $this->findOneDeliveryNoteById($order->id_delivery_note);
}

return $deliveryNoteExist;
}
}

+ 63
- 6
common/logic/Document/DeliveryNote/Service/DeliveryNoteBuilder.php Переглянути файл

@@ -3,8 +3,12 @@
namespace common\logic\Document\DeliveryNote\Service;

use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Document\DeliveryNote\Event\DeliveryNoteCreateEvent;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository;
use common\logic\Document\Document\Service\DocumentBuilder;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Service\OrderSolver;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\User\User\Repository\UserRepository;
use common\logic\User\User\Service\UserSolver;
@@ -14,46 +18,89 @@ class DeliveryNoteBuilder extends DocumentBuilder
{
protected UserSolver $userSolver;
protected UserRepository $userRepository;
protected DeliveryNoteRepository $deliveryNoteRepository;
protected OrderSolver $orderSolver;

public function loadDependencies(): void
{
parent::loadDependencies();
$this->userSolver = $this->loadService(UserSolver::class);
$this->userRepository = $this->loadService(UserRepository::class);
$this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class);
$this->orderSolver = $this->loadService(OrderSolver::class);
}

public function instanciateDeliveryNote(): DeliveryNote
{
$deliveryNote = new DeliveryNote();

$this->initDocumentProducer($deliveryNote);
$this->initTaxCalculationMethod($deliveryNote);

return $deliveryNote;
}

public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution): DeliveryNote
public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution, array $idOrders): DeliveryNote
{
$deliveryNote = $this->instanciateDeliveryNote();
$this->initDocumentProducer($deliveryNote);
$this->initTaxCalculationMethod($deliveryNote);
$this->initDeliveryNoteName($deliveryNote, $pointSale, $distribution);
$this->initDeliveryNoteNameByPointSale($deliveryNote, $distribution, $pointSale);
$this->initDeliveryNoteUserFromPointSale($deliveryNote, $pointSale);
$this->initDeliveryNoteAddress($deliveryNote);

$deliveryNote->save();

$this->triggerDeliveryNoteCreateEvent($deliveryNote, $idOrders);

return $deliveryNote;
}

public function createDeliveryNoteForOrder(Order $order): DeliveryNote
{
$deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteByOrder($order);
$deliveryNote = $this->deleteDeliveryNoteIfStatusDraft($deliveryNote);

if(!$deliveryNote) {
$deliveryNote = $this->instanciateDeliveryNote();
$this->initDeliveryNoteNameByOrder($deliveryNote, $order);
$this->initDeliveryNoteUserFromOrder($deliveryNote, $order);
$this->initDeliveryNoteAddress($deliveryNote);
$deliveryNote->save();

$this->triggerDeliveryNoteCreateEvent($deliveryNote, [$order->id]);
}

return $deliveryNote;
}

public function triggerDeliveryNoteCreateEvent(DeliveryNote $deliveryNote, array $idOrders)
{
$deliveryNoteCreateEvent = new DeliveryNoteCreateEvent();
$deliveryNoteCreateEvent->deliveryNote = $deliveryNote;
$deliveryNoteCreateEvent->idOrders = $idOrders;
$deliveryNote->trigger(DeliveryNote::EVENT_CREATE, $deliveryNoteCreateEvent);
}

public function initDeliveryNoteUserFromPointSale(DeliveryNote $deliveryNote, PointSale $pointSale)
{
$deliveryNote->id_user = $pointSale->id_user;
}

public function initDeliveryNoteName(DeliveryNote $deliveryNote, PointSale $pointSale, Distribution $distribution)
public function initDeliveryNoteUserFromOrder(DeliveryNote $deliveryNote, Order $order)
{
$deliveryNote->id_user = $order->id_user;
}

public function initDeliveryNoteNameByPointSale(DeliveryNote $deliveryNote, Distribution $distribution, PointSale $pointSale)
{
$deliveryNote->name = 'Bon de livraison ' . $pointSale->name .
' (' . date('d/m/Y', strtotime($distribution->date)) . ')';
}

public function initDeliveryNoteNameByOrder(DeliveryNote $deliveryNote, Order $order)
{
$deliveryNote->name = 'Bon de livraison ' . $this->orderSolver->getOrderUsername($order) .
' (' . date('d/m/Y', strtotime($order->distribution->date)) . ')';
}

public function initDeliveryNoteAddress(DeliveryNote $deliveryNote)
{
$user = $this->userRepository->findOneUserById($deliveryNote->id_user);
@@ -64,4 +111,14 @@ class DeliveryNoteBuilder extends DocumentBuilder
throw new ErrorException("L'utilisateur du bon de livraison n'est pas défini.");
}
}

public function deleteDeliveryNoteIfStatusDraft(DeliveryNote $deliveryNote = null): ?DeliveryNote
{
if ($deliveryNote && $this->documentSolver->isStatusDraft($deliveryNote)) {
$deliveryNote->delete();
$deliveryNote = null;
}

return $deliveryNote;
}
}

+ 24
- 0
common/logic/Order/Order/Event/DeliveryNoteObserver.php Переглянути файл

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

namespace common\logic\Order\Order\Event;

use common\logic\Document\DeliveryNote\Event\DeliveryNoteCreateEvent;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Order\Order\Wrapper\OrderManager;
use justcoded\yii2\eventlistener\observers\Observer;

class DeliveryNoteObserver extends Observer
{
public function events()
{
return [
DeliveryNote::EVENT_CREATE => 'onDeliveryNoteCreate'
];
}

public function onDeliveryNoteCreate(DeliveryNoteCreateEvent $event)
{
$orderManager = OrderManager::getInstance();
$orderManager->assignAllOrdersDeliveryNote($event->idOrders, $event->deliveryNote);
}
}

+ 28
- 8
common/logic/Order/Order/Service/OrderBuilder.php Переглянути файл

@@ -11,6 +11,7 @@ use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Distribution\Distribution\Repository\DistributionRepository;
use common\logic\Distribution\Distribution\Service\DistributionSolver;
use common\logic\Document\DeliveryNote\Model\DeliveryNote;
use common\logic\Document\DeliveryNote\Service\DeliveryNoteBuilder;
use common\logic\Document\Document\Model\Document;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository;
@@ -57,6 +58,7 @@ class OrderBuilder extends AbstractBuilder
protected ProductSolver $productSolver;
protected DistributionSolver $distributionSolver;
protected UserRepository $userRepository;
protected DeliveryNoteBuilder $deliveryNoteBuilder;

public function loadDependencies(): void
{
@@ -79,6 +81,7 @@ class OrderBuilder extends AbstractBuilder
$this->productSolver = $this->loadService(ProductSolver::class);
$this->distributionSolver = $this->loadService(DistributionSolver::class);
$this->userRepository = $this->loadService(UserRepository::class);
$this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class);
}

public function instanciateOrder(Distribution $distribution): Order
@@ -143,7 +146,7 @@ class OrderBuilder extends AbstractBuilder
foreach ($subscriptionArray as $subscription) {
if (!$this->subscriptionSolver->hasOrderAlreadyExist($subscription, $orderArray)) {
$order = $this->createOrderFromSubscription($subscription, $date, $force);
if($order) {
if ($order) {
$ordersSubscriptionsArray[] = $order;
}
}
@@ -202,7 +205,7 @@ class OrderBuilder extends AbstractBuilder
$countOrdersDeleted = 0;
if ($ordersArray && count($ordersArray)) {
foreach ($ordersArray as $order) {
if($this->distributionSolver->isDistributionAvailable($order->distribution)) {
if ($this->distributionSolver->isDistributionAvailable($order->distribution)) {
$theOrder = $this->orderRepository->findOneOrderById($order->id);
$this->initOrder($theOrder);

@@ -614,18 +617,26 @@ class OrderBuilder extends AbstractBuilder
return $order->save();
}

public function assignDeliveryNote(Order $order, DeliveryNote $deliveryNote)
public function assignAllOrdersDeliveryNote(array $idOrders, DeliveryNote $deliveryNote)
{
if ($order && $this->orderSolver->isOrderFromProducer($order)) {
$this->updateOrderDeliveryNote($order, $deliveryNote);
$this->unassignAllOrdersDeliveryNote($deliveryNote);

foreach ($idOrders as $idOrder) {
$order = $this->orderRepository->findOneOrderById((int)$idOrder);
if ($order) {
$this->assignOrderDeliveryNote($order, $deliveryNote);
}
}
}

$order = $this->orderRepository->findOneOrderById($order->id);
if ($order) {
public function assignOrderDeliveryNote(Order $order, DeliveryNote $deliveryNote)
{
if ($this->orderSolver->isOrderFromProducer($order)) {
$this->updateOrderDeliveryNote($order, $deliveryNote);

$order = $this->orderRepository->findOneOrderById($order->id);
$user = $this->userRepository->findOneUserById($deliveryNote->id_user);
$userProducer = $this->userProducerRepository->findOneUserProducer($user);

$this->updateOrderInvoicePrices($order,
[
'user' => $user,
@@ -634,4 +645,13 @@ class OrderBuilder extends AbstractBuilder
]);
}
}

public function unassignAllOrdersDeliveryNote(DeliveryNote $deliveryNote)
{
Order::updateAll([
'id_delivery_note' => null
], [
'id_delivery_note' => $deliveryNote->id
]);
}
}

+ 2
- 2
common/logic/Order/Order/Service/OrderSolver.php Переглянути файл

@@ -371,9 +371,9 @@ class OrderSolver extends AbstractService implements SolverInterface
return false;
}

public function isOrderFromProducer(Order $order): bool
public function isOrderFromProducer(Order $order = null): bool
{
return $order->distribution->id_producer == $this->getProducerContextId();
return $order && $order->distribution->id_producer == $this->getProducerContextId();
}

/**

Завантаження…
Відмінити
Зберегти