
/***************************************
* FUNCTION f_odetteCalcNAEL
*
* Compongo il primo campo delle odette campo 14 ( Change Index ) o codice NAEL + esponente di modifica particolare )
*
* Steamware, S.E.L.
* mod: 14/05/2013
*
****************************************/
create FUNCTION f_odetteCalcNAEL (@CRAGCL VARCHAR(50) , @TELND VARCHAR(50), @ECDNAE VARCHAR(50),@ECDNA1 VARCHAR(50)
                                           ,@EESPMD VARCHAR(50), @OCDODM VARCHAR(50)
)
RETURNS NVARCHAR(50) AS
BEGIN

    DECLARE @answ       NVARCHAR(50) = ''
    DECLARE @codNaelP1  NVARCHAR(50) = ''
    DECLARE @codNaelP2  NVARCHAR(50) = ''
    DECLARE @codNaelP3  NVARCHAR(50) = ''

    -- Se il cliente è BMW calcolo campi x NAEL
    SET @codNaelP1  = CASE WHEN dbo.f_trim(@CRAGCL) = '13' OR dbo.f_trim(SUBSTRING(@TELND,19,2)) = '13' THEN dbo.f_trim(@ECDNAE) ELSE '' END
    SET @codNaelP2  = CASE WHEN dbo.f_trim(@CRAGCL) = '13' OR dbo.f_trim(SUBSTRING(@TELND,19,2)) = '13' THEN dbo.f_trim(@ECDNA1) ELSE '' END
    SET @codNaelP3  = CASE WHEN dbo.f_trim(@CRAGCL) = '13' OR dbo.f_trim(SUBSTRING(@TELND,19,2)) = '13' THEN dbo.f_trim(@EESPMD) ELSE '' END

        
    SET @answ       = CASE WHEN dbo.f_trim(@CRAGCL) = '13' OR dbo.f_trim(SUBSTRING(@TELND,19,2)) = '13'         
                           THEN 'ZI:'+ @codNaelP1 + ' ' + @codNaelP2 + ' AI:'+ @codNaelP3 
                            ELSE @OCDODM   -- se non BMW
                      END

RETURN @answ

END
