"""Task to make make unallocated sales re-eligible (by "rolling them over").""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from lib.constants import CONTRACT_TYPES from lib.utils.ows import get_accounting_period_details from tasks.accounting_period_close.helpers import get_event_from_params from templates.accounting_period_close.snowflake_rollover_unallocated_sales_distro \ import insert_stmt_db_sales_distro_staging from templates.accounting_period_close.snowflake_rollover_unallocated_sales_nr \ import insert_stmt_db_sales_nr_staging def snowflake_rollover_unallocated_sales_task(dag_run: dict, **kwargs): """Re-insert unallocated sales to make them re-eligible for future calculations. - Find transactions in the accounting period that have not been matched to contracts - Re-insert these transactions into STMT_DB_SALES_DISTRO_STAGING or STMT_DB_SALES_NR_STAGING (depending on the accounting period's contract_type). - This creates new TXN_IDs (which act as primary keys in snowflake). - In the future, when a user clicks "get eligible sales" in the Abacus UI, these sales will now be included. Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments """ event = get_event_from_params(dag_run, **kwargs) accounting_period_id = event.target_id accounting_period = get_accounting_period_details(accounting_period_id) contract_type = accounting_period.get('contract_type') statement_period_id = accounting_period.get('statement_period_id') hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) if contract_type == CONTRACT_TYPES.DISTRIBUTION: insert_statement = insert_stmt_db_sales_distro_staging().render( accounting_period_id=accounting_period_id, statement_period_id=statement_period_id, schema=config.OWS_ENV ) elif contract_type == CONTRACT_TYPES.NEIGHBOURING_RIGHTS: insert_statement = insert_stmt_db_sales_nr_staging().render( accounting_period_id=accounting_period_id, statement_period_id=statement_period_id, schema=config.OWS_ENV ) else: return hook.run(insert_statement, autocommit=True)