<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <query name="getAllEmployeeWithoutUser">
    <![CDATA[
        SELECT p
        FROM Person p
        LEFT JOIN User u ON p.id = u.person
        WHERE 
            p.active = true
            and u.person is null
            and p.personType = :personType
            and p.root = false
            and p.id != :id
        ORDER BY p.firstName, p.secondName, p.lastName, p.middleName ASC
    ]]>
    </query>
    
    <sql-query name="findAllBirthdateInADate">
	<![CDATA[
           SELECT 
                DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.birthdate))) birthdate,
                CONCAT(first_name, ' ', second_name, ' ', last_name, ' ', middle_name) complete_name,
                person_type person_type
            FROM PERSON p
            WHERE 
                    p.active = 1 AND 
                    p.birthdate IS NOT NULL AND 
                    :startDate
                            <= 
                    DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.birthdate)))
                            AND
                    DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.birthdate))) 
                            < 
                    :endDate
            ORDER BY DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.birthdate))) ASC
        ]]>
    </sql-query>
    
    <sql-query name="findAllDriversByAnniversaryInADate">
        <![CDATA[
            SELECT  
                DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.admission_date))) admission_date,
                CONCAT(first_name, ' ', second_name, ' ', last_name, ' ', middle_name) complete_name,
                person_type person_type
            FROM PERSON p
            WHERE 
                p.person_type = 'DRIVER' AND
                p.active = 1 AND 
                p.admission_date IS NOT NULL AND
                YEAR(p.admission_date) < YEAR(:endDate) AND
                :startDate
                     <= 
                 DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.admission_date)))
                    AND
                DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.admission_date))) 
                    < 
                :endDate
            ORDER BY DATE(MAKEDATE(YEAR(DATE_SUB(:endDate, INTERVAL 1 MONTH)),DAYOFYEAR(p.admission_date))) ASC
        ]]>
    </sql-query>
    
    <sql-query name="findAllDriversBySixMonthInADate">
        <![CDATA[
            SELECT  
                DATE(DATE_ADD(p.admission_date, INTERVAL 6 MONTH)) admission_date,
                CONCAT(first_name, ' ', second_name, ' ', last_name, ' ', middle_name) complete_name,
                person_type person_type
            FROM PERSON  p
            WHERE 
                p.active = 1 AND 
                p.admission_date IS NOT NULL AND
                :startDate <= DATE(DATE_ADD(p.admission_date, INTERVAL 6 MONTH))  AND
                :endDate > DATE(DATE_ADD(p.admission_date, INTERVAL 6 MONTH))
            ORDER BY DATE(DATE_ADD(p.admission_date, INTERVAL 6 MONTH)) ASC
        ]]>
    </sql-query>
</hibernate-mapping>