<?php 
//ob_start();
session_start();
require_once("/home/steelmin/public_html/inc/config.php");
require_once("/home/steelmin/public_html/hf.php");
/*
ini_set('display_errors',1);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
if(isset($_REQUEST['loginthrough'])){
    if(!isset( $_POST['delegates_cnt']) || !isset( $_POST['event_id']) ||  empty($_POST['delegates_cnt']) || empty($_POST['event_id'])){
        $data['status']=false;
        $data['message']="Delegate count and event are mandatory, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
        
    }
    if(!empty($_POST['delegates_cnt']))
       $_SESSION['delegates_cnt']  = $_POST['delegates_cnt'];
    //print_r($_POST);
    if(!empty($_POST['event_id']))
       $_SESSION['event_id']  = $_POST['event_id'];
   
    $_no_delegates = $_SESSION['delegates_cnt'];
    $first_name   = htmlentities(trim($_POST['first_name']), ENT_QUOTES);
    $company_name   = htmlentities(trim($_POST['company_name']), ENT_QUOTES);
    $email   = htmlentities(trim($_POST['email']), ENT_QUOTES);
    $mobile   = htmlentities(trim($_POST['mobile']), ENT_QUOTES);
    $currDateTime=date("Y-m-d H:i:s");
    $hdnMailing_City =  trim($_POST['hdnMailing_City']);
    $other_city    = htmlentities(trim($_POST['other_city']), ENT_QUOTES);
    $ip_countryName    = trim($_POST['ip_countryName']);		
    $company_type=trim($_REQUEST['company_type']);
    $companytype=isset($_REQUEST['companytype'])?trim($_REQUEST['companytype']):0;
    $job_title=isset($_REQUEST['job_title'])?htmlentities(trim($_REQUEST['job_title']), ENT_QUOTES):'';
    //$plan_value=isset($_REQUEST['plan'])?htmlentities(trim($_REQUEST['plan']), ENT_QUOTES):'';
    $subTotal =isset($_REQUEST['total_amount'])?trim($_REQUEST['total_amount']):0; 
    //$plan_value = $_SESSION['plan_value']
    $plan_value='';
    
    if(empty($email) || empty($mobile))
    {
        $data['status']=false;
        $data['message']="Mobile and email are mandatory, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
     if(empty($subTotal) || $subTotal=='')
    {
        $data['status']=false;
        $data['message']="Amount can't be 0.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
    
    $final_ctype=array();
    $ctype_arr=explode(',',$company_type);
    foreach($ctype_arr as $ct){
        if($ct!='undefined')
        {   $final_ctype[]=$ct;     }
    }
    if(!empty($final_ctype))
        $company_type= implode(',',$final_ctype);
    else
        $company_type='';
            
   
    $country=0;
    $city=0;			
    $state=0;
     $city = $hdnMailing_City;
    if($city>0 && !empty($city))
        {
        if( "INDIA" == $ip_countryName )
        {	$clause .= ",city='".$city."'";
            $stcnt=mysql_fetch_array(mysql_query("select * from tbl_state_city where city_id='".$city."' and country_id=113 limit 1"));
            $state = $stcnt['state_id'];
            $country=113;
        }else{				
            $country=$city;
            $city=0;			
            $state=0;
        }
        }
    else if($other_city!='Type Your City' && $other_city!='Type Your Country')
        {
        $other_city = mysql_real_escape_string($other_city);
        $city=0;			
        $state=0;			
        if( "INDIA" == $ip_countryName )
            $country=113;
        else
            $country=0;
        }
    
    $camp_code ='CAMP13';
    if(trim($_REQUEST['camp_code'])!='')
        $camp_code = $_REQUEST['camp_code'];

    //$sql1     = "select * from tbl_sm_events_users where email='$email'"; 
    //$resu1     =  mysql_query($sql1);
    //$memid=''; $verified=0;
    //$_isEmailExists= isEmailExists($id = '', $email, $memid, $verified);

    //if($_isEmailExists == 0){
    $sql1     = "select * from tbl_members where email='$email' or username='$email' or addtnl_email='$email' or phoneno='$mobile' or alternate_mob_no='$mobile'"; 	
    $resu1     =  mysql_query($sql1);
    
    if(mysql_num_rows($resu1)==0){
        $ip =  $_SERVER['REMOTE_ADDR'];
        
        include_once('/home/steelmin/public_html/steeladmin/hf_360.php');
                
        $arr['tbl_members']=array(
            'name'=>$first_name,
            'job_title'=>$job_title,
            'billing_name'=>$first_name,
            'company'=>$company_name, 
            'email'=>$email, 
            'username'=>$email, 
            'phoneno'=>$mobile, 
            'ipaddress'=>$ip, 				 
            'date'=>$currDateTime, 
            'main_source'=>'events website url',
            'source_from'=>"New",
            'events_comp_type'=>$company_type,
            'companytype'=>$companytype,
            'city'=>$city,
            'state'=>$state,
            'country'=>$country,
            'other_city'=>$other_city,
            'password'=>$mobile,
            'ol_be'=>'EVT FE',
            'primary_source' =>$camp_code,
            'lead_type' => 'A1'
        );
        
        $res = create_logs_from_array_of_array($arr);
        
        $memid = $res['tbl_members'];
        
        //echo 'false';
        
    }else{			
        $res = mysql_fetch_array($resu1,MYSQL_ASSOC);
        $memid = $res['id'];
        $mobile = $res['phoneno'];
        $company_name = $res['company'];
        $first_name = $res['name'];
        
        $city1 = $res['city'];
        $state1 = $res['state'];
        $country1 = $res['country'];
        $other_city1 = $res['other_city'];
        
        //echo 'true';
    }
    if(empty($memid))
    {
        $data['status']=false;
        $data['message']="There is some issue in registration, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
    
    if($memid==96212 || $memid==115566)
        $subTotal=1;
            
    $field_array = array();
    $field_array =  array('camp_code'=>$camp_code ,'memid'=>$memid, 'eventId'=>$_SESSION['event_id']);
    addCampaignHistory($field_array);
    
    $_SESSION['user_details'] = 
                array(									
                    'id'=>$memid,
                    'email'=>$email,
                    'first_name'=>$first_name,
                    'job_title'=>$job_title,
                    'company_name'=>$company_name,
                    'mobile'=>$mobile,
                    'city'=>$city,
                    'state'=>$state,
                    'country'=>$country,
                    'other_city'=>$other_city
                );
    mysql_query("
        insert into  tbl_sm_events_registration_logs (datetime,email,registering_id,mobile,event_id) 
        values(NOW(),  '$email',$memid, '$mobile', '" . $_SESSION['event_id'] . "')
    ") or die(mysql_error()."
        insert into  tbl_sm_events_registration_logs (datetime,email,registering_id,mobile,event_id) 
        values(NOW(), '$email', $memid, '$mobile', '" . $_SESSION['event_id'] . "')
    ");
    
    makeEntryInTableFinalStatus($memid, $_SESSION['event_id']);
    $_SESSION['log_id'] = mysql_insert_id();
    $dt=date('Y-m-d');
    $tm=date('H:i:s');
        
    
    mysql_query("INSERT INTO tbl_comments(memid, `f_date`, `f_time`, `comments`, `created_by`, `comments_for`, event_id) VALUES ($memid, '$dt', '$tm', '<strong>Visited on website</strong>', 'Created from registration page  $plan_value', 'EV', ".$_SESSION['event_id'].")") or die(mysql_error());
    //echo mysql_insert_id();
        
    //log insert and amount related section		
    $bill_no = getSMorderNoForCCAVENUE("EVENTS");		
    $amtToCharge = 0;	
    if('' == $discount)
        $discount = 0;
        
    $comments='';        
    /*
    if(isset($_REQUEST['discounted_value']) && "0" != $_REQUEST['discounted_value'] && 0 != $_REQUEST['discounted_value']){
        $discount= $_REQUEST['discounted_value'];
        $comments='Promo Code : '.$coupon_code.' applied//' . $_REQUEST['discounted_value'] . "//";
    }*/
    $del_ids='';//Delegate ids is blank because user not registered as Delegate
    $selected_ids_services=''; //contains data if there is plant visit option selected
    $serviceTax=0; $SBCess=0;		
    $KKCess=0;
    $round_off =0;
    
    //st_applicable will bw change after GST
    if( "india" != strtolower($ip_countryName)){//paypal || $_SESSION['event_id']==17
        
        $crncy = 'USD';
        include_once( BASE_PATH. "sm_group_events/regProcess/paypal_country.php");
        if(in_array(strtoupper($ip_countryName), $_paypal_country)){
            
            $insrt = "INSERT INTO `tbl_sm_events_payment_details` (`event_id`, `registering_id`, `pay_amt`, `pay_date`, `pymt_details`, `pym_mode`, `bill_no`, `nb_order_no`, `nb_bid`, `card_category`, `bank_name`, `comments`, `delegates_ids`, selected_services,sub_total,discount,stax,cess,krishi_kalyan_cess ,round_off, st_applicable, no_of_memids, f_currency, created_on) VALUES(".$_SESSION['event_id'].",".$memid.", '$amtToCharge', '".$GLOBALS['currDateTime']."', 'PAYPAL', 'NEFT/Online', '$bill_no', '', '', '', '', '$comments', '$del_ids', '" . $selected_ids_services . "', " . $subTotal . ", " . $discount . ", " . $serviceTax . ", " . $SBCess . "," . $KKCess . ", " . $round_off . ", 'N', '$_no_delegates', '$crncy', '".$GLOBALS['currDateTime']."');";
        }else{
            $insrt = "INSERT INTO `tbl_sm_events_payment_details` (`event_id`, `registering_id`, `pay_amt`, `pay_date`, `pymt_details`, `pym_mode`, `bill_no`, `nb_order_no`, `nb_bid`, `card_category`, `bank_name`, `comments`, `delegates_ids`, selected_services,sub_total,discount,stax,cess,krishi_kalyan_cess ,round_off, st_applicable, no_of_memids, f_currency, created_on) VALUES(".$_SESSION['event_id'].",".$memid.", '$amtToCharge', '".$GLOBALS['currDateTime']."', 'CCAVENUE', 'NEFT/Online', '$bill_no', '', '', '', '', '$comments', '$del_ids', '" . $selected_ids_services . "', " . $subTotal . ", " . $discount . ", " . $serviceTax . ", " . $SBCess . "," . $KKCess . ", " . $round_off . ", 'Y', '$_no_delegates','$crncy','".$GLOBALS['currDateTime']."');";
        }
        
    }else{//st_applicable will bw change after GST
        $crncy = 'INR';			
        /*if($_SESSION['event_id']==21 || $_SESSION['event_id']==26 || $_SESSION['event_id']==32){//2nd iran or brazil
            $crncy = 'USD';
        }*/
        $insrt = "INSERT INTO `tbl_sm_events_payment_details` (`event_id`, `registering_id`, `pay_amt`, `pay_date`, `pymt_details`, `pym_mode`, `bill_no`, `nb_order_no`, `nb_bid`, `card_category`, `bank_name`, `comments`, `delegates_ids`, selected_services,sub_total,discount,stax,cess,krishi_kalyan_cess ,round_off, st_applicable, no_of_memids, f_currency,created_on) VALUES(".$_SESSION['event_id'].",".$memid.", '$amtToCharge', '".$GLOBALS['currDateTime']."', 'CCAVENUE', 'NEFT/Online', '$bill_no', '', '', '', '', '$comments', '$del_ids', '" . $selected_ids_services . "', " . $subTotal . ", " . $discount . ", " . $serviceTax . ", " . $SBCess . "," . $KKCess . ", " . $round_off . ", 'Y', '$_no_delegates', '$crncy','".$GLOBALS['currDateTime']."');";
    }
    
    $amtToCharge = $subTotal;		
    mysql_query($insrt) or die(mysql_error().'<pre>'.$insrt.'</pre>');
    $_SESSION['reg_log_ids'] = $evnt_pay_id =mysql_insert_id();
    
    if(empty($evnt_pay_id)){
        $data['status']=false;
        $data['message']="There is some issue, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
    $item_desc = getActiveEventName($_SESSION['event_id']);
    
    if( "india" != strtolower($ip_countryName)){//paypal
    //if( "INDIA" != $ip_countryName){//paypal
        ///include_once("sm_group_events/regProcess/paypal_country.php");
        
        
/* 			if ($_SESSION['event_id'] == 11)
            $crncy = 'USD';
        else
            $crncy = 'INR'; */
        $crncy = 'USD';
        /*if($_SESSION['event_id']==20){
            if(in_array(strtoupper($ip_countryName), $_paypal_country) !== false)
                $newPage = BASEURL."paypal/common_payment.php?reglogs_id=".$evnt_pay_id."&uid=".$memid."&amt=" . ($amtToCharge) . "&orderid=" . $bill_no. "&orderid=" . $bill_no. "&pymtSrc=online_reg&productPurchased=" . urlencode($item_desc)."&crency=$crncy&&page_name=Events";
            else			
                $newPage = BASEURL."pay/commonCheckOut.php?reglogs_id=".$evnt_pay_id."&amt=" . ($amtToCharge) . "&orderid=" . $bill_no. "&uid=".$memid."&pymtSrc=online_reg&productPurchased=" . urlencode($item_desc)."&crency=$crncy&page_name=Events";
        }
        else*/{
            if(in_array(strtoupper($ip_countryName), $_paypal_country) !== false)
                $newPage = BASEURL."paypal/common_payment.php?reglogs_id=".$evnt_pay_id."&uid=".$memid."&amt=" . intval($amtToCharge) . "&orderid=" . $bill_no. "&orderid=" . $bill_no. "&pymtSrc=online_reg&productPurchased=" . urlencode($item_desc)."&crency=$crncy&&page_name=Events";
            else			
                $newPage = BASEURL."pay/commonCheckOut.php?reglogs_id=".$evnt_pay_id."&amt=" . intval($amtToCharge) . "&orderid=" . $bill_no. "&uid=".$memid."&pymtSrc=online_reg&productPurchased=" . urlencode($item_desc)."&crency=$crncy&page_name=Events";
        }
    }else {
        /* if($_SESSION['event_id']==11){
            $amtToCharge = round($amtToCharge * 65);
            $amtToCharge += ($amtToCharge * 0.18);
        }
        if($_SESSION['event_id']==16){//iran
            $amtToCharge = round($amtToCharge * 65);
            //$amtToCharge += ($amtToCharge * 0.18);
        } */
        $crncy = 'INR';/*
        if($_SESSION['event_id']==21 || $_SESSION['event_id']==26 || $_SESSION['event_id']==32){//2nd iran
            $crncy = 'USD';
        }*/
        $newPage = BASEURL."pay/commonCheckOut.php?reglogs_id=".$evnt_pay_id."&amt=" . intval($amtToCharge) . "&orderid=" . $bill_no. "&uid=".$memid."&pymtSrc=online_reg&productPurchased=" . urlencode($item_desc)."&crency=$crncy&page_name=Events";
        //$newPage = "https://www.steelmint.com/sm_group_events/regProcess/checkout.php?reglogs_id=".$evnt_pay_id."&uid=".$memid."&amt=" . intval($amtToCharge) . "&orderid=" . $bill_no;
    }
    if($newPage!=''){
        $data['status']=true;
        $data['message']="Successfully registered, Please procceed to pay. ";
        $data['payment_link']=$newPage;
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }		
    //Log insert and amount related section ends 
    require_once("/home/steelmin/public_html/inc/desconfig.php");		
    die;
}	
require_once("/home/steelmin/public_html/inc/desconfig.php");
?>
