<?php
    include_once 'opendb.php';
    $mess="OK";
    $upderrors="None";
    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/parse error");
        $rownum=1;
    //    file_put_contents("temp.txt", $xml);
        foreach ($xml as $Record){ 
            
            if($rownum==1){
                $CompID=$Record->CompID;
                $Entry_No= $Record->Entry_No;				
                $Entry_Date= $Record->Entry_Date;
                $Voucher_No = $Record->Voucher_No;
                $Delman_Code= $Record->Delman_Code;
                $Delman_Name = $Record->Delman_Name;
				$Route_Code= $Record->Route_Code;
                $Route_Name = $Record->Route_Name;				
                //$WebEntry_No = $Record->WebEntry_No;
                $Remark=$Record->Remark;
                $sql = "SELECT * FROM delivery_master WHERE Entry_no =".$Entry_No." and comp_id=" .$CompID;
                $result = $conn->query($sql);
                if ($result->num_rows == 0 ) {
                       $sql = "INSERT INTO delivery_master (entry_no,comp_id) VALUES (".$Entry_No.",".$CompID.")";
                       if ($conn->query($sql) === TRUE)
                       {
                          $WebEntry_No = $conn->insert_id;
                          $mess="OK";
                       } else
                       {
                          $mess= "Error: " . $sql . "<br>" . $conn->error;
                          $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                       }       
                }
                else {                   
                     if($srow = $result->fetch_assoc())
                        {
							$WebEntry_No=$srow["WebEntry_No"];
							$sql = "delete FROM delivery_sub WHERE Wentry_no =".$WebEntry_No." and compid=" .$CompID;
							$conn->query($sql);
						} else {
							$mess= "Error: getting master record ";
						}
                }
                if ($mess=="OK")
                {
                    $sql = "UPDATE delivery_master SET Entry_Date='".$Entry_Date."'," 
                                                ."Voucher_No='".$Voucher_No."',"                                                                                                                                          
                                                ."Remark='".$Remark."',"                                                                                                                                                
                                                ."Delman_Code='".$Delman_Code."',"
												."Delman_Name='".$Delman_Name."',"
												."Route_Code='".$Route_Code."',"
												."Route_Name='".$Route_Name."'"
                                                ." WHERE WebEntry_No = ".$WebEntry_No." and comp_id=".$CompID;


                    if ($conn->query($sql) === TRUE) {
                        $mess="OK";
                    } else {
                        $mess= "Error updating master delivery  record: " . $conn->error;
                         $upderrors.="Error updating updating master delivery:<br>" . $conn->error;
                    }
                }
                
            }
            $rownum+=1;
            $Sub_Code = $Record->Sub_Code;
            $Order_No = $Record->Order_No;
            $Amount = $Record->Amount;			
            $Route_SLNo = $Record->Route_SLNo;
            $Notes = $Record->Notes;
            $is_Delivered=0 ;
            if(isset($Record->Is_Delivered)) $is_Delivered=$Record->Is_Delivered ;
            $sql = "INSERT INTO delivery_sub (Wentry_no,compid,sub_code,Order_No,Amount,route_slno,notes,is_Delivered) "; 
			$sql.=" VALUES ($WebEntry_No,$CompID,'$Sub_Code',$Order_No,$Amount,$Route_SLNo,'$Notes',$is_Delivered)";  
                       if ($conn->query($sql) === TRUE)
                       {                         
                          $mess="OK";
                       } else
                       {
                          $mess= "Error: " . $sql . "<br>" . $conn->error;
                          $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                       }   
            
        }
        if ($upderrors=="None"){
           echo  $WebEntry_No;
        }
        else {
             echo $upderrors;            
        }
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>