package com.lincfusion.udyamlinc.apex;
import com.lincfusion.udyamlinc.common.ResourceNotFoundException;
import com.lincfusion.udyamlinc.enterprise.Enterprise;
import com.lincfusion.udyamlinc.enterprise.EnterpriseRepository;
import com.lincfusion.udyamlinc.institution.*;
import com.lincfusion.udyamlinc.master.District;
import com.lincfusion.udyamlinc.master.DistrictRepository;
import com.lincfusion.udyamlinc.security.TenantContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
/**
* The supervisory tier between head office and a partner bank.
*
*
Every rollup here resolves through {@code branch → district → state →
* office} rather than through anything stored on the bank. Six of the eight
* partner banks are RRBs and state-bound, so a bank-level office column would
* look right today and be wrong the moment the SFB opens a branch in a second
* state.
*
*
Nothing on this surface returns a borrower. An office is NABARD, and the
* reason the apex tier can be trusted with a national portfolio view at all is
* that it can never reach a customer — extending that view to a Regional Office
* must not quietly extend the entitlement with it.
*/
@Service
@RequiredArgsConstructor
public class RegionalOfficeService {
private final RegionalOfficeRepository officeRepository;
private final RegionalOfficeStateRepository officeStateRepository;
private final InstitutionRepository institutionRepository;
private final BranchRepository branchRepository;
private final DistrictRepository districtRepository;
private final EnterpriseRepository enterpriseRepository;
@Value("${udyam.apex.min-cell-size:5}")
private int minCellSize;
@Value("${udyam.apex.min-cell-size.regional-office:5}")
private int minCellSizeRegional;
/** The threshold that applies to this caller — see the note in the properties file. */
private int minCellFor(TenantContext tenant) {
return tenant != null && tenant.isRegionalOffice() ? minCellSizeRegional : minCellSize;
}
/**
* Offices visible to this caller: all of them for head office, exactly one
* for a Regional Office user.
*
* @param populatedOnly hide offices with no portfolio. Nineteen of the
* twenty-five are empty in the pilot, which is truthful
* but makes a screen look broken, so the console can ask
* for either.
*/
public List