>> jour → catégorie → taux → [HT, TVA] (exacts). */ private $buckets = array(); /** @var array> jour → taux → montant encaissé TTC (exact). */ private $payments = array(); /** @var Anomaly[] */ private $anomalies = array(); /** @var array Commandes laissées de côté : identifiant → raison (code). */ private $excluded = array(); public function add( string $day, string $category, int $rate, Amount $net, Amount $tax ): void { if ( ! isset( $this->buckets[ $day ][ $category ][ $rate ] ) ) { $this->buckets[ $day ][ $category ][ $rate ] = array( Amount::zero(), Amount::zero() ); } $this->buckets[ $day ][ $category ][ $rate ][0] = $this->buckets[ $day ][ $category ][ $rate ][0]->add( $net ); $this->buckets[ $day ][ $category ][ $rate ][1] = $this->buckets[ $day ][ $category ][ $rate ][1]->add( $tax ); } /** Encaissement (positif) ou décaissement (négatif) d'une prestation de services, TTC (S10, S11). */ public function addPayment( string $day, int $rate, Amount $gross ): void { if ( ! isset( $this->payments[ $day ][ $rate ] ) ) { $this->payments[ $day ][ $rate ] = Amount::zero(); } $this->payments[ $day ][ $rate ] = $this->payments[ $day ][ $rate ]->add( $gross ); } /** * Encaissements des prestations de services, arrondis au centime une fois par jour et par taux. * * @return array> jour → taux → centimes TTC. */ public function payments(): array { $out = array(); ksort( $this->payments ); foreach ( $this->payments as $day => $rates ) { ksort( $rates ); foreach ( $rates as $rate => $amount ) { $cents = $amount->toCents(); if ( 0 !== $cents ) { $out[ $day ][ $rate ] = $cents; } } } return $out; } public function addAnomaly( Anomaly $anomaly ): void { $this->anomalies[] = $anomaly; } public function exclude( string $orderId, string $reason ): void { $this->excluded[ $orderId ] = $reason; } /** * Totaux arrondis au centime, prêts à déclarer (décision C11). * Les lignes dont la base et la TVA sont toutes deux nulles sont omises. * * @return array>> jour → catégorie → taux → centimes. */ public function totals(): array { $out = array(); ksort( $this->buckets ); foreach ( $this->buckets as $day => $categories ) { ksort( $categories ); foreach ( $categories as $category => $rates ) { ksort( $rates ); foreach ( $rates as $rate => $pair ) { $taxable = $pair[0]->toCents(); $vat = $pair[1]->toCents(); if ( 0 === $taxable && 0 === $vat ) { continue; } $out[ $day ][ $category ][ $rate ] = array( 'taxable' => $taxable, 'vat' => $vat, ); } } } return $out; } /** * Total d'un jour et d'une catégorie (somme des lignes arrondies : exigence G1.53). * * @return array{taxable:int, vat:int} */ public function dayTotal( string $day, string $category ): array { $totals = $this->totals(); $taxable = 0; $vat = 0; if ( isset( $totals[ $day ][ $category ] ) ) { foreach ( $totals[ $day ][ $category ] as $row ) { $taxable += $row['taxable']; $vat += $row['vat']; } } return array( 'taxable' => $taxable, 'vat' => $vat, ); } /** @return Anomaly[] */ public function anomalies(): array { return $this->anomalies; } /** @return string[] Jours dont l'envoi automatique doit attendre une correction. */ public function blockedDays(): array { $days = array(); foreach ( $this->anomalies as $anomaly ) { if ( Anomaly::BLOCKING === $anomaly->severity && null !== $anomaly->day ) { $days[ $anomaly->day ] = true; } } $list = array_keys( $days ); sort( $list ); return $list; } /** @return array */ public function excluded(): array { return $this->excluded; } }