<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="101" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            DROP PROCEDURE IF EXISTS `processed_dig_sales_to_outfile`;;

            CREATE DEFINER=`theorchard`@`10.10.%` PROCEDURE `processed_dig_sales_to_outfile`(IN batchNum SMALLINT(3))
            BEGIN
                   SET @s := '';
               SELECT CONCAT("SELECT
                   `pd`.`statement_detail_id`,
                   `pd`.`period_id`,
                   `pd`.`dms_customer_id`,
                   `pd`.`date`,
                   `pd`.`upc`,
                   IF(pd.trans_type IN ('DA','UA','VR'), 0, pd.cd) AS cd, 
                   IF(pd.trans_type IN ('DA','UA','VR'), 0, pd.track_id) AS track_id,
                   `pd`.`qty`,
                   `pd`.`actual_net`,
                   `pd`.`adjusted_gross`,
                   `pd`.`distribution_fees`,
                   `pd`.`dpd_publishing`,
                   `pd`.`gross`,
                   `pd`.`net_receipt`,
                   `pd`.`oms_fees`,
                   `pd`.`partner_share`,
                   `pd`.`ringtone_publishing`,
                   UPPER(`pd`.`trans_type`),
                   CASE
            WHEN pd.trans_type IN ('DA','UA','VR') OR ((pd.trans_type IS NULL OR pd.trans_type = '') AND (dd.isrc = '' OR dd.isrc IS NULL)) THEN CONCAT(pd.upc, '_ISRC')
            WHEN dd.isrc = ''  OR dd.isrc IS NULL THEN CONCAT_WS('_',pd.upc, pd.cd, pd.track_id)
            ELSE UPPER(dd.isrc) END  AS isrc,        
                   `dd`.`retail_price`,
                   `dd`.`track_name`,
                    (SELECT ISO_4217_code FROM currencies WHERE id = original_currency_id) AS original_currency_code,
                   `pd`.`fx_spread_fee`,
                   `pd`.`activity_rate`,
                   (SELECT ISO_4217_code FROM currencies WHERE id = payout_currency_id) AS payout_currency_code,
                   pd.fx_adjusted_exchange_rate
               FROM processed_dig_sales pd
               INNER JOIN dig_sales_detail dd ON dd.statement_detail_id = pd.statement_detail_id
               INNER JOIN dig_sales ds ON ds.statement_id = dd.statement_id
               WHERE pd.period_id = ", batchNum, " into outfile '/tmp/processed_dig_sales", batchNum, ".tbl' fields terminated by '\t' optionally enclosed by '\"'  lines terminated by '\n'") INTO @s;
               PREPARE stmt FROM @s;
               EXECUTE stmt;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS `processed_dig_sales_to_outfile`;;

                CREATE DEFINER=`ketan`@`%` PROCEDURE `processed_dig_sales_to_outfile`(IN batchNum SMALLINT(3))
                BEGIN
                       SET @s := '';
                   SELECT CONCAT("SELECT
                       `pd`.`statement_detail_id`,
                       `pd`.`period_id`,
                       `pd`.`dms_customer_id`,
                       `pd`.`date`,
                       `pd`.`upc`,
                       IF(pd.trans_type IN ('DA','UA','VR'), 0, pd.cd) AS cd, 
                       IF(pd.trans_type IN ('DA','UA','VR'), 0, pd.track_id) AS track_id,
                       `pd`.`qty`,
                       `pd`.`actual_net`,
                       `pd`.`adjusted_gross`,
                       `pd`.`distribution_fees`,
                       `pd`.`dpd_publishing`,
                       `pd`.`gross`,
                       `pd`.`net_receipt`,
                       `pd`.`oms_fees`,
                       `pd`.`partner_share`,
                       `pd`.`ringtone_publishing`,
                       UPPER(`pd`.`trans_type`),
                       CASE
                WHEN pd.trans_type IN ('DA','UA','VR') OR ((pd.trans_type IS NULL OR pd.trans_type = '') AND (dd.isrc = '' OR dd.isrc IS NULL)) THEN CONCAT(pd.upc, '_ISRC')
                WHEN dd.isrc = ''  OR dd.isrc IS NULL THEN CONCAT_WS('_',pd.upc, pd.cd, pd.track_id)
                ELSE UPPER(dd.isrc) END  AS isrc,        
                       `dd`.`retail_price`,
                       `dd`.`track_name`,
                        (SELECT ISO_4217_code FROM currencies WHERE id = original_currency_id) AS original_currency_code,
                       `pd`.`fx_spread_fee`,
                       `pd`.`activity_rate`,
                       (SELECT ISO_4217_code FROM currencies WHERE id = payout_currency_id) AS payout_currency_code,
                       pd.fx_adjusted_exchange_rate
                   FROM processed_dig_sales pd
                   INNER JOIN dig_sales_detail dd ON dd.statement_detail_id = pd.statement_detail_id
                   INNER JOIN dig_sales ds ON ds.statement_id = dd.statement_id
                   WHERE pd.period_id = ", batchNum, " into outfile '/tmp/processed_dig_sales", batchNum, ".tbl' fields terminated by '\t' optionally enclosed by '\"'  lines terminated by '\n'") INTO @s;
                   PREPARE stmt FROM @s;
                   EXECUTE stmt;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
