 <?php
   
    $mess="Error";
    $upderrors="None";
    $currow=1;
    $content="";
   
    if (isset($_POST["data"]))
    {
        $content=$_POST["data"];
        $content = iconv('UTF-8', 'UTF-8//IGNORE', $content);
    } else {
        $xmlData = file_get_contents('php://input');
        if (!empty($xmlData)) {
            $content = iconv('UTF-8', 'UTF-8//IGNORE', $xmlData);
        }
    }
    if (empty($content)){
        die("ERROR: No data!"); 
    }  
    include_once('opendb.php');
       // $xml=simplexml_load_string($_POST["data"]) or die("Error: parsing data".error_get_last());
         $xml=simplexml_load_string($content)  or die("Error: parsing data".error_get_last());
        foreach ($xml as $Record)
          {
            $SUB_NAME= $Record->sub_name;
            $ADDRESS= $Record->address;
            $SUB_CODE = $Record->sub_code;
            $EMAIL = $Record->email;
            $PLACE = $Record->place;
            $PHONE = $Record->phone;
            $MOBILE = $Record->mobile;
            $FAX = $Record->fax;
            $CONT_NAME = $Record->cont_name;
            $CONT_PHONE = $Record->cont_phone;
            $AREA_CODE=$Record->area_code;
            $SMAN_CODE = $Record->sman_code;
            $SUB_TYPE = $Record->sub_type;
            $Inactive = $Record->inactive;
            $CompID=$Record->compid;
            $Reg_No1 = $Record->reg_no1;
            $AppPass = $Record->app_pass;
            $AreaName = $Record->area_name;
            $EDIT_TIME = $Record->edit_time;
            
              $price_level=1 ;
             try {
                  $price_level=$Record->price_level;
             } catch (Exception $ex) {  

             }
            if ($price_level=='') $price_level="1";
            
            $country_code = isset($Record->country_code) ? $Record->country_code : '';
            $street_name = isset($Record->street_name) ? $Record->street_name : '';
            $bldg_no = isset($Record->bldg_no) ? $Record->bldg_no : '';
            $city = isset($Record->city) ? $Record->city : '';
            $district = isset($Record->district) ? $Record->district : '';
            $id_type = isset($Record->id_type) ? $Record->id_type : '';
            $name_ar = isset($Record->name_ar) ? $Record->name_ar : '';
            $add1_ar = isset($Record->add1_ar) ? $Record->add1_ar : '';
            $add2_ar = isset($Record->add2_ar) ? $Record->add2_ar : '';
            $add3_ar = isset($Record->add3_ar) ? $Record->add3_ar : '';
            $reg_no2 = isset($Record->reg_no2) ? $Record->reg_no2 : '';
            
            $sql = "SELECT * FROM subsidiary WHERE sub_code like '".$SUB_CODE."' and comp_id=" .$CompID;
            $result = $conn->query($sql);
            
            if ($result->num_rows == 0 ) {
                   $sql = "INSERT INTO subsidiary (sub_code,sub_type,comp_id) VALUES ('".$SUB_CODE."','C',".$CompID.")";
                   if ($conn->query($sql) === TRUE)
                   {
                      $mess="OK";
                   } else
                   {
                      $mess= "Error: " . $sql . "<br>" . $conn->error;
                      $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                   } 
            }
            else {
              $mess="OK";  
            }

            if ($mess=="OK")
            {
                $sql = "UPDATE subsidiary SET SUB_NAME='".$SUB_NAME."'," 
                                            ."ADDRESS='".$ADDRESS."',"
                                            ."EMAIL='".$EMAIL."',"
                                            ."PLACE='".$PLACE."',"
                                            ."PHONE='".$PHONE."',"
                                            ."MOBILE='".$MOBILE."',"
                                            ."FAX='".$FAX."',"
                                            ."CONT_NAME='".$CONT_NAME."',"
                                            ."CONT_PHONE='".$CONT_PHONE."',"
                                            ."AREA_CODE='".$AREA_CODE."',"
                                            ."Area_Name='".$AreaName."',"
                                            ."SMAN_CODE='".$SMAN_CODE."',"  
                                            ."SUB_TYPE='".$SUB_TYPE."',"
                                            ."Inactive=".$Inactive.","                                        
                                            ."Reg_No1='".$Reg_No1."',"
                                            ."price_level=".$price_level.","
                                            ."App_Pass='".$AppPass."',"
                                            ."country_code='".$country_code."',"
                                            ."street_name='".$street_name."',"
                                            ."bldg_no='".$bldg_no."',"
                                            ."city='".$city."',"
                                            ."district='".$district."',"
                                            ."id_type='".$id_type."',"
                                            ."name_ar='".$name_ar."',"
                                            ."add1_ar='".$add1_ar."',"
                                            ."add2_ar='".$add2_ar."',"
                                            ."add3_ar='".$add3_ar."',"
                                            ."Reg_No2='".$reg_no2."',"
                                            ."EDIT_TIME=".$EDIT_TIME." WHERE sub_code like '".$SUB_CODE."' and comp_id=".$CompID;
// ."IsNotify='".$IsNotify."',"   ."isInterState=".$isInterState.","
                if ($conn->query($sql) === TRUE) {
                    $mess="OK";
                } else {
                    $mess= "Error updating customer record: " . $conn->error;
                    $upderrors.="Error updating customer record:<br>" . $conn->error.$sql;
                }
            }
        } 
        if ($upderrors=="None"){
           echo $mess;
        }
        else {
             echo $upderrors;            
        }
            
   
    $conn->close();
?>