<?php 
session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
	require_once("config.inc.php");
	require_once("DBConnector.php");
	$db = new DBConnector(); $dbinv = $db->GetConnection();
	
	////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
	header('Content-Type: text/xml'); 
	
		 date_default_timezone_set('Asia/Colombo'); 
	
	/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
	
		
				
	///////////////////////////////////// Registration /////////////////////////////////////////////////////////////////////////
		
		if($_GET["Command"]=="add_address")
		{
			//echo "Regt=".$Regt."RegimentNo=".RegimentNo."Command=".$Command;
			
			
	/*		$sql="Select * from tmp_army_no where edu= '".$_GET['edu']."'";
			$result =$db->RunQuery($sql);
			if($row = $result->fetch()){
				$ResponseXML .= "exist";
				
				
				
			}	else {*/
			
		//	$ResponseXML .= "";
			//$ResponseXML .= "<ArmyDetails>";
			
		/*	$sql1="Select * from mas_educational_qualifications where str_Educational_Qualification= '".$_GET['edu']."'";
			$result1 =$db->RunQuery($sql1);
			$row1 = $result1->fetch();
			$ResponseXML .=  $row1["int_Educational_Qulifications"];*/
			
			$sqlt="Select * from customer_mast where id ='".$_GET['customerid']."'";
			
			$resultt =$db->RunQuery($sqlt);
			if ($rowt = $resultt->fetch()){
				echo $rowt["str_address"];
			}
			
			
				
			
	}
	
			
		if($_GET["Command"]=="new_inv")
		{
			
			$_SESSION["print"]=0;
			
		/*	$sql="Select CAS_INV_NO_m from invpara";
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$tmpinvno="000000".$row["CAS_INV_NO_m"];
			$lenth=strlen($tmpinvno);
			$invno="INV".substr($tmpinvno, $lenth-7);
			echo $invno;*/
			
			$sql="Select PORD_NO from invpara where COMCODE='".$_SESSION['company']."'";
			
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$tmpinvno="000000".$row["PORD_NO"];
			$lenth=strlen($tmpinvno);
			$invno=$_SESSION['company'] .trim("LARN/ ").substr($tmpinvno, $lenth-8);
			$_SESSION["invno"]=$invno;
			
			
			$sql="Select PORD_NO from tmpinvpara";
			$result =$db->RunQuery($sql);
			$row = $result->fetch();
			$_SESSION["tmp_no_arn"]=$_SESSION['company'] . "LARN/".$row["PORD_NO"];
			
			
			
			$sql1="delete from tmp_purord_data where tmp_no='".$_SESSION["tmp_no_arn"]."'";
			$result1 =$db->RunQuery($sql1);
			
			$sql1="update tmpinvpara set PORD_NO=PORD_NO+1";
			$result1 =$db->RunQuery($sql1);
			
			 
				$ResponseXML = "";
	$ResponseXML .= "<salesdetails>";
	$ResponseXML .= "<invno><![CDATA[" . $invno . "]]></invno>";
	$ResponseXML .= "<invdate><![CDATA[" . date('Y-m-d') . "]]></invdate>";
	$ResponseXML .= " </salesdetails>";
	echo $ResponseXML;
		}
	
	
	if($_GET["Command"]=="add_tmp")
		{
		
			$department=$_GET["department"];
			
			$ResponseXML .= "";
			$ResponseXML .= "<salesdetails>";
			
			
			$sql="delete from tmp_purord_data where str_code='".$_GET['itemcode']."' and str_invno='".$_GET['invno']."' ";
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);
			
			//echo $_GET['rate'];
			//echo $_GET['qty'];
			
			$qty=str_replace(",", "", $_GET["qty"]);
			
			
			$sql="Insert into tmp_purord_data (str_invno, str_code, str_description, partno, qty)values 
			('".$_GET['invno']."', '".$_GET['itemcode']."', '".$_GET['item']."', '".$_GET["partno"]."', ".$qty.") ";
			//$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\">Part No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                             
                            </tr>";
							
			
			$sql="Select * from tmp_purord_data where str_invno='".$_GET['invno']."'";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$ResponseXML .= "<tr>                              
                             <td  >".$row['str_code']."</a></td>
							 <td  >".$row['str_description']."</a></td>
							 <td  >".$row['partno']."</a></td>
							 <td  >".number_format($row['qty'], 2, ".", ",")."</a></td>
							 <td  ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=".$row['str_code']."  name=".$row['str_code']." onClick=\"del_item('".$row['str_code']."');\"></td></tr>";
							 
                           
				}			
							
                $ResponseXML .= "   </table>]]></sales_table>";
				
				$ResponseXML .= " </salesdetails>";
				
		//	}	
				
				
				echo $ResponseXML;
				
			
	}
	
	
	if($_GET["Command"]=="arn")
		{
		
			//$department=$_GET["department"];
			
			$ResponseXML .= "";
			$ResponseXML .= "<salesdetails>";
			
			
			$sql="select * from s_ordmas where REFNO='".$_GET['invno']."' ";
			//$ResponseXML .= $sql;
			$result =$db->RunQuery($sql);
			if($row = $result->fetch()){
				$ResponseXML .= "<REFNO><![CDATA[".$row['REFNO']."]]></REFNO>";
				$ResponseXML .= "<SDATE><![CDATA[".$row['SDATE']."]]></SDATE>";
				$ResponseXML .= "<SUP_CODE><![CDATA[".$row['SUP_CODE']."]]></SUP_CODE>";
				$ResponseXML .= "<SUP_NAME><![CDATA[".$row['SUP_NAME']."]]></SUP_NAME>";
				$ResponseXML .= "<REMARK><![CDATA[".$row['REMARK']."]]></REMARK>";
				$ResponseXML .= "<DEP_CODE><![CDATA[".$row['DEP_CODE']."]]></DEP_CODE>";
				$ResponseXML .= "<DEP_NAME><![CDATA[".$row['DEP_NAME']."]]></DEP_NAME>";
				$ResponseXML .= "<REP_CODE><![CDATA[".$row['REP_CODE']."]]></REP_CODE>";
				$ResponseXML .= "<REP_NAME><![CDATA[".$row['REP_NAME']."]]></REP_NAME>";
				$ResponseXML .= "<S_date><![CDATA[".$row['S_date']."]]></S_date>";
				$ResponseXML .= "<LC_No><![CDATA[".$row['LC_No']."]]></LC_No>";
				$ResponseXML .= "<pi_no><![CDATA[".$row['pi_no']."]]></pi_no>";
				$ResponseXML .= "<Brand><![CDATA[".$row['Brand']."]]></Brand>";
				$ResponseXML .= "<COUNTRY><![CDATA[".$row["COUNTRY"]."]]></COUNTRY>";
			}	
			
		//	$sql="delete from tmp_purord_data where str_invno='".$_GET['invno']."' ";
			//$ResponseXML .= $sql;
		//	$result =$db->RunQuery($sql);
			
		//	$sql="select * from s_ordtrn where REFNO='".$_GET['invno']."' ";
			//$ResponseXML .= $sql;
		//	$result =$db->RunQuery($sql);
		//	while($row = $result->fetch()){
		//		$sql1="Insert into tmp_purord_data (str_invno, str_code, str_description, partno, qty)values 
		//		('".$row['REFNO']."', '".$row['STK_NO']."', '".$row['DESCRIPT']."', '".$row["partno"]."', ".$row["ORD_QTY"].") ";
			//$ResponseXML .= $sql;
		//		$result1 =$db->RunQuery($sql1);	
		//	}
							
			
			
	
			
			
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"200\"  background=\"\" ><font color=\"#FFFFFF\">Code</font></td>
                              <td width=\"800\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"300\"  background=\"\"><font color=\"#FFFFFF\">Order Qty</font></td>
                              <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">FOB</font></td>
                              <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
							  <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Cost</font></td>
							   <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Selling</font></td>
							    <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Margin</font></td>
								 <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Sub Total</font></td>
							
                            </tr>";
				
			$mcou=0;	
			$sql="Select count(*) as mcou from s_ordtrn where REFNO='".$_GET['invno']."'";
			$result =$db->RunQuery($sql);	
			$row = $result->fetch();
			$mcou=$row["mcou"]+1;
							
			$i=1;
			$sql="Select * from s_ordtrn where REFNO='".$_GET['invno']."'";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){	
			
				$itemcode="itemcode".$i;
				$itemname="itemname".$i;
				$ord_qty="ord_qty".$i;
				$fob="fob".$i;
				$qty="qty".$i;
				$cost="cost".$i;
				$selling="selling".$i;
				$margin="margin".$i;
				$subtotal="subtotal".$i;
				
				$sql_selling="select SELLING from s_mas where STK_NO='" . $row['STK_NO'] . "'";
				$result_selling =$db->RunQuery($sql_selling);	
				$row_selling = $result_selling->fetch();
							
             	$ResponseXML .= "<tr>                              
                             <td ><input type=\"text\" size=\"15\" name=".$itemcode." id=".$itemcode."   value=".$row['STK_NO']." class=\"text_purchase3\" disabled=\"disabled\"/></td>
							  <td ><input type=\"text\" size=\"15\" name=".$itemname." id=".$itemname."  value='".$row['DESCRIPT']."' class=\"text_purchase3\" disabled=\"disabled\"/></td>
							  <td ><input type=\"text\" size=\"15\" name=".$ord_qty." id=".$ord_qty."  value=".$row['ORD_QTY']." class=\"text_purchase3\" disabled=\"disabled\"/></td>
							
							 <td ><input type=\"text\" size=\"15\" name=".$fob." id=".$fob."  class=\"text_purchase3\"/></td>
							 <td ><input type=\"text\" size=\"15\" name=".$qty." id=".$qty."  class=\"text_purchase3\" onBlur=\"cal_subtot('".$i."', '".$mcou."');\"/></td>
							 <td ><input type=\"text\" size=\"15\" name=".$cost." id=".$cost."  class=\"text_purchase3\" onBlur=\"cal_subtot('".$i."', '".$mcou."');\"/></td>
							 <td ><input type=\"text\" size=\"15\" name=".$selling." id=".$selling." onBlur=\"cal_margine('".$i."', '".$mcou."');\" value=".$row_selling['SELLING']."  class=\"text_purchase3\"/></td>
							 <td ><input type=\"text\" size=\"15\" name=".$margin." id=".$margin."  class=\"text_purchase3\" disabled=\"disabled\"/></td>
							 <td ><input type=\"text\" size=\"15\" name=".$subtotal." id=".$subtotal."  class=\"text_purchase3\" disabled=\"disabled\"/></td>
							</tr>";
							$i=$i+1; 
                           
				}			
							
                $ResponseXML .= "   </table>]]></sales_table>";
				$ResponseXML .= "<count><![CDATA[".$i."]]></count>";
				$ResponseXML .= " </salesdetails>";
				
		//	}	
				
				
				echo $ResponseXML;
				
			
	}
	
	
	if($_GET["Command"]=="setord")
	{
		
		include_once("connection.php");
		
		$len=strlen($_GET["salesord1"]);
		$need=substr($_GET["salesord1"],$len-7, $len);
		$salesord1=trim("ORD/ ").$_GET["salesrep"].trim(" / ").$need;
		
		
		$_SESSION["custno"]=$_GET['custno'];
		$_SESSION["brand"]=$_GET["brand"];
		$_SESSION["department"]=$_GET["department"];
		
		
	
	header('Content-Type: text/xml'); 
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
			
			$ResponseXML = "";
			$ResponseXML .= "<salesdetails>";
			
				$cuscode=$_GET["custno"];	
				$salesrep=$_GET["salesrep"];
				$brand=$_GET["brand"];
					
			//		$ResponseXML .= "<salesord><![CDATA[".$salesord1."]]></salesord>";
				
					
	    //Call SETLIMIT ====================================================================
		
		
		
	/*	$sql = $dbinv->query("DROP VIEW view_s_salma") or die("Error";
					$sql = mysql_query("CREATE VIEW view_s_salma
AS
SELECT     s_salma.*, brand_mas.class AS class
FROM         brand_mas RIGHT OUTER JOIN
                      s_salma ON brand_mas.barnd_name = s_salma.brand") or die("Error";*/

	$OutpDAMT = 0;
	$OutREtAmt = 0;
	$OutInvAmt = 0;
	$InvClass="";
	
	$sqlclass = $dbinv->query("select class from brand_mas where barnd_name='".trim($brand)."'") or die("Error";
	if ($rowclass = $sqlclass->fetch()){
		if (is_null($rowclass["class"])==false){
			$InvClass=$rowclass["class"];
		}
	}
	
	$sqloutinv = $dbinv->query("select sum(GRAND_TOT-TOTPAY) as totout from view_s_salma where GRAND_TOT>TOTPAY and CANCELL='0' and C_CODE='".trim($cuscode)."' and SAL_EX='".trim($salesrep)."' and class='".$InvClass."'") or die("Error";
	if ($rowoutinv = $sqloutinv->fetch()){
		if (is_null($rowoutinv["totout"])==false){
			$OutInvAmt=$rowoutinv["totout"];
		}
	}

$sqlinvcheq = $dbinv->query("SELECT * FROM s_invcheq WHERE che_date>'".date("d-m-Y")."' AND cus_code='".trim($cuscode)."' and trn_type='REC' and sal_ex='".trim($salesrep)."'") or die("Error";
	while($rowinvcheq = $sqlinvcheq->fetch()){
		
		$sqlsttr = $dbinv->query("select * from s_sttr where ST_REFNO='".trim($rowinvcheq["refno"])."' and ST_CHNO ='".trim($rowinvcheq["cheque_no"]) ."'") or die("Error";
		while($rowsttr = $sqlsttr->fetch()){
			$sqlview_s_salma = $dbinv->query("select class from view_s_salma where REF_NO='".trim($rowsttr["ST_INVONO"])."'") or die("Error";
			if($rowview_s_salma = $sqlview_s_salma->fetch()){
				
				if (trim($rowview_s_salma["class"]) == $InvClass){
                    $OutpDAMT = $OutpDAMT + $rowsttr["ST_PAID"];
                }
			}
		}
	}


        
        $pend_ret_set = 0;
		
		$sqlinvcheq = $dbinv->query("SELECT sum(che_amount) as  che_amount FROM s_invcheq WHERE che_date >'".date("d-m-Y")."' AND cus_code='".trim($cuscode)."' and trn_type='RET'") or die("Error";
		if($rowinvcheq = $sqlinvcheq->fetch()){
			if (is_null($rowinvcheq["che_amount"])==false){
				$pend_ret_set=$rowinvcheq["che_amount"];
			}
		}
		
		
		$sqlcheq = $dbinv->query("Select sum(CR_CHEVAL-PAID) as tot from s_cheq where CR_C_CODE='".trim($cuscode)."'  and CR_CHEVAL-PAID>0 and CR_FLAG='0' and S_REF='".trim($salesrep)."'") or die("Error";
		if($rowcheq = $sqlcheq->fetch()){
			if (is_null($rowcheq["tot"])==false){
				$OutREtAmt=$rowcheq["tot"];
			} else {
				$OutREtAmt=0;
			}
		}
		
            
 
$ResponseXML .= "<sales_table_acc><![CDATA[ <table  bgcolor=\"#0000FF\" border=1  cellspacing=0>
						<tr><td>Outstanding Invoice Amount</td><td>".number_format($OutInvAmt, 2, ".", ",")."</td></tr>
						 <td>Return Cheque Amount</td><td>".number_format($OutREtAmt, 2, ".", ",")."</td></tr>
						 <td>Pending Cheque Amount</td><td>".number_format($OutpDAMT, 2, ".", ",")."</td></tr>
						 <td>PSD Cheque Settlments</td><td>".number_format($pend_ret_set, 2, ".", ",")."</td></tr>
						 <td>Total</td><td>".number_format($OutInvAmt+$OutREtAmt+$OutpDAMT+$pend_ret_set, 2, ".", ",")."</td></tr>
						 </table></table>]]></sales_table_acc>";
        

      $sqlbr_trn = $dbinv->query("select * from br_trn where Rep='".trim($salesrep)."' and brand='".trim($InvClass)."' and cus_code='" .trim($cuscode)."'") or die("Error";  
	if($rowbr_trn = $sqlbr_trn->fetch()){
		if(is_null($rowbr_trn["credit_lim"]) == false){
			$crLmt=$rowbr_trn["credit_lim"];
		} else {	
			$crLmt=0;		
		}
	
		
		if(is_null($rowbr_trn["tmpLmt"]) == false){
			$tmpLmt=$rowbr_trn["tmpLmt"];
		} else {	
			$tmpLmt=0;		
		}
		
		if (is_null($rowbr_trn["CAT"])==false) {
			$cuscat = trim($rowbr_trn["cat"]);
            if ($cuscat = "A"){ $m = 2.5; }
            if ($cuscat = "B"){ $m = 2.5; }
            if ($cuscat = "C"){ $m = 1; }
            $txt_crelimi = "0";
            $txt_crebal = "0";
            $txt_crelimi = number_format($crLmt, 2, ".", ",");
			$crebal=$crLmt * $m + $tmpLmt - $OutInvAmt - $OutREtAmt - $OutpDAMT - $pend_ret_set;
            $txt_crebal = number_format($crebal, "2", ".", ",");
          } else {
            $txt_crelimi = "0";
            $txt_crebal = "0";
          }
         $creditbalance = $OutInvAmt + $OutREtAmt + $OutpDAMT;

	   			
			
			 
    }    
			$ResponseXML .= "<txt_crelimi><![CDATA[".$txt_crelimi."]]></txt_crelimi>";
			$ResponseXML .= "<txt_crebal><![CDATA[".$txt_crebal."]]></txt_crebal>";
          
         	 $ResponseXML .= "<creditbalance><![CDATA[".$creditbalance."]]></creditbalance>";


		$ResponseXML .= "</salesdetails>";	
		echo $ResponseXML;
				
				
	
	
	}

		
	if($_GET["Command"]=="del_item")
		{
		
			
			$ResponseXML .= "";
			$ResponseXML .= "<salesdetails>";
			
	
			$sql="delete from tmp_purord_data where str_code='".$_GET['code']."' and str_invno='".$_GET['invno']."' ";
			
			$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\">Part No</font></td>
                              <td width=\"100\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
                            </tr>";
							
			
			$sql="Select * from tmp_purord_data where str_invno='".$_GET['invno']."'";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$ResponseXML .= "<tr>
                              
                             <td  >".$row['str_code']."</a></td>
							 <td  >".$row['str_description']."</a></td>
							 <td  >".$row['partno']."</a></td>
							 <td  >".$row['qty']."</a></td>
							 <td  ><img src=\"images/delete_01.png\" width=\"20\" height=\"20\" id=".$row['str_code']."  name=".$row['str_code']." onClick=\"del_item('".$row['str_code']."');\"></td></tr>";
							 
						
				}			
				
				$ResponseXML .= "   </table>]]></sales_table>";
							
                $ResponseXML .= " </salesdetails>";
				
				
		//	}	
				
				
				echo $ResponseXML;
				
			
	}
	

	
	if ($_GET['Command']=="update_arstat") {
			
			$sql = "update s_trn set seri_no='" . $_GET['arpend'] . "' where refno = '" . $_GET['refno'] . "'";
			$result = $db->RunQuery($sql);
			echo "Updated";
			
	}	
	
if($_POST["Command"]=="save_item")
{
    
  if ($_SESSION['dev']==""){
  	exit("logout");
  }
  
   if ($_POST["count"]>0){
   	
	$sql_invpara="SELECT * from invpara";
	$result_invpara =$db->RunQuery($sql_invpara);
	$row_invpara = $result_invpara->fetch();
			
	$vatrate=$row_invpara["vatrate"];
	
	$ResponseXML .= "";
	$ResponseXML .= "<salesdetails>";
			
			$_SESSION["CURRENT_DOC"] = 1;      //document ID
			$_SESSION["VIEW_DOC"] = false ;     //view current document
			$_SESSION["FEED_DOC"] = true;       //save  current document
			$_GET["MOD_DOC"] = false  ;         //delete   current document
			$_GET["PRINT_DOC"] = false  ;       //get additional print   of  current document
			$_GET["PRICE_EDIT"] = false ;       //edit selling price
			$_GET["CHECK_USER"] = false ;       //check user permission again

			
			$sql="select * from s_purmas where REFNO='".$_POST["invno"]."'";
			$result =$db->RunQuery($sql);
			//echo $sql;
			if($row = $result->fetch()){
				exit("AR Number Already Exists");
			} else {
			    
			    //Working A.V
				$sql1="insert into c_bal(REFNO, SDATE, CUSCODE, AMOUNT, BALANCE, DEP, SAL_EX, Cancell, brand, DEV, trn_type, vatrate, old, flag1, active, totpay) values ('".$_POST["invno"]."', '".$_POST["invdate"]."', '".$_POST["supplier_code"]."', '".$_POST["total_value"]."', '".$_POST["total_value"]."', '".$_POST["department"]."', '".$_POST["salesrep"]."', '0', '".$_POST["brand"]."', '".$_SESSION['dev']."', 'LARN', '".$vatrate."', '0', 0, 1, 0)";
				//echo $sql1;
				$result1 =$db->RunQuery($sql1);
				
				///Working A.V
				$sql1="insert into s_sttr_all(ST_REFNO, ST_DATE, ST_INVONO, ST_PAID, balance, netamount, cus_code, cusname, DEV, del_days, deliin_days, deliin_amo, deliin_lock, department, form_type, trn_type) values
	             ('".$_POST["invno"]."', '".$_POST["invdate"]."', '".$_POST["invno"]."', ".$_POST["total_value"].", ".$_POST["total_value"].", ".(-1*$_POST["total_value"]).", '".$_POST["supplier_code"]."', '".$_POST["supplier_name"]."', '".$_SESSION['dev']."', 0, 0, 0, '0', 'O', 'LARN', 'OVER')";
		        //echo $sql1;
				$result1 =$db->RunQuery($sql1);
	            ///Working A.V
				$sql1="insert into s_purmas(REFNO, SDATE, ORDNO, LCNO, pi_no, COUNTRY, SUP_CODE, SUP_NAME, REMARK, DEPARTMENT, AMOUNT, PUR_DATE,
                TYPE, brand,vat,flag1) values ('".$_POST["invno"]."', '".$_POST["invdate"]."', '".$_POST["orderno1"]."', '".$_POST["lc_no"]."', '".$_POST["pi_no"]."', '".$_POST["country"]."', '".$_POST["supplier_code"]."', '".$_POST["supplier_name"]."', '".$_POST["textarea"]."', '".$_POST["department"]."', '".$_POST["total_value"]."', '".$_POST["dte_dor"]."', '".$_POST["purtype"]."', '".$_POST["brand"]."', '".$_POST["vatno1"]."', 'LARN')";
				//echo $sql1;
				$result1 =$db->RunQuery($sql1); 
				//Ashan
				$sql12="select * from tmp_arn_data where str_invno='".$_POST['invno']."'";
			    $result12 =$db->RunQuery($sql12);
		    	while($row12 = $result12->fetch()){
    			$sql123="insert into s_ordtrn(REFNO, SDATE, STK_NO, DESCRIPT, ORD_QTY, CANCEL, tmp_no,S_net,Vat2,S_gross,cost1,flag1) values 
    			('".$row12["str_invno"]."', '".$_POST["invdate"]."', '".$row12["item_code"]."','".$row12["itemdescript"]."','".$row12["qty1"]."','0', '".$_SESSION['tmp_no_arn']."', '".$row12['net']."', '".$row12['vat']."', '".$row12['gross']."', '".$row12['cost1']."', 'NS')";
				//echo $sql123.'w';
				$result123 =$db->RunQuery($sql123);
				
				
				
				
				
			
 		 	
			}
			}
		
		
		$sql124="select * from s_ordtrn where REFNO='".$_POST['invno']."'";
			    $result124 =$db->RunQuery($sql124);
		    	while($row124 = $result124->fetch()){
    			$sql111="insert into s_purtrn(REFNO, SDATE, STK_NO, DESCRIPT, COST, SELLING, DEPARTMENT,QTYINHAND, vatrate, cost_seling, cost_margin, CANCEL,flag1) values 
                ('".$row124["REFNO"]."', '".$_POST["invdate"]."', '".$row124["STK_NO"]."', '".$row124["DESCRIPT"]."', ".$row124['cost1'].", ".$row124['S_gross'].", '".$_POST["department"]."','".$row124["ORD_QTY"]."', '".$row124['Vat2']."', '0', '".$row124['S_net']."', '0', 'NS')";
 		 
				$result111 =$db->RunQuery($sql111);
		    	}
				
				// 	// $sql1="insert into s_purtrn(REFNO, SDATE, STK_NO, DESCRIPT, COST, MARGIN, SELLING, REC_QTY, FOB, DEPARTMENT, QTYINHAND, O_QTY,Cost_c, acc_cost, acc_cost_c, brand, vatrate, DISCOUNT, ret_qty, cost_seling, cost_margin, CANCEL, soldqty, days) values 
    //             //('".$_POST["invno"]."', '".$_POST["invdate"]."', '".$_POST[$itemcode_name]."', '".$_POST[$itemname_name]."', ".$cost.", ".$margin_name.", ".$_POST[$selling_name].", ".$_POST[$qty_name].", ".$fob_val.", '".$_POST["department"]."', ".$QTYINHAND.", ".$_POST[$ord_qty_name].", ".$COST_mas.", '".$acc_cost."', '".$acc_cost_c."', '".$_POST["brand"]."', '".$vatrate."', 0, 0, 0, 0, '0', '', 0)";
 		 //	$sql111="insert into s_purtrn(REFNO, SDATE, STK_NO, DESCRIPT, COST, SELLING, DEPARTMENT,QTYINHAND, vatrate, cost_seling, cost_margin, CANCEL) values 
    //             ('".$row12["str_invno"]."', '".$_POST["invdate"]."', '".$row12["item_code"]."', '".$row12["itemdescript"]."', ".$row12['cost1'].", ".$row12['gross'].", '".$_POST["department"]."','".$row12["qty1"]."', '".$row12['vat']."', '0', '".$row12['net']."', '0')";
 		 
				// $result111 =$db->RunQuery($sql111);
	
				$sql1="update s_ordmas set cancel='1' where REFNO='".$_POST["orderno1"]."'";
				$result1 =$db->RunQuery($sql1);
				
				$sql1="update s_ordtrn set CANCEL='1' where REFNO='".$_POST["orderno1"]."'";
				$result1 =$db->RunQuery($sql1);
			

				$sql1="insert into s_trn(STK_NO, SDATE, QTY, LEDINDI, REFNO, DEPARTMENT, seri_no, Dev, SAL_EX, ACTIVE, DONO) values ('".$_POST[$itemcode_name]."', '".$_POST["invdate"]."', '".$_POST[$qty_name]."', 'ARN', '".$_POST["invno"]."', '".$_POST["department"]."', '" . $_POST['arpend'] . "', '".$_SESSION['dev']."', '', '1', '')";
				//echo $sql1;
				$result1 =$db->RunQuery($sql1);

			  	
			
		
			
				$sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($_POST["invno"]) . "', '" . $_SESSION["CURRENT_USER"] . "', 'LARN', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
		$resul2 = $db->RunQuery($sql2);
		
			$sql1="update invpara set PORD_NO=PORD_NO+1 where COMCODE='".$_SESSION['company']."'";
			$result1 =$db->RunQuery($sql1);
			

			echo "Saved";
		} else {
			echo "no";
		}	
			
	}
	

	if($_GET["Command"]=="pass_arnno")
	{
		$ResponseXML = "";
		$ResponseXML .= "<salesdetails>";
		$sql="Select * from s_purmas where REFNO='".$_GET['arnno']."'";
		$result =$db->RunQuery($sql);	
		if ($row = $result->fetch()){
			$ResponseXML .= "<REFNO><![CDATA[".$row["REFNO"]."]]></REFNO>";
			$ResponseXML .= "<SDATE><![CDATA[".$row["SDATE"]."]]></SDATE>";
			$ResponseXML .= "<ORDNO><![CDATA[".$row["ORDNO"]."]]></ORDNO>";
			$ResponseXML .= "<LCNO><![CDATA[".$row["LCNO"]."]]></LCNO>";
			$ResponseXML .= "<pi_no><![CDATA[".$row["pi_no"]."]]></pi_no>";
			$ResponseXML .= "<COUNTRY><![CDATA[".$row["COUNTRY"]."]]></COUNTRY>";
			$ResponseXML .= "<SUP_CODE><![CDATA[".$row["SUP_CODE"]."]]></SUP_CODE>";
			$ResponseXML .= "<SUP_NAME><![CDATA[".$row["SUP_NAME"]."]]></SUP_NAME>";
			$ResponseXML .= "<REMARK><![CDATA[".$row["REMARK"]."]]></REMARK>";
			$ResponseXML .= "<DEPARTMENT><![CDATA[".$row["DEPARTMENT"]."]]></DEPARTMENT>";
			$ResponseXML .= "<AMOUNT><![CDATA[".$row["AMOUNT"]."]]></AMOUNT>";
			$ResponseXML .= "<PUR_DATE><![CDATA[".$row["PUR_DATE"]."]]></PUR_DATE>";
			$ResponseXML .= "<brand><![CDATA[".$row["brand"]."]]></brand>";
			$ResponseXML .= "<TYPE><![CDATA[".$row["TYPE"]."]]></TYPE>";
			
				$ResponseXML .= "<sales_table><![CDATA[ <table><tr>
                              <td width=\"200\"  background=\"\" ><font color=\"#FFFFFF\">Code</font></td>
                              <td width=\"800\"  background=\"\"><font color=\"#FFFFFF\">Description</font></td>
                              <td width=\"300\"  background=\"\"><font color=\"#FFFFFF\">Order Qty</font></td>
                              <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">FOB</font></td>
                              <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Qty</font></td>
							  <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Cost</font></td>
							   <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Selling</font></td>
							    <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Margin</font></td>
								 <td width=\"200\"  background=\"\"><font color=\"#FFFFFF\">Sub Total</font></td>
							
                            </tr>";
				
			$mcou=0;	
			$sql="Select count(*) as mcou from s_purtrn where REFNO='".$_GET['arnno']."'";
			$result =$db->RunQuery($sql);	
			$row = $result->fetch();
			$mcou=$row["mcou"]+1;
							
			$i=1;
			$tot=0;
			$sql="Select * from s_purtrn where REFNO='".$_GET['arnno']."'";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){	
			
				$itemcode="itemcode".$i;
				$itemname="itemname".$i;
				$ord_qty="ord_qty".$i;
				$fob="fob".$i;
				$qty="qty".$i;
				$cost="cost".$i;
				$selling="selling".$i;
				$margin="margin".$i;
				$subtotal="subtotal".$i;
							
				if (($row['REC_QTY'] > 0) and ($row['acc_cost'] > 0) and ($row['SELLING'] > 0)) {
					$stot = $row['REC_QTY']* $row['acc_cost'];
					$margine_val = ($row['SELLING'] - $row['acc_cost']) / $row['acc_cost'] * 100;

				} else {
					$stot="";
					$margine_val="";
				}	
							
             	$ResponseXML .= "<tr>                              
                             <td  ><input type=\"text\" size=\"15\" name=".$itemcode." id=".$itemcode."   value='".$row['STK_NO']."' class=\"text_purchase3\" disabled=\"disabled\"/></td>
							  <td  ><input type=\"text\" size=\"15\" name=".$itemname." id=".$itemname."  value='".$row['DESCRIPT']."' class=\"text_purchase3\" disabled=\"disabled\"/></td>
							  <td  ><input type=\"text\" size=\"15\" name=".$ord_qty." id=".$ord_qty."  value='".$row['O_QTY']."' class=\"text_purchase3_right\" disabled=\"disabled\"/></td>
							
							 <td  ><input type=\"text\" size=\"15\" name=".$fob." id=".$fob." value='".$row['FOB']."'  class=\"text_purchase3\"/></td>
							 <td  ><input type=\"text\" size=\"15\" name=".$qty." id=".$qty." value='".$row['REC_QTY']."'  class=\"text_purchase3_right\" onBlur=\"cal_subtot('".$i."', '".$mcou."');\"/></td>
							 <td  ><input type=\"text\" size=\"15\" name=".$cost." id=".$cost." value='".$row['acc_cost']."'  class=\"text_purchase3_right\" onBlur=\"cal_subtot('".$i."', '".$mcou."');\"/></td>
							 <td  ><input type=\"text\" size=\"15\" name=".$selling." id=".$selling." value='".$row['SELLING']."'  class=\"text_purchase3_right\"/></td>
							 <td  ><input type=\"text\" size=\"15\" name=".$margin." id=".$margin." value='".$margine_val."'  class=\"text_purchase3_right\" disabled=\"disabled\"/></td>
							 <td  ><input type=\"text\" size=\"15\" name=".$subtotal." id=".$subtotal." value='".$stot."'  class=\"text_purchase3_right\" disabled=\"disabled\"/></td>
							</tr>";
							//$tot=$tot+($row['COST']*$row['REC_QTY']);
							$subtot=$subtot+$stot;
							$i=$i+1; 
                           
				}	


$sql = "select * from s_trn where refno = '" . $_GET['arnno'] . "' limit 1";
		$result = $db->RunQuery($sql);	
		$row = $result->fetch();
		
						
							
                $ResponseXML .= "</table>]]></sales_table>";
				$ResponseXML .= "<arpend><![CDATA[" . $row['seri_no'] . "]]></arpend>";
				$ResponseXML .= "<tot><![CDATA[".$tot."]]></tot>";
				$ResponseXML .= "<subtot><![CDATA[".number_format($subtot, 2, ".", ",")."]]></subtot>";
				$ResponseXML .= "<count><![CDATA[".$i."]]></count>";
		}
		
		
		
		$ResponseXML .= "</salesdetails>";
		echo $ResponseXML;
	}
	
if($_GET["Command"]=="pass_arnno_gin")
	{
		header('Content-Type: text/xml'); 
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		$ResponseXML = "";
		$ResponseXML .= "<salesdetails>";
		$sql="Select * from s_purmas where REFNO='".$_GET['arnno']."'";
		$result =$db->RunQuery($sql);	
		if ($row = $result->fetch()){
			$ResponseXML .= "<REFNO><![CDATA[".$row["REFNO"]."]]></REFNO>";
			$ResponseXML .= "<SDATE><![CDATA[".$row["SDATE"]."]]></SDATE>";
			
		}
		$ResponseXML .= "</salesdetails>";
		echo $ResponseXML;
	}
	
				
				
				
				
if($_GET["Command"]=="cancel_inv")
{

	$sql="select * from s_purmas where CANCEL='0' order by id desc";
	$result =$db->RunQuery($sql);
	$row = $result->fetch();
	//echo $sql;
	//if ($row["REFNO"]!=$_GET["invno"]){
	//	echo "You Can't cancel this record without cancel last records ";
	//} else {	
		$sql1="update s_purmas set CANCEL='1' where REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
	
		$sql1="update s_purtrn set CANCEL='1' where REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
		
		$sql1="update s_ordmas set cancel='0' where REFNO='".$_GET["orderno1"]."'";
		$result1 =$db->RunQuery($sql1);
	
		$sql1="update s_ordtrn set CANCEL='0' where REFNO='".$_GET["orderno1"]."'";
		$result1 =$db->RunQuery($sql1);
		
		$sql1="delete from s_trn  where REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
		
		$sql1="delete from s_trn_all  where REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
		
		$sql1="delete from c_bal where REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
		
		$sql1="delete from s_sttr_all where ST_REFNO='".$_GET["invno"]."'";
		$result1 =$db->RunQuery($sql1);
		
		
		$sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . trim($_POST["invno"]) . "', '" . $_SESSION["CURRENT_USER"] . "', 'ARN', 'Delete', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
		$resul2 = $db->RunQuery($sql2);
		
		  
// 		$sql1="select * from s_purtrn where REFNO='".$_GET["invno"]."'";
		
// 		$result1 =$db->RunQuery($sql1);
// 		while($row1 = $result1->fetch()){
		
// 			$sql2="update s_mas set COST=".$row1["Cost_c"].", acc_cost=".$row1["acc_cost_c"]." where STK_NO='".$row1["STK_NO"]."'";
// 			$result2 =$db->RunQuery($sql2);
			
// 			$sql2="update s_submas set QTYINHAND=QTYINHAND-".$row1["REC_QTY"]." where STK_NO='".$row1["STK_NO"]."' and STO_CODE='".$_GET["department"]."'";
			
// 			$result2 =$db->RunQuery($sql2);
			
// 			$sql2="update s_mas set QTYINHAND=QTYINHAND-".$row1["REC_QTY"]." where STK_NO='".$row1["STK_NO"]."'";
// 			$result2 =$db->RunQuery($sql2);
// 		}
		
		echo "Canceled!";
	//}
	
	
	
}	
	
if ($_GET["Command"]=="check_print")
{
	
	echo $_SESSION["print"];
}

	
if($_GET["Command"]=="tmp_crelimit")
{	
	echo "abc";
	$crLmt = 0;
	$cat = "";
	
	$rep=trim(substr($_GET["Com_rep1"], 0, 5));
	
	$sql = "select * from br_trn where rep='".$rep."' and cus_code='".trim($_GET["txt_cuscode"])."' and brand='".trim($_GET["cmbbrand1"])."'";
	echo $sql;
	$result =$db->RunQuery($sql);
    if ($row = $result->fetch()) {
		$crLmt = $row["credit_lim"];
   		If (is_null($row["CAT"])==false) {
      		$cat = trim($row["CAT"]);
   		} else {
      		$crLmt = 0;
		}	
   	}
/*	
$_SESSION["CURRENT_DOC"] = 66     //document ID
//$_SESSION["VIEW_DOC"] = true      //  view current document
 $_SESSION["FEED_DOC"] = true      //  save  current document
//$_SESSION["MOD_DOC"] = true       //   delete   current document
//$_SESSION["PRINT_DOC"] = true     // get additional print   of  current document
//$_SESSION["PRICE_EDIT"]= true     // edit selling price
$_SESSION["CHECK_USER"] = true    // check user permission again
$crLmt = $crLmt;
setlocale(LC_MONETARY, "en_US");
$CrTmpLmt = number_format($_GET["txt_tmeplimit"], 2, ".", ",");

$REFNO = trim($_GET["txt_cuscode"]) ;

$AUTH_USER="tmpuser";

$sql = "insert into tmpCrLmt (sdate, stime, username, tmpLmt, class, rep, cuscode, crLmt, cat) values 
        ('".date("Y-m-d")."','".date("H:i:s", time())."' ,'".$AUTH_USER."',".$CrTmpLmt." ,'".trim($_GET["cmbbrand1"])."','".$rep."','".trim($_GET["txt_cuscode"])."',".$crLmt.",'".$cat"' )";
$result =$db->RunQuery($sql);

$sql = "select * from  br_trn where cus_code='".trim($_GET["txt_cuscode"])."' and rep='".$rep."' and brand='".$_GET["cmbbrand1"]."'";
$result =$db->RunQuery($sql);
if ($row = $result->fetch()) {
   $sqlbrtrn= "insert into br_trn (cus_code, rep, credit_lim, brand, tmplmt) values ('".trim($_GET["txt_cuscode"])."','".$rep."','0','".trim($_GET["cmbbrand1"])."',".$CrTmpLmt." )";
   $resultbrtrn =$db->RunQuery($sqlbrtrn);
   
} else {
  
  	$sqlbrtrn= "update br_trn set tmplmt= ".$CrTmpLmt."  where cus_code='".trim($_GET["txt_cuscode"])."' and rep='".$rep."' and brand='".$_GET["cmbbrand1"]."' ";
 	$resultbrtrn =$db->RunQuery($sqlbrtrn);
	
//	$sqlbrtrn= "update vendor set temp_limit= ".$CrTmpLmt."  where code='".trim($_GET["txt_cuscode"])."' "
}

	If ($_GET["Check1"] == 1) {
   		$sqlblack= "update vendor set blacklist= '1'  where code='".trim($_GET["txt_cuscode"])."' ";
		$resultblack =$db->RunQuery($sqlblack);
	} else {	
    	$sqlblack= "update vendor set blacklist= '0'  where code='".trim($_GET["txt_cuscode"])."' ";
		$resultblack =$db->RunQuery($sqlblack);
	}

echo "Tempory limit updated";*/

}
if($_GET["Command"]=="add_tmp_pur")
		{
		
			
			$ResponseXML .= "";
			$ResponseXML .= "<salesdetails>";
			
			
			$sql="delete from tmp_arn_data where item_code='".$_GET['item_code']."' and str_invno='".$_GET['invno']."' and tmp_no='".$_SESSION["tmp_no_arn"]."'";
			 //echo $sql.'d';
			$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_arn"]."') ";
			//$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\">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;
			$vattmp=0;
			$i=0;
			$sql="Select * from tmp_arn_data where str_invno='".$_GET['invno']."'and tmp_no='".$_SESSION["tmp_no_arn"]."'";
			$result =$db->RunQuery($sql);	
			while($row = $result->fetch()){				
             	$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+$row['gross'];
							 $vattmp=$vattmp+$row['vat'];
							 ////echo $tot;
							 //	$ResponseXML .= "<tot><![CDATA[".$tot."]]></tot>";
                           	$i=$i+1;	
				}			
						
                $ResponseXML .= "   </table>]]></sales_table>";
				$ResponseXML .= "<item_count><![CDATA[".$i."]]></item_count>";
				$ResponseXML .= "<total_value><![CDATA[".$tot."]]></total_value>";
				$ResponseXML .= "<total_vattmp><![CDATA[".$vattmp."]]></total_vattmp>";
				$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;
            $x = ($_GET["gross"] *100)/108 ; 
            
            
        // } 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;
}

	
?>

