ALTER TABLE JUSTALTERCOMENS ADD DTCARDAPIO DATE DEFAULT SYSDATE NOT NULL
/

CREATE OR REPLACE PROCEDURE EXPORTA_COMENSAIS
   (P_CDFILIAL   	  IN VARCHAR2,
    P_CDSERVFILI 	  IN VARCHAR2,
    P_DTINICIAL  	  IN DATE,
    P_DTFINAL    	  IN DATE,
    P_IDINSERIR  	  IN VARCHAR2,
    P_CDOPERADOR 	  IN VARCHAR2,
    P_LDIETA     	  IN VARCHAR2,
	 P_JUSTALTCOMENS IN VARCHAR2 DEFAULT NULL,
	 P_ACAOALTCOMENS IN VARCHAR2 DEFAULT NULL) IS

   V_IDCARDIAUTIL VARCHAR2(1);
   V_CONTFERIFILI NUMBER;
   V_CONTFERICARD NUMBER;
   V_DIASEMANA VARCHAR2(1);
   V_CONTCARD NUMBER;
   V_NRCARDAPIO VARCHAR2(20);
   V_QTCOMESTICAR FLOAT(126);
   V_QTCLIESTICAR FLOAT(126);
   V_QTFUNESTICAR FLOAT(126);
   V_QTDOAESTICAR FLOAT(126);
   V_QTTURNO1 FLOAT(126);
   V_QTTURNO2 FLOAT(126);
   V_QTTURNO3 FLOAT(126);
   V_QTTURNO4 FLOAT(126);
   V_QTCOESTCS FLOAT(126);

    CURSOR C_TURNOSERV IS
    SELECT SUM(T1),SUM(T2),SUM(T3),SUM(T4)
    FROM
    (SELECT
        NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T1,
        0 T2,
        0 T3,
        0 T4
     FROM TURNOSERV
     WHERE
        CDFILIAL = P_CDFILIAL AND
        CDSERVFILI = P_CDSERVFILI AND
        CDTURNO = '1'
     UNION
     SELECT
        0 T1,
        NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T2,
        0 T3,
        0 T4
     FROM TURNOSERV
     WHERE
        CDFILIAL = P_CDFILIAL AND
        CDSERVFILI = P_CDSERVFILI AND
        CDTURNO = '2'
     UNION
     SELECT
        0 T1,
        0 T2,
        NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T3,
        0 T4
     FROM TURNOSERV
     WHERE
        CDFILIAL = P_CDFILIAL AND
        CDSERVFILI = P_CDSERVFILI AND
        CDTURNO = '3'
     UNION
     SELECT
        0 T1,
        0 T2,
        0 T3,
        NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T4
     FROM TURNOSERV
     WHERE
        CDFILIAL = P_CDFILIAL AND
        CDSERVFILI = P_CDSERVFILI AND
        CDTURNO = '4'
     );

     PROCEDURE SETLOGALTCOMENS (P_JUST IN VARCHAR2, P_ACAO IN VARCHAR2, P_FILIAL IN VARCHAR2, P_SERVICO IN VARCHAR2, P_OPER IN VARCHAR2, P_DTCARD IN DATE, P_NEWQTCOMESTICAR IN FLOAT)
     IS
		V_OLDQTCOMESTICAR FLOAT(126);
		V_NRJUSTALTERCOMENS NUMBER;
     BEGIN
		
		SELECT QTCOMESTICAR
		  INTO V_OLDQTCOMESTICAR
		  FROM CARDADIA
		 WHERE CDFILIAL = P_FILIAL
			AND CDSERVFILI = P_SERVICO
			AND TRUNC(DTCARDAPIO) = P_DTCARD;

		NOVO_CODIGO('JUSTALTERCOMENS', V_NRJUSTALTERCOMENS, 1);
			
		INSERT INTO JUSTALTERCOMENS (NRJUSTALTERCOMENS, CDFILIAL, CDSERVFILI, DTCARDAPIO, QTCOMENSTOTOLD, QTCOMENSTOTNEW,
											  DTHRALTERCOMENS, CDOPERADOR, DSJUSTIFICATIVA, DSACAO, IDORIGALTERACAO)
		VALUES (V_NRJUSTALTERCOMENS, P_FILIAL, P_SERVICO, P_DTCARD, V_OLDQTCOMESTICAR, P_NEWQTCOMESTICAR,
				  SYSDATE, P_OPER, P_JUST, P_ACAO, 'EXPORTARCOMENS');			
     END;
BEGIN
   SELECT NVL(MAX(IDCARDIAUTIL), 'N')
   INTO V_IDCARDIAUTIL
   FROM PARAMPLANEJA
   WHERE CDFILIAL = P_CDFILIAL;

   IF V_IDCARDIAUTIL IS NULL THEN
      V_IDCARDIAUTIL := 'N';
   END IF;


   FOR C_SERVICO IN
   (SELECT
       IDTIPOSERV,
       NVL(QTCOMESTISEG,0) QTCOMESTISEG,
       NVL(QTCLIESTISEG,0) QTCLIESTISEG,
       NVL(QTFUNESTISEG,0) QTFUNESTISEG,
       NVL(QTDOAESTISEG,0) QTDOAESTISEG,
       NVL(QTCOMESTITER,0) QTCOMESTITER,
       NVL(QTCLIESTITER,0) QTCLIESTITER,
       NVL(QTFUNESTITER,0) QTFUNESTITER,
       NVL(QTDOAESTITER,0) QTDOAESTITER,
       NVL(QTCOMESTIQUA,0) QTCOMESTIQUA,
       NVL(QTCLIESTIQUA,0) QTCLIESTIQUA,
       NVL(QTFUNESTIQUA,0) QTFUNESTIQUA,
       NVL(QTDOAESTIQUA,0) QTDOAESTIQUA,
       NVL(QTCOMESTIQUI,0) QTCOMESTIQUI,
       NVL(QTCLIESTIQUI,0) QTCLIESTIQUI,
       NVL(QTFUNESTIQUI,0) QTFUNESTIQUI,
       NVL(QTDOAESTIQUI,0) QTDOAESTIQUI,
       NVL(QTCOMESTISEX,0) QTCOMESTISEX,
       NVL(QTCLIESTISEX,0) QTCLIESTISEX,
       NVL(QTFUNESTISEX,0) QTFUNESTISEX,
       NVL(QTDOAESTISEX,0) QTDOAESTISEX,
       NVL(QTCOMESTISAB,0) QTCOMESTISAB,
       NVL(QTCLIESTISAB,0) QTCLIESTISAB,
       NVL(QTFUNESTISAB,0) QTFUNESTISAB,
       NVL(QTDOAESTISAB,0) QTDOAESTISAB,
       NVL(QTCOMESTIDOM,0) QTCOMESTIDOM,
       NVL(QTCLIESTIDOM,0) QTCLIESTIDOM,
       NVL(QTFUNESTIDOM,0) QTFUNESTIDOM,
       NVL(QTDOAESTIDOM,0) QTDOAESTIDOM,
       NVL(QTCOMESTIFER,0) QTCOMESTIFER,
       NVL(QTCLIESTIFER,0) QTCLIESTIFER,
       NVL(QTFUNESTIFER,0) QTFUNESTIFER,
       NVL(QTDOAESTIFER,0) QTDOAESTIFER
    FROM SERVICO
    WHERE
       CDFILIAL = P_CDFILIAL AND
       CDSERVFILI = P_CDSERVFILI)
   LOOP
      FOR C_DATA IN
      (SELECT TRUNC(DTDATA) AS DTCARDAPIO,
              DTMES
       FROM DATA
       WHERE
          (DTDATA BETWEEN P_DTINICIAL AND P_DTFINAL)
       ORDER BY DTDATA)
      LOOP

         SELECT COUNT(*)
         INTO V_CONTFERIFILI
         FROM FERIFILI
         WHERE
            CDFILIAL = P_CDFILIAL AND
            DTFERIFILI = C_DATA.DTCARDAPIO;

         SELECT COUNT(*)
         INTO V_CONTFERICARD
         FROM FERICARD
         WHERE
            CDFILIAL = P_CDFILIAL AND
            DTFERICARD = C_DATA.DTCARDAPIO;

         V_DIASEMANA := TO_CHAR(C_DATA.DTCARDAPIO, 'D');

         OPEN C_TURNOSERV;
         FETCH C_TURNOSERV INTO
            V_QTTURNO1,
            V_QTTURNO2,
            V_QTTURNO3,
            V_QTTURNO4;
         IF C_TURNOSERV%NOTFOUND THEN
            V_QTTURNO1 := 0;
            V_QTTURNO2 := 0;
            V_QTTURNO3 := 0;
            V_QTTURNO4 := 0;
         END IF;
         CLOSE C_TURNOSERV;

         IF (C_SERVICO.IDTIPOSERV = 'N') THEN

            SELECT COUNT(*)
            INTO V_CONTCARD
            FROM CARDADIA
            WHERE
               CDFILIAL = P_CDFILIAL AND
               CDSERVFILI = P_CDSERVFILI AND
               TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO;
            IF (V_CONTFERIFILI = 0) OR (V_IDCARDIAUTIL = 'N') THEN
               IF (V_CONTFERICARD = 0) THEN
                  IF (V_DIASEMANA = '1') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTIDOM;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTIDOM;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTIDOM;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTIDOM;
                  END IF;
                  IF (V_DIASEMANA = '2') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTISEG;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTISEG;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTISEG;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTISEG;
                  END IF;
                  IF (V_DIASEMANA = '3') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTITER;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTITER;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTITER;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTITER;
                  END IF;
                  IF (V_DIASEMANA = '4') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTIQUA;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTIQUA;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTIQUA;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTIQUA;
                  END IF;
                  IF (V_DIASEMANA = '5') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTIQUI;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTIQUI;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTIQUI;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTIQUI;
                  END IF;
                  IF (V_DIASEMANA = '6') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTISEX;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTISEX;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTISEX;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTISEX;
                  END IF;
                  IF (V_DIASEMANA = '7') THEN
                     V_QTCOMESTICAR := C_SERVICO.QTCOMESTISAB;
                     V_QTCLIESTICAR := C_SERVICO.QTCLIESTISAB;
                     V_QTFUNESTICAR := C_SERVICO.QTFUNESTISAB;
                     V_QTDOAESTICAR := C_SERVICO.QTDOAESTISAB;
                  END IF;
                  IF (V_QTCOMESTICAR <> 0) THEN
                     IF (V_CONTCARD = 0) THEN
                        IF (P_IDINSERIR = 'S') THEN

                           V_NRCARDAPIO := NULL;
                           NOVO_CODIGO('CARDAPIO'||P_CDFILIAL, V_NRCARDAPIO, 1);
                           V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                           INSERT INTO CARDADIA
                           (CDFILIAL,
                            NRCARDAPIO,
                            CDSERVFILI,
                            CDOPERADOR,
                            DTCARDAPIO,
                            QTCOMESTICAR,
                            QTCLIESTICAR,
                            QTFUNESTICAR,
                            QTDOAESTICAR,
                            QTESTITURNO1,
                            QTESTITURNO2,
                            QTESTITURNO3,
                            QTESTITURNO4,
                            DTULTATUACAR,
                            DTMESCARD)
                           VALUES
                           (P_CDFILIAL,
                            V_NRCARDAPIO,
                            P_CDSERVFILI,
                            P_CDOPERADOR,
                            C_DATA.DTCARDAPIO,
                            V_QTCOMESTICAR,
                            V_QTCLIESTICAR,
                            V_QTFUNESTICAR,
                            V_QTDOAESTICAR,
                            V_QTTURNO1,
                            V_QTTURNO2,
                            V_QTTURNO3,
                            V_QTTURNO4,
                            SYSDATE,
                            C_DATA.DTMES);


                            /* DELETA PONTO DE ENTREGA ANTIGO */
                            DELETE FROM CARDACLI CC
                            WHERE CC.CDFILIAL = P_CDFILIAL
                              AND EXISTS (SELECT C.NRCARDAPIO
                                          FROM CARDADIA C
                                          WHERE C.CDSERVFILI  = P_CDSERVFILI AND
                                                TRUNC(C.DTCARDAPIO)  = C_DATA.DTCARDAPIO AND
                                                CC.CDFILIAL   = C.CDFILIAL AND
                                                CC.NRCARDAPIO = C.NRCARDAPIO);

                            FOR C_PONTOENT IN
                               (SELECT
                                   CDFILIAL,
                                   CDSERVFILI,
                                   CDCLIENTE,
                                   CDDESTFILI,
                                   QTCOESTCSSEG,
                                   QTCOESTCSTER,
                                   QTCOESTCSQUA,
                                   QTCOESTCSQUI,
                                   QTCOESTCSSEX,
                                   QTCOESTCSSAB,
                                   QTCOESTCSDOM
                                FROM
                                   DESTSRVCLI
                                WHERE
                                   CDFILIAL   = P_CDFILIAL  AND
                                   CDSERVFILI = P_CDSERVFILI
                                ORDER BY
                                   CDCLIENTE)
                            LOOP
                               V_QTCOESTCS := 0;
                               IF (V_DIASEMANA = '1') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSDOM;
                               END IF;
                               IF (V_DIASEMANA = '2') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSSEG;
                               END IF;
                               IF (V_DIASEMANA = '3') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSTER;
                               END IF;
                               IF (V_DIASEMANA = '4') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSQUA;
                               END IF;
                               IF (V_DIASEMANA = '5') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSQUI;
                               END IF;
                               IF (V_DIASEMANA = '6') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSSEX;
                               END IF;
                               IF (V_DIASEMANA = '7') THEN
                                  V_QTCOESTCS := C_PONTOENT.QTCOESTCSSAB;
                               END IF;
                               INSERT INTO CARDACLI
                                  (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                               VALUES
                                  (C_PONTOENT.CDFILIAL, V_NRCARDAPIO, C_PONTOENT.CDCLIENTE, C_PONTOENT.CDDESTFILI, V_QTCOESTCS);
                            END LOOP;
                        END IF;
                     ELSE
                        IF (P_JUSTALTCOMENS IS NOT NULL) THEN
									SETLOGALTCOMENS (P_JUSTALTCOMENS, P_ACAOALTCOMENS, P_CDFILIAL, P_CDSERVFILI, P_CDOPERADOR, C_DATA.DTCARDAPIO, V_QTCOMESTICAR);
								END IF;

								UPDATE CARDADIA
                        SET CDOPERADOR   = P_CDOPERADOR,
                            DTULTATUACAR = SYSDATE,
                            QTCOMESTICAR = V_QTCOMESTICAR,
                            QTCLIESTICAR = V_QTCLIESTICAR,
                            QTFUNESTICAR = V_QTFUNESTICAR,
                            QTDOAESTICAR = V_QTDOAESTICAR,
                            QTESTITURNO1 = V_QTTURNO1,
                            QTESTITURNO2 = V_QTTURNO2,
                            QTESTITURNO3 = V_QTTURNO3,
                            QTESTITURNO4 = V_QTTURNO4
                        WHERE
                            CDFILIAL = P_CDFILIAL AND
                            CDSERVFILI = P_CDSERVFILI AND
                            TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO;

                        /* DELETA PONTO DE ENTREGA ANTIGO */
                        DELETE FROM CARDACLI CC
                        WHERE
                           CC.CDFILIAL = P_CDFILIAL AND
                           EXISTS (SELECT
                                      C.NRCARDAPIO
                                   FROM
                                      CARDADIA C
                                   WHERE
                                      C.CDSERVFILI  = P_CDSERVFILI AND
                                      TRUNC(C.DTCARDAPIO)  = C_DATA.DTCARDAPIO AND
                                      CC.CDFILIAL   = C.CDFILIAL AND
                                      CC.NRCARDAPIO = C.NRCARDAPIO);
                        /* ================================ */

                        /* INSERE PONTO DE ENTREGA ======== */
                        FOR C_PONTOENT IN
                           (SELECT
                               CDFILIAL,
                               CDSERVFILI,
                               CDCLIENTE,
                               CDDESTFILI,
                               QTCOESTCSSEG,
                               QTCOESTCSTER,
                               QTCOESTCSQUA,
                               QTCOESTCSQUI,
                               QTCOESTCSSEX,
                               QTCOESTCSSAB,
                               QTCOESTCSDOM
                            FROM
                               DESTSRVCLI
                            WHERE
                               CDFILIAL   = P_CDFILIAL  AND
                               CDSERVFILI = P_CDSERVFILI
                            ORDER BY
                               CDCLIENTE)
                        LOOP
                           V_QTCOESTCS := 0;
                           IF (V_DIASEMANA = '1') THEN /* DOMINGO */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSDOM;
                           END IF;
                           IF (V_DIASEMANA = '2') THEN /* SEGUNDA */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSSEG;
                           END IF;
                           IF (V_DIASEMANA = '3') THEN /* TERCA */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSTER;
                           END IF;
                           IF (V_DIASEMANA = '4') THEN /* QUARTA */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSQUA;
                           END IF;
                           IF (V_DIASEMANA = '5') THEN /* QUINTA */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSQUI;
                           END IF;
                           IF (V_DIASEMANA = '6') THEN /* SEXTA */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSSEX;
                           END IF;
                           IF (V_DIASEMANA = '7') THEN /* SABADO */
                              V_QTCOESTCS := C_PONTOENT.QTCOESTCSSAB;
                           END IF;
                           V_NRCARDAPIO := NULL;
                           SELECT
                              NRCARDAPIO INTO V_NRCARDAPIO
                           FROM
                              CARDADIA
                           WHERE
                              CDFILIAL   = P_CDFILIAL   AND
                              CDSERVFILI = P_CDSERVFILI AND
                              TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO;
                           INSERT INTO CARDACLI
                              (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                           VALUES
                              (P_CDFILIAL, V_NRCARDAPIO, C_PONTOENT.CDCLIENTE, C_PONTOENT.CDDESTFILI, V_QTCOESTCS);
                        END LOOP;
                        /* ================================ */
                     END IF;
                  END IF;
               ELSE
                  V_QTCOMESTICAR := C_SERVICO.QTCOMESTIFER;
                  V_QTCLIESTICAR := C_SERVICO.QTCLIESTIFER;
                  V_QTFUNESTICAR := C_SERVICO.QTFUNESTIFER;
                  V_QTDOAESTICAR := C_SERVICO.QTDOAESTIFER;

                  IF (V_QTCOMESTICAR <> 0) THEN
                     IF (V_CONTCARD = 0) THEN
                        IF (P_IDINSERIR = 'S') THEN
                           V_NRCARDAPIO := NULL;
                           NOVO_CODIGO('CARDAPIO'||P_CDFILIAL, V_NRCARDAPIO, 1);
                           V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                           INSERT INTO CARDADIA
                           (CDFILIAL,
                            NRCARDAPIO,
                            CDSERVFILI,
                            CDOPERADOR,
                            DTCARDAPIO,
                            QTCOMESTICAR,
                            QTCLIESTICAR,
                            QTFUNESTICAR,
                            QTDOAESTICAR,
                            QTESTITURNO1,
                            QTESTITURNO2,
                            QTESTITURNO3,
                            QTESTITURNO4,
                            DTULTATUACAR,
                            DTMESCARD)
                           VALUES
                           (P_CDFILIAL,
                            V_NRCARDAPIO,
                            P_CDSERVFILI,
                            P_CDOPERADOR,
                            C_DATA.DTCARDAPIO,
                            V_QTCOMESTICAR,
                            V_QTCLIESTICAR,
                            V_QTFUNESTICAR,
                            V_QTDOAESTICAR,
                            V_QTTURNO1,
                            V_QTTURNO2,
                            V_QTTURNO3,
                            V_QTTURNO4,
                            SYSDATE,
                            C_DATA.DTMES);

                            /* DELETA PONTO DE ENTREGA ANTIGO */
                            DELETE FROM CARDACLI CC
                            WHERE CC.CDFILIAL = P_CDFILIAL
                              AND EXISTS (SELECT C.NRCARDAPIO
                                          FROM CARDADIA C
                                          WHERE C.CDSERVFILI  = P_CDSERVFILI AND
                                                TRUNC(C.DTCARDAPIO)  = C_DATA.DTCARDAPIO AND
                                                CC.CDFILIAL   = C.CDFILIAL AND
                                                CC.NRCARDAPIO = C.NRCARDAPIO);

                            FOR C_PONTOENT IN
                               (SELECT
                                   CDFILIAL,
                                   CDSERVFILI,
                                   CDCLIENTE,
                                   CDDESTFILI,
                                   QTCOESTCSFER
                                FROM
                                   DESTSRVCLI
                                WHERE
                                   CDFILIAL   = P_CDFILIAL  AND
                                   CDSERVFILI = P_CDSERVFILI
                                ORDER BY
                                   CDCLIENTE)
                            LOOP
                               INSERT INTO CARDACLI
                                  (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                               VALUES
                                  (C_PONTOENT.CDFILIAL, V_NRCARDAPIO, C_PONTOENT.CDCLIENTE, C_PONTOENT.CDDESTFILI, C_PONTOENT.QTCOESTCSFER);
                            END LOOP;
                         END IF;
                     ELSE
                        IF (P_JUSTALTCOMENS IS NOT NULL) THEN
									SETLOGALTCOMENS (P_JUSTALTCOMENS, P_ACAOALTCOMENS, P_CDFILIAL, P_CDSERVFILI, P_CDOPERADOR, C_DATA.DTCARDAPIO, V_QTCOMESTICAR);
								END IF;

                        UPDATE CARDADIA
                        SET CDOPERADOR   = P_CDOPERADOR,
                            DTULTATUACAR = SYSDATE,
                            QTCOMESTICAR = V_QTCOMESTICAR,
                            QTCLIESTICAR = V_QTCLIESTICAR,
                            QTFUNESTICAR = V_QTFUNESTICAR,
                            QTDOAESTICAR = V_QTDOAESTICAR,
                            QTESTITURNO1 = V_QTTURNO1,
                            QTESTITURNO2 = V_QTTURNO2,
                            QTESTITURNO3 = V_QTTURNO3,
                            QTESTITURNO4 = V_QTTURNO4
                        WHERE
                            CDFILIAL   = P_CDFILIAL   AND
                            CDSERVFILI = P_CDSERVFILI AND
                            TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO;

                        /* DELETA PONTO DE ENTREGA ANTIGO */
                        DELETE FROM CARDACLI CC
                        WHERE
                           CC.CDFILIAL = P_CDFILIAL AND
                           EXISTS (SELECT
                                      C.NRCARDAPIO
                                   FROM
                                      CARDADIA C
                                   WHERE
                                      C.CDSERVFILI  = P_CDSERVFILI AND
                                      TRUNC(C.DTCARDAPIO)  = C_DATA.DTCARDAPIO AND
                                      CC.CDFILIAL   = C.CDFILIAL AND
                                      CC.NRCARDAPIO = C.NRCARDAPIO);
                        /* ================================ */

                        /* INSERE PONTO DE ENTREGA ======== */
                        FOR C_PONTOENT IN
                           (SELECT
                               CDFILIAL,
                               CDSERVFILI,
                               CDCLIENTE,
                               CDDESTFILI,
                               QTCOESTCSFER
                            FROM
                               DESTSRVCLI
                            WHERE
                               CDFILIAL   = P_CDFILIAL  AND
                               CDSERVFILI = P_CDSERVFILI
                            ORDER BY
                               CDCLIENTE)
                        LOOP
                           V_NRCARDAPIO := NULL;
                           SELECT
                              NRCARDAPIO INTO V_NRCARDAPIO
                           FROM
                              CARDADIA
                           WHERE
                              CDFILIAL   = P_CDFILIAL   AND
                              CDSERVFILI = P_CDSERVFILI AND
                              TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO;
                           INSERT INTO CARDACLI
                              (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                           VALUES
                              (P_CDFILIAL, V_NRCARDAPIO, C_PONTOENT.CDCLIENTE, C_PONTOENT.CDDESTFILI, C_PONTOENT.QTCOESTCSFER);
                        END LOOP;
                        /* ================================ */
                     END IF;
                  END IF;
               END IF;
            END IF;

         END IF;
         IF (C_SERVICO.IDTIPOSERV = 'D') THEN

            FOR C_DIETSERV IN
            (SELECT
                CDDIETPAD,
                NVL(QTCOMDIETSEG,0) QTCOMDIETSEG,
                NVL(QTCLIDIETSEG,0) QTCLIDIETSEG,
                NVL(QTFUNDIETSEG,0) QTFUNDIETSEG,
                NVL(QTDOADIETSEG,0) QTDOADIETSEG,
                NVL(QTCOMDIETTER,0) QTCOMDIETTER,
                NVL(QTCLIDIETTER,0) QTCLIDIETTER,
                NVL(QTFUNDIETTER,0) QTFUNDIETTER,
                NVL(QTDOADIETTER,0) QTDOADIETTER,
                NVL(QTCOMDIETQUA,0) QTCOMDIETQUA,
                NVL(QTCLIDIETQUA,0) QTCLIDIETQUA,
                NVL(QTFUNDIETQUA,0) QTFUNDIETQUA,
                NVL(QTDOADIETQUA,0) QTDOADIETQUA,
                NVL(QTCOMDIETQUI,0) QTCOMDIETQUI,
                NVL(QTCLIDIETQUI,0) QTCLIDIETQUI,
                NVL(QTFUNDIETQUI,0) QTFUNDIETQUI,
                NVL(QTDOADIETQUI,0) QTDOADIETQUI,
                NVL(QTCOMDIETSEX,0) QTCOMDIETSEX,
                NVL(QTCLIDIETSEX,0) QTCLIDIETSEX,
                NVL(QTFUNDIETSEX,0) QTFUNDIETSEX,
                NVL(QTDOADIETSEX,0) QTDOADIETSEX,
                NVL(QTCOMDIETSAB,0) QTCOMDIETSAB,
                NVL(QTCLIDIETSAB,0) QTCLIDIETSAB,
                NVL(QTFUNDIETSAB,0) QTFUNDIETSAB,
                NVL(QTDOADIETSAB,0) QTDOADIETSAB,
                NVL(QTCOMDIETDOM,0) QTCOMDIETDOM,
                NVL(QTCLIDIETDOM,0) QTCLIDIETDOM,
                NVL(QTFUNDIETDOM,0) QTFUNDIETDOM,
                NVL(QTDOADIETDOM,0) QTDOADIETDOM,
                NVL(QTCOMDIETFER,0) QTCOMDIETFER,
                NVL(QTCLIDIETFER,0) QTCLIDIETFER,
                NVL(QTFUNDIETFER,0) QTFUNDIETFER,
                NVL(QTDOADIETFER,0) QTDOADIETFER
             FROM DIETSERV
             WHERE
                CDFILIAL = P_CDFILIAL AND
                CDSERVFILI = P_CDSERVFILI AND
                (INSTR(P_LDIETA, CDDIETPAD)<>0))
            LOOP
               SELECT COUNT(*)
               INTO V_CONTCARD
               FROM CARDADIA
               WHERE
                  CDFILIAL   = P_CDFILIAL AND
                  CDSERVFILI = P_CDSERVFILI AND
                  TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO AND
                  CDDIETPAD  = C_DIETSERV.CDDIETPAD;

               IF (V_CONTFERIFILI = 0) OR (V_IDCARDIAUTIL = 'N') THEN
                  IF (V_CONTFERICARD = 0) THEN
                     IF (V_DIASEMANA = '1') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETDOM;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETDOM;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETDOM;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETDOM;
                     END IF;
                     IF (V_DIASEMANA = '2') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETSEG;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETSEG;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETSEG;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETSEG;
                     END IF;
                     IF (V_DIASEMANA = '3') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETTER;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETTER;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETTER;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETTER;
                     END IF;
                     IF (V_DIASEMANA = '4') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETQUA;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETQUA;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETQUA;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETQUA;
                     END IF;
                     IF (V_DIASEMANA = '5') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETQUI;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETQUI;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETQUI;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETQUI;
                     END IF;
                     IF (V_DIASEMANA = '6') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETSEX;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETSEX;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETSEX;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETSEX;
                     END IF;
                     IF (V_DIASEMANA = '7') THEN
                        V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETSAB;
                        V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETSAB;
                        V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETSAB;
                        V_QTDOAESTICAR := C_DIETSERV.QTDOADIETSAB;
                     END IF;
                     IF (V_QTCOMESTICAR <> 0) THEN
                        IF (V_CONTCARD = 0) THEN
                           IF (P_IDINSERIR = 'S') THEN

                              V_NRCARDAPIO := NULL;
                              NOVO_CODIGO('CARDAPIO'||P_CDFILIAL, V_NRCARDAPIO, 1);
                              V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                              INSERT INTO CARDADIA
                              (CDFILIAL,
                               NRCARDAPIO,
                               CDSERVFILI,
                               CDDIETPAD,
                               CDOPERADOR,
                               DTCARDAPIO,
                               QTCOMESTICAR,
                               QTCLIESTICAR,
                               QTFUNESTICAR,
                               QTDOAESTICAR,
                               QTESTITURNO1,
                               QTESTITURNO2,
                               QTESTITURNO3,
                               QTESTITURNO4,
                               DTULTATUACAR,
                               DTMESCARD)
                              VALUES
                              (P_CDFILIAL,
                               V_NRCARDAPIO,
                               P_CDSERVFILI,
                               C_DIETSERV.CDDIETPAD,
                               P_CDOPERADOR,
                               C_DATA.DTCARDAPIO,
                               V_QTCOMESTICAR,
                               V_QTCLIESTICAR,
                               V_QTFUNESTICAR,
                               V_QTDOAESTICAR,
                               V_QTTURNO1,
                               V_QTTURNO2,
                               V_QTTURNO3,
                               V_QTTURNO4,
                               SYSDATE,
                               C_DATA.DTMES);
                           END IF;
                        ELSE
                           UPDATE CARDADIA
                           SET CDOPERADOR   = P_CDOPERADOR,
                               DTULTATUACAR = SYSDATE,
                               QTCOMESTICAR = V_QTCOMESTICAR,
                               QTCLIESTICAR = V_QTCLIESTICAR,
                               QTFUNESTICAR = V_QTFUNESTICAR,
                               QTDOAESTICAR = V_QTDOAESTICAR,
                               QTESTITURNO1 = V_QTTURNO1,
                               QTESTITURNO2 = V_QTTURNO2,
                               QTESTITURNO3 = V_QTTURNO3,
                               QTESTITURNO4 = V_QTTURNO4
                           WHERE
                               CDFILIAL = P_CDFILIAL AND
                               CDSERVFILI = P_CDSERVFILI AND
                               TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO AND
                               CDDIETPAD = C_DIETSERV.CDDIETPAD;
                        END IF;
                     END IF;
                  ELSE
                     V_QTCOMESTICAR := C_DIETSERV.QTCOMDIETFER;
                     V_QTCLIESTICAR := C_DIETSERV.QTCLIDIETFER;
                     V_QTFUNESTICAR := C_DIETSERV.QTFUNDIETFER;
                     V_QTDOAESTICAR := C_DIETSERV.QTDOADIETFER;
                     IF (V_QTCOMESTICAR <> 0) THEN
                        IF (V_CONTCARD = 0) THEN
                           IF (P_IDINSERIR = 'S') THEN
                              V_NRCARDAPIO := NULL;
                              NOVO_CODIGO('CARDAPIO'||P_CDFILIAL, V_NRCARDAPIO, 1);
                              V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                              INSERT INTO CARDADIA
                              (CDFILIAL,
                               NRCARDAPIO,
                               CDSERVFILI,
                               CDDIETPAD,
                               CDOPERADOR,
                               DTCARDAPIO,
                               QTCOMESTICAR,
                               QTCLIESTICAR,
                               QTFUNESTICAR,
                               QTDOAESTICAR,
                               QTESTITURNO1,
                               QTESTITURNO2,
                               QTESTITURNO3,
                               QTESTITURNO4,
                               DTULTATUACAR,
                               DTMESCARD)
                              VALUES
                              (P_CDFILIAL,
                               V_NRCARDAPIO,
                               P_CDSERVFILI,
                               C_DIETSERV.CDDIETPAD,
                               P_CDOPERADOR,
                               C_DATA.DTCARDAPIO,
                               V_QTCOMESTICAR,
                               V_QTCLIESTICAR,
                               V_QTFUNESTICAR,
                               V_QTDOAESTICAR,
                               V_QTTURNO1,
                               V_QTTURNO2,
                               V_QTTURNO3,
                               V_QTTURNO4,
                               SYSDATE,
                               C_DATA.DTMES);
                           END IF;
                        ELSE
                           UPDATE CARDADIA
                           SET CDOPERADOR   = P_CDOPERADOR,
                               DTULTATUACAR = SYSDATE,
                               QTCOMESTICAR = V_QTCOMESTICAR,
                               QTCLIESTICAR = V_QTCLIESTICAR,
                               QTFUNESTICAR = V_QTFUNESTICAR,
                               QTDOAESTICAR = V_QTDOAESTICAR,
                               QTESTITURNO1 = V_QTTURNO1,
                               QTESTITURNO2 = V_QTTURNO2,
                               QTESTITURNO3 = V_QTTURNO3,
                               QTESTITURNO4 = V_QTTURNO4
                           WHERE
                               CDFILIAL   = P_CDFILIAL   AND
                               CDSERVFILI = P_CDSERVFILI AND
                               TRUNC(DTCARDAPIO) = C_DATA.DTCARDAPIO AND
                               CDDIETPAD = C_DIETSERV.CDDIETPAD;
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   END LOOP;
   COMMIT;
END;
/