group PaymentSqlDao;

paymentFields(prefix) ::= <<
    <prefix>id,
    <prefix>account_id,
    <prefix>invoice_id,   
    <prefix>payment_method_id,
    <prefix>amount,
    <prefix>effective_date,
    <prefix>currency,
    <prefix>payment_status,
    <prefix>ext_first_payment_ref_id,
    <prefix>ext_second_payment_ref_id,    
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date
>>

insertPayment() ::= <<
    INSERT INTO payments (<paymentFields()>)
    VALUES (:id, :accountId, :invoiceId, :paymentMethodId, :amount, :effectiveDate, :currency, :paymentStatus, :extFirstPaymentRefId, :extSecondPaymentRefId, :userName, :createdDate, :userName, :createdDate);
>>

getPayment() ::= <<
    SELECT <paymentFields()>
    , record_id as payment_number
      FROM payments
    WHERE id = :id;
>>

getPaymentsForInvoice() ::= <<
    SELECT <paymentFields()>
    , record_id as payment_number
      FROM payments
    WHERE invoice_id = :invoiceId;
>>

getPaymentsForAccount() ::= <<
    SELECT <paymentFields()>
    , record_id as payment_number
      FROM payments
    WHERE account_id = :accountId;
>>


updatePaymentStatusAndExtRef() ::= <<
    UPDATE payments
    SET payment_status = :paymentStatus,
        ext_first_payment_ref_id = :extFirstPaymentRefId,
        ext_second_payment_ref_id = :extSecondPaymentRefId
    WHERE id = :id;
>>

updatePaymentAmount() ::= <<
    UPDATE payments
    SET amount = :amount
    WHERE id = :id;
>>

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


historyFields(prefix) ::= <<
    <prefix>record_id,
    <prefix>id,
    <prefix>account_id,
    <prefix>invoice_id,   
    <prefix>payment_method_id,
    <prefix>amount,
    <prefix>effective_date,
    <prefix>currency,
    <prefix>payment_status,
    <prefix>ext_first_payment_ref_id,
    <prefix>ext_second_payment_ref_id,    
    <prefix>created_by,
    <prefix>created_date,
    <prefix>updated_by,
    <prefix>updated_date 
>>

insertHistoryFromTransaction() ::= <<
    INSERT INTO payment_history (<historyFields()>)
    VALUES (:recordId, :id, :accountId, :invoiceId, :paymentMethodId, :amount, :effectiveDate, :currency, :paymentStatus, :extFirstPaymentRefId, :extSecondPaymentRefId, :userName, :createdDate, :userName, :updatedDate);
>>


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

