SELECT
  IH.CMSInvNo,
  IH.AppointmentID AS YearMonth,
  IH.DocumentNumber,
  IH.DocumentDate,
  IH.DueDate,
  IH.AccountCode,
  IH.Description,
  IH.Contact,
  IH.AccountBookType,
  IH.AccountBookID,
  BL.EntityName AS AccountBookName,
  IH.InvoiceAmountIncl
FROM
  taInvoiceHeader AS IH
    LEFT JOIN taBilldozerEntity AS BL ON ((BL.BilldozerEntityID = IH.AccountBookID) AND (IH.AccountBookType = @aAccountBookType))
WHERE
  (BL.Enabled = 1) AND
  (IH.InvoiceType = 'R') AND
  (IH.AppointmentID = @nYearMonth) AND
  (IH.AccountBookType = @aAccountBookType) AND
  (IH.AccountBookID = @nAccountBookID) AND
  (IH.Batch = CONCAT('Batch ', @nBatch)) AND
  (IH.Updated = 1) AND
  (IH.DocumentNumber <> 'VOID')
  @pInvoices

-- For testing.
LIMIT 10;