<?php
    include_once 'opendb.php';
    
    if (isset($_POST["pcode"]))
    {
	
        $pcode=$_POST["pcode"] ;
        $edit_time="";
        $pstock="";
         if (isset($_POST["edit_time"]))
         {
             $edit_time=$_POST["edit_time"];
         }
         if (isset($_POST["pstock"]))
         {
             $pstock=$_POST["pstock"];
         }
        
        $sql = "SELECT server_edit_time,edit_time,quantity FROM products WHERE prod_code like '".$pcode."'";
        $result = $conn->query($sql);
        $ret="";
        if ($result->num_rows > 0 && edit_time!="") {
                if($row = $result->fetch_assoc()) 
                {
                    if (is_null($row["server_edit_time"]))
                    {
                     
                      $ret= "GO ON";
                      
                    }
                    else
                    {
                            if (date_create($edit_time)!=$row["server_edit_time"])
                              {
                              $ret= "GO ON";
                              }
                              else 
                              {
                               $ret= "GO NEXT";
                              }
                      
                    }
                    
                    if($ret=="GO NEXT" && $pstock!="")
                    {
                            if((string)$row["qunantity"]!=$pstock)
                            {
                                $sql = "UPDATE products SET quantity=". $pstock ." WHERE prod_code like '". $pcode ."'";
                                $conn->query($sql);
                            }
                    }
                    echo $ret;
                    
                }
        }
        else {
                echo "GO ON";
        }
        
  
    }
    else
    {
        echo "ERROR: No data!";
    }
    
    $conn->close();
?>