using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Data;
using System.Web.Services;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using static iMESCore.Base.iMESComXML;
using static iMESCore.Base.iMESConst;
using static iMESCore.Base.iMESComSubroutine;
namespace wsSXS
{
    public class wsTEST_14278
    {
        private MEStc_SXS.clsENT  objENT = new MEStc_SXS.clsENT();//
        private MEStc_SXS.clsTest_14278  objTest = new MEStc_SXS.clsTest_14278();//
        private kcSYS.clsSYSUserLog objSYS = new kcSYS.clsSYSUserLog();   // 宣告Sys的物件
        private System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();       // 用以讀取Xml字串
        private DateTime ArriveTime;              // 送達時間
        private string strIdentity;               // Identity XML字串
        private string strReturnValue;            // ReturnValue XML字串
        private string strException;              // Exception XML字串
        private string strResult;                 // Result XML字串
        private string strMessage;                // Message XML字串
        private iMESCore.Settings.AppSettings objSetting = new iMESCore.Settings.AppSettings();
        private string strResourceDir = "Resources";

        public wsTEST_14278()
        {
            try
            {
                strResourceDir = System.IO.Path.Combine("wsUpdateResource", objSetting["ResourceDir"].ToString());
            }
            catch (Exception)
            { }
        }

        #region --- Subcontractor 外包商 ---

        [WebMethod(Description = "Load Subcontractor Data")]
        public string LoadSubcontractor(string InXml)
        {
            string LoadSubcontractorRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            string SubcontractorNo = defString;
            int IssueState = 2;

            ArriveTime = DateTime.Now;
            string AdditionalXml = "";
            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 判斷是否有傳入Subcontractor,若有,表示要依Subcontractor查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // 判斷是否有傳入IssueState,若有,表示要依IssueState查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                }

                // 呼叫副程式處理查詢條件與Additional格式
                AdditionalXml = CombineXMLQueryAdditional(xmlDoc);

                // 呼叫Dll執行
                strReturnValue = objENT.LoadSubcontractor(SubcontractorNo, IssueState, AdditionalXml);

                strException = "";
                strResult = "success";
            }

            catch (iMESException.MESException ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Show Subcontractor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            catch (Exception ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(defWSErrCode, ex.Message, "Show Subcontractor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                LoadSubcontractorRet = CombineXMLResponse(strIdentity, strReturnValue, strException, strResult, "");

            }

            return LoadSubcontractorRet;

        }

        [WebMethod(Description = "Add Subcontractor data")]
        public string AddSubcontractor(string InXml)
        {
            string AddSubcontractorRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            string SubcontractorName;
            string Description = defString;
            string Creator = defString;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);

                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorName = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorName Not Found!");
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Creator
                if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                    {
                        Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // 呼叫Dll執行新增資料
                objENT.AddSubcontractor(SubcontractorNo, SubcontractorName, Description, Creator);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "AddSubcontractor");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Add Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Add Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                AddSubcontractorRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return AddSubcontractorRet;

        }

        [WebMethod(Description = "Edit Specified Subcontractor Data")]
        public string EditSubcontractor(string InXml)
        {
            string EditSubcontractorRet = default(string);

            // 參數先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            int IssueState = defInteger;
            string SubcontractorName = defString;
            string Description = defString;

            int DataStamp = defInteger;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }
                // SubcontractorName不可空白
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorName = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Item(0).InnerText;
                        if (string.IsNullOrEmpty(SubcontractorName))
                        {
                            throw new iMESException.MESException("0000-200001", "SubcontractorName is empty!");
                        }
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // Unfrozen時才可修改
                {
                    // 有修改欄位值時
                    if ((SubcontractorName ?? "") != defString | (Description ?? "") != defString)
                    {
                        // 呼叫Dll執行修改
                        objENT.EditSubcontractor(SubcontractorNo, SubcontractorName, Description, AdditionalXml, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                    }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "EditSubcontractor");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Edit Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Edit Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                EditSubcontractorRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return EditSubcontractorRet;

        }

        [WebMethod(Description = "Delete Subcontractor By Specified Subcontractor")]
        public string DelSubcontractor(string InXml)
        {
            string DelSubcontractorRet = default(string);

            var SubcontractorNo = default(string);
            int IssueState;
            string strDelState; // 寫入tblSysEventLog中的訊息,0:未簽核刪除,2:已簽核刪除,-1:不使用刪除
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                switch (IssueState)
                {
                    case 0:
                        {
                            objENT.DelSubcontractor(SubcontractorNo);
                            strDelState = "未簽核刪除";
                            break;
                        }
                    case 2:
                        {
                            objENT.SetSubcontractorIssueState(SubcontractorNo, -1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            strDelState = "已簽核刪除";
                            break;
                        }
                    case -1:
                        {
                            objENT.DelSubcontractor(SubcontractorNo);
                            strDelState = "不使用刪除";
                            break;
                        }

                    default:
                        {
                            throw new iMESException.MESException("0000-200021", "[%IssueState%]");
                        }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "DelSubcontractor:" + strDelState);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Delete Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Delete Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                DelSubcontractorRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return DelSubcontractorRet;

        }

        [WebMethod(Description = "Approve Specified Subcontractor Data")]
        public string ApproveSubcontractor(string InXml)
        {
            string ApproveSubcontractorRet = default(string);

            string IssueNo = "SUBCONTRACTOR BASIS"; // 簽核編號:SUBCONTRACTOR BASIS.若找不到,表示直接將狀態改為Active.
            var SubcontractorNo = default(string);
            int IssueState;
            var objIssue = new kcISSUE.clsIssue();
            DataRow IssueDR;
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // 當狀態為Unfrozen時才作
                {
                    // 去IssueManagement中找核准的方式
                    IssueDR = objIssue.GetIssueJobMap(IssueNo);

                    // 找不到表示bypass直接核准
                    if (IssueDR["JobNo"] is DBNull)
                    {
                        // 直接核准
                        objENT.SetSubcontractorIssueState(SubcontractorNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                        objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");
                    }

                    else
                    {
                        // 檢查JobNo是否有定義Detail檔,沒有Detail檔則直接核准
                        DataRow tmpDR;
                        tmpDR = objIssue.GetIssueJobDetail(Conversions.ToString(IssueDR["JobNo"]));
                        if (tmpDR["JobNo"] is DBNull)
                        {
                            objENT.SetSubcontractorIssueState(SubcontractorNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");
                        }

                        else
                        {
                            // 將狀態改為Pending,且應以找到的JobNo來進入簽核
                            // 填入簽核現況檔之Initial
                            var ApproveSQL = new Collection();
                            var RejectSQL = new Collection();
                            string tmpSQL;
                            string IssueSubject;
                            string IssueMasterNo;

                            // 核准後應將狀態改為Active
                            tmpSQL = "Update tblENTSubcontractor Set IssueState = 2 Where SubcontractorNo = '" + SubcontractorNo + "'";
                            ApproveSQL.Add(tmpSQL);
                            // 退回則應將狀態回復到Unfrozen
                            tmpSQL = "Update tblENTSubcontractor Set IssueState = 0 Where SubcontractorNo = '" + SubcontractorNo + "'";
                            RejectSQL.Add(tmpSQL);

                            // Creator未傳入表示以目前的User
                            string Creator;
                            if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                            {
                                if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                                {
                                    Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                                }
                                else
                                {
                                    Creator = GetXMLCurUserNo(xmlDoc);
                                }
                            }
                            else
                            {
                                Creator = GetXMLCurUserNo(xmlDoc);
                            }

                            IssueSubject = "SubcontractorNo: " + SubcontractorNo;
                            IssueMasterNo = objIssue.InitIssueMasterNo();
                            // 此筆資料進入Issue流程
                            objIssue.StartIssueProcess(IssueMasterNo, ApproveSQL, RejectSQL, IssueNo, IssueDR["JobNo"].ToString(), IssueSubject, Creator);
                            // 將狀態改為Pending
                            objENT.SetSubcontractorIssueState(SubcontractorNo, 1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);

                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");

                            // 發Email給JobSerialNo=1之負責群組
                            objIssue.SendEmailToFirstGroup(IssueMasterNo, IssueSubject);
                        }
                    }
                }
                strException = "";
                strResult = "success";
            }


            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Approve Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Approve Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                ApproveSubcontractorRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return ApproveSubcontractorRet;

        }

        // Subcontractor Contactor
        [WebMethod(Description = "Load Subcontractor Contactor Data")]
        public string LoadSubcontractorCont(string InXml)
        {
            string LoadSubcontractorContRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            string SubcontractorNo = defString;
            string ContactorName = defString;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 判斷是否有傳入SubcontractorNo,若有,表示要依SubcontractorNo查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // 呼叫Dll執行
                strReturnValue = objENT.LoadSubcontractorCont(SubcontractorNo, ContactorName);
                strException = "";
                strResult = "success";
            }

            catch (iMESException.MESException ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Show Subcontractor Contactor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            catch (Exception ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(defWSErrCode, ex.Message, "Show Subcontractor Contactor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                LoadSubcontractorContRet = CombineXMLResponse(strIdentity, strReturnValue, strException, strResult, "");

            }

            return LoadSubcontractorContRet;

        }

        [WebMethod(Description = "Add Subcontractor Contactor data")]
        public string AddSubcontractorCont(string InXml)
        {
            string AddSubcontractorContRet = default(string);
            // 先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            var ContactorName = default(string);
            string TelNo = defString;
            string FaxNo = defString;
            string Title = defString;
            string Address = defString;
            string EMail = defString;
            string Description = defString;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);

                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // SubcontractorNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                }
                // TelNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("telno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("telno").Item(0).SelectNodes("value").Count > 0)
                    {
                        TelNo = xmlDoc.DocumentElement.GetElementsByTagName("telno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // FaxNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("faxno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Count > 0)
                    {
                        FaxNo = xmlDoc.DocumentElement.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Title
                if (xmlDoc.DocumentElement.GetElementsByTagName("title").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("title").Item(0).SelectNodes("value").Count > 0)
                    {
                        Title = xmlDoc.DocumentElement.GetElementsByTagName("title").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Address
                if (xmlDoc.DocumentElement.GetElementsByTagName("address").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("address").Item(0).SelectNodes("value").Count > 0)
                    {
                        Address = xmlDoc.DocumentElement.GetElementsByTagName("address").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // EMail
                if (xmlDoc.DocumentElement.GetElementsByTagName("email").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("email").Item(0).SelectNodes("value").Count > 0)
                    {
                        EMail = xmlDoc.DocumentElement.GetElementsByTagName("email").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");


                // 呼叫Dll執行新增資料
                objENT.AddSubcontractorCont(SubcontractorNo, ContactorName, TelNo, FaxNo, Title, Address, EMail, Description, AdditionalXml, GetXMLCurUserNo(xmlDoc), ArriveTime);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "AddSubcontractorContactor ContactorName:" + ContactorName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Add SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Add SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                AddSubcontractorContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return AddSubcontractorContRet;

        }

        [WebMethod(Description = "Edit Specified Subcontractor Contactor Data")]
        public string EditSubcontractorCont(string InXml)
        {
            string EditSubcontractorContRet = default(string);
            // 先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            var ContactorName = default(string);
            string TelNo = defString;
            string FaxNo = defString;
            string Title = defString;
            string Address = defString;
            string EMail = defString;
            string Description = defString;

            int DataStamp = defInteger;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // SubcontractorNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                }
                // TelNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("telno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("telno").Item(0).SelectNodes("value").Count > 0)
                    {
                        TelNo = xmlDoc.DocumentElement.GetElementsByTagName("telno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // FaxNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("faxno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Count > 0)
                    {
                        FaxNo = xmlDoc.DocumentElement.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Title
                if (xmlDoc.DocumentElement.GetElementsByTagName("title").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("title").Item(0).SelectNodes("value").Count > 0)
                    {
                        Title = xmlDoc.DocumentElement.GetElementsByTagName("title").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Address
                if (xmlDoc.DocumentElement.GetElementsByTagName("address").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("address").Item(0).SelectNodes("value").Count > 0)
                    {
                        Address = xmlDoc.DocumentElement.GetElementsByTagName("address").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // EMail
                if (xmlDoc.DocumentElement.GetElementsByTagName("email").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("email").Item(0).SelectNodes("value").Count > 0)
                    {
                        EMail = xmlDoc.DocumentElement.GetElementsByTagName("email").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                // If IssueState = 0 Then     'Unfrozen時才可修改
                // 有修改欄位值時
                if ((TelNo ?? "") != defString | (FaxNo ?? "") != defString | (Title ?? "") != defString | (Address ?? "") != defString | (EMail ?? "") != defString | (Description ?? "") != defString)
                {
                    // 呼叫Dll執行修改
                    objENT.EditSubcontractorCont(SubcontractorNo, ContactorName, TelNo, FaxNo, Title, Address, EMail, Description, AdditionalXml, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                }
                // End If

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "EditSubcontractorContactor ContactorName:" + ContactorName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Edit SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Edit SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                EditSubcontractorContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return EditSubcontractorContRet;

        }

        [WebMethod(Description = "Delete Subcontractor Contactor By Specified SubcontractorNo and ContactorName")]
        public string DelSubcontractorCont(string InXml)
        {
            string DelSubcontractorContRet = default(string);

            var SubcontractorNo = default(string);
            string ContactorName = defString; // 沒有傳入表示要刪除指定Subcontractor的所有Contactor

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // SubcontractorNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                objENT.DelSubcontractorCont(SubcontractorNo, ContactorName);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "DelSubcontractorContactor ContactorName:" + ContactorName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Del SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Del SubcontractorContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                DelSubcontractorContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return DelSubcontractorContRet;

        }


        #endregion

        #region "TEST14278"
        [WebMethod(Description = "Load Subcontractor Data")]
        public string LoadTestDataBasis_14278(string InXml)
        {
            string LoadTestDataBasis_14278Ret = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            string SubcontractorNo = defString;
            int IssueState = 2;

            ArriveTime = DateTime.Now;
            string AdditionalXml = "";
            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 判斷是否有傳入Subcontractor,若有,表示要依Subcontractor查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // 判斷是否有傳入IssueState,若有,表示要依IssueState查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                }

                // 呼叫副程式處理查詢條件與Additional格式
                AdditionalXml = CombineXMLQueryAdditional(xmlDoc);

                // 呼叫Dll執行
                strReturnValue = objTest.LoadTestDataBasis_14278(SubcontractorNo, IssueState, AdditionalXml);

                strException = "";
                strResult = "success";
            }

            catch (iMESException.MESException ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Show TestDataBasis_14278 Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            catch (Exception ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(defWSErrCode, ex.Message, "Show TestDataBasis_14278 Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                LoadTestDataBasis_14278Ret = CombineXMLResponse(strIdentity, strReturnValue, strException, strResult, "");

            }

            return LoadTestDataBasis_14278Ret;

        }

        [WebMethod(Description = "Add Subcontractor data")]
        public string AddTestDataBasis_14278(string InXml)
        {
            string AddTestDataBasis_14278Ret = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            string SubcontractorName;
            string Description = defString;
            string Creator = defString;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);

                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorName = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorName Not Found!");
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Creator
                if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                    {
                        Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // 呼叫Dll執行新增資料
                objTest.AddTestDataBasis_14278 (SubcontractorNo, SubcontractorName, Description, Creator);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "TestDataBasis_14278", SubcontractorNo, DateTime.Now, "TestDataBasis_14278");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Add TestDataBasis_14278 failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "TestDataBasis_14278", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Add TestDataBasis_14278 failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "TestDataBasis_14278", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                AddTestDataBasis_14278Ret = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return AddTestDataBasis_14278Ret;

        }

        [WebMethod(Description = "Edit Specified Subcontractor Data")]
        public string EditTestDataBasis_14278(string InXml)
        {
            string EditTestDataBasis_14278Ret = default(string);

            // 參數先給預設值,以判斷是否有傳入該參數
            var SubcontractorNo = default(string);
            int IssueState = defInteger;
            string SubcontractorName = defString;
            string Description = defString;

            int DataStamp = defInteger;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }
                // SubcontractorName
                GetXMLTagValue(ref SubcontractorName, xmlDoc, "subcontractorname", true);

                // Description
                GetXMLTagValue(ref Description, xmlDoc, "description", false );

                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // Unfrozen時才可修改
                {
                    // 有修改欄位值時
                    if ((SubcontractorName ?? "") != defString | (Description ?? "") != defString)
                    {
                        // 呼叫Dll執行修改
                        objTest.EditTestDataBasis_14278 (SubcontractorNo, SubcontractorName, Description, AdditionalXml, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                    }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "EditSubcontractor");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Edit Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Edit Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                EditTestDataBasis_14278Ret = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return EditTestDataBasis_14278Ret;

        }

        [WebMethod(Description = "Delete Subcontractor By Specified Subcontractor")]
        public string DelTestDataBasis_14278(string InXml)
        {
            string DelTestDataBasis_14278Ret = default(string);

            var SubcontractorNo = default(string);
            int IssueState;
            string strDelState; // 寫入tblSysEventLog中的訊息,0:未簽核刪除,2:已簽核刪除,-1:不使用刪除
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                switch (IssueState)
                {
                    case 0:
                        {
                            objTest.DelTestDataBasis_14278 (SubcontractorNo);
                            strDelState = "未簽核刪除";
                            break;
                        }
                    case 2:
                        {
                            objTest.SetTestDataBasis_14278IssueState(SubcontractorNo, -1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            strDelState = "已簽核刪除";
                            break;
                        }
                    case -1:
                        {
                            objTest.DelTestDataBasis_14278(SubcontractorNo);
                            strDelState = "不使用刪除";
                            break;
                        }

                    default:
                        {
                            throw new iMESException.MESException("0000-200021", "[%IssueState%]");
                        }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "DelSubcontractor:" + strDelState);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Delete Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Delete Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                DelTestDataBasis_14278Ret = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return DelTestDataBasis_14278Ret;

        }

        public string ApproveTestDataBasis_14278(string InXml)
        {
            string ApproveSubcontractorRet = default(string);

            string IssueNo = "SUBCONTRACTOR BASIS"; // 簽核編號:SUBCONTRACTOR BASIS.若找不到,表示直接將狀態改為Active.
            var SubcontractorNo = default(string);
            int IssueState;
            var objIssue = new kcISSUE.clsIssue();
            DataRow IssueDR;
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // Subcontractor一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        SubcontractorNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "SubcontractorNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // 當狀態為Unfrozen時才作
                {
                    // 去IssueManagement中找核准的方式
                    IssueDR = objIssue.GetIssueJobMap(IssueNo);

                    // 找不到表示bypass直接核准
                    if (IssueDR["JobNo"] is DBNull)
                    {
                        // 直接核准
                        objTest.SetTestDataBasis_14278IssueState(SubcontractorNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                        objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");
                    }

                    else
                    {
                        // 檢查JobNo是否有定義Detail檔,沒有Detail檔則直接核准
                        DataRow tmpDR;
                        tmpDR = objIssue.GetIssueJobDetail(Conversions.ToString(IssueDR["JobNo"]));
                        if (tmpDR["JobNo"] is DBNull)
                        {
                            objTest.SetTestDataBasis_14278IssueState(SubcontractorNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");
                        }

                        else
                        {
                            // 將狀態改為Pending,且應以找到的JobNo來進入簽核
                            // 填入簽核現況檔之Initial
                            var ApproveSQL = new Collection();
                            var RejectSQL = new Collection();
                            string tmpSQL;
                            string IssueSubject;
                            string IssueMasterNo;

                            // 核准後應將狀態改為Active
                            tmpSQL = "Update tblENTTestDataBasis_14278 Set IssueState = 2 Where SubcontractorNo = '" + SubcontractorNo + "'";
                            ApproveSQL.Add(tmpSQL);
                            // 退回則應將狀態回復到Unfrozen
                            tmpSQL = "Update tblENTTestDataBasis_14278 Set IssueState = 0 Where SubcontractorNo = '" + SubcontractorNo + "'";
                            RejectSQL.Add(tmpSQL);

                            // Creator未傳入表示以目前的User
                            string Creator;
                            if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                            {
                                if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                                {
                                    Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                                }
                                else
                                {
                                    Creator = GetXMLCurUserNo(xmlDoc);
                                }
                            }
                            else
                            {
                                Creator = GetXMLCurUserNo(xmlDoc);
                            }

                            IssueSubject = "SubcontractorNo: " + SubcontractorNo;
                            IssueMasterNo = objIssue.InitIssueMasterNo();
                            // 此筆資料進入Issue流程
                            objIssue.StartIssueProcess(IssueMasterNo, ApproveSQL, RejectSQL, IssueNo, IssueDR["JobNo"].ToString(), IssueSubject, Creator);
                            // 將狀態改為Pending
                            objTest.SetTestDataBasis_14278IssueState(SubcontractorNo, 1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);

                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, "ApproveSubcontractor");

                            // 發Email給JobSerialNo=1之負責群組
                            objIssue.SendEmailToFirstGroup(IssueMasterNo, IssueSubject);
                        }
                    }
                }
                strException = "";
                strResult = "success";
            }


            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Approve Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Approve Subcontractor failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "SubcontractorNo", SubcontractorNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                ApproveSubcontractorRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return ApproveSubcontractorRet;

        }
        #endregion
    }
}
