DROP Procedure TMP_OP_CTRLFUELPRICEInsProcAutomatico
GO
Create Procedure TMP_OP_CTRLFUELPRICEInsProcAutomatico
(@CORRNBR	INT,
 @SITEID	UD_IDSITE,
 @DATEPROCE	UD_FECHA)
as
BEGIN
     DECLARE @CODMONEDABASE	UD_CURYID
     DECLARE @CODCATFUEL	CHAR(2)

     SET @CODMONEDABASE=(SELECT CURYORIG FROM SETUP)
     SET @CODCATFUEL=(SELECT CATEGORYFUEL FROM OP_SETUP)
          
     BEGIN TRAN 

        INSERT INTO TMP_OP_CTRLFUELPRICE(CORRNBR,SITEID,DATEPROCE,INVTIDSKU,SLS_PRICE,CURYID,QTYSALES,TOTAL)                      
        SELECT @CORRNBR,@SITEID,@DATEPROCE,IN_INVENTORYALTER.INVTIDSKU,
               CAST(PRECIO AS NUMERIC(8,4))/100,@CODMONEDABASE,
               SUM(CAST(GALONES AS NUMERIC(12,4)))/1000,SUM(CAST(SOLES AS NUMERIC(12,4)))/100 
                        FROM ((OP_HISTRANSFUEL INNER JOIN IN_INVENTORYALTER
                        ON OP_HISTRANSFUEL.producto=IN_INVENTORYALTER.INVTIDBARR)   
                        INNER JOIN IN_INVENTORY ON IN_INVENTORYALTER.INVTIDSKU=IN_INVENTORY.INVTIDSKU)
               WHERE DATEPROCE=@DATEPROCE 
        GROUP BY IN_INVENTORYALTER.INVTIDSKU,DESCR,PRECIO 


        IF (@@ERROR !=0) 
          BEGIN
             ROLLBACK           
             RAISERROR  20000 'No se pudo Actualizar' 
          END 

    COMMIT TRAN 
END

