
    hjt                    v    d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 dZ
h dZdd	hZdd
ddZddddZdS )     )annotationsN)date)
Connection)utc_nowinvestment_performance_scope_v1>   crypto_portfoliopostfinance_etrading_cashpostfinance_etrading_depot canonical_truewealth_total_value#not_in_investment_performance_scopepostfinance_efinance_control)classified_atconnr   
account_idstrincludedboolclassification_rolesourcenoter   
str | Nonereturnc                  |r|t           vrt          d          |s|t          vrt          d          |                     d|f                                          st          d          |                     d|f                                          }t          |          |t          f}|rt          |          |k    rdS |pt                      }	t          |                     d|f                                          d                   }
|rt          |          n|
d	d	f}| d
t           d
t          |           d
| d
|d
|	 }dt          j
        |                    d                                                    d	d         z   }|                     d||	|dd|t          j        d|
idd          t          j        t          |          |ddd          |d|	f           |                     d|t          |          |t          ||	f           |                     dt          |          |f           dS )zKApply one explicit, audited scope classification; equal repeats are no-ops.z:Performance inclusion requires an approved investment rolez>Performance exclusion requires an approved non-investment rolez)SELECT 1 FROM accounts WHERE account_id=?zUnknown accountzySELECT included,classification_role,decision_version
           FROM performance_scope_classifications WHERE account_id=?Fz<SELECT performance_included FROM accounts WHERE account_id=?r   N|audit_perf_scope_utf-8   INSERT INTO audit_log(
             audit_id,timestamp,source,action,entity_type,entity_id,old_values_json,
             new_values_json,user_text_note,created_by,created_at
           ) VALUES (?,?,?,?,?,?,?,?,?,?,?)performance_scope_classified performance_scope_classificationperformance_included,:T
separators	sort_keys)r!   r   systema  INSERT INTO performance_scope_classifications(
             account_id,included,classification_role,decision_version,audit_id,classified_at)
           VALUES (?,?,?,?,?,?)
           ON CONFLICT(account_id) DO UPDATE SET
             included=excluded.included,
             classification_role=excluded.classification_role,
             decision_version=excluded.decision_version,
             audit_id=excluded.audit_id,
             classified_at=excluded.classified_atz=UPDATE accounts SET performance_included=? WHERE account_id=?)INCLUDED_ROLES
ValueErrorEXCLUDED_ROLESexecutefetchoneintDECISION_VERSIONtupler   hashlibsha256encode	hexdigestjsondumps)r   r   r   r   r   r   r   currenttargetnowold_includedpreviousidentityaudit_ids                 /home/agent/.hermes/worktrees/FinanceManager-sprint16-smart-household-inbox-real-data-onboarding-v2/src/jarvis_finance/services/performance_scope.py$set_performance_scope_classificationr?      s     W'~==UVVV [+>AAYZZZ<<Cj]SS\\^^ ,*+++ll	H	  hjj	 
 (mm02BCF 5>>V++u

$799CSV`Ubccllnnopq L ")HuW~~~|T4.HH 	 	( 	 	3x== 	 	;N 	 		 		 	  #W^HOOG4L4L%M%M%W%W%Y%YZ][]Z]%^^HLL	/
 *.J.=*`deeeJ),XObcc%  
 	
  . 	LL	5 
S]]$79I8UXY   	LLG	X
#   4    )recorded_atcoverage_fromcoverage_tostatusrA   c                   	 t          j        |          }t          j        |          }	n"# t          $ r}
t          d          |
d}
~
ww xY w||	k    rt          d          |dvrt          d          |                                st          d          |                     d|t
          f                                          st          d          |                     d	|f                                          }||||f}|rt          |          |k    rd
S |pt                      }| d|d|rt          |          ndd| }dt          j
        |                    d                                                    dd         z   }||||d}|                     d|||dd|t          j        |rt          |          ni dd          t          j        |dd          |d|f           |                     d|||||||f           dS )zKRecord explicit, audited evidence about external-cashflow history coverage.z$Cashflow coverage requires ISO datesNz/Cashflow coverage start must not follow its end>   partialcompleteunavailablez$Unsupported cashflow coverage statusz#Cashflow coverage requires a sourcezsSELECT 1 FROM performance_scope_classifications
           WHERE account_id=? AND included=1 AND decision_version=?z:Cashflow coverage requires an included performance accountzoSELECT coverage_from,coverage_to,status,source
           FROM performance_cashflow_coverage WHERE account_id=?Fr   audit_perf_cashflow_r   r   )rB   rC   rD   r   r   &performance_cashflow_coverage_recordedperformance_cashflow_coverager"   Tr%   r(   a  INSERT INTO performance_cashflow_coverage(
             account_id,coverage_from,coverage_to,status,source,audit_id,recorded_at)
           VALUES (?,?,?,?,?,?,?)
           ON CONFLICT(account_id) DO UPDATE SET
             coverage_from=excluded.coverage_from,
             coverage_to=excluded.coverage_to,
             status=excluded.status,
             source=excluded.source,
             audit_id=excluded.audit_id,
             recorded_at=excluded.recorded_at)r   fromisoformatr*   stripr,   r/   r-   r0   r   r1   r2   r3   r4   r5   r6   dict)r   r   rB   rC   rD   r   r   rA   startendexcr7   r8   r9   r<   r=   
new_valuess                    r>   !set_performance_cashflow_coveragerS   d   s   J"=11 -- J J J?@@cIJs{{JKKK;;;?@@@<<>> @>???<<	G	%&  hjj	W
 UVVVll	D	  hjj	 
 [&&9F 5>>V++u

"CWWvWWG*M%...WWRUWWH%xw7O7O(P(P(Z(Z(\(\]`^`]`(aaH&"	 J 	LL	/
 4+J7tG}}}RJZ^___JzjDIII	
  & 	LL		1 
]K3O   4s   (+ 
A
AA
)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   rB   r   rC   r   rD   r   r   r   r   r   rA   r   r   r   )
__future__r   r1   r5   datetimer   sqlite3r   !jarvis_finance.storage.migrationsr   r/   r)   r+   r?   rS    r@   r>   <module>rY      s    " " " " " "               5 5 5 5 5 54    *" !%J J J J J Jl #N N N N N N N Nr@   