Browse Source

[Technique] Administration : refactoring DistributionController #1173

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
7fbe9e0902
5 changed files with 109 additions and 66 deletions
  1. +40
    -59
      backend/controllers/DistributionController.php
  2. +1
    -0
      common/logic/Order/Order/Repository/OrderRepository.php
  3. +1
    -1
      common/logic/PointSale/PointSale/Repository/PointSaleRepository.php
  4. +41
    -6
      common/logic/Product/Product/Repository/ProductRepository.php
  5. +26
    -0
      common/logic/Product/Product/Service/ProductSolver.php

+ 40
- 59
backend/controllers/DistributionController.php View File

@@ -128,66 +128,13 @@ class DistributionController extends BackendController
$json['distributions'] = $this->buildAjaxInfosResponseDistributions($dateObject);

if ($dateObject && $dateObject->format($format) === $date) {

$distribution = $distributionManager->createDistributionIfNotExist($date);
$ordersArray = Order::searchAll([
'distribution.id' => $distribution->id,
], [
'orderby' => 'user.lastname ASC, user.name ASC'
]);
$ordersArray = $orderManager->findOrdersByDistribution($distribution);
$ordersArrayObject = $ordersArray;
$productsArray = $productManager->findProductsByDistributionAsArray($distribution);


// products
$productsQuery = Product::find()
->orWhere(['id_producer' => GlobalParam::getCurrentProducerId(),])
->joinWith([
'taxRate',
'productDistribution' => function ($query) use ($distribution) {
$query->andOnCondition(
'product_distribution.id_distribution = ' . $distribution->id
);
}
])
->orderBy('product_distribution.active DESC, order ASC');

$productsArray = $productsQuery->asArray()->all();
$potentialRevenues = 0;
$potentialWeight = 0;

foreach ($productsArray as &$theProduct) {
$productObject = $productManager->findOneProductById($theProduct['id']);
$quantityOrder = $orderManager->getProductQuantity($productObject, $ordersArray);
$theProduct['quantity_ordered'] = $quantityOrder;

if (!isset($theProduct['productDistribution'][0])) {
$theProduct['productDistribution'][0] = $distributionManager->addProduct($distribution, $productObject);
}

if (!is_numeric($theProduct['productDistribution'][0]['quantity_max'])) {
$theProduct['quantity_remaining'] = null;
} else {
$theProduct['quantity_remaining'] = $theProduct['productDistribution'][0]['quantity_max'] - $quantityOrder;
}

$theProduct['quantity_form'] = 0;

if ($theProduct['productDistribution'][0]['active'] && $theProduct['productDistribution'][0]['quantity_max']) {
$potentialRevenues += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['price'];
$potentialWeight += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['weight'] / 1000;
}

if (!isset($theProduct['taxRate'])) {
$theProduct['taxRate'] = $producer->taxRate;
}
}

$json['products'] = $productsArray;

$json['distribution'] = $this->buildAjaxInfosResponseDistribution($distribution, $ordersArrayObject);
$json['distribution']['potential_revenues'] = Price::format($potentialRevenues);
$json['distribution']['potential_weight'] = number_format($potentialWeight, 2);

$json['products'] = $this->buildAjaxInfosResponseProducts($producer, $distribution, $productsArray, $ordersArray);
$json['distribution'] = $this->buildAjaxInfosResponseDistribution($distribution, $ordersArrayObject, $productsArray);
$json['orders'] = $this->buildAjaxInfosResponseOrders($ordersArray, $productsArray);
$json['points_sale'] = $pointSaleManager->findPointSalesByDistributionAsArray($distribution);
$json['delivery_notes'] = $this->buildAjaxInfosResponseDeliveryNotes($date);
@@ -201,6 +148,37 @@ class DistributionController extends BackendController
return $json;
}

public function buildAjaxInfosResponseProducts(Producer $producer, Distribution $distribution, array &$productsArray, array $ordersArray)
{
$distributionManager = $this->getDistributionManager();
$orderManager = $this->getOrderManager();
$productManager = $this->getProductManager();

foreach ($productsArray as &$theProduct) {
$productObject = $productManager->findOneProductById($theProduct['id']);
$quantityOrder = $orderManager->getProductQuantity($productObject, $ordersArray);
$theProduct['quantity_ordered'] = $quantityOrder;

if (!isset($theProduct['productDistribution'][0])) {
$theProduct['productDistribution'][0] = $distributionManager->addProduct($distribution, $productObject);
}

if (!is_numeric($theProduct['productDistribution'][0]['quantity_max'])) {
$theProduct['quantity_remaining'] = null;
} else {
$theProduct['quantity_remaining'] = $theProduct['productDistribution'][0]['quantity_max'] - $quantityOrder;
}

$theProduct['quantity_form'] = 0;

if (!isset($theProduct['taxRate'])) {
$theProduct['taxRate'] = $producer->taxRate;
}
}

return $productsArray;
}

public function buildAjaxInfosResponseProducer($producer)
{
return [
@@ -242,8 +220,9 @@ class DistributionController extends BackendController
]);
}

public function buildAjaxInfosResponseDistribution(Distribution $distribution, array $ordersArray)
public function buildAjaxInfosResponseDistribution(Distribution $distribution, array $ordersArray, array $productsArray)
{
$productManager = $this->getProductManager();
$orderManager = $this->getOrderManager();
$distributionJsonData = [
'id' => $distribution->id,
@@ -270,6 +249,8 @@ class DistributionController extends BackendController
}
$distributionJsonData['revenues'] = Price::format($revenues);
$distributionJsonData['weight'] = number_format($weight, 2);
$distributionJsonData['potential_revenues'] = Price::format($productManager->getProductDistributionPotentialRevenues($productsArray));
$distributionJsonData['potential_weight'] = number_format($productManager->getProductDistributionPotentialWeight($productsArray), 2);

return $distributionJsonData;
}
@@ -392,7 +373,7 @@ class DistributionController extends BackendController
$productOrderArray[$product['id']] = [
'quantity' => 0,
'unit' => $product['unit'],
'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
//'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
];
}
return [

+ 1
- 0
common/logic/Order/Order/Repository/OrderRepository.php View File

@@ -109,6 +109,7 @@ class OrderRepository extends AbstractRepository
->filterByDistributionDate($distribution->date)
->filterIsValid()
->filterByCondition($conditionAppend)
->orderBy('user.lastname ASC, user.name ASC')
->find();
}


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

@@ -47,7 +47,7 @@ class PointSaleRepository extends AbstractRepository
return $this->createDefaultQuery()
->filterIsOnline()
->filterByName($name)
->fineOne();
->findOne();
}

public function queryPointSalesByDistribution(Distribution $distribution)

+ 41
- 6
common/logic/Product/Product/Repository/ProductRepository.php View File

@@ -66,19 +66,54 @@ class ProductRepository extends AbstractRepository
return $this->createDefaultQuery()->count();
}

public function queryProductsByDistribution(Distribution $distribution)
{
return $this->createDefaultQuery()
//->leftJoin('product_distribution', 'product.id = product_distribution.id_product')
->joinWith([
//'taxRate',
'productDistribution' => function ($query) use ($distribution) {
$query->andOnCondition(
'product_distribution.id_distribution = ' . $distribution->id
);
}
])
//->filterByDistribution($distribution)
->orderBy('product_distribution.active DESC, product.order ASC');


/*$productsQuery = Product::find()
->orWhere(['id_producer' => GlobalParam::getCurrentProducerId(),])
->joinWith([
'taxRate',
'productDistribution' => function ($query) use ($distribution) {
$query->andOnCondition(
'product_distribution.id_distribution = ' . $distribution->id
);
}
])
->orderBy('product_distribution.active DESC, order ASC');*/

}

/**
* Retourne les produits d'une production donnée.
*/
public function findProductsByDistribution(Distribution $distribution)
{
$productArray = $this->createDefaultQuery()
->leftJoin('product_distribution', 'product.id = product_distribution.id_product')
->filterByDistribution($distribution)
->orderBy('product_distribution.active DESC, product.order ASC')
->find();
$productArray = $this->queryProductsByDistribution($distribution)->find();
return $this->buildProductsArrayById($productArray);
}

public function findProductsByDistributionAsArray(Distribution $distribution)
{
return $this->queryProductsByDistribution($distribution)->asArray()->find();
}

public function buildProductsArrayById(array $productsArray): array
{
$productArrayById = [];
foreach($productArray as $product) {
foreach($productsArray as $product) {
$productArrayById[$product->id] = $product;
}


+ 26
- 0
common/logic/Product/Product/Service/ProductSolver.php View File

@@ -243,4 +243,30 @@ class ProductSolver extends AbstractService implements SolverInterface

return $productArrayFilter;
}

public function getProductDistributionPotentialRevenues(array $productsArray)
{
$potentialRevenues = 0;

foreach($productsArray as $product) {
if ($product['productDistribution'][0]['active'] && $product['productDistribution'][0]['quantity_max']) {
$potentialRevenues += $product['productDistribution'][0]['quantity_max'] * $product['price'];
}
}

return $potentialRevenues;
}

public function getProductDistributionPotentialWeight(array $productsArray)
{
$potentialWeight = 0;

foreach($productsArray as $product) {
if ($product['productDistribution'][0]['active'] && $product['productDistribution'][0]['quantity_max']) {
$potentialWeight += $product['productDistribution'][0]['quantity_max'] * $product['weight'] / 1000;
}
}

return $potentialWeight;
}
}

Loading…
Cancel
Save