SET TERM ^ ;

CREATE OR ALTER trigger ttbpdv_cfe_item_b04i for tbpdv_cfe_item
active before insert or update position 1
AS
BEGIN
    /* AJUSTAR VALORES NULOS (IMPORTACAO DE DOCS) */
    IF(COALESCE(NEW.FAMILIA_ID, 0) = 0) THEN
    BEGIN
        NEW.FAMILIA_ID = COALESCE((SELECT T.FAMILIA_ALTERNATIVA FROM TBPRODUTO T WHERE T.CODIGO = NEW.PRODUTO_ID), 0);
    END

    IF(NEW.VALOR_SUFRAMA IS NULL) THEN
    BEGIN
        NEW.VALOR_SUFRAMA = 0;
    END

    /*ATUALIZA OS CAMPOS DO TIPO FISCAL DO CUPOM*/
    IF(NEW.ID > 0)THEN
    BEGIN
        
      IF(INSERTING AND NEW.OFFLINE_ID = 0 AND NEW.BRINDE = 0) THEN
      BEGIN

        IF(NEW.TRIBUTACAO_ICMS = 0 OR NEW.TRIBUTACAO_ICMS = 10 OR NEW.TRIBUTACAO_ICMS = 101 OR NEW.TRIBUTACAO_ICMS = 201 OR NEW.TRIBUTACAO_ICMS = 20 OR NEW.TRIBUTACAO_ICMS = 70) THEN
        BEGIN
            /* IF(COALESCE(NEW.BASECALCULO_ICMS, 0) <= 0) THEN
            BEGIN
                EXCEPTION "..."'BASE DE CALCULO DO ICMS NO ITEM '||NEW.SEQUENCIA||' NAO DEFINIDA PARA O CST '||COALESCE(NEW.TRIBUTACAO_ICMS, '')||'. ENTRE EM CONTATO COM O SUPORTE TECNICO.';
            END*/
        END

      END

      IF(INSERTING OR (UPDATING AND (NEW.ALTERACAO_TRIBUTACAO = 1 OR (OLD.CFOP_CODIGO IS DISTINCT FROM NEW.CFOP_CODIGO)))) THEN
      BEGIN

          NEW.FISCAL_CFOP_CODIGO             =  NEW.CFOP_CODIGO;
          NEW.FISCAL_TRIBUTACAO_ORIGEM       =  NEW.TRIBUTACAO_ORIGEM;
          NEW.FISCAL_TRIBUTACAO_ICMS         =  NEW.TRIBUTACAO_ICMS;
          NEW.FISCAL_TRIBUTACAO_PIS          =  NEW.TRIBUTACAO_PIS;
          NEW.FISCAL_TRIBUTACAO_COFINS       =  NEW.TRIBUTACAO_COFINS;
    
          NEW.FISCAL_PERC_BASECALCULO_ICMS   =  NEW.PERCENTUAL_BASECALCULO_ICMS;
          NEW.FISCAL_BASECALCULO_ICMS        =  NEW.BASECALCULO_ICMS;
          NEW.FISCAL_ALIQUOTA_ICMS           =  NEW.ALIQUOTA_ICMS;
          NEW.FISCAL_VALOR_ICMS              =  NEW.VALOR_ICMS;
    
          NEW.FISCAL_BASECALCULO_PIS         =  NEW.BASECALCULO_PIS;
          NEW.FISCAL_ALIQUOTA_PIS            =  NEW.ALIQUOTA_PIS;
          NEW.FISCAL_VALOR_PIS               =  NEW.VALOR_PIS;
    
          NEW.FISCAL_BASECALCULO_COFINS      =  NEW.BASECALCULO_COFINS;
          NEW.FISCAL_ALIQUOTA_COFINS         =  NEW.ALIQUOTA_COFINS;
          NEW.FISCAL_VALOR_COFINS            =  NEW.VALOR_COFINS;
    
          NEW.FISCAL_OUTRAS_ICMS             = COALESCE(NEW.VALOR_CONTABIL, 0) - COALESCE(NEW.FISCAL_BASECALCULO_ICMS, 0);
          NEW.FISCAL_OUTRAS_IPI              = COALESCE(NEW.VALOR_CONTABIL, 0) - COALESCE(NEW.FISCAL_BASECALCULO_IPI, 0) - COALESCE(NEW.VALOR_IPI, 0);
    
          IF(NEW.FISCAL_OUTRAS_ICMS < 0) THEN
          BEGIN
            NEW.FISCAL_OUTRAS_ICMS = 0;
          END
    
          IF(NEW.FISCAL_OUTRAS_IPI < 0) THEN
          BEGIN
            NEW.FISCAL_OUTRAS_IPI = 0;
          END
        END
    END

END
^

SET TERM ; ^


SET TERM ^ ;

CREATE OR ALTER trigger ttbpdv_cfe_a01d for tbpdv_cfe
active after delete position 1
AS
BEGIN
    UPDATE TBPDV_CFE T SET T.NF_ACOMPANHAMENTO_ID = 0 WHERE T.NF_ACOMPANHAMENTO_ID = OLD.ID;
    DELETE FROM TBPDV_CFE_ITEM T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBPDV_CFE_ITEM_TRATAMENTO T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBPDV_CFE_ITEM_TROCA T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBPDV_CFE_ITEM_SALDO T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBPDV_CFE_FORMA_PAGAMENTO T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBCHEQUE T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBNFS_PARCELAMENTO T WHERE T.PDV_CFE_ID = OLD.ID;
    DELETE FROM TBNFS_REFERENCIADA T WHERE T.PDV_CFE_ID = OLD.ID;
END
^

SET TERM ; ^

