group PaymentAttemptSqlDao;

CHECK_TENANT(prefix) ::= "<prefix>tenant_record_id = :tenantRecordId"
AND_CHECK_TENANT(prefix) ::= "AND <CHECK_TENANT(prefix)>"

paymentAttemptFields(prefix) ::= <<
    <prefix>id,
    <prefix>payment_id,    
    <prefix>gateway_error_code,
    <prefix>gateway_error_msg,    
    <prefix>processing_status,
    <prefix>requested_amount,
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date,
    <prefix>account_record_id,
    <prefix>tenant_record_id
>>

insertPaymentAttempt() ::= <<
    INSERT INTO payment_attempts (<paymentAttemptFields()>)
    VALUES (:id, :paymentId, :gatewayErrorCode, :gatewayErrorMsg, :processingStatus, :requestedAmount, :userName,
            :createdDate, :userName, :createdDate, :accountRecordId, :tenantRecordId);
>>

getPaymentAttempt() ::= <<
    SELECT <paymentAttemptFields("pa.")>
    , pa.created_date as effective_date
    , p.account_id as account_id
    , p.invoice_id as invoice_id
      FROM payment_attempts pa join payments p
     WHERE pa.id = :id 
     AND pa.payment_id = p.id
     <AND_CHECK_TENANT("pa.")>
     <AND_CHECK_TENANT("p.")>
     ;
>>

getPaymentAttempts() ::= <<
    SELECT <paymentAttemptFields("pa.")>
    , pa.created_date as effective_date
    , p.account_id as account_id
    , p.invoice_id as invoice_id
      FROM payment_attempts pa join payments p
     WHERE pa.payment_id = :paymentId
     AND p.id = :paymentId
     <AND_CHECK_TENANT("pa.")>
     <AND_CHECK_TENANT("p.")>
     ORDER BY effective_date ASC;
>>

updatePaymentAttemptStatus() ::= <<
    UPDATE payment_attempts
    SET processing_status = :processingStatus,
        gateway_error_code = :gatewayErrorCode,
        gateway_error_msg = :gatewayErrorMsg        
    WHERE  id = :id
    <AND_CHECK_TENANT()>
    ;
>>

getRecordId() ::= <<
    SELECT record_id
    FROM payment_attempts
    WHERE id = :id
    <AND_CHECK_TENANT()>
    ;
>>

getPaymentAttemptIdFromPaymentId() ::= <<
    SELECT id
    FROM payment_attempts
    WHERE payment_id = :paymentId
    <AND_CHECK_TENANT()>
    ;
>>

historyFields(prefix) ::= <<
    <prefix>record_id,
    <prefix>id,
    <prefix>payment_id,
    <prefix>gateway_error_code,
    <prefix>gateway_error_msg,    
    <prefix>processing_status,
    <prefix>requested_amount,    
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date,
    <prefix>account_record_id,
    <prefix>tenant_record_id
>>

insertHistoryFromTransaction() ::= <<
    INSERT INTO payment_attempt_history (<historyFields()>)
    VALUES (:recordId, :id, :paymentId, :gatewayErrorCode, :gatewayErrorMsg, :processingStatus, :requestedAmount,
            :userName, :createdDate, :userName, :updatedDate, :accountRecordId, :tenantRecordId);
>>

getHistoryRecordId() ::= <<
    SELECT MAX(history_record_id)
    FROM payment_attempt_history
    WHERE record_id = :recordId
    <AND_CHECK_TENANT()>
    ;
>>

auditFields(prefix) ::= <<
    <prefix>table_name,
    <prefix>record_id,
    <prefix>change_type,
    <prefix>change_date,
    <prefix>changed_by,
    <prefix>reason_code,
    <prefix>comments,
    <prefix>user_token,
    <prefix>account_record_id,
    <prefix>tenant_record_id
>>

insertAuditFromTransaction() ::= <<
    INSERT INTO audit_log(<auditFields()>)
    VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken, :accountRecordId, :tenantRecordId);
>>






