| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- <?php
- namespace App\Services;
- use App\Models\FranchiseeAccountReceive;
- use App\Models\FranchiseeAccountReceiveDetail;
- use App\Models\FranchiseeContract;
- use App\Models\InhabitantClassification;
- use App\Models\StudentContractInstallment;
- use App\Models\Tbr;
- use App\Models\TbrCalculation;
- use App\Models\UnitAccountReceivable;
- use App\Models\UnitFinancial;
- use Carbon\Carbon;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Validation\ValidationException;
- class TbrCalculationService
- {
- private const ROYALTIES_REVENUE_RATE = 0.08;
- private const FNM_REVENUE_RATE = 0.02;
- private const FNM_BRACKET_PERCENTAGE = 0.20;
- private const MAINTENANCE_RATE = 0.30;
- private const EXEMPT_THRESHOLD_MONTH = 3;
- // Faturamento (competência): por padrão usa o próprio mês de referência
- // (Opção A — assume-se o mês já fechado ao gerar). Ligue para usar o mês
- // anterior fechado (Opção B) quando a geração ocorrer dentro do mês corrente.
- private const REVENUE_FROM_PREVIOUS_MONTH = false;
- /**
- * Faturamento da unidade no mês (competência): soma das parcelas de aluno com
- * vencimento no mês, de contratos não cancelados. Alimenta a regra "maior entre
- * valor fixo e % do faturamento".
- */
- public function resolveRevenue(int $unitId, int $referenceYear, int $referenceMonth): float
- {
- $base = Carbon::createFromDate($referenceYear, $referenceMonth, 1);
- if (self::REVENUE_FROM_PREVIOUS_MONTH) {
- $base = $base->subMonthNoOverflow();
- }
- return $this->resolveRevenueBetween(
- [$unitId],
- $base->copy()->startOfMonth(),
- $base->copy()->endOfMonth(),
- );
- }
- /**
- * Faturamento-base do TBR em um intervalo. Mantém dashboard e geração do TBR
- * usando a mesma composição: parcelas de alunos + recebíveis avulsos.
- * Um array vazio de unidades representa toda a rede.
- */
- public function resolveRevenueBetween(array $unitIds, Carbon $startDate, Carbon $endDate): float
- {
- $start = $startDate->toDateString();
- $end = $endDate->toDateString();
- // Base de faturamento = parcelas de aluno + recebíveis avulsos da unidade
- // + contas manuais que a franqueadora vinculou à unidade. Recebíveis de
- // TBR ficam fora para não realimentar a própria base de cálculo.
- $installments = (float) StudentContractInstallment::whereNull('deleted_at')
- ->when(! empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
- ->where('status', '!=', 'cancelled')
- ->whereBetween('due_date', [$start, $end])
- ->sum('value');
- $manual = (float) UnitAccountReceivable::whereNull('deleted_at')
- ->when(! empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
- ->where('status', '!=', 'cancelled')
- ->whereBetween('due_date', [$start, $end])
- ->sum('value');
- $franchisorLinked = (float) FranchiseeAccountReceive::whereNull('deleted_at')
- ->where('origin', 'manual_unit')
- ->whereNotNull('unit_id')
- ->when(! empty($unitIds), fn ($q) => $q->whereIn('unit_id', $unitIds))
- ->where('status', '!=', 'cancelled')
- ->whereBetween('due_date', [$start, $end])
- ->sum('value');
- return $installments + $manual + $franchisorLinked;
- }
- /**
- * Faturamento do cálculo individual: usa o valor informado manualmente
- * (override) quando > 0; senão calcula automaticamente pelas parcelas do mês.
- */
- private function pickRevenue(array $data, int $unitId, int $year, int $month): float
- {
- $manual = (float) ($data['revenue_value'] ?? 0);
- return $manual > 0 ? $manual : $this->resolveRevenue($unitId, $year, $month);
- }
- public function paginate(int $perPage = 15): LengthAwarePaginator
- {
- return TbrCalculation::with(['unit', 'user'])
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- public function listAll(int $limit = 100): \Illuminate\Database\Eloquent\Collection
- {
- return TbrCalculation::with(['unit', 'user'])
- ->orderBy('created_at', 'desc')
- ->limit($limit)
- ->get();
- }
- public function findById(int $id): ?TbrCalculation
- {
- return TbrCalculation::with([
- 'unit',
- 'user',
- 'royaltiesBracket',
- ])->find($id);
- }
- public function preview(array $data): array
- {
- $contract = $this->resolveContract($data['unit_id']);
- $year = (int) $data['reference_year'];
- $month = (int) $data['reference_month'];
- return $this->buildPreview($contract, $year, $month, $this->pickRevenue($data, $contract->unit_id, $year, $month));
- }
- public function previewBatch(int $referenceYear, int $referenceMonth): array
- {
- $contracts = $this->loadActiveContracts($referenceYear, $referenceMonth);
- return $contracts->map(function (FranchiseeContract $contract) use ($referenceYear, $referenceMonth) {
- try {
- $revenue = $this->resolveRevenue($contract->unit_id, $referenceYear, $referenceMonth);
- return $this->buildPreview($contract, $referenceYear, $referenceMonth, $revenue);
- } catch (ValidationException $e) {
- return [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $contract->unit?->fantasy_name,
- 'error' => collect($e->errors())->flatten()->first(),
- ];
- }
- })->values()->toArray();
- }
- public function calculate(array $data): TbrCalculation
- {
- return DB::transaction(function () use ($data) {
- $contract = $this->resolveContract($data['unit_id']);
- $year = (int) $data['reference_year'];
- $month = (int) $data['reference_month'];
- $payload = $this->buildPreview($contract, $year, $month, $this->pickRevenue($data, $contract->unit_id, $year, $month));
- return $this->persistCalculation($payload);
- });
- }
- public function generateReceivable(int $calculationId): FranchiseeAccountReceive
- {
- return DB::transaction(function () use ($calculationId) {
- $calculation = TbrCalculation::lockForUpdate()->findOrFail($calculationId);
- if ($calculation->receivable_generated) {
- throw ValidationException::withMessages([
- 'tbr_calculation_id' => 'Já existe um título gerado para este cálculo.',
- ]);
- }
- $duplicate = TbrCalculation::where('unit_id', $calculation->unit_id)
- ->where('contract_month_reference', $calculation->contract_month_reference)
- ->where('receivable_generated', true)
- ->where('id', '!=', $calculation->id)
- ->exists();
- if ($duplicate) {
- throw ValidationException::withMessages([
- 'tbr_calculation_id' => 'Já existe um título gerado para esta unidade no mês de contrato '
- .$calculation->contract_month_reference.'.',
- ]);
- }
- $contract = FranchiseeContract::where('unit_id', $calculation->unit_id)
- ->orderByDesc('start_date')
- ->first();
- return $this->buildReceivable($calculation, $contract);
- });
- }
- public function generateBatch(int $referenceYear, int $referenceMonth, ?array $unitIds = null): array
- {
- $contracts = $this->loadActiveContracts($referenceYear, $referenceMonth);
- if ($unitIds !== null) {
- $contracts = $contracts->filter(fn ($c) => in_array($c->unit_id, $unitIds, true))->values();
- }
- $generated = [];
- $skipped = [];
- $errors = [];
- foreach ($contracts as $contract) {
- try {
- DB::transaction(function () use ($contract, $referenceYear, $referenceMonth, &$generated, &$skipped) {
- $revenue = $this->resolveRevenue($contract->unit_id, $referenceYear, $referenceMonth);
- $payload = $this->buildPreview($contract, $referenceYear, $referenceMonth, $revenue);
- if ($payload['receivable_already_generated']) {
- $skipped[] = [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $payload['unit_name'],
- 'reason' => 'Já gerado para este mês de contrato.',
- ];
- return;
- }
- $calculation = $this->persistCalculation($payload);
- $receive = $this->buildReceivable($calculation, $contract);
- $generated[] = [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $payload['unit_name'],
- 'tbr_calculation_id' => $calculation->id,
- 'receivable_id' => $receive->id,
- 'total' => $payload['final_value'],
- ];
- });
- } catch (ValidationException $e) {
- $errors[] = [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $contract->unit?->fantasy_name,
- 'reason' => collect($e->errors())->flatten()->first(),
- ];
- } catch (\Throwable $e) {
- $errors[] = [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $contract->unit?->fantasy_name,
- 'reason' => $e->getMessage(),
- ];
- }
- }
- return [
- 'generated_count' => count($generated),
- 'skipped_count' => count($skipped),
- 'error_count' => count($errors),
- 'generated' => $generated,
- 'skipped' => $skipped,
- 'errors' => $errors,
- ];
- }
- private function loadActiveContracts(int $referenceYear, int $referenceMonth): \Illuminate\Support\Collection
- {
- $referenceLastDay = Carbon::createFromDate($referenceYear, $referenceMonth, 1)->endOfMonth()->toDateString();
- $referenceFirstDay = Carbon::createFromDate($referenceYear, $referenceMonth, 1)->startOfMonth()->toDateString();
- return FranchiseeContract::with(['unit', 'municipalitySize'])
- ->whereNotNull('start_date')
- ->whereNotNull('municipality_size_id')
- ->where('start_date', '<=', $referenceLastDay)
- ->where(function ($q) use ($referenceFirstDay) {
- $q->whereNull('end_date')->orWhere('end_date', '>=', $referenceFirstDay);
- })
- ->orderByDesc('start_date')
- ->orderByDesc('id')
- ->get()
- ->unique('unit_id')
- ->values();
- }
- private function resolveContract(int $unitId): FranchiseeContract
- {
- $contract = FranchiseeContract::with(['unit', 'municipalitySize'])
- ->where('unit_id', $unitId)
- ->whereNotNull('start_date')
- ->orderByDesc('start_date')
- ->orderByDesc('id')
- ->first();
- if (! $contract) {
- throw ValidationException::withMessages([
- 'unit_id' => 'Unidade não possui contrato cadastrado.',
- ]);
- }
- if (! $contract->municipality_size_id) {
- throw ValidationException::withMessages([
- 'unit_id' => 'O contrato da unidade não tem a faixa de habitantes definida. Edite o contrato para informá-la.',
- ]);
- }
- return $contract;
- }
- private function buildPreview(FranchiseeContract $contract, int $referenceYear, int $referenceMonth, float $revenueValue): array
- {
- $tbrValue = (float) ($contract->tbr_fixed_value ?? 0);
- if ($tbrValue <= 0) {
- $tbrValue = (float) (Tbr::where('year', $referenceYear)->orderByDesc('id')->value('tbr_value') ?? 0);
- }
- if ($tbrValue <= 0) {
- throw ValidationException::withMessages([
- 'unit_id' => 'TBR não definida para o contrato nem para o ano de referência.',
- ]);
- }
- $contractMonth = $this->resolveContractMonth($contract->start_date, $referenceYear, $referenceMonth);
- $municipalitySizeId = (int) $contract->municipality_size_id;
- // Flags de cobrança da unidade (sem registro financeiro => cobra, padrão).
- $financial = UnitFinancial::where('unit_id', $contract->unit_id)->first();
- $chargeRoi = $financial ? (bool) $financial->charge_roi : true;
- $chargeFnm = $financial ? (bool) $financial->charge_fnm : true;
- // Royalties só busca a faixa quando há cobrança; desligado não exige faixa.
- $royaltiesBracket = $chargeRoi
- ? $this->findRoyaltiesBracket($municipalitySizeId, $contractMonth)
- : null;
- $royaltiesBracketPercentage = $royaltiesBracket ? (float) $royaltiesBracket->tbr_percentage : 0.0;
- $fnmPercentage = $chargeFnm ? $this->resolveFnmPercentage($contractMonth) : 0.0;
- $maintenancePercentage = self::MAINTENANCE_RATE;
- $royaltiesBracketValue = round($royaltiesBracketPercentage * $tbrValue, 2);
- $fnmBracketValue = round($fnmPercentage * $tbrValue, 2);
- $maintenanceBracketValue = round($maintenancePercentage * $tbrValue, 2);
- [$royaltiesEffectiveValue, $royaltiesEffectivePercentage, $royaltiesAppliedCriteria,
- $fnmEffectiveValue, $fnmEffectivePercentage] = $this->resolveEffectiveValues(
- $contractMonth,
- $revenueValue,
- $royaltiesBracketPercentage,
- $royaltiesBracketValue,
- $fnmPercentage,
- $fnmBracketValue,
- );
- // Cobrança desligada: zera o respectivo componente (fora do total).
- if (! $chargeRoi) {
- $royaltiesEffectiveValue = 0.0;
- $royaltiesEffectivePercentage = 0.0;
- $royaltiesAppliedCriteria = 'nao_cobrado';
- }
- if (! $chargeFnm) {
- $fnmEffectiveValue = 0.0;
- $fnmEffectivePercentage = 0.0;
- }
- $maintenanceEffectiveValue = $maintenanceBracketValue;
- $maintenanceEffectivePercentage = $maintenancePercentage;
- $bracketSubtotal = round($royaltiesBracketValue + $fnmBracketValue + $maintenanceBracketValue, 2);
- $subtotal = round($royaltiesEffectiveValue + $fnmEffectiveValue + $maintenanceEffectiveValue, 2);
- return [
- 'unit_id' => $contract->unit_id,
- 'unit_name' => $contract->unit?->fantasy_name,
- 'contract_id' => $contract->id,
- 'reference_year' => $referenceYear,
- 'reference_month' => $referenceMonth,
- 'contract_month_reference' => $contractMonth,
- 'revenue_value' => $revenueValue,
- 'tbr_value' => $tbrValue,
- 'municipality_size_id' => $municipalitySizeId,
- 'municipality_size_name' => $contract->municipalitySize?->description,
- 'royalties_bracket_id' => $royaltiesBracket?->id,
- 'royalties_bracket_percentage' => $royaltiesBracketPercentage,
- 'royalties_bracket_value' => $royaltiesBracketValue,
- 'fnm_bracket_percentage' => $fnmPercentage,
- 'fnm_bracket_value' => $fnmBracketValue,
- 'maintenance_bracket_percentage' => $maintenancePercentage,
- 'maintenance_bracket_value' => $maintenanceBracketValue,
- 'royalties_effective_percentage' => $royaltiesEffectivePercentage,
- 'royalties_effective_value' => $royaltiesEffectiveValue,
- 'fnm_effective_percentage' => $fnmEffectivePercentage,
- 'fnm_effective_value' => $fnmEffectiveValue,
- 'maintenance_effective_percentage' => $maintenanceEffectivePercentage,
- 'maintenance_effective_value' => $maintenanceEffectiveValue,
- 'bracket_subtotal' => $bracketSubtotal,
- 'subtotal' => $subtotal,
- 'final_value' => $subtotal,
- 'royalties_applied_criteria' => $royaltiesAppliedCriteria,
- 'receivable_already_generated' => $this->existingReceivable($contract->unit_id, $contractMonth),
- ];
- }
- private function persistCalculation(array $payload): TbrCalculation
- {
- return TbrCalculation::create([
- 'unit_id' => $payload['unit_id'],
- 'reference_year' => $payload['reference_year'],
- 'reference_month' => $payload['reference_month'],
- 'revenue_value' => $payload['revenue_value'],
- 'contract_month_reference' => $payload['contract_month_reference'],
- 'tbr_value' => $payload['tbr_value'],
- 'royalties_bracket_id' => $payload['royalties_bracket_id'],
- 'royalties_bracket_percentage' => $payload['royalties_bracket_percentage'],
- 'royalties_bracket_value' => $payload['royalties_bracket_value'],
- 'fnm_bracket_percentage' => $payload['fnm_bracket_percentage'],
- 'fnm_bracket_value' => $payload['fnm_bracket_value'],
- 'maintenance_bracket_percentage' => $payload['maintenance_bracket_percentage'],
- 'maintenance_bracket_value' => $payload['maintenance_bracket_value'],
- 'royalties_effective_percentage' => $payload['royalties_effective_percentage'],
- 'royalties_effective_value' => $payload['royalties_effective_value'],
- 'fnm_effective_percentage' => $payload['fnm_effective_percentage'],
- 'fnm_effective_value' => $payload['fnm_effective_value'],
- 'maintenance_effective_percentage' => $payload['maintenance_effective_percentage'],
- 'maintenance_effective_value' => $payload['maintenance_effective_value'],
- 'bracket_subtotal' => $payload['bracket_subtotal'],
- 'subtotal' => $payload['subtotal'],
- 'final_value' => $payload['final_value'],
- 'user_id' => Auth::id(),
- 'royalties_applied_criteria' => $payload['royalties_applied_criteria'],
- 'receivable_generated' => false,
- ]);
- }
- private function buildReceivable(TbrCalculation $calculation, ?FranchiseeContract $contract): FranchiseeAccountReceive
- {
- // Mês de competência = o que a pessoa escolheu ao gerar (não a data de geração).
- $referenceDate = $calculation->reference_year && $calculation->reference_month
- ? Carbon::create($calculation->reference_year, $calculation->reference_month, 1)
- : Carbon::parse($calculation->created_at);
- $referenceLabel = $referenceDate->format('m/Y');
- $dueDate = $this->resolveDueDate($contract, $referenceDate);
- $receive = FranchiseeAccountReceive::create([
- 'unit_id' => $calculation->unit_id,
- 'tbr_calculation_id' => $calculation->id,
- 'order' => $calculation->contract_month_reference,
- 'history' => 'Royalties / FNM / Manutenção — '.$referenceLabel,
- 'value' => $calculation->final_value,
- 'paid_value' => 0,
- 'due_date' => $dueDate,
- 'discount' => 0,
- 'fees' => 0,
- 'obs' => null,
- 'asaas_id' => null,
- 'status' => 'pending',
- ]);
- FranchiseeAccountReceiveDetail::create([
- 'franchisee_account_receive_id' => $receive->id,
- 'value' => $calculation->royalties_effective_value,
- 'history' => 'Royalties '.$referenceLabel,
- ]);
- FranchiseeAccountReceiveDetail::create([
- 'franchisee_account_receive_id' => $receive->id,
- 'value' => $calculation->fnm_effective_value,
- 'history' => 'FNM '.$referenceLabel,
- ]);
- FranchiseeAccountReceiveDetail::create([
- 'franchisee_account_receive_id' => $receive->id,
- 'value' => $calculation->maintenance_effective_value,
- 'history' => 'Taxa Manutenção '.$referenceLabel,
- ]);
- $calculation->update(['receivable_generated' => true]);
- // Espelho na unidade: o TBR que a matriz cobra vira uma Conta a Pagar da
- // franquia (ciclo próprio, com ou sem Asaas). A dívida é registrada sempre.
- \App\Models\UnitAccountPayable::create([
- 'unit_id' => $receive->unit_id,
- 'franchisee_account_receive_id' => $receive->id,
- 'origin' => \App\Models\UnitAccountPayable::ORIGIN_TBR,
- 'history' => $receive->history,
- 'value' => $receive->value,
- 'paid_value' => 0,
- 'discount' => 0,
- 'fine' => 0,
- 'due_date' => $receive->due_date,
- 'status' => 'pending',
- ]);
- \App\Jobs\SyncFranchiseeChargeJob::dispatch($receive);
- return $receive->load('details');
- }
- private function resolveEffectiveValues(
- int $contractMonth,
- float $revenueValue,
- float $royaltiesBracketPercentage,
- float $royaltiesBracketValue,
- float $fnmBracketPercentage,
- float $fnmBracketValue,
- ): array {
- if ($contractMonth <= self::EXEMPT_THRESHOLD_MONTH) {
- return [0.0, 0.0, 'tbr_fixo', 0.0, 0.0];
- }
- $royaltiesFromRevenue = round(self::ROYALTIES_REVENUE_RATE * $revenueValue, 2);
- $fnmFromRevenue = round(self::FNM_REVENUE_RATE * $revenueValue, 2);
- if ($royaltiesBracketValue >= $royaltiesFromRevenue) {
- $royaltiesEffectiveValue = $royaltiesBracketValue;
- $royaltiesEffectivePercentage = $royaltiesBracketPercentage;
- $royaltiesAppliedCriteria = 'tbr_fixo';
- } else {
- $royaltiesEffectiveValue = $royaltiesFromRevenue;
- $royaltiesEffectivePercentage = self::ROYALTIES_REVENUE_RATE;
- $royaltiesAppliedCriteria = 'percentual_faturamento';
- }
- if ($fnmBracketValue >= $fnmFromRevenue) {
- $fnmEffectiveValue = $fnmBracketValue;
- $fnmEffectivePercentage = $fnmBracketPercentage;
- } else {
- $fnmEffectiveValue = $fnmFromRevenue;
- $fnmEffectivePercentage = self::FNM_REVENUE_RATE;
- }
- return [$royaltiesEffectiveValue, $royaltiesEffectivePercentage, $royaltiesAppliedCriteria, $fnmEffectiveValue, $fnmEffectivePercentage];
- }
- private function resolveContractMonth(?Carbon $startDate, int $year, int $month): int
- {
- if (! $startDate) {
- return 1;
- }
- $start = $startDate->copy()->startOfMonth();
- $reference = Carbon::createFromDate($year, $month, 1)->startOfMonth();
- $diff = (int) $start->diffInMonths($reference);
- return max(1, $diff + 1);
- }
- private function findRoyaltiesBracket(int $municipalitySizeId, int $contractMonth): InhabitantClassification
- {
- $bracket = InhabitantClassification::where('municipality_size_id', $municipalitySizeId)
- ->where('is_renewal', false)
- ->where('start', '<=', $contractMonth)
- ->where(function ($q) use ($contractMonth) {
- $q->whereNull('end')->orWhere('end', '>=', $contractMonth);
- })
- ->orderBy('start')
- ->first();
- if (! $bracket) {
- throw ValidationException::withMessages([
- 'municipality_size_id' => 'Não foi encontrada faixa de royalties para o porte e mês de contrato informados.',
- ]);
- }
- return $bracket;
- }
- private function resolveFnmPercentage(int $contractMonth): float
- {
- return $contractMonth <= self::EXEMPT_THRESHOLD_MONTH ? 0.0 : self::FNM_BRACKET_PERCENTAGE;
- }
- private function resolveDueDate(?FranchiseeContract $contract, Carbon $referenceDate): Carbon
- {
- $dueDay = (int) ($contract?->invoice_due_date ?? 10);
- $dueDay = max(1, min(28, $dueDay));
- return $referenceDate->copy()->addMonthNoOverflow()->day($dueDay);
- }
- private function existingReceivable(int $unitId, int $contractMonth): bool
- {
- return TbrCalculation::where('unit_id', $unitId)
- ->where('contract_month_reference', $contractMonth)
- ->where('receivable_generated', true)
- ->exists();
- }
- }
|