Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions ramz_hr/countries/saudi/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,33 @@ def validate_employee(doc, method=None):
compute_probation_end_date(doc)


def _get_iqama_number(doc):
"""Get Iqama number, supporting both custom_ prefixed and standard field names."""
return (doc.get("custom_iqama_number") or doc.get("iqama_number") or "").strip()


def _get_iqama_expiry(doc):
"""Get Iqama expiry date, supporting both custom_ prefixed and standard field names."""
return doc.get("custom_iqama_expiry") or doc.get("iqama_expiry_date")


def _get_nationality(doc):
"""Get nationality, supporting both custom_ prefixed and standard field names."""
return (doc.get("custom_nationality") or doc.get("nationality") or "").strip()


def validate_iqama_format(doc):
iqama = (doc.get("custom_iqama_number") or "").strip()
iqama = _get_iqama_number(doc)
if not iqama:
return
if not IQAMA_RE.match(iqama):
frappe.throw("Iqama / National ID must be exactly 10 digits.")


def validate_iqama_expiry_presence(doc):
nationality = (doc.get("custom_nationality") or "").strip()
nationality = _get_nationality(doc)
if nationality and nationality != "Saudi Arabia":
if not doc.get("custom_iqama_expiry"):
if not _get_iqama_expiry(doc):
frappe.throw("Iqama Expiry is required for non-Saudi employees.")


Expand Down
16 changes: 16 additions & 0 deletions ramz_hr/tests/test_employee_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,26 @@ def test_iqama_expiry_required_for_non_saudi(self):
with self.assertRaises(frappe.ValidationError):
validate_iqama_expiry_presence(doc)

def test_iqama_expiry_required_for_non_saudi_with_staff_pro_fields(self):
"""Test with Staff Pro field names (no custom_ prefix)."""
doc = _Stub(nationality="India", iqama_expiry_date=None)
with self.assertRaises(frappe.ValidationError):
validate_iqama_expiry_presence(doc)

def test_iqama_expiry_passes_with_staff_pro_fields(self):
"""Test with Staff Pro field names when expiry is provided."""
doc = _Stub(nationality="India", iqama_expiry_date="2026-12-31")
validate_iqama_expiry_presence(doc) # should not raise

def test_iqama_expiry_optional_for_saudi(self):
doc = _Stub(custom_nationality="Saudi Arabia", custom_iqama_expiry=None)
validate_iqama_expiry_presence(doc) # not raised

def test_iqama_expiry_optional_for_saudi_with_staff_pro_fields(self):
"""Test with Staff Pro field names for Saudi employee."""
doc = _Stub(nationality="Saudi Arabia", iqama_expiry_date=None)
validate_iqama_expiry_presence(doc) # not raised

def test_iban_valid(self):
doc = _Stub(custom_iban="SA" + "1" * 22)
validate_iban_format(doc)
Expand Down
Loading