group PaymentSqlDao: EntitySqlDao;


tableFields(prefix) ::= <<
  <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
>>

tableValues() ::= <<
  :accountId
, :invoiceId
, :paymentMethodId
, :amount
, :effectiveDate
, :currency
, :paymentStatus
, :extFirstPaymentRefId
, :extSecondPaymentRefId
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>

tableName() ::= "payments"

historyTableName() ::= "payment_history"


getPaymentsForAccount() ::= <<
select <allTableFields()>
, record_id as payment_number
from payments
where account_id = :accountId
<AND_CHECK_TENANT()>
;
>>


getPaymentsForInvoice() ::= <<
select <allTableFields()>
, record_id as payment_number
from payments
where invoice_id = :invoiceId
<AND_CHECK_TENANT()>
;
>>


getLastPaymentForAccountAndPaymentMethod() ::= <<
select <allTableFields()>
, record_id as payment_number
from payments
where account_id = :accountId
and payment_method_id = :paymentMethodId
<AND_CHECK_TENANT()>
order by effective_date desc limit 1
;
>>



updatePaymentStatusAndExtRef() ::= <<
update payments
set payment_status = :paymentStatus
, ext_first_payment_ref_id = :extFirstPaymentRefId
, ext_second_payment_ref_id = :extSecondPaymentRefId
where id = :id
<AND_CHECK_TENANT()>
;
>>

updatePaymentAmount() ::= <<
update <tableName()>
set amount = :amount
where id = :id
<AND_CHECK_TENANT()>
;
>>


