CREATE OR REPLACE PROCEDURE DELETA_OP(
    P_FILIAL       IN ORDPRODUCAO.CDFILIAL%TYPE,
    P_NRORDPROD    IN ORDPRODUCAO.NRORDPROD%TYPE,
    P_NRSUBORDPROD IN ORDPRODUCAO.NRSUBORDPROD%TYPE)

IS
    V_COUNT        NUMBER;
    V_AUTOFORN     VARCHAR2(10);
    V_CDFILIAL     VARCHAR2(04);
    V_NRLANCTONF   VARCHAR2(10);
    V_IDSISTEMA    VARCHAR2(03);
BEGIN
   DELETE FROM SUBPRETAPAOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM ETAFABRICOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   FOR C_RETIRADA IN (SELECT CDFILIAL, NRRETEST
                        FROM RETIRADAESTO
                       WHERE CDFILIAL = P_FILIAL
                         AND NRORDPROD = P_NRORDPROD
                         AND NRSUBORDPROD = P_NRSUBORDPROD)
   LOOP
      DELETE FROM ITEMRETIEST
      WHERE CDFILIAL = C_RETIRADA.CDFILIAL
        AND NRRETEST = C_RETIRADA.NRRETEST;

      DELETE FROM LOTITRETIEST
      WHERE CDFILIAL = C_RETIRADA.CDFILIAL
        AND NRRETEST = C_RETIRADA.NRRETEST;

   END LOOP;

   DELETE FROM RETIRADAESTO
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM PERDAOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM CONETAPAOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   FOR C_AUDITORIA IN (SELECT CDFILIAL, NRAUDITORIA
                         FROM AUDITORIA
                        WHERE CDFILIAL = P_FILIAL
                          AND NRORDPROD = P_NRORDPROD
                          AND NRSUBORDPROD = P_NRSUBORDPROD)
   LOOP
      DELETE FROM INSPECAUDIT
      WHERE CDFILIAL = C_AUDITORIA.CDFILIAL
        AND NRAUDITORIA = C_AUDITORIA.NRAUDITORIA;

      DELETE FROM ITEMINSAUDIT
      WHERE CDFILIAL = C_AUDITORIA.CDFILIAL
        AND NRAUDITORIA = C_AUDITORIA.NRAUDITORIA;

      DELETE FROM FORMULAUDITANA
      WHERE CDFILIAL = C_AUDITORIA.CDFILIAL
        AND NRAUDITORIA = C_AUDITORIA.NRAUDITORIA;

      DELETE FROM RECURAUDITANA
      WHERE CDFILIAL = C_AUDITORIA.CDFILIAL
        AND NRAUDITORIA = C_AUDITORIA.NRAUDITORIA;

      DELETE FROM RECURETAPAOP
      WHERE CDFILIAL = C_AUDITORIA.CDFILIAL
        AND NRAUDITORIA = C_AUDITORIA.NRAUDITORIA;
   END LOOP;


   DELETE FROM AUDITORIA
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM TAREFACHECKOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM APRCUSTOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM PRODACABADO
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM QUESTTAROP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM OBSPADOP
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;

   DELETE FROM ORDPRODUCAO
   WHERE CDFILIAL = P_FILIAL
     AND NRORDPROD = P_NRORDPROD
     AND NRSUBORDPROD = P_NRSUBORDPROD;


END;
/