"""Task to calculate transactions mechanical royalty amounts.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from tasks.accounting_period_mechanicals import helpers from templates.accounting_period_mechanicals.snowflake_snapshot_mechanical_transactions\ import calculate_mechanical_royalty_amounts def snowflake_calculate_mechanical_royalty_amounts_task( dag_run: dict, **kwargs ) -> None: """Calculate mechanical royalty_amounts for each eligible transaction. - Finds SNAPSHOT_MECHANICAL_TRANSACTION_DETAIL records for the accounting_period where IS_LICENSED is true - Groups the records by the MECHANICAL_TRANSACTION_ID and sums the royalty_amount - Uses the summed amount to update the parent SNAPSHOT_MECHANICAL_TRANSACTION's royalty_amount using the MECHANICAL_TRANSACTION_ID - This will be the amount deducted from contracts during the accounting run depending on the contract_mechanical_deduction.admin_type ('both', 'business', or 'customer') Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) update_statement = calculate_mechanical_royalty_amounts().render( accounting_period_id=event.target_id, schema=config.OWS_ENV ) hook.run(update_statement, autocommit=True)