-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathfaraid.py
More file actions
379 lines (324 loc) · 11.9 KB
/
Copy pathfaraid.py
File metadata and controls
379 lines (324 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from __future__ import annotations
from dataclasses import dataclass, field
from decimal import Decimal
from fractions import Fraction
from typing import Any, Dict, List, Optional, Tuple
from pydantic import BaseModel, Field
# ---------------------------------------------------------------------------
# Constants and shared data
# ---------------------------------------------------------------------------
DISCLAIMER = (
"This calculation follows the classical Sunni (majority) position on faraid. "
"Islamic inheritance is a serious matter with school-specific differences. "
"Please consult a qualified scholar for a final ruling."
)
# Qur'anic references for the fixed shares (furud).
# Surah an-Nisa 4:11 covers children and parents; 4:12 covers spouses;
# 4:176 covers kalala (siblings). These are the only verses that specify
# fractional shares in inheritance.
QURAN_4_11 = "Surah an-Nisa 4:11"
QURAN_4_12 = "Surah an-Nisa 4:12"
QURAN_4_176 = "Surah an-Nisa 4:176"
# Juristic principles for awl, radd, and hajb.
AWL_BASIS = "Juristic principle of awl (proportional reduction) — recognized by all four Sunni schools."
RADD_BASIS = "Juristic principle of radd (return of surplus) — majority Sunni position, excluding the spouse."
HAJB_BASIS = "Juristic principle of hajb (blocking) — a nearer relative blocks a more distant one."
# ---------------------------------------------------------------------------
# Heir definitions
# ---------------------------------------------------------------------------
# Each heir is defined by:
# - key: unique identifier used in requests
# - name: display name
# - category: 'fard' (fixed share) or 'asaba' (residuary)
# - base_share: Fraction or None for asaba
# - blocked_by: list of heir keys that block this heir (hajb)
# - radd_eligible: whether this heir can receive from radd (spouses are excluded)
HEIRS: Dict[str, Dict[str, Any]] = {
"son": {
"name": "Son",
"category": "asaba",
"base_share": None,
"blocked_by": [],
"radd_eligible": False,
},
"daughter": {
"name": "Daughter",
"category": "fard",
"base_share": Fraction(1, 2),
"blocked_by": [],
"radd_eligible": True,
},
"wife": {
"name": "Wife",
"category": "fard",
"base_share": Fraction(1, 4),
"blocked_by": [],
"radd_eligible": False,
},
"husband": {
"name": "Husband",
"category": "fard",
"base_share": Fraction(1, 2),
"blocked_by": [],
"radd_eligible": False,
},
"father": {
"name": "Father",
"category": "fard",
"base_share": Fraction(1, 6),
"blocked_by": [],
"radd_eligible": True,
},
"mother": {
"name": "Mother",
"category": "fard",
"base_share": Fraction(1, 6),
"blocked_by": [],
"radd_eligible": True,
},
"full_sister": {
"name": "Full Sister",
"category": "fard",
"base_share": Fraction(1, 2),
"blocked_by": ["son", "daughter", "father", "grandfather"],
"radd_eligible": True,
},
"full_brother": {
"name": "Full Brother",
"category": "asaba",
"base_share": None,
"blocked_by": ["son", "grandson", "father", "grandfather"],
"radd_eligible": False,
},
"paternal_grandfather": {
"name": "Paternal Grandfather",
"category": "fard",
"base_share": Fraction(1, 6),
"blocked_by": ["father"],
"radd_eligible": True,
},
"paternal_grandmother": {
"name": "Paternal Grandmother",
"category": "fard",
"base_share": Fraction(1, 6),
"blocked_by": ["mother", "father"],
"radd_eligible": True,
},
"maternal_grandmother": {
"name": "Maternal Grandmother",
"category": "fard",
"base_share": Fraction(1, 6),
"blocked_by": ["mother", "father"],
"radd_eligible": True,
},
"grandson": {
"name": "Grandson (son's son)",
"category": "asaba",
"base_share": None,
"blocked_by": ["son"],
"radd_eligible": False,
},
"granddaughter": {
"name": "Granddaughter (son's daughter)",
"category": "fard",
"base_share": Fraction(1, 2),
"blocked_by": ["son", "daughter"],
"radd_eligible": True,
},
}
# ---------------------------------------------------------------------------
# Data models
# ---------------------------------------------------------------------------
class FaraidRequest(BaseModel):
estate: Decimal = Field(..., gt=0, description="Total estate value")
heirs: List[str] = Field(..., min_length=1, description="List of heir keys")
class HeirShare(BaseModel):
heir: str
name: str
category: str
fraction: str
amount: str
basis: str
blocked_by: Optional[str] = None
class FaraidResponse(BaseModel):
estate: str
shares: List[HeirShare]
disclaimer: str
adjustments: List[str]
# ---------------------------------------------------------------------------
# Engine
# ---------------------------------------------------------------------------
@dataclass
class FaraidResult:
shares: Dict[str, Fraction]
steps: List[str]
adjustments: List[str]
def _get_heir(key: str) -> Dict[str, Any]:
if key not in HEIRS:
raise ValueError(f"Unknown heir: {key}")
return HEIRS[key]
def _apply_hajb(heirs: List[str]) -> Tuple[List[str], Dict[str, str]]:
"""Return (active_heirs, blocked_by_map)."""
active = list(heirs)
blocked_by: Dict[str, str] = {}
for heir in heirs:
for blocker in _get_heir(heir)["blocked_by"]:
if blocker in heirs:
active.remove(heir)
blocked_by[heir] = blocker
break
return active, blocked_by
def _furud_shares(active: List[str]) -> Dict[str, Fraction]:
"""Assign fixed shares to fard heirs. Asaba heirs get None."""
shares: Dict[str, Optional[Fraction]] = {}
for heir in active:
info = _get_heir(heir)
if info["category"] == "fard":
shares[heir] = info["base_share"]
else:
shares[heir] = None
return shares
def _asaba_share(active: List[str], shares: Dict[str, Optional[Fraction]]) -> Optional[Fraction]:
"""Compute the total asaba share (residue) and per-asaba fractions."""
asaba_heirs = [h for h in active if shares[h] is None]
if not asaba_heirs:
return None
fixed_sum = sum(shares[h] for h in active if shares[h] is not None)
residue = Fraction(1) - fixed_sum
if residue <= 0:
return None
return residue
def _distribute_asaba(
active: List[str], shares: Dict[str, Optional[Fraction]], residue: Fraction
) -> Dict[str, Fraction]:
"""Distribute residue among asaba heirs, male gets twice female."""
asaba_heirs = [h for h in active if shares[h] is None]
if not asaba_heirs:
return {}
# Count units: male = 2, female = 1
units = 0
for h in asaba_heirs:
if "son" in h or "brother" in h or "father" in h or "grandfather" in h:
units += 2
else:
units += 1
result = {}
for h in asaba_heirs:
if "son" in h or "brother" in h or "father" in h or "grandfather" in h:
result[h] = residue * Fraction(2, units)
else:
result[h] = residue * Fraction(1, units)
return result
def _apply_awl(shares: Dict[str, Fraction]) -> Tuple[Dict[str, Fraction], bool]:
"""Scale down shares if they sum to more than 1."""
total = sum(shares.values())
if total <= 1:
return shares, False
factor = Fraction(1) / total
return {k: v * factor for k, v in shares.items()}, True
def _apply_radd(
shares: Dict[str, Fraction], active: List[str]
) -> Tuple[Dict[str, Fraction], bool]:
"""Return surplus to eligible sharers, excluding spouses."""
total = sum(shares.values())
if total >= 1:
return shares, False
surplus = Fraction(1) - total
eligible = [h for h in active if _get_heir(h)["radd_eligible"]]
if not eligible:
return shares, False
eligible_total = sum(shares[h] for h in eligible)
if eligible_total == 0:
return shares, False
for h in eligible:
shares[h] += surplus * (shares[h] / eligible_total)
return shares, True
def distribute(estate: Decimal, heirs: List[str]) -> FaraidResult:
"""Compute faraid shares for the given heirs and estate."""
if estate <= 0:
raise ValueError("Estate must be positive")
if not heirs:
raise ValueError("At least one heir required")
steps: List[str] = []
adjustments: List[str] = []
# 1. Hajb
active, blocked_by = _apply_hajb(heirs)
for heir, blocker in blocked_by.items():
steps.append(f"{HEIRS[heir]['name']} blocked by {HEIRS[blocker]['name']} ({HAJB_BASIS})")
adjustments.append(f"hajb: {HEIRS[heir]['name']} blocked by {HEIRS[blocker]['name']}")
# 2. Furud
shares = _furud_shares(active)
for heir in active:
if shares[heir] is not None:
basis = _basis_for(heir)
steps.append(f"{HEIRS[heir]['name']} gets {shares[heir]} ({basis})")
# 3. Asaba
residue = _asaba_share(active, shares)
asaba_shares: Dict[str, Fraction] = {}
if residue is not None:
asaba_shares = _distribute_asaba(active, shares, residue)
for heir, frac in asaba_shares.items():
steps.append(f"{HEIRS[heir]['name']} gets residue {frac} as asaba")
# Combine
final_shares: Dict[str, Fraction] = {}
for heir in active:
if shares[heir] is not None:
final_shares[heir] = shares[heir]
elif heir in asaba_shares:
final_shares[heir] = asaba_shares[heir]
else:
final_shares[heir] = Fraction(0)
# 4. Awl
final_shares, awl_applied = _apply_awl(final_shares)
if awl_applied:
steps.append(f"Awl applied: shares scaled down proportionally ({AWL_BASIS})")
adjustments.append("awl: over-subscription, shares reduced proportionally")
# 5. Radd
final_shares, radd_applied = _apply_radd(final_shares, active)
if radd_applied:
steps.append(f"Radd applied: surplus returned to eligible sharers ({RADD_BASIS})")
adjustments.append("radd: surplus returned to sharers, spouse excluded")
# Add blocked heirs with zero
for heir in blocked_by:
final_shares[heir] = Fraction(0)
return FaraidResult(shares=final_shares, steps=steps, adjustments=adjustments)
def _basis_for(heir: str) -> str:
"""Return the fiqh basis for a fard heir."""
if heir in ["wife", "husband"]:
return QURAN_4_12
if heir in ["full_sister", "full_brother"]:
return QURAN_4_176
return QURAN_4_11
# ---------------------------------------------------------------------------
# Router and endpoint
# ---------------------------------------------------------------------------
from fastapi import APIRouter, HTTPException
router = APIRouter(tags=["faraid"])
@router.post("/faraid", response_model=FaraidResponse)
def faraid_endpoint(request: FaraidRequest) -> FaraidResponse:
try:
result = distribute(request.estate, request.heirs)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
estate_frac = Fraction(request.estate)
shares = []
for heir, frac in result.shares.items():
info = _get_heir(heir)
amount = estate_frac * frac
shares.append(
HeirShare(
heir=heir,
name=info["name"],
category=info["category"],
fraction=str(frac),
amount=str(amount),
basis=_basis_for(heir) if frac > 0 else HAJB_BASIS,
blocked_by=result.steps[0] if heir in result.shares and frac == 0 else None,
)
)
return FaraidResponse(
estate=str(request.estate),
shares=shares,
disclaimer=DISCLAIMER,
adjustments=result.adjustments,
)