
use bridge;
DROP PROCEDURE IF EXISTS `bridge`.`sendBridgePenerimaanSkaDetail`;

DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `bridge`.`sendBridgePenerimaanSkaDetail`(IN `PID` INT, IN `PPENERIMAAN` INT, IN `PPPN` INT, IN `PFLAG` INT)
BEGIN
	DECLARE VID_DETIL INT;
	DECLARE VBARANG INT;
	DECLARE VJUMLAH DECIMAL(10,2);
	DECLARE VHARGA,VHRGDASAR DECIMAL(20,2);
	DECLARE VDISKON DECIMAL(10,2);
	DECLARE VONGKIR DECIMAL(10,2);
	DECLARE VSTATUS TINYINT;
	DECLARE VJENISBARANG SMALLINT;
	DECLARE VTKDN SMALLINT;
	DECLARE SUCCESS INT DEFAULT TRUE;
	DECLARE DATA_NOT_FOUND_DETIL TINYINT DEFAULT FALSE;
	DECLARE CR_PENERIMAAN_DETIL CURSOR FOR
		SELECT pnd.ID, pd.IDBARANG, pnd.JUMLAH, pd.HARGASATUAN
				 , (((pd.HARGASATUAN - (pd.HARGASATUAN * (pd.POTONGAN / 100)) + (pd.ONGKIR / pd.VOLUME)) + ((pd.HARGASATUAN - (pd.HARGASATUAN * (pd.POTONGAN / 100)) + (pd.ONGKIR / pd.VOLUME)) * (PPPN / 100))))
				 , pd.POTONGAN, pd.ONGKIR, pnd.STATUS, pd.IDJENISBARANG, IF(pd.IDJENISBARANG=2,100,pd.TKDN)
		  FROM `bridge`.ska_penerimaandetil pnd,
		  		 `bridge`.ska_pesanandetil pd
		 WHERE pnd.IDPENERIMAAN = PID
		   AND pd.ID = pnd.IDPESANANDETIL;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET DATA_NOT_FOUND_DETIL = TRUE;
	
	OPEN CR_PENERIMAAN_DETIL;
		EOF_DETIL: LOOP
			FETCH CR_PENERIMAAN_DETIL INTO VID_DETIL, VBARANG, VJUMLAH, VHRGDASAR, VHARGA, VDISKON, VONGKIR, VSTATUS, VJENISBARANG, VTKDN;
			
				IF DATA_NOT_FOUND_DETIL THEN
					UPDATE temp.temp SET ID = 0 WHERE ID = 0;
					LEAVE EOF_DETIL;
				END IF;
				
				BEGIN
					
					IF NOT EXISTS(
						SELECT 1 
						  FROM inventory.penerimaan_barang_detil pbd 
						 WHERE pbd.PENERIMAAN = PPENERIMAAN AND pbd.REF_PO_DETIL = VID_DETIL) THEN
						IF VSTATUS = 1 THEN
							INSERT INTO inventory.penerimaan_barang_detil(PENERIMAAN, BARANG, REF_BRG, REF_QTY, REF_HRG, ONGKIR, BONUS, DISKON, DISKON_P, MASA_BERLAKU, STATUS,REF_PO_DETIL, IDJENISBARANG, TKDN)
							VALUES(PPENERIMAAN, 0, VBARANG, VJUMLAH, VHARGA, VONGKIR, 0, VDISKON
							, IF((VDISKON / ((VJUMLAH * VHARGA) * 100)) IS NULL,0,(VDISKON / ((VJUMLAH * VHARGA) * 100)))
							, null, 1, VID_DETIL, VJENISBARANG, VTKDN);	
						END IF;
					ELSE
						IF PFLAG = 1 THEN
							UPDATE inventory.penerimaan_barang_detil
							   SET REF_BRG = VBARANG
							   	 , REF_QTY = VJUMLAH
							   	 , REF_HRG = VHARGA
							   	 , DISKON = VDISKON
							   	 , DISKON_P = VDISKON / ((VJUMLAH * VHARGA) * 100)
							   	 , ONGKIR = VONGKIR
							   	 , IDJENISBARANG = VJENISBARANG
							   	 , TKDN = VTKDN
							 WHERE PENERIMAAN = PPENERIMAAN 
							   AND REF_PO_DETIL = VID_DETIL;
						ELSE
							UPDATE inventory.penerimaan_barang_detil
						   SET REF_BRG = VBARANG
						   	 , REF_QTY = VJUMLAH
						   	 , REF_HRG = VHARGA
						   	 , BONUS = 0
						   	 , DISKON = VDISKON
						   	 , DISKON_P = VDISKON / ((VJUMLAH * VHARGA) * 100)
						   	 , ONGKIR = VONGKIR
						   	 , STATUS = IF(VSTATUS = 0 , 0 , 1)
						   	 , IDJENISBARANG = VJENISBARANG
							 , TKDN = VTKDN
						 WHERE PENERIMAAN = PPENERIMAAN 
						   AND REF_PO_DETIL = VID_DETIL AND STATUS IN (1,0);
						END IF;
					END IF;
				END;
		END LOOP;
		
	CLOSE CR_PENERIMAAN_DETIL;
	
	UPDATE bridge.ska_penerimaan SET FLAG_KIRIM = 2 WHERE ID = PID;
	
END//
DELIMITER ;