CREATE OR ALTER PROCEDURE sp_GetInvoiceHeader
(
    @Tip INT,
    @InvoiceId INT
)
AS
BEGIN
    SET NOCOUNT ON;

    ------------------------------------------------------------------
    -- 1) Invoice/SendBox kaydı var mı?
    ------------------------------------------------------------------
    IF (@Tip = 2)
    BEGIN
        IF NOT EXISTS(SELECT 1 FROM SendBoxInvoice WHERE Id = @InvoiceId)
            THROW 50001, 'SendBoxInvoice içinde fatura bulunamadı!', 1;
    END
    ELSE
    BEGIN
        IF NOT EXISTS(SELECT 1 FROM Invoice WHERE Id = @InvoiceId)
            THROW 50002, 'Invoice içinde fatura bulunamadı!', 1;
    END


    ------------------------------------------------------------------
    -- 2) JOIN'li SELECT sonuç verecek mi? (Cari eşleşmesi dahil)
    ------------------------------------------------------------------
    DECLARE @Count INT;

    IF (@Tip = 2)
    BEGIN
        SELECT @Count = COUNT(*)
        FROM SendBoxInvoice I
        JOIN AMUSTERI m ON m.MUSVERNO = I.ReceiverIdentifier
        WHERE I.Id = @InvoiceId;

        IF @Count = 0
            THROW 50003, 'Invoice bulundu ancak cari eşleşmesi yok veya fatura verisi okunamadı!', 1;
    END
    ELSE
    BEGIN
        SELECT @Count = COUNT(*)
        FROM Invoice I
        JOIN AMUSTERI m ON m.MUSVERNO = I.SenderIdentifier
        WHERE I.Id = @InvoiceId;

        IF @Count = 0
            THROW 50004, 'Invoice bulundu ancak cari eşleşmesi yok veya fatura verisi okunamadı!', 1;
    END


    ------------------------------------------------------------------
    -- 3) Kayıt varsa detay SELECT'i döndür (bu her zaman 1 satırdır)
    ------------------------------------------------------------------
    IF (@Tip = 2)
    BEGIN
        SELECT 
            I.*,
            m.MUSNO  AS CariNo,
            m.MUSADI AS CariAdi,
            m.MUSKOD AS CariKodu,
            SUBSTRING(I.InvoiceNo,1,3) AS FatSeri,
            SUBSTRING(I.InvoiceNo,8, LEN(I.InvoiceNo)-3) AS FatNo,
            CASE 0
                WHEN 'TICARIFATURA' THEN 0
                WHEN 'TEMELFATURA'  THEN 1
                ELSE 2
            END AS Senaryo,
            (SELECT TOP 1 NoId     FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizNo,
            (SELECT TOP 1 DovizKodu FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizKod,
            (SELECT TOP 1 DovizAdi  FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizAdi
        FROM SendBoxInvoice I
        JOIN AMUSTERI m ON m.MUSVERNO = I.ReceiverIdentifier
        WHERE I.Id = @InvoiceId;
    END
    ELSE
    BEGIN
        SELECT 
            I.*,
            m.MUSNO  AS CariNo,
            m.MUSADI AS CariAdi,
            m.MUSKOD AS CariKodu,
            SUBSTRING(I.InvoiceNo,1,3) AS FatSeri,
            SUBSTRING(I.InvoiceNo,8, LEN(I.InvoiceNo)-3) AS FatNo,
            CASE I.InvoiceType
                WHEN 'TICARIFATURA' THEN 0
                WHEN 'TEMELFATURA'  THEN 1
                ELSE 2
            END AS Senaryo,
            (SELECT TOP 1 NoId     FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizNo,
            (SELECT TOP 1 DovizKodu FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizKod,
            (SELECT TOP 1 DovizAdi  FROM DOVIZT WHERE IsoKodu = I.CurrencyId) AS DovizAdi
        FROM Invoice I
        JOIN AMUSTERI m ON m.MUSVERNO = I.SenderIdentifier
        WHERE I.Id = @InvoiceId;
    END
END
