USE bridge;

DROP PROCEDURE IF EXISTS bridge.sinkronisasiBarang;

DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `bridge`.`sinkronisasiBarang`()
BEGIN
	DECLARE VID INT(11);
	DECLARE VNAMA CHAR(250);
	DECLARE VSPESIFIKASI TEXT;
	DECLARE VMERK,VKODE CHAR(50);
	DECLARE VSATUAN,VSITOSTATIKA, VSTATUS SMALLINT(6);

	DECLARE DATA_NOT_FOUND TINYINT DEFAULT FALSE;
	DECLARE CR_BARANG_SKA_SINKRON CURSOR FOR
		SELECT sb.ID,sb.NAMA ,sb.SPESIFIKASI ,sb.MERK ,sb.KODE, sb.SATUAN , sb.SITOSTATIKA , sb.STATUS 
			FROM bridge.ska_barang sb 
			LEFT JOIN inventory.barang_ska bs ON bs.ID = sb.ID
			WHERE bs.ID IS NULL LIMIT 5;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET DATA_NOT_FOUND = TRUE;
			
	OPEN CR_BARANG_SKA_SINKRON;
	EOF: LOOP
		FETCH CR_BARANG_SKA_SINKRON INTO VID, VNAMA, VSPESIFIKASI, VMERK, VKODE, VSATUAN, VSITOSTATIKA, VSTATUS;
		
		IF DATA_NOT_FOUND THEN
			UPDATE temp.temp SET ID = 0 WHERE ID = 0;
			LEAVE EOF;
		END IF;
		
		INSERT INTO inventory.barang_ska (ID,NAMA ,SPESIFIKASI ,MERK ,KODE, SATUAN , SITOSTATIKA , STATUS)
		VALUES (VID, VNAMA, VSPESIFIKASI, VMERK, VKODE, VSATUAN, VSITOSTATIKA, VSTATUS);
		
	END LOOP;
	CLOSE CR_BARANG_SKA_SINKRON;


	
END$$
DELIMITER ;