<?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="updateLoanById">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                amountPaid = :amountPaid,
                amountToPay = :amountToPay,
                lastReferenceNumber = :lastReferenceNumber,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="updateLoanFromRenovation">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                amountPaid = :amountPaid,
                lastReferenceNumber = :lastReferenceNumber,
                loanStatus = :loanStatus,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="updateLoanStatusWhereIdIn">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                amountPaid = :amountPaid,
                lastReferenceNumber = :lastReferenceNumber,
                loanStatus = :loanStatus,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id IN :id
        ]]>
    </query>
    
    <query name="updateLoanFromWeb">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                loanStatus = :loanStatus,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="findLoansByCustomer">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            WHERE
                customer = :customer
                AND loanStatus != 'DELETED'
            ORDER BY
                createdOn DESC
        ]]>
    </query>
    
    <query name="findLoansJuridical">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            INNER JOIN 
                LoanByUser inner_lby ON inner_lby.loan = l.id AND inner_lby.user != :idUser
            WHERE
                l.lastReferenceNumber > 30 
                AND l.loanStatus = 'APPROVED'
                AND (l.amountToPay - l.amountPaid) > 0
            ORDER BY
                l.createdOn
        ]]>
    </query>
    
    <query name="findLoansByEndorsement">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            WHERE
                endorsement = :endorsement 
                AND loanStatus != 'DELETED'
            ORDER BY
                createdOn
        ]]>
    </query>
    
    <query name="updateLoanByIdFromCertifiedView">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                loanStatus = :loanStatus,
                comments = :comments,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="updateLoanWithCreatedOnByIdFromCertifiedView">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                loanStatus = :loanStatus,
                comments = :comments,
                createdOn = :createdOn,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="findLoansByStatusPending">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            WHERE
                loanStatus = 'PENDING'
            ORDER BY
                createdOn
        ]]>
    </query>
    
    <query name="findAllLoans">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            WHERE 
                loanStatus != 'DELETED'
            ORDER BY
                createdOn
        ]]>
    </query>
    
    <query name="findLoansZero">
        <![CDATA[
            SELECT 
                l
            FROM
                LoanDetailZeroView l
        ]]>
    </query>
    
    <query name="findLoansFinished">
        <![CDATA[
            SELECT 
                l
            FROM
                LoanFinishedView l
        ]]>
    </query>    
    
    <query name="findAllLoansView">
        <![CDATA[
            SELECT 
                l
            FROM
                HistoryLoanView l
        ]]>
    </query>
    
    <query name="findAllLoansViewByStartAndEndDate">
        <![CDATA[
            SELECT 
                l
            FROM
                HistoryLoanView l
            WHERE 
                DATE(l.fecha) >= DATE(:startDate) AND 
                DATE(:endDate) >= DATE(l.fecha)
            ORDER BY l.fecha
        ]]>
    </query>
    
    <query name="findAllLoansJuridicalViewByStartAndEndDate">
        <![CDATA[
            SELECT 
                l
            FROM
                HistoryLoanView l
            WHERE 
                DATE(l.fechaJuridico) >= DATE(:startDate) AND 
                DATE(:endDate) >= DATE(l.fechaJuridico)
            ORDER BY l.fecha
        ]]>
    </query>
    
    <query name="findLoansDetailById">
        <![CDATA[
            SELECT 
                l
            FROM
                LoanDetails l
            WHERE
                loan = :loan
            ORDER BY
                referenceNumber DESC
        ]]>
    </query>
    
    <query name="updateAndFinishLoanById">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                loanStatus = :loanStatus,
                amountPaid = :amountPaid,
                amountToPay = :amountToPay,
                lastReferenceNumber = :lastReferenceNumber,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="updateDiscountAndLoanByIdFromCertifiedView">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                amountPaid = :amountPaid,
                lastReferenceNumber = :lastReferenceNumber,
                loanStatus = :loanStatus,
                comments = :comments,
                lastUpdatedBy = :lastUpdatedBy,
                lastUpdatedOn = :lastUpdatedOn
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="findAllLoansAprovedByUser">
        <![CDATA[
            SELECT 
                l
            FROM
                Loan l
            WHERE 
                loanStatus != 'DELETED'
            ORDER BY
                createdOn
        ]]>
    </query>
    
    <query name="searchPaymentDetails">
        <![CDATA[
            SELECT  
                CONCAT(
                    CASE
                        WHEN cstmr.firstName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(cstmr.firstName), 1, 1),SUBSTR(LOWER(cstmr.firstName), 2), ' ')
                        ELSE ''
                    END,
                    CASE
                        WHEN cstmr.secondName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(cstmr.secondName), 1, 1),SUBSTR(LOWER(cstmr.secondName), 2), ' ')
                        ELSE ''
                    END, 
                    CASE
                        WHEN cstmr.lastName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(cstmr.lastName), 1, 1),SUBSTR(LOWER(cstmr.lastName), 2))
                        ELSE ''
                    END
                ) AS customerName, 
                cstmr.addressHome AS customerAddressHome,
                cstmr.addressBusiness AS customerAddressBusiness,
                cstmr.companyName AS companyName,
                cstmr.thumbnail AS customerThumbnail,
                CONCAT(
                    CASE
                        WHEN ndrsmnt.firstName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(ndrsmnt.firstName), 1, 1),SUBSTR(LOWER(ndrsmnt.firstName), 2), ' ')
                        ELSE ''
                    END,
                    CASE
                        WHEN ndrsmnt.secondName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(ndrsmnt.secondName), 1, 1),SUBSTR(LOWER(ndrsmnt.secondName), 2), ' ')
                        ELSE ''
                    END, 
                    CASE
                        WHEN ndrsmnt.lastName IS NOT NULL
                        THEN CONCAT(SUBSTR(UPPER(ndrsmnt.lastName), 1, 1),SUBSTR(LOWER(ndrsmnt.lastName), 2))
                        ELSE ''
                    END
                )AS endorsementName,
                ndrsmnt.addressHome AS endorsementAddressHome,
                ndrsmnt.thumbnail AS endorsementThumbnail,
                ndrsmnt.phoneHome AS endorsementPhoneHome,
                (
                    CASE
                        WHEN 
                            (l.amountToPay - l.amountPaid) >= lt.paymentDaily
                        THEN
                            lt.paymentDaily
                        ELSE
                            (l.amountToPay - l.amountPaid)
                    END
                )AS paymentDaily,
                lt.fee AS fee,
                (SELECT count(notificationNumber) FROM LoanFeeNotification WHERE loan = l.id) AS notificationNumber,
                (
                    CASE
                        WHEN 
                            l.amountPaid >= (SELECT FLOOR(lt.paymentTotal * .6364) FROM LoanType WHERE id = l.loanType)
                        THEN
                            CASE
                                WHEN 
                                    (SELECT COUNT(notificationNumber) as total 
                                    FROM LoanFeeNotification
                                    WHERE loan = l.id
                                    ) < 4   
                                THEN 'ENEBLED'
                                WHEN 
                                    (SELECT count(notificationNumber) as total 
                                    FROM LoanFeeNotification
                                    WHERE loan = l.id
                                    ) BETWEEN 4 AND 5   
                                    AND
                                    (SELECT COUNT(lt_inner.id) 
                                                    FROM Loan l_inner
                                                    INNER JOIN LoanType lt_inner ON l_inner.loanType = lt_inner.id
                                                    WHERE l_inner.id = l.id AND
                                                        lt.payment <= lt_inner.payment - 1000) > 0
                                THEN 'ENEBLED'
                                ELSE 'DISABLED'
                            END
                        ELSE
                            'DISABLED'
                    END
                ) AS renovation,
                (SELECT amountToPay - amountPaid FROM Loan WHERE id = l.id) AS maxAmountToPay,
                (
                    CASE
                        WHEN 
                        (SELECT COUNT(count_ld.id) FROM LoanDetails count_ld WHERE count_ld.loan = l.id AND DATE(count_ld.createdOn) = CURDATE()) > 0
                        THEN
                            1
                        ELSE
                            0
                    END
                ) AS hasPaymentToday,
                l.id AS loanId,
                (
                SELECT 
                    inner_lby.user.id
                FROM 
                    LoanByUser inner_lby
                INNER JOIN 
                    Loan inner_l ON inner_lby.loan = inner_l.id
                WHERE
                    inner_l.loanStatus = 'APPROVED' AND
                    inner_lby.ownerLoan = 'CURRENT_OWNER' AND
                    inner_lby.loan = l.id
                ) AS currentOwner
            FROM 
                LoanByUser lbu
                INNER JOIN Loan l ON lbu.loan = l.id
                INNER JOIN LoanType lt ON l.loanType = lt.id
                INNER JOIN People cstmr ON l.customer = cstmr.id
                INNER JOIN People ndrsmnt ON l.endorsement = ndrsmnt.id
            WHERE
                l.customer = :customer AND
                l.loanStatus = 'APPROVED' AND
                1 = (CASE
                    WHEN
                        (SELECT COUNT(case_l.id) AS TOTAL
			FROM Loan case_l
			INNER JOIN LoanByUser case_lbu ON case_l.id = case_lbu.loan
			WHERE 
                            case_l.customer = :customer AND
                            case_l.loanStatus = 'APPROVED' AND
                            case_lbu.ownerLoan = 'CURRENT_OWNER' AND
                            case_lbu.user = :user
                        
                        ) = 1
                    THEN 
                        1
                    WHEN
                        l.routeCtlg IN (SELECT case_hrhr.routeCtlg
                            FROM HumanResourceHasRoute case_hrhr
                            INNER JOIN User case_u ON case_hrhr.humanResource = case_u.humanResource
                            WHERE 
                                case_u.id = :id AND
                                case_u.certifier = 'ENEBLED')
                    THEN 
                        1
                    ELSE 
                        0
                END)
        ]]>
    </query>
    
    <query name="updateRouteFromLoan">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                routeCtlg = :routeCtlg,
                lastUpdatedBy = :lastUpdatedBy
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="updateBonusNewCustomer">
        <![CDATA[
            UPDATE 
                Loan
            SET 
                newCustomer = :newCustomer,
                lastUpdatedBy = :lastUpdatedBy
            WHERE
                id = :id
        ]]>
    </query>
    
    <query name="findLastReferenceNumberByLoan">
        <![CDATA[
            SELECT 
                lastReferenceNumber
            FROM
                Loan
            WHERE
                id = :id
                
        ]]>
    </query>
    
    <query name="countLoanInStatuses">
        <![CDATA[
            SELECT 
                COUNT(l.id)
            FROM
                Loan l
            WHERE
                l.id = :id AND
                l.loanStatus in :loanStatus
        ]]>
    </query>
    
    <query name="countLoanByCustomerInStatuses">
        <![CDATA[
            SELECT 
                COUNT(l.id)
            FROM
                Loan l
            WHERE
                l.customer = :customer AND
                l.loanStatus in :loanStatus
        ]]>
    </query>
    
    <query name="selectLoanIdByCustomerInStatuses">
        <![CDATA[
            SELECT 
                l.id
            FROM
                Loan l
            WHERE
                l.customer = :customer AND
                l.loanStatus in :loanStatus
        ]]>
    </query>
    
</hibernate-mapping>
