fix: SonarQube - computeDepenses CC 17->5, extraction dans ComptaExportService

- computeStripeCommissions et computeServiceCostsFromFactures
  deplaces dans ComptaExportService
- groupFactureLinesByTypeFromList public pour accepter des factures
- ComptabiliteController : 19 methodes (etait 21)
- ClientsController : persistNewContact extrait de handleContactForm

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-08 12:03:25 +02:00
parent 92bf777597
commit faeee1bffc
2 changed files with 84 additions and 56 deletions

View File

@@ -411,60 +411,15 @@ class ComptabiliteController extends AbstractController
*
* @return array<string, float>
*/
/**
* @param list<Facture> $factures
*
* @return array<string, float>
*/
private function computeDepenses(\DateTimeImmutable $from, \DateTimeImmutable $to, array $factures): array
{
// Commissions Stripe
$payments = $this->em->createQueryBuilder()
->select('p')
->from(AdvertPayment::class, 'p')
->where('p.createdAt BETWEEN :from AND :to')
->andWhere('p.type = :type')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('type', AdvertPayment::TYPE_SUCCESS)
->getQuery()
->getResult();
$totalCommission = 0.0;
foreach ($payments as $p) {
$totalCommission += round((float) $p->getAmount() * ComptaExportService::STRIPE_COMMISSION_RATE + ComptaExportService::STRIPE_FIXED_FEE, 2);
}
// Couts services
$raw = [];
foreach (ComptaExportService::SERVICE_COSTS as $type => $config) {
$raw[$type] = ['lines' => 0];
}
foreach ($factures as $facture) {
foreach ($facture->getLines() as $line) {
$type = $line->getType() ?? 'other';
if (!isset($raw[$type])) {
$type = 'other';
}
$title = $line->getTitle();
if ('ndd' !== $type || str_contains($title, 'Renouvellement') || str_contains($title, 'Depot')) {
++$raw[$type]['lines'];
}
}
}
$serviceGroups = [
'esite' => ['types' => ['website', 'hosting', 'maintenance']],
'esymail' => ['types' => ['esymail']],
'ndd' => ['types' => ['ndd']],
'other' => ['types' => ['other']],
];
$coutServices = 0.0;
foreach ($serviceGroups as $group) {
foreach ($group['types'] as $t) {
$config = ComptaExportService::SERVICE_COSTS[$t];
$coutServices += $config['cout'] + (($config['cout_par_ligne'] ?? 0.0) * $raw[$t]['lines']);
}
}
$coutInfra = 80.0;
$totalCommission = $this->exportService->computeStripeCommissions($from, $to);
$coutServices = $this->exportService->computeServiceCostsFromFactures($factures);
$depenses = [];
if ($totalCommission > 0) {
@@ -473,7 +428,7 @@ class ComptabiliteController extends AbstractController
if ($coutServices > 0) {
$depenses['Prestataires services numeriques'] = $coutServices;
}
$depenses['Infrastructure serveur'] = $coutInfra;
$depenses['Infrastructure serveur'] = 80.0;
return $depenses;
}

View File

@@ -428,7 +428,7 @@ class ComptaExportService
*
* @return array<string, array{ca_ht: float, lines: int}>
*/
private function groupFactureLinesByType(\DateTimeImmutable $from, \DateTimeImmutable $to): array
public function groupFactureLinesByType(\DateTimeImmutable $from, \DateTimeImmutable $to): array
{
$factures = $this->em->createQueryBuilder()
->select('f')
@@ -463,10 +463,83 @@ class ComptaExportService
return $grouped;
}
/**
* Groupe les lignes de factures fournies par type de service.
*
* @param list<Facture> $factures
*
* @return array<string, array{ca_ht: float, lines: int}>
*/
public function groupFactureLinesByTypeFromList(array $factures): array
{
$grouped = [];
foreach (self::SERVICE_COSTS as $type => $config) {
$grouped[$type] = ['ca_ht' => 0.0, 'lines' => 0];
}
foreach ($factures as $facture) {
foreach ($facture->getLines() as $line) {
$type = $line->getType() ?? 'other';
if (!isset($grouped[$type])) {
$type = 'other';
}
$grouped[$type]['ca_ht'] += (float) $line->getPriceHt();
$title = $line->getTitle();
if ('ndd' !== $type || str_contains($title, 'Renouvellement') || str_contains($title, 'Depot')) {
++$grouped[$type]['lines'];
}
}
}
return $grouped;
}
/**
* Calcule le total des commissions Stripe sur la periode.
*/
public function computeStripeCommissions(\DateTimeImmutable $from, \DateTimeImmutable $to): float
{
$payments = $this->em->createQueryBuilder()
->select('p')
->from(AdvertPayment::class, 'p')
->where('p.createdAt BETWEEN :from AND :to')
->andWhere('p.type = :type')
->setParameter('from', $from)
->setParameter('to', $to)
->setParameter('type', AdvertPayment::TYPE_SUCCESS)
->getQuery()
->getResult();
$total = 0.0;
foreach ($payments as $p) {
$total += round((float) $p->getAmount() * self::STRIPE_COMMISSION_RATE + self::STRIPE_FIXED_FEE, 2);
}
return $total;
}
/**
* Calcule le cout total des services a partir de factures fournies.
*
* @param list<Facture> $factures
*/
public function computeServiceCostsFromFactures(array $factures): float
{
$grouped = $this->groupFactureLinesByTypeFromList($factures);
$cost = 0.0;
foreach ($this->getServiceGroups() as $group) {
[, $cout] = $this->aggregateServiceGroup($group, $grouped);
$cost += $cout;
}
return $cost;
}
/**
* @return array<string, array{types: list<string>, name: string}>
*/
private function getServiceGroups(): array
public function getServiceGroups(): array
{
return [
'esite' => ['types' => ['website', 'hosting', 'maintenance'], 'name' => 'E-Site'],
@@ -484,7 +557,7 @@ class ComptaExportService
*
* @return array{float, float} [caHt, cout]
*/
private function aggregateServiceGroup(array $group, array $grouped): array
public function aggregateServiceGroup(array $group, array $grouped): array
{
$caHt = 0.0;
$cout = 0.0;