"""Task to match contract terms to sales and insert into snowflake staging table.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from tasks.accounting_run_calculate import helpers as shared_helpers from tasks.accounting_run_calculate_nr import helpers from templates.accounting_run_calculate.snowflake_match_contract_terms_nr import \ insert_contract_transaction_staging_nr_template from templates.accounting_run_calculate.snowflake_match_contract_terms_nr import \ insert_contributor_only_contract_transaction_staging_nr_template def match_contract_terms_to_sales_task( dag_run: dict, is_contributor_only: bool = False, **kwargs ) -> None: """Match denormalized contract attachments and term conditions to sales. Contract attachments are contributor_ids, contribution_ids. Contract term conditions are countries, stores, transaction_types Inserts into CONTRACT_TRANSACTION_NR_STAGING. Args: dag_run (dict): config of the DAG this task belongs to is_contributor_only (bool): whether to match contributor-only data kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) accounting_run_id = event.target_id accounting_period, _ = shared_helpers.get_event_records(accounting_run_id) accounting_period_id = accounting_period.get('accounting_period_id') sales_file_ids = shared_helpers.get_sales_file_ids(accounting_period_id) hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) if is_contributor_only: insert_statement = \ insert_contributor_only_contract_transaction_staging_nr_template().render( accounting_run_id=accounting_run_id, sales_file_ids=sales_file_ids, schema=config.OWS_ENV ) else: insert_statement = insert_contract_transaction_staging_nr_template().render( accounting_run_id=accounting_run_id, sales_file_ids=sales_file_ids, schema=config.OWS_ENV ) hook.run(insert_statement, autocommit=True)