group InvoicePayment;

invoicePaymentFields(prefix) ::= <<
  <prefix>id,
  <prefix>type,
  <prefix>invoice_id,
  <prefix>payment_id,
  <prefix>payment_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, :paymentId, :paymentDate, :amount, :currency,
         :paymentCookieId, :linkedInvoicePaymentId, :userName, :createdDate);
>>

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

getByPaymentId() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments
  WHERE payment_id = :paymentId;
>>

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

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

getPaymentsForCookieId() ::= <<
  SELECT <invoicePaymentFields()>
  FROM invoice_payments
  WHERE payment_cookie_id = :paymentCookieId;
>>

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

notifyOfPayment() ::= <<
  INSERT INTO invoice_payments(<invoicePaymentFields()>)
  VALUES(:id, :type, :invoiceId, :paymentId, :paymentDate, :amount, :currency,
        :paymentCookieId, :linkedInvoicePaymentId, :userName, :createdDate);
>>

getInvoicePayment() ::= <<
    SELECT <invoicePaymentFields()>
    FROM invoice_payments
    WHERE payment_id = :paymentId;
>>

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

getChargebacksByPaymentId() ::= <<
    SELECT <invoicePaymentFields()>
    FROM invoice_payments
    WHERE linked_invoice_payment_id IN
        (SELECT id FROM invoice_payments WHERE payment_id = :paymentId);
>>
;
