group InvoicePayment;

invoicePaymentFields(prefix) ::= <<
  <prefix>id,
  <prefix>type,
  <prefix>invoice_id,
  <prefix>payment_attempt_id,
  <prefix>payment_attempt_date,
  <prefix>amount,
  <prefix>currency,
  <prefix>payment_cookie_id,
  <prefix>linked_invoice_payment_id,
  <prefix>created_by,
  <prefix>created_date
>>

create() ::= <<
  INSERT INTO invoice_payments(<invoicePaymentFields()>)
  VALUES(:id, :type, :invoiceId, :paymentAttemptId, :paymentAttemptDate, :amount, :currency,
         :paymentCookieId, :linkedInvoicePaymentId, :userName, :createdDate);
>>

batchCreateFromTransaction() ::= <<
  INSERT INTO invoice_payments(<invoicePaymentFields()>)
  VALUES(:id, :type, :invoiceId, :paymentAttemptId, :paymentAttemptDate, :amount, :currency,
        :paymentCookieId, :linkedInvoicePaymentId, :userName, :createdDate);
>>

getByPaymentAttemptId() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments
  WHERE payment_attempt_id = :paymentAttemptId;
>>

get() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments;
>>

getById() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments
  WHERE id = :id;
>>

getPaymentsForInvoice() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments
  WHERE invoice_id = :invoiceId;
>>

notifyOfPaymentAttempt() ::= <<
  INSERT INTO invoice_payments(<invoicePaymentFields()>)
  VALUES(:id, :type, :invoiceId, :paymentAttemptId, :paymentAttemptDate, :amount, :currency,
        :paymentCookieId, :linkedInvoicePaymentId, :userName, :createdDate);
>>

getInvoicePayment() ::= <<
    SELECT <invoicePaymentFields()>
    FROM invoice_payments
    WHERE payment_attempt_id = :paymentAttemptId;
>>

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

getRecordIds() ::= <<
    SELECT record_id, id
    FROM invoice_payments
    WHERE invoice_id = :invoiceId;
>>

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

test() ::= <<
    SELECT 1 FROM invoice_payments;
>>

getRemainingAmountPaid() ::= <<
    SELECT SUM(amount)
    FROM invoice_payments
    WHERE id = :invoicePaymentId
    OR linked_invoice_payment_id = :invoicePaymentId;
>>

getAccountIdFromInvoicePaymentId() ::= <<
    SELECT account_id
    FROM invoice_payments ip
    INNER JOIN invoices i ON i.id = ip.invoice_id
    WHERE ip.id = :invoicePaymentId;
>>

getChargeBacksByAccountId() ::= <<
    SELECT <invoicePaymentFields("ip.")>
    FROM invoice_payments ip
    INNER JOIN invoices i ON i.id = ip.invoice_id
    WHERE i.account_id = :accountId
    AND linked_invoice_payment_id IS NOT NULL;
>>

getChargebacksByAttemptPaymentId() ::= <<
    SELECT <invoicePaymentFields()>
    FROM invoice_payments
    WHERE linked_invoice_payment_id IN
        (SELECT id FROM invoice_payments WHERE payment_attempt_id = :paymentAttemptId);
>>
;
