USE `sakti`;

DROP PROCEDURE IF EXISTS `sakti`.`onHitungUlangTotalDetailBarang`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sakti`.`onHitungUlangTotalDetailBarang`(IN `PHEADER` BIGINT)
BEGIN
	DECLARE VQTY, VTOTAL DECIMAL (60,2);
	
	SELECT 
	SUM(sbd.JUMLAH_BARANG) JML_QTY, SUM(sbd.JUMLAH_BARANG * sbd.HARGA_SATUAN) TOTAL_NILAI INTO VQTY, VTOTAL
	FROM sakti.sakti_barang_detail sbd 
	WHERE sbd.HEADER = PHEADER;

	
	IF JML_QTY > 0 THEN
	
		UPDATE sakti.sakti_coa
			SET NILAI_COA_DETAIL = VTOTAL, NILAI_VALAS_DETAIL = VTOTAL, VOL_SUB_OUTPUT = VQTY
		WHERE s.HEADER = PHEADER;
		
		UPDATE sakti.sakti_header h 
			SET h.NILAI_BAST = VTOTAL, IS_CUSTOM_ITEM = 1
		WHERE h.ID = PHEADER;
	
	END IF;
	
END//
DELIMITER ;