<?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="1" author="mparkar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;

            DROP PROCEDURE IF EXISTS sp_get_active_vendor_contract;;

            CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_get_active_vendor_contract(IN vendorId INT(11))
            BEGIN
                SELECT vcc.id AS vendor_contract_id, v.vendor_id AS vendor_id
                    FROM ((vendor v
                            JOIN vendor_contract vcc
                            ON ((v.vendor_id = vcc.vendor_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 ISNULL(vcc2.contract_type))
                                    AND ((vcc2.cont_end >= CURDATE())
                                        OR ISNULL(vcc2.cont_end)
                                        OR (vcc2.cont_end = '0000-00-00'))))
                            AND (vcc.cont_start < vcc2.cont_start)))
                    WHERE (1
                        AND v.vendor_id = vendorId
                        AND ISNULL(vcc2.id)
                        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 ISNULL(vcc.contract_type))
                                AND ((vcc.cont_end >= CURDATE())
                                    OR ISNULL(vcc.cont_end)
                                    OR (vcc.cont_end = '0000-00-00'))))
                        AND (v.status = 'signed'))
                    GROUP BY `v`.`vendor_id`;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;            

                DROP PROCEDURE IF EXISTS sp_get_active_vendor_contract;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
