Просмотр исходного кода

feat(tbr): respect unit ROI/FNM charge flags in calculation

When charge_roi/charge_fnm is off, the respective component is zeroed and excluded from the total; missing financial record defaults to charging.
ebagabee 1 месяц назад
Родитель
Сommit
b79d87fd36
1 измененных файлов с 18 добавлено и 0 удалено
  1. 18 0
      app/Services/TbrCalculationService.php

+ 18 - 0
app/Services/TbrCalculationService.php

@@ -8,6 +8,7 @@
 use App\Models\InhabitantClassification;
 use App\Models\InhabitantClassification;
 use App\Models\Tbr;
 use App\Models\Tbr;
 use App\Models\TbrCalculation;
 use App\Models\TbrCalculation;
+use App\Models\UnitFinancial;
 use Carbon\Carbon;
 use Carbon\Carbon;
 use Illuminate\Pagination\LengthAwarePaginator;
 use Illuminate\Pagination\LengthAwarePaginator;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Auth;
@@ -251,6 +252,23 @@ private function buildPreview(FranchiseeContract $contract, int $referenceYear,
             $fnmBracketValue,
             $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;
+
+        if (!$chargeRoi) {
+            $royaltiesEffectiveValue      = 0.0;
+            $royaltiesEffectivePercentage = 0.0;
+            $royaltiesAppliedCriteria     = 'nao_cobrado';
+        }
+
+        if (!$chargeFnm) {
+            $fnmEffectiveValue      = 0.0;
+            $fnmEffectivePercentage = 0.0;
+        }
+
         $maintenanceEffectiveValue      = $maintenanceBracketValue;
         $maintenanceEffectiveValue      = $maintenanceBracketValue;
         $maintenanceEffectivePercentage = $maintenancePercentage;
         $maintenanceEffectivePercentage = $maintenancePercentage;