<?php
    include_once 'opendb.php';
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "SaleQuote"; //fruits
    $xml         .= "<".$root_element.">";
    if (isset($_GET["filter"]))
    {
         $filter=" and " . $_GET["filter"];    
    }
        
    $CompID=$_GET["comp_id"]; 
    $Last_syncNo=$_GET["last_sync_no"]; 
    $filter.=" and comp_id=" .$CompID;
    
    $Qry="SELECT M.Entry_no as Entry_No, Entry_Date,Notes,Amount,Tax_Amt,Voucher_no,Sub_Code,Cust_Name,Counter_Code,client_app_id,sync_time,prod_code,SL_No,Quantity,Rate,Unit,COST,pack_qty,discount,Foc_Qty,Foc_Unit,Loc_code,Batch FROM sale_quote M LEFT  JOIN  sale_quote_sub  S ON M.Entry_No=S.Entry_No ";
    if (isset( $Last_syncNo)){
        if ($Last_syncNo>0){
           // $filter.=" and M.Entry_no > " . $Last_syncNo ;    
        }
    }
     $cutDate=date('Y-m-d',strtotime('-15 day'));
    $Qry.=" where SYNC_ENTRY_NO=0 and Entry_date > '$cutDate' ".$filter . " order by M.Entry_No ";  
   // echo $Qry;
    $result = $conn->query($Qry) or die(mysqli_error($conn). $Qry); 
    
   header("Content-Type: application/xml");
   echo sqlToXml($result, "SaleOrder", "Rows");
    $rowcount=mysqli_num_rows($result);
   if($rowcount==0){
      clear_old_records($CompID, $conn);
   }
   mysqli_free_result($result);
  
   
   $conn->close(); 
  
function sqlToXml($queryResult, $rootElementName, $childElementName)
{ 
    $xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
    $xmlData .= "<" . $rootElementName . ">";
    
    while ($row = mysqli_fetch_assoc($queryResult))
    {
            $xmlData .= "<" . $childElementName . ">";
            foreach( $row as $field => $value) {
             
            // echo "<"."{$field}".">"."  - {$value}<br>";

                $xmlData .= "<{$field}>{$value}</{$field}>";
            }
            $xmlData .= "</" . $childElementName . ">"; 
    }
 
   
    $xmlData .= "</" . $rootElementName . ">"; 
 
    return $xmlData; 
}

function clear_old_records($CompID, $conn){
      $Set_Name="QT_CLRDATE";
    $isClear=1;
    $sql = "SELECT * FROM appsettings WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
    $result = $conn->query($sql);
    if ($result->num_rows == 0 ) {
           $sql = "INSERT INTO appsettings (set_category,set_name,set_value,val_type,comp_id) VALUES ('GENERAL','".$Set_Name."','','S',".$CompID.")";
           if ($conn->query($sql) === TRUE)
           {} else {
              die( "Error: Inerting settings " . $sql . "<br>" . $conn->error);
           } 
    } else if ($nrow=mysqli_fetch_assoc($result)) {
        if ($nrow['set_value']==date('Y-m-d')){
            $isClear=0;        
        }
    }
    
    if ($isClear==1){
        $cutDate=date('Y-m-d',strtotime('-30 day'));
        $sql="select entry_no from sale_quote where sync_entry_no > 0 and Entry_date < '$cutDate' and comp_id=$CompID";
        $mresult = $conn->query($sql);
        while ($row = mysqli_fetch_assoc($mresult)){
             $mENo=$row['entry_no'];
             $mqry="delete from sale_quote_sub where entry_no=$mENo";
             $conn->query($mqry);
             $mqry="delete from sale_quote where entry_no=$mENo";
             $conn->query($mqry);
        }
        mysqli_free_result($mresult);
        $sql = "update appsettings set set_value='". date('Y-m-d') ."'  WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
        $conn->query($sql);
    }
    
        
}
    
    
    
?>