<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";
	@$page_id	= base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID
	@$page_id1	= base64_decode(base64_decode(base64_decode($_GET['page_id1']))); //THIS IS PAGE ID

if($page_id==1) //this is for basic details add page	
{

		$p_code		=	$_POST['p_code']; 
		$c_name		=	$_POST['c_name'];

		$con_nm1	=	$_POST['con_nm1'];
		$con_nm2	=	$_POST['con_nm2'];
		$con_nm3	=	$_POST['con_nm3'];

		$des1		=	$_POST['des1'];
		$des2		=	$_POST['des2'];
		$des3		=	$_POST['des3'];

		$mobile1	=	$_POST['mobile1'];
		$mobile2	=	$_POST['mobile2'];
		$mobile3	=	$_POST['mobile3'];

		$ln_no1		=	$_POST['ln_no1'];
		$ln_no2		=	$_POST['ln_no2'];
		$ln_no3		=	$_POST['ln_no3'];

		$of_add		=	$_POST['of_add'];
		$co_add		=	$_POST['co_add'];

		$e_id1		=	$_POST['e_id1'];
		$e_id2		=	$_POST['e_id2'];
		$e_id3		=	$_POST['e_id3'];

		$dob1		=	$_POST['dob1'];
		$dob2		=	$_POST['dob2'];
		$dob3		=	$_POST['dob3'];

		$w_url		=	$_POST['w_url'];
		if(isset($_POST['chk_image'])){
			$chk_img	=	$_POST['chk_image'];
		} else {
			$chk_img	=0;
		}
		if(isset($_POST['chk_stone'])){
			$chk_stone	=	$_POST['chk_stone'];
		} else {
			$chk_stone	=0;
		}
		if(isset($_POST['subs_image'])){
			$subs_image	=	$_POST['subs_image'];
		} else {
			$subs_image	=0;
		}
		if(isset($_POST['subs_stone'])){
			$subs_stone	=	$_POST['subs_stone'];
		} else {
			$subs_stone	=0;
		}

//		$t_no		=	$_POST['t_no'];
//		$cst_no		=	$_POST['cst_no'];
//		$tn_date	=	$_POST['tn_date'];

		$t_no		=	"";
		$cst_no		=	"";
		$tn_date	=	"";

		$of_town	=	$_POST['of_town'];
		$of_city	=	$_POST['of_city'];
		$of_state	=	$_POST['of_state'];
		$of_country	=	$_POST['of_country'];
		$sel_mem	=	$_POST['sel_mem'];
		$cour_chrgs	=   $_POST['courier_charges'];
		$gstin		=	$_POST['gst_no'];
		$direct_costing = $_POST['direct_costing'];
		$dc_cost1 = $_POST['dc_cost1'];
		$dc_cost2 = $_POST['dc_cost2'];
		$subscription_type = $_POST['sel_mem'];

		if($direct_costing==''){
			$direct_costing=0;
		}

		if($_POST['of_state_foreign'] != "foreign country")
		{
			$of_country = 1;

			$INSRT = "INSERT INTO party_det(`party_code`, `direct_costing`, `cost_val`,`gramprice`,`subscription_id`, `cmp_name`, `con_pers_1`, `con_pers_2`, `con_pers_3`, `con_pers_des_1`, `con_pers_des_2`, `con_pers_des_3`, `con_no_1`, `con_no_2`, `con_no_3`, `con_ln_no_1`, `con_ln_no_2`, `con_ln_no_3`, `com_addr`, `cour_addr`, `com_town`, `com_city`, `com_master_state_id`, `com_master_country_id`, `email_id_1`, `email_id_2`, `email_id_3`, `dob_1`, `dob_2`, `dob_3`, `web_url`, `cour_charges`, `gstin`, `design_img`,`design_stone`, `subs_img`,`subs_stone`, `tin_no`, `cst_no`, `tin_date`, `membership_id`,`post_dt`) VALUES
			 ('$p_code','$direct_costing','$dc_cost1','$dc_cost2','$subscription_type', '$c_name','$con_nm1','$con_nm2','$con_nm3','$des1','$des2','$des3','$mobile1','$mobile2','$mobile3','$ln_no1','$ln_no2','$ln_no3','$of_add','$co_add', '$of_town', '$of_city', '$of_state', '$of_country', '$e_id1','$e_id2','$e_id3','$dob1','$dob2','$dob3','$w_url', '$cour_chrgs', '$gstin', '$chk_img','$chk_stone','$subs_image','$subs_stone','$t_no','$cst_no','$tn_date','$sel_mem','$cur_time')";

		 }
		 else if($_POST['of_state_foreign'] == "foreign country")
		 {
			$INSRT = "INSERT INTO party_det (`party_code`, `direct_costing`, `cost_val`,`gramprice`,`subscription_id`,  `cmp_name`, `con_pers_1`, `con_pers_2`, `con_pers_3`, `con_pers_des_1`, `con_pers_des_2`, `con_pers_des_3`, `con_no_1`, `con_no_2`, `con_no_3`, `con_ln_no_1`, `con_ln_no_2`, `con_ln_no_3`, `com_addr`, `cour_addr`, `com_town`, `com_city`, `com_master_state_id`, `com_state_foreign`, `com_master_country_id`, `email_id_1`, `email_id_2`, `email_id_3`, `dob_1`, `dob_2`, `dob_3`, `web_url`, `cour_charges`, `gstin`, `design_img`,`design_stone`, `subs_img`,`subs_stone`,`tin_no`, `cst_no`, `tin_date`,`membership_id`, `post_dt`) VALUES
			 ('$p_code','$direct_costing','$dc_cost1','$dc_cost2','$subscription_type','$c_name','$con_nm1','$con_nm2','$con_nm3','$des1','$des2','$des3','$mobile1','$mobile2','$mobile3','$ln_no1','$ln_no2','$ln_no3','$of_add','$co_add', '$of_town', '$of_city', '0', '$_POST[of_state]', '$_POST[of_country]', '$e_id1','$e_id2','$e_id3','$dob1','$dob2','$dob3','$w_url', '$cour_chrgs', '$gstin', '$chk_img','$chk_stone','$subs_image','$subs_stone','$t_no','$cst_no','$tn_date','$sel_mem','$cur_time')";

		 }
	//	echo $INSRT;
		$QURY = mysqli_query($connect,$INSRT);

		$SLCT_sk = "SELECT MAX(party_det_id) as max_ids FROM party_det ORDER BY party_det_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids;

		$INSRT_log		= "INSERT INTO party_det_log(`party_det_id`, `cmp_name`, `active_date`) VALUES ('$max_id','$c_name','$cur_time')";
		$QURY_log		= mysqli_query($connect,$INSRT_log);

		$new_dt = date("d-m-Y", strtotime($cur_date));
		$tot_cn = $_POST['tot_cn'];
		for($i=1; $i<$tot_cn; $i++)
		{
			$cat_id = $_POST['cat_id'][$i];
			$fmdate = $_POST['fmdate'][$i];
			//$todate = $_POST['todate'][$i];
			$cost_vl = $_POST['cost_vl'][$i];

			$INSRT_d	= "INSERT INTO party_cost_det(`party_det_id`, `master_cat_id`,`fm_date`, `cost_val`,`post_dt`) VALUES('$max_id','$cat_id','$fmdate','$cost_vl','$cur_time')";		
			$QURY_d		= mysqli_query($connect,$INSRT_d);
		}
?>
		<script type="text/javascript">
            alert("Data inserted successfullyY");
            /*history.back();*/
            window.location='../party_list.php';
        </script>
<?php
}
else if($page_id==2) //this is for basic details list page	
{
	$party_id	= base64_decode(base64_decode(base64_decode($_GET['party_id'])));

	$c_name		=	$_POST['c_name'];

	$con_nm1	=	$_POST['con_nm1'];
	$con_nm2	=	$_POST['con_nm2'];
	$con_nm3	=	$_POST['con_nm3'];

	$des1		=	$_POST['des1'];
	$des2		=	$_POST['des2'];
	$des3		=	$_POST['des3'];

	$mobile1	=	$_POST['mobile1'];
	$mobile2	=	$_POST['mobile2'];
	$mobile3	=	$_POST['mobile3'];

	$ln_no1		=	$_POST['ln_no1'];
	$ln_no2		=	$_POST['ln_no2'];
	$ln_no3		=	$_POST['ln_no3'];

	$of_add		=	$_POST['of_add'];
	$co_add		=	$_POST['co_add'];

	$e_id1		=	$_POST['e_id1'];
	$e_id2		=	$_POST['e_id2'];
	$e_id3		=	$_POST['e_id3'];

	$dob1		=	$_POST['dob1'];
	$dob2		=	$_POST['dob2'];
	$dob3		=	$_POST['dob3'];

	$w_url		=	$_POST['w_url'];

//	$t_no		=	$_POST['t_no'];
//	$cst_no		=	$_POST['cst_no'];
//	$tn_date	=	$_POST['tn_date'];	
		if(isset($_POST['chk_image'])){
			$chk_img	=	$_POST['chk_image'];
		} else {
			$chk_img	=0;
		}
		if(isset($_POST['chk_stone'])){
			$chk_stone	=	$_POST['chk_stone'];
		} else {
			$chk_stone	=0;
		}
		if(isset($_POST['subs_image'])){
			$subs_image	=	$_POST['subs_image'];
		} else {
			$subs_image	=0;
		}
		if(isset($_POST['subs_stone'])){
			$subs_stone	=	$_POST['subs_stone'];
		} else {
			$subs_stone	=0;
		}


	$t_no		=	"";
	$cst_no		=	"";
	$tn_date	=	"";
	
	$of_town	=	$_POST['of_town'];
	$of_city	=	$_POST['of_city'];
	$of_state	=	$_POST['of_state'];
	$of_country	=	$_POST['of_country'];
	$sel_mem	=	$_POST['sel_mem'];
	$direct_costing = $_POST['direct_costing'];
	$dc_cost1 = $_POST['dc_cost1'];
	$dc_cost2 = $_POST['dc_cost2'];
	$subscription_type = $_POST['sel_mem'];

	$gstin		=	$_POST['gst_no'];
	$courier_charges = $_POST['courier_charges'];

		if($direct_costing==''){
			$direct_costing=0;
		}


		$SLCT_sk1 = "SELECT * FROM master_state where state_name='".$_POST['of_state']."'";
		$QURY_sk = mysqli_query($connect,$SLCT_sk1);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		if(mysqli_num_rows($QURY_sk)>0){
			$master_state_id = $FTCH_sk->master_state_id;
		} else{
			$master_state_id =0;
		}

	if($_POST['of_state_foreign'] != "foreign country")
	{
		$of_country = 1;
		
		 $UPDATE = "UPDATE party_det SET `cmp_name` = '$c_name', `direct_costing`='$direct_costing', `cost_val`='$dc_cost1',`gramprice`='$dc_cost2',`subscription_id`='$subscription_type',`con_pers_1` = '$con_nm1', `con_pers_2` = '$con_nm2', `con_pers_3` = '$con_nm3', `con_pers_des_1` = '$des1', `con_pers_des_2` = '$des2', `con_pers_des_3` = '$des3', `con_no_1` = '$mobile1', `con_no_2` = '$mobile2', `con_no_3` = '$mobile3', `con_ln_no_1` = '$ln_no1', `con_ln_no_2` = '$ln_no2', `con_ln_no_3` = '$ln_no3', `com_addr` = '$of_add', `cour_addr` = '$co_add', `com_town` = '$of_town', `com_city` = '$of_city', `com_master_state_id`='$master_state_id',`com_state_foreign` = '$of_state', `com_state_foreign` = '', `com_master_country_id` = '$of_country', `email_id_1` = '$e_id1', `email_id_2` = '$e_id2', `email_id_3` = '$e_id3',`dob_1`='$dob1', `dob_2`='$dob2', `dob_3`='$dob3', `web_url` = '$w_url', `cour_charges` = '$courier_charges', `gstin` = '$gstin', `tin_no` = '$t_no', `cst_no` = '$cst_no', `tin_date` = '$tn_date',`design_img` = '$chk_img',`design_stone` = '$chk_stone',`subs_img` = '$subs_image',`subs_stone` = '$subs_stone',`update_dt` = '$cur_time',`membership_id` = '$sel_mem' WHERE party_det_id = '$party_id'";
	}
	else if($_POST['of_state_foreign'] == "foreign country")
	{
		$UPDATE = "UPDATE party_det SET `cmp_name` = '$c_name', `direct_costing`='$direct_costing', `cost_val`='$dc_cost1',`gramprice`='$dc_cost2',`subscription_id`='$subscription_type',`con_pers_1` = '$con_nm1', `con_pers_2` = '$con_nm2', `con_pers_3` = '$con_nm3', `con_pers_des_1` = '$des1', `con_pers_des_2` = '$des2', `con_pers_des_3` = '$des3', `con_no_1` = '$mobile1', `con_no_2` = '$mobile2', `con_no_3` = '$mobile3', `con_ln_no_1` = '$ln_no1', `con_ln_no_2` = '$ln_no2', `con_ln_no_3` = '$ln_no3', `com_addr` = '$of_add', `cour_addr` = '$co_add', `com_town` = '$of_town', `com_city` = '$of_city', `com_master_state_id` = '$master_state_id', `com_state_foreign` = '$of_state', `com_master_country_id` = '$of_country', `email_id_1` = '$e_id1', `email_id_2` = '$e_id2', `email_id_3` = '$e_id3',`dob_1`='$dob1', `dob_2`='$dob2', `dob_3`='$dob3', `web_url` = '$w_url', `cour_charges` = '$courier_charges', `gstin` = '$gstin', `tin_no` = '$t_no', `cst_no` = '$cst_no', `tin_date` = '$tn_date', `design_img` = '$chk_img',`design_stone` = '$chk_stone', `subs_img` = '$subs_image',`subs_stone` = '$subs_stone', `update_dt` = '$cur_time',`membership_id` = '$sel_mem' WHERE party_det_id = '$party_id'";
	}
	//echo $UPDATE;
	//exit;
	$QRY    = mysqli_query($connect,$UPDATE); 

	$max_id = $party_id;

	$SLCT_SLC = "SELECT * FROM party_det_log WHERE party_det_id='$max_id' AND cmp_name = '$c_name' AND statu_s='1'";
	$QURY_SLC = mysqli_query($connect,$SLCT_SLC);  
	if(mysqli_num_rows($QURY_SLC)>0)
	{}
	else
	{
		$prev_date = date('Y-m-d', strtotime($cur_time .' -1 day'));

		$UPDATE = "UPDATE party_det_log SET `statu_s`='0', `inactive_date` = '$prev_date' WHERE party_det_id = '$party_id' AND statu_s='1'";
		$QRY    = mysqli_query($connect,$UPDATE);

		$INSRT_log = "INSERT INTO party_det_log(`party_det_id`, `cmp_name`, `active_date`) VALUES ('$max_id','$c_name','$cur_time')";
		$QURY_log = mysqli_query($connect,$INSRT_log);
	}

	$new_dt = date("d-m-Y", strtotime($cur_date));
	$tot_cn = $_POST['tot_cn'];
	for($i=1; $i<$tot_cn; $i++)
	{
		$cat_id = $_POST['cat_id'][$i];
		$fmdate = $_POST['fmdate'][$i];
		//$todate = $_POST['todate'][$i];
		$cost_vl = $_POST['cost_vl'][$i];
		$SLCT_cat_check = "SELECT * FROM party_cost_det WHERE master_cat_id = '$cat_id' AND party_det_id = '$party_id' AND updated = 'no'";
		//echo $SLCT_cat_check;
		$QURY_cat_check = mysqli_query($connect,$SLCT_cat_check);
		if(mysqli_num_rows($QURY_cat_check)>0)
		{
			$SLCT = "SELECT * FROM party_cost_det WHERE master_cat_id = '$cat_id' AND party_det_id = '$party_id' AND updated = 'no' AND fm_date != ''";
			//echo $SLCT;
			$QURY = mysqli_query($connect,$SLCT);
			if(mysqli_num_rows($QURY)>0) //this condition for checking the category id is exist in the table or not
			{
				$FTCH = mysqli_fetch_object($QURY);
				if($FTCH->cost_val != 0) { $ex_cost_vl = $FTCH->cost_val ; }else{ $ex_cost_vl = ""; }

				if( $ex_cost_vl != $cost_vl || $FTCH->fm_date != $fmdate) //this condition for checking the existing value and new values
				{
					$todate = date('d-m-Y',(strtotime ( '-1 day' , strtotime ( $fmdate) ) ));
					$todate =  date("d-m-Y", strtotime($todate));

					$UPDATE_dt = "UPDATE party_cost_det SET `to_date`='$todate', `updated` = 'yes',`update_dt` = '$cur_time' WHERE updated = 'no' AND party_det_id = '$party_id' AND master_cat_id = '$cat_id'";
					//echo $UPDATE_dt."<br/>";
					$QRY_dt    = mysqli_query($connect,$UPDATE_dt);

					$INSRT_d	= "INSERT INTO party_cost_det(`party_det_id`, `master_cat_id`,`fm_date`, `cost_val`,`post_dt`) VALUES('$max_id','$cat_id','$fmdate','$cost_vl','$cur_time')";	
					//echo $INSRT_d."<br/>";
					$QURY_d		= mysqli_query($connect,$INSRT_d);	

				}

			}
			else
			{
				if($fmdate!='')
				{
				$UPDATE_dt = "UPDATE party_cost_det SET `fm_date`='$fmdate', `cost_val`='$cost_vl', `update_dt` = '$cur_time' WHERE updated = 'no' AND party_det_id = '$party_id' AND master_cat_id = '$cat_id'";
				$QRY_dt    = mysqli_query($connect,$UPDATE_dt);
				//echo $UPDATE_dt."df<br/>";
				}

			}
		}
		else
		{
			$INSRT_d	= "INSERT INTO party_cost_det(`party_det_id`, `master_cat_id`,`fm_date`, `cost_val`,`post_dt`) VALUES('$max_id','$cat_id','$fmdate','$cost_vl','$cur_time')";	
			//echo $INSRT_d."<br/>";
			$QURY_d		= mysqli_query($connect,$INSRT_d);
		}
	}
	?>
  			<script type="text/javascript">
    		alert("Party Data(s) Updated successfully");
    		//history.back();
   			window.location='../party_list.php';
  			</script>
   <?php
}
else if($page_id1==3) //this is for basic details list page	
{
	$party_id = base64_decode(base64_decode(base64_decode($_GET['party_id'])));
	$get_status = $_GET['statu_s'];
	if($get_status=='active'){
		$db_status = 'Inactive';
	} else {
		$db_status = 'active';
	}

	 $UPDATE_dt = "UPDATE party_det SET `statu_s`='$db_status' where party_det_id = '$party_id'";
	$QRY_dt    = mysqli_query($connect,$UPDATE_dt);
	?>

	<script type="text/javascript">
    		alert("Party Status Updated successfully");
    		//history.back();
   			window.location='../party_list.php';
  			</script>

<?php } ?>

