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

            CREATE DEFINER=`theorchard`@`10.10.%` PROCEDURE `sp_get_missing_exchange_rates`(IN periodID INT)
            BEGIN
                SELECT 
                    oc.id AS original_currrency_id,
                    pc.id AS payout_currency_id,
                    oc.ISO_4217_code AS original_currency_code,
                    pc.ISO_4217_code AS payout_currency_code,
                    cer_orig_to_payout.exchange_rate AS orig_to_payout_rate,
                    cer_payout_to_usd.exchange_rate AS payout_to_usd_rate,
                    cer_usd_to_payout.exchange_rate AS usd_to_payout_rate
                FROM period p
                
                JOIN ( 
                    SELECT 
                        pc.id,
                        pc.`ISO_4217_code`
                    FROM vendor v
                    JOIN vendor_contract vcc ON v.vendor_id=vcc.vendor_id
                    JOIN currencies pc ON pc.`id` = vcc.`currency_id`
                    LEFT JOIN vendor_contract vcc2 ON vcc.vendor_id = vcc2.vendor_id 
                        AND vcc2.cont_start <= NOW()
                        AND ( vcc2.contract_type = 'per_release_term' 
                            AND ( vcc2.cont_start = '0000-00-00' 
                                OR vcc2.release_term <=0 
                                OR ( vcc2.cont_start + INTERVAL vcc2.release_term MONTH) > NOW()  ) 
                                OR ( (vcc2.contract_type <> 'per_release_term' OR vcc2.contract_type IS NULL) 
                                    AND (vcc2.cont_end >= NOW() OR vcc2.cont_end IS NULL OR vcc2.cont_end = '0000-00-00'
                                ) 
                            ) 
                        )
                        AND vcc.cont_start < vcc2.cont_start                                                                                  
                    WHERE 1
                        AND vcc2.id IS NULL  
                        AND vcc.cont_start <= NOW()
                        AND ( vcc.contract_type = 'per_release_term' 
                            AND ( vcc.cont_start = '0000-00-00' 
                                OR vcc.release_term <=0 
                                OR ( vcc.cont_start + INTERVAL vcc.release_term MONTH) > NOW() )
                                OR ( (vcc.contract_type <> 'per_release_term' OR vcc.contract_type IS NULL) 
                                    AND (vcc.cont_end >= NOW()  OR vcc.cont_end IS NULL OR vcc.cont_end = '0000-00-00') 
                                )
                            ) 
                        AND v.status = "signed" 
                        GROUP BY vcc.currency_id) pc
                
                JOIN (
                    SELECT 
                        oc.id,
                        t.period_id,
                        oc.`ISO_4217_code`
                    FROM TEMP_dig_sales_statements t
                    JOIN currencies oc ON oc.id = t.original_currency_id
                    WHERE 1 
                    GROUP BY oc.id ) oc ON oc.period_id = p.period_id
                
                LEFT JOIN currency_exchange_rates cer_orig_to_payout ON `cer_orig_to_payout`.`period_id` = p.period_id
                    AND cer_orig_to_payout.`currency_from_id` = oc.id 
                    AND cer_orig_to_payout.`currency_to_id` = pc.id 
                
                LEFT JOIN currency_exchange_rates cer_payout_to_usd ON cer_payout_to_usd.`period_id` = p.`period_id`
                    AND cer_payout_to_usd.`currency_to_id` = pc.id 
                    AND cer_payout_to_usd.`currency_from_id` = '1' 
                
                LEFT JOIN currency_exchange_rates cer_usd_to_payout ON cer_usd_to_payout.period_id = p.period_id
                    AND cer_usd_to_payout.currency_to_id = '1' 
                    AND cer_usd_to_payout.currency_from_id = pc.id 
                WHERE 1
                AND p.period_id = periodID
                ORDER BY oc.`ISO_4217_code`, pc.`ISO_4217_code`;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS `sp_get_missing_exchange_rates`;;

                CREATE DEFINER=`ketan`@`%` PROCEDURE `sp_get_missing_exchange_rates`(IN periodID INT)
                BEGIN
                    SELECT 
                        oc.id AS original_currrency_id,
                        pc.id AS payout_currency_id,
                        oc.ISO_4217_code AS original_currency_code,
                        pc.ISO_4217_code AS payout_currency_code,
                        cer_orig_to_payout.exchange_rate AS orig_to_payout_rate,
                        cer_payout_to_usd.exchange_rate AS payout_to_usd_rate,
                        cer_usd_to_payout.exchange_rate AS usd_to_payout_rate
                    FROM period p
                    
                    JOIN ( 
                        SELECT 
                            pc.id,
                            pc.`ISO_4217_code`
                        FROM vendor v
                        JOIN vendor_contract vcc ON v.vendor_id=vcc.vendor_id
                        JOIN currencies pc ON pc.`id` = vcc.`currency_id`
                        LEFT JOIN vendor_contract vcc2 ON vcc.vendor_id = vcc2.vendor_id 
                            AND vcc2.cont_start <= NOW()
                            AND ( vcc2.contract_type = 'per_release_term' 
                                AND ( vcc2.cont_start = '0000-00-00' 
                                    OR vcc2.release_term <=0 
                                    OR ( vcc2.cont_start + INTERVAL vcc2.release_term MONTH) > NOW()  ) 
                                    OR ( (vcc2.contract_type <> 'per_release_term' OR vcc2.contract_type IS NULL) 
                                        AND (vcc2.cont_end >= NOW() OR vcc2.cont_end IS NULL OR vcc2.cont_end = '0000-00-00'
                                    ) 
                                ) 
                            )
                            AND vcc.cont_start < vcc2.cont_start                                                                                  
                        WHERE 1
                            AND vcc2.id IS NULL  
                            AND vcc.cont_start <= NOW()
                            AND ( vcc.contract_type = 'per_release_term' 
                                AND ( vcc.cont_start = '0000-00-00' 
                                    OR vcc.release_term <=0 
                                    OR ( vcc.cont_start + INTERVAL vcc.release_term MONTH) > NOW() )
                                    OR ( (vcc.contract_type <> 'per_release_term' OR vcc.contract_type IS NULL) 
                                        AND (vcc.cont_end >= NOW()  OR vcc.cont_end IS NULL OR vcc.cont_end = '0000-00-00') 
                                    )
                                ) 
                            AND v.status = "signed" 
                            GROUP BY vcc.currency_id) pc
                    
                    JOIN (
                        SELECT 
                            oc.id,
                            t.period_id,
                            oc.`ISO_4217_code`
                        FROM TEMP_dig_sales_statements t
                        JOIN currencies oc ON oc.id = t.original_currency_id
                        WHERE 1 
                        GROUP BY oc.id ) oc ON oc.period_id = p.period_id
                    
                    LEFT JOIN currency_exchange_rates cer_orig_to_payout ON `cer_orig_to_payout`.`period_id` = p.period_id
                        AND cer_orig_to_payout.`currency_from_id` = oc.id 
                        AND cer_orig_to_payout.`currency_to_id` = pc.id 
                    
                    LEFT JOIN currency_exchange_rates cer_payout_to_usd ON cer_payout_to_usd.`period_id` = p.`period_id`
                        AND cer_payout_to_usd.`currency_to_id` = pc.id 
                        AND cer_payout_to_usd.`currency_from_id` = '1' 
                    
                    LEFT JOIN currency_exchange_rates cer_usd_to_payout ON cer_usd_to_payout.period_id = p.period_id
                        AND cer_usd_to_payout.currency_to_id = '1' 
                        AND cer_usd_to_payout.currency_from_id = pc.id 
                    WHERE 1
                    AND p.period_id = periodID
                    ORDER BY oc.`ISO_4217_code`, pc.`ISO_4217_code`;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
