CREATE OR REPLACE FUNCTION TIPO_NOTAFISCAL
   (P_CDFILIAL   IN NOTAFISCAL.CDFILIAL%TYPE,
    P_NRLANCTONF IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN STRING
AS
   V_COUNT_HANDLING INTEGER;
   V_COUNT_REFEICAO INTEGER;
BEGIN
   SELECT COUNT(*)
   INTO V_COUNT_REFEICAO
   FROM SERVPLANO SP, SERVICOCIA SC
   WHERE SP.CDFILIALNF = P_CDFILIAL   AND
         SP.NRLANCTONF = P_NRLANCTONF AND
         SC.CDSERVICO  = SP.CDSERVICO AND
         SC.CDCIA      = SP.CDCIA     AND
         SC.IDSERVICO  = '1';

   SELECT COUNT(*)
   INTO V_COUNT_HANDLING
   FROM SERVPLANO SP, SERVICOCIA SC
   WHERE SP.CDFILIALNF = P_CDFILIAL   AND
         SP.NRLANCTONF = P_NRLANCTONF AND
         SC.CDSERVICO  = SP.CDSERVICO AND
         SC.CDCIA      = SP.CDCIA     AND
         SC.IDSERVICO  = '3';

   IF (V_COUNT_REFEICAO = 0) THEN
      RETURN('S');
   ELSE
      RETURN('M');
   END IF;
END;
/


