AccountingStatementExport
========================
## About
The Accounting Statement Export is a queue-consumer system, running on a cron schedule. Upon receiving an SQS message,
it uses the message contents to build a query and COPY query results from the Redshift fact_sales table to an S3 bucket.
It downloads the files from S3 to a local, temporary directory and transforms the results to meet the specifications of the
SQS message contents. Finally, it uploads the transformed file to another location on S3.

## Configuration
### Update configs/aws.yml
1. Set `global_params` with your AWS credentials
2. Specify the appropriate AWS SQS queue (e.g. `dev-statement-detail-report`)

### Update configs/db.yml
Be sure to use valid hosts and credentials for all parts of this configuration file.

## Run
On the command-line, execute the following:
```
composer install
php scripts/accounting_statement_export_consumer.php
```

### Notes
`scripts/accounting_statement_export_consumer.php` has a hard-coded value for `$parent_folder`, which points to `/mnt/data/tmp/statement_detail_export/`. You may need to update the script to specify a path available on your local system (e.g. `/tmp`);
    

### Sample contents of an SQS message
```
[{
    "report_record_id": 12669,
    "bucket": "dev-db-data",
    "subkey": "ppl/12669/",
    "filename": "statement-detail-export-201-12669",
    "periodid": 201,
    "headers": {"period":"Period","activityperiod":"Activity Period","dms":"DMS","territory":"Territory","orchardupc":"Orchard UPC","manufacturerupc":"Manufacturer's UPC","catalognumber":"Label Catalog #","imprint":"Imprint Label","artistname":"Artist Name","releasename":"Release Name","trackname":"Track Name","isrc":"ISRC","cd":"Volume","track_id":"Track #","quantity":"Quantity","unitprice":"Unit Price","gross":"Gross","transactiontypeabbr":"Trans Type","adjusted_gross":"Adjusted Gross","splitrate":"Split Rate","labelsharednetreceipts":"Label Share Net Receipts","ringtone_publishing":"Ringtone Publishing","cloud_publishing":"Cloud Publishing","publishing":"Publishing","mechadministrativefee":"Mech. Administrative Fee","preferredcurrency":"Preferred Currency"},
    "user_type": "label",
    "number_format": "europe",
    "apply_payout_currency_exchange_rate": 1,
    "ringtone_publishing_digital_split": 1,
    "publishing_digital_split": 1,
    "payout_currency": "USD",
    "transaction_type_ids": "",
    "subaccount_ids": "",
    "label_ids": "12669"
}]
```

## Deployment
This repo is deployed on the `taskrunner` box. Systems has a manual process for deployment:
* SSH into `taskrunner` server
* `cd` into the directory for accounting-statement-export
* `git pull`
