 <?php
        include_once('opendb.php');
    $mess="OK";
    if (isset($_POST["data"]))
    {
            $xml=simplexml_load_string($_POST["data"]) or die("Error: parsing data".error_get_last());

            $pKey= $xml->mobile_clients[0]->pKey;
            $client_app_id= $xml->mobile_clients[0]->client_app_id;
            $inactive = $xml->mobile_clients[0]->inactive;
            $Description = $xml->mobile_clients[0]->Description;
            $Next_SO_No = $xml->mobile_clients[0]->Next_SO_No;
            $Next_Bill_No = $xml->mobile_clients[0]->Next_Bill_No;
            $Next_Rec_No = $xml->mobile_clients[0]->Next_Rec_No;
            $Counter_Code = $xml->mobile_clients[0]->Counter_Code;
            $client_type = $xml->mobile_clients[0]->client_type;
            $isSales = $xml->mobile_clients[0]->isSales;
            $isSO = $xml->mobile_clients[0]->isSO;
            $isRec = $xml->mobile_clients[0]->isRec;
            $CompID=$xml->mobile_clients[0]->CompID;
            $serialprefix=$xml->mobile_clients[0]->serial_prefix;
            $AppPass = $xml->mobile_clients[0]->App_Pass;
            $isRequest=$xml->mobile_clients[0]->isRequest;
            $isSRet=$xml->mobile_clients[0]->isSRet;            
            $isQuote=$xml->mobile_clients[0]->isquote;
            $ispricelock=$xml->mobile_clients[0]->ispricelock;
            try {
                $isRequest=$xml->mobile_clients[0]->isRequest;
            } catch (Exception $ex) {
                
            }
            try {
                $isSRet=$xml->mobile_clients[0]->isSRet;   
            } catch (Exception $ex) {
                
            }
            try {
                $ispricelock=$xml->mobile_clients[0]->ispricelock;
            } catch (Exception $ex) {
                
            }
             $price_level=1 ;
             try {
                 $price_level=$xml->mobile_clients[0]->price_level;
             } catch (Exception $ex) {  

             }
            if ($price_level=='') $price_level="1";
              $isCost=0 ;
             try {
                 $isCost=$xml->mobile_clients[0]->isCost;
             } catch (Exception $ex) {  

             }
             if ($isCost=='') $isCost="0";
            
            if ($isRequest=='') $isRequest="0";
            if ($isSRet=='') $isSRet="0";            
            if($isQuote=='') $isQuote="0";
            if($ispricelock=='') $ispricelock="0";
            $sql = "SELECT * FROM mobile_clients WHERE Counter_Code like '".$Counter_Code."' and comp_id=" .$CompID;
            $result = $conn->query($sql);
            if ($result->num_rows == 0 ) {
                   $sql = "INSERT INTO mobile_clients (comp_id,Counter_Code) VALUES (".$CompID.",'".$Counter_Code."')";
                   if ($conn->query($sql) === TRUE)
                   {
                      $mess="OK";
                   } else
                   {
                      $mess= "Error: " . $sql . "<br>" . $conn->error;
                   } 
            }

            if ($mess=="OK")
            {
                $sql = "UPDATE mobile_clients SET " 
                                            ."inactive=".$inactive.","
                                            ."Description='".$Description."',"
                                            ."Next_SO_No=".$Next_SO_No.","
                                            ."Next_Bill_No=".$Next_Bill_No.","
                                            ."Next_Rec_No=".$Next_Rec_No.","
                                            ."client_type=".$client_type."," 
                                            ."App_Pass='".$AppPass."',"
                                            ."isSO=".$isSO.","
                                            ."isRec=".$isRec.","
                                            ."isRequest=".$isRequest.","
                                            ."isSRet=".$isSRet.","
                                            ."isSales=".$isSales.","
                                            ."client_app_id='".$client_app_id."',"    
                                            ."price_level=".$price_level.","
                                            ."isCost=".$isCost.","
                                            ."isQuote=".$isQuote."," 
                                            ."serial_prefix='".$serialprefix."' "
                                            ." WHERE Counter_Code like '".$Counter_Code."'  and comp_id=" .$CompID.";";

                if ($conn->query($sql) === TRUE) {
                    $mess="OK";
                } else {
                    $mess= "Error updating clients record: " . $conn->error . $sql;
                }



            }
            echo $mess;


    }
      Else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>