|
|
@@ -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 [ |