<?php
    include_once 'opendb.php';
    $filter="";
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "Receipts"; //fruits
    $xml         .= "<".$root_element.">";
    if (isset($_GET["filter"]))
    {
         $filter=" and " . $_GET["filter"];    
    }
        
    $CompID=$_GET["comp_id"]; 
    $filter.=" and comp_id=" .$CompID;
    $Last_syncNo=$_GET["last_sync_no"]; 
    $Qry="SELECT  M.Entry_No, M.Trans_Date,M.Vchr_No,M.Counter_code,M.Cust_Code,M.Cust_Name,M.Disc_Amt,M.Amount,M.Pay_Mode,M.Acc_Name,M.Ref_No,M.client_app_id,M.Notes,M.Cheque_No,M.sync_time,S.Bill_Date,S.Doc_Entry_No,S.Doc_Vchr_No,S.Amt_Paid FROM receipts M left join receipts_bills S on M.Entry_no=S.Entry_No  ";
    if (isset( $Last_syncNo)){
        if ($Last_syncNo>0){
           //  $filter.=" and M.Entry_no > " . $Last_syncNo ;    
        }
    }
   
    $Qry.=" where SYNC_ENTRY_NO=0 ".$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, "Receipts", "Rows");
   $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; 
}


    
    
?>