group PaymentSqlDao;

paymentInfoFields(prefix) ::= <<
    <prefix>id,
    <prefix>external_payment_id,
    <prefix>amount,
    <prefix>refund_amount,
    <prefix>bank_identification_number,
    <prefix>payment_number,
    <prefix>payment_type,
    <prefix>status,
    <prefix>reference_id,
    <prefix>payment_method_id,
    <prefix>payment_method,
    <prefix>card_type,
    <prefix>card_country,
    <prefix>effective_date,
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date
>>

insertPaymentInfo() ::= <<
    INSERT INTO payments (<paymentInfoFields()>)
    VALUES (:id, :externalPaymentId, :amount, :refundAmount, :bankIdentificationNumber, :paymentNumber,
    :paymentType, :status, :referenceId, :paymentMethodId, :paymentMethod, :cardType,
    :cardCountry, :effectiveDate, :userName, :createdDate, :userName, :createdDate);
>>

updatePaymentInfo() ::= <<
    UPDATE payments
       SET payment_method = :paymentMethod,
           card_type = :cardType,
           card_country = :cardCountry,
           updated_by = :userName,
           updated_date = :updatedDate
     WHERE id = :id
>>

getPaymentInfoList(invoiceIds) ::= <<
    SELECT <paymentInfoFields("p.")>
      FROM payments p, payment_attempts pa
    WHERE pa.invoice_id in (<invoiceIds>)
       AND pa.payment_id = p.id
>>

getLastPaymentInfo(invoiceIds) ::= <<
    SELECT <paymentInfoFields("p.")>
    FROM payments p, payment_attempts pa
    WHERE pa.invoice_id in (<invoiceIds>)
    AND pa.payment_id = p.id
    ORDER BY p.created_date DESC
    LIMIT 1;
>>

getPaymentInfoForPaymentAttemptId() ::= <<
    SELECT <paymentInfoFields("p.")>
      FROM payments p, payment_attempts pa
    WHERE pa.payment_attempt_id = :paymentAttemptId
       AND pa.payment_id = p.id
>>

getPaymentInfo() ::= <<
    SELECT <paymentInfoFields()>
    FROM payments
    WHERE id = :id
>>

historyFields(prefix) ::= <<
    record_id,
    id,
    external_payment_id,
    amount,
    refund_amount,
    payment_number,
    bank_identification_number,
    status,
    reference_id,
    payment_type,
    payment_method_id,
    payment_method,
    card_type,
    card_country,
    effective_date,
    created_by,
    created_date,
    updated_by,
    updated_date
>>

insertHistoryFromTransaction() ::= <<
    INSERT INTO payment_history (<historyFields()>)
    VALUES (:recordId, :id, :externalPaymentId, :amount, :refundAmount, :bankIdentificationNumber, :paymentNumber,
    :paymentType, :status, :referenceId, :paymentMethodId, :paymentMethod, :cardType,
    :cardCountry, :effectiveDate, :userName, :createdDate, :userName, :updatedDate);
>>

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

getHistoryRecordId() ::= <<
    SELECT MAX(history_record_id)
    FROM payment_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);
>>

