SELECT
  ROUND(SUM(IF((PF.EntryType = 'N'),                            PF.Difference, 0)), 2) AS SalesData,
  ROUND(SUM(IF((PF.EntryType = 'C') OR (PF.EntryType = 'S'),    PF.Difference, 0)), 2) AS CancelData,
  ROUND(SUM(IF((PF.EntryType = 'A'),                            PF.Difference, 0)), 2) AS AutoData,
  ROUND(SUM(IF(((PF.EntryType = 'M') AND (PF.Difference < 0)),  PF.Difference, 0)), 2) AS DecreaseData,
  ROUND(SUM(IF(((PF.EntryType = 'M') AND (PF.Difference >= 0)), PF.Difference, 0)), 2) AS IncreaseData,
  ROUND(SUM(IF((PF.EntryType = 'D') OR (PF.EntryType = 'P') OR (PF.EntryType = 'R'), PF.Difference, 0)), 2) AS ConvertData,
  ROUND(SUM(PF.Difference), 2) AS Total
FROM
  taClientProductFees AS PF
    LEFT JOIN taClientBranchProducts AS BP ON (BP.ClientBranchProductID = PF.ClientBranchProductID)
    LEFT JOIN taClientBranches       AS CB ON (CB.ClientBranchID = BP.ClientBranchID)
    LEFT JOIN taClientProducts       AS CP ON (CP.ClientProductID = BP.ClientProductID)
    LEFT JOIN taClients              AS CS ON (CS.ClientID = CB.ClientID)
    LEFT JOIN taProducts             AS PR ON (PR.ProductID = CP.ProductID)
    LEFT JOIN taBranches             AS BR ON (BR.BranchID = CB.LabourNetBranchID)
    LEFT JOIN taClientProducts       AS P1 ON (P1.ClientProductID = CP.ParentClientProductID)
WHERE
  (ABS(PF.Difference) > 0.001) AND
  (BR.BranchID IS NOT NULL) AND
  (PR.ProductID IS NOT NULL) AND
  ((CS.ExcludeFromReport IS NULL) OR (CS.ExcludeFromReport = 0)) AND
  (PF.StartDate BETWEEN @aFromDate AND @aToDate)

  @pBranchID
  @pProductID
