USE `inventory`;
-- --------------------------------------------------------
-- Versi server:                 8.0.11 - MySQL Community Server - GPL
-- OS Server:                    Linux
-- HeidiSQL Versi:               9.2.0.4947
-- --------------------------------------------------------


-- Dumping structure for trigger inventory.penerimaan_barang_detil_after_update
DROP TRIGGER IF EXISTS `penerimaan_barang_detil_after_update`;
SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_ENGINE_SUBSTITUTION';
DELIMITER //
CREATE TRIGGER `penerimaan_barang_detil_after_update` AFTER UPDATE ON `penerimaan_barang_detil` FOR EACH ROW BEGIN
	DECLARE VBARANG_RUANGAN,VBARANG BIGINT;
	DECLARE VTANGGAL_TERIMA DATETIME;
	DECLARE VTANGGAL_PEMBATALAN DATETIME;	
	DECLARE VRUANGAN CHAR(10);
	DECLARE VPPN CHAR(10);
	
	DECLARE VHRG_DSR_DISKON DECIMAL(60,2);
	DECLARE VHRG_DSR_DISKON_PPN DECIMAL(60,2);
	
	IF OLD.STATUS != NEW.STATUS THEN
		SELECT IF(br.ID IS NULL,0,br.ID), pb.TANGGAL_DIBUAT, pb.RUANGAN, pb.PPN INTO VBARANG_RUANGAN, VTANGGAL_TERIMA, VRUANGAN, VPPN
		  FROM inventory.penerimaan_barang pb
		  		 LEFT JOIN inventory.barang_ruangan br ON br.RUANGAN = pb.RUANGAN AND br.BARANG = OLD.BARANG
		 WHERE pb.ID = OLD.PENERIMAAN;
	 	
	 	IF FOUND_ROWS() > 0 THEN
	 		
	 		IF VBARANG_RUANGAN > 0 THEN
	 			SET VBARANG = VBARANG_RUANGAN;
	 		ELSE
	 			SET VBARANG = (SELECT inventory.autoInsertBarangRuangan(VRUANGAN, OLD.BARANG));
	 		END IF;
	 		
	 		
		 	IF OLD.STATUS = 1 AND NEW.STATUS = 2 THEN
					INSERT INTO inventory.transaksi_stok_ruangan(BARANG_RUANGAN, JENIS, REF, TANGGAL, JUMLAH)
						  VALUES(VBARANG, 21, OLD.ID, VTANGGAL_TERIMA, NEW.JUMLAH);
				UPDATE inventory.barang  SET HARGA_BELI = NEW.HARGA WHERE ID = NEW.BARANG;
				
				
				SET VHRG_DSR_DISKON = (((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) / NEW.JUMLAH);
				SET VHRG_DSR_DISKON_PPN = ((((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) + (IF(VPPN = 'Ya', 0, (((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) * 11 / 100)))) / NEW.JUMLAH);

				UPDATE inventory.monitoring_harga 
				SET HARGA_DASAR = NEW.HARGA, HARGA_INCLUDE_DISKON = VHRG_DSR_DISKON, HARGA_INCLUDE_DISKON_PPN = VHRG_DSR_DISKON_PPN, REF = NEW.ID
				WHERE BARANG = NEW.BARANG;
				
				
			END IF;
			
			
			IF OLD.STATUS = 2 AND NEW.STATUS = 1 THEN
				INSERT INTO inventory.transaksi_stok_ruangan(BARANG_RUANGAN, JENIS, REF, TANGGAL, JUMLAH)
					  VALUES(VBARANG, 24, OLD.ID, VTANGGAL_TERIMA, NEW.JUMLAH);
			END IF;
	 	END IF;
	END IF;
	
	IF OLD.RECALC != NEW.RECALC THEN
		SELECT IF(br.ID IS NULL,0,br.ID), pb.TANGGAL_DIBUAT, pb.RUANGAN, pb.PPN INTO VBARANG_RUANGAN, VTANGGAL_TERIMA, VRUANGAN, VPPN
		  FROM inventory.penerimaan_barang pb
		  		 LEFT JOIN inventory.barang_ruangan br ON br.RUANGAN = pb.RUANGAN AND br.BARANG = OLD.BARANG
		 WHERE pb.ID = OLD.PENERIMAAN;
	 	
	 	IF FOUND_ROWS() > 0 THEN
	 		
	 		IF VBARANG_RUANGAN > 0 THEN
	 			SET VBARANG = VBARANG_RUANGAN;
	 		ELSE
	 			SET VBARANG = (SELECT inventory.autoInsertBarangRuangan(VRUANGAN, OLD.BARANG));
	 		END IF;
	 		
		 	IF OLD.FINAL != NEW.IS_MUTASI_FINAL AND NEW.RECALC = 2 THEN
					INSERT INTO inventory.transaksi_stok_ruangan(BARANG_RUANGAN, JENIS, REF, TANGGAL, JUMLAH)
						  VALUES(VBARANG, 21, OLD.ID, VTANGGAL_TERIMA, OLD.JUMLAH);
				UPDATE inventory.barang  SET HARGA_BELI = OLD.HARGA WHERE ID = OLD.BARANG;
				
				SET VHRG_DSR_DISKON = (((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) / NEW.JUMLAH);
				SET VHRG_DSR_DISKON_PPN = ((((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) + (IF(VPPN = 'Ya', 0, (((NEW.JUMLAH * NEW.HARGA) - NEW.DISKON) * 11 / 100)))) / NEW.JUMLAH);

				UPDATE inventory.monitoring_harga 
				SET HARGA_DASAR = NEW.HARGA, HARGA_INCLUDE_DISKON = VHRG_DSR_DISKON, HARGA_INCLUDE_DISKON_PPN = VHRG_DSR_DISKON_PPN, REF = NEW.ID
				WHERE BARANG = NEW.BARANG;
				
			END IF;
			
			IF NEW.BATAL != OLD.IS_MUTASI_BATAL AND NEW.RECALC = 1 THEN
				INSERT INTO inventory.transaksi_stok_ruangan(BARANG_RUANGAN, JENIS, REF, TANGGAL, JUMLAH)
					  VALUES(VBARANG, 24, OLD.ID, VTANGGAL_TERIMA, OLD.JUMLAH);
			END IF;
	 	END IF;
	END IF;
	
END//
DELIMITER ;
