<?php

if(isset($coupon) && isset($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'];
                
                $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'];
                        $coupon_code = $row5['code'];
                        $discount_percentage = $row5['percentage'];
                    }
                }
                else{
                $total='';
                }
            }
            else{
                $total='';
            }
        }

}


if(isset($total)){
    // response for user
    $myObj = new \stdClass();
    $c = $title.' '.$discount_percentage.'% Discount For Coupon Code -'.$coupon_code;  
    $_SESSION['discount_percentage2']=$discount_percentage;
    $myObj->succ = 1;
    $myObj->error = 0;
    $myObj->title = $title;
    $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;
}

     