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

$json['distributions'] = $this->buildAjaxInfosResponseDistributions($dateObject); $json['distributions'] = $this->buildAjaxInfosResponseDistributions($dateObject);


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

$distribution = $distributionManager->createDistributionIfNotExist($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; $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['orders'] = $this->buildAjaxInfosResponseOrders($ordersArray, $productsArray);
$json['points_sale'] = $pointSaleManager->findPointSalesByDistributionAsArray($distribution); $json['points_sale'] = $pointSaleManager->findPointSalesByDistributionAsArray($distribution);
$json['delivery_notes'] = $this->buildAjaxInfosResponseDeliveryNotes($date); $json['delivery_notes'] = $this->buildAjaxInfosResponseDeliveryNotes($date);
return $json; 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) public function buildAjaxInfosResponseProducer($producer)
{ {
return [ return [
]); ]);
} }


public function buildAjaxInfosResponseDistribution(Distribution $distribution, array $ordersArray)
public function buildAjaxInfosResponseDistribution(Distribution $distribution, array $ordersArray, array $productsArray)
{ {
$productManager = $this->getProductManager();
$orderManager = $this->getOrderManager(); $orderManager = $this->getOrderManager();
$distributionJsonData = [ $distributionJsonData = [
'id' => $distribution->id, 'id' => $distribution->id,
} }
$distributionJsonData['revenues'] = Price::format($revenues); $distributionJsonData['revenues'] = Price::format($revenues);
$distributionJsonData['weight'] = number_format($weight, 2); $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; return $distributionJsonData;
} }
$productOrderArray[$product['id']] = [ $productOrderArray[$product['id']] = [
'quantity' => 0, 'quantity' => 0,
'unit' => $product['unit'], 'unit' => $product['unit'],
'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
//'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
]; ];
} }
return [ return [

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

->filterByDistributionDate($distribution->date) ->filterByDistributionDate($distribution->date)
->filterIsValid() ->filterIsValid()
->filterByCondition($conditionAppend) ->filterByCondition($conditionAppend)
->orderBy('user.lastname ASC, user.name ASC')
->find(); ->find();
} }



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

return $this->createDefaultQuery() return $this->createDefaultQuery()
->filterIsOnline() ->filterIsOnline()
->filterByName($name) ->filterByName($name)
->fineOne();
->findOne();
} }


public function queryPointSalesByDistribution(Distribution $distribution) public function queryPointSalesByDistribution(Distribution $distribution)

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

return $this->createDefaultQuery()->count(); 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. * Retourne les produits d'une production donnée.
*/ */
public function findProductsByDistribution(Distribution $distribution) 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 = []; $productArrayById = [];
foreach($productArray as $product) {
foreach($productsArray as $product) {
$productArrayById[$product->id] = $product; $productArrayById[$product->id] = $product;
} }



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



return $productArrayFilter; 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