<?php	ob_start();


    include_once 'includes/session.php';
    include_once 'includes/constants.php';
    include_once 'includes/functions.php';
    include_once 'testmail.php';

    $partners=new partners;
    $partners->connection($host,$user,$pass,$db);

#------------------------------------------------------------------------------
# getting variables
#------------------------------------------------------------------------------

# getting ip address
    $ipaddress		= $HTTP_SERVER_VARS['REMOTE_ADDR'];
	if(empty($ipaddress)) $ipaddress = $_SERVER['REMOTE_ADDR'];

# Merchantid
	$mid					=    intval(trim($_GET['mid']));

# random no
   $leadRand				= trim($_GET['sec_id']);

# tranactionid
   $orderId				    = $_GET['orderId'];

	$referer 				= 	getenv(HTTP_REFERER);

# sub id
	$subid					= $_GET['subid'];

#------------------------------------------------------------------------------
# checking whether its a geniune lead request
#------------------------------------------------------------------------------

 # chacking whether the sale request is the outcome of a rececnt click
 # withing 60sec
 # from the same ip as click

 $unSql = "SELECT transaction_orderid FROM  partners_transaction WHERE transaction_orderid = '".addslashes($orderId)."' AND transaction_referer='".addslashes($referer)."' AND transaction_type='lead'" ;
 $unRet = @mysql_query($unSql);

 if(@mysql_num_rows($unRet)>0){
  # lead is by refresh
 }
 else{
   $sql = " SELECT * ,date_format(DATE_ADD( ipblocking_time, INTERVAL 60 MINUTE ),'%H/%i/%s/%d/%m/%y') as d ";
  $sql.= " FROM partners_ipblocking ";
  $sql.= " WHERE ipblocking_ipaddress = '".addslashes($ipaddress)."'";
  $sql.= " And ipblocking_randNo = '".addslashes($leadRand)."'  ";
  $sql.= " And ipblocking_merchantid= '$mid'  ORDER BY ipblocking_time DESC ";
  $ret = @mysql_query($sql);


  //if there is entry in ip block table
  if(mysql_num_rows($ret)>0){
     $row        = mysql_fetch_object($ret);
     $ipblock    = $row->d;
     $ipid       = $row->ipblocking_id;

	$mid         = $row->ipblocking_merchantid;
	$aid         = $row->ipblocking_affiliateid;
	$pgmid       = $row->ipblocking_programid;
	$joinid      = $row->ipblocking_joinpgmid;
	$linkid      = $row->ipblocking_linkid;
	$leadrate    = $row->ipblocking_lead;
	$statusid    = $row->ipblocking_statusid;

 # comparing last click and current click time
     	if(!CompareTime($ipblock)) {
			//if not within ip block period
	
			# updating lead
			GetTransaction($mid,$pgmid,$joinid,$aid,$linkid,$leadrate,$statusid,$minimum_amount,$admin_leadrate,$orderId,$subid, $admin_leadrate_type) ;
	
			//update ip block table entry
			# updating last click time
			$sql  ="update partners_ipblocking set ipblocking_time=now(),ipblocking_randNo='".addslashes($leadRand)."'  where ipblocking_id='$ipid'";
			@mysql_query($sql);
     	}
	 	else  {
			# Added By DPT on 01/July/05
			# If within Ip block period then also mark the lead but merchant doesn't have to pay for this
			AddLeadTransaction($mid,$pgmid,$joinid,$aid,$linkid,$leadrate,$statusid,$minimum_amount,$admin_leadrate,$orderId,$subid);
		}
  }
 }
 
 
# ------------------------------------------------------------------------------
# Function records the transctions
# And updates all required tables
#-------------------------------------------------------------------------------

function GetTransaction($mid,$pgmid,$joinid,$aid,$linkid,$leadrate,$statusid,$minimum_amount,$admin_leadrate,$orderId,$subid,$admin_leadrate_type)
  {

		# gets the http referer
		   $referer				= getenv(HTTP_REFERER);

		# GEts the Lead Commision for Programs
		$sql_lead = "SELECT * FROM partners_program WHERE program_id = '$pgmid' ";
		$res_lead = @mysql_query($sql_lead);
		if(@mysql_num_rows($res_lead) > 0)
		{
			$row_lead = mysql_fetch_object($res_lead);
	
			$pgm_admin_lead		= $row_lead->program_admin_lead;
			$pgm_admin_leadtype	= $row_lead->program_admin_leadtype;
			$admin_default		= $row_lead->program_admin_default;
			
			$mailmerchant 	=	$row_lead->program_mailmerchant;
			$mailaffiliate 	=	$row_lead->program_mailaffiliate;
			
			$program_countries = $row_lead->program_countries;
		}	


		if($admin_default)
		{
			if($admin_leadrate_type == "percentage") {
				$admin_lead_rate  = ($leadrate * $admin_leadrate)/100;
			}
			else {
			 	$admin_lead_rate  =  $admin_leadrate;  
			}
		}
		else  //If Admin has set Trans comm for the Program
		{
			if($pgm_admin_leadtype == "%") {
				$admin_lead_rate  = ($leadrate * $pgm_admin_lead)/100;
			}
			else {
				$admin_lead_rate  =  $pgm_admin_lead;
			}
		}		

	#GEst the Approval method for the cyurrent lead made by the affiliate
	#$approvalValue = GetLeadApproval($pgmid, $joinid);
	list($approval, $geoLead, $leadrate) = split("~",GetLeadValues($pgmid, $joinid));

  #  automatic approval
  if($approval=="automatic") 
  {

  # set status to approved
     $approvalstatus = "approved";



  $today         = date("Y-m-d");
  //$admin_amount  = $admin_leadrate;
  $admin_amount  = $admin_lead_rate;
  $ip 			= $GLOBALS['REMOTE_ADDR'];

  //checking ip of user against offer available countries  jin on 6th Jul
  $user_ip = (ip2long($ip) < 0 ? -1 * ip2long($ip) : ip2long($ip));
  $temp_con = "OUT";

  $offer_in = explode(",",$program_countries);

  //if geo-targeting feature is enabled for click, then check whether the click is from allowed list of countries
  if($geoLead)
  {
	  for($cind=0;$cind<count($offer_in);$cind++){
	  $selq = "SELECT `ip_from`,`ip_to` FROM `partners_countryFlag` WHERE `country_name` = '".$offer_in[$cind]."'"
			  ." AND ip_from <=".$user_ip." AND `ip_to` >=".$user_ip."";
			if(mysql_num_rows(mysql_query($selq))) $temp_con = "IN";
	  }
   }
   else $temp_con = "IN";

  // add transaction
  $validLead = 0;
  if($temp_con == "OUT")
  {
    $sql = "INSERT into partners_transaction(transaction_joinpgmid,transaction_type,transaction_status,transaction_dateoftransaction,transaction_amttobepaid,transaction_linkid,transaction_admin_amount,transaction_referer,transaction_ip,transaction_orderid,transaction_subid) values ('$joinid','lead','$approvalstatus','$today',0,'$linkid',0,'".addslashes($referer)."','".addslashes($ip)."','".addslashes($orderId)."','".addslashes($subid)."') ";
  }
  else
  {
  	$validLead = 1;
  	$sql="INSERT into partners_transaction(transaction_joinpgmid,transaction_type,transaction_status,transaction_dateoftransaction,transaction_amttobepaid,transaction_dateofpayment ,transaction_linkid,transaction_admin_amount,transaction_referer,transaction_orderid,transaction_ip,transaction_subid) values ('$joinid','lead','$approvalstatus','$today','$leadrate','$today','$linkid','$admin_amount','".addslashes($referer)."','".addslashes($orderId)."','".addslashes($ip)."','".addslashes($subid)."') ";
  }
  mysql_query($sql);
  UpdateLeadsCount($joinid);

	//If transaction is successful then transfer of money between account
	if($validLead == 1)
	{
			  # get merchant account
			  $sql        =	"SELECT *  FROM `merchant_pay`  WHERE pay_merchantid='$mid' ";
			  $res        =	mysql_query($sql);
			  $num		  =	mysql_num_rows($res);
			  $cut_off    = $minimum_amount;
			  if($num>0) 
			  {
				  $row1               =   mysql_fetch_object($res);
				  $merchant_balance   = $row1->pay_amount;
				  //if(($merchant_balance-($leadrate+$admin_leadrate) )<=$cut_off)
				  if(($merchant_balance-($leadrate + $admin_lead_rate) )<=$cut_off)
				  {
				  }else
				  {
			
				#---------------------------------------------------------------------------
				# Update Account info of admin,affil,merchant
				#---------------------------------------------------------------------------
			
				# Adding money to affiliate account
					//$merchant_balance = $merchant_balance-($leadrate+$admin_leadrate);
					$merchant_balance = $merchant_balance-($leadrate + $admin_lead_rate);
					$sql1       ="SELECT *  FROM `affiliate_pay` where pay_affiliateid='$aid' ";
					$res1       =mysql_query($sql1);
					//echo @mysql_error();
			
					$num = mysql_num_rows($res1);
					if($num>0)
					{
					   $row1       =  mysql_fetch_object($res1);
					   $curamount  = $row1->pay_amount;
					   $curamount  = $curamount+$leadrate;
			
					   $sql2 ="update affiliate_pay set pay_amount='$curamount' where pay_affiliateid='$aid'";
					   $ret2 =mysql_query($sql2);
					}
					else
					{
					   $sql2 ="INSERT INTO  affiliate_pay set pay_affiliateid='$aid' , pay_amount='$leadrate'";
					   $ret2 =mysql_query($sql2);
					}
			
				# affiliate account editing Ends here-
			
				# Subtract Money From Merchant Account
				   $sql        = "UPDATE  `merchant_pay` SET  pay_amount='$merchant_balance'  WHERE pay_merchantid='$mid' ";
				   $res        = mysql_query($sql);
				# END OF  Subtract Money From Merchant Account
			
				# Adding To Admins Account
			
				   $sql1      ="SELECT *  FROM `admin_pay`  ";
				   $res1      =mysql_query($sql1);
				   //echo mysql_error();
				   $num=mysql_num_rows($res1);
				   if($num>0)
				   {
					  $row1   =   mysql_fetch_object($res1);
					  $admin_curamount    = $row1->pay_amount;
					  //$admin_curamount    = $admin_curamount + $admin_leadrate;
					  $admin_curamount    = $admin_curamount + $admin_lead_rate;
				   }
			
				  $sql        ="UPDATE `admin_pay` SET `pay_amount` = '$admin_curamount'";
				  $res        =mysql_query($sql);
			
				# END OF Adding to admin's account
			   }
			 }
	}   //  END Valid Lead
	
 }
 else
 {
  $approvalstatus = "pending";          //manuall approval
  $today          = date("Y-m-d");
  //$admin_amount   = $admin_leadrate;
  $admin_amount   = $admin_lead_rate;
  $ip 			= $GLOBALS['REMOTE_ADDR'];

  //checking ip of user against offer available countries  jin on 6th Jul
  $user_ip = (ip2long($ip) < 0 ? -1 * ip2long($ip) : ip2long($ip));
  $temp_con = "OUT";

  $offer_in = explode(",",$program_countries);

  //if geo-targeting feature is enabled for click, then check whether the click is from allowed list of countries
  if($geoLead)
  {
	  for($cind=0;$cind<count($offer_in);$cind++){
	  $selq = "SELECT `ip_from`,`ip_to` FROM `partners_countryFlag` WHERE `country_name` = '".$offer_in[$cind]."'"
			  ." AND ip_from <=".$user_ip." AND `ip_to` >=".$user_ip."";
			if(mysql_num_rows(mysql_query($selq))) $temp_con = "IN";
	  }
   }
   else $temp_con = "IN";

  // add transaction
	if($temp_con == "OUT")
		$sql = "INSERT into partners_transaction(transaction_joinpgmid,transaction_type,transaction_status,transaction_dateoftransaction,transaction_amttobepaid,transaction_linkid,transaction_admin_amount,transaction_referer,transaction_ip,transaction_orderid,transaction_subid) values ('$joinid','lead','$approvalstatus','$today',0,'$linkid',0,'".addslashes($referer)."','".addslashes($ip)."','".addslashes($orderId)."','".addslashes($subid)."') ";
	else
		$sql = "INSERT into partners_transaction(transaction_joinpgmid,transaction_type,transaction_status,transaction_dateoftransaction,transaction_amttobepaid,transaction_linkid,transaction_admin_amount,transaction_referer,transaction_orderid,transaction_ip,transaction_subid) values ('$joinid','lead','$approvalstatus','$today','$leadrate','$linkid','$admin_amount','".addslashes($referer)."','".addslashes($orderId)."','".addslashes($ip)."','".addslashes($subid)."') ";
	
	mysql_query($sql);
	UpdateLeadsCount($joinid);

 }
  # maling
 if ($mailmerchant =="yes") {
        //MailMerchant($aid,$mid,$pgmid,$leadrate,$admin_leadrate,"lead");
 		MailMerchant($aid,$mid,$pgmid,$leadrate,$admin_lead_rate,"lead");
 }
 if($mailaffiliate =="yes"){
        MailAffilaite($aid,$mid,$pgmid,$leadrate,"lead");
 }

}

 #---------------------------------------------------------------------------
 # Added By DPT on 01/July/05
 # if click is within IP block period mark the lead as transaction
 #---------------------------------------------------------------------------
function AddLeadTransaction($mid,$pgmid,$joinid,$aid,$linkid,$leadrate,$statusid,$minimum_amount,$admin_leadrate,$orderId,$subid)
{

		# gets the http referer
	   	$referer				= getenv(HTTP_REFERER);
	  	# amount will be 0 in this case
	  	$admin_amount = 0;
		$today          = date("Y-m-d");
		$ip 			= $GLOBALS['REMOTE_ADDR'];

		# getting lead approval type
		#Gets the Approval method for the cyurrent lead made by the affiliate
		#$approval = GetLeadValues($pgmid, $joinid);
		list($approval, $geoLead, $leadrate) = split("~",GetLeadValues($pgmid, $joinid));

		# GEts the Mail sending type for the Program
		$sql_lead = "SELECT * FROM partners_program WHERE program_id = '$pgmid' ";
		$res_lead = @mysql_query($sql_lead);
		if(@mysql_num_rows($res_lead) > 0)
		{
			$row_lead = mysql_fetch_object($res_lead);

			$mailmerchant 	=	$row_lead->program_mailmerchant;
			$mailaffiliate 	=	$row_lead->program_mailaffiliate;
		}	


	  #  automatic approval
	  	if($approval=="automatic")
            $approvalstatus = "approved";        # set status to approved
	 	else
          	$approvalstatus = "pending";    //manuall approval
		      

		$sql = "INSERT into partners_transaction(transaction_joinpgmid,transaction_type,transaction_status,transaction_dateoftransaction,transaction_amttobepaid,transaction_linkid,transaction_admin_amount,transaction_referer,transaction_ip,transaction_orderid,transaction_subid) values ('$joinid','lead','$approvalstatus','$today',0,'$linkid',0,'".addslashes($referer)."','".addslashes($ip)."','".addslashes($orderId)."','".addslashes($subid)."') ";
		mysql_query($sql);
	 	UpdateLeadsCount($joinid);

	 # maling
	 if ($mailmerchant =="yes") {
			MailMerchant($aid,$mid,$pgmid,0,0,"lead");
	 }
	 if($mailaffiliate =="yes"){
			MailAffilaite($aid,$mid,$pgmid,0,"lead");
	 }
}


 #-----------------------------------------------------------------------------
 # comparing date
 #----------------------------------------------------------------------------
function CompareTime($ipblock) {

	# making ot mktime foamt
             $dtarray       =explode("/",$ipblock);
             $iphour        =$dtarray[0];
             $ipminute      =$dtarray[1];
             $ipsecond      =$dtarray[2];
             $ipdate        =$dtarray[3];
             $ipmonth       =$dtarray[4];
             $ipyear        =$dtarray[5];

    # current
             $d=date("d");
             $m=date("m");
             $y=date("Y");
             $h=date("H");
             $i=date("i");
             $s=date("s");
             $today=mktime($h,$i,$s,$m,$d,$y)."<br/>";
             $ipblock= mktime($iphour,$ipminute,$ipsecond,$ipmonth,$ipdate,$ipyear);

    #compare time
             if($ipblock<$today)
                return true;
             else
                return false;

      }

#-----------------------------------------------------------------------------
# Get the Approval type of the Lead for the affiliate
#----------------------------------------------------------------------------
	function GetLeadValues($pgmid, $joinid) 
	{
		# Gets the actual Lead  COmmissions value for the Affiliate
		# Checks if admin has assigned default commission structure for the affiliate
			$sql_def_comm = "SELECT * FROM partners_joinpgm, partners_pgm_commission 
				WHERE joinpgm_id='$joinid' AND joinpgm_commissionid != '0'
				AND joinpgm_commissionid = commission_id ";
			$res_def_comm = mysql_query($sql_def_comm);
			if(mysql_num_rows($res_def_comm) > 0) {
				$row_def_comm = mysql_fetch_object($res_def_comm);
				
				 $approval	=	$row_def_comm->commission_leadapproval;
				 $geoLead 	=	$row_def_comm->commission_geotargeting_lead; 
				 $leadRate	=	$row_def_comm->commission_leadrate;  
			} 
			else 
			{
				$sql_joinpgm = "SELECT * FROM partners_joinpgm WHERE joinpgm_id='$joinid' ";
				$res_joinpgm = mysql_query($sql_joinpgm);
				$row_joinpgm = mysql_fetch_object($res_joinpgm);
				
				$leadsMade	= $row_joinpgm->joinpgm_lead_count;
				if(!$leadsMade) $leadsMade	= 1;
	
				$sql_lead_comm = "SELECT commission_leadapproval, commission_geotargeting_lead, commission_leadrate FROM partners_pgm_commission WHERE commission_programid='$pgmid' 
					AND commission_lead_from <= '$leadsMade' AND commission_lead_to >=  '$leadsMade' ";
				$res_lead_comm = mysql_query($sql_lead_comm);
				if(mysql_num_rows($res_lead_comm) > 0) 
				{
					list($approval, $geoLead, $leadRate)	= mysql_fetch_row($res_lead_comm);
				} 
				
				
				#checking whether any group exist for this program
					$sql	=	"select * from partners_group where  group_programid='$pgmid'";
					$ret	=	@mysql_query($sql);
			
					# group exists
					if(@mysql_num_rows($ret)>0) {
					   $row				=	@mysql_fetch_object($ret);
					   $group			=	$row->group_id;
					   $leadgroup		=	$row->group_leadrate;
			
					   # checking whether member of any group
					   $sql1	=	"select * from partners_joinpgm where  joinpgm_id='$joinid' and joinpgm_group='$group'";
					   $ret1	=	@mysql_query($sql1);
			
					   if(@mysql_num_rows($ret1)>0) {
							 $leadRate	=	$leadgroup;
					   }
				   }
				#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
				
			}
		return $approval."~".$geoLead."~".$leadRate;
	}	


#-----------------------------------------------------------------------------
# Update number of  Leads for the affiliate
#----------------------------------------------------------------------------
	function UpdateLeadsCount($joinid)
	{
		$sql = "UPDATE partners_joinpgm SET joinpgm_lead_count=joinpgm_lead_count+1 WHERE joinpgm_id='$joinid' ";
		$res = mysql_query($sql);  
	}
?>