CREATE OR REPLACE PROCEDURE ARQUIVO_PIS_COFINS(
   P_CDLIVROMAG   IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_LIVROMAG     IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_VERSAO       IN VARCHAR2,
   P_INDESCRITU   IN NOTAFISCAL.IDSTATUS%TYPE,
   P_IDSITUESP    IN NOTAFISCAL.IDSTATUS%TYPE,
   P_NUMRECANT    IN VARCHAR2,
   P_ATIVIDADE    IN NOTAFISCAL.IDSTATUS%TYPE,
   P_CDINCTRIB    IN NOTAFISCAL.IDSTATUS%TYPE,
   P_INDAPROCRED  IN NOTAFISCAL.IDSTATUS%TYPE,
   P_CDTPCON      IN NOTAFISCAL.IDSTATUS%TYPE,
   P_CRTESCRITUR  IN NOTAFISCAL.IDSTATUS%TYPE,
   P_TPESCRITU    IN NOTAFISCAL.IDSTATUS%TYPE,
   P_TPNATJURPJ   IN NOTAFISCAL.CDMODDOCFISC%TYPE,
   P_GERABCLP     IN NOTAFISCAL.IDSTATUS%TYPE,
   P_NATUJURID    IN VARCHAR2,
   P_IDFORAPER    IN NOTAFISCAL.IDSTATUS%TYPE,
   P_IDCONSPRODEXT IN NOTAFISCAL.IDSTATUS%TYPE DEFAULT 'N',
   P_NRSEQPROJETO IN NUMBER DEFAULT NULL)
AS
   V_ICMS         VARCHAR2(2);
   V_IPI          VARCHAR2(2);
   V_ISS_E        VARCHAR2(2);
   V_ISS_S        VARCHAR2(2);
   V_ISS_RET_E    VARCHAR2(2);
   V_ISS_RET_S    VARCHAR2(2);
   V_PIS          VARCHAR2(2);
   V_PIS_RET_E    VARCHAR2(2);
   V_PIS_RET_S    VARCHAR2(2);
   V_COFINS       VARCHAR2(2);
   V_COFINS_RET_E VARCHAR2(2);
   V_COFINS_RET_S VARCHAR2(2);
   V_ICMS_ST      VARCHAR2(2);
   V_CDPAIS       VARCHAR2(4);
   V_TOTREG       NUMBER;
BEGIN
   V_ICMS         := '00';
   V_IPI          := '10';
   V_ISS_E        := '30';
   V_ISS_S        := '31';
   V_ISS_RET_E    := '35';
   V_ISS_RET_S    := '36';
   V_PIS          := '50';
   V_PIS_RET_E    := '55';
   V_PIS_RET_S    := '56';
   V_COFINS       := '60';
   V_COFINS_RET_E := '65';
   V_COFINS_RET_S := '66';
   V_ICMS_ST      := '80';
   BEGIN
      SELECT CDPAIS
        INTO V_CDPAIS
        FROM PAIS
       WHERE UPPER(NMPAIS) = 'BRASIL';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDPAIS := '0055';
   END;
   V_TOTREG := SPED_PIS_COFINS_BLOCO0(P_CDEMPRESA,P_NRSEQARQMAG,P_DTINICIAL,P_DTFINAL,P_VERSAO,P_TPESCRITU,
                                      P_IDSITUESP,P_NUMRECANT,P_TPNATJURPJ,P_ATIVIDADE,P_CDINCTRIB,NVL(P_INDAPROCRED,'1'),
                                      P_CDTPCON,NVL(P_CRTESCRITUR,'9'),P_CDLIVROMAG,NVL(P_GERABCLP,'0'),V_CDPAIS,
                                      V_ICMS,V_IPI,V_ISS_E,V_ISS_S,V_ISS_RET_E,V_ISS_RET_S,V_PIS,V_PIS_RET_E,V_PIS_RET_S,
                                      V_COFINS,V_COFINS_RET_E,V_COFINS_RET_S,V_ICMS_ST, P_NATUJURID, P_IDFORAPER, P_IDCONSPRODEXT, P_NRSEQPROJETO);

   DELETE FROM TMP_REGDADOSSPED_PIS_AUX;
   INSERT INTO TMP_REGDADOSSPED_PIS_AUX (SELECT * FROM  TMP_REGDADOSSPED_PIS);
   DELETE FROM TMP_REGDADOSSPED_PIS;
   COMMIT;

   DELETE FROM TMP_REGBLOCOM_PIS_AUX;
   INSERT INTO TMP_REGBLOCOM_PIS_AUX (SELECT * FROM TMP_REGBLOCOM_PIS);
   DELETE FROM TMP_REGBLOCOM_PIS;
   COMMIT;
END;
/

