<?php 
date_default_timezone_set("Asia/Bangkok");
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
  
   $user_id=$_POST['user_id'];
   $product_id=$_POST['product_id'];
   $amount=$_POST['amount'];
   $transaction_id='trendinfo_'.mt_rand(100000,999999);
   $used_cashpoints=$_POST['used_cashpoints'];
   $use_voucherpoints=$_POST['use_voucherpoints'];
   $address=$_POST['address'];
   $city=$_POST['city'];
   $state=$_POST['state'];
   $pincode=$_POST['pincode'];
   $discount_type=$_POST['discount_type'];
   $coupon_id=$_POST['coupon_id'];
   $price_reduce_by_coupon=$_POST['price_reduce_by_coupon'];
   $cart_price=$_POST['cart_price'];
   $shipping_charge=$_POST['shipping_charge'];
   $floor=$_POST['floor'];
   $unit_no=$_POST['unit_no'];
   $country=$_POST['country'];
   
 $cat=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id=".$user_id));
  
  if($cat['point_wallet'] >= $used_cashpoints){
		$createitem=mysqli_query($con,"INSERT INTO orders (user_id,product_id,amount,transaction_id,used_cashpoints,use_voucherpoints,floor,unit_no,address,city,country,pincode,
		discount_type,coupon_id,price_reduce_by_coupon,cart_price,shipping_charge,status) VALUES('".$user_id."','".$product_id."','".$amount."','".$transaction_id."','".$used_cashpoints."','".$use_voucherpoints."','".$floor."','".$unit_no."',
		'".$address."','".$city."','".$country."','".$pincode."','".$discount_type."','".$coupon_id."','".$price_reduce_by_coupon."','".$cart_price."','".$shipping_charge."',1)");
	 if($createitem){
		 $errors['orderid']='"'.mysqli_insert_id($con).'"';
		 mysqli_query($con,'DELETE FROM cart_items WHERE user_id='.$user_id);
		
		 $credits=$cat['point_wallet']-$used_cashpoints;
		$vouchers=$cat['vouchers']-$use_voucherpoints;
		 mysqli_query($con,"UPDATE users SET point_wallet=".$credits.",vouchers=".$vouchers." WHERE id=".$user_id);
	     	
	     	$errors['status']=true;
	     	$errors['order']=mysqli_insert_id($con);
	     	$errors['message']='Order Created Sucessfully';
	 }else{
		 $errors['status']=false;
	     $errors['message']='Somthing went wrong !!';
	 }
  }else{
	  $errors['status']=false;
	     $errors['message']='You do not have point balance !!';
  }
 	 
	 	echo json_encode($errors);
}