<?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="findAllPayrollByOffice">
        <![CDATA[
            SELECT 
                c
            FROM Payroll c 
                WHERE office = :office
                AND activeStatus = :activeStatus 
            ORDER BY createdOn DESC
        ]]>
    </query>
    
    <query name="findAllPayrollByOfficeBetweenDates">
        <![CDATA[
            SELECT 
                c
            FROM Payroll c 
                WHERE 
                    office = :office AND
                    DATE(createdOn) >= DATE(:startDate) AND 
                    DATE(:endDate) >= DATE(createdOn)
            ORDER BY createdOn DESC
        ]]>
    </query>
    
    <query name="updatePayrollByStatus">
        <![CDATA[
            UPDATE Payroll
            SET 
                activeStatus = :activeStatus,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE id = :id
        ]]>
    </query>
    
    <query name="totalAdvanceForDatesByUser">
        <![CDATA[
            SELECT 
                SUM(c.amount)
            FROM Advance c 
                WHERE office = :office
                AND activeStatus = :activeStatus 
                AND humanResource = :humanResource
                AND DATE(createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query> 
    
    <query name="totalNewCustomerLoansByUser">
        <![CDATA[
            SELECT 
                l
            FROM Loan l 
            INNER JOIN LoanByUser lbu ON l = lbu.loan
                WHERE 
                l.loanStatus in ('APPROVED')
                AND l.newCustomer = 'ENEBLED' 
                AND l.createdBy = :user 
                AND DATE(l.createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query> 
    
    <query name="totalLoansByUserForDates">
        <![CDATA[
            SELECT 
              SUM(l.loanType.payment)
            FROM Loan l 
            INNER JOIN LoanByUser lbu ON l = lbu.loan
                WHERE 
                l.loanStatus in ('APPROVED')
                AND l.createdBy = :user 
                AND DATE(l.createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query> 
    
    <query name="getGoalByDates">
        <![CDATA[
            SELECT 
                g
            FROM 
                Goal g 
            WHERE 
                activeStatus = 'ENEBLED'
            AND 
                office = :office 
            AND 
                DATE(startDate) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query> 
    
    <query name="getLastGoal">
        <![CDATA[
            SELECT 
                g
            FROM 
                Goal g 
            WHERE 
                activeStatus = 'ENEBLED'
            AND 
                office = :office 
            AND 
                createdOn = (SELECT max(createdOn) FROM Goal)
        ]]>
    </query> 
    
    <query name="getLoanDetailsByUserForDates">
        <![CDATA[
            SELECT 
                SUM(ld.paymentAmount)
            FROM 
                LoanDetails ld
            INNER JOIN Loan l on ld.loan = l
            INNER JOIN LoanByUser lbu on lbu.loan = l 
            WHERE 
                l.loanStatus in ('APPROVED','FINISH','PENDING_RENOVATION')
            AND 
                ld.loanDetailsType in ('PAYMENT', 'RENOVATION_PAYMENT', 'TRANSFER') 
            AND 
                lbu.user = :user  
            AND 
                DATE(ld.createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query>
    
    <query name="getFeeByUserForDates">
        <![CDATA[
            SELECT 
                SUM(lt.paymentDaily)
            FROM 
                LoanDetails ld
            INNER JOIN Loan l on ld.loan = l
            INNER JOIN LoanByUser lbu on lbu.loan = l 
            INNER JOIN LoanType lt on l.loanType = lt
            WHERE 
                l.loanStatus in ('APPROVED','FINISH','PENDING_RENOVATION')
            AND 
                ld.loanDetailsType in ('FEE') 
            AND 
                lbu.user = :user  
            AND 
                DATE(ld.createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query>
    
    <query name="getDiscountByUserThisWeek">
        <![CDATA[
            SELECT 
                SUM(amountExpected - amountPaid)
            FROM 
                ClosingDay c
            WHERE 
                activeStatus = 'ENEBLED' 
            AND 
                user = :user  
            AND 
                WEEK(DATE(createdOn),1) = WEEK(CURDATE(),1) 
            AND 
                YEAR(DATE(createdOn)) = YEAR(CURDATE())
        ]]>
    </query> 
    
    <query name="sumAllDiferencesByUser">
        <![CDATA[
            SELECT 
                SUM(t.faltante)
            FROM LoanDiferencesByUserLastWeekView t
            WHERE idUser = :idUser               
        ]]>
    </query> 
    
    <query name="totalComisionAperturaByUserForDates">
        <![CDATA[
            SELECT 
              SUM(l.loanType.openingFee)
            FROM Loan l 
            INNER JOIN LoanByUser lbu ON l = lbu.loan
                WHERE 
                l.loanStatus in ('APPROVED')
                AND lbu.user = :user 
                AND DATE(l.createdOn) BETWEEN :dateInit AND :dateEnd
        ]]>
    </query> 
    
    <query name="totalExpectedWeekByUser">
        <![CDATA[
            SELECT 
              SUM(totalExpectedPayment)
            FROM TotalExpectedPaymentDailyByUser  
                WHERE 
                activeStatus = 'ENEBLED' 
                AND user = :user 
                AND WEEK(DATE(createdOn),1) = (WEEK(CURDATE(),1)-1) 
                AND YEAR(DATE(createdOn)) = YEAR(CURDATE()) 
        ]]>
    </query> 
    
</hibernate-mapping>