IF EXISTS (SELECT
              NAME
           FROM
              SYSOBJECTS
           WHERE
              NAME = 'RETORNA_PEDIDOOL_NENVIADO' AND
              TYPE = 'TR')
   DROP TRIGGER RETORNA_PEDIDOOL_NENVIADO
GO

CREATE TRIGGER RETORNA_PEDIDOOL_NENVIADO
ON ITEMPEDIDO
FOR DELETE
AS

DECLARE @V_OLD_DSINCONITMOLEXP VARCHAR(30);
DECLARE @V_IDENVIOITMPEDOL     VARCHAR(1);

BEGIN

   SELECT @V_OLD_DSINCONITMOLEXP = MAX(CDFILIAL)+MAX(NRPEDIDO)+MAX(NRITEMPED)
     FROM DELETED;

   SELECT @V_IDENVIOITMPEDOL  = ISNULL(MAX(IDENVIOITMPEDOL), 'N')
     FROM PARASUPRI
    WHERE PARASUPRI.CDFILIAL = (SELECT ISNULL(FILIAL.CDFILCENTCMP, FILIAL.CDFILIAL)
                                  FROM FILIAL
                                 WHERE FILIAL.CDFILIAL = SUBSTRING(@V_OLD_DSINCONITMOLEXP, 1, 4));

   IF (@V_IDENVIOITMPEDOL = 'S') BEGIN

      SELECT @V_OLD_DSINCONITMOLEXP = MAX(CDFILIAL)+MAX(NRPEDIDO)+MAX(NRITEMPED)
        FROM DELETED;

      UPDATE ITEMPEDEXOL
         SET DSINCONITMOLEXP = NULL,
             IDEXPTITMOLEXP  = 'N'
       WHERE DSINCONITMOLEXP = @V_OLD_DSINCONITMOLEXP;
   END;
END;
GO


-- VERSAO --
BEGIN ATUVEROBJ('RETORNA_PEDIDOOL_NENVIADO','5.11.000'); END;
GO
