group RecurringInvoiceItemSqlDao;

fields(prefix) ::= <<
  <prefix>id,
  <prefix>invoice_id,
  <prefix>account_id,
  <prefix>bundle_id,
  <prefix>subscription_id,
  <prefix>plan_name,
  <prefix>phase_name,
  <prefix>start_date,
  <prefix>end_date,
  <prefix>amount,
  <prefix>rate,
  <prefix>currency,
  <prefix>reversed_item_id,
  <prefix>created_by,
  <prefix>created_date
>>

getById() ::= <<
  SELECT <fields()>
  FROM recurring_invoice_items
  WHERE id = :id;
>>

getInvoiceItemsByInvoice() ::= <<
  SELECT <fields()>
  FROM recurring_invoice_items
  WHERE invoice_id = :invoiceId;
>>

getInvoiceItemsByAccount() ::= <<
  SELECT <fields("rii.")>
  FROM recurring_invoice_items rii
  INNER JOIN invoices i ON i.id = rii.invoice_id
  WHERE i.account_id = :accountId;
>>

getInvoiceItemsBySubscription() ::= <<
  SELECT <fields()>
  FROM recurring_invoice_items
  WHERE subscription_id = :subscriptionId;
>>

create() ::= <<
  INSERT INTO recurring_invoice_items(<fields()>)
  VALUES(:id, :invoiceId, :accountId, :bundleId, :subscriptionId, :planName, :phaseName, :startDate, :endDate,
         :amount, :rate, :currency, :reversedItemId, :userName, :createdDate);
>>

batchCreateFromTransaction() ::= <<
  INSERT INTO recurring_invoice_items(<fields()>)
  VALUES(:id, :invoiceId, :accountId, :bundleId, :subscriptionId, :planName, :phaseName, :startDate, :endDate,
         :amount, :rate, :currency, :reversedItemId, :userName, :createdDate);
>>

getRecordIds() ::= <<
    SELECT record_id, id
    FROM recurring_invoice_items
    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 recurring_invoice_items;
>>
;
