fix: SonarQube StatsController - constantes, CC, variable inutilisee

- DQL_BETWEEN_DATES, DQL_IS_PAID, COLOR_GOLD constantes
- Suppression $paymentsByMethod inutilisee
- getServiceStats CC 23->~10 : extraction groupServiceLines,
  mergeServiceGroups, resolveServiceStatus
- Ternaire imbrique remplace par resolveServiceStatus()
- Tests mis a jour (retrait getPaymentsByMethod de toutes les sequences)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-08 13:40:01 +02:00
parent 89f73f0ccf
commit 2ec4bb33c1
2 changed files with 64 additions and 38 deletions

View File

@@ -19,6 +19,9 @@ class StatsController extends AbstractController
private const STATUS_SURPLUS = 'Surplus';
private const STATUS_RENTABLE = 'Rentable';
private const STATUS_NEGATIF = 'Negatif';
private const DQL_BETWEEN_DATES = 'f.createdAt BETWEEN :from AND :to';
private const DQL_IS_PAID = 'f.isPaid = true';
private const COLOR_GOLD = '#fabf04';
public function __construct(
private EntityManagerInterface $em,
@@ -82,9 +85,6 @@ class StatsController extends AbstractController
'montant_impaye' => (float) $factureStats['totalEmis'] - (float) $factureStats['totalTtc'],
];
// Paiements par methode
$paymentsByMethod = $this->getPaymentsByMethod($from, $to);
// Evolution mensuelle (6 derniers mois)
$monthlyEvolution = $this->getMonthlyEvolution();
@@ -112,7 +112,7 @@ class StatsController extends AbstractController
// Toutes les factures de la periode
$allFactures = $qb->select('f')
->from(Facture::class, 'f')
->where('f.createdAt BETWEEN :from AND :to')
->where(self::DQL_BETWEEN_DATES)
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
@@ -218,8 +218,8 @@ class StatsController extends AbstractController
$factures = $this->em->createQueryBuilder()
->select('f')
->from(Facture::class, 'f')
->where('f.createdAt BETWEEN :from AND :to')
->andWhere('f.isPaid = true')
->where(self::DQL_BETWEEN_DATES)
->andWhere(self::DQL_IS_PAID)
->setParameter('from', $monthStart)
->setParameter('to', $monthEnd)
->getQuery()
@@ -243,9 +243,9 @@ class StatsController extends AbstractController
}
private const SERVICE_CONFIG = [
'website' => ['name' => 'E-Site', 'color' => '#fabf04', 'cout' => 150.00, 'group' => 'esite'],
'hosting' => ['name' => 'E-Site', 'color' => '#fabf04', 'cout' => 0.00, 'group' => 'esite'],
'maintenance' => ['name' => 'E-Site', 'color' => '#fabf04', 'cout' => 0.00, 'group' => 'esite'],
'website' => ['name' => 'E-Site', 'color' => self::COLOR_GOLD, 'cout' => 150.00, 'group' => 'esite'],
'hosting' => ['name' => 'E-Site', 'color' => self::COLOR_GOLD, 'cout' => 0.00, 'group' => 'esite'],
'maintenance' => ['name' => 'E-Site', 'color' => self::COLOR_GOLD, 'cout' => 0.00, 'group' => 'esite'],
'esymail' => ['name' => 'E-Mail', 'color' => '#dc2626', 'cout' => 70.00, 'group' => 'esymail'],
'ndd' => ['name' => 'Nom de domaine', 'color' => '#64748b', 'cout' => 0.00, 'cout_par_ligne' => 15.00, 'group' => 'ndd'],
'other' => ['name' => 'Autre', 'color' => '#6b7280', 'cout' => 0.00, 'group' => 'other'],
@@ -256,18 +256,45 @@ class StatsController extends AbstractController
*/
private function getServiceStats(\DateTimeImmutable $from, \DateTimeImmutable $to): array
{
// Recuperer toutes les lignes de factures payees sur la periode
$factures = $this->em->createQueryBuilder()
->select('f')
->from(Facture::class, 'f')
->where('f.createdAt BETWEEN :from AND :to')
->andWhere('f.isPaid = true')
->where(self::DQL_BETWEEN_DATES)
->andWhere(self::DQL_IS_PAID)
->setParameter('from', $from)
->setParameter('to', $to)
->getQuery()
->getResult();
// Grouper les lignes par type
$raw = $this->groupServiceLines($factures);
$grouped = $this->mergeServiceGroups($raw);
$services = [];
foreach ($grouped as $data) {
$marge = $data['ca_ht'] - $data['cout'];
$services[] = [
'name' => $data['name'],
'color' => $data['color'],
'ca_ht' => $data['ca_ht'],
'cout' => $data['cout'],
'marge' => $marge,
'status' => $this->resolveServiceStatus($data['ca_ht'], $marge),
'clients' => \count($data['customers']),
];
}
usort($services, fn ($a, $b) => $b['ca_ht'] <=> $a['ca_ht']);
return $services;
}
/**
* @param list<Facture> $factures
*
* @return array<string, array{ca_ht: float, customers: array<int, true>, lines: int}>
*/
private function groupServiceLines(array $factures): array
{
$raw = [];
foreach (self::SERVICE_CONFIG as $type => $config) {
$raw[$type] = ['ca_ht' => 0.0, 'customers' => [], 'lines' => 0];
@@ -281,7 +308,6 @@ class StatsController extends AbstractController
$type = 'other';
}
$raw[$type]['ca_ht'] += (float) $line->getPriceHt();
// Cout par ligne NDD : uniquement Renouvellement et Depot (pas Gestion)
$title = $line->getTitle();
if ('ndd' !== $type || str_contains($title, 'Renouvellement') || str_contains($title, 'Depot')) {
++$raw[$type]['lines'];
@@ -292,7 +318,16 @@ class StatsController extends AbstractController
}
}
// Fusionner par group (website+hosting+maintenance -> esite)
return $raw;
}
/**
* @param array<string, array{ca_ht: float, customers: array<int, true>, lines: int}> $raw
*
* @return array<string, array{name: string, color: string, ca_ht: float, cout: float, customers: array<int, true>}>
*/
private function mergeServiceGroups(array $raw): array
{
$grouped = [];
foreach (self::SERVICE_CONFIG as $type => $config) {
$group = $config['group'];
@@ -304,25 +339,16 @@ class StatsController extends AbstractController
$grouped[$group]['customers'] += $raw[$type]['customers'];
}
$services = [];
foreach ($grouped as $data) {
$marge = $data['ca_ht'] - $data['cout'];
return $grouped;
}
$services[] = [
'name' => $data['name'],
'color' => $data['color'],
'ca_ht' => $data['ca_ht'],
'cout' => $data['cout'],
'marge' => $marge,
'status' => $data['ca_ht'] <= 0 ? 'Inactif' : ($marge >= 0 ? 'Rentable' : 'Negatif'),
'clients' => \count($data['customers']),
];
private function resolveServiceStatus(float $caHt, float $marge): string
{
if ($caHt <= 0) {
return 'Inactif';
}
// Trier par CA decroissant
usort($services, fn ($a, $b) => $b['ca_ht'] <=> $a['ca_ht']);
return $services;
return $marge >= 0 ? 'Rentable' : 'Negatif';
}
private function getCoutPrestataire(\DateTimeImmutable $from, \DateTimeImmutable $to): float

View File

@@ -215,7 +215,7 @@ class StatsControllerTest extends TestCase
[$paidFacture, $unpaidFacture], // 1. getFactureStats
[], // 2. getPaymentStats
[], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution (6 months)
[$paidFacture], // 6. getServiceStats
];
@@ -246,7 +246,7 @@ class StatsControllerTest extends TestCase
[], // 1. getFactureStats
[$payment, $paymentNoMethod], // 2. getPaymentStats
[], // 3. getCoutPrestataire
[$payment, $paymentNoMethod], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution
[], // 6. getServiceStats
];
@@ -309,7 +309,7 @@ class StatsControllerTest extends TestCase
[$facture], // 1. getFactureStats
[], // 2. getPaymentStats
[], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution
[$facture], // 6. getServiceStats
];
@@ -335,7 +335,7 @@ class StatsControllerTest extends TestCase
[], // 1. getFactureStats
[], // 2. getPaymentStats
[$fp], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution
[], // 6. getServiceStats
];
@@ -366,7 +366,7 @@ class StatsControllerTest extends TestCase
[$facture], // 1. getFactureStats
[], // 2. getPaymentStats
[], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution
[$facture], // 6. getServiceStats
];
@@ -401,7 +401,7 @@ class StatsControllerTest extends TestCase
[$facture], // 1. getFactureStats
[], // 2. getPaymentStats
[$fp], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[], [], [], [], [], [], // 5. getMonthlyEvolution
[$facture], // 6. getServiceStats
];
@@ -443,7 +443,7 @@ class StatsControllerTest extends TestCase
[], // 1. getFactureStats
[], // 2. getPaymentStats
[], // 3. getCoutPrestataire
[], // 4. getPaymentsByMethod
[$paidFacture], [$paidFacture], [$paidFacture], [], [], [], // 5. getMonthlyEvolution
[], // 6. getServiceStats
];