<?php  

 include_once 'opendb.php';
$json =file_get_contents('php://input');

//$json='[{"Entry_Date":"2019-06-01","Notes":"","Amount":0,"Voucher_no":"-0003","CUST_AREA":"","CUST_PHONE":"","Sub_Code":"C0001","Counter_Code":"C0001","comp_id":"8","client_app_id":"2498-6297-9899-1009-1","sync_time":"2019-06-01 16:06:49","BillID":"3","prod_code":"P1011","SL_No":1,"Quantity":5,"Rate":280,"Unit":"PAIRS","COST":0,"pack_qty":1,"sale_Date":"2019-06-01","discount":0,"Foc_Qty":0,"Foc_Unit":"PAIRS","Loc_code":"","Batch":""},{"Entry_Date":"2019-06-01","Notes":"","Amount":0,"Voucher_no":"-0003","CUST_AREA":"","CUST_PHONE":"","Sub_Code":"C0001","Counter_Code":"C0001","comp_id":"8","client_app_id":"2498-6297-9899-1009-1","sync_time":"2019-06-01 16:06:49","BillID":"3","prod_code":"P1010","SL_No":2,"Quantity":4,"Rate":100,"Unit":"PAIRS","COST":0,"pack_qty":1,"sale_Date":"2019-06-01","discount":0,"Foc_Qty":0,"Foc_Unit":"PAIRS","Loc_code":"","Batch":""}]';
//die($json);
$array=json_decode($json);

//var_dump($array); 
//echo $json;

//die($array1[1]->Entry_Date);
//die(json_last_error() );
$Eno=-1;

for ($x = 0; $x < count($array); $x++) 
{        
        if($x ==0)
        {   
               
               $Entry_date=$array[$x]->Entry_Date;               
               $notes=$array[$x]->Notes;
               $Amount=$array[$x]->Amount;
               $TaxAmt=$array[$x]->Tax_Amt;
               $Voucher_No=$array[$x]->Voucher_no;
               $sub_code=$array[$x]->Sub_Code;
               $cust_name=$array[$x]->CUST_NAME;
               $cust_name1=""; //$array[$x]->CUST_NAME1;
               $cust_phone=$array[$x]->CUST_PHONE;
               $cust_area=$array[$x]->CUST_AREA;               
               $counter_code=$array[$x]->Counter_Code;
               $client_app_id=$array[$x]->client_app_id;
               $Comp_ID=$array[$x]->comp_id;
               $cust_name=substr($cust_name,0,40);
               $sql="select entry_no from sale_quote where Entry_date ='$Entry_date' and Voucher_No like '$Voucher_No' and client_app_id like '$client_app_id' and Amount=$Amount and comp_id=$Comp_ID;";
               $sresult = $conn->query($sql);
                if ($sresult->num_rows > 0 )
                { 
                    if($srow = $sresult->fetch_assoc())
                      {
                       $Eno= $srow['entry_no'];
                        $conn->close();
                        exit($Eno);
                    }
                   
                }
               
               $sql = "INSERT INTO sale_quote (Entry_no,Entry_date,notes,Amount,Tax_Amt,Voucher_No,sub_code,cust_name,cust_name1,cust_area,cust_phone,counter_code,client_app_id,comp_id) " .
               " VALUES (".$Eno.",'".$Entry_date."','".$notes."',". $Amount.",$TaxAmt,'".$Voucher_No."','".$sub_code."','".$cust_name."','".$cust_name1."','".$cust_area."','".$cust_phone."','".$counter_code."','".$client_app_id. "',0)";  //".$Comp_ID updated later."
               
               if ($conn->query($sql) === TRUE)
               {
                  $Eno = $conn->insert_id;
               } else
               {
                  die ( "Error in insert SO :" . $sql . "<br>" . $conn->error);
               } 
               $conn->begin_transaction();
       }
       
        $Comp_ID=$array[$x]->comp_id;
       $prod_code=$array[$x]->prod_code;
       $SL_No=$array[$x]->SL_No;
       $Quantity=$array[$x]->Quantity;
       $Rate=$array[$x]->Rate;
       $Unit=$array[$x]->Unit;
       $COST=$array[$x]->COST;
       $pack_qty=$array[$x]->pack_qty;       
       $discount=$array[$x]->discount;
       $Foc_Qty=$array[$x]->Foc_Qty;
       $Foc_Unit=$array[$x]->Foc_Unit;
       $Loc_code=$array[$x]->Loc_code;
       $Batch=$array[$x]->Batch;

       
       $sql = "INSERT INTO sale_quote_sub (Entry_no,sale_date,prod_code,SL_No,Quantity,Rate,Unit,COST,pack_qty,discount,Foc_Qty,Foc_Unit,Loc_code,Batch,compid) " .
                                  " VALUES (".$Eno.",'".$Entry_date."','".$prod_code."',". $SL_No.",".$Quantity.",".$Rate.",'".$Unit."',".$COST.",".$pack_qty. ",".$discount.",".$Foc_Qty.",'".$Foc_Unit."','".$Loc_code."','".$Batch."',".$Comp_ID.")";
       
       
       $conn->query($sql);
       
}
if($Eno>0){
	try {  
		$conn->commit();
		 $sql="update sale_quote set entry_no= $Eno ,comp_id = $Comp_ID where pkey=" . $Eno ;
		  if ($conn->query($sql) === TRUE)
		  {
			  echo($Eno);
		  }
		  else 
		  {
			 die ( "Error in Entry No update " . $sql . "<br>" . $conn->error);
		  }
	} catch (mysqli_sql_exception $exception) {
			$conn->rollback();
			$sql="delete from sale_quote where pkey=". $Eno  ;
			echo $x.  $sql ."<br>".$conn->error;
			throw $exception;
	}
} else {
	echo ( "Error : No Data ");
}

$conn->close();
?>