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

	foreach ($interest as $interest) 
	{
		$coin=$interest->coin_id;
		$find_coin=$this->db->query("select * from coin_info where coid='".$coin."'")->row();
		$profit=$find_coin->int_rate;
		$amount=$interest->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");
		if(strtotime($end_date)>strtotime($now)) /*if start*/
		{	
				$flag=0;
				$row_chk=$this->db->query("select * from history where description LIKE '%arbit%' and type='interest' and depositid='".$interest->id."' order by date desc limit 1")->row();
				if($row_chk)
				{
					$date=date("Y-m-d H:i:s",strtotime($row_chk->date));
					$now=date("Y-m-d H:i:s");
					$old_date = $date;
					if($period=='1')
					{
						$days='1';
						$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' hour'));
					}
					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=='4')
					{
						$days='30';
						$next_due_date = date('Y-m-d H:i:s', 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 H:i:s",strtotime($interest->invest_date));
					$now=date("Y-m-d H:i:s");

					if($period=='1')
					{
						$days='1';
						$next_due_date = date('Y-m-d H:i:s', strtotime($old_date. ' +'.$days.' hour'));
					}
					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=='4')
					{
						$days='30';
						$next_due_date = date('Y-m-d H:i:s', 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);
				$this->db->insert("history",$insert_data);


			}


		}/*if end*/
		else
		{

		}
	}
}
?>