<?php
 	session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}


date_default_timezone_set("Asia/Kolkata");
$mdate1= date('Y-m-d H:i:s');
$mdate= date('d-m-Y');
$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	$umid = $_SESSION['umid'];
	include("connect.php");
include("mylibrary.php");
	
	//$cmname=$_POST['cmname'];
	
	
	
	
	$visatmid=$_POST['visatmid'];
	$subservicemid=$_POST['subservicemid'];
	$countrymid=$_POST['countrymid'];
	$umid1=$_POST['umid'];
	$acincentivetype=checkvalidstring ($_POST['acincentivetype']);
	$actargettype=checkvalidstring ($_POST['actargettype']);
	$actargetvalue=checkvalidstring ($_POST['actargetvalue']);
	$acmremark=checkvalidstring ($_POST['acmremark']);
	$commissionstrucher=checkvalidstring ($_POST['commissionstrucher']);
	$cmcurrency=checkvalidstring ($_POST['cmcurrency']);
	$cmgstincexc=checkvalidstring ($_POST['cmgstincexc']);
	$ctaxpercent=checkvalidstring ($_POST['ctaxpercent']);
	$commissionstruchervalue=checkvalidstring ($_POST['commissionstruchervalue']);
	$ctaxname=checkvalidstring ($_POST['ctaxname']);
	
	
	$acstartdate = date("Y-m-d",strtotime($_POST['acstartdate']));
	$acenddate = date("Y-m-d",strtotime($_POST['acenddate']));
	
	if($subservicemid == 'All')
	{
		 $eve = "select subservicemid from res_subservice_master where visatmid=$visatmid";
		$re = mysqli_query($conn, $eve);
																			
		while($rt = mysqli_fetch_assoc($re))

		{
			$subservicemid = $rt['subservicemid'];
			 $eve1 = "INSERT INTO res_agentcomission_master (umid,visatmid,subservicemid,countrymid,commissionstrucher,commissionstruchervalue,ctaxname,ctaxpercent,cmgstincexc,cmcurrency,actype,acstartdate,acenddate,acstatusacincentivetype,actargettype,actargetvalue,acmremark) VALUES('$umid1','$visatmid','$subservicemid','$countrymid','$commissionstrucher','$commissionstruchervalue','$ctaxname','$ctaxpercent','$cmgstincexc','$cmcurrency','Service','$acstartdate','$acenddate','Inactive','$acincentivetype','$actargettype','$actargetvalue','$acmremark')";
			if ($conn->query($eve1) === TRUE)
		{
			$last_id = $conn->insert_id;
			 if($commissionstrucher == 'Slab')
		  {
		  $delete_checkbox_array = $_POST['slabmid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$slabmid=$one_by_one_sid;
					
				$svalue=$_POST['svalue'][$n];
				$n = $n + 1;
			
				
				if($slabmid==0)
				{
				}
				else
				{	
					$eve2="insert into res_agentcomissionslab_detail (acsdid,slabmid,svalue) values($last_id,'$slabmid','$svalue')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
		  }
			
			$_SESSION['msg'] = "Record has been added successfully";
		}
		}
		$_SESSION['msg'] = "Record has been added successfully";
	}else{
		  $eve1 = "INSERT INTO res_agentcomission_master (umid,visatmid,subservicemid,countrymid,commissionstrucher,commissionstruchervalue,ctaxname,ctaxpercent,cmgstincexc,cmcurrency,actype,acstartdate,acenddate,acstatus,acincentivetype,actargettype,actargetvalue) VALUES('$umid1','$visatmid','$subservicemid','$countrymid','$commissionstrucher','$commissionstruchervalue','$ctaxname','$ctaxpercent','$cmgstincexc','$cmcurrency','Service','$acstartdate','$acenddate','Inactive','$acincentivetype','$actargettype','$actargetvalue')";
		
		if ($conn->query($eve1) === TRUE)
		{
			$last_id = $conn->insert_id;
			 if($commissionstrucher == 'Slab')
		  {
		  $delete_checkbox_array = $_POST['slabmid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$slabmid=$one_by_one_sid;
					
				$svalue=$_POST['svalue'][$n];
				$n = $n + 1;
			
				
				if($slabmid==0)
				{
				}
				else
				{	
					$eve2="insert into res_agentcomissionslab_detail (acsdid,slabmid,svalue) values($last_id,'$slabmid','$svalue')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			}
		  }
			
			$_SESSION['msg'] = "Record has been added successfully";
		}
		else
		{
			$_SESSION['msg1'] = "Error";
		}
	}
	
	
	
	
		 

	header("Location: userdetail.php?id=$umid1");
			
	
?>