<?php
    include_once 'opendb.php';
    $mess="Start";
   
    if (isset($_POST["data"]))
    {
        $content=$_POST["data"];
        $content = iconv('UTF-8', 'UTF-8//IGNORE', $content);
        $xml=simplexml_load_string($content) or die("Error: Cannot create object" . $_POST["data"]);
        foreach ($xml as $Record){
            $Vtype= $Record->VType;
            $WEntry_No= $Record->WEntry_No;
            $PCEntry_No= $Record->PCEntry_No;
            if($Vtype=='Sale'){
                 $sql = "update sales set SYNC_ENTRY_NO=".$PCEntry_No." , IS_SYNCHED=1 WHERE Entry_No = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } elseif($Vtype=='SO'){
                 $sql = "update sale_order set SYNC_ENTRY_NO=".$PCEntry_No." , IS_SYNCHED=1 WHERE Entry_No = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } elseif($Vtype=='Rec'){
                 $sql = "update receipts set SYNC_ENTRY_NO=".$PCEntry_No." , IS_SYNCHED=1 WHERE Entry_No = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } elseif($Vtype=='Visit'){
                 $sql = "update customer_visits set  IS_SYNCHED=1 WHERE web_entry_no = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } elseif($Vtype=='QT'){
                 $sql = "update sale_quote set SYNC_ENTRY_NO=".$PCEntry_No." , IS_SYNCHED=1 WHERE Entry_No = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } elseif($Vtype=='SR'){
                 $sql = "update sales_return set SYNC_ENTRY_NO=".$PCEntry_No." , IS_SYNCHED=1 WHERE Entry_No = ".$WEntry_No.";";
                   if ($conn->query($sql) === TRUE)
                   {
                     
                   } else
                   {
                      $mess.= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            } else {
                $mess.= "Error: Invalid Doc Type";
            }
            
           
        }
        if ($mess=="Start") $mess="OK";
        echo $mess;    
            
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>