
    Ujy                        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 dl
mZ  ed           G d	 d
                      ZddddZdS )    )annotationsN)	dataclass)
Connection)record_audit_event)utc_nowT)frozenc                  $    e Zd ZU ded<   ded<   dS )ExecutionImportResultstrtransaction_idboolimportedN)__name__
__module____qualname____annotations__     _/home/agent/.hermes/worktrees/FinanceManager-sprint23.1/src/jarvis_finance/imports/execution.pyr
   r
      s%         NNNNNr   r
   system)
created_byconnr   execution_plan_idr   noter   returnc                D   |                                 st          d          |                     d|f                                          }|t          d|           |d         dk    rt          d|           |d         dk    s|d	         rt          d
|           |                     d|d         |d         |d         f                                          }|rt          d          t	          j        |d         pd          g d}fd|D             }|r%t          dd                    |          z             dt          t          j	                              z   }t                      }	                    d          dk    sd|d         pdv rdnd}
|                     d|dd         d          d!         d!         d"                             d#          d d d d                     d$                              d%                              d&          pd'                    dd                              d(          d d d d)|d         |d         |d         d*|
||	|	f           |                     d+||	|	||f           |                     d,||	|d-         f           t          | d.d/d0|i ||d-         d|
d1|d2|3
  
         t          | d4d5d6|d-         d7d8id|d9|d2|3
  
         |                                  t          |d2:          S );Nz execution import requires a notezESELECT * FROM broker_import_execution_plans WHERE execution_plan_id=?zexecution plan not found: payload_statusreadyzexecution plan is not ready: execution_statusr   r   z!execution plan already imported: z
        SELECT transaction_id FROM transactions
        WHERE source_type='broker_import_reviewed_snapshot'
          AND (source_id=? OR external_transaction_id=? OR row_hash=?)
        LIMIT 1
        r   source_row_hashz2execution plan or source row hash already importedplanned_write_summary_jsonz{})
account_idinstrument_id
trade_datequantitycurrency_originalc                >    g | ]}                     |          |S r   )get).0keypayloads     r   
<listcomp>z1execute_initial_snapshot_plan.<locals>.<listcomp>)   s*    ???sgkk#.>.>?s???r   z+execution payload missing required fields: z, ztx-	fx_statusmissingcost_basis_uncertainpayload_quality_flags_json warningoka@  
        INSERT INTO transactions(
          transaction_id, transaction_type, account_id, instrument_id, trade_date, settlement_date,
          quantity, price_original, gross_amount_original, fee_original, tax_original, net_amount_original,
          currency_original, fx_rate_to_chf, fx_source, fx_status, gross_amount_chf, fee_chf, tax_chf, net_amount_chf,
          source_type, source_id, external_transaction_id, row_hash, is_confirmed, quality_status, notes, created_at, updated_at
        ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        initial_position_snapshotr"   r#   r$   r%   price_originalr&   fx_rate_to_chf	fx_sourcemissing_in_sourcegross_amount_chfbroker_import_reviewed_snapshot   zUPDATE broker_import_execution_plans SET execution_status='imported', transaction_id=?, executed_at=?, updated_at=?, notes=? WHERE execution_plan_id=?ztUPDATE broker_import_review_items SET review_status='imported', reviewer_note=?, updated_at=? WHERE review_item_id=?review_item_idbroker_import_execution_planexecute_mini_importtransaction)r   r<   transaction_typequality_statusT)	sourceactionentity_type	entity_id
old_values
new_valuesuser_text_note	confirmedr   manual_review_queuemark_review_item_importedbroker_import_review_itemreview_statusresolved)rM   r   )r   r   )strip
ValueErrorexecutefetchonejsonloadsjoinr   uuiduuid4r   r(   r   commitr
   )r   r   r   r   planexistingrequiredr.   tx_idnowqualityr+   s              @r   execute_initial_snapshot_planr_      s_   ::<< =;<<<<<_bsauvv  B  BD|I6GIIJJJ((L9JLLMMM:--6F1G-P=NPPQQQ||	 
!	"D)<$=tDU?VW  hjj   OMNNNj:;CtDDG]]]H????h???G ]FSZI[I[[\\\C
%%%E
))C#KK44	AAE[`d  fB  aC  aI  GI  FJ  FJii  QUGLL	 .0EwG_ahiuavx  AM  yNJ-=!>!>dDRVKK+,,gkk:J.K.KW[[YdMeMeM|i|  F  J  J  KV  Xa  b  b  dk  do  do  pB  dC  dC  EI  KO  QU-t4G/H$ObJceij{e|~  BI#s	
  " 	LL 	a	S$ 12   	LL~	sD)*+   -$!):dScNd  {V  jq  r  r    $*/'(#Z0%/5II    	KKMMM EEEEr   )
r   r   r   r   r   r   r   r   r   r
   )
__future__r   rS   rV   dataclassesr   sqlite3r   jarvis_finance.audit.logr   jarvis_finance.imports.commonr   r
   r_   r   r   r   <module>re      s    " " " " " "   ! ! ! ! ! !       7 7 7 7 7 7 1 1 1 1 1 1 $       
 mu OF OF OF OF OF OF OF OFr   