USE `inventory`;
-- --------------------------------------------------------
-- Versi server:                 8.0.11 - MySQL Community Server - GPL
-- OS Server:                    Win64
-- HeidiSQL Versi:               9.2.0.4947
-- --------------------------------------------------------


-- Dumping structure for function inventory.getHargaBeli
DROP FUNCTION IF EXISTS `getHargaBeli`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` FUNCTION `getHargaBeli`(
	`PBARANG` INT,
	`PTANGGAL` DATETIME
) RETURNS decimal(60,2)
    DETERMINISTIC
BEGIN
	
	DECLARE VHARGA DECIMAL(60,2);
	
	SELECT 
	ROUND(((d.HARGA - d.DISKON) + (if(b.PPN = 'Ya', 0, (d.HARGA * 11/100)))), 2) INTO VHARGA
	FROM inventory.penerimaan_barang b, inventory.penerimaan_barang_detil d
	
	WHERE d.PENERIMAAN = b.ID AND d.BARANG = PBARANG AND b.TANGGAL_DIBUAT <= PTANGGAL
	ORDER BY b.TANGGAL_DIBUAT DESC LIMIT 1;
	
	IF FOUND_ROWS() > 0 then 
		RETURN VHARGA;
	ELSE 
		RETURN 0;
	END IF; 

END//
DELIMITER ;
