<?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 `FlattenAndPopulatePublishingEscrowTable`;;

            CREATE DEFINER=`theorchard`@`10.10.%` PROCEDURE `FlattenAndPopulatePublishingEscrowTable`() DETERMINISTIC
            BEGIN
                TRUNCATE TABLE TEMP_mechanical_publishing_escrow;

                INSERT INTO `TEMP_mechanical_publishing_escrow` ( `escrow_id`, `statement_detail_id`, `track_unique_id`, `dms_customer_id`, `period_id`, `year`, `month`, `trans_type`, `qty`, `ownership`, `royalty_rate`, `royalty`, `ownership_released`, `royalty_released`, `license_nos`, `oms_fees`, `dpd_publishing`, `gross`, `adjusted_gross`, `net_receipt`, `actual_net`, `payout_currency`, `vendor_id`, `upc`, `cd`, `track_id`, `length_minute`, `length_seconds` )
                SELECT 
                pe.escrow_id
                , pe.statement_detail_id
                , pe.track_id AS track_unique_id
                , pe.dms_customer_id
                , pe.period_id
                , p.year
                , p.month
                , pe.trans_type
                , pe.qty
                , pe.ownership
                , pe.royalty_rate
                , pe.royalty
                , (SELECT SUM(ownership) FROM publishing_escrow_released WHERE escrow_id = pe.escrow_id) AS ownership_released
                , (SELECT SUM(amount) FROM publishing_escrow_released WHERE escrow_id = pe.escrow_id) AS royalty_released
                , GROUP_CONCAT(pl.license_no) AS license_nos
                , pds.oms_fees
                , pds.dpd_publishing
                , pds.gross
                , pds.adjusted_gross
                , pds.net_receipt
                , pds.actual_net
                , pds.payout_currency_id AS payout_currency
                , a.vendor_id
                , t.upc, t.cd
                , t.track_id
                , t.length_minute
                , t.length_seconds
                FROM publishers_license pl
                INNER JOIN publishing_escrow pe ON pe.track_id = pl.track_id
                INNER JOIN period p ON p.period_id = pe.period_id
                INNER JOIN processed_dig_sales pds ON pds.statement_detail_id = pe.statement_detail_id
                INNER JOIN track t ON t.id = pe.track_id
                INNER JOIN releases r ON r.upc = t.upc
                INNER JOIN artist_info a ON a.artist_id = r.artist_id
                LEFT JOIN publishing_escrow_released per ON per.escrow_id = pe.escrow_id AND per.released_to_type = 'publisher' AND per.released_to = pl.license_no
                LEFT JOIN publisher_statement_detail psd ON psd.license_no = pl.license_no AND psd.period_id = pe.period_id
                WHERE pl.license_type = 'dpd' AND pe.trans_type = 'DPD' AND psd.license_no IS NULL AND per.id IS NULL
                GROUP BY pe.escrow_id
                HAVING ownership_released < pe.ownership OR ownership_released IS NULL
                ORDER BY a.vendor_id;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS `FlattenAndPopulatePublishingEscrowTable`;;

                CREATE DEFINER=`ketan`@`%` PROCEDURE `FlattenAndPopulatePublishingEscrowTable`() DETERMINISTIC
                BEGIN
                    TRUNCATE TABLE TEMP_mechanical_publishing_escrow;

                    INSERT INTO `TEMP_mechanical_publishing_escrow` ( `escrow_id`, `statement_detail_id`, `track_unique_id`, `dms_customer_id`, `period_id`, `year`, `month`, `trans_type`, `qty`, `ownership`, `royalty_rate`, `royalty`, `ownership_released`, `royalty_released`, `license_nos`, `oms_fees`, `dpd_publishing`, `gross`, `adjusted_gross`, `net_receipt`, `actual_net`, `payout_currency`, `vendor_id`, `upc`, `cd`, `track_id`, `length_minute`, `length_seconds` )
                    SELECT 
                    pe.escrow_id
                    , pe.statement_detail_id
                    , pe.track_id AS track_unique_id
                    , pe.dms_customer_id
                    , pe.period_id
                    , p.year
                    , p.month
                    , pe.trans_type
                    , pe.qty
                    , pe.ownership
                    , pe.royalty_rate
                    , pe.royalty
                    , (SELECT SUM(ownership) FROM publishing_escrow_released WHERE escrow_id = pe.escrow_id) AS ownership_released
                    , (SELECT SUM(amount) FROM publishing_escrow_released WHERE escrow_id = pe.escrow_id) AS royalty_released
                    , GROUP_CONCAT(pl.license_no) AS license_nos
                    , pds.oms_fees
                    , pds.dpd_publishing
                    , pds.gross
                    , pds.adjusted_gross
                    , pds.net_receipt
                    , pds.actual_net
                    , pds.payout_currency_id AS payout_currency
                    , a.vendor_id
                    , t.upc, t.cd
                    , t.track_id
                    , t.length_minute
                    , t.length_seconds
                    FROM publishers_license pl
                    INNER JOIN publishing_escrow pe ON pe.track_id = pl.track_id
                    INNER JOIN period p ON p.period_id = pe.period_id
                    INNER JOIN processed_dig_sales pds ON pds.statement_detail_id = pe.statement_detail_id
                    INNER JOIN track t ON t.id = pe.track_id
                    INNER JOIN releases r ON r.upc = t.upc
                    INNER JOIN artist_info a ON a.artist_id = r.artist_id
                    LEFT JOIN publishing_escrow_released per ON per.escrow_id = pe.escrow_id AND per.released_to_type = 'publisher' AND per.released_to = pl.license_no
                    LEFT JOIN publisher_statement_detail psd ON psd.license_no = pl.license_no AND psd.period_id = pe.period_id
                    WHERE pl.license_type = 'dpd' AND pe.trans_type = 'DPD' AND psd.license_no IS NULL AND per.id IS NULL
                    GROUP BY pe.escrow_id
                    HAVING ownership_released < pe.ownership OR ownership_released IS NULL
                    ORDER BY a.vendor_id;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
