//IVM158.aspx
        /// <summary>
        /// 客服查補
        /// </summary>
        /// <param name="ParameterList">UserID,PickDate,TransDate,AcceptDate</param>
        /// <param name="RootDBT">傳入的交易</param>
        /// <param name="OK">OK FLAG</param>
        /// <param name="PickNo">回傳的揀貨單號</param>
        /// <returns></returns>
        public bool CreateByCRMTrans(ArrayList ParameterList, DbTransaction RootDBT, out string OK, out string PickNo)
        {
            OK = string.Empty;
            string SubOK = string.Empty;
            PickNo = string.Empty;
            //string PickDataNo = "";
            ArrayList DBOPara = new ArrayList();
            bool IsRootTranscation = false;
            //int PID = -1;

            string UserID = ParameterList[0].ToString();
            string PickDate = ParameterList[1].ToString();
            string TransDate = ParameterList[2].ToString();
            string AcceptDate = ParameterList[3].ToString();

            try
            {
                DBO.VDS_IVM15_DBO IVMDBO = new VDS_IVM15_DBO(ref USEDB);

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;

                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                DBOPara.Clear();
                DBOPara.Add(GetValueSetParameter(TransDate, "string", false));
                DBOPara.Add(GetValueSetParameter(AcceptDate, "string", false));

                DataTable dtPickData = IVMDBO.GetPickDataByCRMTrans(DBOPara);

                DataTable dtSumPickData = IVMDBO.GetSumPickDataByCRMTrans(DBOPara);

                if (dtSumPickData.Rows.Count == 0)
                {
                    OK = "0";
                    PickNo = "";
                    return false;
                }
                else
                {
                    OK = "1";
                }

                DataTable DtNew = new DataTable();
                DtNew.Columns.Add("Item", System.Type.GetType("System.String"));
                DtNew.Columns.Add("period", System.Type.GetType("System.String"));
                DtNew.Columns.Add("OnHD_Qty", System.Type.GetType("System.Int32"));
                DtNew.Columns.Add("IniPickQty", System.Type.GetType("System.Int32"));
                DtNew.Columns.Add("RealPickQty", System.Type.GetType("System.Int32"));
                DtNew.Columns.Add("BLocateNo", System.Type.GetType("System.String"));
                DtNew.Columns.Add("BLocateSection", System.Type.GetType("System.String"));
                DtNew.Columns.Add("ELocateNo", System.Type.GetType("System.String"));
                DtNew.Columns.Add("ELocateSection", System.Type.GetType("System.String"));

                //A.Item,
                //A.Period,
                //A.SRC_LOCATE_TYPE,
                //A.SRC_Locate_No,
                //A.DST_Locate_No,
                //A.Pick_Qty,
                //L.LOCATE_SECTION DST_Locate_Section

                for (int i = 0; i < dtSumPickData.Rows.Count; i++)
                { 
                    DBOPara.Clear();
                    DBOPara.Add(dtSumPickData.Rows[i]["SRC_Locate_No"].ToString());
                    DBOPara.Add(dtSumPickData.Rows[i]["SRC_LOCATE_TYPE"].ToString());
                    DBOPara.Add(dtSumPickData.Rows[i]["Item"].ToString());
                    DBOPara.Add(dtSumPickData.Rows[i]["Period"].ToString());
                    DBOPara.Add(int.Parse(dtSumPickData.Rows[i]["Pick_Qty"].ToString()));
                    //DBOPara.Add(dtPickData.Rows[i]["DST_Locate_Section"].ToString());
                    //取得庫存資料及揀貨儲位
                    DataTable dtTemp = GetSugLocateQty(DBOPara, out SubOK);

                    if (SubOK == "2")
                    {
                        OK = "2";
                    }

                    if (dtTemp.Rows.Count == 0)
                    {
                        for (int x = 0; x < dtPickData.Rows.Count; x++)
                        {
                            if (dtSumPickData.Rows[i]["Item"].ToString() == dtPickData.Rows[x]["Item"].ToString() &&
                                dtSumPickData.Rows[i]["Period"].ToString() == dtPickData.Rows[x]["Period"].ToString() &&
                                dtSumPickData.Rows[i]["SRC_Locate_No"].ToString() == dtPickData.Rows[x]["SRC_Locate_No"].ToString())
                            {
                                DataRow dr = DtNew.NewRow();
                                dr["item"] = dtPickData.Rows[x]["Item"].ToString();
                                dr["period"] = dtPickData.Rows[x]["Period"].ToString();
                                dr["OnHD_Qty"] = 0;
                                dr["IniPickQty"] = 0;
                                dr["RealPickQty"] = dtPickData.Rows[x]["Pick_Qty"].ToString();
                                dr["BLocateNo"] = dtPickData.Rows[x]["SRC_Locate_No"].ToString();
                                dr["BLocateSection"] = "";
                                dr["ELocateNo"] = dtPickData.Rows[x]["DST_Locate_Section"].ToString().Substring(0, 1);
                                dr["ELocateSection"] = "";
                                DtNew.Rows.Add(dr);
                            }
                        }
                    }

                    //根據資料分配儲區、儲位及數量
                    for (int y = 0; y < dtPickData.Rows.Count;y++ )
                    {
                        if (dtSumPickData.Rows[i]["Item"].ToString() == dtPickData.Rows[y]["Item"].ToString() &&
                                dtSumPickData.Rows[i]["Period"].ToString() == dtPickData.Rows[y]["Period"].ToString() &&
                                dtSumPickData.Rows[i]["SRC_Locate_No"].ToString() == dtPickData.Rows[y]["SRC_Locate_No"].ToString())
                        for (int j = 0; j < dtTemp.Rows.Count; j++)
                        {

                            if (int.Parse(dtTemp.Rows[j]["RealPickQty"].ToString()) > 0)
                            {
                                int RealPickQty = int.Parse(dtTemp.Rows[j]["RealPickQty"].ToString()); //該儲位被分配到的應揀貨量
                                int OnHD_Qty = int.Parse(dtTemp.Rows[j]["OnHD_Qty"].ToString()); //該儲位的庫存量
                                int Pick_Qty = int.Parse(dtPickData.Rows[y]["Pick_Qty"].ToString()); //該商品於該儲區的實際應揀貨量
                                int Pick = 0; //儲位揀貨量
                                int Pick_OnHD_Qty = 0;
                                int RemainQty = 0; //分配儲位剩餘量
                                if(Pick_Qty > 0)
                                {
                                    if(RealPickQty >= Pick_Qty) //該儲位數量可以滿足一次揀貨
                                    {
                                        RemainQty = RealPickQty - Pick_Qty; //剩餘的應揀貨量
                                        int RemainOnHDQty = OnHD_Qty - Pick_Qty;  //該儲位剩餘的庫存量
                                        Pick = Pick_Qty;
                                        Pick_OnHD_Qty = OnHD_Qty; //Liea加
                                        dtPickData.Rows[y]["Pick_Qty"] = "0";
                                        dtTemp.Rows[j]["RealPickQty"] = RealPickQty - Pick_Qty;//Liea Pick.ToString();
                                        
                                        dtTemp.Rows[j]["OnHD_Qty"] = RemainOnHDQty.ToString();

                                        //Liea
                                        if(OnHD_Qty > Pick_Qty) //庫存大於揀貨量
                                        {

                                            //Pick_OnHD_Qty = RemainOnHDQty; //Liea  Pick_OnHD_Qty = RemainQty;
                                            dtTemp.Rows[j]["OnHD_Qty"] = RemainOnHDQty.ToString();
                                        }
                                        else //庫存等於揀貨量
                                        {
                                            //Pick_OnHD_Qty = 0; //Liea  Pick_OnHD_Qty = OnHD_Qty;
                                            dtTemp.Rows[j]["OnHD_Qty"] = "0";
                                        }
                                        //Liea
                                        
                                    }
                                    else //該儲位數量小於一次揀貨
                                    {
                                        RemainQty = 0;
                                        Pick = RealPickQty; //該儲位數量全部揀光
                                        int TempQty = Pick_Qty - RealPickQty;
                                        dtPickData.Rows[y]["Pick_Qty"] = TempQty.ToString();
                                        Pick_OnHD_Qty = OnHD_Qty;
                                        dtTemp.Rows[j]["OnHD_Qty"] = "0";
                                        dtTemp.Rows[j]["RealPickQty"] = "0";//Liea
                                    }
                                    DataRow dr = DtNew.NewRow();
                                    dr["item"] = dtTemp.Rows[j]["Item"].ToString();
                                    dr["period"] = dtTemp.Rows[j]["period"].ToString();
                                    dr["OnHD_Qty"] = Pick_OnHD_Qty.ToString();
                                    dr["IniPickQty"] = Pick.ToString(); // Liea  int.Parse(dtTemp.Rows[j]["IniPickQty"].ToString());
                                    dr["RealPickQty"] = Pick.ToString();
                                    dr["BLocateNo"] = dtTemp.Rows[j]["BLocateNo"].ToString();
                                    dr["BLocateSection"] = dtTemp.Rows[j]["BLocateSection"].ToString();
                                    dr["ELocateNo"] = dtPickData.Rows[y]["DST_Locate_Section"].ToString().Substring(0,1);
                                    dr["ELocateSection"] = dtPickData.Rows[y]["DST_Locate_Section"].ToString();
                                    DtNew.Rows.Add(dr);
                                }
                            }
                        }
                    }
                }

                DataView dv = new DataView(DtNew);
                dv.Sort="BLOCATENO,ELOCATENO asc";
                DataTable dtInsert = dv.ToTable();

                ArrayList LocateNoList = new ArrayList();

                //取得儲位類型 BLocateSecList + ElocateSecList不同視為相異
                for (int i = 0; i < dtInsert.Rows.Count;i++ )
                {
                    string BNo = dtInsert.Rows[i]["BLOCATENO"].ToString();
                    string ENo = dtInsert.Rows[i]["ELOCATENO"].ToString();

                    bool InList = false;
                    for (int j = 0; j < LocateNoList.Count; j++)
                    {
                        InList = false;
                        string BNO = LocateNoList[j].ToString().Split(',')[0];
                        string ENO = LocateNoList[j].ToString().Split(',')[1];

                        if (BNo == BNO && ENo == ENO)
                        {
                            InList = true; 
                        }
                    }
                    if (InList == false)
                        LocateNoList.Add(BNo + "," + ENo);
                }

                for (int i = 0; i < LocateNoList.Count; i++)
                {
                    string TempPickNo = string.Empty;
                    int TempMainID = -1;

                    string NewBNo = LocateNoList[i].ToString().Split(',')[0];
                    string NewENo = LocateNoList[i].ToString().Split(',')[1];

                    int TempInsertCount = 0;
                    DBOPara.Clear();
                    DBOPara.Add(GetValueSetParameter(UserID,"string",false));
                    DBOPara.Add(GetValueSetParameter(AcceptDate,"string",false));
                    DBOPara.Add(GetValueSetParameter("","string",false));
                    DBOPara.Add(GetValueSetParameter(PickDate,"string",false));
                    DBOPara.Add(GetValueSetParameter("7","int",false));
                    DBOPara.Add(GetValueSetParameter("","string",false));
                    TempInsertCount = IVMDBO.CreatePickMain(DBOPara, DBT, out TempMainID, out TempPickNo);

                    if (TempInsertCount == 0)
                    {
                        throw new Exception("新增揀貨單主檔失敗");
                    }

                    for (int j = 0; j < dtInsert.Rows.Count; j++)
                    {
                        if (NewBNo == dtInsert.Rows[j]["BLocateNo"].ToString() && NewENo == dtInsert.Rows[j]["ELocateNo"].ToString())
                        {
                            string item = dtInsert.Rows[j]["item"].ToString();
                            string period = dtInsert.Rows[j]["period"].ToString();
                            int onhd_qty = int.Parse(dtInsert.Rows[j]["onhd_qty"].ToString());
                            int IniPickQty = int.Parse(dtInsert.Rows[j]["IniPickQty"].ToString());
                            int RealPickQty = int.Parse(dtInsert.Rows[j]["RealPickQty"].ToString());
                            string strBLocateNo = dtInsert.Rows[j]["BLocateNo"].ToString();
                            string strBLocateSec = dtInsert.Rows[j]["BLocateSection"].ToString();
                            string strELocateNo = dtInsert.Rows[j]["ELocateNo"].ToString();
                            string strELocateSec = dtInsert.Rows[j]["ELocateSection"].ToString();

                            DBOPara.Clear();
                            DBOPara.Add(GetValueSetParameter(UserID,"string",false));
                            DBOPara.Add(GetValueSetParameter(PickDate,"string",false));
                            DBOPara.Add(GetValueSetParameter(TempMainID.ToString(),"int",false));
                            DBOPara.Add(GetValueSetParameter(item,"string",false));
                            DBOPara.Add(GetValueSetParameter(period,"string",false));
                            DBOPara.Add(GetValueSetParameter(onhd_qty.ToString(),"int",false));
                            DBOPara.Add(GetValueSetParameter(IniPickQty.ToString(),"int",false));
                            DBOPara.Add(GetValueSetParameter(RealPickQty.ToString(),"int",false));
                            DBOPara.Add(GetValueSetParameter(strBLocateNo,"string",false));
                            DBOPara.Add(GetValueSetParameter(strBLocateSec,"string",false));
                            DBOPara.Add(GetValueSetParameter(strELocateNo,"string",false));
                            DBOPara.Add(GetValueSetParameter(strELocateSec,"string",false));
                            DBOPara.Add(GetValueSetParameter("","int",false));
                            IVMDBO.CreatePickDetl(DBOPara, DBT);
                        }
                    }


                    DBOPara.Clear();
                    DBOPara.Add(UserID);
                    DBOPara.Add(PickDate);
                    DBOPara.Add(TempPickNo);
                    DBOPara.Add(NewBNo);
                    DBOPara.Add(NewENo);
                    DBOPara.Add(TransDate);
                    DBOPara.Add(AcceptDate);

                    IVMDBO.UpdateCRMOrderDetl(DBOPara, DBT);

                    PickNo += TempPickNo + ",";
                }

                #region 交易成功

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }

                if (PickNo.Length > 0)
                {
                    PickNo = PickNo.Substring(0, PickNo.Length - 1);
                }

                return true;

                #endregion
            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw ex;
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion
            }
        }