create or replace PROCEDURE INTEGRA_CARDAPIO_WEBSERVICE
      (P_NRLOGINTCARDAMES IN NUMBER,
       P_IDERRO OUT VARCHAR2,
       P_NRCENARIOCARD IN NUMBER DEFAULT NULL)
AS
   V_CDFILIAL VARCHAR2(4);
   V_CDSERVFILI VARCHAR2(5);
   V_CDOPERADOR VARCHAR2(12);
   V_DTMESCARD DATE;
   V_COUNT NUMBER;
   V_QTDIABLOQCAR NUMBER;
   V_DTLIBE_DTREFE DATE;
   V_IDUTILDTPROC VARCHAR2(1);
   V_DTPROCESSA DATE;
   V_DTCARDAPIO DATE;
   V_NRCARDAPIO VARCHAR2(20);
   V_NRLOGINTCARDWS NUMBER;
   V_PERCENTUAL FLOAT;
   V_QTCLIESTISEG FLOAT;
   V_QTCLIESTITER FLOAT;
   V_QTCLIESTIQUA FLOAT;
   V_QTCLIESTIQUI FLOAT;
   V_QTCLIESTISEX FLOAT;
   V_QTCLIESTISAB FLOAT;
   V_QTCLIESTIDOM FLOAT;
   V_QTCLIESTIFER FLOAT;
   V_QTCOMESSERVICO FLOAT;
   V_DIASEMANA VARCHAR2(1);
   V_QTESTITURNO1 FLOAT;
   V_QTESTITURNO2 FLOAT;
   V_QTESTITURNO3 FLOAT;
   V_QTESTITURNO4 FLOAT;
   V_IDFERIADO VARCHAR2(1);
   V_QTCOMEST_SUM FLOAT;
   V_EXIST_CARDAMES NUMBER;
BEGIN
   P_IDERRO := 'N';
   V_NRLOGINTCARDWS := 0;
   SELECT CDFILIAL, CDSERVFILI, CDOPERADOR, TRUNC(DTMESCARD)
      INTO V_CDFILIAL, V_CDSERVFILI, V_CDOPERADOR, V_DTMESCARD
   FROM LOGINTCARDAMES
   WHERE NRLOGINTCARDAMES = P_NRLOGINTCARDAMES;
   SELECT COUNT(1)
     INTO V_COUNT
   FROM OPERADOR
   WHERE CDOPERADOR = V_CDOPERADOR;
   IF V_COUNT = 0 THEN
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, V_CDOPERADOR, 'Operador nao cadastrado');
   END IF;
   SELECT COUNT(1)
     INTO V_COUNT
   FROM FILIAL
   WHERE CDFILIAL = V_CDFILIAL;
   IF V_COUNT = 0 THEN
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, V_CDFILIAL, 'Unidade nao cadastrada');
   END IF;
   SELECT COUNT(1)
     INTO V_COUNT
   FROM SERVICO
   WHERE CDFILIAL = V_CDFILIAL
     AND CDSERVFILI = V_CDSERVFILI;
   IF V_COUNT = 0 THEN
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, V_CDSERVFILI, 'Servico nao cadastrado');
   END IF;
   IF V_DTMESCARD IS NULL THEN
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, 'Mes nao informado');
   END IF;
   SELECT NVL(QTDIABLOQCAR, 0), NVL(IDUTILDTPROC, 'N')
     INTO V_QTDIABLOQCAR, V_IDUTILDTPROC
   FROM PARAMGERAL;
   V_DTPROCESSA := TRUNC(SYSDATE);
   IF V_IDUTILDTPROC = 'S' THEN
      SELECT NVL(MAX(DTPROCESSA), SYSDATE)
        INTO V_DTPROCESSA
      FROM   PARAMFILIAL
      WHERE  CDFILIAL = V_CDFILIAL;
   END IF;
   IF V_DTMESCARD <= V_DTPROCESSA + V_QTDIABLOQCAR THEN
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(V_DTMESCARD), 'Cardapio bloqueado '||V_DTPROCESSA || ' - ' || V_QTDIABLOQCAR);
   END IF;
   SELECT MAX(DTLIBECARDMATR)
      INTO V_DTLIBE_DTREFE
   FROM CTRLAPROVCARD
   WHERE CDFILIAL = V_CDFILIAL
     AND CDSERVFILI = V_CDSERVFILI
     AND TRUNC(DTMESCARDAPROV) = V_DTMESCARD;
   IF V_DTLIBE_DTREFE IS NOT NULL THEN
      /*CARDAPIO LIBERADO DA MATRIZ PARA AS FILIAIS*/
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(V_DTMESCARD), 'Cardapio liberado da matriz para as filiais '||V_DTLIBE_DTREFE);
   END IF;
   SELECT MAX(DTREFECUSTMES)
      INTO V_DTLIBE_DTREFE
   FROM CARDAMES
   WHERE CDFILIAL = V_CDFILIAL
     AND CDSERVFILI = V_CDSERVFILI
     AND TRUNC(DTMESCARDAPIO) = V_DTMESCARD;
   IF V_DTLIBE_DTREFE IS NOT NULL THEN
      /*DATA DE REFERENCIA JA INFORMADA PARA O CARDAPIO*/
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(V_DTMESCARD), 'Data de referencia ja informada '||V_DTLIBE_DTREFE);
   END IF;
   FOR C_CONF IN (
      SELECT CDTIPOPRAT, NRSEQPRATCAR, NRLOGINTCOMPCARD
      FROM LOGINTCOMPCARD
      WHERE NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND NOT EXISTS(SELECT CF.CDFILIAL
                       FROM CONFCARD CF LEFT JOIN CONFCARDMESATIVO CFA ON CFA.CDFILIAL = CF.CDFILIAL AND CFA.CDSERVFILI = CF.CDSERVFILI AND CFA.NRSEQCNFCARD = CF.NRSEQCNFCARD
                       WHERE CF.CDFILIAL = V_CDFILIAL
                         AND CF.CDSERVFILI = V_CDSERVFILI
                         AND CF.NRSEQCNFCARD = LOGINTCOMPCARD.NRSEQPRATCAR
                         AND CF.CDTIPOPRAT = LOGINTCOMPCARD.CDTIPOPRAT
                         AND SUBSTR(NVL(CFA.IDMESATIVO,'SSSSSSSSSSSS'), TO_NUMBER(TO_CHAR(V_DTMESCARD, 'MM')),1) = 'S'
                      )
   ) LOOP
      /* CONFIGURACAO DO SERVICO DIFERENTE DO CARDAPIO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCOMPCARD)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, 'Tipo de Prato = ' ||C_CONF.CDTIPOPRAT|| ' - Sequencial = '||C_CONF.NRSEQPRATCAR, 'Configuracao do servico diferente do cardapio', C_CONF.NRLOGINTCOMPCARD);
   END LOOP;
   FOR C_CARD IN (
      SELECT DTCARDAPIO, NRLOGINTCARDADIA
      FROM LOGINTCARDADIA
      WHERE NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND NOT DTCARDAPIO BETWEEN V_DTMESCARD AND (ADD_MONTHS(V_DTMESCARD, 1)-1)
   ) LOOP
      /* EXISTE CARDAPIO FORA DO MES INFORMADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCARDADIA)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_CARD.DTCARDAPIO), 'Dias fora do mes do cardapio', C_CARD.NRLOGINTCARDADIA);
   END LOOP;
   FOR C_DIANUTIL IN (
      SELECT DTCARDAPIO, NRLOGINTCARDADIA
      FROM LOGINTCARDADIA
      WHERE NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND EXISTS(SELECT DTFERIFILI
                   FROM FERIFILI F, PARAMPLANEJA P
                   WHERE F.CDFILIAL = V_CDFILIAL
                     AND DTFERIFILI BETWEEN V_DTMESCARD AND (ADD_MONTHS(V_DTMESCARD, 1)-1)
                     AND P.CDFILIAL = V_CDFILIAL
                     AND NVL(IDCARDIAUTIL,'N') = 'S'
                     AND LOGINTCARDADIA.DTCARDAPIO = F.DTFERIFILI)
   ) LOOP
      /* EXISTE CARDAPIO PARA DIA NAO UTIL */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCARDADIA)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_DIANUTIL.DTCARDAPIO), 'Cardapio planejado para dia nao util', C_DIANUTIL.NRLOGINTCARDADIA);
   END LOOP;
   FOR C_COMENS IN(
      SELECT DTCARDAPIO, NRLOGINTCARDADIA
      FROM LOGINTCARDADIA
      WHERE NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND NVL(QTCOMESTICAR, 0) = 0
   ) LOOP
      /* COMENSAIS ESTIMADOS DO DIA IGUAL A ZERO OU NAO INFORMADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCARDADIA)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_COMENS.DTCARDAPIO), 'Comensais estimados do dia igual a zero ou nao informado', C_COMENS.NRLOGINTCARDADIA);
   END LOOP;
   FOR C_PRATO_SEMCOMENS IN(
      SELECT DTCARDAPIO, NRLOGINTCARDADIA
      FROM LOGINTCARDADIA L, SERVICO S
      WHERE L.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND S.CDFILIAL = V_CDFILIAL
        AND S.CDSERVFILI = V_CDSERVFILI
        AND (NVL(DECODE(TO_CHAR(L.DTCARDAPIO, 'D'),
                                '1', QTCLIESTIDOM,
                                '2', QTCLIESTISEG,
                                '3', QTCLIESTITER,
                                '4', QTCLIESTIQUA,
                                '5', QTCLIESTIQUI,
                                '6', QTCLIESTISEX,
                                '7', QTCLIESTISAB,
                                '8', QTCLIESTIFER), 0) = 0)
   ) LOOP
      /* PRATO PARA UM DIA SEM COMENSAIS ESTIMADOS INFORMADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCARDADIA)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_PRATO_SEMCOMENS.DTCARDAPIO), 'Prato informado para um dia sem comensais estimados parametrizado', C_PRATO_SEMCOMENS.NRLOGINTCARDADIA);
   END LOOP;
   FOR C_TPPRATO IN (
      SELECT C.DTCARDAPIO, P.CDTIPOPRAT, P.NRLOGINTCOMPCARD
      FROM LOGINTCOMPCARD P, LOGINTCARDADIA C
      WHERE P.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND C.NRLOGINTCARDAMES = P.NRLOGINTCARDAMES
        AND C.NRLOGINTCARDADIA = P.NRLOGINTCARDADIA
        AND NOT EXISTS(SELECT TP.CDTIPOPRAT
                       FROM TIPOPRAT TP
                       WHERE TP.CDTIPOPRAT = P.CDTIPOPRAT
                      )
   ) LOOP
      /* TIPO PRATO NAO CADASTRADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCOMPCARD)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_TPPRATO.DTCARDAPIO), 'Tipo de prato nao cadastrado - '||C_TPPRATO.CDTIPOPRAT, C_TPPRATO.NRLOGINTCOMPCARD);
   END LOOP;
   FOR C_PRATO IN (
      SELECT C.DTCARDAPIO, P.CDPRATO, P.CDFILAUXPRAT, P.NRLOGINTCOMPCARD
      FROM LOGINTCOMPCARD P, LOGINTCARDADIA C
      WHERE P.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND C.NRLOGINTCARDAMES = P.NRLOGINTCARDAMES
        AND C.NRLOGINTCARDADIA = P.NRLOGINTCARDADIA
        AND NOT EXISTS(SELECT PT.CDPRATO
                       FROM PRATO PT
                       WHERE PT.CDPRATO = P.CDPRATO
                         AND PT.CDFILAUXPRAT = P.CDFILAUXPRAT
                         AND (PT.CDFILAUXPRAT = 'P' OR PT.CDFILAUXPRAT = V_CDFILIAL)
                      )
   ) LOOP
      /* PRATO NAO CADASTRADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCOMPCARD)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_PRATO.DTCARDAPIO), 'Prato nao cadastrado - '||C_PRATO.CDPRATO ||' Arvore= '||C_PRATO.CDFILAUXPRAT, C_PRATO.NRLOGINTCOMPCARD);
   END LOOP;
   FOR C_CONSUMO IN (
      SELECT C.DTCARDAPIO, P.CDPRATO, P.CDFILAUXPRAT, P.NRLOGINTCOMPCARD
      FROM LOGINTCOMPCARD P, LOGINTCARDADIA C
      WHERE P.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND C.NRLOGINTCARDAMES = P.NRLOGINTCARDAMES
        AND C.NRLOGINTCARDADIA = P.NRLOGINTCARDADIA
        AND NVL(P.VRPECONSCARD, 0) = 0
   ) LOOP
      /* PERCENTUAL DE CONSUMO DO PRATO IGUAL ZERO OU NAO INFORMADO */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCOMPCARD)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, TRUNC(C_CONSUMO.DTCARDAPIO), 'Percentual de consumo do prato nao informado - '||C_CONSUMO.CDPRATO ||' - '||C_CONSUMO.CDFILAUXPRAT, C_CONSUMO.NRLOGINTCOMPCARD);
   END LOOP;
   FOR C_SEQ IN (
      SELECT CM.CDFILIAL, CM.CDSERVFILI, CM.DTMESCARD, CD.DTCARDAPIO, CP.NRSEQPRATCAR, CP.CDTIPOPRAT, CP.NRLOGINTCOMPCARD
      FROM LOGINTCARDAMES CM, LOGINTCARDADIA CD, LOGINTCOMPCARD CP
      WHERE CM.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
        AND CD.NRLOGINTCARDAMES = CM.NRLOGINTCARDAMES
        AND CP.NRLOGINTCARDAMES = CM.NRLOGINTCARDAMES
        AND CP.NRLOGINTCARDADIA = CD.NRLOGINTCARDADIA
      GROUP BY CM.CDFILIAL, CM.CDSERVFILI, CM.DTMESCARD, CD.DTCARDAPIO, CP.NRSEQPRATCAR, CP.CDTIPOPRAT, CP.NRLOGINTCOMPCARD
      HAVING COUNT(1) > 1
   ) LOOP
      /* EXISTE MAIS UM PRATO PARA O MESMO SEQUENCIAL */
      NOVO_CODIGO_SC('LOGINTCARDWS', V_NRLOGINTCARDWS, 1);
      INSERT INTO LOGINTCARDWS(NRLOGINTCARDWS, NRLOGINTCARDAMES, DSITEM, DSERRO, NRLOGINTCOMPCARD)
      VALUES(V_NRLOGINTCARDWS, P_NRLOGINTCARDAMES, C_SEQ.DTCARDAPIO, 'Existe mais de um prato para o mesmo sequencial - '||C_SEQ.NRSEQPRATCAR ||' Tipo de prato - '||C_SEQ.CDTIPOPRAT, C_SEQ.NRLOGINTCOMPCARD);
   END LOOP;
   IF V_NRLOGINTCARDWS = 0 THEN
      /*  VERIFICAR SE EXISTE CARDAPIO */
      SELECT COUNT(1)
         INTO V_COUNT
      FROM CARDADIA
      WHERE CDFILIAL  = V_CDFILIAL
        AND CDSERVFILI = V_CDSERVFILI
        AND TRUNC(DTMESCARD) = V_DTMESCARD;
      IF V_COUNT > 0 THEN
         INSERT INTO LOGINTCARDAPIO
            (NRLOGINTCARDAPIO, NRLOGINTCARDAMES, DTCARDAPIO, CDTIPOPRAT, CDPRATO, CDFILAUXPRAT, NRSEQPRAT, VRPECONSCARD, QTCOMESTICAR)
         (SELECT ROWNUM, P_NRLOGINTCARDAMES, CA.DTCARDAPIO, CP.CDTIPOPRAT, CP.CDPRATO, CP.CDFILAUXPRAT, CP.NRSEQPRATCAR, CP.VRPECONSCARD, CA.QTCOMESTICAR
          FROM CARDADIA CA, COMPCARD CP
          WHERE CA.CDFILIAL = V_CDFILIAL
            AND CA.CDSERVFILI = V_CDSERVFILI
            AND TRUNC(CA.DTMESCARD) = V_DTMESCARD
            AND CP.CDFILIAL = CA.CDFILIAL
            AND CP.NRCARDAPIO = CA.NRCARDAPIO
         );
      END IF;         
      FOR C_CARD IN
         (SELECT NRCARDAPIO
            FROM CARDADIA
           WHERE CDFILIAL = V_CDFILIAL
             AND CDSERVFILI = V_CDSERVFILI
             AND TRUNC(DTMESCARD) = V_DTMESCARD
         ) LOOP
            DELETE COMPCARD
             WHERE CDFILIAL = V_CDFILIAL
               AND NRCARDAPIO = C_CARD.NRCARDAPIO;
      END LOOP;
      DELETE CARDADIA
       WHERE CDFILIAL = V_CDFILIAL
         AND CDSERVFILI = V_CDSERVFILI
         AND TRUNC(DTMESCARD) = V_DTMESCARD;
      DELETE CNFCAMES
       WHERE CDFILIAL = V_CDFILIAL
         AND CDSERVFILI = V_CDSERVFILI
         AND TRUNC(DTREFECNFMES) = V_DTMESCARD;
      INSERT INTO CNFCAMES
        (CDFILIAL,     CDSERVFILI, NRSEQCNFMES,
         DTREFECNFMES, CDTIPOPRAT, IDOPCCNFMES,
         IDIMPCNFMES,  NRPAGCNFMES, NRORDCNFMES, DSCOMPLTPMES,
         IDCONCNFMES, IDTPPRATSEGM, IDTPPRATTERM,
         IDTPPRATQUAM, IDTPPRATQUIM, IDTPPRATSEXM,
         IDTPPRATSABM, IDTPPRATDOMM, IDTPPRATFERM)
      (SELECT
         CF.CDFILIAL,   CF.CDSERVFILI,CF.NRSEQCNFCARD,
         V_DTMESCARD,   CF.CDTIPOPRAT,   CF.IDOPCCNFCARD,
         CF.IDIMPCNFCARD,  1, CF.NRORDCNFCARD, CF.DSCOMPLTP,
         CF.IDCONCNFCARD, CF.IDTPPRATSEGP, CF.IDTPPRATTERP,
         CF.IDTPPRATQUAP, CF.IDTPPRATQUIP, CF.IDTPPRATSEXP,
         CF.IDTPPRATSABP, CF.IDTPPRATDOMP, CF.IDTPPRATFERP
       FROM CONFCARD CF LEFT JOIN CONFCARDMESATIVO CFA ON CFA.CDFILIAL = CF.CDFILIAL AND CFA.CDSERVFILI = CF.CDSERVFILI AND CFA.NRSEQCNFCARD = CF.NRSEQCNFCARD
       WHERE (CF.CDFILIAL   = V_CDFILIAL)
         AND (CF.CDSERVFILI = V_CDSERVFILI) 
         AND (SUBSTR(NVL(CFA.IDMESATIVO,'SSSSSSSSSSSS'), TO_NUMBER(TO_CHAR(V_DTMESCARD, 'MM')),1) = 'S'));
      /*VERIFICA SE EXISTE CARDAMES ENTAO INCLUI O NUMERO DO CENARIO */
      SELECT COUNT(1)
        INTO V_EXIST_CARDAMES
        FROM CARDAMES 
       WHERE CDFILIAL = V_CDFILIAL AND
             CDSERVFILI = V_CDSERVFILI AND
             TRUNC(DTMESCARDAPIO) = V_DTMESCARD;
      IF V_EXIST_CARDAMES > 0 THEN
         UPDATE CARDAMES SET NRCENARIOCARD = P_NRCENARIOCARD
          WHERE CDFILIAL = V_CDFILIAL AND CDSERVFILI = V_CDSERVFILI AND TRUNC(DTMESCARDAPIO) = V_DTMESCARD;
      ELSE
         INSERT INTO CARDAMES(CDFILIAL, CDSERVFILI, DTMESCARDAPIO, IDCARDLIBCENT, IDCARDLIBAGRE, IDCARDCICLO, CDOPERINCLUSAO, NRCENARIOCARD) 
              VALUES (V_CDFILIAL, V_CDSERVFILI, V_DTMESCARD, 'N', 'N', 'N', V_CDOPERADOR, P_NRCENARIOCARD);
      END IF;
      V_DTCARDAPIO := NULL;
      /*SELECT NVL(QTCLIESTISEG, 0), NVL(QTCLIESTITER, 0), NVL(QTCLIESTIQUA, 0), NVL(QTCLIESTIQUI, 0),
             NVL(QTCLIESTISEX, 0), NVL(QTCLIESTISAB, 0), NVL(QTCLIESTIDOM, 0), NVL(QTCLIESTIFER, 0)
        INTO V_QTCLIESTISEG, V_QTCLIESTITER, V_QTCLIESTIQUA, V_QTCLIESTIQUI,
             V_QTCLIESTISEX, V_QTCLIESTISAB, V_QTCLIESTIDOM, V_QTCLIESTIFER
      FROM SERVICO
      WHERE CDFILIAL = V_CDFILIAL
        AND CDSERVFILI = V_CDSERVFILI;*/
      FOR C_CARDAPIO IN
         (SELECT CM.CDFILIAL, CM.CDSERVFILI, CM.DTMESCARD,
                 CD.DTCARDAPIO, CD.QTCOMESTICAR, CD.QTCLIESTICAR, CD.QTFUNESTICAR, CD.QTDOAESTICAR,
                 CP.CDFILAUXPRAT, CP.CDPRATO, CP.NRSEQPRATCAR, CP.CDTIPOPRAT, CP.VRPECONSCARD, CNFCAMES.NRORDCNFMES
          FROM LOGINTCARDAMES CM, LOGINTCARDADIA CD, LOGINTCOMPCARD CP, CNFCAMES
          WHERE CM.NRLOGINTCARDAMES = P_NRLOGINTCARDAMES
            AND CD.NRLOGINTCARDAMES = CM.NRLOGINTCARDAMES
            AND CP.NRLOGINTCARDAMES = CM.NRLOGINTCARDAMES
            AND CP.NRLOGINTCARDADIA = CD.NRLOGINTCARDADIA
            AND CNFCAMES.CDFILIAL = V_CDFILIAL
            AND CNFCAMES.CDSERVFILI = V_CDSERVFILI
            AND TRUNC(DTREFECNFMES) = V_DTMESCARD
            AND CNFCAMES.NRSEQCNFMES = CP.NRSEQPRATCAR
          ORDER BY CD.DTCARDAPIO
         )LOOP
         IF (V_DTCARDAPIO IS NULL) OR (V_DTCARDAPIO <> C_CARDAPIO.DTCARDAPIO) THEN
            /* TURNOS */
            V_QTESTITURNO1 := NULL;
            V_QTESTITURNO2 := NULL;
            V_QTESTITURNO3 := NULL;
            V_QTESTITURNO4 := NULL;
            V_DIASEMANA := TO_CHAR(TRUNC(C_CARDAPIO.DTCARDAPIO), 'D');
            SELECT DECODE(MAX(DTFERIADO), NULL, 'N', 'S')
              INTO V_IDFERIADO
            FROM(SELECT DTFERIFILI AS DTFERIADO
                 FROM   FERIFILI F
                 WHERE  F.CDFILIAL = V_CDFILIAL  AND
                        DTFERIFILI = C_CARDAPIO.DTCARDAPIO
                 UNION ALL
                 SELECT DTFERICARD AS DTFERIADO
                 FROM   FERICARD
                 WHERE  CDFILIAL = V_CDFILIAL AND
                        DTFERICARD = C_CARDAPIO.DTCARDAPIO
                 );
            IF V_IDFERIADO = 'S' THEN
               V_DIASEMANA := '8';
            END IF;
            V_QTCOMESSERVICO := C_CARDAPIO.QTCLIESTICAR;/*CASE V_DIASEMANA
                                WHEN '1' THEN V_QTCLIESTIDOM
                                WHEN '2' THEN V_QTCLIESTISEG
                                WHEN '3' THEN V_QTCLIESTITER
                                WHEN '4' THEN V_QTCLIESTIQUA
                                WHEN '5' THEN V_QTCLIESTIQUI
                                WHEN '6' THEN V_QTCLIESTISEX
                                WHEN '7' THEN V_QTCLIESTISAB
                                WHEN '8' THEN V_QTCLIESTIFER END;*/
            IF V_QTCOMESSERVICO > 0 THEN
               V_PERCENTUAL := (C_CARDAPIO.QTCOMESTICAR * 100)/V_QTCOMESSERVICO;
               FOR C_TURNO IN
                  (SELECT CDTURNO, NVL(TRUNC(DECODE(V_DIASEMANA,
                                                   '1', QTCOMESTTDOM,
                                                   '2', QTCOMESTTSEG,
                                                   '3', QTCOMESTTTER,
                                                   '4', QTCOMESTTQUA,
                                                   '5', QTCOMESTTQUI,
                                                   '6', QTCOMESTTSEX,
                                                   '7', QTCOMESTTSAB,
                                                   '8', QTCOMESTTFER) * V_PERCENTUAL / 100), 0) AS QTCOMESTURNO
                   FROM TURNOSERV
                   WHERE (CDFILIAL = V_CDFILIAL)
                     AND (CDSERVFILI = V_CDSERVFILI)
                   ORDER BY CDTURNO
               ) LOOP
                  IF C_TURNO.CDTURNO = '1' THEN
                     V_QTESTITURNO1 := C_TURNO.QTCOMESTURNO;
                  END IF;
                  IF C_TURNO.CDTURNO = '2' THEN
                     V_QTESTITURNO2 := C_TURNO.QTCOMESTURNO;
                  END IF;
                  IF C_TURNO.CDTURNO = '3' THEN
                     V_QTESTITURNO3 := C_TURNO.QTCOMESTURNO;
                  END IF;
                  IF C_TURNO.CDTURNO = '4' THEN
                     V_QTESTITURNO4 := C_TURNO.QTCOMESTURNO;
                  END IF;
               END LOOP;
               V_QTCOMEST_SUM := NVL(V_QTESTITURNO1, 0) + NVL(V_QTESTITURNO2, 0) +
                                 NVL(V_QTESTITURNO3, 0) + NVL(V_QTESTITURNO4, 0);
               IF V_QTCOMEST_SUM < C_CARDAPIO.QTCOMESTICAR THEN
                   IF V_QTESTITURNO1 <> 0 THEN
                      V_QTESTITURNO1 := V_QTESTITURNO1 + (C_CARDAPIO.QTCOMESTICAR - V_QTCOMEST_SUM);
                   ELSIF V_QTESTITURNO2 <> 0 THEN
                      V_QTESTITURNO2 := V_QTESTITURNO2 + (C_CARDAPIO.QTCOMESTICAR - V_QTCOMEST_SUM);
                   ELSIF V_QTESTITURNO3 <> 0 THEN
                      V_QTESTITURNO3 := V_QTESTITURNO3 + (C_CARDAPIO.QTCOMESTICAR - V_QTCOMEST_SUM);
                   ELSIF V_QTESTITURNO4 <> 0 THEN
                      V_QTESTITURNO4 := V_QTESTITURNO4 + (C_CARDAPIO.QTCOMESTICAR - V_QTCOMEST_SUM);
                   END IF;
               END IF;
               IF V_QTCOMEST_SUM > C_CARDAPIO.QTCOMESTICAR THEN
                   IF V_QTESTITURNO1 <> 0 THEN
                      V_QTESTITURNO1 := V_QTESTITURNO1 - (V_QTCOMEST_SUM - C_CARDAPIO.QTCOMESTICAR);
                   ELSIF V_QTESTITURNO2 <> 0 THEN
                      V_QTESTITURNO2 := V_QTESTITURNO2 - (V_QTCOMEST_SUM - C_CARDAPIO.QTCOMESTICAR);
                   ELSIF V_QTESTITURNO3 <> 0 THEN
                      V_QTESTITURNO3 := V_QTESTITURNO3 - (V_QTCOMEST_SUM - C_CARDAPIO.QTCOMESTICAR);
                   ELSIF V_QTESTITURNO3 <> 0 THEN
                      V_QTESTITURNO3 := V_QTESTITURNO3 - (V_QTCOMEST_SUM - C_CARDAPIO.QTCOMESTICAR);
                   ELSIF V_QTESTITURNO4 <> 0 THEN
                      V_QTESTITURNO4 := V_QTESTITURNO4 - (V_QTCOMEST_SUM - C_CARDAPIO.QTCOMESTICAR);
                   END IF;
               END IF;
            END IF;
            /* FIM TURNOS */
            NOVO_CODIGO_SC('CARDAPIO'||V_CDFILIAL, V_NRCARDAPIO, 1);
            V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);
            INSERT INTO CARDADIA
               (CDFILIAL, NRCARDAPIO, CDSERVFILI, CDOPERADOR, DTCARDAPIO, QTCOMESTICAR, QTCLIESTICAR, QTFUNESTICAR, QTDOAESTICAR, DTMESCARD, DSRDMUSUCARD,
                QTESTITURNO1, QTESTITURNO2, QTESTITURNO3, QTESTITURNO4)
            VALUES
               (V_CDFILIAL, V_NRCARDAPIO, V_CDSERVFILI, V_CDOPERADOR, TRUNC(C_CARDAPIO.DTCARDAPIO), C_CARDAPIO.QTCOMESTICAR, C_CARDAPIO.QTCLIESTICAR, 
                C_CARDAPIO.QTFUNESTICAR, C_CARDAPIO.QTDOAESTICAR, C_CARDAPIO.DTMESCARD, 'WebService-IntegracaoCardapio',
                V_QTESTITURNO1, V_QTESTITURNO2, V_QTESTITURNO3, V_QTESTITURNO4);
         END IF;
         INSERT INTO COMPCARD
            (CDFILIAL, NRCARDAPIO, NRSEQPRATCAR, CDFILAUXPRAT, CDPRATO, VRPECONSCARD, CDTIPOPRAT,
             NRORDPRATCAR, QTSOBRLIMPPR, QTCOMEFETPRA, QTPRODUZPRAT)
         VALUES
            (V_CDFILIAL, V_NRCARDAPIO, C_CARDAPIO.NRSEQPRATCAR, C_CARDAPIO.CDFILAUXPRAT, C_CARDAPIO.CDPRATO, C_CARDAPIO.VRPECONSCARD, C_CARDAPIO.CDTIPOPRAT,
             C_CARDAPIO.NRORDCNFMES, 0, 0,   0);
         V_DTCARDAPIO := C_CARDAPIO.DTCARDAPIO;
      END LOOP;
   END IF;
   IF V_NRLOGINTCARDWS <> 0 THEN
      P_IDERRO := 'S';
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('INTEGRA_CARDAPIO_WEBSERVICE','6.22.000'); END;
/