<?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 .= "</salesdetails>";
   echo $ResponseXML;
}

if ($_GET["Command"] == "pass_quot1") {
    $_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_mas where   STK_NO ='" . $cuscode . "'";
    $result = $conn->query($sql);

    if ($rowM = $result->fetch()) {
        $ResponseXML .= "<id><![CDATA[" . json_encode($rowM) .  "]]></id>";
    }

     

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

if ($_GET['Command']=="uprep")
{
  try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    $sql = "select * from s_mas where STK_NO ='" . $_GET['stkno'] . "' "; 
    $result = $conn->query($sql);
      if ($row = $result->fetch()) {
        
        $sql = "update s_mas set th1 ='" . $_GET['th1'] . "',th2 ='" . $_GET['th2'] . "',th3 ='" . $_GET['th3'] . "',th4 ='" . $_GET['th4'] . "' where  STK_NO ='" . $_GET['stkno'] . "' "; 
        $result = $conn->query($sql);

         $sql2s = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values ('" . $_GET["stkno"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'Thread Up', 'Update', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $results2s = $conn->query($sql2s);

         $conn->commit();
        echo "Updated";
    } else{
        echo "No Result Found...";
    }
   
  
  } catch (Exception $e) {
      $conn->rollBack();
      echo $e;
  }
}

?>