<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$dateTimeNow = dateTimeNow();

/*$indicator_Query = "SELECT t_pemohon.*
				  FROM t_pemohon
				  WHERE emel = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, fetch_postget_val("emel", $_POST, $_GET), PDO::PARAM_STR);
$PDO_stmt->execute();
$indicator_List2 = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

if($indicator_List2['id_pemohon'] > 0)
{
	echo -777;
	exit(0);
}*/

$ladang_id = fetch_postget_val("ladang", $_POST, $_GET);
$pemilik_id = fetch_postget_val("pemilik", $_POST, $_GET);

$active_ruminan = fetch_postget_val("active_ruminan", $_POST, $_GET);
$active_tenusu = fetch_postget_val("active_tenusu", $_POST, $_GET);
$active_babi = fetch_postget_val("active_babi", $_POST, $_GET);
$active_aneka = fetch_postget_val("active_aneka", $_POST, $_GET);
$ruminan_id = 0;
$tenusu_id = 0;
$babi_id = 0;
$aneka_id = 0;

$indicator_Query = "SELECT *
				  FROM i_dun
				  WHERE id_dun = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, fetch_postget_val("dun_premis", $_POST, $_GET), PDO::PARAM_STR);
$PDO_stmt->execute();
$dun_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 2; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();

	$kpbaru = fetch_postget_val("kpbaru_pemilik", $_POST, $_GET);
	$year = ((substr($kpbaru, 0, 2) <= date("y"))?'20':'19').substr($kpbaru, 0, 2);
	$tkh_lahir = $year."-".substr($kpbaru, 2, 2)."-".substr($kpbaru, 4, 2);
	$id_jantina = (substr($kpbaru, -1) % 2 == 0 ? 2 : 1);
	
	$indicator_Array = array();
	$indicator_Array[] = array('nama_pemilik', fetch_postget_val("nama_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('kpbaru_pemilik', fetch_postget_val("kpbaru_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tkhlahir_pemilik', $tkh_lahir, PDO::PARAM_STR);
	$indicator_Array[] = array('id_jantina', $id_jantina, PDO::PARAM_STR);
	$indicator_Array[] = array('id_bangsa', fetch_postget_val("bangsa_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tel_pemilik', fetch_postget_val("tel_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('alamat_pemilik', fetch_postget_val("alamat_pemilik", $_POST, $_GET), PDO::PARAM_STR);
	
	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	
	$whereCondition = "WHERE id_pemilik = $pemilik_id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemilik', $indicator_Array, $whereCondition);

	$indicator_Array2 = array();
	$indicator_Array2[] = array('id_pemilik', $pemilik_id, PDO::PARAM_INT);
	$indicator_Array2[] = array('id_premis', fetch_postget_val("id_premis", $_POST, $_GET), PDO::PARAM_STR,1);
	$indicator_Array2[] = array('alamat_ladang', fetch_postget_val("alamat_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('nama_penternak', fetch_postget_val("nama_penternak", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('gps_ladang_lat', fetch_postget_val("gps_lat", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('gps_ladang_long', fetch_postget_val("gps_long", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_negeri', 11, PDO::PARAM_STR); //hardcode to penang
	$indicator_Array2[] = array('id_daerah', $dun_List['id_daerah'], PDO::PARAM_STR);
	$indicator_Array2[] = array('id_mukim', 0, PDO::PARAM_STR); //what is this?
	$indicator_Array2[] = array('id_parlimen', $dun_List['id_parlimen'], PDO::PARAM_STR);
	$indicator_Array2[] = array('id_dun', fetch_postget_val("dun_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_status_ladang', fetch_postget_val("status_ladang_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_projek_kerajaan', fetch_postget_val("projek_kerajaan_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_katpemilik', fetch_postget_val("katpemilik_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_status_lesen', fetch_postget_val("status_lesen_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_sistem_pemeliharaan', fetch_postget_val("sistem_pemeliharaan_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_hakmilik', fetch_postget_val("hakmilik_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_kategori_tanah', fetch_postget_val("kategori_tanah_premis", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('id_syarat_nyata_tanah', fetch_postget_val("syarat_nyata_tanah_premis", $_POST, $_GET), PDO::PARAM_STR);

	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	
	$whereCondition = "WHERE id_ladang = $ladang_id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_ladang', $indicator_Array2, $whereCondition);
	
	if($active_ruminan > 0)
	{
		$indicator_Array3 = array();
		$indicator_Array3[] = array('id_ladang', $pemilik_id, PDO::PARAM_INT);
		$indicator_Array3[] = array('lr_luas_kandang', fetch_postget_val("luas_kandang_ruminan", $_POST, $_GET), PDO::PARAM_STR);
		//$indicator_Array3[] = array('luas_fodder', 888 /*fetch_postget_val("luas_fodder_ruminan", $_POST, $_GET)*/, PDO::PARAM_STR);
		//$indicator_Array3[] = array('luas_ragutan', 777 /*fetch_postget_val("luas_ragutan_ruminan", $_POST, $_GET)*/, PDO::PARAM_STR);
		$indicator_Array3[] = array('id_insert', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array3[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_ladang_ruminan', $indicator_Array3);
		$ruminan_id = $PDO->lastInsertId();
	}
	
	if($active_tenusu > 0)
	{
		$indicator_Array4 = array();
		$indicator_Array4[] = array('id_ladang', $pemilik_id, PDO::PARAM_INT);
		$indicator_Array4[] = array('lt_luas_kandang', fetch_postget_val("luas_kandang_tenusu", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array4[] = array('id_insert', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array4[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_ladang_tenusu', $indicator_Array4);
		$tenusu_id = $PDO->lastInsertId();
	}
	
	if($active_babi > 0)
	{
		$indicator_Array5 = array();
		$indicator_Array5[] = array('id_ladang', $pemilik_id, PDO::PARAM_INT);
		$indicator_Array5[] = array('lb_luas_ladang', fetch_postget_val("luas_kandang_babi", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array5[] = array('id_insert', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array5[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_ladang_babi', $indicator_Array5);
		$babi_id = $PDO->lastInsertId();
	}
	
	if($active_aneka > 0)
	{
		$indicator_Array6 = array();
		$indicator_Array6[] = array('id_ladang', $pemilik_id, PDO::PARAM_INT);
		$indicator_Array6[] = array('la_bil_kandang', fetch_postget_val("bil_kandang_aneka", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array6[] = array('id_insert', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array6[] = array('tkh_insert', $dateTimeNow, PDO::PARAM_STR);
		
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT($PDO, 'm_ladang_aneka', $indicator_Array6);
		$aneka_id = $PDO->lastInsertId();
	}
	
	$indicator_Array7 = array();
	$indicator_Array7[] = array('id_ladang_ruminan', $ruminan_id, PDO::PARAM_INT);
	$indicator_Array7[] = array('id_ladang_tenusu', $tenusu_id, PDO::PARAM_INT);
	$indicator_Array7[] = array('id_ladang_babi', $babi_id, PDO::PARAM_INT);
	$indicator_Array7[] = array('id_ladang_aneka', $aneka_id, PDO::PARAM_INT);
	
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_ladang = $ladang_id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_ladang', $indicator_Array7, $whereCondition);


	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>