group PaymentAttemptSqlDao;

paymentAttemptFields(prefix) ::= <<
    <prefix>id,
    <prefix>payment_id,    
    <prefix>payment_error,
    <prefix>processing_status,
    <prefix>requested_amount,
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date
>>

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

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;
>>

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
     ORDER BY effective_date ASC;
>>

updatePaymentAttemptStatus() ::= <<
    UPDATE payment_attempts
    SET processing_status = :processingStatus,
        payment_error = :paymentError
    WHERE  id = :id;
>>


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

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

historyFields(prefix) ::= <<
    <prefix>record_id,
    <prefix>id,
    <prefix>payment_id,
    <prefix>payment_error,
    <prefix>processing_status,
    <prefix>requested_amount,    
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date
>>

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

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

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
>>

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






