<?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="findLoanDetailsByLoan">
        <![CDATA[
            SELECT  
                DATE_FORMAT(ld.createdOn, '%d-%m-%Y') AS createdOn,
                ld.paymentAmount AS paymentAmount,
                ld.loanDetailsType AS loanDetailsType,
                lt.paymentTotal AS amountToPay,
                ld.comments AS comments
            FROM
                LoanDetails ld
                INNER JOIN Loan l ON ld.loan = l.id
                INNER JOIN LoanType lt ON l.loanType = lt.id 
            WHERE 
                ld.loan = :loan 
            ORDER BY  
                ld.createdOn, 
                ld.referenceNumber
        ]]>
    </query>
    
    <query name="findLoanDetailsPaymentCurdateByLoan">
        <![CDATA[
            SELECT  
                ld
            FROM
                LoanDetails ld
            WHERE 
                ld.loan = :loan 
            AND 
                ld.loanDetailsType IN ('PAYMENT', 'TRANSFER')
            AND
                DATE(createdOn) = CURDATE()
        ]]>
    </query>
    
    <query name="deleteLoanDetailsPaymentCurdateByLoan">
        <![CDATA[
            DELETE
            FROM
                LoanDetails ld
            WHERE 
                ld.loan = :loan 
            AND 
                ld.loanDetailsType IN ('PAYMENT', 'TRANSFER')
            AND
                DATE(createdOn) = CURDATE()
        ]]>
    </query>
    
    <query name="findLoanDetailsFeeCurdateByLoan">
        <![CDATA[
            SELECT  
                ld
            FROM
                LoanDetails ld
            WHERE 
                ld.loan = :loan 
            AND 
                ld.loanDetailsType IN ('FEE')
            AND
                DATE(createdOn) = CURDATE()
        ]]>
    </query>
    
    <query name="deleteLoanDetailsFeeCurdateByLoan">
        <![CDATA[
            DELETE
            FROM
                LoanDetails ld
            WHERE 
                ld.loan = :loan 
            AND 
                ld.loanDetailsType IN ('FEE')
            AND
                DATE(createdOn) = CURDATE()
        ]]>
    </query>
    
    <query name="deleteLoanFeeNotificationCurdateByLoan">
        <![CDATA[
            DELETE
            FROM
                LoanFeeNotification ld
            WHERE 
                ld.loan = :loan 
            AND
                DATE(createdOn) = CURDATE()
        ]]>
    </query>
    
    <query name="deleteLoanFeeNotificationByLoan">
        <![CDATA[
            DELETE
            FROM
                LoanFeeNotification ld
            WHERE 
                ld.loan = :loan
        ]]>
    </query>
    
    <query name="findFeesToPayByLoanId">
        <![CDATA[
            SELECT
                id AS id,
                DATE(createdOn) AS createdOn,
                feeStatus AS feeStatus
            FROM
                LoanDetails
            WHERE 
                loan = :loan AND
                loanDetailsType = 'FEE' AND
                feeStatus = 'TO_PAY'
            ORDER BY createdOn
        ]]>
    </query>
    
    <query name="findAllFeesByLoanId">
        <![CDATA[
            SELECT
                id AS id,
                DATE(createdOn) AS createdOn,
                feeStatus AS feeStatus
            FROM
                LoanDetails
            WHERE 
                loan = :loan AND
                loanDetailsType = 'FEE'
            ORDER BY createdOn
        ]]>
    </query>
    
    <query name="updatePaidFeesStatusInLoanDetailIds">
        <![CDATA[
            UPDATE
                LoanDetails
            SET 
                feeStatus = 'PAID'
            WHERE 
                id IN :id
        ]]>
    </query>
    
    <query name="countLoanDetailsInCurrdate">
        <![CDATA[
            SELECT  
                COUNT(ld.id)
            FROM
                LoanDetails ld
            WHERE 
                ld.loan = :loan 
            AND 
                ld.loanDetailsType IN :loanDetailsType
            AND
                DATE(ld.createdOn) = CURDATE()
        ]]>
    </query>
    
    
    <query name="findAllTransfersLoanDetail">
        <![CDATA[
            SELECT  
                ld
            FROM
                LoanDetails ld
            WHERE 
                loan_details_type = 'TRANSFER' and transfer_status = 'PENDING' 
        ]]>
    </query>
    
    <query name="updateAuthorizeTransferStatusInLoanDetailIds">
        <![CDATA[
            UPDATE
                LoanDetails
            SET 
                transfer_status = 'AUTHORIZED'
            WHERE 
                id IN :id
        ]]>
    </query>
    
    <query name="updateTransferStatusWhereIdIn">
        <![CDATA[
            UPDATE
                LoanDetails
            SET 
                transferStatus = :transferStatus,
                comments = CONCAT(comments, ', ', :comments)
            WHERE 
                id IN :id
        ]]>
    </query>
    
    <query name="updateRejectTransferStatusInLoanDetailIds">
        <![CDATA[
            UPDATE
                LoanDetails
            SET 
                transfer_status = 'REJECTED'
            WHERE 
                id IN :id
        ]]>
    </query>
    
    
    <query name="countLoanDetailsAuthorize">
        <![CDATA[
            SELECT 
                count(ld) 
            FROM 
                LoanDetails ld
            WHERE 
                loan_details_type = 'TRANSFER' and transfer_status='PENDING' and id_user = :user
        ]]>
    </query>
    
</hibernate-mapping>
