USE `sakti`;
DROP PROCEDURE IF EXISTS `setHeader`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `setHeader`(IN `PBAST` CHAR(50), IN `PTGL` DATETIME, IN `PSP` CHAR(50), IN `PREF` BIGINT)
BEGIN
	INSERT INTO sakti_header (BAST_REALISASI,KODE_AKUN_SKA,NO_SP, KODE_SATKER,KODE_TAHUN_ANGGARAN,NO_DOKUMEN,TGL_BUKU,TGL_DOKUMEN,URAIAN_DOKUMEN,KATEGORI,NAMA_PENERIMA,NO_REKENING,KODE_MATA_UANG,NILAI_BAST,NO_DIPA,TGL_DIPA,NILAI_KURS,KODE_COA,NPWP)
	(
		SELECT
			PREF,
			ak.AKUN,
			PSP,
			'548886' KODE_SATKER, 
			ps.RBATAHUN KODE_TAHUN_ANGGARAN,
			PBAST NO_DOKUMEN,
			PTGL TGL_BUKU,
			PTGL TGL_DOKUMEN,
			ps.KETERANGAN URAIAN_DOKUMEN,
			'1' KATEGORI,
			'Tim Penerima' NAMA_PENERIMA,
			'80027005488861000' NO_REKENING,
			'IDR' KODE_MATA_UANG,
			SUM(pd.JUMLAH * psd.HARGASATUAN) NILAI_BAST,
			'DIPA-024.04.2.548886/2023' NO_DIPA,
			'2022-11-30 00:00:00' TGL_DIPA,
			'1' NILAI_KURS,
			(SELECT `getCOA`(ak.AKUN)) KODE_COA
			, '001456011801000' NPWP
		FROM `bridge`.ska_penerimaan pn
		, `bridge`.ska_penerimaandetil pd
		, `bridge`.ska_pesanandetil psd
		, `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 = pn.IDPESANAN AND 
		psd.ID = pd.IDPESANANDETIL AND pd.IDPENERIMAAN = pn.ID 
	);
END//
DELIMITER ;