|
|
@@ -234,11 +234,21 @@ private function buildPreview(FranchiseeContract $contract, int $referenceYear,
|
|
|
$contractMonth = $this->resolveContractMonth($contract->start_date, $referenceYear, $referenceMonth);
|
|
|
$municipalitySizeId = (int) $contract->municipality_size_id;
|
|
|
|
|
|
- $royaltiesBracket = $this->findRoyaltiesBracket($municipalitySizeId, $contractMonth);
|
|
|
- $fnmPercentage = $this->resolveFnmPercentage($contractMonth);
|
|
|
+ // 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((float) $royaltiesBracket->tbr_percentage * $tbrValue, 2);
|
|
|
+ $royaltiesBracketValue = round($royaltiesBracketPercentage * $tbrValue, 2);
|
|
|
$fnmBracketValue = round($fnmPercentage * $tbrValue, 2);
|
|
|
$maintenanceBracketValue = round($maintenancePercentage * $tbrValue, 2);
|
|
|
|
|
|
@@ -246,18 +256,13 @@ private function buildPreview(FranchiseeContract $contract, int $referenceYear,
|
|
|
$fnmEffectiveValue, $fnmEffectivePercentage] = $this->resolveEffectiveValues(
|
|
|
$contractMonth,
|
|
|
$revenueValue,
|
|
|
- (float) $royaltiesBracket->tbr_percentage,
|
|
|
+ $royaltiesBracketPercentage,
|
|
|
$royaltiesBracketValue,
|
|
|
$fnmPercentage,
|
|
|
$fnmBracketValue,
|
|
|
);
|
|
|
|
|
|
- // Respeita os flags de cobrança da unidade: se desativado, zera o respectivo
|
|
|
- // componente e ele não entra no total. 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;
|
|
|
-
|
|
|
+ // Cobrança desligada: zera o respectivo componente (fora do total).
|
|
|
if (!$chargeRoi) {
|
|
|
$royaltiesEffectiveValue = 0.0;
|
|
|
$royaltiesEffectivePercentage = 0.0;
|
|
|
@@ -286,8 +291,8 @@ private function buildPreview(FranchiseeContract $contract, int $referenceYear,
|
|
|
'tbr_value' => $tbrValue,
|
|
|
'municipality_size_id' => $municipalitySizeId,
|
|
|
'municipality_size_name' => $contract->municipalitySize?->description,
|
|
|
- 'royalties_bracket_id' => $royaltiesBracket->id,
|
|
|
- 'royalties_bracket_percentage' => (float) $royaltiesBracket->tbr_percentage,
|
|
|
+ 'royalties_bracket_id' => $royaltiesBracket?->id,
|
|
|
+ 'royalties_bracket_percentage' => $royaltiesBracketPercentage,
|
|
|
'royalties_bracket_value' => $royaltiesBracketValue,
|
|
|
'fnm_bracket_percentage' => $fnmPercentage,
|
|
|
'fnm_bracket_value' => $fnmBracketValue,
|