USE `sakti`;
DROP PROCEDURE IF EXISTS `setDataCoa`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `setDataCoa`(IN `PHEADER` BIGINT, IN `PNOMOR` BIGINT)
BEGIN
	INSERT INTO sakti_coa (HEADER, KODE_COA, NILAI_COA_DETAIL, NILAI_VALAS_DETAIL, VOL_SUB_OUTPUT)
	(
		SELECT
			PHEADER HEADER
			, ak.AKUN KODE_COA
			, SUM(pd.JUMLAH * pd.HARGA) NILAI_COA_DETAIL
			, SUM(pd.JUMLAH * pd.HARGA) NILAI_VALAS_DETAIL
			, SUM(pd.JUMLAH) VOL_SUB_OUTPUT
		FROM sakti.sakti_bast_realisasi sba
		, inventory.penerimaan_barang pb
		LEFT JOIN aplikasi.pengguna pg ON pg.ID = pb.OLEH
		, inventory.penerimaan_barang_detil pd
		, `bridge`.ska_penerimaandetil spd
		, `bridge`.ska_penerimaan sp
		, `bridge`.ska_pesanan ps
		, `bridge`.ska_permintaananggaran pa
		, `bridge`.ska_akun ak
		WHERE
		ak.ID = pa.AKUN 
		AND pa.NOMOR = ps.NOKEN 
		AND ps.ID = sp.IDPESANAN
		AND sp.ID = spd.IDPENERIMAAN
		AND spd.ID = pd.REF_PO_DETIL
		AND pd.PENERIMAAN = pb.ID AND pd.`STATUS` = 2
		AND pb.ID = sba.REF AND sba.ID = PNOMOR
		#GROUP BY pb.ID
	);
END//
DELIMITER ;