CREATE  PROCEDURE [dbo].[SupplyChainHIX_GetInvoiceByID] @InvoiceId NUMERIC
AS
--               InvoiceGeneralInfo 
SELECT REPLACE(dbo.MiladiToShamsi(headH.Tarikh_faktor), '/', '-') jiDocumentDate,
       Nu_Forosh jiDocumentNumber,
       REPLACE(dbo.MiladiToShamsi(headH.Tarikh_Pardakht), '/', '-') jiReferencedDocumentDate,
       dbo.headH.Number_bar jiReferencedDocumentNumber,
       Id_Havaleh InvoiceId,
       CAST(1 AS VARCHAR(10)) jiDocumentType,
       CAST(11 AS VARCHAR(10)) jiDocumentSubType,
       CAST(0 AS FLOAT) jiRespite,
       CAST(1 AS BIT) jiCashPayment
FROM dbo.headH
WHERE Id_Havaleh = @InvoiceId;
----               productAndCosts
SELECT CAST(dt.Radif AS FLOAT) jiRowNumber,
       g.Meli_Code jiProductCode,
       dt.K_Code jiInternalCode,
       dt.K_Code jiProductTrackId,
       g.Barcode jiProductIRC,
       g.Name2 jiProductName,
       p.Name jiProducerName,
       --       '' jiSupplierName,
       CAST(0 AS FLOAT) jiExtraOfPackageItems,
       CAST(ISNULL(gof.GoodsFormId, '409') AS VARCHAR(10)) BehdashtProductForm,
       gf.Name ProductFormName,
       CAST(ISNULL(gof.TemperatureType, 0) AS VARCHAR(10)) MaintainTemperature,
       CAST(g.R_Unit1_2 AS FLOAT) jiPackagingUnit,
       CAST(dt.K_Qty2 AS FLOAT) jiNumberOfPackages,
       CAST(ISNULL(gof.GoodsType, 0) AS TINYINT) MeasureUnitType,
       CASE
           WHEN CAST(ISNULL(gof.GoodsType, 0) AS TINYINT) = 0 THEN
               'دماي اتاق'
           WHEN CAST(ISNULL(gof.GoodsType, 0) AS TINYINT) = 1 THEN
               'دماي خنک'
           WHEN CAST(ISNULL(gof.GoodsType, 0) AS TINYINT) = 2 THEN
               'دماي يخچال'
           WHEN CAST(ISNULL(gof.GoodsType, 0) AS TINYINT) = 3 THEN
               'دماي فريزي'
       END AS MeasureUnitTypeName,
       CAST(dt.K_Qty1 AS FLOAT) jiTotalNumber,
       ISNULL(s.Serial, '0') jiBatchNumber,
       REPLACE(CONVERT(VARCHAR(10), e.Exp_Date, 111), '/', '-') [jiExpireDate],
       0 jiAward,
       CAST(dt.Price_Forosh * dt.R_Unit1_2 AS FLOAT) jiPriceUnit,
       CAST(dt.Price_Consumer * dt.R_Unit1_2 AS FLOAT) PriceUnitForConsumer,
       CAST(0 AS FLOAT) jiTotalTaxes,
       CAST(dt.K_Qty1 * dt.Price_Forosh AS FLOAT) jiTotalCostForSales,
       CAST(0 AS FLOAT) jiTotalCostForConsumer,
       CAST(0 AS FLOAT) jiDiscount,
       CAST(dt.K_Qty1 * dt.Price_Forosh AS FLOAT) jiTotalCostAfterTaxes,
       CAST(dt.K_Qty1 * dt.Price_Forosh AS FLOAT) jiTotalCostAfterDiscount
FROM dbo.Havaleh dt
    JOIN dbo.KalaId g
        ON g.K_Code = dt.K_Code
    LEFT JOIN dbo.Guy p
        ON p.M_Code = g.M_Code
           AND g.M_Code IS NOT NULL
           AND g.M_Code <> ''
    LEFT JOIN dbo.Serial_File s
        ON s.AutoId = dt.AutoId
    LEFT JOIN dbo.Exp_File e
        ON e.AutoId = dt.AutoId
    LEFT JOIN dbo.GoodsOtherFeature gof
        ON gof.GoodsCode = g.K_Code
    LEFT JOIN dbo.GoodsForm gf
        ON gf.Id = gof.GoodsFormId
WHERE Id_Havaleh = @InvoiceId;
--               ScStackholder
SELECT jiPartyId,
       OrganizationType,
       CAST(1 AS CHAR(1)) PartyType
FROM
(
    SELECT [Value] AS jiPartyId
    FROM dbo.GeneralSetting gs
    WHERE gs.Parameter = 'SupplyChainHIX_SenderId'
) AS ScStackholder
    CROSS JOIN
    (
        SELECT [Value] AS OrganizationType
        FROM dbo.GeneralSetting gs
        WHERE gs.Parameter = 'SupplyChainHIX_OrganizationType'
    ) AS t2;
--                    receiver   
SELECT ISNULL(MIN(gi.HIXCode), '0000000000') jiPartyId,
       ISNULL(CAST(0 AS CHAR(1)), '0') OrganizationType,
       ISNULL(CAST(1 AS CHAR(1)), '1') PartyType,
       g.Name receiverName
FROM dbo.Havaleh dt
    LEFT JOIN dbo.tblGuy_Info gi
        ON gi.Guy_Code = dt.Reciver
    LEFT JOIN dbo.Guy g
        ON g.Guy_Code = gi.Guy_Code
WHERE Id_Havaleh = @InvoiceId
GROUP BY dt.Id_Havaleh,
         g.Name;
--                    deliverer
SELECT v.Name jiFullName,
       ISNULL(v.Postal_Code, '0000000000') jiPostalCode,
       v.Addr jiPostalAddress,
       ISNULL(v.mobile, '00000000000') PhoneNumber
FROM headH h
    INNER JOIN Guy v
        ON v.Guy_Code = h.Nu_Kharid
WHERE h.Id_Havaleh = @InvoiceId;