SELECT
  X.*,
  IF(Status = 'CANCELLED', 0.0, (SELECT fnClientBranchProductRetainer(X.ClientBranchProductID, NOW()) * 1.0)) AS Retainer
FROM (

  SELECT
    CB.ClientID,
    CP.ProductID,
    BP.ClientBranchID,
    BP.ClientProductID,
    BP.ClientBranchProductID,
    CB.LabourNetBranchID,
    BP.ConsultantID,
    BP.SalesReferrerID,
    BP.SalesCloserID,
    CB.NoOfEmployees,
    CB.BranchName AS ClientBranchName,
    IF(BP.Status = 1, 'ACTIVE', 'CANCELLED') AS Status,
    BR.BranchName AS LabourNetBranchName,
    BP.JoinDate,
    U1.FullName AS Consultant,
    U2.FullName AS SalesReferrer,
    U3.FullName AS SalesCloser
  FROM
    taClientBranchProducts AS BP
      LEFT JOIN taClientBranches AS CB ON (CB.ClientBranchID = BP.ClientBranchID)
      LEFT JOIN taClientProducts AS CP ON (CP.ClientProductID = BP.ClientProductID)
      LEFT JOIN taProducts AS PR ON (PR.ProductID = CP.ProductID)
      LEFT JOIN taClients AS CS ON (CS.ClientID = CB.ClientID)
      LEFT JOIN taUsers AS U1 ON (U1.UserID = BP.ConsultantID)
      LEFT JOIN taUsers AS U2 ON (U2.UserID = BP.SalesReferrerID)
      LEFT JOIN taUsers AS U3 ON (U3.UserID = BP.SalesCloserID)
      LEFT JOIN taBranches AS BR ON (BR.BranchID = CB.LabourNetBranchID)
  WHERE
    (BP.ClientProductID = @nClientProductID)
  ORDER BY
    BP.JoinDate

) AS X