<?php
$db = db_connect();
$interest=$db->query("select * from arbit_fund where status='1'")->getResult();
if($interest)
{

	foreach ($interest as $interest) 
	{
		$coin=$interest->coin_id;
		$find_coin=$db->query("select * from coin_info where coid='".$coin."'")->getRow();
		$find_plan=$db->query("select * from arbit_plans where id='".$interest->plan_id."'")->getRow();
		$profit=$find_plan->profit;
		$amount=$find_plan->amount;
		$int_amt=($amount*$profit)/100;
		$period=$find_coin->payout_peroid;
		$start_date=$interest->invest_date;
		$end_date=$interest->mature_date;
		$now=date("Y-m-d H:i:s");
		$flag=0;
		$row_chk=$db->query("select * from history where description LIKE '%arbit%' and type='interest' and depositid='".$interest->id."' order by date desc limit 1")->getRow();
		if($row_chk)
		{
			$date=date("Y-m-d",strtotime($row_chk->date));
			$now=date("Y-m-d");
			$old_date = $date;
			if($period=='1')
			{
				$days='1';
				$next_due_date = date('Y-m-d', strtotime($old_date. ' +'.$days.' days'));
			}
			/*else if($period=='2')
			{
				$days='1';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' days'));
			}
			else if($period=='3')
			{
				$days='7';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' days'));
			}*/
			else if($period=='2')
			{
				$days='30';
				$next_due_date = date('Y-m-d', strtotime($old_date. ' +'.$days.' days'));
			}
			/*else if($period=='5')
			{
				$days='365';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' days'));
			}*/

			if(strtotime($now)>=strtotime($next_due_date))
			{
				$flag=1;
			}

		}
		else
		{
			$old_date=date("Y-m-d",strtotime($interest->invest_date));
			$now=date("Y-m-d");
			/*if($period=='1')
			{
				$days='1';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' hour'));
			}
			else */
			if($period=='1')
			{
				$days='1';
				$next_due_date = date('Y-m-d', strtotime($old_date. ' +'.$days.' days'));
			}
			/*else if($period=='3')
			{
				$days='7';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' days'));
			}*/
			else if($period=='2')
			{
				$days='30';
				$next_due_date = date('Y-m-d', strtotime($old_date. ' +'.$days.' days'));
			}
			/*else if($period=='5')
			{
				$days='365';
				$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' days'));
			}*/

			if(strtotime($now)>=strtotime($next_due_date))
			{
				$flag=1;
			}
		}

		
		if($flag=='1')
		{
			$desc="interest Earned For ArbitRage";
			$trans = 'INT'.rand(0,9999999);	
			$insert_data=array("uusersid" => $interest->uusersid,"amount" => $int_amt, "type" => 'interest',"description" => $desc, "date" => $next_due_date,  "transactionid" => $trans, "depositid" => $interest->id, "currency" =>$interest->coin_id);

			$this->db->insert("history",$insert_data);
		}

		if(strtotime($end_date)==strtotime($next_due_date))
		{
			$db->query("update arbit_fund set status='2' where id='".$interest->id."'");
			$return=$find_coin->ret_balance;
			if($return=='1')
			{

				$desc="Relesed ArbitRage Investment";
				$trans = 'REL'.rand(0,9999999);	
				$insert_data=array("uusersid" => $interest->uusersid,"amount" => $int_amt, "type" => 'return_principal',"description" => $desc, "date" => $next_due_date,  "transactionid" => $trans, "depositid" => $interest->id, "currency" =>$interest->coin_id);
				$this->db->insert("history",$insert_data);
			}


		}



	}
}





/*Trade Profit*/
$tra_query=$db->query("select * from trade_history where status = '1'")->getResult();
foreach ($tra_query as $key) 
{
	$condition = "id = '".$key->trade_id."'";  
	$prof = $this->common_model->GetRow('trader',$condition);
	$Profit1=$prof->profit;
	$profit = $key->amount * ($prof->profit / 100);
	$date=date("Y-m-d H:i:s");
	$trans = 'TRA'.rand(0,9999999);	
    if($key->end_date <= $date)
    {   
      
        $tra_profit = array(
		 "uusersid" => $key->usersid,
         "amount"  => $profit,
         "type"   => "trade_profit",
         "date"   => $date,
         "currency" => $key->currency,
         "transactionid" => $trans,
         "depositid" => $key->id,
         "description"	=>"Trade Profit Earned" 
            );
        $query_status = $this->db->insert('history',$tra_profit);
        if($query_status)
        {
				$sts=$db->query("UPDATE  trade_history SET status = '2'  WHERE id = '".$key->id."'")->getResult();
        }

    }
   
}

/*Trade Profit*/



?>