Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR ALTER PROCEDURE PROC_VENDA_REGISTRAITEM (
- IN_NUMEROVENDA BIGINT NOT NULL,
- IN_I02CPROD VARCHAR(60) NOT NULL,
- IN_I03CEAN VARCHAR(14) NOT NULL,
- IN_I04XPROD VARCHAR(120) NOT NULL,
- IN_I05NCM VARCHAR(8) NOT NULL,
- IN_I09UCOM VARCHAR(10)NOT NULL,
- IN_I10QCOM NUMERIC(15,4)NOT NULL,
- IN_I10AVUNCOM NUMERIC(15,2)NOT NULL,
- IN_VLCUSTOPROD NUMERIC(15,2)NOT NULL,
- IN_I12CEANTRIB VARCHAR(14) NOT NULL,
- IN_I13UTRIB VARCHAR(6) NOT NULL,
- IN_I14QTRIB NUMERIC(15,4) NOT NULL,
- IN_I14AVUNTRIB NUMERIC(15,2) NOT NULL,
- IN_CSTCSOSN TYPE OF COLUMN PROD_ICMS.CSTCSOSN,
- IN_I15VFRETE TYPE OF COLUMN PROD.I15VFRETE,
- IN_I16VSEG TYPE OF COLUMN PROD.I16VSEG,
- IN_I17VDESC TYPE OF COLUMN PROD.I17VDESC,
- IN_I17AVOUTRO TYPE OF COLUMN PROD.I17AVOUTRO,
- IN_PICMS TYPE OF COLUMN PROD_ICMS.PICMS,
- IN_ICMSORIG TYPE OF COLUMN PROD_ICMS.ICMSORIG,
- IN_PIS_CST TYPE OF COLUMN PROD_ICMS.PIS_CST,
- IN_PIS_ALIQ TYPE OF COLUMN PROD_ICMS.PIS_ALIQ,
- IN_COFINS_CST TYPE OF COLUMN PROD_ICMS.COFINS_CST,
- IN_COFINS_ALIQ TYPE OF COLUMN PROD_ICMS.COFINS_ALIQ,
- IN_CFOP TYPE OF COLUMN PROD_ICMS.CFOP,
- IN_IBPT_ALIQMUN TYPE OF COLUMN PROD_ICMS.IBPT_ALIQMUN,
- IN_IBPT_ALIQEST TYPE OF COLUMN PROD_ICMS.IBPT_ALIQEST,
- IN_IBPT_ALIQFED TYPE OF COLUMN PROD_ICMS.IBPT_ALIQFED,
- IN_IBPT_ALIQIMP TYPE OF COLUMN PROD_ICMS.IBPT_ALIQIMP,
- IN_VICMS TYPE OF COLUMN PROD_ICMS.VICMS,
- IN_VPIS TYPE OF COLUMN PROD_ICMS.VPIS,
- IN_VCOFINS TYPE OF COLUMN PROD_ICMS.VCOFINS,
- IN_N17BPFCP TYPE OF COLUMN PROD_ICMS.N17BPFCP,
- IN_N17CVFCP TYPE OF COLUMN PROD_ICMS.N17CVFCP,
- IN_N17AVBCFCP TYPE OF COLUMN PROD_ICMS.N17AVBCFCP,
- IN_N18MODBCST TYPE OF COLUMN PROD_ICMS.N18MODBCST,
- IN_N19PMVAST TYPE OF COLUMN PROD_ICMS.N19PMVAST,
- IN_N20PREDBCST TYPE OF COLUMN PROD_ICMS.N20PREDBCST,
- IN_N21VBCST TYPE OF COLUMN PROD_ICMS.N21VBCST,
- IN_N22PICMSST TYPE OF COLUMN PROD_ICMS.N22PICMSST,
- IN_N23AVBCFCPST TYPE OF COLUMN PROD_ICMS.N23AVBCFCPST,
- IN_N23BPFCPST TYPE OF COLUMN PROD_ICMS.N23BPFCPST,
- IN_N23DVFCPST TYPE OF COLUMN PROD_ICMS.N23DVFCPST,
- IN_N28AVICMSDESON TYPE OF COLUMN PROD_ICMS.N28AVICMSDESON,
- IN_N28MOTDESICMS TYPE OF COLUMN PROD_ICMS.N28MOTDESICMS,
- IN_N26VBCSTRET TYPE OF COLUMN PROD_ICMS.N26VBCSTRET,
- IN_N26APST TYPE OF COLUMN PROD_ICMS.N26APST,
- IN_N26BVICMSSUBSTITUTO TYPE OF COLUMN PROD_ICMS.N26BVICMSSUBSTITUTO,
- IN_N27VICMSSTRET TYPE OF COLUMN PROD_ICMS.N27VICMSSTRET,
- IN_N14PREDBC TYPE OF COLUMN PROD_ICMS.N14PREDBC,
- IN_N27AVBCFCPSTRET TYPE OF COLUMN PROD_ICMS.N27AVBCFCPSTRET,
- IN_N27BPFCPSTRET TYPE OF COLUMN PROD_ICMS.N27BPFCPSTRET,
- IN_N27DVFCPSTRET TYPE OF COLUMN PROD_ICMS.N27DVFCPSTRET,
- IN_N34PREDBCEFET TYPE OF COLUMN PROD_ICMS.N34PREDBCEFET,
- IN_N35VBCEFET TYPE OF COLUMN PROD_ICMS.N35VBCEFET,
- IN_N36PICMSEFET TYPE OF COLUMN PROD_ICMS.N36PICMSEFET,
- IN_N37VICMSEFET TYPE OF COLUMN PROD_ICMS.N37VICMSEFET,
- IN_N15VBC TYPE OF COLUMN PROD_ICMS.N15VBC,
- IN_VITEM12741 TYPE OF COLUMN PROD_ICMS.VITEM12741,
- IN_N13MODBC TYPE OF COLUMN PROD_ICMS.N13MODBC,
- IN_I17BINDTOT TYPE OF COLUMN PROD.I17BINDTOT,
- IN_I05ANVE TYPE OF COLUMN PROD.I05ANVE,
- IN_I06EXTIPI TYPE OF COLUMN PROD.I06EXTIPI,
- IN_I07IPPT TYPE OF COLUMN PROD.I07IPPT
- )
- RETURNS (
- ID BIGINT,
- FKEMPRESA BIGINT,
- B001DATETIME TIMESTAMP,
- B002NATOP VARCHAR(200),
- B003NROPDV SMALLINT,
- B004OPERADOR VARCHAR(60),
- B005SIT INTEGER,
- B006IDMOVCAIXA INTEGER,
- B007IDSESSAO INTEGER,
- B008VOLUME SMALLINT,
- TIPO SMALLINT,
- TIPOFISCAL SMALLINT,
- TIPODOCFISCAL SMALLINT,
- CONSUMDOC VARCHAR(17),
- I16PROFITABILITRATE NUMERIC(15,4),
- I17PROFITABILITVAL NUMERIC(15,4),
- CNPJ VARCHAR(17),
- XNOME VARCHAR(60),
- XFANT VARCHAR(60),
- XLGR VARCHAR(60),
- NRO VARCHAR(60),
- XCPL VARCHAR(60),
- XBAIRRO VARCHAR(60),
- CMUN INTEGER,
- XMUN VARCHAR(60),
- UF VARCHAR(2),
- CEP INTEGER,
- CPAIS SMALLINT,
- XPAIS VARCHAR(60),
- FONE VARCHAR(35),
- XMAIL VARCHAR(200),
- C18IEST VARCHAR(14),
- C17IE VARCHAR(14),
- C19IM VARCHAR(15),
- C20CNAE VARCHAR(7),
- C21CRT INTEGER,
- LMESSAGE TYPE OF COLUMN WARNINGS.MENSAGEM,
- H02NITEM TYPE OF COLUMN PROD.H02NITEM,
- I02CPROD TYPE OF COLUMN PROD.I02CPROD,
- I03CEAN TYPE OF COLUMN PROD.I03CEAN,
- I04XPROD TYPE OF COLUMN PROD.I04XPROD,
- I05NCM TYPE OF COLUMN PROD.I05NCM,
- I08CFOP TYPE OF COLUMN PROD.I08CFOP,
- I09UCOM TYPE OF COLUMN PROD.I09UCOM,
- I10QCOM TYPE OF COLUMN PROD.I10QCOM,
- I10AVUNCOM TYPE OF COLUMN PROD.I10AVUNCOM,
- I11VPROD TYPE OF COLUMN PROD.I11VPROD,
- I12CEANTRIB TYPE OF COLUMN PROD.I12CEANTRIB,
- I13UTRIB TYPE OF COLUMN PROD.I13UTRIB,
- I14QTRIB TYPE OF COLUMN PROD.I14QTRIB,
- I14AVUNTRIB TYPE OF COLUMN PROD.I14AVUNTRIB,
- I15VFRETE TYPE OF COLUMN PROD.I15VFRETE,
- I16VSEG TYPE OF COLUMN PROD.I16VSEG,
- I17VDESC TYPE OF COLUMN PROD.I17VDESC,
- I17AVOUTRO TYPE OF COLUMN PROD.I17AVOUTRO,
- I17BINDTOT TYPE OF COLUMN PROD.I17BINDTOT,
- I05CCEST TYPE OF COLUMN PROD.I05CCEST,
- IVLCUSTOPROD TYPE OF COLUMN PROD.IVLCUSTOPROD,
- I05ANVE TYPE OF COLUMN PROD.I05ANVE,
- I06EXTIPI TYPE OF COLUMN PROD.I06EXTIPI,
- I07IPPT TYPE OF COLUMN PROD.I07IPPT,
- I08VTOTPROD TYPE OF COLUMN PROD.I08VTOTPROD,
- ICMSORIG TYPE OF COLUMN PROD_ICMS.ICMSORIG,
- CSTCSOSN TYPE OF COLUMN PROD_ICMS.CSTCSOSN,
- PICMS TYPE OF COLUMN PROD_ICMS.PICMS,
- PIS_CST TYPE OF COLUMN PROD_ICMS.PIS_CST,
- PIS_ALIQ TYPE OF COLUMN PROD_ICMS.PIS_ALIQ,
- COFINS_CST TYPE OF COLUMN PROD_ICMS.COFINS_CST,
- COFINS_ALIQ TYPE OF COLUMN PROD_ICMS.COFINS_ALIQ,
- CFOP TYPE OF COLUMN PROD_ICMS.CFOP,
- IBPT_ALIQMUN TYPE OF COLUMN PROD_ICMS.IBPT_ALIQMUN,
- IBPT_ALIQEST TYPE OF COLUMN PROD_ICMS.IBPT_ALIQEST,
- IBPT_ALIQFED TYPE OF COLUMN PROD_ICMS.IBPT_ALIQFED,
- IBPT_ALIQIMP TYPE OF COLUMN PROD_ICMS.IBPT_ALIQIMP,
- VICMS TYPE OF COLUMN PROD_ICMS.VICMS,
- VPIS TYPE OF COLUMN PROD_ICMS.VPIS,
- VCOFINS TYPE OF COLUMN PROD_ICMS.VCOFINS,
- N17BPFCP TYPE OF COLUMN PROD_ICMS.N17BPFCP,
- N17CVFCP TYPE OF COLUMN PROD_ICMS.N17CVFCP,
- N17AVBCFCP TYPE OF COLUMN PROD_ICMS.N17AVBCFCP,
- N18MODBCST TYPE OF COLUMN PROD_ICMS.N18MODBCST,
- N19PMVAST TYPE OF COLUMN PROD_ICMS.N19PMVAST,
- N20PREDBCST TYPE OF COLUMN PROD_ICMS.N20PREDBCST,
- N21VBCST TYPE OF COLUMN PROD_ICMS.N21VBCST,
- N22PICMSST TYPE OF COLUMN PROD_ICMS.N22PICMSST,
- N23AVBCFCPST TYPE OF COLUMN PROD_ICMS.N23AVBCFCPST,
- N23BPFCPST TYPE OF COLUMN PROD_ICMS.N23BPFCPST,
- N23DVFCPST TYPE OF COLUMN PROD_ICMS.N23DVFCPST,
- N28AVICMSDESON TYPE OF COLUMN PROD_ICMS.N28AVICMSDESON,
- N28MOTDESICMS TYPE OF COLUMN PROD_ICMS.N28MOTDESICMS,
- N26VBCSTRET TYPE OF COLUMN PROD_ICMS.N26VBCSTRET,
- N26APST TYPE OF COLUMN PROD_ICMS.N26APST,
- N26BVICMSSUBSTITUTO TYPE OF COLUMN PROD_ICMS.N26BVICMSSUBSTITUTO,
- N27VICMSSTRET TYPE OF COLUMN PROD_ICMS.N27VICMSSTRET,
- N14PREDBC TYPE OF COLUMN PROD_ICMS.N14PREDBC,
- N27AVBCFCPSTRET TYPE OF COLUMN PROD_ICMS.N27AVBCFCPSTRET,
- N27BPFCPSTRET TYPE OF COLUMN PROD_ICMS.N27BPFCPSTRET,
- N27DVFCPSTRET TYPE OF COLUMN PROD_ICMS.N27DVFCPSTRET,
- N34PREDBCEFET TYPE OF COLUMN PROD_ICMS.N34PREDBCEFET,
- N35VBCEFET TYPE OF COLUMN PROD_ICMS.N35VBCEFET,
- N36PICMSEFET TYPE OF COLUMN PROD_ICMS.N36PICMSEFET,
- N37VICMSEFET TYPE OF COLUMN PROD_ICMS.N37VICMSEFET,
- N15VBC TYPE OF COLUMN PROD_ICMS.N15VBC,
- VITEM12741 TYPE OF COLUMN PROD_ICMS.vitem12741,
- N13MODBC TYPE OF COLUMN PROD_ICMS.N13MODBC
- )
- AS
- DECLARE VARIABLE LEHSN SMALLINT;
- DECLARE VARIABLE SLINEBREAK CHAR(23);
- DECLARE VARIABLE L_OLD_CSTCSOSN TYPE OF COLUMN PROD_ICMS.CSTCSOSN ;
- DECLARE VARIABLE L_VAR_VBRUTOPROD TYPE OF COLUMN PROD.I08VTOTPROD;
- DECLARE VARIABLE L_VAR_TIPOFISCALSTR VARCHAR(3);
- DECLARE VARIABLE L_VAR_VTOTALPRODSERV NUMERIC(15,2);
- DECLARE VARIABLE L_VAR_PERCLUCROPROD NUMERIC(15,4);
- BEGIN
- SLINEBREAK = ''||ASCII_CHAR(13) || ASCII_CHAR(10)||'';
- FOR
- SELECT
- T1.ID, T1.FKEMPRESA, T1.B001DATETIME, T1.B002NATOP,
- T1.B003NROPDV, T1.B004OPERADOR, T1.B005SIT, T1.B006IDMOVCAIXA,
- T1.B007IDSESSAO, T1.B008VOLUME, T1.TIPO, T1.TIPOFISCAL,
- T1.TIPODOCFISCAL, T1.CONSUMDOC, T2.CNPJ, T2.XNOME,
- T2.XFANT, T2.XLGR, T2.NRO, T2.XCPL,
- T2.XBAIRRO, T2.CMUN, T2.XMUN, T2.UF,
- T2.CEP, T2.CPAIS, T2.XPAIS, T2.FONE,
- T2.XMAIL, T2.C18IEST, T2.C17IE, T2.C19IM,
- T2.C20CNAE, T2.C21CRT
- FROM EMIT T2
- INNER JOIN IDE T1 ON (T2.IDPK = T1.ID)
- WHERE T1.ID = :IN_NUMEROVENDA
- INTO :ID,
- :FKEMPRESA, :B001DATETIME, :B002NATOP, :B003NROPDV, :B004OPERADOR,
- :B005SIT, :B006IDMOVCAIXA,:B007IDSESSAO,:B008VOLUME, :TIPO,
- :TIPOFISCAL, :TIPODOCFISCAL, :CONSUMDOC, :CNPJ, :XNOME,
- :XFANT, :XLGR, :NRO, :XCPL, :XBAIRRO,
- :CMUN, :XMUN, :UF, :CEP, :CPAIS,
- :XPAIS, :FONE, :XMAIL, :C18IEST, :C17IE,
- :C19IM, :C20CNAE, :C21CRT
- DO
- BEGIN
- IF (ID IS NULL OR ID = 0) THEN
- EXCEPTION eebadparam 'VENDA DE NUMERO '||IN_NUMEROVENDA||' NAO LOCALIZADA NO SISTEMA';
- SELECT TRIM(MENSAGEM) FROM WARNINGS WHERE WARNINGS.IDCAIXA = :B006IDMOVCAIXA
- AND WARNINGS.IDSESSAO = :B007IDSESSAO AND WARNINGS.IDVENDA = :IN_NUMEROVENDA
- INTO :LMESSAGE;
- IF (CHARACTER_LENGTH( TRIM(LMESSAGE) ) <= 0 OR ( LMESSAGE IS NULL ) ) THEN
- LMESSAGE = '';
- ELSE
- LMESSAGE = TRIM(:LMESSAGE)||SLINEBREAK||LPAD('',80,'*');
- /* 0 VENDA ABERTA - 1 VENDA FECHADA 2 VENDA CANCELADA 3 VENDA ESTORNADA */
- IF (:B005SIT = 1) THEN EXCEPTION EEBADPARAM 'VENDA DE NUMERO ' || :ID || ' FECHADA NO SISTEMA!';
- IF (:B005SIT = 2) THEN EXCEPTION EEBADPARAM 'VENDA DE NUMERO ' || :ID || ' CANCELADA NO SISTEMA!';
- IF (:B005SIT = 3) THEN EXCEPTION EEBADPARAM 'VENDA DE NUMERO ' || :ID || ' ESTORNADA NO SISTEMA!';
- IF ( :C21CRT IS NULL ) THEN
- EXCEPTION EECRITICALFAIL 'NAO EXISTE EMPRESA CARREGADA NO PLU EMPRESA';
- /* VERIFICA O CRT DA EMPRESA */
- IF ( NOT C21CRT IN (1,2,3) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK || 'VENDA NUMERO '||:IN_NUMEROVENDA||' - AJUSTE I03CEAN '
- ||:C21CRT|| ' INVALIDO PARA TIPO DE CLASSIFICACAO TRIBUTARIA AJUSTADO PARA 1 SIMPLES NACIONAL ';
- C21CRT = 1;
- END
- LEHSN = IIF( C21CRT = 1,1,0 );
- IF ( CHARACTER_LENGTH( TRIM( (SELECT * FROM ISONOASC( :IN_I04XPROD )))) <= 0 ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK ||'VENDA NUMERO '||:IN_NUMEROVENDA||' - AJUSTE I04XPROD '
- ||:IN_I04XPROD|| ' INVALIDO PRODUTO DE CÓDIGO DE BARRAS '
- ||:IN_I02CPROD||'TEM UM I04XPROD QUE NAO CORRESPONDE A UMA DESCRICAO'
- ||' DE PRODUTO VALIDA AJUSTADO PARA '||'PRODUTO DE CODIGO '||IN_I02CPROD;
- IN_I04XPROD = 'PRODUTO DE CODIGO '||IN_I02CPROD;
- END
- /*Preencher com o código GTIN-8, GTIN-12, GTIN-13 ou
- GTIN-14 (antigos códigos EAN, UPC e DUN-14) da unidade tributável do produto.
- O GTIN da unidade tributável deve corresponder àquele da menor unidade
- comercializável identificada por código GTIN.
- Para produtos que não possuem código de barras com GTIN, deve ser informado
- o literal "SEM GTIN”; (Atualizado NT 2017.001) */
- IF (CHARACTER_LENGTH( :IN_I03CEAN ) NOT IN ( 8,12,13,14 )) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK ||'VENDA NUMERO '||:IN_NUMEROVENDA||' - AJUSTE I03CEAN '
- ||:IN_I03CEAN|| ' INVALIDO PRODUTO DE CÓDIGO DE BARRAS '
- ||:IN_I02CPROD||'TEM UM I03CEAN QUE NAO CORRESPONDE A UM GTIN/CEAN VALIDO';
- IN_I03CEAN = 'SEM GTIN';
- END
- /* Obrigatória informação do NCM completo (8 dígitos).
- Nota: Em caso de item de serviço ou item que não tenham
- produto (ex. transferência de crédito, crédito do
- ativo imobilizado, etc.), informar o valor 00 (dois
- zeros). (NT 2014/004 ) */
- IF ( CHARACTER_LENGTH( :IN_I05NCM )< 8 ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK || 'VENDA NUMERO '||:IN_NUMEROVENDA||' - AJUSTE NCM '
- ||:IN_I05NCM|| ' INVALIDO PRODUTO DE CÓDIGO DE BARRAS '
- ||:IN_I02CPROD||'TEM UM NCM MENOR QUE 8 DIGITOS AJUSTADO PARA "39269090"';
- /*CORRIGE COM NCM 3926.90.90 - Plásticos e suas obras - Outras obras de
- plásticos e obras de outras matérias das posições
- 3901 a 3914 - Outras - Outras */
- IN_I05NCM = '39269090';
- END
- IF ( CHARACTER_LENGTH(:IN_I09UCOM) <= 0 OR ( :IN_I09UCOM IS NULL ) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK ||'VENDA NUERO '
- ||:IN_NUMEROVENDA||' - AJUSTE I09UCOM '
- ||:IN_I09UCOM|| ' INVALIDO, NAO CORRESPONDE A UMA UNIDADE COMERCIAL VALIDA '
- ||:IN_I02CPROD||' TEVE A UNIDADE COMERCIAL AJUSTADA PARA "UNID" ';
- IN_I09UCOM = 'UNID';
- END
- IF ( IN_I10QCOM <= 0.0000 ) THEN
- EXCEPTION eebadparam 'QUANTIDADE INVÁLIDA INFORMADA NA VENDA';
- IF ( IN_I10AVUNCOM <= 0.00 ) THEN
- EXCEPTION eebadparam 'VALOR UNITARIO DO PRODUTO INVÁLIDO INFORMADO NO'
- ||' CAMPO I10AVUNCOM NA VENDA';
- /* VERIFICA O CUSTO DO PRODUTO E AJUSTA */
- IF ( :IN_VLCUSTOPROD <= 0.00 ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK ||'VENDA NUMERO '
- ||:IN_NUMEROVENDA||' - AJUSTE VLCUSTOPROD '
- ||:IN_VLCUSTOPROD|| ' INVALIDO E AJUSTADO '
- ||'PARA 0.01 CENTAVO(S) ';
- IN_VLCUSTOPROD = 0.01;
- END
- IF (IN_I12CEANTRIB IS NULL OR CHAR_LENGTH(TRIM(IN_I12CEANTRIB)) <= 0) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK || 'VENDA NUMERO '
- ||:IN_NUMEROVENDA||' - AJUSTE I12CEANTRIB '
- ||:IN_I12CEANTRIB|| ' INVALIDO E AJUSTADO PARA '
- ||:IN_I03CEAN;
- IN_I12CEANTRIB = :IN_I03CEAN;
- END
- IF (IN_I13UTRIB IS NULL OR CHAR_LENGTH(TRIM(IN_I13UTRIB)) <= 0) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK ||'VENDA NUMERO '
- ||:IN_NUMEROVENDA||' - AJUSTE I13UTRIB '
- ||:IN_I13UTRIB|| ' INVALIDO E AJUSTADO PARA '
- ||:IN_I09UCOM;
- IN_I13UTRIB = :IN_I09UCOM;
- END
- IF (IN_I14AVUNTRIB IS NULL OR ( IN_I14AVUNTRIB <= 0.00 ) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE)|| SLINEBREAK || 'VENDA NUMERO '
- ||:IN_NUMEROVENDA||' - AJUSTE I14AVUNTRIB '
- ||:IN_I14AVUNTRIB|| ' INVALIDO E AJUSTADO PARA '
- ||:IN_I10AVUNCOM;
- IN_I14AVUNTRIB = :IN_I10AVUNCOM;
- END
- IF (IN_I14QTRIB IS NULL OR ( IN_I14QTRIB <= 0.0000 ) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK ||' VENDA NUMERO '||:IN_NUMEROVENDA
- ||' - AJUSTE I14QTRIB '
- ||:IN_I14QTRIB|| ' INVALIDO E AJUSTADO PARA '
- ||:IN_I10QCOM;
- IN_I14QTRIB = :IN_I10QCOM;
- END
- IF (:IN_VLCUSTOPROD IN ( 0.00, 0.01, 0.02 ) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK ||' VENDA NUMERO '||:IN_NUMEROVENDA
- ||' - AVISO VLCUSTOPROD '
- ||:IN_VLCUSTOPROD|| ' CUSTO DO PRODUTO ESTÁ MUITO ABAIXO DO CUSTO'
- ||' PRATICADO NO COMERCIO, GERALMENTE ENTRE 30 A 60% DO VALOR DE '
- ||'VENDA';
- END
- L_OLD_CSTCSOSN = :IN_CSTCSOSN;
- IF (:C21CRT = 1) THEN
- BEGIN
- IF (:IN_CSTCSOSN IN ('40','41')) THEN IN_CSTCSOSN = '400';
- IF (:IN_CSTCSOSN IN ('60')) THEN IN_CSTCSOSN = '500';
- IF (:IN_CSTCSOSN IN ('00','01','20','90')) THEN IN_CSTCSOSN = '102';
- IF (:IN_CSTCSOSN NOT IN ('00', '01', '02', '20', '30', '40', '41',
- '50', '51', '60', '90','70')) THEN IN_CSTCSOSN = '102';
- IF (L_OLD_CSTCSOSN <> :IN_CSTCSOSN) THEN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE CSTCSOSN '
- || L_OLD_CSTCSOSN || ' INVALIDO PARA REGIME TRIBUTARIO DA '
- ||'EMPRESA E AJUSTADO PARA ' || :IN_CSTCSOSN;
- CSTCSOSN = :IN_CSTCSOSN;
- END
- IF (:C21CRT IN (2,3)) THEN
- BEGIN
- IF (:IN_CSTCSOSN IN ('102')) THEN IN_CSTCSOSN = '00';
- IF (:IN_CSTCSOSN IN ('500')) THEN IN_CSTCSOSN = '60';
- IF (:IN_CSTCSOSN IN ('400','300')) THEN IN_CSTCSOSN = '40';
- IF (:IN_CSTCSOSN IN ('900')) THEN IN_CSTCSOSN = '90';
- IF (:IN_CSTCSOSN IN ('103')) THEN IN_CSTCSOSN = '40';
- IF (:IN_CSTCSOSN IN ('101')) THEN IN_CSTCSOSN = '20';
- IF (:IN_CSTCSOSN IN ('201','203')) THEN IN_CSTCSOSN = '10';
- IF (:IN_CSTCSOSN IN ('202')) THEN IN_CSTCSOSN = '30';
- IF (:IN_CSTCSOSN IN ('101', '102', '103', '201', '202', '203', '300',
- '400', '500', '900')) THEN IN_CSTCSOSN = '00';
- IF (L_OLD_CSTCSOSN <> :IN_CSTCSOSN) THEN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE CSTCSOSN '
- || L_OLD_CSTCSOSN || ' INVALIDO PARA REGIME TRIBUTARIO DA '
- ||'EMPRESA E AJUSTADO PARA ' || :IN_CSTCSOSN;
- CSTCSOSN = :IN_CSTCSOSN;
- END
- L_VAR_VBRUTOPROD = ( :IN_I10AVUNCOM * :IN_I10QCOM );
- IF (:IN_I17VDESC > :L_VAR_VBRUTOPROD ) THEN
- EXCEPTION EECRITICALFAIL 'O VALOR DE DESCONTOS DO ITEM NAO PODE SER MAIOR QUE'
- ||' O VALOR BRUTO DE PRODUTOS VALOR UNITARIO * QUANTIDADE ';
- /* *****************************************************************************
- INICIO DA ANALISE DE ENTRADA DOS DADOS DE TRIBUTAÇÃO E IMPOSTOS PROC_ICMS
- -
- *******************************************************************************/
- IF ( NOT :IN_ICMSORIG IN (0, 1, 2, 3, 4, 5, 6, 7, 8) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE ICMSORIG '
- || :IN_ICMSORIG || ' NAO CORRESPONDE A UMA ORIGEM DE MERCADORIA '
- ||' VALIDA, AJUSTADO PARA 0 0 - NACIONAL, EXCETO AS INDICADAS'
- ||' NOS CÓDIGOS 3, 4, 5 E 8; ';
- IN_ICMSORIG = 0;
- END
- IF ( NOT :IN_N13MODBC IN (0, 1, 2, 3) ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE N13MODBC '
- || :IN_N13MODBC || ' MODALIDADE DA BASE DE CALCULO DO ICMS NAO'
- ||' VALIDA, AJUSTADO PARA 3 3=VALOR DA OPERACAO';
- IN_N13MODBC = 3;
- END
- IF ( :IN_PICMS <= 0.0000 ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_PICMS || ' - AJUSTE PICMS '
- || :IN_N13MODBC || ' PERCENTUAL DE ICMS INVALIDO'
- ||', AJUSTADO PARA 0.0000%';
- IN_PICMS = 0.0000;
- END
- /* *****************************************************************************
- BLOCO A1 TRATA O BLOCO CFOP E CRUZAMENTOS PARA EMPRESA DO SIMPLES NACIONAL
- SUBROUTINE CALCULA ICMS_SN SAT CFE | NFCE
- *******************************************************************************/
- L_VAR_TIPOFISCALSTR = CASE :TIPODOCFISCAL
- WHEN 0 THEN '59'
- WHEN 1 THEN '65'
- WHEN 99 THEN '99'
- END;
- IF (:C21CRT = 1 OR ( :C21CRT NOT IN ( 1,2,3 ) ) ) THEN
- BEGIN
- SELECT OUT_ALIQICMS, OUT_BCICMS, OUT_VICMS,
- OUT_VPROD, OUT_VITEM12741, OUT_CFOP, OUT_VTOTPROD
- FROM SUBROUT_CALCULAICMS_SN(:IN_CSTCSOSN, :IN_I10AVUNCOM, :IN_I17VDESC,
- :IN_I15VFRETE, :IN_I16VSEG, :IN_I17AVOUTRO,
- :IN_PICMS,:L_VAR_TIPOFISCALSTR , :IN_I10QCOM)
- INTO :PICMS, :N15VBC, :VICMS, :I11VPROD, :VITEM12741,
- :I08CFOP, :I08VTOTPROD;
- IF (:in_picms <> :PICMS) THEN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE PICMS '
- || :in_picms || ' AJUSTADO PARA SIMPLES NACIONAL '
- ||' PARA ' || :PICMS;
- IF (:in_n15vbc <> :N15VBC) THEN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK || 'VENDA NUMERO '
- || :IN_NUMEROVENDA || ' - AJUSTE N15VBC '
- || :in_n15vbc || ' AJUSTADO PARA SIMPLES NACIONAL '
- ||' PARA ' || :N15VBC;
- /* pis e cofins simples nacional */
- SELECT OUT_PISCST, OUT_VPIS, OUT_PPIS, OUT_COFINSCST, OUT_VCOFINS, OUT_PCOFINS
- FROM SUBROUT_CALCULA_PISCOFINS_SN(:L_VAR_TIPOFISCALSTR)
- INTO :PIS_CST, :VPIS, :PIS_ALIQ, :COFINS_CST, :VCOFINS, :COFINS_ALIQ;
- END
- /* ******************* FIM BLOCO A1 ****************************************/
- /* *************************************************************************
- INSERE DADOS NA TABELA DE PRODUTOS
- ************************************************************************** */
- H02NITEM = ( SELECT COALESCE(MAX( P1.H02NITEM ),0) + 1 FROM PROD P1
- WHERE P1.I001AIDPK = :IN_NUMEROVENDA );
- I16VSEG = :IN_I16VSEG;
- I15VFRETE = :IN_I15VFRETE;
- I17VDESC = :IN_I17VDESC;
- I17AVOUTRO = :IN_I17AVOUTRO;
- I17BINDTOT = :IN_I17BINDTOT;
- I02CPROD = :IN_I02CPROD;
- I03CEAN = :IN_I03CEAN;
- I04XPROD = :IN_I04XPROD;
- I05NCM = :IN_I05NCM;
- I09UCOM = :IN_I09UCOM;
- I10QCOM = :IN_I10QCOM;
- I10AVUNCOM = :IN_I10AVUNCOM;
- I12CEANTRIB = :IN_I12CEANTRIB;
- I13UTRIB = :IN_I13UTRIB;
- I14QTRIB = :IN_I14QTRIB;
- I14AVUNTRIB = :IN_I14AVUNTRIB;
- IVLCUSTOPROD = :IN_VLCUSTOPROD;
- I05ANVE = :IN_I05ANVE;
- I06EXTIPI = :IN_I06EXTIPI;
- I07IPPT = :IN_I07IPPT;
- INSERT INTO PROD (I001AIDPK, H02NITEM, I02CPROD, I03CEAN, I04XPROD, I05NCM,
- I08CFOP, I09UCOM, I10QCOM, I10AVUNCOM,
- I11VPROD, I12CEANTRIB, I13UTRIB, I14QTRIB, I14AVUNTRIB,
- I15VFRETE, I16VSEG, I17VDESC, I17AVOUTRO,
- I17BINDTOT, I05CCEST, IVLCUSTOPROD, I05ANVE, I06EXTIPI,
- I07IPPT, I08VTOTPROD)
- VALUES (:IN_NUMEROVENDA, :H02NITEM, :I02CPROD, :I03CEAN, :I04XPROD, :I05NCM,
- :I08CFOP, :I09UCOM, :I10QCOM, :I10AVUNCOM,
- :I11VPROD, :I12CEANTRIB, :I13UTRIB, :I14QTRIB,
- :I14AVUNTRIB, :I15VFRETE, :I16VSEG, :I17VDESC, :I17AVOUTRO,
- :I17BINDTOT, :I05CCEST, :IVLCUSTOPROD, :I05ANVE,
- :I06EXTIPI, :I07IPPT, :I08VTOTPROD)
- RETURNING ID
- INTO :ID;
- /* *************************************************************************
- INICIO DO BLOCO A2
- - CALCULO DE TRIBUTOS E INCIDENTES FISCAIS
- - GRAVAÇÃO DOS DADOS DE TRIBUTOS NA TABELA
- ************************************************************************** */
- ICMSORIG = :IN_ICMSORIG;
- N13MODBC = :IN_N13MODBC;
- PICMS = :IN_PICMS;
- INSERT INTO PROD_ICMS (ICMSORIG, CSTCSOSN, PICMS, PIS_CST, PIS_ALIQ,
- COFINS_CST, COFINS_ALIQ, CFOP,IBPT_ALIQMUN, IBPT_ALIQEST,
- IBPT_ALIQFED, IBPT_ALIQIMP, VICMS, VPIS, VCOFINS,
- N17BPFCP, N17CVFCP, N17AVBCFCP, N18MODBCST, N19PMVAST,
- N20PREDBCST, N21VBCST, N22PICMSST, N23AVBCFCPST, N23BPFCPST,
- N23DVFCPST, N28AVICMSDESON, N28MOTDESICMS, N26VBCSTRET,
- N26APST, N26BVICMSSUBSTITUTO,N27VICMSSTRET, N14PREDBC,
- N27AVBCFCPSTRET, N27BPFCPSTRET, N27DVFCPSTRET,
- N34PREDBCEFET, N35VBCEFET, N36PICMSEFET, N37VICMSEFET,
- N15VBC, VITEM12741)
- VALUES (:IN_ICMSORIG, :IN_CSTCSOSN, :IN_PICMS, :IN_PIS_CST, :IN_PIS_ALIQ,
- :IN_COFINS_CST,:IN_COFINS_ALIQ, :IN_CFOP, :IN_IBPT_ALIQMUN,
- :IN_IBPT_ALIQEST, :IN_IBPT_ALIQFED, :IN_IBPT_ALIQIMP, :IN_VICMS,
- :IN_VPIS, :IN_VCOFINS, :IN_N17BPFCP, :IN_N17CVFCP, :IN_N17AVBCFCP,
- :IN_N18MODBCST, :IN_N19PMVAST,:IN_N20PREDBCST, :IN_N21VBCST,
- :IN_N22PICMSST, :IN_N23AVBCFCPST, :IN_N23BPFCPST, :IN_N23DVFCPST,
- :IN_N28AVICMSDESON, :IN_N28MOTDESICMS, :IN_N26VBCSTRET, :IN_N26APST,
- :IN_N26BVICMSSUBSTITUTO, :IN_N27VICMSSTRET, :IN_N14PREDBC,
- :IN_N27AVBCFCPSTRET, :IN_N27BPFCPSTRET, :IN_N27DVFCPSTRET,
- :IN_N34PREDBCEFET, :IN_N35VBCEFET, :IN_N36PICMSEFET, :IN_N37VICMSEFET,
- :IN_N15VBC, :IN_VITEM12741)
- returning ICMSORIG, CSTCSOSN, PICMS, PIS_CST, PIS_ALIQ, COFINS_CST, COFINS_ALIQ,
- CFOP, IBPT_ALIQMUN, IBPT_ALIQEST, IBPT_ALIQFED, IBPT_ALIQIMP, VICMS,
- VPIS, VCOFINS, N17BPFCP, N17CVFCP, N17AVBCFCP, N18MODBCST, N19PMVAST,
- N20PREDBCST, N21VBCST, N22PICMSST, N23AVBCFCPST, N23BPFCPST,
- N23DVFCPST, N28AVICMSDESON, N28MOTDESICMS, N26VBCSTRET, N26APST,
- N26BVICMSSUBSTITUTO, N27VICMSSTRET, N14PREDBC, N27AVBCFCPSTRET,
- N27BPFCPSTRET, N27DVFCPSTRET, N34PREDBCEFET, N35VBCEFET, N36PICMSEFET,
- N37VICMSEFET, N15VBC, VITEM12741
- INTO :ICMSORIG, :CSTCSOSN, :PICMS, :PIS_CST, :PIS_ALIQ, :COFINS_CST,
- :COFINS_ALIQ, :CFOP, :IBPT_ALIQMUN, :IBPT_ALIQEST, :IBPT_ALIQFED,
- :IBPT_ALIQIMP, :VICMS, :VPIS, :VCOFINS, :N17BPFCP, :N17CVFCP, :N17AVBCFCP,
- :N18MODBCST, :N19PMVAST, :N20PREDBCST, :N21VBCST, :N22PICMSST,
- :N23AVBCFCPST, :N23BPFCPST, :N23DVFCPST, :N28AVICMSDESON, :N28MOTDESICMS,
- :N26VBCSTRET, :N26APST, :N26BVICMSSUBSTITUTO, :N27VICMSSTRET, :N14PREDBC,
- :N27AVBCFCPSTRET, :N27BPFCPSTRET, :N27DVFCPSTRET, :N34PREDBCEFET,
- :N35VBCEFET, :N36PICMSEFET, :N37VICMSEFET, :N15VBC, :VITEM12741;
- /* *************************************************************************
- CALCULO LUCRO, E PERCENTUAL DE LUCRO BRUTO NA VENDA
- ************************************************************************** */
- SELECT SUM( E1."108CALCLUCROBRT") ,
- SUM( I08VTOTPROD ) ,
- SUM( FLOOR( E1.I10QCOM )),
- MAX("108CALCLUCROPCT") FROM PROD E1
- WHERE E1.I001AIDPK = :IN_NUMEROVENDA
- AND E1.id = :ID
- INTO :I17PROFITABILITVAL, :L_VAR_VTOTALPRODSERV,
- :B008VOLUME, :L_VAR_PERCLUCROPROD;
- I16PROFITABILITRATE = ( :I17PROFITABILITVAL / :L_VAR_VTOTALPRODSERV ) * 100;
- IF ( L_VAR_PERCLUCROPROD <= 10.000 ) THEN
- BEGIN
- LMESSAGE = TRIM(:LMESSAGE) || SLINEBREAK ||' VENDA NUMERO '||:IN_NUMEROVENDA
- ||' - AVISO PERCENTUAL DE LUCRO '
- ||:L_VAR_PERCLUCROPROD|| ', MARKUP DO PRODUTO É MENOR QUE 10%, '
- ||'MUITO ABAIXO DO '
- ||'PRATICADO NO COMERCIO, GERALMENTE ENTRE 30 A 60% DO VALOR DE '
- ||'CUSTO';
- END
- UPDATE IDE SET B008VOLUME = :B008VOLUME,
- I16PROFITABILITRATE = :I16PROFITABILITRATE ,
- I17PROFITABILITVAL = :I17PROFITABILITVAL
- WHERE (ID = :IN_NUMEROVENDA);
- /* *************************************************************************
- GRAVA AS MENSAGENS DE ALERTA
- ************************************************************************** */
- IF (CHARACTER_LENGTH(TRIM(:LMESSAGE)) > 0 OR (NOT :LMESSAGE IS NULL) ) THEN
- UPDATE OR INSERT INTO WARNINGS (DATETIMEC, MENSAGEM, IDCAIXA, IDSESSAO, IDVENDA)
- VALUES (CURRENT_TIMESTAMP,
- TRIM(:LMESSAGE),
- :B006IDMOVCAIXA,
- :B007IDSESSAO,:IN_NUMEROVENDA)
- MATCHING (IDCAIXA, IDSESSAO, IDVENDA);
- END
- SUSPEND;
- END
Add Comment
Please, Sign In to add comment