<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class CronController extends Controller
{
    public function cronubai(){
        
        $getid =DB::table('users_history')->where('status', '1')->get();
        
        foreach($getid as $data){
            $o_id = $data->order_id;
            // $o_id = 672164;
            
            
            $post = array(
              'api' 	=> '2019gantidiaivanbangsatbocahtolol',
              'action'  	=> 'status',
              'order_id'	=> $o_id
            );
            $_post = Array();
            
            if (is_array($post)) {
              foreach ($post as $name => $value) {
                $_post[] = $name.'='.urlencode($value);
              }
            }
            
            $ch = curl_init('https://followers-gratis.com/api.php');
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if (is_array($post)) {
              curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
            }
    		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                $result = curl_error($ch);
            }
            curl_close($ch);
            $de = json_decode($result);
            
            
            if(isset($de->info->status)){
                $un_status = $de->info->status;
                
                $con = mysqli_connect('localhost', 'instagr9_panel', 'T;y^54SN@!Sa', 'instagr9_panel');
                
                $query = mysqli_query($con, "UPDATE users_history SET status='$un_status' WHERE order_id='$o_id'");
            
            //         DB::table('users_history')
        			 //   ->where('order_id', $o_id)
        			 //   ->update(['status' => $un_status]);
        			    
        			
        			
        			    
        			echo "Update Status ".$o_id." To ".$un_status;
                
            }else{
                echo "Gagal Update Status";
            }
            
    			    
        }
    }
    
    public function cronubai1(){
        
        $getid =DB::table('users_history')->where('status', 'Process')->get();
        
        foreach($getid as $data){
            $o_id = $data->order_id;
            // $o_id = 672164;
            
            
            $post = array(
              'api' 	=> '2019gantidiaivanbangsatbocahtolol',
              'action'  	=> 'status',
              'order_id'	=> $o_id
            );
            $_post = Array();
            
            if (is_array($post)) {
              foreach ($post as $name => $value) {
                $_post[] = $name.'='.urlencode($value);
              }
            }
            
            $ch = curl_init('https://followers-gratis.com/api.php');
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if (is_array($post)) {
              curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
            }
    		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                $result = curl_error($ch);
            }
            curl_close($ch);
            $de = json_decode($result);
            
            
            if(isset($de->info->status)){
                $un_status = $de->info->status;
                
                $con = mysqli_connect('localhost', 'instagr9_panel', 'T;y^54SN@!Sa', 'instagr9_panel');
                
                $query = mysqli_query($con, "UPDATE users_history SET status='$un_status' WHERE order_id='$o_id'");
            
            //         DB::table('users_history')
        			 //   ->where('order_id', $o_id)
        			 //   ->update(['status' => $un_status]);
        			    
        			
        			
        			    
        			echo "Update Status ".$o_id." To ".$un_status;
                
            }else{
                echo "Gagal Update Status";
            }
            
    			    
        }
    }
    
    public function cronubai2(){
        
        $getid =DB::table('users_history')->where('status', 'Processing')->get();
        
        foreach($getid as $data){
            $o_id = $data->order_id;
            // $o_id = 672164;
            
            
            $post = array(
              'api' 	=> '2019gantidiaivanbangsatbocahtolol',
              'action'  	=> 'status',
              'order_id'	=> $o_id
            );
            $_post = Array();
            
            if (is_array($post)) {
              foreach ($post as $name => $value) {
                $_post[] = $name.'='.urlencode($value);
              }
            }
            
            $ch = curl_init('https://followers-gratis.com/api.php');
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if (is_array($post)) {
              curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
            }
    		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                $result = curl_error($ch);
            }
            curl_close($ch);
            $de = json_decode($result);
            
            
            if(isset($de->info->status)){
                $un_status = $de->info->status;
                
                $con = mysqli_connect('localhost', 'instagr9_panel', 'T;y^54SN@!Sa', 'instagr9_panel');
                
                $query = mysqli_query($con, "UPDATE users_history SET status='$un_status' WHERE order_id='$o_id'");
            
            //         DB::table('users_history')
        			 //   ->where('order_id', $o_id)
        			 //   ->update(['status' => $un_status]);
        			    
        			
        			
        			    
        			echo "Update Status ".$o_id." To ".$un_status;
                
            }else{
                echo "Gagal Update Status";
            }
            
    			    
        }
    }
    
    
    public function jap(){
        set_time_limit(1000);

    	$check_order = DB::table('orders')->whereIn('status', ['', 'pending', 'processing'])->where('provider', 'JAP')->where('poid', '<>', '', 'and')->get();
    	

		if ($check_order == NULL) {
		  die("Order Pending not found.");
		} else {
		  
		  foreach($check_order as $data_order){
		    $o_oid = $data_order->oid;
		    $o_poid = $data_order->poid;
		    $o_provider = $data_order->provider;
			  if ($o_provider == "MANUAL") {
			    echo "Order manual<br />";
			  } else {
			    
			    $data_provider = DB::table('providers')->where('code', $o_provider)->first();
			    
			    $p_apikey = $data_provider->api_key;
			    $p_link = $data_provider->link;
			    
			    if ($o_provider != "MANUAL") {
			      $api_postdata = "key=".$p_apikey."&action=status&order=".$o_poid;
			    } else {
			      die("System error!");
			    }
			    
			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_URL, 'https://justanotherpanel.com/api/v2');
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_postdata);
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			    $chresult = curl_exec($ch);
			    curl_close($ch);
			    $json_result = json_decode($chresult);
			    

			    if ($o_provider == "JAP") {
			          $u_status = $json_result->status;
			          $u_starts = $json_result->start_count;
			          if($u_starts == null || $u_starts == 0){
			              $u_start = 0;
			          }else{
			              $u_start = $json_result->start_count;
			          }
			          $u_remains = $json_result->remains;
			         if ($u_status == "Pending") {
			            $un_status = "pending";
			         } else if ($u_status == "In Progress" OR $u_status == 'Processing') {
			            $un_status = "processing";
			         } else if ($u_status == "Partial") {
			            $un_status = "partial";
			         } else if ($u_status == "Canceled") {
			            $un_status = "error";
			         } else if ($u_status == "Completed") {
			            $un_status = "success";
			         } else {
			             $un_status = "Pending";
			         }
			         
			         $update_order = DB::table('orders')
    			    ->where('poid', $o_poid)
    			    ->update([
    			        'status' => $un_status,
    			        'remains' => $u_remains,
    			        'start_count' => $u_start,
    			        'poid' => $o_poid
    			    ]);
    
    			 //   $update_order = \App\Order::where('poid', $o_poid)->first();
    			 //   $update_order->status = $un_status;
    			 //   $update_order->remains = $u_remains;
    			 //   $update_order->start_count = $u_start;
    			 //   $update_order->poid = $o_poid;
    			 //   $update_order->save();
    			    if ($update_order == TRUE) {
    			      echo "ID Web: $o_oid<br />ID Pusat: $o_poid<br /> Status: $u_status<br /> Remains: $u_remains<br /><br />";
    			    } else {
    			        
    			      echo "Error database.";
    			    }
			    }
			    
			    
			  }
		  }
		}
    }
    
    public function vmp(){
        set_time_limit(1000);

    	$check_order = DB::table('orders')->whereIn('status', ['', 'pending', 'processing'])->where('provider', 'VMP')->where('poid', '<>', '', 'and')->get();
    	

		if ($check_order == NULL) {
		  die("Order Pending not found.");
		} else {
		  
		  foreach($check_order as $data_order){
		    $o_oid = $data_order->oid;
		    $o_poid = $data_order->poid;
		    $o_provider = $data_order->provider;
			  if ($o_provider == "MANUAL") {
			    echo "Order manual<br />";
			  } else {
			    
			    $data_provider = \App\Provider::where('code', $o_provider)->first();
			    
			    $p_apikey = $data_provider->api_key;
			    $p_link = $data_provider->link;
			    
			    if ($o_provider != "MANUAL") {
			      $api_postdata = "api_key=".$p_apikey."&action=status&order_id=".$o_poid;
			    } else {
			      die("System error!");
			    }
			    
			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_URL, 'https://vip-member.id/api/json');
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_postdata);
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			    $chresult = curl_exec($ch);
			    curl_close($ch);
			    $json_result = json_decode($chresult);
			    
			    

			    if ($o_provider == "VMP") {
			          $u_status = $json_result->data->status;
			          $u_starts = $json_result->data->start_count;
			          if($u_starts == null || $u_starts == 0){
			              $u_start = 0;
			          }else{
			              $u_start = $json_result->data->start_count;
			          }
			          $u_remains = $json_result->data->remains_count;
			         if ($u_status == "Pending") {
			            $un_status = "pending";
			         } else if ($u_status == "In Progress" OR $u_status == 'Processing') {
			            $un_status = "processing";
			         } else if ($u_status == "Partial") {
			            $un_status = "partial";
			         } else if ($u_status == "Canceled") {
			            $un_status = "error";
			         } else if ($u_status == "Completed") {
			            $un_status = "success";
			         } else {
			             $un_status = "Pending";
			         }
			    }
			    $update_order = DB::table('orders')
			    ->where('poid', $o_poid)
			    ->update([
			        'status' => $un_status,
			        'remains' => $u_remains,
			        'start_count' => $u_start,
			        'poid' => $o_poid
			    ]);


			    if ($update_order == TRUE) {
			      echo "ID Web: $o_oid<br />ID Pusat: $o_poid<br /> Status: $u_status<br /> Remains: $u_remains<br /><br />";
			    } else {
			        
			      echo "Error database.";
			    }
			  }
		  }
		}
    }

    public function regram(){
        set_time_limit(0);

    	$check_order = \App\Order::whereIn('status', ['', 'pending', 'processing'])->where('provider', 'REGRAM')->where('poid', '<>', '', 'and')->get();
    	

		if ($check_order == NULL) {
		  die("Order Pending not found.");
		} else {
		  
		  foreach($check_order as $data_order){
		      
		      
		    $o_oid = $data_order->oid;
		    $o_poid = $data_order->poid;
		    $o_provider = $data_order->provider;
		    
			  if ($o_provider == "MANUAL") {
			    echo "Order manual<br />";
			  }  else {
			    
			    $data_provider = \App\Provider::where('code', $o_provider)->first();
			    
			    
			    $p_apikey = $data_provider->api_key;
			    $p_link = $data_provider->link;
			    
			    if ($o_provider != "MANUAL") {
			      $api_postdata = "api_key=".$p_apikey."&code=".$o_poid;
			    } else {
			      die("System error!");
			    }
			    
			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_URL, 'https://api.smmpanel.id/order/status');
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_postdata);
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			    $chresult = curl_exec($ch);
			    curl_close($ch);
			    $json_result = json_decode($chresult);
			    
			    

			    if ($o_provider == "REGRAM") {
			          $u_status = $json_result->data->status;
			          $u_start = $json_result->data->start;
			          $u_remains = $json_result->data->remains;
			         if ($u_status == "Pending") {
			            $un_status = "Pending";
			         } else if ($u_status == "Processing") {
			            $un_status = "Processing";
			         } else if ($u_status == "In Progress") {
			            $un_status = "Processing";
			         } else if ($u_status == "Partial") {
			            $un_status = "Partial";
			         } else if ($u_status == "Canceled") {
			            $un_status = "Error";
			         } else if ($u_status == "Completed") {
			            $un_status = "Success";
			         } else {
			             $un_status = "Pending";
			         }
			    }
			    
			    $update_order = DB::table('orders')
			    ->where('poid', $o_poid)
			    ->update([
			        'status' => $un_status,
			        'remains' => $u_remains,
			        'start_count' => $u_start,
			        'poid' => $o_poid
			    ]);
    
			 //   $update_order = \App\Order::where('poid', $o_poid)->first();
			 //   $update_order->status = $un_status;
			 //   $update_order->remains = $u_remains;
			 //   $update_order->start_count = $u_start;
			 //   $update_order->poid = $o_poid;
			 //   $update_order->save();
			    if ($update_order == TRUE) {
			      echo "ID Web: $o_oid<br />ID Pusat: $o_poid<br /> Status: $u_status<br /> Remains: $u_remains<br /><br />";
			    } else {
			      echo "Error database.";
			    }
			  }
		  }
		}
    }

    public function refund(){
        set_time_limit(0);
    	$check_order = DB::table('orders')->whereIn('status', ['error', 'partial'])->where('refund', '0')->get();

		if ($check_order == NULL) {
			die("Order Error or Partial not found.");
		} else {
				foreach($check_order as $data_order){
				    
				$o_oid = $data_order->oid;
				$u_remains = $data_order->remains;
				
				    $priceone = $data_order->price / $data_order->quantity;
				    $refund = $priceone * $u_remains;
				    $buyer = $data_order->user;
				    if($u_remains == 0) {
				        $refund = $data_order->price;
				    }

					$update_user = \App\User::where('username', $buyer)->first();
					if($update_user == NULL){
					    echo "Error database.";
					}else{
					    
		                $update_user->saldo = $update_user->saldo + $refund;
    					$update_user->save();
    
    		    		// $update_order = \App\Order::where('oid', $o_oid)->first();
    		    		// $update_order->refund = '1';
    		    		// $update_order->save();
    		    		
    		    		$update_order = DB::table('orders')
        			    ->where('oid', $o_oid)
        			    ->update([
        			        'refund' => '1',
        			    ]);
    
    		    		if ($update_order == TRUE) {
    		    			echo "Refunded Rp $refund - $o_oid<br />";
    		    		} else {
    		    			echo "Error database.";
    		    		}	    
					}
					
			}
		}

    }
    
    public function mentalis(){
        set_time_limit(1000);

    // 	$check_order = DB::table('orders')->whereIn('status', ['', 'pending', 'processing'])->where('provider', 'JAP')->where('poid', '<>', '', 'and')->get();
    	
$check_order = DB::table('users_history')->whereIn('status', ['', 'pending', 'processing', 'partial'])->get();
		if ($check_order == NULL) {
		  die("Order Pending not found.");
		} else {
		  
		  foreach($check_order as $data_order){
		  //  $o_oid = $data_order->oid;
		  $o_poid = $data_order->order_id;
		  //  $o_poid = $data_order->poid;
		  //  $o_provider = $data_order->provider;
			  
			    
			   
			    
			    $p_apikey = "Ah8Zf93do6Krt0cPOYNMeVE5BXwnUL";
			    $p_link = "https://mentalispanel.id/api/json.php";
			    
			    
			      $api_postdata = "api_key=".$p_apikey."&action=status&id=".$o_poid;
		
			    
			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_URL, $p_link);
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_postdata);
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			    $chresult = curl_exec($ch);
			    curl_close($ch);
			    $json_result = json_decode($chresult);
			    
			    
			    
			    
			    

			    
			          $u_status = $json_result->data->status;
			          $u_starts = $json_result->data->start_count;
			          if($u_starts == null || $u_starts == 0){
			              $u_start = 0;
			          }else{
			              $u_start = $json_result->data->start_count;
			          }
			          $u_remains = $json_result->data->remains;
			         if ($u_status == "Pending") {
			            $un_status = "pending";
			         } else if ($u_status == "In Progress" OR $u_status == 'Processing') {
			            $un_status = "processing";
			         } else if ($u_status == "Partial") {
			            $un_status = "partial";
			         } else if ($u_status == "Error") {
			            $un_status = "error";
			         } else if ($u_status == "Success") {
			            $un_status = "success";
			         } else {
			             $un_status = "Pending";
			         }
			         
			     //    $update_order = DB::table('users')
    			 //   ->where('poid', $o_poid)
    			 //   ->update([
    			 //       'status' => $un_status,
    			 //       'remains' => $u_remains,
    			 //       'start_count' => $u_start,
    			 //       'poid' => $o_poid
    			 //   ]);
    			 
    			 $update_order = DB::table('users_history')
        			    ->where('order_id', $o_poid)
        			    ->update(['status' => $un_status]);
    
    			 //   $update_order = \App\Order::where('poid', $o_poid)->first();
    			 //   $update_order->status = $un_status;
    			 //   $update_order->remains = $u_remains;
    			 //   $update_order->start_count = $u_start;
    			 //   $update_order->poid = $o_poid;
    			 //   $update_order->save();
    			    if ($update_order == TRUE) {
    			      echo "ID Pusat: $o_poid<br /> Status: $u_status<br /> Remains: $u_remains<br /><br />";
    			    } else {
    			        
    			      echo "Error database.";
    			    }
			    
			    
			    
			  
		  }
		}
    }
    
    public function mentalisorder(){
        set_time_limit(1000);

    	$check_order = DB::table('orders')->whereIn('status', ['', 'pending', 'processing'])->where('provider', 'MENTALIS')->where('poid', '<>', '', 'and')->get();
    	
// $check_order = DB::table('users_history')->whereIn('status', ['', 'pending', 'processing', 'partial'])->get();
		if ($check_order == NULL) {
		  die("Order Pending not found.");
		} else {
		  
		  foreach($check_order as $data_order){
		    $o_oid = $data_order->oid;
		  //$o_poid = $data_order->order_id;
		    $o_poid = $data_order->poid;
		    $o_provider = $data_order->provider;
			  
			    
			   
			    
			    $p_apikey = "Ah8Zf93do6Krt0cPOYNMeVE5BXwnUL";
			    $p_link = "https://mentalispanel.id/api/json.php";
			    
			    
			      $api_postdata = "api_key=".$p_apikey."&action=status&id=".$o_poid;
		
			    
			    $ch = curl_init();
			    curl_setopt($ch, CURLOPT_URL, $p_link);
			    curl_setopt($ch, CURLOPT_POST, 1);
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_postdata);
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			    $chresult = curl_exec($ch);
			    curl_close($ch);
			    $json_result = json_decode($chresult);
			    
			    
			    
			    
			    

			    
			          $u_status = $json_result->data->status;
			          $u_starts = $json_result->data->start_count;
			          if($u_starts == null || $u_starts == 0){
			              $u_start = 0;
			          }else{
			              $u_start = $json_result->data->start_count;
			          }
			          $u_remains = $json_result->data->remains;
			         if ($u_status == "Pending") {
			            $un_status = "pending";
			         } else if ($u_status == "In Progress" OR $u_status == 'Processing') {
			            $un_status = "processing";
			         } else if ($u_status == "Partial") {
			            $un_status = "partial";
			         } else if ($u_status == "Error") {
			            $un_status = "error";
			         } else if ($u_status == "Success") {
			            $un_status = "success";
			         } else {
			             $un_status = "Pending";
			         }
			         
			     //    $update_order = DB::table('users')
    			 //   ->where('poid', $o_poid)
    			 //   ->update([
    			 //       'status' => $un_status,
    			 //       'remains' => $u_remains,
    			 //       'start_count' => $u_start,
    			 //       'poid' => $o_poid
    			 //   ]);
    			 
    			 $update_order = DB::table('orders')
    			    ->where('poid', $o_poid)
    			    ->update([
    			        'status' => $un_status,
    			        'remains' => $u_remains,
    			        'start_count' => $u_start,
    			        'poid' => $o_poid
    			    ]);
    			 //   $update_order = \App\Order::where('poid', $o_poid)->first();
    			 //   $update_order->status = $un_status;
    			 //   $update_order->remains = $u_remains;
    			 //   $update_order->start_count = $u_start;
    			 //   $update_order->poid = $o_poid;
    			 //   $update_order->save();
    			    if ($update_order == TRUE) {
    			      echo "ID Pusat: $o_poid<br /> Status: $u_status<br /> Remains: $u_remains<br /><br />";
    			    } else {
    			        
    			      echo "Error database.";
    			    }
			    
			    
			    
			  
		  }
		}
    }
    
}
