<?php
# Baca Variabel Form
$Act		= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$prodid		= isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$message = array();
if($Act=="SAVE"){
	if(count($message)==0){	
		foreach($prodid as $i => $id)
		{
			$mtp_price		= $_REQUEST['mtp_price'][$id];
			$mtp_currency	= $_REQUEST['mtp_currency'][$id];
			$mtp_nett	= $_REQUEST['mtp_nett'][$id];
			$mtp_gross	= $_REQUEST['mtp_gross'][$id];
			$mtp_box	= $_REQUEST['mtp_box'][$id];
			$mtp_pallet	= $_REQUEST['mtp_pallet'][$id];
			
			//if(($mtp_price != 0) && ($mtp_currency != 0)){
			//echo "test: $id-->$mtp_price-->$mtp_currency"; exit;
				#validasi
				$mtp = mysql_fetch_array(mysql_query("SELECT id FROM master_price WHERE mtp_prodid='$id' AND mtp_status=10"));
				if(empty($mtp['id'])){
					mysql_query("INSERT INTO master_price SET mtp_prodid='$id', mtp_price='$mtp_price', mtp_currency='$mtp_currency',
					mtp_nett='$mtp_nett', mtp_gross='$mtp_gross', mtp_box='$mtp_box', mtp_pallet='$mtp_pallet', mtp_status=10");
					#Aktivitas User
					$qrySave=mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data master price - $mtp_price', 
										  user_date='$date', user_time='$time'") or die ("Gagal query".mysql_error());	
				}else{
					mysql_query("UPDATE master_price SET mtp_price='$mtp_price', mtp_currency='$mtp_currency',
					mtp_nett='$mtp_nett', mtp_gross='$mtp_gross', mtp_box='$mtp_box', mtp_pallet='$mtp_pallet' WHERE mtp_prodid='$id'");
					#Aktivitas User
					$qrySave=mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data master price - $mtp_price', 
										  user_date='$date', user_time='$time'") or die ("Gagal query".mysql_error());
				}
			//}
		}				
		if($qrySave){		
			$message[] = "Data Barang telah tersimpan.";			
			$status	   = "OK";	
		}
  	}			
}
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php 		
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Master-Price'>";	
	}
}
?>