CREATE  PROC [dbo].[SupplyChainHIX_Invoice]
    @InputStartDate VARCHAR(10),
    @InputEndDate VARCHAR(10),
    @Status TINYINT,
    @IsLastCycle BIT,
    @InvoiceType TINYINT
AS
DECLARE @StartDate DATE = dbo.ShamsiToMiladi(@InputStartDate),
        @EndDate DATE = dbo.ShamsiToMiladi(@InputEndDate);

IF @IsLastCycle = 0
    SELECT schix.[Id],
           schix.[InvoiceId],
           schix.[Status],
           dbo.MiladiToShamsi([InsertDate]) InsertDate,
           CONVERT(VARCHAR(10), schix.InsertDate, 108) InsertTime,
           dbo.MiladiToShamsi(schix.[ResultDate]) ResultDate,
           CONVERT(VARCHAR(10), schix.[ResultDate], 108) ResultTime,
           StatusName = CASE
                            WHEN schix.Status = 2 THEN
                                'ناموفق'
                            WHEN schix.Status = 1 THEN
                                'موفق'
                            WHEN schix.Status = 3 THEN
                                'خطاي محتوا'
                            ELSE
                                'ارسال نشده'
                        END,
           schix.[ExceptionCode],
           schix.[ExceptionMessage],
           schix.[PayloadClass],
           schix.[Type],
           schix.[ConversationId],
           schix.[ResultId],
           schix.[DocumentTrackingCode],
           invoice.Nu_Forosh invoiceNumber,
           invoice.Kind_Flag invoiceType,
           dbo.MiladiToShamsi(invoice.Tarikh_faktor) invoiceDate,
           invoiceTypeName = CASE
                                 WHEN invoice.Kind_Flag = 7 THEN
                                     'خرده فروشي'
                                 WHEN invoice.Kind_Flag = 5 THEN
                                     'فروش'
                                 WHEN invoice.Kind_Flag = 9 THEN
                                     'عمده فروشي'
                             END,
           invoice.Recive_Name CustomerName
    FROM [dbo].[SupplyChainHIXInvoice] schix
        JOIN dbo.headH invoice
            ON invoice.Id_Havaleh = schix.InvoiceId
    WHERE CAST(schix.InsertDate AS DATE)
          BETWEEN @StartDate AND @EndDate
          AND schix.[Status] = CASE
                                   WHEN @Status = 4 THEN
                                       schix.Status
                                   ELSE
                                       @Status
                               END
          AND invoice.Kind_Flag IN (   CASE
                                           WHEN @InvoiceType = 0 THEN
                                               invoice.Kind_Flag
                                           WHEN @InvoiceType = 1 THEN
                                               7
                                           WHEN @InvoiceType = 2 THEN
                                               5
                                           WHEN @InvoiceType = 3 THEN
                                               9
                                       END
                                   )
    ORDER BY schix.Id;

ELSE
    SELECT schix.[Id],
           schix.[InvoiceId],
           schix.[Status],
           dbo.MiladiToShamsi([InsertDate]) InsertDate,
           CONVERT(VARCHAR(10), schix.InsertDate, 108) InsertTime,
           dbo.MiladiToShamsi(schix.[ResultDate]) ResultDate,
           CONVERT(VARCHAR(10), schix.[ResultDate], 108) ResultTime,
           StatusName = CASE
                            WHEN schix.Status = 2 THEN
                                'ناموفق'
                            WHEN schix.Status = 1 THEN
                                'موفق'
                            WHEN schix.Status = 3 THEN
                                'خطاي محتوا'
                            ELSE
                                'ارسال نشده'
                        END,
           schix.[ExceptionCode],
           schix.[ExceptionMessage],
           schix.[PayloadClass],
           schix.[Type],
           schix.[ConversationId],
           schix.[ResultId],
           schix.[DocumentTrackingCode],
           invoice.Nu_Forosh invoiceNumber,
           invoice.Kind_Flag invoiceType,
           dbo.MiladiToShamsi(invoice.Tarikh_faktor) invoiceDate,
           invoiceTypeName = CASE
                                 WHEN invoice.Kind_Flag = 7 THEN
                                     'خرده فروشي'
                                 WHEN invoice.Kind_Flag = 5 THEN
                                     'فروش'
                                 WHEN invoice.Kind_Flag = 9 THEN
                                     'عمده فروشي'
                             END,
           invoice.Recive_Name CustomerName
    FROM [dbo].[SupplyChainHIXInvoice] schix
        JOIN dbo.headH invoice
            ON invoice.Id_Havaleh = schix.InvoiceId
    WHERE CAST(schix.InsertDate AS DATE)
          BETWEEN @StartDate AND @EndDate
          AND schix.Status = CASE
                                 WHEN @Status = 4 THEN
                                     schix.Status
                                 ELSE
                                     @Status
                             END
          AND invoice.Kind_Flag IN (   CASE
                                           WHEN @InvoiceType = 0 THEN
                                               invoice.Kind_Flag
                                           WHEN @InvoiceType = 1 THEN
                                               7
                                           WHEN @InvoiceType = 2 THEN
                                               5
                                           WHEN @InvoiceType = 3 THEN
                                               9
                                       END
                                   )
          AND schix.Id IN (
                              SELECT MAX(hix.Id) Id
                              FROM dbo.SupplyChainHIXInvoice hix
                              WHERE CAST(hix.InsertDate AS DATE)
                              BETWEEN @StartDate AND @EndDate
                              GROUP BY hix.InvoiceId
                          )
    ORDER BY schix.Id;