<?php
     use App\Models\CommonModel;
     $this->db = db_connect();
      date_default_timezone_set("Asia/Kolkata");
  
    // get trading settings
    $tradSets = $this->common_model->GetResults('adminsettings','');
    $tradSet = $tradSets[0];

		    // get  1 hour diferent
			$now = ''; 
			$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' +1 hour' ) );
			$curTim = date( 'Y-m-d H:00:00');


	if(strtotime($next_hour) == strtotime($curTim) )
	{
   
		if($tradSet->deduction_type=="Trading")
		{

	// get trading time
	$trdhor = 1; // add 1 hour
	$now = ''; 
	$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' - '.$trdhor.'hour' ) );
	$curTim = date( 'Y-m-d H:00:00');

	// get trading history
	$condition =  "type='trading' AND date BETWEEN '".$next_hour."' and '".$curTim."' GROUP by uusersid";
	$getHis_count = $this->common_model->RecordCount('history',$condition);
	$getHis = $this->common_model->GetResults('history',$condition);

        if($getHis_count)
        {

        for ($i=0; $i < $getHis_count ; $i++) { 

		$condition =  "type='trading' AND date BETWEEN '".$next_hour."' and '".$curTim."' and uusersid ='".$getHis[$i]->uusersid."' ";
		$getHis_counts = $this->common_model->RecordCount('history',$condition);

		if($getHis_counts==$tradSet->no_of_trading)
		{
		$totTrad = 1;
		}
		else
		{
		$totTrad = $getHis_counts;
		}
       
       
        $Tradfee = $totTrad/$tradSet->amount * $getHis_counts;


		$trading_fee = array(

		"amount" => $Tradfee,
		"uusersid" => $getHis[$i]->uusersid,
		"type" => "trading_fee",
		"date" => date('Y-m-d H:i:s'),

		);

	    $traFee = $this->db->table('history')->insert($trading_fee);
       
       	}
     
        }


		}
		else
		{
	       
	        // get trading time
	        $trdhor = $tradSet->no_of_hours;
			$now = ''; 
			$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' -'.$trdhor.'hour' ) );
			$curTim = date( 'Y-m-d H:00:00');
			

// get trading history
$condition =  "type='trading' AND date BETWEEN '".$next_hour."' and '".$curTim."' and substatus!='1' GROUP by uusersid ";

$getHis = $this->common_model->GetResults('history',$condition);
$getHis_count = $this->common_model->RecordCount('history',$condition);


        if($getHis)
        {

     // fetch records in user
     for ($i=0; $i < $getHis_count ; $i++) { 
   
    $condition =  "type='trading' AND date BETWEEN '".$next_hour."' and '".$curTim."' and substatus!='1' AND uusersid = '".$getHis[$i]->uusersid."'";
    $getHis_counts = $this->common_model->RecordCount('history',$condition);
    $getHiss= $this->common_model->GetResults('history',$condition);


	// update substatus each history    
		for ($j=0; $j < $getHis_counts ; $j++) { 

		$subStus = array(
		"substatus" => 1
		);

		$query_status = $this->db->table('history')->where('historyid', $getHiss[$j]->historyid)->update($subStus);

		}
	//end update

        //insert fee
		    $Tradfee = $tradSet->amount;
			$trading_fee = array(

			"amount" => $Tradfee,
			"uusersid" => $getHis[$i]->uusersid,
			"type" => "trading_fee",
			"date" => date('Y-m-d H:i:s'),

			);
			$traFee = $this->db->table('history')->insert($trading_fee);
		// end

	}
    // end


	}

    }

	}



?>