<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
		
	   // loading common model
	    $this->load->model('Common_model');
	}

	public function index()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Orders';
		
        // we are loading admin dashboard
        
         if($_SESSION['admin']['vender']==1)
         {
		$this->load->view('master/order/list',$data);
         }
         else
         {
			$this->load->view('master/order/lists',$data);	
         }
	}


public function orders()
{
  
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Orders';
		
        // we are loading admin dashboard
		$this->load->view('master/order/lists',$data);	
	  
}
	public function bill_print()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Bill Print';
		
        // we are loading admin dashboard
		$this->load->view('master/order/bill_print',$data);	
	}


	public function bill_pdf()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Bill PDF';
		
        // we are loading admin dashboard
		$this->load->view('master/order/bill_pdf',$data);	
	}


	public function mini_bill_print()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Bill Print';
		
        // we are loading admin dashboard
		$this->load->view('master/order/mini_bill_print',$data);	
	}

    public function shipping_change_status(){
        
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        if( !isset($_GET['id']) || !isset($_GET['task']) ) {
        	redirect(base_url().'master/order');
        	exit;
        } else {
        	// Check the id is valid or not
        	$statement = $this->db->query("SELECT * FROM tbl_payment WHERE id=?",array($_GET['id']));
        	$total = $statement->num_rows();
        	if( $total == 0 ) {
        		redirect(base_url().'master/order');
        		exit;
        	}
        }
        $payment_id = $_GET['payment_id'];
        $addon=date('Y-m-d H:i:s');
        
         if($_GET['task']=='Arrive')
         {
            $statement = $this->db->query("UPDATE tbl_payment SET shipping_status=?, shipping_date=? WHERE id=?",array($_GET['task'],$addon,$_GET['id']));
         }
         
         if($_GET['task']=='Cancelled')
         {
            $statement = $this->db->query("UPDATE tbl_payment SET shipping_status=?, cancelled_date=? WHERE id=?",array($_GET['task'],$addon,$_GET['id']));
         }
         
        
           
         if($_GET['task']=='Completed')
         {
           
     $query11 = $this->db->where('id',$_GET['id']);
     $query11 = $this->db->where('shipping_status','Delivered');
  $query11 = $this->db->get('tbl_payment');
	if ($query11->num_rows() == 0)  
        {       
            
    $queryj = $this->db->query("SELECT * FROM tbl_payment where id='$_GET[id]' ");
    $rowj = $queryj->row(); 
    $customer_id=$rowj->customer_id;
    $tpv=$rowj->tpv;
    $db_id=$rowj->db_id;
            
     $queryj1 = $this->db->query("SELECT * FROM tbl_customer where cust_id='$customer_id' ");
    $rowj1 = $queryj1->row(); 
    $sps11=$rowj1->uid;       
            

 $queryf8111 = $this->db->query("SELECT * FROM details where uid='$sps11' ");
 $rowf8111 = $queryf8111->row(); 
 $wallet8=$rowf8111->wallet2; 
 $one_sp=$rowf8111->sponsor;
 $one_stssg=$rowf8111->status+0;

$mrp=$tpv;
$inv=$tpv/100*20;
$tamt1=$tpv/100*10;

 $queryf81111 = $this->db->query("SELECT SUM(per) AS tsd FROM details");
 $rowf81111 = $queryf81111->row(); 
 $per=$rowf81111->tsd;
 
 $share=$inv/$per;









  $query11 = $this->db->where('mt',2);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls=$row11->wallet2; 
$tuid=$row11->uid; 
$tamt=$row11->per*$share;

$data158=[
         'wallet2'=>$walls+$tamt
         ]; 
         
     $this->db->where('uid',$tuid);
     $this->db->update('details',$data158);
	 
	 
     
            $datak89=[
            'uid'=>$tuid,
            'note'=>'Share Holder Income',
            'credit'=>$tamt,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak89);



}
}





$query11 = $this->db->where('mt',1);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls1=$row11->wallet2; 
$tuid1=$row11->uid; 
$tamt=$row11->per*$share;


$query111 = $this->db->like('root',$tuid1);
  $query111 = $this->db->get('details');
	if ($query111->num_rows() > 0)  
        {
    

$data158=[
         'wallet2'=>$walls1+$tamt1
         ]; 
         
     $this->db->where('uid',$tuid1);
     $this->db->update('details',$data158);
	 
	 
     
            $datak89=[
            'uid'=>$tuid1,
            'note'=>'Leader Ship Income',
            'credit'=>$tamt1,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak89);

}

}
}



$query11 = $this->db->where('uid',$db_id);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls11=$row11->wallet2; 
$tuid11=$row11->uid; 



    

$data1581=[
         'wallet2'=>$walls11+$tamt1
         ]; 
         
     $this->db->where('uid',$tuid11);
     $this->db->update('details',$data1581);
	 
	 
     
            $datak891=[
            'uid'=>$tuid11,
            'note'=>'Delevry Commission',
            'credit'=>$tamt1,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak891);



}
}





$query11 = $this->db->where('uid','7258079165');
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls11=$row11->wallet2; 
$tuid11=$row11->uid; 



    

$data1581=[
         'wallet2'=>$walls11+$tamt1
         ]; 
         
     $this->db->where('uid',$tuid11);
     $this->db->update('details',$data1581);
	 
	 
     
            $datak891=[
            'uid'=>$tuid11,
            'note'=>'Prabhu Jee',
            'credit'=>$tamt1,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak891);



}
}


/*
if($one_stssg==0 || $one_stssg==1)
{
$data158=[
         'wallet2'=>$wallet8+$tpv
         ]; 
         
     $this->db->where('uid',$sps11);
     $this->db->update('details',$data158);
	 
	 
     
            $datak8=[
            'uid'=>$sps11,
            'note'=>'Self Repurchage Income',
            'credit'=>$tpv,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak8);

}*/

 
 $querya =  $this->db->where('uid',$one_sp);
 $querya =  $this->db->get('details');
 $rowu = $querya->row(); 
 $two_sp=$rowu->sponsor;
 $two_wal=$rowu->wallet2;
  $two_sts=$rowu->status+0;
 

 $querya1 =  $this->db->where('uid',$two_sp);
 $querya1 =  $this->db->get('details');
 $rowu1 = $querya1->row(); 
 $three_sp=$rowu1->sponsor;
 $three_wal=$rowu1->wallet2;
 $three_sts=$rowu1->status+0;
 

 $querya2 =  $this->db->where('uid',$three_sp);
 $querya2 =  $this->db->get('details');
 $rowu2 = $querya2->row(); 
 $four_sp=$rowu2->sponsor;
 $four_wal=$rowu2->wallet2;
 $four_sts=$rowu2->status+0;
   

 
 $querya3 =  $this->db->where('uid',$four_sp);
 $querya3 =  $this->db->get('details');
 $rowu3 = $querya3->row(); 
 $five_sp=$rowu3->sponsor;
 $five_wal=$rowu3->wallet2;
 $five_sts=$rowu3->status+0;
 
 
 
 $querya4 =  $this->db->where('uid',$five_sp);
 $querya4 =  $this->db->get('details');
 $rowu4 = $querya4->row(); 
 $six_sp=$rowu4->sponsor;
 $six_wal=$rowu4->wallet2;
 $six_sts=$rowu4->status+0;
 
 $querya5 =  $this->db->where('uid',$six_sp);
 $querya5 =  $this->db->get('details');
 $rowu5 = $querya5->row(); 
 $seven_sp=$rowu5->sponsor;
 $seven_wal=$rowu5->wallet2;
 $seven_sts=$rowu5->status+0;
 
 $querya6 =  $this->db->where('uid',$seven_sp);
 $querya6 =  $this->db->get('details');
 $rowu6 = $querya6->row(); 
 $eight_sp=$rowu6->sponsor;
 $eight_wal=$rowu6->wallet2;
 $eight_sts=$rowu6->status+0;
 
 
 $querya7 =  $this->db->where('uid',$eight_sp);
 $querya7 =  $this->db->get('details');
 $rowu7 = $querya7->row(); 
 $nine_sp=$rowu7->sponsor;
 $nine_wal=$rowu7->wallet2;
 $nine_sts=$rowu7->status+0;
 
 
 $querya8 =  $this->db->where('uid',$nine_sp);
 $querya8 =  $this->db->get('details');
 $rowu8 = $querya8->row(); 
 $ten_sp=$rowu8->sponsor;
 $ten_wal=$rowu8->wallet2;
 $ten_sts=$rowu8->status+0;
 
 
 $querya9 =  $this->db->where('uid',$ten_sp);
 $querya9 =  $this->db->get('details');
 $rowu9 = $querya9->row(); 
 $ten1_sp=$rowu9->sponsor;
 $ten1_wal=$rowu9->wallet2;
 $ten1_sts=$rowu9->status+0;
 
 $querya10 =  $this->db->where('uid',$ten1_sp);
 $querya10=  $this->db->get('details');
 $rowu10 = $querya10->row(); 
 $ten10_sp=$rowu10->sponsor;
 $ten10_wal=$rowu10->wallet2;
 $ten10_sts=$rowu10->status+0;
 
 
 
 $amt=$mrp/100*20;

if($two_sts==0 || $two_sts==1)
{
 

 $data_sp1=[
         'wallet2'=>$two_wal+$amt
         ]; 
     
     
    $this->db->where('uid',$one_sp);
    $this->db->update('details',$data_sp1);
     
    $data_sps1=[
            'uid'=>$one_sp,
            'note'=>'Repurchage Income Level 1 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
         
     $this->db->insert('passbook',$data_sps1);
  
}	 


 $amt=$mrp/100*10;
if($three_sts==0 || $three_sts==1){
    
	 $data_sp2=[
         'wallet2'=>$three_wal+$amt
         ]; 
     
    $this->db->where('uid',$two_sp);
    $this->db->update('details',$data_sp2);
     
    $data_sps2=[
            'uid'=>$two_sp,
            'note'=>'Repurchage Income Level 2 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
     $this->db->insert('passbook',$data_sps2);
         
}

 $amt=$mrp/100*10;	 
 if($four_sts==0 || $four_sts==1){     	 
	 $data_sp3=[
         'wallet2'=>$four_wal+$amt
         ]; 
    
    $this->db->where('uid',$three_sp);
    $this->db->update('details',$data_sp3);
     
    $data_sps3=[
            'uid'=>$three_sp,
            'note'=>'Repurchage Income Level 3 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
      
     $this->db->insert('passbook',$data_sps3);
    
} 



if($five_sts==0 || $five_sts==1){	 
	  $data_sp4=[
         'wallet2'=>$five_wal+$amt
         ]; 
      
    $this->db->where('uid',$four_sp);
    $this->db->update('details',$data_sp4);
     
    $data_sps4=[
            'uid'=>"$four_sp",
            'note'=>'Repurchage Income Level 4 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
            
     $this->db->insert('passbook',$data_sps4);
           
}


 if($six_sts==0 || $six_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$six_wal+$amt
         ]; 
  
    $this->db->where('uid',$five_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps41=[
            'uid'=>"$five_sp",
            'note'=>'Repurchage Income Level 5 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps41);
           
}


$amt=$mrp/100*6;
if($seven_sts==0 || $seven_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$seven_wal+$amt
         ]; 
   
    $this->db->where('uid',$six_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps42=[
            'uid'=>"$six_sp",
            'note'=>'Repurchage Income Level 6 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps42);
           
}


if($eight_sts==0 || $eight_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$eight_wal+$amt
         ]; 
     
    $this->db->where('uid',$seven_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$seven_sp",
            'note'=>'Repurchage Income Level 7 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}


 if($nine_sts==0 || $nine_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$nine_wal+$amt
         ]; 
      
    $this->db->where('uid',$eight_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$eight_sp",
            'note'=>'Repurchage Income Level 8 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}



 if($ten_sts==0 || $ten_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$ten_wal+$amt
         ]; 
   
    $this->db->where('uid',$nine_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$nine_sp",
            'note'=>'Repurchage Income Level 9 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}

   
            
          
            
            
            
   if($ten1_sts==0 || $ten1_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$ten1_wal+$amt
         ]; 
     
    $this->db->where('uid',$ten_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$ten_sp",
            'note'=>'Repurchage Income Level 10 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}         
            
            
            
         }
         
          $statement = $this->db->query("UPDATE tbl_payment SET shipping_status=?, delivery_date=? WHERE id=?",array('Delivered',$addon,$_GET['id']));
            
         
         
    }  
                    
                $statement1 = $this->db->query("SELECT * FROM tbl_customer WHERE cust_id=?",array($_GET['cust_id']));
                $result1 = $statement1->result_array();                            
                foreach ($result1 as $row1) {
                    $cust_email = $row1['cust_email'];
                    $cust_name = $row1['cust_name'];
                    $cust_phone = $row1['cust_phone'];
                    
                     if($_GET['task']=='Arrive')
         {
             $value="Shipped";
         }
         else
         {
                                     $value = $_GET['task'];
         }
                $to = $cust_email;
                $subject = "Booking Information !!";
                $message = "Dear $cust_name, Your Order ID : $payment_id has been " .$value. " successfully. For any Query Feel Free To Contact Us visit customer care section in app. Thank you. ".$data['base_name']."!!";
                $this->email->from($data['contact_email'], $data['base_name']);
                $this->email->to($to);
                $this->email->cc($data['base_name']);
                $this->email->bcc($data['web_url']);
                $this->email->subject($subject);
                $this->email->message($message);
                $this->email->send();
                 
                 $sender = $data['sms_sender'];
                    $username = $data['sms_user'];
                    $password2 = $data['sms_pass'];
                 $number=$cust_phone;
                 $message=$message;
                  $url="http://buzz.azmarq.com/http-api.php?username=".urlencode($username)."&password=".urlencode($password2)."&senderid=".urlencode($sender)."&route=1&number=".urlencode($number)."&message=".urlencode($message);
                $ch = curl_init();
                   $timeout = 90; // set to zero for no timeout
                   $ch = curl_init($url);
                   curl_setopt ($ch, CURLOPT_URL, $url);
                   curl_setopt ($ch, CURLOPT_HEADER, 0);
                   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                   $res_data = curl_exec($ch);
                   $curl_error = curl_errno($ch);
                   curl_close($ch);
                   $rj = (json_decode($res_data, true));
        }
        $this->session->set_flashdata('success','Order ID : ' .$payment_id. ' has been ' .$value. ' successfully.');
        redirect(base_url().'master/order');
    }
    
    
    
    
      public function shipping_change_status1(){
          $queryj = $this->db->query("SELECT * FROM tbl_payment where id='$_GET[id]' ");
    $rowj = $queryj->row(); 
    $customer_id=$rowj->customer_id;
    $tpv=$rowj->tpv;
    $db_id=$rowj->db_id;
    $codes1=$rowj->dcode;  
    
    
     
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
       
		
		
		
        if( !isset($_GET['id']) || !isset($_GET['task']) ) {
        	redirect(base_url().'master/dashboard');
        	exit;
        } else {
        	// Check the id is valid or not
        	$statement = $this->db->query("SELECT * FROM tbl_payment WHERE id=?",array($_GET['id']));
        	$total = $statement->num_rows();
        	if( $total == 0 ) {
        		redirect(base_url().'master/dashboard');
        		exit;
        	}
        }
        $payment_id = $_GET['payment_id'];
        $addon=date('Y-m-d H:i:s');
        
    
  
        
       if($_GET['codes']==$codes1)
         {    
         if($_GET['task']=='Completed')
         {
          
 
   
   
  $query11 = $this->db->where('id',$_GET['id']);
     $query11 = $this->db->where('shipping_status','Delivered');
  $query11 = $this->db->get('tbl_payment');
	if ($query11->num_rows() == 0)  
        {      
   
   
   
            
     $queryj1 = $this->db->query("SELECT * FROM tbl_customer where cust_id='$customer_id' ");
    $rowj1 = $queryj1->row(); 
    $sps11=$rowj1->uid;       
            

 $queryf8111 = $this->db->query("SELECT * FROM details where uid='$sps11' ");
 $rowf8111 = $queryf8111->row(); 
 $wallet8=$rowf8111->wallet2; 
 $one_sp=$rowf8111->sponsor;
 $one_stssg=$rowf8111->status+0;

$mrp=$tpv;
$inv=$tpv/100*20;
$tamt1=$tpv/100*10;

 $queryf81111 = $this->db->query("SELECT SUM(per) AS tsd FROM details");
 $rowf81111 = $queryf81111->row(); 
 $per=$rowf81111->tsd;
 
 $share=$inv/$per;









  $query11 = $this->db->where('mt',2);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls=$row11->wallet2; 
$tuid=$row11->uid; 
$tamt=$row11->per*$share;

$data158=[
         'wallet2'=>$walls+$tamt
         ]; 
         
     $this->db->where('uid',$tuid);
     $this->db->update('details',$data158);
	 
	 
     
            $datak89=[
            'uid'=>$tuid,
            'note'=>'Share Holder Income',
            'credit'=>$tamt,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak89);



}
}





$query11 = $this->db->where('mt',1);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls1=$row11->wallet2; 
$tuid1=$row11->uid; 
$tamt=$row11->per*$share;


$query111 = $this->db->like('root',$tuid1);
  $query111 = $this->db->get('details');
	if ($query111->num_rows() > 0)  
        {
    

$data158=[
         'wallet2'=>$walls1+$tamt1
         ]; 
         
     $this->db->where('uid',$tuid1);
     $this->db->update('details',$data158);
	 
	 
     
            $datak89=[
            'uid'=>$tuid1,
            'note'=>'Leader Ship Income',
            'credit'=>$tamt1,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak89);

}

}
}



$query11 = $this->db->where('uid',$db_id);
  $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
$walls11=$row11->wallet2; 
$tuid11=$row11->uid; 



    

$data1581=[
         'wallet2'=>$walls11+$tamt1
         ]; 
         
     $this->db->where('uid',$tuid11);
     $this->db->update('details',$data1581);
	 
	 
     
            $datak891=[
            'uid'=>$tuid11,
            'note'=>'Delevry Commission',
            'credit'=>$tamt1,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak891);



}
}




/*if($one_stssg==0 || $one_stssg==1)
{
$data158=[
         'wallet2'=>$wallet8+$tpv
         ]; 
         
     $this->db->where('uid',$sps11);
     $this->db->update('details',$data158);
	 
	 
     
            $datak8=[
            'uid'=>$sps11,
            'note'=>'Self Repurchage Income',
            'credit'=>$tpv,
			'addon'=>$addon
            ];
         
$this->db->insert('passbook',$datak8);

}*/

 
 $querya =  $this->db->where('uid',$one_sp);
 $querya =  $this->db->get('details');
 $rowu = $querya->row(); 
 $two_sp=$rowu->sponsor;
 $two_wal=$rowu->wallet2;
  $two_sts=$rowu->status+0;
 

 $querya1 =  $this->db->where('uid',$two_sp);
 $querya1 =  $this->db->get('details');
 $rowu1 = $querya1->row(); 
 $three_sp=$rowu1->sponsor;
 $three_wal=$rowu1->wallet2;
 $three_sts=$rowu1->status+0;
 

 $querya2 =  $this->db->where('uid',$three_sp);
 $querya2 =  $this->db->get('details');
 $rowu2 = $querya2->row(); 
 $four_sp=$rowu2->sponsor;
 $four_wal=$rowu2->wallet2;
 $four_sts=$rowu2->status+0;
   

 
 $querya3 =  $this->db->where('uid',$four_sp);
 $querya3 =  $this->db->get('details');
 $rowu3 = $querya3->row(); 
 $five_sp=$rowu3->sponsor;
 $five_wal=$rowu3->wallet2;
 $five_sts=$rowu3->status+0;
 
 
 
 $querya4 =  $this->db->where('uid',$five_sp);
 $querya4 =  $this->db->get('details');
 $rowu4 = $querya4->row(); 
 $six_sp=$rowu4->sponsor;
 $six_wal=$rowu4->wallet2;
 $six_sts=$rowu4->status+0;
 
 $querya5 =  $this->db->where('uid',$six_sp);
 $querya5 =  $this->db->get('details');
 $rowu5 = $querya5->row(); 
 $seven_sp=$rowu5->sponsor;
 $seven_wal=$rowu5->wallet2;
 $seven_sts=$rowu5->status+0;
 
 $querya6 =  $this->db->where('uid',$seven_sp);
 $querya6 =  $this->db->get('details');
 $rowu6 = $querya6->row(); 
 $eight_sp=$rowu6->sponsor;
 $eight_wal=$rowu6->wallet2;
 $eight_sts=$rowu6->status+0;
 
 
 $querya7 =  $this->db->where('uid',$eight_sp);
 $querya7 =  $this->db->get('details');
 $rowu7 = $querya7->row(); 
 $nine_sp=$rowu7->sponsor;
 $nine_wal=$rowu7->wallet2;
 $nine_sts=$rowu7->status+0;
 
 
 $querya8 =  $this->db->where('uid',$nine_sp);
 $querya8 =  $this->db->get('details');
 $rowu8 = $querya8->row(); 
 $ten_sp=$rowu8->sponsor;
 $ten_wal=$rowu8->wallet2;
 $ten_sts=$rowu8->status+0;
 
 
 $querya9 =  $this->db->where('uid',$ten_sp);
 $querya9 =  $this->db->get('details');
 $rowu9 = $querya9->row(); 
 $ten1_sp=$rowu9->sponsor;
 $ten1_wal=$rowu9->wallet2;
 $ten1_sts=$rowu9->status+0;
 
 $querya10 =  $this->db->where('uid',$ten1_sp);
 $querya10=  $this->db->get('details');
 $rowu10 = $querya10->row(); 
 $ten10_sp=$rowu10->sponsor;
 $ten10_wal=$rowu10->wallet2;
 $ten10_sts=$rowu10->status+0;
 
 
 
 $amt=$mrp/100*20;

if($two_sts==0 || $two_sts==1)
{
 

 $data_sp1=[
         'wallet2'=>$two_wal+$amt
         ]; 
     
     	   
    $this->db->where('uid',$one_sp);
    $this->db->update('details',$data_sp1);
     
    $data_sps1=[
            'uid'=>$one_sp,
            'note'=>'Repurchage Income Level 1 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
         
     $this->db->insert('passbook',$data_sps1);
  
}	 


 $amt=$mrp/100*10;
if($three_sts==0 || $three_sts==1){
    
	 $data_sp2=[
         'wallet2'=>$three_wal+$amt
         ]; 
    	    
    $this->db->where('uid',$two_sp);
    $this->db->update('details',$data_sp2);
     
    $data_sps2=[
            'uid'=>$two_sp,
            'note'=>'Repurchage Income Level 2 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
     $this->db->insert('passbook',$data_sps2);
         
}

 $amt=$mrp/100*10;	 
 if($four_sts==0 || $four_sts==1){     	 
	 $data_sp3=[
         'wallet2'=>$four_wal+$amt
         ]; 
    	    
    $this->db->where('uid',$three_sp);
    $this->db->update('details',$data_sp3);
     
    $data_sps3=[
            'uid'=>$three_sp,
            'note'=>'Repurchage Income Level 3 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
      
     $this->db->insert('passbook',$data_sps3);
    
} 



if($five_sts==0 || $five_sts==1){	 
	  $data_sp4=[
         'wallet2'=>$five_wal+$amt
         ]; 
     	   
    $this->db->where('uid',$four_sp);
    $this->db->update('details',$data_sp4);
     
    $data_sps4=[
            'uid'=>"$four_sp",
            'note'=>'Repurchage Income Level 4 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
            
     $this->db->insert('passbook',$data_sps4);
           
}


 if($six_sts==0 || $six_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$six_wal+$amt
         ]; 
       
    $this->db->where('uid',$five_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps41=[
            'uid'=>"$five_sp",
            'note'=>'Repurchage Income Level 5 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps41);
           
}


$amt=$mrp/100*6;
if($seven_sts==0 || $seven_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$seven_wal+$amt
         ]; 
       
    $this->db->where('uid',$six_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps42=[
            'uid'=>"$six_sp",
            'note'=>'Repurchage Income Level 6 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps42);
           
}


if($eight_sts==0 || $eight_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$eight_wal+$amt
         ]; 
       
    $this->db->where('uid',$seven_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$seven_sp",
            'note'=>'Repurchage Income Level 7 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}


 if($nine_sts==0 || $nine_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$nine_wal+$amt
         ]; 
       
    $this->db->where('uid',$eight_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$eight_sp",
            'note'=>'Repurchage Income Level 8 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}



 if($ten_sts==0 || $ten_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$ten_wal+$amt
         ]; 
       
    $this->db->where('uid',$nine_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$nine_sp",
            'note'=>'Repurchage Income Level 9 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}

   
            
          
            
            
            
   if($ten1_sts==0 || $ten1_sts==1){	 
	  $data_sp41=[
         'wallet2'=>$ten1_wal+$amt
         ]; 
       
    $this->db->where('uid',$ten_sp);
    $this->db->update('details',$data_sp41);
     
    $data_sps43=[
            'uid'=>"$ten_sp",
            'note'=>'Repurchage Income Level 10 '.$sps11,
            'credit'=>$amt,
			'addon'=>$addon
            ];
            
           
     $this->db->insert('passbook',$data_sps43);
           
}         
            
            
            
         }
         
            $statement = $this->db->query("UPDATE tbl_payment SET shipping_status=?, delivery_date=? WHERE id=?",array('Delivered',$addon,$_GET['id']));
            
            
           
         }   
                    
                $statement1 = $this->db->query("SELECT * FROM tbl_customer WHERE cust_id=?",array($_GET['cust_id']));
                $result1 = $statement1->result_array();                            
                foreach ($result1 as $row1) {
                    $cust_email = $row1['cust_email'];
                    $cust_name = $row1['cust_name'];
                    $cust_phone = $row1['cust_phone'];
                    
                     if($_GET['task']=='Arrive')
         {
             $value="Shipped";
         }
         else
         {
                                     $value = $_GET['task'];
         }
                $to = $cust_email;
                $subject = "Booking Information !!";
                $message = "Dear $cust_name, Your Order ID : $payment_id has been " .$value. " successfully. For any Query Feel Free To Contact Us visit customer care section in app. Thank you. ".$data['base_name']."!!";
                $this->email->from($data['contact_email'], $data['base_name']);
                $this->email->to($to);
                $this->email->cc($data['base_name']);
                $this->email->bcc($data['web_url']);
                $this->email->subject($subject);
                $this->email->message($message);
                $this->email->send();
                 
                 $sender = $data['sms_sender'];
                    $username = $data['sms_user'];
                    $password2 = $data['sms_pass'];
                 $number=$cust_phone;
                 $message=$message;
                  $url="http://buzz.azmarq.com/http-api.php?username=".urlencode($username)."&password=".urlencode($password2)."&senderid=".urlencode($sender)."&route=1&number=".urlencode($number)."&message=".urlencode($message);
                $ch = curl_init();
                   $timeout = 90; // set to zero for no timeout
                   $ch = curl_init($url);
                   curl_setopt ($ch, CURLOPT_URL, $url);
                   curl_setopt ($ch, CURLOPT_HEADER, 0);
                   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                   $res_data = curl_exec($ch);
                   $curl_error = curl_errno($ch);
                   curl_close($ch);
                   $rj = (json_decode($res_data, true));
        }
        
       
        $this->session->set_flashdata('success','Order ID : ' .$payment_id. ' has been ' .$value. ' successfully.');
        
         }
       else
       {
             $this->session->set_flashdata('error','Invalid Delevery Code.');
       }
       
       
       
       
        redirect(base_url().'master/dashboard');
    }
    
    public function order_change_status(){
        /************************************/
        // loading common model
        $this
            ->load
            ->model('Common_model');
            
        /************************************/
        $column['id'] = '1';
        $table = 'tbl_settings';
        
        /************************************/
        // we are getting results from common modal
        $data = $this->Common_model->Result($column,$table);
        /************************************/
        
        if( !isset($_GET['id']) || !isset($_GET['task']) ) {
            redirect(base_url().'master/order');
            exit;
        } else {
            // Check the id is valid or not
            $statement = $this->db->query("SELECT * FROM tbl_payment WHERE id=?",array($_GET['id']));
            $total = $statement->num_rows();
            if( $total == 0 ) {
                redirect(base_url().'master/order');
                exit;
            }
        }
        $statement = $this->db->query("UPDATE tbl_payment SET payment_status=? WHERE id=?",array($_GET['task'],$_GET['id']));
        
        $this->session->set_flashdata('success','Shipping status change to "'.$_GET['task'].'" successfully.');
		redirect(base_url().'master/order');
    }

}

?>