@@ -38,6 +38,7 @@ | |||
namespace backend\controllers; | |||
use common\helpers\Ajax; | |||
use common\helpers\CSV; | |||
use common\helpers\GlobalParam; | |||
use common\helpers\MeanPayment; | |||
@@ -876,8 +877,6 @@ class DistributionController extends BackendController | |||
{ | |||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | |||
$userManager = $this->getUserManager(); | |||
$userProducerManager = $this->getUserProducerManager(); | |||
$orderManager = $this->getOrderManager(); | |||
$deliveryNoteManager = $this->getDeliveryNoteManager(); | |||
@@ -910,61 +909,20 @@ class DistributionController extends BackendController | |||
} while ($deliveryNote == null && isset($ordersArray[$i])); | |||
if ($deliveryNote && $deliveryNote->status == Document::STATUS_VALID) { | |||
return [ | |||
'return' => 'error', | |||
'alert' => [ | |||
'type' => 'danger', | |||
'message' => 'Vous ne pouvez pas modifier un bon de livraison déjà validé.' | |||
] | |||
]; | |||
return Ajax::responseError('Vous ne pouvez pas modifier un bon de livraison déjà validé.'); | |||
} | |||
if ($firstOrder) { | |||
// génération du BL | |||
if (!$deliveryNote) { | |||
$deliveryNote = new DeliveryNote; | |||
$deliveryNoteManager->initTaxCalculationMethod($deliveryNote); | |||
$deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date( | |||
'd/m/Y', | |||
strtotime( | |||
$firstOrder->distribution->date | |||
) | |||
) . ')'; | |||
$deliveryNote->id_producer = GlobalParam::getCurrentProducerId(); | |||
if (!$deliveryNote) { | |||
if ($firstOrder->pointSale->id_user) { | |||
$deliveryNote->id_user = $firstOrder->pointSale->id_user; | |||
$user = $userManager->findOneUserById($deliveryNote->id_user); | |||
$userProducer = $userProducerManager->findOneUserProducer($user); | |||
$deliveryNote = $deliveryNoteManager->createDeliveryNoteForPointSale( | |||
$firstOrder->pointSale, | |||
$firstOrder->distribution | |||
); | |||
} else { | |||
$user = new User(); | |||
$user->type = User::TYPE_LEGAL_PERSON; | |||
$user->name_legal_person = $firstOrder->pointSale->name; | |||
$user->address = $firstOrder->pointSale->address; | |||
$user->id_producer = 0; | |||
$userManager->setPassword($user, Password::generate()); | |||
$userManager->generateAuthKey($user); | |||
$user->email = ''; | |||
if (!strlen($user->email)) { | |||
$user->username = 'inconnu@opendistrib.net'; | |||
} | |||
$user->save(); | |||
$userProducer = new UserProducer(); | |||
$userProducer->id_user = $user->id; | |||
$userProducer->id_producer = GlobalParam::getCurrentProducerId(); | |||
$userProducer->credit = 0; | |||
$userProducer->active = 1; | |||
$userProducer->save(); | |||
$firstOrder->pointSale->id_user = $user->id; | |||
$firstOrder->pointSale->save(); | |||
$deliveryNote->id_user = $user->id; | |||
return Ajax::responseError("Vous devez définir un contact de facturation pour ce point de vente."); | |||
} | |||
$deliveryNote->address = $userManager->getFullAddress($user); | |||
$deliveryNote->save(); | |||
} else { | |||
// réinitialisation des order.id_delivery_note | |||
Order::updateAll([ | |||
@@ -974,49 +932,19 @@ class DistributionController extends BackendController | |||
]); | |||
} | |||
if (!isset($user) || !$user) { | |||
$user = $userManager->findOneUserById($deliveryNote->id_user); | |||
$userProducer = $userProducerManager->findOneUserProducer($user); | |||
} | |||
// affectation du BL aux commandes | |||
foreach ($idOrders as $idOrder) { | |||
$order = $orderManager->findOneOrderById((int)$idOrder); | |||
if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) { | |||
$order->id_delivery_note = $deliveryNote->id; | |||
$order->save(); | |||
} | |||
// init invoice price | |||
$order = $orderManager->findOneOrderById((int)$idOrder); | |||
if ($order) { | |||
$orderManager->updateOrderInvoicePrices($order, | |||
[ | |||
'user' => $user, | |||
'user_producer' => $userProducer, | |||
'point_sale' => $firstOrder->pointSale | |||
]); | |||
if($order) { | |||
$orderManager->assignDeliveryNote($order, $deliveryNote); | |||
} | |||
} | |||
return [ | |||
'return' => 'success', | |||
'alert' => [ | |||
'type' => 'success', | |||
'message' => 'Bon de livraison ' . ($isUpdate ? 'modifié' : 'généré') | |||
] | |||
]; | |||
return Ajax::responseSuccess('Bon de livraison ' . ($isUpdate ? 'modifié' : 'généré')); | |||
} | |||
} | |||
} | |||
return [ | |||
'return' => 'error', | |||
'alert' => [ | |||
'type' => 'danger', | |||
'message' => 'Une erreur est survenue lors de la génération du bon de livraison.' | |||
] | |||
]; | |||
return Ajax::responseError('Une erreur est survenue lors de la génération du bon de livraison.'); | |||
} | |||
public function actionAjaxGenerateDeliveryNoteEachUser($idOrders) |
@@ -0,0 +1,43 @@ | |||
<?php | |||
namespace common\helpers; | |||
class Ajax | |||
{ | |||
const RESPONSE_TYPE_ERROR = 'error'; | |||
const RESPONSE_TYPE_SUCCESS = 'success'; | |||
public static function responseSuccess(string $message): array | |||
{ | |||
return self::response(self::RESPONSE_TYPE_SUCCESS, $message); | |||
} | |||
public static function responseError(string $message): array | |||
{ | |||
return self::response(self::RESPONSE_TYPE_ERROR, $message); | |||
} | |||
private static function response(string $responseType, string $message): array | |||
{ | |||
return [ | |||
'return' => $responseType, | |||
'alert' => [ | |||
'type' => self::getAlertClass($responseType), | |||
'message' => 'Vous ne pouvez pas modifier un bon de livraison déjà validé.' | |||
] | |||
]; | |||
} | |||
private static function getAlertClass(string $responseType) | |||
{ | |||
$class = ''; | |||
if($responseType == self::RESPONSE_TYPE_SUCCESS) { | |||
$class = 'success'; | |||
} | |||
elseif($responseType == self::RESPONSE_TYPE_ERROR) { | |||
$class = 'danger'; | |||
} | |||
return $class; | |||
} | |||
} |
@@ -2,15 +2,66 @@ | |||
namespace common\logic\Document\DeliveryNote\Service; | |||
use common\logic\Distribution\Distribution\Model\Distribution; | |||
use common\logic\Document\DeliveryNote\Model\DeliveryNote; | |||
use common\logic\Document\Document\Service\DocumentBuilder; | |||
use common\logic\PointSale\PointSale\Model\PointSale; | |||
use common\logic\User\User\Repository\UserRepository; | |||
use common\logic\User\User\Service\UserSolver; | |||
use yii\base\ErrorException; | |||
class DeliveryNoteBuilder extends DocumentBuilder | |||
{ | |||
protected UserSolver $userSolver; | |||
protected UserRepository $userRepository; | |||
public function loadDependencies(): void | |||
{ | |||
parent::loadDependencies(); | |||
$this->userSolver = $this->loadService(UserSolver::class); | |||
$this->userRepository = $this->loadService(UserRepository::class); | |||
} | |||
public function instanciateDeliveryNote(): DeliveryNote | |||
{ | |||
$deliveryNote = new DeliveryNote(); | |||
return $deliveryNote; | |||
} | |||
public function createDeliveryNoteForPointSale(PointSale $pointSale, Distribution $distribution): DeliveryNote | |||
{ | |||
$deliveryNote = $this->instanciateDeliveryNote(); | |||
$this->initDocumentProducer($deliveryNote); | |||
$this->initTaxCalculationMethod($deliveryNote); | |||
$this->initDeliveryNoteName($deliveryNote, $pointSale, $distribution); | |||
$this->initDeliveryNoteUserFromPointSale($deliveryNote, $pointSale); | |||
$this->initDeliveryNoteAddress($deliveryNote); | |||
$deliveryNote->save(); | |||
return $deliveryNote; | |||
} | |||
public function initDeliveryNoteUserFromPointSale(DeliveryNote $deliveryNote, PointSale $pointSale) | |||
{ | |||
$deliveryNote->id_user = $pointSale->id_user; | |||
} | |||
public function initDeliveryNoteName(DeliveryNote $deliveryNote, PointSale $pointSale, Distribution $distribution) | |||
{ | |||
$deliveryNote->name = 'Bon de livraison ' . $pointSale->name . | |||
' (' . date('d/m/Y', strtotime($distribution->date)) . ')'; | |||
} | |||
public function initDeliveryNoteAddress(DeliveryNote $deliveryNote) | |||
{ | |||
$user = $this->userRepository->findOneUserById($deliveryNote->id_user); | |||
if($user) { | |||
$deliveryNote->address = $this->userSolver->getFullAddress($user); | |||
} | |||
else { | |||
throw new ErrorException("L'utilisateur du bon de livraison n'est pas défini."); | |||
} | |||
} | |||
} |
@@ -21,6 +21,11 @@ class DocumentBuilder extends AbstractBuilder | |||
$this->producerRepository = $this->loadService(ProducerRepository::class); | |||
} | |||
public function initDocumentProducer(Document $document): void | |||
{ | |||
$document->id_producer = $this->getProducerContextId(); | |||
} | |||
public function generateReference(DocumentInterface $document): void | |||
{ | |||
$class = $this->documentSolver->getClass($document); |
@@ -10,6 +10,7 @@ use common\logic\Config\TaxRate\Model\TaxRate; | |||
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\Document\Model\Document; | |||
use common\logic\Order\Order\Model\Order; | |||
use common\logic\Order\Order\Repository\OrderRepository; | |||
@@ -30,6 +31,7 @@ use common\logic\Subscription\Subscription\Service\SubscriptionSolver; | |||
use common\logic\User\CreditHistory\Model\CreditHistory; | |||
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | |||
use common\logic\User\CreditHistory\Repository\CreditHistoryRepository; | |||
use common\logic\User\User\Repository\UserRepository; | |||
use common\logic\User\User\Service\UserSolver; | |||
use common\logic\User\UserProducer\Repository\UserProducerRepository; | |||
use yii\web\NotFoundHttpException; | |||
@@ -54,6 +56,7 @@ class OrderBuilder extends AbstractBuilder | |||
protected SubscriptionSolver $subscriptionSolver; | |||
protected ProductSolver $productSolver; | |||
protected DistributionSolver $distributionSolver; | |||
protected UserRepository $userRepository; | |||
public function loadDependencies(): void | |||
{ | |||
@@ -75,6 +78,7 @@ class OrderBuilder extends AbstractBuilder | |||
$this->subscriptionSolver = $this->loadService(SubscriptionSolver::class); | |||
$this->productSolver = $this->loadService(ProductSolver::class); | |||
$this->distributionSolver = $this->loadService(DistributionSolver::class); | |||
$this->userRepository = $this->loadService(UserRepository::class); | |||
} | |||
public function instanciateOrder(Distribution $distribution): Order | |||
@@ -601,4 +605,30 @@ class OrderBuilder extends AbstractBuilder | |||
} | |||
} | |||
public function updateOrderDeliveryNote(Order $order, DeliveryNote $deliveryNote): bool | |||
{ | |||
$order->id_delivery_note = $deliveryNote->id; | |||
return $order->save(); | |||
} | |||
public function assignDeliveryNote(Order $order, DeliveryNote $deliveryNote) | |||
{ | |||
if ($order && $this->orderSolver->isOrderFromProducer($order)) { | |||
$this->updateOrderDeliveryNote($order, $deliveryNote); | |||
} | |||
$order = $this->orderRepository->findOneOrderById($order->id); | |||
if ($order) { | |||
$user = $this->userRepository->findOneUserById($deliveryNote->id_user); | |||
$userProducer = $this->userProducerRepository->findOneUserProducer($user); | |||
$this->updateOrderInvoicePrices($order, | |||
[ | |||
'user' => $user, | |||
'user_producer' => $userProducer, | |||
'point_sale' => $order->pointSale | |||
]); | |||
} | |||
} | |||
} |
@@ -6,6 +6,7 @@ use common\helpers\Price; | |||
use common\logic\AbstractService; | |||
use common\logic\Document\Document\Service\DocumentSolver; | |||
use common\logic\Order\Order\Model\Order; | |||
use common\logic\Producer\Producer\Model\Producer; | |||
use common\logic\Product\Product\Model\Product; | |||
use common\logic\SolverInterface; | |||
use common\logic\User\CreditHistory\Model\CreditHistory; | |||
@@ -361,7 +362,6 @@ class OrderSolver extends AbstractService implements SolverInterface | |||
} | |||
} | |||
// belongsToUser | |||
public function isOrderBelongsToUser(Order $order, User $user = null): bool | |||
{ | |||
if($user) { | |||
@@ -371,6 +371,11 @@ class OrderSolver extends AbstractService implements SolverInterface | |||
return false; | |||
} | |||
public function isOrderFromProducer(Order $order): bool | |||
{ | |||
return $order->distribution->id_producer == $this->getProducerContextId(); | |||
} | |||
/** | |||
* Retourne les informations relatives à la commande au format JSON. | |||
*/ |