 /****** Object:  StoredProcedure [dbo].[P_EST_PROD_CUSTO_ACERTO_PENDENCIA_PROCESSAMENTO]    Script Date: 03/26/2014 10:46:14 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_EST_PROD_CUSTO_ACERTO_PENDENCIA_PROCESSAMENTO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_EST_PROD_CUSTO_ACERTO_PENDENCIA_PROCESSAMENTO]
GO
--EXEC P_EST_PROD_CUSTO_ACERTO_PENDENCIA_PROCESSAMENTO 1,1,58595,'2015-01-01',9.10
CREATE PROCEDURE P_EST_PROD_CUSTO_ACERTO_PENDENCIA_PROCESSAMENTO (@CD_EMP INT,
										   @CD_FILIAL INT,
										   @CD_PROD INT,
										   @DT_PROC DATETIME,
										   @VLR_PROD_CUSTO MONEY) AS
begin

---------------------------------------------------------------------------------------------------------

DELETE 
	FROM 
		EST_PROD_CPL_CUSTO_MEDIO_PROCESSAMENTO_PENDENTE
	WHERE
		CD_EMP =@CD_EMP
		AND CD_FILIAL=@CD_FILIAL
		AND CD_PROD=@CD_PROD

INSERT INTO EST_PROD_CPL_CUSTO_MEDIO_PROCESSAMENTO_PENDENTE( CD_EMP , CD_FILIAL , CD_PROD , DT_PROC , DT_CAD)
    SELECT
            @CD_EMP
        , @CD_FILIAL
        , @CD_PROD
        , @DT_PROC
        , GETDATE();

---------------------------------------------------------------------------------------------------------

UPDATE EST_PROD_CPL
  SET
      EST_PROD_CPL.VLR_CUSTO_GER = @VLR_PROD_CUSTO
    , EST_PROD_CPL.VLR_CUSTO_MD = @VLR_PROD_CUSTO
    , EST_PROD_CPL.VLR_CUSTO = @VLR_PROD_CUSTO
    , EST_PROD_CPL.VLR_CUSTO_MD_FLUTUANTE = @VLR_PROD_CUSTO
WHERE
      EST_PROD_CPL.CD_EMP = @CD_EMP
      AND EST_PROD_CPL.CD_FILIAL = @CD_FILIAL
      AND EST_PROD_CPL.CD_PROD = @CD_PROD;
---------------------------------------------------------------------------------------------------------

UPDATE EST_PROD_TRANSF_IT
  SET
      EST_PROD_TRANSF_IT.VLR_CUSTO = @VLR_PROD_CUSTO
FROM EST_PROD_TRANSF
     INNER JOIN EST_PROD_TRANSF_IT ON EST_PROD_TRANSF.CD_EMP = EST_PROD_TRANSF_IT.CD_EMP
                                      AND EST_PROD_TRANSF.CD_FILIAL = EST_PROD_TRANSF_IT.CD_FILIAL
                                      AND EST_PROD_TRANSF.CD_CTR = EST_PROD_TRANSF_IT.CD_CTR
     INNER JOIN EST_PROD_CPL ON EST_PROD_TRANSF_IT.CD_EMP = EST_PROD_CPL.CD_EMP
                                AND EST_PROD_TRANSF_IT.CD_FILIAL = EST_PROD_CPL.CD_FILIAL
                                AND EST_PROD_TRANSF_IT.CD_PROD = EST_PROD_CPL.CD_PROD
     INNER JOIN EST_PROD_TRANSF_PENDENCIA ON EST_PROD_TRANSF.CD_EMP = EST_PROD_TRANSF_PENDENCIA.CD_EMP
                                             AND EST_PROD_TRANSF.CD_FILIAL = EST_PROD_TRANSF_PENDENCIA.CD_FILIAL
                                             AND EST_PROD_TRANSF.CD_CTR = EST_PROD_TRANSF_PENDENCIA.CD_CTR
WHERE
      EST_PROD_TRANSF_IT.CD_EMP = @CD_EMP
      AND EST_PROD_TRANSF_IT.CD_FILIAL = @CD_FILIAL
      AND EST_PROD_TRANSF_IT.CD_PROD = @CD_PROD
      AND EST_PROD_TRANSF_IT.VLR_CUSTO = 0;

---------------------------------------------------------------------------------------------------------
--Remove os erros para reprocessamento
	 
DELETE FROM T
FROM EST_PROD_TRANSF_ERROS_VALIDACAO T
     INNER JOIN EST_PROD_TRANSF_IT ON T.CD_EMP = EST_PROD_TRANSF_IT.CD_EMP
                                      AND T.CD_FILIAL = EST_PROD_TRANSF_IT.CD_FILIAL
                                      AND T.CD_CTR = EST_PROD_TRANSF_IT.CD_CTR
     INNER JOIN EST_PROD_TRANSF_PENDENCIA ON T.CD_EMP = EST_PROD_TRANSF_PENDENCIA.CD_EMP
                                             AND T.CD_FILIAL = EST_PROD_TRANSF_PENDENCIA.CD_FILIAL
                                             AND T.CD_CTR = EST_PROD_TRANSF_PENDENCIA.CD_CTR
WHERE
      EST_PROD_TRANSF_IT.CD_EMP = @CD_EMP
      AND EST_PROD_TRANSF_IT.CD_FILIAL = @CD_FILIAL
      AND EST_PROD_TRANSF_IT.VLR_CUSTO = 0
      AND EST_PROD_TRANSF_IT.CD_PROD = @CD_PROD;
	  	
---------------------------------------------------------------------------------------------------------
UPDATE EST_PED_VD_IT
  SET
      EST_PED_VD_IT.VLR_IT = @VLR_PROD_CUSTO
FROM EST_PED_VD_IT
     INNER JOIN EST_PROD_CPL ON EST_PED_VD_IT.CD_EMP = EST_PROD_CPL.CD_EMP
                                AND EST_PED_VD_IT.CD_FILIAL = EST_PROD_CPL.CD_FILIAL
                                AND EST_PED_VD_IT.CD_PROD = EST_PROD_CPL.CD_PROD
     INNER JOIN EST_PED_VD_PENDENCIA ON EST_PED_VD_IT.CD_EMP = EST_PED_VD_PENDENCIA.CD_EMP
                                        AND EST_PED_VD_IT.CD_FILIAL = EST_PED_VD_PENDENCIA.CD_FILIAL
                                        AND EST_PED_VD_IT.CD_PED = EST_PED_VD_PENDENCIA.CD_PED
WHERE
      EST_PED_VD_IT.CD_EMP = @CD_EMP
      AND EST_PED_VD_IT.CD_FILIAL = @CD_FILIAL
      AND EST_PED_VD_IT.VLR_IT = 0;
---------------------------------------------------------------------------------------------------------
--Remove os erros para reprocessamento

	DELETE FROM P
	FROM EST_PED_VD_IT
		 INNER JOIN EST_PED_VD_ERROS_VALIDACAO P ON EST_PED_VD_IT.CD_EMP = P.CD_EMP
													AND EST_PED_VD_IT.CD_FILIAL = P.CD_FILIAL
													AND EST_PED_VD_IT.CD_PED = P.CD_PED
		 INNER JOIN EST_PED_VD_PENDENCIA ON EST_PED_VD_IT.CD_EMP = EST_PED_VD_PENDENCIA.CD_EMP
											AND EST_PED_VD_IT.CD_FILIAL = EST_PED_VD_PENDENCIA.CD_FILIAL
											AND EST_PED_VD_IT.CD_PED = EST_PED_VD_PENDENCIA.CD_PED
	WHERE
		  EST_PED_VD_IT.CD_EMP = @CD_EMP
		  AND EST_PED_VD_IT.CD_FILIAL = @CD_FILIAL
		  AND EST_PED_VD_IT.VLR_IT = 0
		  AND EST_PED_VD_IT.CD_PROD = @CD_PROD;

end