<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";





if($_POST['type']=="get_quantity"){
    
    $product_name_id = $_POST['product_name_id'];
    $where="";
    
        $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE  id=?");
		$statement->execute(array($product_name_id));
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		if($result){
		    $response['quantity']=$result[0]['quantity'];
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Quantity Fetched Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Failed';
		}
		
		echo json_encode($response);
 
}


if($_POST['type']=="get_size"){
    
    $product_name_id = $_POST['product_name_id'];
    $where="";
    
        $statement = $pdo->prepare("SELECT s.*,tbl_products_name.name as product_name FROM `tbl_size` s LEFT JOIN tbl_products_name ON tbl_products_name.id=s.product_name_id WHERE s.status=1 and s.product_name_id=?");
		$statement->execute(array($product_name_id));
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		if($result){
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Size Fetched Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Failed';
		}
		
		echo json_encode($response);
 
}


if($_POST['type']=="get_product_name"){
    
    $product_ids = $_POST['product_ids'];
    $where="";
    
        $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE status=1 and product_ids=?");
		$statement->execute(array($product_ids));
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		if($result){
		    $response['count']=$totalrow;
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Products Name Fetched Successfully";
		}else{
		    $response['count']=$totalrow;
		    $response['error']=true;
		    $response['message']='Get Purity Failed';
		}
		
		echo json_encode($response);
 
}


if($_POST['type']=="get_purity"){
    
    $product_ids = $_POST['product_ids'];
    $where="";
    
        if($product_ids == 1){
             $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE  product_ids=? AND value=?");
    		$statement->execute(array($product_ids,'22'));
    		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
    		$totalrow = $statement->rowCount();
        }else{
            $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE  product_ids=?");
    		$statement->execute(array($product_ids));
    		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
    		$totalrow = $statement->rowCount();
        }
        
       
		
		if($result){
		    
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Purity Fetched Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Get Purity Failed';
		}
		
		echo json_encode($response);
 
}

if($_POST['type']=="customer_scheme_payment_month"){
    
    $customer_id = $_POST['customer_id'];
    $scheme_id = $_POST['scheme_id'];
    $payment_month = $_POST['payment_month'];
    $payment_year = date("Y");
    $accounts = 3;
    
        $statement = $pdo->prepare("SELECT * FROM `tbl_payments` WHERE  accounts=? and scheme_id=? and customer_id=? and month=? and year=?");
		$statement->execute(array($accounts, $scheme_id, $customer_id, $payment_month, $payment_year));
		
		error_log("MUS: SELECT * FROM `tbl_payments` WHERE  accounts=$accounts and scheme_id=$scheme_id and customer_id=$customer_id and month=$payment_month and year=$payment_year");
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		if($totalrow>0){
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Customer This Month Already Paid";
		}else{
		    $response['error']=true;
		    $response['message']='Customer This Month Not Paid';
		}
		
		echo json_encode($response);
 
}


if($_POST['type']=="get_today_rates"){
    
    $product_ids = $_POST['product_ids'];
    $purity_ids = $_POST['purity_ids'];
    $where="";
    
        if(!empty($product_ids) && !empty($purity_ids)){
            $statement = $pdo->prepare("SELECT * FROM `tbl_prices` WHERE  purity_id=? and product_id=?");
		    $statement->execute(array($purity_ids, $product_ids));
        }else{
            $statement = $pdo->prepare("SELECT * FROM `tbl_prices` WHERE product_id=?");
		    $statement->execute(array($product_ids));
        }
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		if($result){
		    $response['today_rate'] = $result[0]['amount'];
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Today Rate Fetched Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Get Today Price Failed';
		}
		
		echo json_encode($response);
 
}

if($_POST['type']=="get_product_rates"){
    
    $product_ids = $_POST['product_ids'];
    $purity_ids = $_POST['purity_ids'];
    $date = $_POST['date'];
    $where="";
    
        $statement = $pdo->prepare("SELECT * FROM `tbl_prices_reports` WHERE  purity_id=? and product_id=? and date=?");
		$statement->execute(array($purity_ids, $product_ids, $date));
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		if($totalrow>0){
		    $response['today_rate'] = $result[0]['amount'];
		}else{
		    $response['today_rate'] = 0;
		}
		if($result){
		    
		    $response['data']=$result;
		    $response['error']=false;
		    $response['message']="Today Rate Fetched Successfully";
		}else{
		    $response['today_rate'] = 0;
		    $response['error']=true;
		    $response['message']='Get Today Price Failed';
		}
		
		echo json_encode($response);
 
}

if($_POST['field_type']=="get_scheme_sender_details"){
    
    
    $send_type = $_POST['send_type'];
    $row_id = $_POST['row_id'];
    
    if($send_type == '1'){ // customer details
    
    
        $statement = $pdo->prepare("SELECT c.customer_name,c.id AS 'c_id',c.mobile_no 
                                    FROM `tbl_customer_scheme` AS cs
                                    LEFT JOIN tbl_customers AS c ON c.id = cs.customer_id
                                    WHERE cs.id=?");
		$statement->execute(array($row_id));
		$data = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		
		$opt = '<option value="">Select Sender Details</option>';
		$opt .= "<option value=".$data[0]['c_id'].">".$data[0]['customer_name'] ." - ". $data[0]['mobile_no']."</option>";
	    
        if($totalrow>0){
		    $response['data']= $opt;
		    $response['error']=false;
		    $response['message']="Customer Details Get Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Customer Details Not Found!'; 
		}
        
        
    }elseif($send_type == '2'){ // sales person details  
    
    
        $statement = $pdo->prepare("SELECT u.id as 'u_id', u.name AS 'username', u.mobile AS 'user_number' FROM `tbl_users` AS u");
		$statement->execute();
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		$opt = '<option value="">Select Sender Details</option>';
		foreach($datas as $data){
		   $opt .= "<option value=".$data['u_id'].">".$data['username'] ." - ". $data['user_number']."</option>";  
		}
		
	    
        if($totalrow>0){
		    $response['data']= $opt;
		    $response['error']=false;
		    $response['message']="User Details Get Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='User Details Not Found!'; 
		}
		
        
    }else{
        
        $response['error']=true;
		$response['message']='Something Went Wrong';
        
    }
    
    echo json_encode($response);
    
}


if($_POST['field_type']=="scheme_send_otp"){
    error_reporting(E_ALL);
    
    $pdo->beginTransaction(); 
    
    $send_type = $_POST['send_type'];
    $sender_id = $_POST['sender_id'];
    $current_date = date('Y-m-d h:i:s');
    
    // function sendOTP(){
        
    // }
    
    if($send_type == '1'){
        
        $statement = $pdo->prepare("SELECT c.*  FROM `tbl_customers` AS c WHERE c.id=?");
		$statement->execute(array($sender_id));
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		$customer_name = $datas[0]['customer_name'];
		$mobile_no = isset($datas[0]['mobile_no']) ? $datas[0]['mobile_no'] : $datas[0]['alter_mobile_no'] ;
        
        if($totalrow>0){
            
        
            try{
                
                $otp = rand(999999,100000);
                
                $insert = $pdo->prepare("INSERT INTO otp_table (`username`, `sender_type`, `otp`, `otp_send_at`, `otp_type`, `created_at`)
                                        VALUES (?,?,?,?,?,?) ");
                                        
                $insert_result = $insert->execute(array($sender_id, $send_type, $otp, $current_date, 'Scheme-Completed-OTP', $current_date));
                
                if($insert_result){
                    $pdo->commit();
                    $response['data']= $otp;
        		    $response['error']=false;
        		    $response['message']="Customer OTP Send Successfully";
                    
                }else{
                    $response['error']=true;
		            $response['message']='Customer OTP Send Failed..';  
                }
                
            }catch(PDOException $e){
                $pdo->rollBack();
                $response['error']=true;
                $response['message']="$e";
            } 
		    
		    
		}else{
		    $response['error']=true;
		    $response['message']='Customer Details Not Found!'; 
		}
        
        
    }elseif($sender_id == '2'){
        
        
        $statement = $pdo->prepare("SELECT u.* FROM `tbl_users` AS u WHERE u.id=?");
		$statement->execute(array($sender_id));
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
			$user_name = $datas[0]['name'];
			$mobile = isset($datas[0]['mobile']) ? $datas[0]['mobile'] : $datas[0]['alter_mobile'] ;
		
		if($totalrow > 0){
		        
		    try{
                
                $otp = rand(999999,100000);
                
                $insert = $pdo->prepare("INSERT INTO otp_table (`username`, `sender_type`, `otp`, `otp_send_at`, `otp_type`, `created_at`)
                                        VALUES (?,?,?,?,?,?) ");
                                        
                $insert_result = $insert->execute(array($sender_id, $send_type, $otp, $current_date, 'Scheme-Completed-OTP', $current_date));
                
                if($insert_result){
                    $pdo->commit();
                    $response['data']= $otp;
        		    $response['error']=false;
        		    $response['message']="User OTP Send Successfully";
                    
                }else{
                    $response['error']=true;
		            $response['message']='User OTP Send Failed..';  
                }
                
            }catch(PDOException $e){
                $pdo->rollBack();
                $response['message']="$e";
                $response['error']=true;
            } 
		        
		    
		}else{
		 $response['error']=true;
		    $response['message']='User Details Not Found!';        
		}
        
    }else{
	    $response['error']=true;
	    $response['message']='OTP Send Failed';
	}
		
	echo json_encode($response);
    
    
    
}


if($_POST['field_type']=="scheme_submit_otp"){
    
    $send_type = $_POST['send_type'];
    $sender_id = $_POST['sender_id'];
    $otp = $_POST['otp'];
    $otp_type = 'Scheme-Completed-OTP';
    $current_date = date('Y-m-d h:i:s');
    
    $check_otp = $pdo->prepare("SELECT * FROM `otp_table` WHERE username=? and sender_type=? and otp_type=? AND is_used=? ORDER BY id DESC LIMIT 1");
	$check_otp->execute(array($sender_id, $send_type, $otp_type, 0));
	$check_data = $check_otp->fetchAll(PDO::FETCH_ASSOC);
	$otprow = $check_otp->rowCount();
	
	if($otprow > 0){
	    $db_otp = $check_data[0]['otp'];
	    
	    if($db_otp == $otp){
	        
	        $update = $pdo->prepare("UPDATE otp_table SET is_used=?, verified_date=? WHERE username=? and sender_type=? and otp_type=? AND is_used=? ORDER BY id DESC LIMIT 1 ");
                                        
            $update_result = $update->execute(array(1, $current_date, $sender_id, $send_type, $otp_type, 0));
            
            if($update_result){

    		    $response['error']=false;
    		    $response['message']="OTP Verification Successfully Completed.";
            }else{
               $response['error']=true;
	           $response['message']='OTP Verification Failed';    
            }
	        
	        
	    }else{
	        $response['error']=true;
	        $response['message']='OTP MisMatched';   
	    }
	    
	    
	}else{
	    $response['error']=true;
	    $response['message']='OTP Check Failed';
	}
	
    echo json_encode($response);
    
}


if($_POST['field_type']=="get_repair_sender_details"){
    
    
    $send_type = $_POST['send_type'];
    $row_id = $_POST['row_id'];
    
    if($send_type == '1'){ // customer details
    
    
        $statement = $pdo->prepare("SELECT c.customer_name,c.id AS 'c_id',c.mobile_no 
                                    FROM `tbl_repair_voucher` AS rp
                                    LEFT JOIN tbl_customers AS c ON c.id = rp.customer_id
                                    WHERE rp.id=?");
		$statement->execute(array($row_id));
		$data = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		
		$opt = '<option value="">Select Sender Details</option>';
		$opt .= "<option value=".$data[0]['c_id'].">".$data[0]['customer_name'] ." - ". $data[0]['mobile_no']."</option>";
	    
        if($totalrow>0){
		    $response['data']= $opt;
		    $response['error']=false;
		    $response['message']="Customer Details Get Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Customer Details Not Found!'; 
		}
        
        
    }elseif($send_type == '2'){ // sales person details  
    
    
        $statement = $pdo->prepare("SELECT u.id as 'u_id', u.name AS 'username', u.mobile AS 'user_number' FROM `tbl_users` AS u");
		$statement->execute();
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		$opt = '<option value="">Select Sender Details</option>';
		foreach($datas as $data){
		   $opt .= "<option value=".$data['u_id'].">".$data['username'] ." - ". $data['user_number']."</option>";  
		}
		
	    
        if($totalrow>0){
		    $response['data']= $opt;
		    $response['error']=false;
		    $response['message']="User Details Get Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='User Details Not Found!'; 
		}
		
        
    }else{
        
        $response['error']=true;
		$response['message']='Something Went Wrong';
        
    }
    
    echo json_encode($response);
    
}


if($_POST['field_type']=="repair_send_otp"){
    error_reporting(E_ALL);
    
    $pdo->beginTransaction(); 
    
    $send_type = $_POST['send_type'];
    $sender_id = $_POST['sender_id'];
    $current_date = date('Y-m-d h:i:s');
    
    // function sendOTP(){
        
    // }
    
    if($send_type == '1'){
        
        $statement = $pdo->prepare("SELECT c.*  FROM `tbl_customers` AS c WHERE c.id=?");
		$statement->execute(array($sender_id));
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
		$customer_name = $datas[0]['customer_name'];
		$mobile_no = isset($datas[0]['mobile_no']) ? $datas[0]['mobile_no'] : $datas[0]['alter_mobile_no'] ;
        
        if($totalrow>0){
            
        
            try{
                
                $otp = rand(999999,100000);
                
                $insert = $pdo->prepare("INSERT INTO otp_table (`username`, `sender_type`, `otp`, `otp_send_at`, `otp_type`, `created_at`)
                                        VALUES (?,?,?,?,?,?) ");
                                        
                $insert_result = $insert->execute(array($sender_id, $send_type, $otp, $current_date, 'Repair-Completed-OTP', $current_date));
                
                if($insert_result){
                    $pdo->commit();
                    $response['data']= $otp;
        		    $response['error']=false;
        		    $response['message']="Customer OTP Send Successfully";
                    
                }else{
                    $response['error']=true;
		            $response['message']='Customer OTP Send Failed..';  
                }
                
            }catch(PDOException $e){
                $pdo->rollBack();
                $response['error']=true;
                $response['message']="$e";
            } 
		    
		    
		}else{
		    $response['error']=true;
		    $response['message']='Customer Details Not Found!'; 
		}
        
        
    }elseif($sender_id == '2'){
        
        
        $statement = $pdo->prepare("SELECT u.* FROM `tbl_users` AS u WHERE u.id=?");
		$statement->execute(array($sender_id));
		$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		
			$user_name = $datas[0]['name'];
			$mobile = isset($datas[0]['mobile']) ? $datas[0]['mobile'] : $datas[0]['alter_mobile'] ;
		
		if($totalrow > 0){
		        
		    try{
                
                $otp = rand(999999,100000);
                
                $insert = $pdo->prepare("INSERT INTO otp_table (`username`, `sender_type`, `otp`, `otp_send_at`, `otp_type`, `created_at`)
                                        VALUES (?,?,?,?,?,?) ");
                                        
                $insert_result = $insert->execute(array($sender_id, $send_type, $otp, $current_date, 'Repair-Completed-OTP', $current_date));
                
                if($insert_result){
                    $pdo->commit();
                    $response['data']= $otp;
        		    $response['error']=false;
        		    $response['message']="User OTP Send Successfully";
                    
                }else{
                    $response['error']=true;
		            $response['message']='User OTP Send Failed..';  
                }
                
            }catch(PDOException $e){
                $pdo->rollBack();
                $response['message']="$e";
                $response['error']=true;
            } 
		        
		    
		}else{
		 $response['error']=true;
		    $response['message']='User Details Not Found!';        
		}
        
    }else{
	    $response['error']=true;
	    $response['message']='OTP Send Failed';
	}
		
	echo json_encode($response);
    
    
    
}



if($_POST['field_type']=="repair_submit_otp"){
    
    $send_type = $_POST['send_type'];
    $sender_id = $_POST['sender_id'];
    $otp = $_POST['otp'];
    $otp_type = 'Repair-Completed-OTP';
    $current_date = date('Y-m-d h:i:s');
    
    $check_otp = $pdo->prepare("SELECT * FROM `otp_table` WHERE username=? and sender_type=? and otp_type=? AND is_used=? ORDER BY id DESC LIMIT 1");
	$check_otp->execute(array($sender_id, $send_type, $otp_type, 0));
	$check_data = $check_otp->fetchAll(PDO::FETCH_ASSOC);
	$otprow = $check_otp->rowCount();
	
	if($otprow > 0){
	    $db_otp = $check_data[0]['otp'];
	    
	    if($db_otp == $otp){
	        
	        $update = $pdo->prepare("UPDATE otp_table SET is_used=?, verified_date=? WHERE username=? and sender_type=? and otp_type=? AND is_used=? ORDER BY id DESC LIMIT 1 ");
                                        
            $update_result = $update->execute(array(1, $current_date, $sender_id, $send_type, $otp_type, 0));
            
            if($update_result){

    		    $response['error']=false;
    		    $response['message']="OTP Verification Successfully Completed.";
            }else{
               $response['error']=true;
	           $response['message']='OTP Verification Failed';    
            }
	        
	        
	    }else{
	        $response['error']=true;
	        $response['message']='OTP MisMatched';   
	    }
	    
	    
	}else{
	    $response['error']=true;
	    $response['message']='OTP Check Failed';
	}
	
    echo json_encode($response);
    
}


?>