Employee Manager Role for only Active Employees

Good afternoon. Tell me, please.
The following role exists - Base roles\Employee Managers.
It is necessary that this role is displayed only for working employees.
The following problem arose. When an employee quits, his manager is displayed in the manager field. Unfortunately, this breaks the logic of AD and other systems. Since the manager displays dismissed employees. Thus, dead souls are formed.
Please tell me how can I fix this.....

i found this script

UID_Person in (select p.UID_PersonHead
from HelperHeadPerson p
where p.XOrigin > 0
union all
select o.UID_PersonHead
from HelperHeadOrg o left outer join BaseTree b on o.UID_Org=b.UID_Org and b.UID_OrgRoot='QER-V-AERole'
where o.XOrigin > 0 and b.UID_Org is null -- a manager of AERole should not be employee manager
)
and isnull(IsInActive, 0) = 0