SELECT
  JH.CMSJournalNo,
  JH.CMSInvNo,
  JH.ItemNumber,
  JH.JournalDate,
  JH.Narration,
  JH.ExternalID,
  JH.Updated,
  JH.AccountBookType,
  JH.AccountBookID,
  JH.ClientID AS CMSClientID,
  IH.DocumentNumber,
  IH.ExternalID AS InvoiceExternalID,
  '' AS LineItems
FROM
  taManualJournalHeader AS JH
    LEFT JOIN taInvoiceHeader AS IH ON (IH.CMSInvNo = JH.CMSInvNo)
WHERE
  (JH.AccountBookType = 'Xero') AND
  ((IH.CMSInvNo IS NULL) OR (IH.Updated = 1)) AND
  ((IH.CMSInvNo IS NULL) OR (IH.DocumentNumber <> 'VOID')) AND
  (JH.Updated = 0)
  @pFilter
ORDER BY
  JH.AccountBookID,
  JH.CMSJournalNo
