Pārlūkot izejas kodu

[Administration] Statistiques > chiffre d'affaire : affichage année par année #1256

feature/souke
Guillaume Bourgeois pirms 1 gada
vecāks
revīzija
0bf6b2a246
4 mainītis faili ar 97 papildinājumiem un 65 dzēšanām
  1. +12
    -48
      backend/controllers/StatsController.php
  2. +12
    -1
      backend/views/stats/index.php
  3. +1
    -1
      backend/views/stats/products.php
  4. +72
    -15
      common/logic/Producer/Producer/Repository/ProducerRepository.php

+ 12
- 48
backend/controllers/StatsController.php Parādīt failu

@@ -66,61 +66,25 @@ class StatsController extends BackendController
}

/**
* Affiche les statistiques de l'année avec le CA réalisé par mois.
* Affiche le CA réalisé par mois sur une année donnée
*/
public function actionIndex()
public function actionIndex(int $year = null)
{
/*
* Volume de commande de l'année passée (par mois)
*/
$dateStart = date('Y-m-d', time() - 60 * 60 * 24 * 365);
$dateEnd = date('Y-m-d', time() + 60 * 60 * 24 * 31);

$data = [];

// labels
$dataLabels = [];

$start = new DateTime($dateStart);
$interval = new DateInterval('P1M');
$end = new DateTime($dateEnd);

$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
$month = date('m/Y', $date->getTimestamp());
$dataLabels[] = $month;

$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', GlobalParam::getCurrentProducerId())
->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;
}
$producerManager = $this->getProducerManager();
$producerCurrent = $this->getProducerCurrent();

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

// création d'un tableau sans index car chart.js n'accepte pas les index
$dataNoIndex = [];
foreach ($data as $key => $val) {
$dataNoIndex[] = round($val, 2);
}
$yearsWithTurnoverArray = $producerManager->getYearsWithTurnover($producerCurrent);
$dataChartTurnover = $producerManager->getDatasChartTurnoverStatistics($producerCurrent, $year);

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


+ 12
- 1
backend/views/stats/index.php Parādīt failu

@@ -37,12 +37,23 @@ termes.
*/

use dosamigos\chartjs\ChartJs;
use yii\helpers\Html;

$this->setTitle('Statistiques <small>Chiffre d\'affaire</small>') ;
$this->setTitle('Statistiques - Chiffre d\'affaire') ;
$this->addBreadcrumb('Statistiques (chiffre d\'affaire)') ;

?>

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

<?= ChartJs::widget([
'type' => 'line',
'options' => [

+ 1
- 1
backend/views/stats/products.php Parādīt failu

@@ -39,7 +39,7 @@ termes.
use \backend\controllers\StatsController ;
use yii\helpers\Html;

$this->setTitle('Statistiques <small>Produits '.$year.'</small>', 'Statistiques produits '.$year) ;
$this->setTitle('Statistiques - Produits '.$year, 'Statistiques produits '.$year) ;
$this->addBreadcrumb('Statistiques produits '.$year) ;
$this->addButton(['label' => '&lt; '.($year - 1), 'url' => ['stats/products','year' => $year - 1], 'class' => 'btn btn-default']) ;
$this->addButton(['label' => ($year + 1).' &gt;', 'url' => ['stats/products','year' => $year + 1], 'class' => 'btn btn-default']) ;

+ 72
- 15
common/logic/Producer/Producer/Repository/ProducerRepository.php Parādīt failu

@@ -3,7 +3,6 @@
namespace common\logic\Producer\Producer\Repository;

use common\helpers\Departments;
use common\helpers\GlobalParam;
use common\helpers\Price;
use common\logic\AbstractRepository;
use common\logic\Document\Document\Model\DocumentInterface;
@@ -119,6 +118,31 @@ class ProducerRepository extends AbstractRepository
return $this->getTurnover($producer, $period, $format);
}

public function getYearsWithTurnover(Producer $producer): array
{
$year = date('Y');
$yearsArray = [];

for($i = 0; $i <= 10; $i++) {
if($this->hasTurnoverOverTheYear($producer, $year - $i)) {
array_unshift($yearsArray, $year - $i);
}
}

return $yearsArray;
}

public function hasTurnoverOverTheYear(Producer $producer, int $year): bool
{
for($month = 1; $month <= 12; $month++) {
if($this->getTurnover($producer, $year.'-'.$month)) {
return true;
}
}

return false;
}

/**
* Retourne le CA du producteur pour un mois donné
*/
@@ -132,11 +156,10 @@ class ProducerRepository extends AbstractRepository
$command = $connection->createCommand(
'
SELECT SUM(product_order.price * product_order.quantity) AS turnover
FROM `order`, product_order, distribution, product
FROM `order`, product_order, distribution
WHERE `order`.id = product_order.id_order
AND distribution.id_producer = :id_producer
AND `order`.id_distribution = distribution.id
AND product_order.id_product = product.id
AND distribution.date > :date_begin
AND distribution.date < :date_end',
[
@@ -160,7 +183,7 @@ class ProducerRepository extends AbstractRepository
{
$totalTurnover = 0;

for($i = 1; $i <= $numberMonths; $i++) {
for ($i = 1; $i <= $numberMonths; $i++) {
$timeMonth = strtotime('-' . $i . ' month');
$month = date('Y-m', $timeMonth);
$totalTurnover += $this->getTurnover($producer, $month);
@@ -175,6 +198,40 @@ class ProducerRepository extends AbstractRepository
return $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, $format);
}

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

$data = [];
$dataLabels = [];

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
$dataNoIndex = [];
foreach ($data as $key => $val) {
$dataNoIndex[] = round($val, 2);
}

return [
'labels' => $dataLabels,
'data' => $dataNoIndex
];
}

public function getSummaryAmountsToBeBilled(Producer $producer, string $label, int $numberOfMonths = 1, $context = 'list'): string
{
$text = '';
@@ -192,14 +249,14 @@ class ProducerRepository extends AbstractRepository
$turnover = $this->getTurnover($producer, $month);

if ($turnover) {
if($numberOfMonths > 1) {
$text .= ucfirst(strftime('%B ', $timeMonth)).' '.date('Y', $timeMonth).' : ';
if ($numberOfMonths > 1) {
$text .= ucfirst(strftime('%B ', $timeMonth)) . ' ' . date('Y', $timeMonth) . ' : ';
}
$isBold = $this->producerSolver->isBillingTypeClassic($producer) && !$producer->option_billing_permanent_transfer;
if ($isBold) $text .= '<strong>';
$text .= $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, true);
if ($isBold) $text .= '</strong>';
$text .= ' (' . Price::format($turnover, 0). ' CA)';
$text .= ' (' . Price::format($turnover, 0) . ' CA)';
$text .= '<br />';

$sumInvoicePrice += $this->producerPriceRangeRepository->getAmountToBeBilledByTurnover($turnover, false);
@@ -207,12 +264,12 @@ class ProducerRepository extends AbstractRepository
}

if (strlen($text)) {
$text = '<i>'.$label.'</i>' . ' : <br />' . $text;
$text = '<i>' . $label . '</i>' . ' : <br />' . $text;
}
}

if($numberOfMonths > 1 && $sumInvoicePrice > 0) {
$text .= '<br />Total : <strong>'.Price::format($sumInvoicePrice, 0).'</strong>';
if ($numberOfMonths > 1 && $sumInvoicePrice > 0) {
$text .= '<br />Total : <strong>' . Price::format($sumInvoicePrice, 0) . '</strong>';
}

return $text;
@@ -273,9 +330,9 @@ class ProducerRepository extends AbstractRepository
$count = 0;
$producersArray = $this->findProducersActive();

foreach($producersArray as $producer) {
if($this->getTurnoverByNumberMonths($producer, 3)) {
$count ++;
foreach ($producersArray as $producer) {
if ($this->getTurnoverByNumberMonths($producer, 3)) {
$count++;
}
}

@@ -311,9 +368,9 @@ class ProducerRepository extends AbstractRepository
$producersWithTimeSavedArray = $this->findProducersWithTimeSaved();
$countProducersWithOptionTimeSaved = count($producersWithTimeSavedArray);

if($countProducersWithOptionTimeSaved) {
if ($countProducersWithOptionTimeSaved) {
$sumTimeSaved = 0;
foreach($producersWithTimeSavedArray as $producerWithTimeSaved) {
foreach ($producersWithTimeSavedArray as $producerWithTimeSaved) {
$sumTimeSaved += $producerWithTimeSaved->option_time_saved;
}


Notiek ielāde…
Atcelt
Saglabāt