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

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