INSERT INTO taClientBranchProductHistory (
  ClientBranchProductID,
  BillingYearMonth,
  AppointmentCount,
  CoverUsed,
  FreeTimeUsed,
  InvoiceAmount,
  RetainerFee
)

SELECT
  BP.ClientBranchProductID,
  @nBillingYearMonth AS BillingYearMonth,
  0 AS AppointmentCount,
  0.0 AS CoverUsed,
  0.0 AS FreeTimeUsed,
  0.0 AS InvoiceAmount,
  ROUND(fnClientBranchProductRetainer(BP.ClientBranchProductID, CONCAT(LEFT(@nBillingYearMonth, 4), '-', RIGHT(@nBillingYearMonth, 2), '-01')) * 1.0, 2) AS RetainerFee
FROM
  taClientBranchProducts AS BP
WHERE
  BP.ClientBranchProductID NOT IN (
    SELECT ClientBranchProductID FROM taClientBranchProductHistory WHERE BillingYearMonth = @nBillingYearMonth
  )
