SELECT
  BE.FinNumber AS AccountCode,
  BE.GroupName AS TradingName,
  BE.VATNumber,
  HO.Address1,
  HO.Address2,
  HO.Address3,
  HO.Address4,
  HO.Address5,
  BE.Postal1,
  BE.Postal2,
  BE.Postal3,
  BE.PostalCode,
  TRIM(CONCAT(TRIM(HO.TelephoneAreaCode), ' ', TRIM(HO.TelephoneNo))) AS Telephone,
  TRIM(CONCAT(TRIM(HO.FaxAreaCode),       ' ', TRIM(HO.FaxNo))) AS Fax,
  (SELECT U1.FullName FROM taUsers AS U1 WHERE U1.UserID = AP.ConsultantID) AS Consultant,
  (SELECT TRIM(CONCAT(TRIM(CC.Title), ' ', TRIM(CONCAT(TRIM(CC.FirstName), ' ', TRIM(CC.Surname)))))
   FROM taClientContacts AS CC WHERE (CC.TypeID = AP.AppointmentID) LIMIT 1) AS Contact,
  BE.TaxTypeID,
  ROUND(fnVATRateOnDate(NOW(), BE.TaxTypeID), 3) AS TaxRate,
  CP.Batch,
  BE.PaymentMethod
FROM
  tbAppointments AS AP
    LEFT JOIN taClients AS BE ON (BE.ClientID = AP.ClientID)
    LEFT JOIN taClientBranches AS HO ON ((HO.ClientID = BE.ClientID) AND (HO.IsHeadOffice = 1) AND (HO.BranchStatus = 1))
    LEFT JOIN taClientBranchProducts AS BP ON (BP.ClientBranchProductID = AP.ClientBranchProductID)
    LEFT JOIN taClientProducts AS CP ON (CP.ClientProductID = BP.ClientProductID)
WHERE
  (AP.AppointmentID = @nAppointmentID)
