Parcourir la source

[backend] Optimisation requête statistiques (chiffre d'affaire)

refactoring
Guillaume Bourgeois il y a 5 ans
Parent
révision
21abc1d31c
1 fichiers modifiés avec 19 ajouts et 14 suppressions
  1. +19
    -14
      backend/controllers/StatsController.php

+ 19
- 14
backend/controllers/StatsController.php Voir le fichier

@@ -96,22 +96,27 @@ class StatsController extends BackendController
foreach ($period as $date) {
$month = date('m/Y', $date->getTimestamp());
$dataLabels[] = $month;
$data[$month] = 0;
}

// commandes
$ordersArray = Order::searchAll([],[
'conditions' => 'distribution.date > :date',
'params' => [':date' => $dateStart]
]) ;

if($ordersArray) {
foreach ($ordersArray as $order) {
$month = date('m/Y', strtotime($order->distribution->date));
if (isset($data[$month])) {
$data[$month] += $order->amount;
}
$res = Yii::$app->db->createCommand("SELECT SUM(product_order.price * product_order.quantity) AS total
FROM `order`, product_order, distribution
WHERE distribution.id_producer = :id_producer
AND `order`.id_distribution = distribution.id
AND `order`.id = product_order.id_order
AND distribution.date >= :date_start
AND distribution.date <= :date_end
")
->bindValue(':id_producer', Producer::getId())
->bindValue(':date_start', date('Y-m-', $date->getTimestamp()).'01')
->bindValue(':date_end', date('Y-m-', $date->getTimestamp()).'31' )
->queryOne();

if($res['total']) {
$data[$month] = $res['total'];
}
else {
$data[$month] = 0;
}

}

// création d'un tableau sans index car chart.js n'accepte pas les index

Chargement…
Annuler
Enregistrer