Repository: PolicyEngine/policyengine-us (language: python)

Issue:
North Carolina Military Retirement Deduction 2022

![NC militray retirement](https://github.com/user-attachments/assets/e7edeede-7aa2-4806-b70d-2c60c75bcca0)

[Reference](https://www.ncdor.gov/2022-tax-law-changespdf-0/open)



Discussion / hints:

Fix patch:
diff --git a/changelog_entry.yaml b/changelog_entry.yaml
index e69de29bb2d..b6abeb3cc2a 100644
--- a/changelog_entry.yaml
+++ b/changelog_entry.yaml
@@ -0,0 +1,4 @@
+- bump: minor
+  changes:
+    added:
+    - North Carolina military retirement deduction.
diff --git a/policyengine_us/parameters/gov/states/nc/tax/income/deductions/deductions.yaml b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/deductions.yaml
index f356f8839a0..cb73add2d88 100644
--- a/policyengine_us/parameters/gov/states/nc/tax/income/deductions/deductions.yaml
+++ b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/deductions.yaml
@@ -6,6 +6,12 @@ values:
     - nc_child_deduction
     - taxable_social_security
 
+  2022-01-01:
+    - us_govt_interest
+    - nc_standard_or_itemized_deductions
+    - nc_child_deduction
+    - taxable_social_security
+    - nc_military_retirement_deduction
 
 metadata:
   unit: list
diff --git a/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/fraction.yaml b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/fraction.yaml
new file mode 100644
index 00000000000..eb910c64f60
--- /dev/null
+++ b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/fraction.yaml
@@ -0,0 +1,13 @@
+description: North Carolina subtracts this fraction of military retirement benefits from federal adjusted gross income.
+values:
+  2022-01-01: 1
+
+metadata:
+  label: North Carolina military retirement subtraction fraction
+  unit: /1
+  period: year
+  reference: 
+  - title: 2022 North Carolina Form D-401, Line 12
+    href: https://www.ncdor.gov/2022-d-401-individual-income-tax-instructions/open#page=18
+  - title: Code of North Carolina | § 105-153.5. Modifications to adjusted gross income. (b)(5a)
+    href: https://law.justia.com/codes/north-carolina/chapter-105/article-4/section-105-153-5/
diff --git a/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/minimum_years.yaml b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/minimum_years.yaml
new file mode 100644
index 00000000000..cfacf45aefd
--- /dev/null
+++ b/policyengine_us/parameters/gov/states/nc/tax/income/deductions/military_retirement/minimum_years.yaml
@@ -0,0 +1,13 @@
+description: North Carolina requires a filer to have served for at least this many years in the military to qualify for specific military retirement deductions, given the filer is not medically retired.
+values:
+  2022-01-01: 20
+
+metadata:
+  label: North Carolina minimum years serving in the military
+  unit: years
+  period: year
+  reference: 
+  - title: 2022 North Carolina Form D-401, Line 12
+    href: https://www.ncdor.gov/2022-d-401-individual-income-tax-instructions/open#page=18
+  - title: Code of North Carolina | § 105-153.5. Modifications to adjusted gross income. (b)(5a)
+    href: https://law.justia.com/codes/north-carolina/chapter-105/article-4/section-105-153-5/
diff --git a/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.py b/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.py
new file mode 100644
index 00000000000..674710caa27
--- /dev/null
+++ b/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.py
@@ -0,0 +1,23 @@
+from policyengine_us.model_api import *
+
+
+class nc_military_retirement_deduction(Variable):
+    value_type = float
+    entity = TaxUnit
+    label = "North Carolina military retirement deduction"
+    unit = USD
+    definition_period = YEAR
+    defined_for = "nc_military_retirement_deduction_eligible"
+    reference = (
+        "https://www.ncdor.gov/2022-d-401-individual-income-tax-instructions/open#page=18",
+        "https://law.justia.com/codes/north-carolina/chapter-105/article-4/section-105-153-5/",
+    )
+
+    def formula(tax_unit, period, parameters):
+        p = parameters(
+            period
+        ).gov.states.nc.tax.income.deductions.military_retirement
+        military_retirement_benefits = add(
+            tax_unit, period, ["military_retirement_pay"]
+        )
+        return military_retirement_benefits * p.fraction
diff --git a/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.py b/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.py
new file mode 100644
index 00000000000..3ac5c741a96
--- /dev/null
+++ b/policyengine_us/variables/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.py
@@ -0,0 +1,25 @@
+from policyengine_us.model_api import *
+
+
+class nc_military_retirement_deduction_eligible(Variable):
+    value_type = bool
+    entity = Person
+    label = "North Carolina military retirement deduction eligible"
+    definition_period = YEAR
+    defined_for = StateCode.NC
+    reference = (
+        "https://www.ncdor.gov/2022-d-401-individual-income-tax-instructions/open#page=18",
+        "https://law.justia.com/codes/north-carolina/chapter-105/article-4/section-105-153-5/",
+    )
+
+    def formula(person, period, parameters):
+        p = parameters(
+            period
+        ).gov.states.nc.tax.income.deductions.military_retirement
+        is_medically_retired = person(
+            "is_permanently_disabled_veteran", period
+        )
+        served_minimum_years = (
+            person("years_in_military", period) >= p.minimum_years
+        )
+        return is_medically_retired | served_minimum_years
diff --git a/policyengine_us/variables/household/demographic/person/years_in_military.py b/policyengine_us/variables/household/demographic/person/years_in_military.py
new file mode 100644
index 00000000000..ddb16dee68a
--- /dev/null
+++ b/policyengine_us/variables/household/demographic/person/years_in_military.py
@@ -0,0 +1,8 @@
+from policyengine_us.model_api import *
+
+
+class years_in_military(Variable):
+    value_type = int
+    entity = Person
+    label = "Years served in military"
+    definition_period = YEAR


Test patch:
diff --git a/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.yaml b/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.yaml
new file mode 100644
index 00000000000..9ef643d5c27
--- /dev/null
+++ b/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction.yaml
@@ -0,0 +1,17 @@
+- name: Military retirement pay, not eligible
+  period: 2022
+  input:
+     military_retirement_pay: 30_000
+     nc_military_retirement_deduction_eligible: true
+     state_code: NC
+  output:
+    nc_military_retirement_deduction: 30_000
+
+- name: Military retirement pay, is eligible
+  period: 2022
+  input:
+     military_retirement_pay: 30_000
+     nc_military_retirement_deduction_eligible: false
+     state_code: NC
+  output:
+    nc_military_retirement_deduction: 0
diff --git a/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.yaml b/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.yaml
new file mode 100644
index 00000000000..9b8e8664e12
--- /dev/null
+++ b/policyengine_us/tests/policy/baseline/gov/states/nc/tax/income/deductions/military_retirement/nc_military_retirement_deduction_eligible.yaml
@@ -0,0 +1,26 @@
+- name: Is medically retired, is not years eligible
+  period: 2022
+  input:
+    is_permanently_disabled_veteran: true
+    years_in_military: 19
+    state_code: NC
+  output:
+    nc_military_retirement_deduction_eligible: true
+
+- name: Is not medically retired, is years eligible
+  period: 2022
+  input:
+    is_permanently_disabled_veteran: false
+    years_in_military: 20
+    state_code: NC
+  output:
+    nc_military_retirement_deduction_eligible: true
+
+- name: Not medically retired, not years eligible
+  period: 2022
+  input:
+    is_permanently_disabled_veteran: false
+    years_in_military: 19
+    state_code: NC
+  output:
+    nc_military_retirement_deduction_eligible: false