//*****************************************************************************
        //do it later      do it later      do it later
        //*****************************************************************************
        /// <summary>
        /// 保存备货计划
        /// </summary>
        /// <param name="data">备货单实例</param>
        /// <param name="dtStockUpDetall">备货明细数据源</param>
        /// <param name="issubmit">是否提交</param>
        /// <returns></returns>
        public bool SaveBArrangeBill(CStockUpBillData data, DataTable dtStockUpDetall, bool isSubmit)
        {
            bool ret = false;
            CStockUpDetailBB stockUpDetailBB = new CStockUpDetailBB(connection);
            CStockUpBillBB stockUpBillBB = new CStockUpBillBB(connection);
            SCommBB commBB = new SCommBB(this.connection);
            SqlTransaction trans = null;

            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TransSave");
                    stockUpDetailBB.Transaction = trans;
                    stockUpBillBB.Transaction = trans;
                    commBB.Transaction = trans;
                }
                else
                {
                    stockUpDetailBB.Transaction = this.transaction;
                    stockUpBillBB.Transaction = this.transaction;
                    commBB.Transaction = this.transaction;
                }

                int mainId = 0;
                CStockUpDetailData stockUpDetailModel = new CStockUpDetailData();
                string strSaleBillNos = "", strAbsEntrys = "", strLineNums = "";
                string[] saleBillNoArray = null, absEntryArray = null, lineNumArray = null;

                if (data.id == 0)
                {
                    #region 新增备货单

                    //新增备货主表数据
                    mainId = stockUpBillBB.AddRecord(data);

                    //新增备货单明细数据
                    foreach (DataRow dr in dtStockUpDetall.Rows)
                    {
                        if (dr["isdel"].ToString() == "0")
                        {
                            stockUpDetailModel = new CStockUpDetailData();

                            stockUpDetailModel.financeBillNo = dr["financeBillNo"].ToString();
                            stockUpDetailModel.financeBillSort = dr["financeBillSort"].ToString();
                            stockUpDetailModel.materialNo = dr["materialNo"].ToString();
                            stockUpDetailModel.num = Convert.ToInt32(dr["num"]);
                            stockUpDetailModel.instantState = "01";
                            stockUpDetailModel.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            stockUpDetailModel.stockUpBillNo = data.stockUpBillNo;
                            stockUpDetailModel.saleBillNo = dr["saleBillNo"].ToString();
                            stockUpDetailModel.DocEntry = Convert.ToInt32(dr["DocEntry"]);
                            stockUpDetailModel.absEntry = Convert.ToInt32(dr["absEntry"]);//销售提货单ID
                            stockUpDetailModel.lineNum = Convert.ToInt32(dr["lineNum"]);//销售订单行号
                            stockUpDetailModel.GZT = "";//分配工作台

                            stockUpDetailBB.AddRecord(stockUpDetailModel);
                        }
                    }

                    #endregion 新增备货单
                }
                else
                {
                    #region 修改备货单

                    //修改备货主表数据
                    stockUpBillBB.ModifyRecord(data);

                    //维护备货单明细数据
                    foreach (DataRow dr in dtStockUpDetall.Rows)
                    {
                        if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "1")//删除方法
                        {
                            stockUpDetailBB.DeleteRecord(Convert.ToInt32(dr["id"]));

                        }
                        else if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "0")//修改
                        {
                            stockUpDetailModel = stockUpDetailBB.GetModel(Convert.ToInt32(dr["id"]));

                            stockUpDetailModel.num = Convert.ToInt32(dr["num"]);

                            stockUpDetailBB.ModifyRecord(stockUpDetailModel);

                        }
                        else if (Convert.ToInt32(dr["id"]) == 0 && dr["isdel"].ToString() == "0")//添加
                        {
                            stockUpDetailModel = new CStockUpDetailData();

                            stockUpDetailModel.financeBillNo = dr["financeBillNo"].ToString();
                            stockUpDetailModel.financeBillSort = dr["financeBillSort"].ToString();
                            stockUpDetailModel.materialNo = dr["materialNo"].ToString();
                            stockUpDetailModel.num = Convert.ToInt32(dr["num"]);
                            stockUpDetailModel.instantState = "01";
                            stockUpDetailModel.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            stockUpDetailModel.stockUpBillNo = data.stockUpBillNo;
                            stockUpDetailModel.saleBillNo = dr["saleBillNo"].ToString();
                            stockUpDetailModel.DocEntry = Convert.ToInt32(dr["DocEntry"]);//销售订单ID
                            stockUpDetailModel.absEntry = Convert.ToInt32(dr["absEntry"]);//销售提货单ID
                            stockUpDetailModel.lineNum = Convert.ToInt32(dr["lineNum"]);//销售订单行号
                            stockUpDetailModel.GZT = "";//分配工作台

                            stockUpDetailBB.AddRecord(stockUpDetailModel);

                        }
                    }

                    #endregion 修改备货单
                }

                //根据提货单ID,更改销售订单状态
                strSaleBillNos = data.saleBillNos;
                strAbsEntrys = data.absEntrys;//提货单ID组合
                strLineNums = data.lineNums;
                saleBillNoArray = strSaleBillNos.Split(',');//销售订单编号数组
                absEntryArray = strAbsEntrys.Split(',');//提货单ID数组
                lineNumArray = strLineNums.Split(',');

                for (int n = 0; n < saleBillNoArray.Length; n++)
                {
                    //更改提货单状态
                    commBB.ExecuteSql("update dbo.CSaleBill set instantState='02',stockupBillNo='" + data.stockUpBillNo
                        + "' where saleBillNo='" + saleBillNoArray[n] + "' and absEntry='" + absEntryArray[n]
                        + "' and lineNum='" + lineNumArray[n] + "'");
                }

                if (this.transaction == null) trans.Commit();
                ret = true;
            }
            catch (Exception ex)
            {
                if (this.transaction == null) trans.Rollback("TransSave");
                throw ex;
            }
            finally
            {
                stockUpDetailBB.Dispose();
                stockUpBillBB.Dispose();
                commBB.Dispose();
            }

            return ret;
        }
        //*****************************************************************************
        //do it later      do it later      do it later
        //*****************************************************************************
        /// <summary>
        /// 保存排托出库信息
        /// </summary>
        /// <param name="stockUpDetailID">备货单明细ID</param>
        /// <param name="strNewPalletNo">新托盘号</param>
        /// <param name="strOldBoxNo">原箱号</param>
        /// <param name="strNewBoxNo">新箱号</param>
        /// <param name="num">数量</param>
        /// <returns></returns>
        public bool SaveOutStockInfo(int stockUpDetailID, string strNewPalletNo, string strOldBoxNo,
            string strNewBoxNo, double num)
        {
            bool ret = false;
            SqlTransaction trans = null;
            BArrangeBillBoxBB arraneBillBoxBB = new BArrangeBillBoxBB(this.connection);
            BForkliftTaskBC forkliftTaskBC = new BForkliftTaskBC(this.connection);
            CDEPickOutBoxBB DEPickOutBoxBB = new CDEPickOutBoxBB(this.connection);
            CStockUpDetailBB stockUpDetailBB = new CStockUpDetailBB(this.connection);
            CSaleDetailBB saleDetailBB = new CSaleDetailBB(this.connection);
            SCommBB commBB = new SCommBB(this.connection);
            UStockBB stockBB = new UStockBB(this.connection);
            UStockOutInDetailBB stockOutInDetailBB = new UStockOutInDetailBB(this.connection);
            LMaterialBB materialBB = new LMaterialBB(this.connection);

            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TransSaveOutStockInfo");
                    this.pickOutBillBB.Transaction = trans;
                    arraneBillBoxBB.Transaction = trans;
                    forkliftTaskBC.Transaction = trans;
                    DEPickOutBoxBB.Transaction = trans;
                    stockUpDetailBB.Transaction = trans;
                    saleDetailBB.Transaction = trans;
                    commBB.Transaction = trans;
                    stockBB.Transaction = trans;
                    stockOutInDetailBB.Transaction = trans;
                    materialBB.Transaction = trans;
                }
                else
                {
                    this.pickOutBillBB.Transaction = this.transaction;
                    arraneBillBoxBB.Transaction = this.transaction;
                    forkliftTaskBC.Transaction = this.transaction;
                    DEPickOutBoxBB.Transaction = this.transaction;
                    stockUpDetailBB.Transaction = this.transaction;
                    saleDetailBB.Transaction = this.transaction;
                    commBB.Transaction = this.transaction;
                    stockBB.Transaction = this.transaction;
                    stockOutInDetailBB.Transaction = this.transaction;
                    materialBB.Transaction = this.transaction;
                }

                CStockUpDetailData stockUpDetailModel = new CStockUpDetailData();
                vCStockUpDetailData vStockUpDetailModel = new vCStockUpDetailData();
                DataTable dtArrangeBillBox = new DataTable();
                DataTable dtDEPickOutBox = new DataTable();
                DataTable dtWareLocator = new DataTable();
                DataTable dtStockUpDetail = new DataTable();
                string strSql = "", strPrePalletNo = "", strPreWareLocatorNo = "";

                //获取备货单明细实例
                stockUpDetailModel = stockUpDetailBB.GetModel(stockUpDetailID);

                //拆托拣货箱信息
                dtDEPickOutBox = DEPickOutBoxBB.GetVList("boxNo='" + strOldBoxNo + "'").Tables[0];

                //普通拣货箱信息
                dtArrangeBillBox = arraneBillBoxBB.GetVList("boxNo='" + strOldBoxNo + "' and wareNo<>''").Tables[0];

                //扫描箱以前所在库位
                if (dtArrangeBillBox.Rows[0]["preWareLocatorNo"].ToString() != "")
                {
                    strPrePalletNo = dtArrangeBillBox.Rows[0]["palletNo"].ToString();//原托盘条码号
                    strPreWareLocatorNo = dtArrangeBillBox.Rows[0]["preWareLocatorNo"].ToString();//物料箱以前所在库位
                }

                //获取排托区下线库位信息
                strSql = @"select wareLocator.wareNo,wareLocator.downWareLocatorNo as wareLocatorNo
                           from dbo.BArrangeBillBox as arrangeBillBoxBox
                           left join dbo.LWareLocator as wareLocator on wareLocator.wareLocatorNo=arrangeBillBoxBox.wareLocatorNo
                           where arrangeBillBoxBox.boxNo='" + strOldBoxNo + "'";

                dtWareLocator = commBB.Query(strSql).Tables[0];

                #region 新增销售拣货单信息

                CPickOutBillData pickOutBillModel = new CPickOutBillData();

                //新增销售拣货单信息
                pickOutBillModel.stockUpDetailID = stockUpDetailID;//备货单明细ID

                //从哪个拣货库区、库位赋值
                if (dtDEPickOutBox.Rows.Count > 0)
                {
                    pickOutBillModel.wareNo = dtDEPickOutBox.Rows[0]["preWareNo"].ToString();//以前的仓库编号
                    pickOutBillModel.wareLocatorNo = dtDEPickOutBox.Rows[0]["preWareLocatorNo"].ToString();//以前的库位编号
                }
                else
                {
                    pickOutBillModel.wareNo = dtArrangeBillBox.Rows[0]["preWareNo"].ToString();//以前的仓库编号
                    pickOutBillModel.wareLocatorNo = dtArrangeBillBox.Rows[0]["preWareLocatorNo"].ToString();//以前的库位编号
                }

                pickOutBillModel.preWareNo = "";//以前的仓库编号
                pickOutBillModel.preWareLocatorNo = "";//以前的库位编号
                pickOutBillModel.oldPalletNo = dtArrangeBillBox.Rows[0]["palletNo"].ToString();//原托盘号
                pickOutBillModel.oldRegion = dtArrangeBillBox.Rows[0]["region"].ToString();//原托内区域
                pickOutBillModel.oldBoxNo = strOldBoxNo;//原箱号
                pickOutBillModel.newBoxNo = strNewBoxNo;//新箱号
                pickOutBillModel.num = num;//数量
                pickOutBillModel.isrtEmpId = this.empId;//添加人
                pickOutBillModel.isrtDt = System.DateTime.Now.ToString();//添加时间
                pickOutBillModel.instantState = "01";//未发货

                pickOutBillBB.AddRecord(pickOutBillModel);

                #endregion 新增销售拣货单信息

                #region 新增新的排托箱

                DataTable dtArrangeBillBoxNew = new DataTable();

                dtArrangeBillBoxNew = arraneBillBoxBB.GetVList("boxNo='" + strNewBoxNo + "'").Tables[0];

                //新增新的排托箱
                if (dtArrangeBillBoxNew.Rows.Count == 0)
                {
                    BArrangeBillBoxData newArrangeBillBoxModel = new BArrangeBillBoxData();

                    newArrangeBillBoxModel.arriveBillNo = "";//到货单号
                    newArrangeBillBoxModel.financeBillNo = dtArrangeBillBox.Rows[0]["financeBillNo"].ToString();//采购订单号
                    newArrangeBillBoxModel.arrangeBillNo = "";//排托单号
                    newArrangeBillBoxModel.stockUpBillNo = stockUpDetailModel.stockUpBillNo;//备货单编号
                    newArrangeBillBoxModel.boxNo = strNewBoxNo;//箱号
                    newArrangeBillBoxModel.oldBoxNo = strOldBoxNo;//原箱号
                    newArrangeBillBoxModel.materialNo = stockUpDetailModel.materialNo;//转换后物料编号
                    newArrangeBillBoxModel.oldMaterialNo = dtArrangeBillBox.Rows[0]["materialNo"].ToString();//转换前物料编号
                    newArrangeBillBoxModel.factNum = 0;//数量赋0,通过拣货箱明细计算数量
                    newArrangeBillBoxModel.palletNo = strNewPalletNo;//托盘号
                    newArrangeBillBoxModel.region = "";//区域
                    newArrangeBillBoxModel.oldPalletNo = dtArrangeBillBox.Rows[0]["palletNo"].ToString();//原托盘号
                    newArrangeBillBoxModel.oldRegion = dtArrangeBillBox.Rows[0]["region"].ToString();//原托内区域

                    //新箱库区、库位赋值,值为下线线体
                    if (dtWareLocator.Rows.Count > 0
                       && dtWareLocator.Rows[0]["wareLocatorNo"] != DBNull.Value
                       && dtWareLocator.Rows[0]["wareLocatorNo"].ToString() != "")
                    {
                        newArrangeBillBoxModel.wareNo = dtWareLocator.Rows[0]["wareNo"].ToString();//库区
                        newArrangeBillBoxModel.wareLocatorNo = dtWareLocator.Rows[0]["wareLocatorNo"].ToString();//库位
                    }

                    newArrangeBillBoxModel.preWareNo = "";//原库区
                    newArrangeBillBoxModel.preWareLocatorNo = "";//原库位
                    newArrangeBillBoxModel.isBoxArrange = true;//是否排托
                    newArrangeBillBoxModel.isPalletUsing = true;//托盘是否正在使用
                    newArrangeBillBoxModel.acceptEmpId = this.EmpID;//接收人
                    newArrangeBillBoxModel.acceptDt = System.DateTime.Now.ToString();//接收时间
                    newArrangeBillBoxModel.isOutStocking = false;//是否正在出库

                    arraneBillBoxBB.AddRecord(newArrangeBillBoxModel);
                }

                #endregion 新增新的排托箱

                #region 库存所有操作

                DataTable dtStock = new DataTable();
                DataTable dtMaterial = new DataTable();
                UStockOutInDetailData stockOutInDetailModel = new UStockOutInDetailData();

                //更改正式库存信息
                commBB.ExecuteSql("update dbo.UStock set num=num-" + num.ToString() + " where boxNo='" + strOldBoxNo + "'");

                //增加出入库明细-拣货箱出库
                dtStock = stockBB.GetList("boxNo='" + strOldBoxNo + "'").Tables[0];
                if (dtStock.Rows.Count > 0)//原箱有库存
                {
                    dtMaterial = materialBB.GetList("materialNo='" + dtStock.Rows[0]["materialNo"].ToString() + "'").Tables[0];

                    //保存出入库明细数据
                    stockOutInDetailModel = new UStockOutInDetailData();

                    stockOutInDetailModel.billNo = stockUpDetailModel.stockUpBillNo;//单据编号
                    stockOutInDetailModel.outInType = "06";//销售出库
                    stockOutInDetailModel.isOut = true;//是否出库
                    stockOutInDetailModel.stockDt = System.DateTime.Today.ToString();//库存日期
                    stockOutInDetailModel.dt = System.DateTime.Today.ToString();//操作日期
                    stockOutInDetailModel.wareNo = dtStock.Rows[0]["wareNo"].ToString();//仓库
                    stockOutInDetailModel.wareLocatorNo = dtStock.Rows[0]["wareLocatorNo"].ToString();//库位
                    stockOutInDetailModel.salverNo = dtStock.Rows[0]["palletNo"].ToString();//托盘号
                    stockOutInDetailModel.boxNo = strOldBoxNo;//箱号
                    stockOutInDetailModel.materialNo = dtStock.Rows[0]["materialNo"].ToString();//物料编号
                    stockOutInDetailModel.factMonomerNum = Convert.ToDouble(dtMaterial.Rows[0]["U_UintQty"]);//单体数量
                    stockOutInDetailModel.factPackNum = Convert.ToDouble(dtMaterial.Rows[0]["U_BoxQty"]);//整箱数量
                    stockOutInDetailModel.num = num;//数量
                    stockOutInDetailModel.isrtEmpId = this.empId;
                    stockOutInDetailModel.isrtDt = System.DateTime.Now.ToString();

                    stockOutInDetailBB.AddRecord(stockOutInDetailModel);
                }

                #endregion 库存所有操作

                #region 更改备货单明细的新托盘号、状态

                //更改备货单明细的新托盘号、状态
                vStockUpDetailModel = stockUpDetailBB.GetVModel(stockUpDetailID);

                stockUpDetailModel.palletNo = strNewPalletNo;//新托盘号

                if (vStockUpDetailModel.num > vStockUpDetailModel.factNum)
                {
                    stockUpDetailModel.instantState = "02";//更改状态为“备货中”
                }
                else
                {
                    stockUpDetailModel.instantState = "03";//更改状态为“备货完成”
                }

                stockUpDetailBB.ModifyRecord(stockUpDetailModel);

                #endregion 更改备货单明细的新托盘号、状态

                #region 生成从拣货区到打托区叉车任务

                bool isFinish = true;

                //如果某个托盘的备货单明细已经全部备货完成,生成托盘的叉车任务
                strSql = @"select palletNo,instantState from dbo.CStockUpDetail where stockUpBillNo='" + stockUpDetailModel.stockUpBillNo
                    + "' and palletIndex='" + stockUpDetailModel.palletIndex.ToString() + "'";

                dtStockUpDetail = commBB.Query(strSql).Tables[0];

                foreach (DataRow row in dtStockUpDetail.Rows)
                {
                    if (row["instantState"].ToString() == "01" || row["instantState"].ToString() == "02")
                    {
                        isFinish = false;
                        break;
                    }
                }

                if (isFinish)
                {
                    //保存从拣货区到打托区叉车任务
                    forkliftTaskBC.SaveForkliftTask(dtStockUpDetail.Rows[0]["palletNo"].ToString(), "", "", "14", this.empId);
                }

                #endregion 生成从拣货区到打托区叉车任务

                #region 更改备货单状态

                //如果备货单明细的状态全部为“03 备货完成”,更改备货单状态为“08 备货完成”
                strSql = @"update dbo.CStockUpBill set instantState='08'
                           where not exists(select 1 from dbo.CStockUpDetail as t where t.stockUpBillNo=dbo.CStockUpBill.stockUpBillNo and instantState<>'03')";

                commBB.ExecuteSql(strSql);

                #endregion 更改备货单状态

                #region 更改箱锁定状态

                int stockId = 0, boxId = 0;
                object obj = null;

                //查找库存
                obj = commBB.ExecuteScalar("select top 1 id from dbo.UStock where boxNo='" + strOldBoxNo + "' and num>0 and isOutStocking=1");
                if (obj == null)
                {
                    obj = commBB.ExecuteScalar("select top 1 id from dbo.UStock where palletNo='" + strPrePalletNo + "' and num>0 and isOutStocking=1 order by id");
                    if (obj != null)
                    {
                        stockId = Convert.ToInt32(obj);
                    }
                }
                else
                {
                    stockId = Convert.ToInt32(obj);
                }

                //查找物料箱
                obj = commBB.ExecuteScalar("select top 1 id from dbo.BArrangeBillBox where boxNo='" + strOldBoxNo + "' and wareNo<>'' and isOutStocking=1");
                if (obj == null)
                {
                    obj = commBB.ExecuteScalar("select top 1 id from dbo.BArrangeBillBox where palletNo='" + strPrePalletNo + "' and wareNo<>'' and isOutStocking=1 order by id");
                    if (obj != null)
                    {
                        boxId = Convert.ToInt32(obj);
                    }
                }
                else
                {
                    boxId = Convert.ToInt32(obj);
                }

                //更改库存中原箱的是否锁定状态
                strSql = "update dbo.UStock set isOutStocking=0 where id='" + stockId.ToString() + "'";
                commBB.ExecuteSql(strSql);

                //更改原箱的是否锁定状态
                strSql = "update dbo.BArrangeBillBox set isOutStocking=0 where id='" + boxId.ToString() + "'";
                commBB.ExecuteSql(strSql);

                #endregion 更改箱锁定状态

                #region 如果原箱剩余数量为零,更改原箱的托盘号

                if (strNewBoxNo == strOldBoxNo)
                {
                    if (dtWareLocator.Rows.Count > 0
                        && dtWareLocator.Rows[0]["wareLocatorNo"] != DBNull.Value
                        && dtWareLocator.Rows[0]["wareLocatorNo"].ToString() != "")
                    {
                        strSql = @"update dbo.BArrangeBillBox set oldMaterialNo=materialNo,materialNo='" + stockUpDetailModel.materialNo
                            + "',oldPalletNo=palletNo,palletNo='" + strNewPalletNo + "',preWareNo=wareNo,preWareLocatorNo=wareLocatorNo,wareNo='"
                            + dtWareLocator.Rows[0]["wareNo"].ToString() + "',wareLocatorNo='" + dtWareLocator.Rows[0]["wareLocatorNo"].ToString()
                            + "',oldRegion=region,region='' where boxNo='" + strOldBoxNo + "'";

                        commBB.ExecuteSql(strSql);
                    }
                }

                //如果箱内剩余数量为0,清空托盘号和库区、库位信息
                dtArrangeBillBox = arraneBillBoxBB.GetVList("boxNo='" + strOldBoxNo + "'").Tables[0];
                if (Convert.ToInt32(dtArrangeBillBox.Rows[0]["leavingNum"]) == 0)
                {
                    commBB.ExecuteSql(@"update dbo.BArrangeBillBox set oldPalletNo=palletNo,palletNo='',preWareNo=wareNo,preWareLocatorNo=wareLocatorNo,
                                        wareNo='',wareLocatorNo='' where boxNo='" + strOldBoxNo + "'");
                }

                #endregion 如果原箱剩余数量为零,更改原箱的托盘号

                #region 原库位释放

                //释放库位,首先判断原托盘上是否有物料
                strSql = @"update dbo.LWareLocator set isUsing=0
                           where not exists(select 1 from dbo.BArrangeBillBox as t where t.palletNo='"
                    + strPrePalletNo + "' and isnull(t.wareNo,'')<>'') and wareLocatorNo='"
                    + strPreWareLocatorNo + "' and isUsing=1 and isDel=0";

                commBB.ExecuteSql(strSql);

                #endregion 原库位释放

                #region 出入库所有操作

                //库存明细数量为0,删除信息
                strSql = "delete from dbo.UStock where num=0";
                commBB.ExecuteSql(strSql);

                #endregion 出入库所有操作

                if (this.transaction == null) trans.Commit();
                ret = true;
            }
            catch (Exception ex)
            {
                if (this.transaction == null) trans.Rollback("TransSaveOutStockInfo");
                throw ex;
            }
            finally
            {
                arraneBillBoxBB.Dispose();
                forkliftTaskBC.Dispose();
                stockUpDetailBB.Dispose();
                commBB.Dispose();
                saleDetailBB.Dispose();
                DEPickOutBoxBB.Dispose();
                stockBB.Dispose();
                stockOutInDetailBB.Dispose();
                materialBB.Dispose();
            }

            return ret;
        }