<?php
$response = array();
include("../connect.php");
if(isset($_POST['distributor_id']) && isset($_POST['customer_id']) && isset($_POST['transaction_json']))
{  
	$d_id = $_POST['distributor_id'];
	$c_id = $_POST['customer_id'];
	$t_json = $_POST['transaction_json'];
	$t_remark = isset($_POST['remark']) ? $_POST['remark'] : '';
	date_default_timezone_set('Asia/Calcutta');
	$scro = date("H:i:s");
	$da  = date("Y-m-d");
	$scri =$da." ".$scro;
	
	if($t_json!="")
	{ 
    
	$check_jeson=json_decode($t_json, true);
	if(!empty($check_jeson)) { 
    $temp_explode='';
	$select_order=mysqli_query($con,"select receipt_sequence from tbl_sequence where distributor_id='$d_id'");
	$select_row =  mysqli_fetch_assoc($select_order);
	if(mysqli_num_rows($select_order)>0)
	{
		$temp=$select_row['receipt_sequence'];
		if($temp=="")
		{
			$sd="R00001";
		}
		else
		{
			$final_invoice=(int)substr($temp,1);
			$final_invoice=$final_invoice+1;
			$final_invoice1=(string)$final_invoice;
			$string_count=5-(int)strlen($final_invoice1);
			for($vb=0;$vb<$string_count;$vb++)
			{
				$temp_explode=$temp_explode."0";
			}
			$sd="R".$temp_explode.$final_invoice;
		}
		$uq_sequence=mysqli_query($con,"UPDATE tbl_sequence SET receipt_sequence='$sd',last_update_date='$da' where distributor_id='$d_id'");
	}
	else
	{
		$sd="R00001";
		$uq_sequence =mysqli_query($con,"insert into tbl_sequence(distributor_id,receipt_sequence,last_update_date)values('$d_id','$sd','$da')");
	}
	
	$query ="insert into tbl_transaction(customer_transaction_id,customer_id,transaction_date,transaction_time,remark)values('$sd','$c_id','$da','$scro','$t_remark')";
	$res = mysqli_query($con,$query);		
	if($res)
	{
		$query1="SELECT * FROM  tbl_transaction where customer_id ='$c_id' and customer_transaction_id='$sd' ORDER BY `transaction_id` DESC LIMIT 1";
		$res1 = mysqli_query($con,$query1);
		if(mysqli_num_rows($res1)>0)
		{
			$row =  mysqli_fetch_assoc($res1);
			$re= $row['transaction_id'];
			$json_a=json_decode($t_json, true); 
			$total=0;
			$totalq=0;
			foreach ($json_a as $person)
			{
				$product_id=$person['product_id'];
				$price=$person['price'];
				$quantity=$person['quantity'];
				$temp_amt=$quantity*$price;
				$total=$total+$temp_amt;
				$temp_qty=$quantity;
				$totalq=$totalq+$temp_qty;
				
				$query2=mysqli_query($con,"insert into tbl_transaction_detail(transaction_id,product_id,quantity,price)values('$re','$product_id','$quantity','$price')");				
			}

            $update_tot_amt=mysqli_query($con,"UPDATE tbl_transaction SET total_amount='$total', total_quantity='$totalq' where transaction_id='$re'");
			
            if($update_tot_amt)
            {
				 
			 $title="Tarkashcorp Management";
             
			 $message=" Dear, Admin You Have New Order. Order No. $sd Please Check APK/Admin Panel For More Detail.";
			 
			 $flag=0; 
			 $FIREBASE_API_KEY="AAAAeuYrumI:APA91bFlnBgFQvjGUN0qe_iDJyJN5U8TX_yuQOkkoA0Lqdwfea-Y3kJrOAFH0SkD6zv74xnVrGUgQW47F9m6xW631f6dkdbKmoqO4yYyX5p4nQuGmV203LTMoeL-ECINmvPfRnoJruZ8";
			 $url = 'https://fcm.googleapis.com/fcm/send';
			 $headers = array( 'Authorization: key=' . $FIREBASE_API_KEY,'Content-Type: application/json');
								
			 $sel_dist=mysqli_query($con,"select * from tbl_distributor_users where distributor_id='$d_id'");
			 $num=mysqli_num_rows($sel_dist);
			 $ch = curl_init ();
			 while($row=mysqli_fetch_array($sel_dist))
			 {
				$key=$row['device_id'];
				
				$fields = array('to' => $key, 'notification' =>array('title'=>$title,'body'=>$message));                         
				$payload = json_encode ( $fields );
				curl_setopt ( $ch, CURLOPT_URL, $url );
				curl_setopt ( $ch, CURLOPT_POST, true );
				curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
				curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
				//curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
				curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
				curl_setopt ( $ch, CURLOPT_POSTFIELDS, $payload); 
			    $result = curl_exec ( $ch );
				$flag=1; 
			 }
	         
			 if($flag=='1')
		     {
			   $response['success']=1;
		       $response['details']="Order Placed Successfully Your Order No. $sd!";
			 }			   
			}				
		}
	}
	else 
	{
		$response['success']=0;
		$response['details']="Error In Order Placing!";
	}	
	}
	else
	{
	$response['success']=0;
	$response['details']="Please add to product in cart";	
	}
	}
	else
	{
	$response['success']=0;
	$response['details']="Please add to product in cart";	
	}
}
else
{
	$response['success']=0;
	$response['details']="Required Field Missing...!!";
}		
echo json_encode($response);
?> 