<?php  
 include_once 'opendb.php';
$json =file_get_contents('php://input');
//$json='[{"Entry_Date":"2018-01-29","Notes":"","Amount":325,"Voucher_no":"-0001","CUST_NAME":" ","Pay_Mode":"0","Sub_Code":"","Counter_Code":"","client_app_id":"0866-7600-3933-0909-0","sync_time":"2018-01-29 13:56:35","BillID":"1","prod_code":"1104","SL_No":1,"Quantity":1,"Rate":250,"Unit":"CTN","COST":7.24,"pack_qty":1,"sale_Date":"2018-01-29","discount":0,"Foc_Qty":0,"Foc_Unit":"CTN","Loc_code":"","Batch":""},{"Entry_Date":"2018-01-29","Notes":"","Amount":325,"Voucher_no":"-0001","CUST_NAME":" ","Pay_Mode":"0","Sub_Code":"","Counter_Code":"","client_app_id":"0866-7600-3933-0909-0","sync_time":"2018-01-29 13:56:35","BillID":"1","prod_code":"1106-E","SL_No":2,"Quantity":3,"Rate":25,"Unit":"CTN","COST":2.02,"pack_qty":1,"sale_Date":"2018-01-29","discount":0,"Foc_Qty":0,"Foc_Unit":"CTN","Loc_code":"","Batch":""}]';


$array=json_decode($json);
//var_dump($array1);
//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;
               $disc_amt=$array[$x]->disc_amt;
               $Voucher_No=$array[$x]->Voucher_no;
               $sub_code=$array[$x]->Sub_Code;
               $cust_name=$array[$x]->CUST_NAME;
               $cust_name = str_replace("&", "and", $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;
               $tax_amt=$array[$x]->tax_amt;
               $cgst_amt=$array[$x]->cgst_amt;
               $sgst_amt=$array[$x]->sgst_amt;
               $cash_paid=$array[$x]->cash_paid;
               $Pay_Mode=$array[$x]->Pay_Mode;
               $Comp_ID=$array[$x]->comp_id;
               if( $Comp_ID==0){
                    $conn->close();
                    die ( "Error: Co ID is Zero");
               }
               
               $sql="select entry_no from sales 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 sales (Entry_no,Entry_date,notes,Amount,Voucher_No,sub_code,cust_name,counter_code,client_app_id,pay_mode,disc_amt,tax_amt,sgst_amt,cgst_amt,comp_id,cash_paid,additions,cust_reg_no) " .
               " VALUES ($Eno,'$Entry_date','$notes', $Amount,'$Voucher_No','$sub_code','$cust_name','$counter_code','$client_app_id',$Pay_Mode,$disc_amt,$tax_amt,$sgst_amt,$cgst_amt,0,$cash_paid,0,'')"; //$Comp_ID
               
               if ($conn->query($sql) === TRUE)
               {
                  $Eno = $conn->insert_id;
               } else
               {
                  die ( "Error in insert sales :" . $sql . "-" . $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;
       
       $TAX_RATE=$array[$x]->TAX_RATE;
       $SGST_RATE=$array[$x]->SGST_RATE;
       $CGST_RATE=$array[$x]->CGST_RATE;

       
       $sql = "INSERT INTO sales_sub (Entry_no,sale_date,prod_code,SL_No,Quantity,Rate,Unit,COST,pack_qty,discount,Foc_Qty,Foc_Unit,Loc_code,Batch,taxrate,cgst_rate,sgst_rate,compid) " .
                                  " VALUES (".$Eno.",'".$Entry_date."','".$prod_code."',". $SL_No.",".$Quantity.",".$Rate.",'".$Unit."',".$COST.",".$pack_qty. ",".$discount.",".$Foc_Qty.",'".$Foc_Unit."','".$Loc_code."','".$Batch."',".$TAX_RATE.",".$CGST_RATE.",".$SGST_RATE.",".$Comp_ID.")";
       
      $conn->query($sql);
}
if($Eno>0){
	try {  
		$conn->commit();
		 $sql="update sales 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 sales where pkey=". $Eno  ;
			echo $x.  $sql ."<br>".$conn->error;
			throw $exception;
	}
} else {
	echo ( "Error : No Data ");
}
$conn->close();
?>