# Accounting Run Approval and Commit

> Source: [Accounting Run (Notion)](https://www.notion.so/c9ecc13c18cb4362962ce54853992580)  
> Whimsical: [accounting_run_commit](https://whimsical.com/accounting-run-commit-fka-commit-DNSqEARY5SgtC45oRSFc4m)

## Overview

After the calculation completes, users review the run summary and BI reports. If the results are correct, they **approve** the run, which commits results to account ledgers. If incorrect, they can **invalidate** the run and start a new calculation.

## Approval Pipeline

When a user clicks "Approve":

```
1. Move results from staging to main tables (Snowflake)
2. Move contract_transactions from staging to main tables (Snowflake)
3. Invoke commit_royalties lambda (MySQL ledger)
4. Invoke reserves_take lambda (distribution only)
5. Take reserves (distribution only)
6. Invoke reserves_schedule lambda (distribution only)
```

## Step 1: Move Results from Staging to Main

> [Code](https://github.com/theorchard/ows-royalties-workflows/blob/master/dags/templates/accounting_run_commit/snowflake_copy_results.py)

Copies data from:
- `accounting_run_results_distro_staging` → `accounting_run_results_distro`
- `accounting_run_results_nr_staging` → `accounting_run_results_nr`

## Step 2: Move Contract Transactions from Staging to Main

> [Code](https://github.com/theorchard/ows-royalties-workflows/blob/master/dags/templates/accounting_run_commit/snowflake_copy_contract_transactions.py)

Copies data from:
- `contract_transaction_staging` → `contract_transaction_distro`
- `contract_transaction_nr_staging` → `contract_transaction_nr`

## Step 3: Commit Royalties Lambda

> [Code](https://github.com/theorchard/lambda-abacus/blob/master/lambda/commit_royalties/README.md)

Creates a `commit_royalties` abacus_event and invokes the lambda. The lambda:
1. Reads results from `accounting_run_results_distro` (or NR equivalent)
2. Sums net revenue per contract in the payee's currency
3. Rounds totals and inserts into `ledger_account_contract`
4. Inserts rounding remainders into `ledger_deposit`

## Step 4: Take Reserves (Distribution Only)

> [Code](https://github.com/theorchard/lambda-abacus/blob/master/lambda/reserves_take/README.md)

Distribution runs include physical sales that are reserved and released later. The [`reserves_take`](https://github.com/theorchard/lambda-abacus/blob/master/lambda/reserves_take/README.md) lambda calculates what reserves to take, inserting into `ledger_reserve_taken`.

## Step 5: Debit Reserves (Distribution Only)

Queries `ledger_reserve_taken` and debits `reserve_amount` from `ledger_account_contract`.

## Step 6: Schedule Reserve Releases (Distribution Only)

> [Code](https://github.com/theorchard/lambda-abacus/blob/master/lambda/reserves_schedule/README.md)

The [`reserves_schedule`](https://github.com/theorchard/lambda-abacus/blob/master/lambda/reserves_schedule/README.md) lambda schedules the release of taken reserves over a defined time period.

For more details on how Abacus handles reserves, see the [Reserves documentation (Notion)](https://www.notion.so/89307b6eb5c241729948f3ba78518a0a).

## Invalidation

If a run is deemed incorrect:
1. The user marks the run as "Invalid"
2. A brand new accounting run appears on the page
3. The user can recalculate and compare results to the invalidated run
