<?php
 	session_start(); 

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



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");

	
	$bankname=str_replace ("'","\'",$_POST['bankname']);
	$bankname=str_replace ('"','\"',$bankname);
	$bankwebsite=str_replace ("'","\'",$_POST['bankwebsite']);
	$bankwebsite=str_replace ('"','\"',$bankwebsite);
	$bankbranch=str_replace ("'","\'",$_POST['bankbranch']);
	$bankbranch=str_replace ('"','\"',$bankbranch);
	
	$countrymid=str_replace ("'","\'",$_POST['countrymid']);
	$countrymid=str_replace ('"','\"',$countrymid);
	$statemid=str_replace ("'","\'",$_POST['statemid']);
	$statemid=str_replace ('"','\"',$statemid);
	$citymid=str_replace ("'","\'",$_POST['citymid']);
	$citymid=str_replace ('"','\"',$citymid);
	$bankpostalcode=str_replace ("'","\'",$_POST['bankpostalcode']);
	$bankpostalcode=str_replace ('"','\"',$bankpostalcode);
	
	
	$accountopendate=date("Y-m-d", strtotime($_POST['accountopendate']));
	$bankaccounttype=str_replace ("'","\'",$_POST['bankaccounttype']);
	$bankaccounttype=str_replace ('"','\"',$bankaccounttype);
	$bankaccountnumber=str_replace ("'","\'",$_POST['bankaccountnumber']);
	$bankaccountnumber=str_replace ('"','\"',$bankaccountnumber);
	$bankcurrentbalance=str_replace ("'","\'",$_POST['bankcurrentbalance']);
	$bankcurrentbalance=str_replace ('"','\"',$bankcurrentbalance);
	$bankmicrcode=str_replace ("'","\'",$_POST['bankmicrcode']);
	$bankmicrcode=str_replace ('"','\"',$bankmicrcode);
	$bankiffccode=str_replace ("'","\'",$_POST['bankiffccode']);
	$bankiffccode=str_replace ('"','\"',$bankiffccode);
	$bankswiftcode=str_replace ("'","\'",$_POST['bankswiftcode']);
	$bankswiftcode=str_replace ('"','\"',$bankswiftcode);
	$bankbranchcode=str_replace ("'","\'",$_POST['bankbranchcode']);
	$bankbranchcode=str_replace ('"','\"',$bankbranchcode);
	$bankcustomerid=str_replace ("'","\'",$_POST['bankcustomerid']);
	$bankcustomerid=str_replace ('"','\"',$bankcustomerid);
	
	
	
	
	
	
	$contactname=str_replace ("'","\'",$_POST['contactname']);
	$contactname=str_replace ('"','\"',$contactname);
	
	$cmiddlename=str_replace ("'","\'",$_POST['cmiddlename']);
	$cmiddlename=str_replace ('"','\"',$cmiddlename);
	
	$csurname=str_replace ("'","\'",$_POST['csurname']);
	$csurname=str_replace ('"','\"',$csurname);
	
	$csurname=str_replace ("'","\'",$_POST['csurname']);
	$csurname=str_replace ('"','\"',$csurname);
	
		$contactnumber=str_replace ("'","\'",$_POST['contactnumber']);
	$contactnumber=str_replace ('"','\"',$contactnumber);
	$mobilecode=$_POST['mobilecode'];
	$contactnumber=$mobilecode."".$contactnumber;
	$cmobilenumber=str_replace ("'","\'",$_POST['cmobilenumber']);
	$cmobilenumber=str_replace ('"','\"',$cmobilenumber);
	$homemobilecode=$_POST['homemobilecode'];
	$cmobilenumber=$homemobilecode."".$cmobilenumber;
	$contactemail=str_replace ("'","\'",$_POST['contactemail']);
	$contactemail=str_replace ('"','\"',$contactemail);
	$caltenateemail=str_replace ("'","\'",$_POST['caltenateemail']);
	$caltenateemail=str_replace ('"','\"',$caltenateemail);
	
	$contactdepartmnet='Owner';
	
	$cposition=str_replace ("'","\'",$_POST['cposition']);
	$cposition=str_replace ('"','\"',$cposition);
	$csubcategory='Direct';
	
	$cremarks=str_replace ("'","\'",$_POST['cremarks']);
	$cremarks=str_replace ('"','\"',$cremarks);
	
	$caddress1=str_replace ("'","\'",$_POST['caddress1']);
	$caddress1=str_replace ('"','\"',$caddress1);
	
	$caddress2=str_replace ("'","\'",$_POST['caddress2']);
	$caddress2=str_replace ('"','\"',$caddress2);
	
	$cmsgappid=$_POST['cmsgappid'];
	
	$cmsgappname=str_replace ("'","\'",$_POST['cmsgappname']);
	$cmsgappname=str_replace ('"','\"',$cmsgappname);
	
	$cvideoappname=str_replace ("'","\'",$_POST['cvideoappname']);
	$cvideoappname=str_replace ('"','\"',$cvideoappname);
	
	$cvideoappid=$_POST['cvideoappid'];
	
	if($_SESSION['utype']=="admin")
	{ 
	$branchmid=str_replace ("'","\'",$_POST['branchmid']);
	}
	else
	{
		$branchmid= $_SESSION['branchid'];
	}
	$companymid= $_POST['companymid'];
	 $eve3 = "select * from res_company_master where companymid='$companymid'";
	$re3 = mysqli_query($conn, $eve3);
	if(mysqli_num_rows($re3) > 0)
	{
		while($rt3 = mysqli_fetch_assoc($re3))
		{
			$companyname = $rt3['companyname'];
		}
	}
	
	
	$attechment="";
	/* if(empty($_FILES['attechment']['name']) == false  )
	{
		$attechment = date("YmdHis").$_FILES['attechment']['name'];
		$path = "docs/";
		move_uploaded_file( $_FILES['attechment']['tmp_name'], $path ."/". $attechment );
	
	}
	 */
	
	
		
	 $eve = "select * from res_bank_master where bankname='$bankname'";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		$_SESSION['msg'] = "bank Already Inserted.";
		header('Location: bank.php');
	}
	else
	{	
		
		   $eve1 = "INSERT INTO res_bank_master (branchmid,bankname,bankwebsite,bankbranch,accountopendate,bankaccounttype,bankaccountnumber,bankcurrentbalance,bankmicrcode,bankiffccode,companymid,bankbranchcode,bankcustomerid,bankswiftcode) VALUES('$branchmid','$bankname','$bankwebsite','$bankbranch','$accountopendate','$bankaccounttype','$bankaccountnumber','$bankcurrentbalance','$bankmicrcode','$bankiffccode','$companymid','$bankbranchcode','$bankcustomerid','$bankswiftcode')";
	
			
			
			if($conn->query($eve1) === TRUE)
	{
			$last_id = $conn->insert_id;
		  $eve2 = "INSERT INTO res_contact_master (contactname, contactemail,contactnumber,cmobilenumber,contactdepartmnet,cposition,cremarks,caltenateemail,caddress1,caddress2,ccountry,cstate,ccity,cpostalcode,cmiddlename,csurname,cmsgappname,cmsgappid,cvideoappname,cvideoappid,ccompanyname,csubcategory,clinktype,clinkid) VALUES('$contactname','$contactemail','$contactnumber','$cmobilenumber','$contactdepartmnet','$cposition','$cremarks','$caltenateemail','$caddress1','$caddress2','$countrymid','$statemid','$citymid','$bankpostalcode','$cmiddlename','$csurname','$cmsgappname','$cmsgappid','$cvideoappname','$cvideoappid','$companyname','$csubcategory','Bank','$last_id')";
		
		$re2 = mysqli_query($conn, $eve2);
		
		$delete_checkbox_array = $_POST['bankaccountdname'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$bankaccountdname=$one_by_one_sid;
				$bankaccountmiddlename=$_POST['bankaccountmiddlename'][$n];
				$bankaccountsurname=$_POST['bankaccountsurname'][$n];
					
				
			
				
				if($bankaccountdname=='')
				{
				}
				else
				{	
					$eve2="insert into res_bankaccholdername_detail (bankmid,bankaccountdname,bankaccountmiddlename,bankaccountsurname) values($last_id,'$bankaccountdname','$bankaccountmiddlename','$bankaccountsurname')";
					$re2 = mysqli_query($conn, $eve2);
				}
$n++;				
				
			}
	
	$_SESSION['msg'] = "Record saved successfully";
	}else{
		$_SESSION['msg1'] = "Error";
	}
	header('Location: bank.php');
			
	}
?>