Browse Source

[Administration] Statistiques > Chiffre d'affaires : vue semaine par semaine

feature/souke
Guillaume Bourgeois 11 months ago
parent
commit
83dfb7e625
3 changed files with 61 additions and 19 deletions
  1. +4
    -5
      backend/controllers/StatsController.php
  2. +14
    -1
      backend/views/stats/index.php
  3. +43
    -13
      common/logic/Producer/Producer/Repository/ProducerRepository.php

+ 4
- 5
backend/controllers/StatsController.php View File

/** /**
* Affiche le CA réalisé par mois sur une année donnée * Affiche le CA réalisé par mois sur une année donnée
*/ */
public function actionIndex(int $year = null)
public function actionIndex(int $year = null, string $displayBy = 'month')
{ {
$producerModule = $this->getProducerModule();
$producerCurrent = $this->getProducerCurrent(); $producerCurrent = $this->getProducerCurrent();

if(!$year) { if(!$year) {
$year = date('Y'); $year = date('Y');
} }


$yearsWithTurnoverArray = $producerModule->getYearsWithTurnover($producerCurrent);
$dataChartTurnover = $producerModule->getDatasChartTurnoverStatistics($producerCurrent, $year);
$yearsWithTurnoverArray = $this->getProducerModule()->getRepository()->getYearsWithTurnover($producerCurrent);
$dataChartTurnover = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy);


return $this->render('index', [ return $this->render('index', [
'displayBy' => $displayBy,
'yearCurrent' => $year, 'yearCurrent' => $year,
'dataLabels' => $dataChartTurnover['labels'], 'dataLabels' => $dataChartTurnover['labels'],
'data' => $dataChartTurnover['data'], 'data' => $dataChartTurnover['data'],

+ 14
- 1
backend/views/stats/index.php View File



?> ?>


Affichage :
<?php
echo Html::a('Mois',
['stats/index', 'year' => $yearCurrent, 'displayBy' => 'month'],
['class' => 'btn btn-xs '.(($displayBy == 'month') ? 'btn-primary' : 'btn-default')]).' ';
echo Html::a('Semaine',
['stats/index', 'year' => $yearCurrent, 'displayBy' => 'week'],
['class' => 'btn btn-xs '.(($displayBy == 'week') ? 'btn-primary' : 'btn-default')]).' ';

?>

<br><br>

<?php <?php
foreach($yearsWithTurnoverArray as $year) { foreach($yearsWithTurnoverArray as $year) {
$classBtn = 'btn-default'; $classBtn = 'btn-default';
if($yearCurrent == $year) { if($yearCurrent == $year) {
$classBtn = 'btn-primary'; $classBtn = 'btn-primary';
} }
echo Html::a($year, ['stats/index', 'year' => $year], ['class' => 'btn '.$classBtn]).' ';
echo Html::a($year, ['stats/index', 'year' => $year, 'displayBy' => $displayBy], ['class' => 'btn '.$classBtn]).' ';
} }
?> ?>



+ 43
- 13
common/logic/Producer/Producer/Repository/ProducerRepository.php View File

$period = date('Y-m'); $period = date('Y-m');
} }


$dateStart = date('Y-m-31', strtotime("-1 month", strtotime($period)));
$dateEnd = date('Y-m-01', strtotime("+1 month", strtotime($period)));

return $this->getTurnoverByDateStartEnd($producer, $dateStart, $dateEnd, $format);
}

public function getTurnoverByWeek(Producer $producer, int $year, int $week, bool $format = false)
{
$date = new \DateTime();
$date->setISODate($year, $week);
$dateStart = $date->format('Y-m-d');
$date->modify('+6 days');
$dateEnd = $date->format('Y-m-d');

return $this->getTurnoverByDateStartEnd($producer, $dateStart, $dateEnd, $format);
}

public function getTurnoverByDateStartEnd(Producer $producer, string $dateStart, string $dateEnd, bool $format = false)
{
$connection = \Yii::$app->getDb(); $connection = \Yii::$app->getDb();
$command = $connection->createCommand( $command = $connection->createCommand(
' '
WHERE `order`.id = product_order.id_order WHERE `order`.id = product_order.id_order
AND distribution.id_producer = :id_producer AND distribution.id_producer = :id_producer
AND `order`.id_distribution = distribution.id AND `order`.id_distribution = distribution.id
AND distribution.date > :date_begin
AND distribution.date > :date_start
AND distribution.date < :date_end', AND distribution.date < :date_end',
[ [
':date_begin' => date('Y-m-31', strtotime("-1 month", strtotime($period))),
':date_end' => date('Y-m-01', strtotime("+1 month", strtotime($period))),
':date_start' => $dateStart,
':date_end' => $dateEnd,
':id_producer' => $producer->id ':id_producer' => $producer->id
] ]
); );
return $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, $format); return $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, $format);
} }


public function getDatasChartTurnoverStatistics(Producer $producer, int $year)
public function getDatasChartTurnoverStatistics(Producer $producer, int $year, string $displayBy = 'month')
{ {
$interval = new \DateInterval('P1M');
$data = [];
$dataLabels = [];

$start = new \DateTime($year.'-01-01'); $start = new \DateTime($year.'-01-01');
if($year == date('Y')) { if($year == date('Y')) {
$end = new \DateTime('last day of this month'); $end = new \DateTime('last day of this month');
else { else {
$end = new \DateTime($year.'-12-31'); $end = new \DateTime($year.'-12-31');
} }
$period = new \DatePeriod($start, $interval, $end);


$data = [];
$dataLabels = [];
$interval = new \DateInterval(($displayBy == 'week') ? 'P1W' : 'P1M');
$period = new \DatePeriod($start, $interval, $end);


foreach ($period as $date) {
$month = $date->format('m/Y');
$dataLabels[] = $month;
$turnover = $this->getTurnover($producer, $date->format('Y-m'));
$data[$month] = $turnover;
if($displayBy == 'week') {
foreach ($period as $date) {
$week = $date->format('W');
$dataLabels[] = $week;
$turnover = $this->getTurnoverByWeek($producer, $year, $date->format('W'));
$data[$week] = $turnover;
}
}
else {
foreach ($period as $date) {
$month = $date->format('m/Y');
$dataLabels[] = $month;
$turnover = $this->getTurnover($producer, $date->format('Y-m'));
$data[$month] = $turnover;
}
} }


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

Loading…
Cancel
Save