<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once ("./connection_sql.php");

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

date_default_timezone_set('Asia/Colombo');

if ($_GET["Command"] == "new_inv") {
    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

    $ResponseXML = "";
    $ResponseXML .= "<new>";
    
     $COMPANY=$_SESSION['company']; 
     
    $sql = "select INQUIRY from invpara where flag='".$COMPANY."'"; 
    $result = $conn->query($sql);
    $row = $result->fetch();
    
    $tmpinvno = "000000" . $row["INQUIRY"] ;
    $lenth = strlen($tmpinvno);
    //  . "/" . $_GET["salesrep"]
    $refno = trim("".$COMPANY."/INQ/") . substr($tmpinvno, $lenth - 6);
        
         
    
    $uniq = uniqid();
    $ResponseXML .= "<id><![CDATA[$refno]]></id>";
    $ResponseXML .= "<sdate><![CDATA[". date("Y-m-d")."]]></sdate>"; 
    $ResponseXML .= "<uniq><![CDATA[$uniq]]></uniq>";
    $ResponseXML .= "</new>";

    echo $ResponseXML;
}

if ($_GET["Command"] == "save_inv") {

    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        
        $sql = "SELECT * from s_inquiry where REFNO  = '" . $_GET['refno'] . "'";
        $sql = $conn->query($sql);
        if ($row = $sql->fetch()) {
            exit("Already Saved..!!!");
        }

       $sql = "select INQUIRY from invpara where flag='".$_SESSION['company']."'"; 
    $result = $conn->query($sql);
    $row = $result->fetch();
    
    $tmpinvno = "000000" . $row["INQUIRY"] ;
    $lenth = strlen($tmpinvno);
    //  . "/" . $_GET["salesrep"]
    $refno = trim("".$_SESSION['company']."/INQ/") . substr($tmpinvno, $lenth - 6);
        
        
 

        $sqlbrand = "insert into s_inquiry(REFNO,SDATE,REP,NAME,COMPANY,VEHICLE,TEL1,TEL2,ADDRESS,NOTE,USER,USERDTIME,SIZE,BRAND) values 
        ('" . $refno . "','" . $_GET['sdate'] . "', '" . $_GET['rep'] . "', '" . $_GET['name'] . "', '" . $_SESSION['company'] . "', '" . $_GET['vehicle'] . "', '" . $_GET['tel1'] . "', '" . $_GET['tel2'] . "', '" . $_GET['address'] . "', '" . $_GET['note'] . "','" . $_SESSION["CURRENT_USER"] . "', '" . date("Y-m-d H:i:s") . "', '" . $_GET['size'] . "', '" . $_GET['brand'] . "')";
        $resultbrand = $conn->query($sqlbrand);

        $sqlbrand1 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
        ('" . $refno . "', '" . $_SESSION["CURRENT_USER"] . "', 'Inuqiry ', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
        $resultbrand1 = $conn->query($sqlbrand1);

        
        $sql = "update invpara set INQUIRY =INQUIRY+1 where flag='".$_SESSION['company']."'"; 
        $result = $conn->query($sql);
        echo "Saved";
        $conn->commit();
     
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}


 if ($_GET["Command"] == "pass_quot") { 

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

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

    $cuscode = $_GET["custno"];

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

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

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


 

if ($_GET["Command"] == "cancel_inv") {


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        $sql = "SELECT * from s_inquiry where  REFNO='".$_GET['refno']."' and CANCELL='1'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Canceled Inquiry...!!!");
        }
        $sql1 = "SELECT * from s_inquiry where  REFNO='".$_GET['refno']."' ";
        $result1 = $conn->query($sql1);
        if (!$row1 = $result1->fetch()) { 
             exit("No Result Of Inquiry...!!!");
        } 
        
    
        
        $sql = "SELECT * from s_inquiry where REFNO ='" . $_GET['refno'] . "'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            $sql = "update s_inquiry set CANCELL='1' where REFNO = '" . $row['REFNO'] . "'";
            $conn->exec($sql);
 
            
            $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values
            ('" . $_GET['refno'] . "', '" . $_SESSION["CURRENT_USER"] . "', 'INQUIRY', 'CANCELL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
            $resultlog = $conn->query($sqllog);  
            
            echo "Canceled"; 
        }

        $conn->commit();
        

    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}


if ($_GET["Command"] == "upcl_inv") {


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        $sql = "SELECT * from s_inquiry where  REFNO='".$_GET['refno']."' and CANCELL='1'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            exit("Already Canceled Inquiry...!!!");
        }
        $sql1 = "SELECT * from s_inquiry where  REFNO='".$_GET['refno']."' ";
        $result1 = $conn->query($sql1);
        if (!$row1 = $result1->fetch()) { 
             exit("No Result Of Inquiry...!!!");
        } 
        
   
        
       
         
        
        $sql = "SELECT * from s_inquiry where REFNO ='" . $_GET['refno'] . "'";
        $result = $conn->query($sql);
        if ($row = $result->fetch()) {
            if($row['REMIND1']==""){
                $sql = "update s_inquiry set REMIND1='".$_SESSION["CURRENT_USER"]."-".date("Y-m-d H:i:s")."' where REFNO = '" . $row['REFNO'] . "'";
                $conn->exec($sql);
            }else if($row['REMIND2']==""){
                $sql = "update s_inquiry set REMIND2='".$_SESSION["CURRENT_USER"]."-".date("Y-m-d H:i:s")."' where REFNO = '" . $row['REFNO'] . "'";
                $conn->exec($sql);
            }else if($row['REMIND3']==""){
                $sql = "update s_inquiry set REMIND3='".$_SESSION["CURRENT_USER"]."-".date("Y-m-d H:i:s")."' where REFNO = '" . $row['REFNO'] . "'";
                $conn->exec($sql);
            }else if($row['REMIND4']==""){
                $sql = "update s_inquiry set REMIND4='".$_SESSION["CURRENT_USER"]."-".date("Y-m-d H:i:s")."' where REFNO = '" . $row['REFNO'] . "'";
                $conn->exec($sql);
            }else if($row['REMIND5']==""){
                $sql = "update s_inquiry set REMIND5='".$_SESSION["CURRENT_USER"]."-".date("Y-m-d H:i:s")."' where REFNO = '" . $row['REFNO'] . "'";
                $conn->exec($sql);
            }else { 
                exit("Can't Update...!!!");
            }
            
 
            
            $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values
            ('" . $_GET['refno'] . "', '" . $_SESSION["CURRENT_USER"] . "', 'INQUIRY CL UPDATE', 'CANCELL', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
            $resultlog = $conn->query($sqllog);  
            
            echo "Cl Updated"; 
        }

        $conn->commit();
        

    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}
?>