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

class Functions extends CI_Controller {
    
    public function size() {
        /************************************/
        // loading common model
        $this->load->model('Common_model');
        
        /************************************/
        if(is_numeric($_POST['id'])){
            $id = $_POST['id'];
            $column['p_id'] = $id;
            $table = 'tbl_product_size';
            
            /************************************/
            // we are getting results from common modal
            $data['product_size'] = $this->Common_model->ResultsWhere2($column,$table);
            
            /************************************/
            // we are loading admin dashboard
            $this->load->view('website/size',$data);	
        }
        else
        {
            echo 'No Result';
        }
    }
    
    
    
public function coupon_otp()
{
    
    $otp = $_POST['otp'];
   
   if($_SESSION['coupon_otp']==$otp) 
    {
    $myObj = new \stdClass();    
    $myObj->succ = 1;
    $_SESSION['coupon_otp_verify']=1;
    }
    else
    {
    $myObj = new \stdClass();    
    $myObj->succ = 0;
    $_SESSION['coupon_otp_verify']=0;
    }
    $myJSON = json_encode($myObj);
    echo $myJSON;
}
    
    
    
    
    public function coupon() {
        /************************************/
        // loading common model
        $this->load->model('Common_model');
       
        /************************************/
        if($_POST['coupon']){
            $coupon = $_POST['coupon'];
            $final_total = $_POST['final_total'];
            $data['coupon'] = $coupon;
            $data['final_total'] = $final_total;
            
            /************************************/
          
          
           $statement5 = $this->db->query("SELECT * FROM tbl_coupon WHERE status = ? AND code=? ORDER BY coupon_id DESC",array(1,$coupon));
    $result5 = $statement5->result_array();
        foreach ($result5 as $row5) {
            if($final_total>=$row5['min_amt']){
            $end_date = date('Ymd', strtotime($row5['end_date']));
            $date=date('Ymd');
                if($end_date>=$date){
                $coupon_id = $row5['coupon_id'];
                $uid = $row5['uid'];
                $queryf8111 = $this->db->query("SELECT * FROM tbl_customer where uid='$uid' ");
                $rowf8111 = $queryf8111->row(); 
                $number=$rowf8111->cust_phone;
     
                $statement5 = $this->db->query("SELECT * FROM applied_coupon WHERE coupon_id=? AND user_id=?",array($coupon_id,$_SESSION['customer']['cust_id']));
                    if($statement5->num_rows()>0) {
                    $total='';
                    }
                    else{
                        $total=1;
                        $title = $row5['title'];
                        $type = $row5['type'];
                        $coupon_code = $row5['code'];
                        $discount_percentage = $row5['percentage'];
                    }
                }
                else{
                $total='';
                }
            }
            else{
                $total='';
            }
        }
        
        
        
        
        }
        else
        {
            echo 'No Result';
        }
        
        
        
        
        
    if($total==1){
    // response for user
    $myObj = new \stdClass();
    if($type==1){
    $_SESSION['coupon_otp']=rand(111111,999999);
    $otp=$_SESSION['coupon_otp'];
    $web='www.tryitindia.com';
    
                  $url = "https://2factor.in/API/R1/?module=TRANS_SMS&apikey=2a3157c9-ed20-11eb-8089-0200cd936042&to=".urlencode($number)."&from=TRYITT&templatename=tryit+otp&var1=".urlencode($otp)."&var2=".urlencode($web)."";

                    $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));
    
    $c = $title.' '.$discount_percentage.'Rs Discount For Coupon Code : '.$coupon_code; 
    }
       if($type==0){
         $c = $title.' '.$discount_percentage.'% Discount For Coupon Code : '.$coupon_code; 
    }
    
    $_SESSION['discount_percentage2']=$discount_percentage;
    $_SESSION['discount_type']=$type;
    
    $myObj->succ = 1;
    $myObj->error = 0;
    $myObj->title = $title;
    $myObj->type = $type;
    $myObj->coupon_code = $coupon_code;
    $myObj->coupon_id = $coupon_id;
    $myObj->message = $c;
    if(isset($_SESSION['discount_percentage2'])){
        $myObj->discount_percentage = $_SESSION['discount_percentage2'];
    }
    $myJSON = json_encode($myObj);
    echo $myJSON;
}
else{
    // response for user
    $myObj = new \stdClass();
    unset($_SESSION['discount_percentage']);
    $myObj->succ = 0;
    $myObj->error = 1;
    $myJSON = json_encode($myObj);
    echo $myJSON;
}
    }
    

}
