<?php

require_once("config.inc.php");
	require_once("DBConnector.php");
	$db = new DBConnector();
	
	////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
	header('Content-Type: text/xml'); 
	
    date_default_timezone_set('Asia/Colombo'); 

if ($_GET["Command"]=="pass_itno"){
	header('Content-Type: text/xml'); 
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
			
			$ResponseXML = "";
			$ResponseXML .= "<salesdetails>";
			
					
				$sql = "select * from s_mas where stk_no = '".$_GET["itno"]."'";
                             $result =$db->RunQuery($sql);
			if ($row = mysql_fetch_array($result)){ 
				
				if ($row['Flagdesc']=='1'){
                                            
                 $ResponseXML .= "<str_Flagdesc><![CDATA[yess]]></str_Flagdesc>";
                 } else {
                                             
                 $ResponseXML .= "<str_Flagdesc><![CDATA[Noo]]></str_Flagdesc>";
                 }
                                        
				
					$ResponseXML .= "<STK_NO><![CDATA[".$row['STK_NO']."]]></STK_NO>";
					$ResponseXML .= "<DESCRIPT><![CDATA[".$row['DESCRIPT']."]]></DESCRIPT>";
					
				}				
			
				
				$ResponseXML .= "</salesdetails>";	
				echo $ResponseXML;
				
				
	
}


if($_GET["Command"]=="add_tmp")
		{
		
			
			$ResponseXML .= "";
			$ResponseXML .= "<salesdetails>";
			
			
			$sql="delete from tmp_arn_data where item_code='".$_GET['item_code']."' and str_invno='".$_GET['invno']."' and itemd_hidden='".$_GET['itemd_hidden']."'";
			 
			$result =$db->RunQuery($sql);
			
			//echo $_GET['rate'];
			//echo $_GET['qty'];
			
			$qty=str_replace(",", "", $_GET["qty"]);
			
			
			$sql="Insert into tmp_arn_data (str_invno, item_code, itemdescript,qty1,cost1,net,vat,gross,tmp_no)values 
			('".$_GET['invno']."', '".$_GET['item_code']."', '".$_GET['itemdescript']."', '".$_GET["qty1"]."','".$_GET["cost1"]."','".$_GET["net"]."','".$_GET["vat"]."','".$_GET["gross"]."','".$_SESSION['tmp_no_purord']."') ";
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);	
			
			
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"100\"  background=\"\" ><font color=\"#FFFFFF\">Code</font></td>
                              <td width=\"300\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"100\"  background=\"\" ><font color=\"#FFFFFF\">Qty</font></td>
                              <td width=\"300\"  background=\"\"><font color=\"#FFFFFF\">Cost</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Selling</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Net</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Vat</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Gross</font></td>
                             
                            </tr>";
							
			$tot=0;
			$sql="Select * from tmp_arn_data where str_invno='".$_GET['invno']."'";
			$result =$db->RunQuery($sql);	
			while($row = mysql_fetch_array($result)){				
             	$ResponseXML .= "<tr>                              
                             <td  >".$row['item_code']."</a></td>
							 <td  >".$row['itemdescript']."</a></td>
                             <td  >".number_format($row['qty1'], 2, ".", ",")."</a></td>
                             <td  >".number_format($row['cost1'], 2, ".", ",")."</a></td>
                             <td  >".number_format($row['net'], 2, ".", ",")."</a></td> 
							 <td  >".number_format($row['vat'], 2, ".", ",")."</a></td>
                             <td  >".number_format($row['gross'], 2, ".", ",")."</a></td>
							 <td  ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=".$row['item_code']."  name=".$row['item_code']." onClick=\"del_item('".$row['item_code']."');\"></td></tr>";
							 
							 
							 //$tot=$tot+number_format($row['gross'], 2, ".", ",");
							 ////echo $tot;
							 //	$ResponseXML .= "<tot><![CDATA[".$tot."]]></tot>";
                           
				}			
						
                $ResponseXML .= "   </table>]]></sales_table>";
				
				$ResponseXML .= " </salesdetails>";
				
		//	}	
				
				
				echo $ResponseXML;
				
			
	}
	if ($_GET["Command"] == "calcu") {
   header('Content-Type: text/xml');
     


    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";
    $total = 0;
    if ($_GET["gross"] != '') {
        
        // if ($_GET["ssvat"] != '') {
//            var tot = document.getElementById('tot').value;
    		//vat_change
            $x = ($_GET["gross"] *100)/100 ; 
            
            
        // } else {
 
        // }
    }
// echo $x.'/';
            $v=$_GET["gross"] - $x ;
            //echo $v;
   
    //$tot = number_format($x);
   $tot= number_format($x, 2, '.', '');
   $vat1= number_format($v, 2, '.', '');

    $ResponseXML .= "<ssnet><![CDATA[$tot]]></ssnet>";
    $ResponseXML .= "<ssvat><![CDATA[$vat1]]></ssvat>";
 $ResponseXML .= "</salesdetails>";
    echo $ResponseXML;
}

if ($_GET["Command"] == "search_item") {
  
    $ResponseXML .= "";
    //$ResponseXML .= "<invdetails>";


    $ResponseXML .= "<table width=\"735\" border=\"0\" class=\"form-matrix-table\">
                            <tr>
                              <td width=\"121\"  background=\"images/headingbg.gif\" ><font color=\"#FFFFFF\">Item No</font></td>
                              <td width=\"424\"  background=\"images/headingbg.gif\"><font color=\"#FFFFFF\">Description</font></td>";

    $ResponseXML .= "</tr>";
 
    
    $sql1 = "select * from s_mas where STK_NO <> ''";
    if ($_GET['itno'] != "") {
        $sql1 .= " and STK_NO like '%" . $_GET['itno'] . "%'";
    }
    if ($_GET['itemname'] != "") {
        $sql1 .= " and DESCRIPT like '%" . $_GET['itemname'] . "%'";
    }
    
  	$result =$db->RunQuery($sql1);
 	while($row = mysql_fetch_array($result)){

            $ResponseXML .= "<tr>
                              <td onclick=\"itno_grn('".$row['STK_NO']."','".$_GET["stname"]."');\");\">". $row['STK_NO'] . "</a></td>
                              <td onclick=\"itno_grn('".$row['STK_NO']."','".$_GET["stname"]."');\");\">".$row['DESCRIPT']."</a></td>
							  <td onclick=\"itno_grn('".$row['STK_NO']."','".$_GET["stname"]."');\");\">".$row['QTYINHAND']."</a></td>";
			$ResponseXML .= "</tr>";
    }
    

    $ResponseXML .= "</table>";


    echo $ResponseXML;
     
}


?>
