You are on page 1of 4

PROCEDURE LANCARCORTE ( pnNUMCAR IN NUMBER, pnCODPROD IN NUMBER, pnQTSEPARADA IN NUMBER, pnCODFUNC IN NUMBER, pnNUMPEDINI IN NUMBER, pnNUMPEDFIM IN NUMBER, pvMENSAGEM

OUT VARCHAR2) IS --- To modify this template, edit file PROC.TXT in TEMPLATE -- directory of SQL Navigator --- Purpose: Briefly explain the functionality of the procedure --- MODIFICATION HISTORY -- Person Date Comments -- -------------- -----------------------------------------vnQTSEPARADA NUMBER; vnQTCORTE NUMBER; vnQTITENS NUMBER; vnVLTOTAL NUMBER; vnVLTABELA NUMBER; vnVLCUSTOREAL NUMBER; vnQTCORTEORIG NUMBER; vnVLCUSTOFIN NUMBER; vnTOTPESOLIQ NUMBER; vnTOTPESOBRUTO NUMBER; vnTOTVOLUME NUMBER; vnTOTPESO NUMBER; --LUCIEN - 22/10/2007 vnPESOLIQ NUMBER; vsPESOQUANT VARCHAR(1); vnCODPRODPRINC NUMBER; ERRO_LANCARCORTE EXCEPTION;

-- Declare program variables as shown above BEGIN -- 1. pvMENSAGEM := '1'; -vnQTSEPARADA := pnQTSEPARADA; FOR ITEMPED1 IN ( SELECT ITEMPED.NUMPED, ITEMPED.CODPROD, ITEMPED.CODFILIALRETIRA, ITEMPED.S EQ, ITEMPED.QTPEDIDA, ITEMPED.PTABELA, ITEMPED.PVENDA, ITEMPED.VLCUSTOREAL, ITEMPED.VLCUSTOFIN, ITEMPED.QTORIG, DECODE(ITEMPED.QTORIG,ITEMPED.QTPEDIDA,ROUND(ITEMPED.QTPEDIDA / ITEMPED .QTORIG))AS FATOR FROM ITEMPED, CABPED WHERE ITEMPED.NUMPED = CABPED.NUMPED AND CABPED.NUMCAR = pnNUMCAR AND ITEMPED.CODPROD = pnCODPROD

AND CABPED.NUMPED >= pnNUMPEDINI AND CABPED.NUMPED <= pnNUMPEDFIM AND CABPED.POSICAO = DECODE(pnNUMCAR,0,'L','M') ) LOOP -IF ITEMPED1.FATOR > 0 THEN vnQTCORTEORIG:= vnQTSEPARADA / ITEMPED1.FATOR; ELSE vnQTCORTEORIG:=ITEMPED1.QTPEDIDA -(ITEMPED1.QTPEDIDA - vnQTSEPARADA); END IF; IF vnQTSEPARADA >= ITEMPED1.QTPEDIDA THEN vnQTSEPARADA := vnQTSEPARADA - ITEMPED1.QTPEDIDA; ELSE vnQTCORTE := ITEMPED1.QTPEDIDA - vnQTSEPARADA; -- 2. Registrar o Corte pvMENSAGEM := '2'; INSERT INTO CORTE ( ID, NUMCAR, NUMPED, CODPROD, CODFILIAL, DATA, CODFUNC, QTSEPARADA, QTPEDIDA, QTCORTE ) VALUES ( CORTE_DFSEQ.NEXTVAL, pnNUMCAR, ITEMPED1.NUMPED, pnCODPROD, ITEMPED1.CO DFILIALRETIRA, TRUNC(SYSDATE), pnCODFUNC, pnQTSEPARADA, ITEMPED1.QTPEDIDA, vnQTCORTE ); -- 3. Atualizou a Tabela de Itens do Pedido pvMENSAGEM := '3'; -- Lucien - Seleciona os dados do produto -SELECT PESOLIQ, NVL(PESOQUANTIDADE, 'N') AS PESOQUANTIDADE, CODPRODPRINC INTO vnPESOLIQ, vsPESOQUANT, vnCODPRODPRINC FROM PRODUTO WHERE CODPROD = pnCODPROD; ------------------------------------------------------------------------UPDATE ITEMPED SET QTPEDIDA = NVL(QTPEDIDA,0) - vnQTCORTE ,QTORIG = vnQTCO RTEORIG WHERE NUMPED = ITEMPED1.NUMPED AND CODPROD = pnCODPROD AND CODFILIALRETIRA = ITEMPED1.CODFILIALRETIRA; -DELETE FROM ITEMPED WHERE NUMPED = ITEMPED1.NUMPED AND CODPROD = pnCODPROD AND QTPEDIDA <= 0; -- 4. Atualizou a Tabela de Cabecalho do Pedido pvMENSAGEM := '4'; SELECT COUNT(*) QT,

SUM(ITEMPED.QTPEDIDA*PVENDA) AS VLTOTAL, SUM(ITEMPED.QTPEDIDA*PTABELA) AS VLTABELA, SUM(ITEMPED.QTPEDIDA*VLCUSTOREAL) AS VLCUSTOREAL, SUM(ITEMPED.QTPEDIDA*VLCUSTOFIN) AS VLCUSTOFIN, SUM(ITEMPED.QTPEDIDA*PRODUTO.PESOLIQ) AS TOTPESOLIQ, SUM(ITEMPED.QTPEDIDA*PRODUTO.PESOBRUTO) AS TOTPESOBRUTO, SUM(ITEMPED.QTPEDIDA*PRODUTO.VOLUME) AS TOTVOLUME INTO vnQTITENS, vnVLTOTAL, vnVLTABELA, vnVLCUSTOREAL, vnVLCUSTOFIN, vnTOTPESOLIQ, vnTOTPESOBRUTO, vnTOTVOLUME FROM ITEMPED, PRODUTO WHERE ITEMPED.CODPROD = PRODUTO.CODPROD AND ITEMPED.NUMPED = ITEMPED1.NUMPED; -UPDATE CABPED SET NUMITENS = vnQTITENS, VLTABELA = vnVLTABELA, VLTOTAL = vnVLTOTAL, VLCUSTOREAL = vnVLCUSTOREAL, VLCUSTOFIN = vnVLCUSTOFIN, TOTPESO = vnTOTPESOBRUTO, TOTVOLUME = vnTOTVOLUME, QTVOL = QTVOL - vnQTCORTE WHERE NUMPED = ITEMPED1.NUMPED; -DELETE FROM CABPED WHERE NUMPED = ITEMPED1.NUMPED AND NUMITENS = 0; -- 5. Tirar a Qtde cortada da Reserva pvMENSAGEM := '5'; -- Lucien - Adicionado DECODE - 22/10/2007 -UPDATE ESTOQUE SET QTRESERV = DECODE(vsPESOQUANT, 'N', GREATEST((NVL(QTRES ERV,0) - vnQTCORTE),0), GREATEST((NVL(QTRES ERV,0) - (vnPESOLIQ*vnQTCORTE)),0)) WHERE CODPROD = DECODE(NVL(vnCODPRODPRINC,0),0, pnCODPROD, vnCODPRODPRINC ) AND CODFILIAL = ITEMPED1.CODFILIALRETIRA; vnQTSEPARADA := 0; END IF; END LOOP; -- 6. Atualizacao do Carregamento pvMENSAGEM := '6'; SELECT SUM(VLTOTAL), SUM(TOTPESO), SUM(TOTVOLUME) INTO vnVLTOTAL, vnTOTPESO, vnTOTVOLUME FROM CABPED WHERE NUMCAR = pnNUMCAR;

UPDATE CARREGAMENTO SET VLTOTAL = vnVLTOTAL, TOTPESO = vnTOTPESO, TOTVOLUME = vnTOTVOLUME WHERE NUMCAR = pnNUMCAR; COMMIT WORK; pvMENSAGEM := 'OK'; EXCEPTION WHEN OTHERS THEN ROLLBACK ; pvMENSAGEM := SQLCODE '-' SQLERRM '->' pvMENSAGEM ; END; -- Procedure