| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Services;
- use Carbon\Carbon;
- use Illuminate\Support\Collection;
- /**
- * Prévia de cobrança "da rede toda, mês corrente" usada pela tela de billing
- * preview. Delega para TbrCalculationService::previewBatch() para que a regra
- * (isenção 1-3, maior valor x faturamento, faixa de porte, flags de cobrança,
- * renovação pós-mês-60) seja sempre a mesma do cálculo oficial — só o formato
- * de saída é adaptado para o que a tela já espera.
- */
- class TbrBillingPreviewService
- {
- public function __construct(
- protected TbrCalculationService $calculationService,
- ) {}
- public function getAll(): Collection
- {
- $now = Carbon::now();
- return collect($this->calculationService->previewBatch($now->year, $now->month))
- ->reject(fn (array $item) => isset($item['error']))
- ->map(fn (array $item) => $this->toBillingPreview($item))
- ->values();
- }
- private function toBillingPreview(array $item): array
- {
- return [
- 'id' => $item['unit_id'],
- 'unit_name' => $item['unit_name'],
- 'tbr_value' => $item['tbr_value'],
- 'royalties_value' => $item['royalties_effective_value'],
- 'royalties_rule' => $this->ruleLabel(
- $item['contract_month_reference'],
- $item['royalties_effective_value'],
- $item['royalties_effective_percentage'],
- $item['royalties_bracket_percentage'],
- ),
- 'fnm_value' => $item['fnm_effective_value'],
- 'fnm_rule' => $this->ruleLabel(
- $item['contract_month_reference'],
- $item['fnm_effective_value'],
- $item['fnm_effective_percentage'],
- $item['fnm_bracket_percentage'],
- ),
- 'maintenance_value' => $item['maintenance_effective_value'],
- 'total' => $item['final_value'],
- ];
- }
- private function ruleLabel(int $contractMonth, float $effectiveValue, float $effectivePercentage, float $bracketPercentage): string
- {
- if ($contractMonth <= 3) {
- return 'Isento';
- }
- if ($effectiveValue <= 0.0) {
- return 'Não cobrado';
- }
- return abs($effectivePercentage - $bracketPercentage) < 0.0001 ? 'Fixo TBR' : '% Faturamento';
- }
- }
|