CREATE OR REPLACE PROCEDURE ARQITENS_SERVEMIT_436(
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_NRINSJURFILI IN FILIAL.NRINSJURFILI%TYPE,
   P_CDIMPOSTO    IN IMPOSTONF.CDIMPOSTO%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE)
AS
   V_PROD            VARCHAR(50);
   V_SEQ_PROD        NUMBER;
   V_SEQUNCIAL       NUMBER;
BEGIN

   V_SEQ_PROD := 0;

   SELECT NVL(MAX(CDCONTREGI),0)
     INTO V_SEQ_PROD
     FROM TMP_REGDADOS_IN86
    WHERE CDREGISTRO = '495';

   FOR C_TMP IN (SELECT CDREGISTRO, CDCHAVEREG, CDCAMPO1
                   FROM TMP_REGDADOS_IN86
                  WHERE CDREGISTRO = '435' ) LOOP

       FOR C_ARQITENS_SERVEMIT IN (SELECT C_TMP.CDCAMPO1                                                  || /*INICIO REG.433*/
                                          SUBSTR(LPAD(IT.NRSEQITPROD, 4, 0), 2, 3)                        ||
                                          RPAD(NVL(PR.CDPRODUTO, ' '), 20, ' ')                           ||
                                          RPAD(' ', 45, ' ')                                              ||
                                          LPAD(NVL(SUBSTR(IT.VRTOTAITEM * 100, 1, 17), 0), 17, 0)         ||
                                          LPAD(NVL(SUBSTR(IT.VRDESCITEM * 100, 1, 17), 0), 17, 0)         ||
                                          LPAD(NVL(SUBSTR(II.VRALIQITEM * 100, 1, 5), 0), 5, 0)           ||
                                          LPAD(NVL(SUBSTR(SUM(II.VRBASECALCIT) * 100, 1, 17), 0), 17, 0)  ||
                                          LPAD(NVL(SUBSTR(SUM(II.VRIMPOSTOIT) * 100, 1, 17), 0), 17, 0)    AS ITEMSERVICO,
                                          PR.CDPRODUTO
                                     FROM ITEMPRODUTO IT, PRODUTO PR, ITEMPROIMPOS II, IMPOSTO IP
                                    WHERE IT.CDFILIAL     = SUBSTR(C_TMP.CDCHAVEREG,1,4)
                                      AND IT.NRLANCTONF   = SUBSTR(C_TMP.CDCHAVEREG,5,10)
                                      AND IT.CDPRODUTO = PR.CDPRODUTO
                                      AND II.CDFILIAL = IT.CDFILIAL
                                      AND II.NRLANCTONF = IT.NRLANCTONF
                                      AND II.NRSEQITPROD = IT.NRSEQITPROD
                                      AND II.CDIMPOSTO = IP.CDIMPOSTO
                                      AND IP.CDIMPOSTONFE IN ('30','31','32') /*31-ISS_PREST, 32-ISS_RET_PREST*/
                                      AND II.NRITPROIMPOS = '1'
                                    GROUP BY IT.NRSEQITPROD, PR.CDPRODUTO, IT.VRTOTAITEM, IT.VRDESCITEM, II.VRALIQITEM
                                    ORDER BY IT.NRSEQITPROD) LOOP

          V_SEQUNCIAL := NVL(V_SEQUNCIAL,0) +1;
          INSERT INTO ARQMAGNETICO
            (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
          VALUES
            ('05'||LPAD(RTRIM(V_SEQUNCIAL), 8, '0'), C_ARQITENS_SERVEMIT.ITEMSERVICO,  P_NRSEQARQMAG);

          SELECT MAX(CDCHAVEREG)
            INTO V_PROD
            FROM TMP_REGDADOS_IN86
           WHERE CDREGISTRO = '495'
             AND CDCHAVEREG = C_ARQITENS_SERVEMIT.CDPRODUTO;
          IF (V_PROD IS NULL) AND (C_ARQITENS_SERVEMIT.CDPRODUTO IS NOT NULL) THEN
             V_SEQ_PROD := V_SEQ_PROD + 1;
             INSERT INTO TMP_REGDADOS_IN86
               (CDREGISTRO, CDCONTREGI, CDCHAVEREG)
             VALUES
               ('495', V_SEQ_PROD, C_ARQITENS_SERVEMIT.CDPRODUTO);
          END IF;
      END LOOP;
   END LOOP;

   COMMIT;
END;
/