group PaymentSqlDao: EntitySqlDao;


extraTableFieldsWithComma(prefix) ::= <<
, <prefix>record_id as payment_number
>>

tableFields(prefix) ::= <<
  <prefix>account_id
, <prefix>invoice_id
, <prefix>payment_method_id
, <prefix>amount
, <prefix>effective_date
, <prefix>currency
, <prefix>payment_status
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>

tableValues() ::= <<
  :accountId
, :invoiceId
, :paymentMethodId
, :amount
, :effectiveDate
, :currency
, :paymentStatus
, :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
;
>>


updatePaymentStatus() ::= <<
update payments
set payment_status = :paymentStatus
where id = :id
<AND_CHECK_TENANT()>
;
>>


updatePaymentForNewAttempt() ::= <<
update <tableName()>
set amount = :amount
, effective_date = :effectiveDate
, payment_method_id= :paymentMethodId
where id = :id
<AND_CHECK_TENANT()>
;
>>


