CREATE PROC spGetLastStock  @NationalCode VARCHAR(10) 
AS
SELECT  S.K_Code,
       UsedQty = CAST(SUM(ISNULL(FD.K_Qty1, 0)) AS MONEY),
       dbo.MiladiToShamsi(MAX(F.Tarikh_faktor)) LastBuyDate
FROM dbo.tblSahmiye_Kala S
    JOIN dbo.Havaleh FD
        ON FD.K_Code = S.K_Code
    JOIN dbo.headH F
        ON F.Id_Havaleh = FD.Id_Havaleh
WHERE S.Status = 0
      AND CONVERT(VARCHAR(10), GETDATE(), 111)
      BETWEEN CONVERT(VARCHAR(10), S.Start_Date, 111) AND CONVERT(VARCHAR(10), S.End_Date, 111)
      AND F.Tarikh_faktor
      BETWEEN S.Start_Date AND S.End_Date
      AND (S.Active = 0)
      AND F.Kind_Flag = 7
      AND FD.Reciver = '10000'
      AND ISNULL(Number_bar, '0') = @NationalCode
GROUP BY S.K_Code;