USE `inventory`;

DROP PROCEDURE IF EXISTS `inventory`.`CetakBuktiTransaksiKoreksi`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `inventory`.`CetakBuktiTransaksiKoreksi`(IN `PID` INT)
BEGIN
	
	SET @hrgdasar = 0;
	SET @margin = 0;
	SET @hargadasar = 0;

	SELECT
		tk.ID NOMOR, tk.TANGGAL, tk.KET, ru.DESKRIPSI RUANGAN
		, CONCAT('BUKTI TRANSAKSI ',IF(tk.JENIS = 1, r1.DESKRIPSI, r2.DESKRIPSI)) JENISLAPORAN
		, br.NAMA NMBARANG
		, tkd.JUMLAH
		, @hrgdasar:=(SELECT inventory.getHrgDasarBarang(tkd.BARANG, tk.TANGGAL)) HRGDASAR
		, @margin:=(SELECT inventory.getMarginHarga(1, 1)) MARGIN
		, @hargadasar:= (@hrgdasar + (@hrgdasar * @margin / 100)) HARGADASARMARGIN
		, usr.NAMA PETUGAS
		, inst.NAMA NAMAINST, inst.ALAMAT ALAMATINST
	FROM inventory.transaksi_koreksi tk
	LEFT JOIN master.referensi r1 ON r1.ID = tk.ALASAN AND r1.JENIS=900601
	LEFT JOIN master.referensi r2 ON r2.ID = tk.ALASAN AND r2.JENIS=900602
	LEFT JOIN master.ruangan ru ON ru.ID = tk.RUANGAN
	LEFT JOIN aplikasi.pengguna usr ON usr.ID = tk.OLEH
	, inventory.transaksi_koreksi_detil tkd
	, inventory.barang br
	, (SELECT mp.NAMA, ai.PPK, mp.ALAMAT
					FROM aplikasi.instansi ai
						, master.ppk mp
					WHERE ai.PPK=mp.ID) inst
	WHERE br.ID = tkd.BARANG AND tkd.KOREKSI = tk.ID AND tk.ID = PID;
END//
DELIMITER ;