@@ -1852,6 +1852,46 @@ def aggregate_doc_insights_for_questionnaire(qid: int) -> dict:
18521852 itr = {}
18531853
18541854 per_doc_extracts = []
1855+
1856+ # Extract full ITR data from metadata_json (includes deductions_claimed, income_sources, etc.)
1857+ for upload in uploads :
1858+ doc_type = (upload .get ("doc_type" ) or upload ["doc_type" ] if isinstance (upload , dict ) else "" ).lower ()
1859+ if "itr" in doc_type :
1860+ metadata_json = upload .get ("metadata_json" ) or (upload ["metadata_json" ] if isinstance (upload , dict ) else None )
1861+ if metadata_json :
1862+ try :
1863+ metadata = json .loads (metadata_json ) if isinstance (metadata_json , str ) else metadata_json
1864+ # Merge full ITR extracted data into itr dict
1865+ # Include deductions_claimed, income_sources, tax_computation, etc.
1866+ if metadata .get ("deductions_claimed" ):
1867+ itr ["deductions_claimed" ] = metadata ["deductions_claimed" ]
1868+ if metadata .get ("income_sources" ):
1869+ itr ["income_sources" ] = metadata ["income_sources" ]
1870+ if metadata .get ("tax_computation" ):
1871+ itr ["tax_computation" ] = metadata ["tax_computation" ]
1872+ if metadata .get ("carry_forward_losses" ):
1873+ itr ["carry_forward_losses" ] = metadata ["carry_forward_losses" ]
1874+ if metadata .get ("assets_and_liabilities" ):
1875+ itr ["assets_and_liabilities" ] = metadata ["assets_and_liabilities" ]
1876+ # Also get numeric fields from metadata if not already from metrics
1877+ if metadata .get ("gross_total_income" ) and not itr .get ("gross_total_income" ):
1878+ try :
1879+ itr ["gross_total_income" ] = float (metadata ["gross_total_income" ])
1880+ except (ValueError , TypeError ):
1881+ pass
1882+ if metadata .get ("taxable_income" ) and not itr .get ("taxable_income" ):
1883+ try :
1884+ itr ["taxable_income" ] = float (metadata ["taxable_income" ])
1885+ except (ValueError , TypeError ):
1886+ pass
1887+ if metadata .get ("total_tax_paid" ) and not itr .get ("total_tax_paid" ):
1888+ try :
1889+ itr ["total_tax_paid" ] = float (metadata ["total_tax_paid" ])
1890+ except (ValueError , TypeError ):
1891+ pass
1892+ except Exception as e :
1893+ print (f"Error parsing ITR metadata: { e } " )
1894+ continue
18551895
18561896 for did in doc_ids :
18571897 # 1) Deterministic summaries from indexed sections/tables
@@ -2851,6 +2891,7 @@ def _build_client_facts(q: dict, analysis: dict, doc_insights=None) -> dict:
28512891 "analysis" : analysis ,
28522892 "retirement_planning" : retirement_planning ,
28532893 "term_insurance" : term_insurance ,
2894+ "itr" : di .get ("itr" ), # Full ITR data for tax optimization section
28542895 }
28552896 return facts
28562897
0 commit comments