Browse Source

[Administration] Statistiques > Chiffre d'affaire : affichage du CA en TTC et HT

feature/souke
Guillaume Bourgeois 9 months ago
parent
commit
28720f0da6
3 changed files with 51 additions and 23 deletions
  1. +6
    -1
      backend/controllers/StatsController.php
  2. +26
    -10
      backend/views/stats/index.php
  3. +19
    -12
      domain/Producer/Producer/ProducerRepository.php

+ 6
- 1
backend/controllers/StatsController.php View File

$year = date('Y'); $year = date('Y');
} }


$isVatNotApplicable = !$producerCurrent->taxRate->value;
$yearsWithTurnoverArray = $this->getProducerModule()->getRepository()->getYearsWithTurnover($producerCurrent); $yearsWithTurnoverArray = $this->getProducerModule()->getRepository()->getYearsWithTurnover($producerCurrent);
$dataChartTurnover = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy); $dataChartTurnover = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy);
$dataChartTurnoverWithTax = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy, true);



return $this->render('index', [ return $this->render('index', [
'isVatNotApplicable' => $isVatNotApplicable,
'displayBy' => $displayBy, 'displayBy' => $displayBy,
'yearCurrent' => $year, 'yearCurrent' => $year,
'dataLabels' => $dataChartTurnover['labels'], 'dataLabels' => $dataChartTurnover['labels'],
'data' => $dataChartTurnover['data'], 'data' => $dataChartTurnover['data'],
'yearsWithTurnoverArray' => $yearsWithTurnoverArray
'dataWithTax' => $dataChartTurnoverWithTax['data'],
'yearsWithTurnoverArray' => $yearsWithTurnoverArray,
]); ]);
} }



+ 26
- 10
backend/views/stats/index.php View File

} }
echo Html::a($year, ['stats/index', 'year' => $year, 'displayBy' => $displayBy], ['class' => 'btn ' . $classBtn]) . ' '; echo Html::a($year, ['stats/index', 'year' => $year, 'displayBy' => $displayBy], ['class' => 'btn ' . $classBtn]) . ' ';
} }

if($isVatNotApplicable) {
$datasets = [
[
'label' => 'Recettes commandes',
'borderColor' => "#F39C12",
'data' => $data,
]
];
}
else {
$datasets = [
[
'label' => 'Recettes commandes (HT)',
'borderColor' => "#b4b4b4",
'data' => $data,
],
[
'label' => 'Recettes commandes (TTC)',
'borderColor' => "#F39C12",
'data' => $dataWithTax
]
];
}

?> ?>


<?= ChartJs::widget([ <?= ChartJs::widget([
], ],
'data' => [ 'data' => [
'labels' => $dataLabels, 'labels' => $dataLabels,
'datasets' => [
[
'label' => 'Recettes commandes',
'backgroundColor' => "rgb(255,127,0,0.5)",
'borderColor' => "rgb(255,127,0,1)",
'pointBackgroundColor' => "rgb(255,127,0,1)",
'pointStrokeColor' => "#fff",
'data' => $data
]
]
'datasets' => $datasets
] ]
]); ]);



+ 19
- 12
domain/Producer/Producer/ProducerRepository.php View File

public function getTurnoverLastMonth(Producer $producer, bool $format = false) public function getTurnoverLastMonth(Producer $producer, bool $format = false)
{ {
$period = date('Y-m', strtotime('-1 month')); $period = date('Y-m', strtotime('-1 month'));
return $this->getTurnover($producer, $period, $format);
return $this->getTurnover($producer, $period, false, $format);
} }


public function getYearsWithTurnover(Producer $producer): array public function getYearsWithTurnover(Producer $producer): array
/** /**
* Retourne le CA du producteur pour un mois donné * Retourne le CA du producteur pour un mois donné
*/ */
public function getTurnover(Producer $producer, string $period = '', bool $format = false)
public function getTurnover(Producer $producer, string $period = '', bool $withTax = false, bool $format = false)
{ {
if (!$period) { if (!$period) {
$period = date('Y-m'); $period = date('Y-m');
$dateStart = date('Y-m-31', strtotime("-1 month", strtotime($period))); $dateStart = date('Y-m-31', strtotime("-1 month", strtotime($period)));
$dateEnd = date('Y-m-01', strtotime("+1 month", strtotime($period))); $dateEnd = date('Y-m-01', strtotime("+1 month", strtotime($period)));


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


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


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


public function getTurnoverByDateStartEnd(Producer $producer, string $dateStart, string $dateEnd, bool $format = false)
public function getTurnoverByDateStartEnd(Producer $producer, string $dateStart, string $dateEnd, bool $withTax = false, bool $format = false)
{ {
$connection = \Yii::$app->getDb(); $connection = \Yii::$app->getDb();

$selectSum = 'product_order.price * product_order.quantity';
if($withTax) {
$selectSum .= ' * (tax_rate.value + 1)';
}

$command = $connection->createCommand( $command = $connection->createCommand(
' '
SELECT SUM(product_order.price * product_order.quantity) AS turnover
FROM `order`, product_order, distribution
SELECT SUM('.$selectSum.') AS turnover
FROM `order`, product_order, distribution, tax_rate
WHERE `order`.id = product_order.id_order WHERE `order`.id = product_order.id_order
AND `order`.date_delete IS NULL AND `order`.date_delete IS NULL
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_start AND distribution.date > :date_start
AND distribution.date < :date_end',
AND distribution.date < :date_end
AND product_order.id_tax_rate = tax_rate.id',
[ [
':date_start' => $dateStart, ':date_start' => $dateStart,
':date_end' => $dateEnd, ':date_end' => $dateEnd,
return $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, $format); return $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, $format);
} }


public function getDatasChartTurnoverStatistics(Producer $producer, int $year, string $displayBy = 'month')
public function getDatasChartTurnoverStatistics(Producer $producer, int $year, string $displayBy = 'month', bool $withTax = false)
{ {
$data = []; $data = [];
$dataLabels = []; $dataLabels = [];
foreach ($period as $date) { foreach ($period as $date) {
$week = $date->format('W'); $week = $date->format('W');
$dataLabels[] = $week; $dataLabels[] = $week;
$turnover = $this->getTurnoverByWeek($producer, $year, $date->format('W'));
$turnover = $this->getTurnoverByWeek($producer, $year, $date->format('W'), $withTax);
$data[$week] = $turnover; $data[$week] = $turnover;
} }
} }
foreach ($period as $date) { foreach ($period as $date) {
$month = $date->format('m/Y'); $month = $date->format('m/Y');
$dataLabels[] = $month; $dataLabels[] = $month;
$turnover = $this->getTurnover($producer, $date->format('Y-m'));
$turnover = $this->getTurnover($producer, $date->format('Y-m'), $withTax);
$data[$month] = $turnover; $data[$month] = $turnover;
} }
} }

Loading…
Cancel
Save