<?php

session_start();


require_once ("connection_sql.php");

header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');
if ($_GET["Command"] == "pass_quot") {
    $_SESSION["custno"] = $_GET['custno'];

    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

    $ResponseXML = "";
    $ResponseXML .= "<salesdetails>";

    $cuscode = $_GET["custno"];

    $sql = "Select * from s_salma where   REF_NO ='" . $cuscode . "'";
    $result = $conn->query($sql);

    if ($rowM = $result->fetch()) {
        $ResponseXML .= "<id><![CDATA[" . json_encode($rowM) .  "]]></id>";
    }
    
    $ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered\" style=\"width:100%\">
    <tr class='info'>
    <td>Invoice Item</td>
    <td>Change Item</td>
    <td>Invoice Qty</td>
    <td>Change Qty</td>
    </tr>
    ";

    $i = 0;
     
    $sql = "Select * from s_invo where REF_NO='" . $cuscode. "'  and CANCELL='0'";
    foreach ($conn->query($sql) as $row) {
        
        $item = "item" . $i; 
        $item1 = "item1" . $i;
        $qty = "qty" . $i;
        $qtyy = "qtyy" . $i; 
                        
         
        $ResponseXML .= "<tr>      
            <td class='col-md-2'><input type=\"text\"   class=\"form-control  input-sm\" id=\"" . $item . "\" name=\"" . $item . "\"   value=\"" . $row['STK_NO'] . "\" disabled/></td>
            <td class='col-md-2'><input type=\"text\"   class=\"form-control  input-sm\" id=\"" . $item1 . "\" name=\"" . $item1 . "\"   value=\"" . $row['STK_NO'] . "\"  /></td>
            <td class='col-md-2'><input type=\"text\"   class=\"form-control  input-sm\" id=\"" . $qty . "\" name=\"" . $qty . "\"   value=\"" . $row['QTY'] . "\" disabled/></td>
            <td class='col-md-2'><input type=\"text\"   class=\"form-control  input-sm\" id=\"" . $qtyy . "\" name=\"" . $qtyy . "\"   value=\"" . $row['QTY'] . "\"  /></td> 
             
        </tr>";
 
        $i = $i + 1; 
    } 

    $ResponseXML .= "   </table>]]></sales_table>";
    $ResponseXML .= "<mcount><![CDATA[" .$i. "]]></mcount>";

     

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

if ($_GET['Command']=="uprep")
{
  try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    $sql = "select * from s_salma where REF_NO ='" . $_GET['invno'] . "'   and CANCELL='0'"; 
    $result = $conn->query($sql);
      if ($rowmain = $result->fetch()) {
        $sql = "update s_salma set CUS_NAME='".$_GET['cusname']."' ,type1 ='" . $_GET['paytypeto'] . "',SDATE ='" . $_GET['sdate1'] . "',sdate1 ='" . $_GET['sdate1'] . "',C_CODE ='" . $_GET['cuscode1'] . "',TEL ='" . $_GET['con1'] . "'   where REF_NO ='" . $_GET['invno'] . "'   and CANCELL='0'";
        $result = $conn->query($sql);
        
        $sql = "update s_invo set  SDATE ='" . $_GET['sdate1'] . "', type1 ='" . $_GET['paytypeto'] . "',sdate1 ='" . $_GET['sdate1'] . "',c_code ='" . $_GET['cuscode1'] . "'   where REF_NO ='" . $_GET['invno'] . "'   and CANCELL='0'";
        $result = $conn->query($sql);
        
        $sql = "update s_trn set SDATE ='" . $_GET['sdate1'] . "',sdate1 ='" . $_GET['sdate1'] . "'  where REFNO ='" . $_GET['invno'] . "'  ";
        $result = $conn->query($sql);
        
        
        $sql = "update s_led set   SDATE ='" . $_GET['sdate1'] . "',C_CODE ='" . $_GET['cuscode1'] . "'    where REF_NO ='" . $_GET['invno'] . "'  ";
        $result = $conn->query($sql);
        
        $sql1 = "select * from s_crec where INVNO ='" . $_GET['invno'] . "'  and  CANCELL='0'"; 
        $result1 = $conn->query($sql1);
        if ($row1 = $result1->fetch()) {
             $sql = "update s_crec set  CA_CODE ='" . $_GET['cuscode1'] . "',CA_DATE ='" . $_GET['sdate1'] . "'    where INVNO ='" . $_GET['invno'] . "'  and  CANCELL='0'"; 
            $result = $conn->query($sql);
            
            $sql = "update s_sttr set  cus_code ='" . $_GET['cuscode1'] . "',ST_DATE ='" . $_GET['sdate1'] . "',st_chdate ='" . $_GET['sdate1'] . "'    where ST_INVONO ='" . $_GET['invno'] . "'  ";
            $result = $conn->query($sql);
        }
        
        
        $i = 0;  
		while ($_GET["mcount"] > $i) {	
		    
     
            $item = "item" . $i; 
            $item1 = "item1" . $i;
            $qty = "qty" . $i;
            $qtyy = "qtyy" . $i;
            
            $sqlmas = "select * from s_mas where STK_NO='" . $_GET[$item1] . "'";
            $resultmas = $conn->query($sqlmas);
            $rowmas = $resultmas->fetch();
             
            
            $sql = "update s_invo set  STK_NO ='" . $_GET[$item1] . "',QTY ='" . $_GET[$qtyy] . "',DESCRIPT='".$rowmas['DESCRIPT']."'    where REF_NO ='" . $_GET['invno'] . "'   and CANCELL='0' and STK_NO ='" . $_GET[$item] . "'";
           $result = $conn->query($sql);
            
            $sql = "update s_trn set  STK_NO ='" . $_GET[$item1] . "',QTY ='" . $_GET[$qtyy] . "'    where REFNO ='" . $_GET['invno'] . "'     and STK_NO ='" . $_GET[$item] . "'";
            $result = $conn->query($sql);
            
            $sqls_mas = "update s_mas set QTYINHAND= QTYINHAND+" . $_GET[$qty] . " where STK_NO='" . $_GET[$item] . "'";
            $results_mas = $conn->query($sqls_mas);

            $sqls_submas = "update s_submas set QTYINHAND=QTYINHAND+ " . $_GET[$qty] . " where STK_NO= '" . $_GET[$item] . "' and STO_CODE= '" . $rowmain["DEPARTMENT"]  . "'";
            $results_submas = $conn->query($sqls_submas);
            
            
            $sqls_mas = "update s_mas set QTYINHAND= QTYINHAND-" . $_GET[$qtyy] . " where STK_NO='" . $_GET[$item1] . "'";
            $results_mas = $conn->query($sqls_mas);

            $sqls_submas = "update s_submas set QTYINHAND=QTYINHAND- " . $_GET[$qtyy] . " where STK_NO= '" . $_GET[$item1] . "' and STO_CODE= '" . $rowmain["DEPARTMENT"]  . "'";
            $results_submas = $conn->query($sqls_submas);
            
            
             
            $i = $i + 1;
        }
        
        $sql2s = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET["invno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'INVOICE', 'REP CHANGE', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $results2s = $conn->query($sql2s);
        
        echo "Updated";
        $conn->commit();
        
    } else{
        echo "No Result Found...";
    }
   
  
  } catch (Exception $e) {
      $conn->rollBack();
      echo $e;
  }
}

?>