예제 #1
0
        //删除出库单
        public static void DelCKD(int i_OutStockID)
        {
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                OutStock toDel1 = session.Get <OutStock>(i_OutStockID);


                //删除OutStockItem中的相关项
                List <int> toDelItemsID = BLL.CKBLL.GetItemIDs(toDel1.OutStockID);
                foreach (int i in toDelItemsID)
                {
                    OutStockItems toDel2 = session.Get <OutStockItems>(i);
                    session.Delete(toDel2);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = toDel2.GoodsID;
                    pk.WareHouseID = toDel1.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH != null)
                    {
                        tWH.Number += toDel2.GoodsQty;
                        tWH.Amount += toDel2.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        else
                        {
                            tWH.Price = 0;
                        }
                    }
                }
                session.Delete(toDel1);
                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #2
0
        //加入WareHouseStock
        public static void AddWareHouseStock(WareHouseStock tNew)
        {
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                if (IsWareHouseStockHas(tNew.Pk.WareHouseID, tNew.Pk.GoodsID))
                {
                    session.Close();
                    return;
                }
                session.Save(tNew);
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #3
0
        //合并货品
        public static void MergeGoods(Goods tG1, Goods tG2)
        {
            //更新ReceiptItems表
            string sSQL = "UPDATE ReceiptItems SET GoodsID = " + tG2.GoodsID.ToString() + ",GoodsName = '"
                          + tG2.GoodsName + "', GoodsSpec = '" + tG2.GoodsSpec + "', GoodsUnit = '" + tG2.GoodsUnit
                          + "' WHERE GoodsID = " + tG1.GoodsID.ToString();

            SQLiteHelper.ExecuteNonQuery(sSQL);
            //更新OutStockItems表
            sSQL = "UPDATE OutStockItems SET GoodsID = " + tG2.GoodsID.ToString() + ",GoodsName = '"
                   + tG2.GoodsName + "', GoodsSpec = '" + tG2.GoodsSpec + "', GoodsUnit = '" + tG2.GoodsUnit
                   + "' WHERE GoodsID = " + tG1.GoodsID.ToString();
            SQLiteHelper.ExecuteNonQuery(sSQL);
            //更新WareHouseStock表
            DataSet      ds1     = BLL.WareHouseBLL.GetGoods1(tG1.GoodsID);
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                Goods tDelGoods = session.Get <Goods>(tG1.GoodsID);
                foreach (DataRow row1 in ds1.Tables[0].Rows)
                {
                    string sGoodsID1     = row1["GoodsID"].ToString();
                    string sWareHouseID1 = row1["WareHouseID"].ToString();

                    PKModel pk = new PKModel();
                    pk.GoodsID     = tG1.GoodsID;
                    pk.WareHouseID = Convert.ToInt32(sWareHouseID1);

                    PKModel pk2 = new PKModel();
                    pk2.GoodsID     = tG2.GoodsID;
                    pk2.WareHouseID = Convert.ToInt32(sWareHouseID1);

                    WareHouseStock tWH1 = session.Get <WareHouseStock>(pk);
                    WareHouseStock tWH2 = session.Get <WareHouseStock>(pk2);

                    if (tWH2 == null)
                    {
                        //合并到的目的此仓库无库存记录
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk        = pk2;
                        tWHNew.Amount    = tWH1.Amount;
                        tWHNew.LastPrice = tWH1.LastPrice;
                        tWHNew.Number    = tWH1.Number;
                        tWHNew.Price     = tWH1.Price;
                        session.Save(tWHNew);
                        //删除
                        session.Delete(tWH1);
                    }
                    else
                    {
                        //合并到的目的此仓库有库存记录
                        tWH2.Number += tWH1.Number;
                        tWH2.Amount += tWH1.Amount;
                        if (tWH2.Number != 0)
                        {
                            tWH2.Price = Math.Round(tWH2.Amount / tWH2.Number, 2);
                        }
                        session.Flush();
                        //删除仓库货品
                        session.Delete(tWH1);
                    }
                }
                //删除货品
                session.Delete(tDelGoods);
                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #4
0
        //删除入库单
        public static void DelRKD(int i_ReceiptID)
        {
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                Receipt toDel1 = session.Get <Receipt>(i_ReceiptID);

                //更新未付款
                //如果原应付款不为0,则删除AccountBill表的相关数据,并更新WareHouseStock表中的相关数据
                if (toDel1.ReceiptDiscAmt != 0)
                {
                    AccountBill tAB = BLL.AccountBillBLL.GetAccountBill(toDel1.ProjectID, toDel1.CompanyID, 100, "采购应付款" + toDel1.ReceiptNum);
                    if (tAB.BillNo == 0)
                    {
                        tx.Rollback();
                        session.Close();
                        MessageBox.Show("没有找到可以删除的入库单生成的应付款记录!请检查数据库!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    AccountBill toDelete = session.Get <AccountBill>(tAB.BillNo);
                    session.Delete(toDelete);
                }
                //删除ReceiptItem中的相关项
                List <int> toDelItemsID = BLL.RKBLL.GetItemIDs(toDel1.ReceiptID);
                foreach (int i in toDelItemsID)
                {
                    ReceiptItems toDel2 = session.Get <ReceiptItems>(i);
                    session.Delete(toDel2);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = toDel2.GoodsID;
                    pk.WareHouseID = toDel1.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH != null)
                    {
                        tWH.Number -= toDel2.GoodsQty;
                        tWH.Amount -= toDel2.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        else
                        {
                            tWH.Price = 0;
                        }
                    }
                }
                session.Delete(toDel1);
                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #5
0
        //修改入库单
        public static void ModifyRKD(Receipt tReceipt, List <ReceiptItems> tItemsList)
        {
            //修改入库单
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                //得到未变更的入库单
                Receipt tOldReceipt = new Receipt();
                tOldReceipt = BLL.RKBLL.GetReceipt(tReceipt.ReceiptID);
                //如果原应付款不为0,则删除AccountBill表的相关数据,并更新Companies表中的相关数据
                if (tOldReceipt.ReceiptDiscAmt != 0)
                {
                    AccountBill tAB = BLL.AccountBillBLL.GetAccountBill(tOldReceipt.ProjectID, tOldReceipt.CompanyID, 100, "采购应付款" + tOldReceipt.ReceiptNum);
                    if (tAB.BillNo == 0)
                    {
                        tx.Rollback();
                        session.Close();
                        MessageBox.Show("没有找到可以删除的入库单生成的应付款记录!请检查数据库!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    AccountBill toDelete = session.Get <AccountBill>(tAB.BillNo);
                    session.Delete(toDelete);
                }
                //删除ReceiptItem中的相关项
                List <int> toDelItemsID = BLL.RKBLL.GetItemIDs(tOldReceipt.ReceiptID);
                foreach (int i in toDelItemsID)
                {
                    ReceiptItems toDel1 = session.Get <ReceiptItems>(i);
                    session.Delete(toDel1);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = toDel1.GoodsID;
                    pk.WareHouseID = tOldReceipt.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number = toDel1.GoodsQty;
                        tWHNew.Price  = toDel1.GoodsPrc;
                        tWHNew.Amount = toDel1.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number -= toDel1.GoodsQty;
                        tWH.Amount -= toDel1.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }
                //保存Receipt的修改
                Receipt toModify = session.Get <Receipt>(tOldReceipt.ReceiptID);
                toModify.CompanyID      = tReceipt.CompanyID;
                toModify.ProjectID      = tReceipt.ProjectID;
                toModify.ReceiptType    = tReceipt.ReceiptType;
                toModify.ReceiptBillAmt = tReceipt.ReceiptBillAmt;
                toModify.ReceiptDisc    = tReceipt.ReceiptDisc;
                toModify.ReceiptDiscAmt = tReceipt.ReceiptDiscAmt;
                toModify.PurchName      = tReceipt.PurchName;
                toModify.ReceiverName   = tReceipt.ReceiverName;
                toModify.RecordStatus   = "已记账";
                toModify.Remark         = tReceipt.Remark;
                toModify.WareHouseID    = tReceipt.WareHouseID;


                //将新的ReceiptItem加入
                foreach (ReceiptItems tRI in tItemsList)
                {
                    tRI.ReceiptID = tReceipt.ReceiptID;
                    session.Save(tRI);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = tRI.GoodsID;
                    pk.WareHouseID = tReceipt.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number = tRI.GoodsQty;
                        tWHNew.Price  = tRI.GoodsPrc;
                        tWHNew.Amount = tRI.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number += tRI.GoodsQty;
                        tWH.Amount += tRI.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }
                //根据应付款是否为0添加AccountBill记录及更新Companies表
                if (tReceipt.ReceiptDiscAmt != 0)
                {
                    Projects    tP1  = session.Get <Projects>(tReceipt.ProjectID);
                    Companies   tC1  = session.Get <Companies>(tReceipt.CompanyID);
                    AccountBill tAcc = new AccountBill();
                    tAcc.CompanyID     = tReceipt.CompanyID;
                    tAcc.CompanyName   = tC1.CompanyName;
                    tAcc.BillDate      = tReceipt.ReceiptDate;
                    tAcc.BillMemo      = "采购应付款" + tReceipt.ReceiptNum;
                    tAcc.BillType      = 1;
                    tAcc.BillYF        = tReceipt.ReceiptDiscAmt;
                    tAcc.ProjectID     = tReceipt.ProjectID;
                    tAcc.ProjectName   = tP1.ProjectName;
                    tAcc.MoneyTypeID   = 100;                     //材料购买
                    tAcc.MoneyTypeName = "入库单生成";
                    tAcc.BillStatus    = "已记账";
                    session.Save(tAcc);
                }

                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #6
0
        //增加入库单
        public static void AddRKD(Receipt tReceipt, List <ReceiptItems> tItemsList)
        {
            //保存数据,注意使用事务进行处理
            //1、入库单号的生成与修改;2、入库单的增加;3入库单项目的增加
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                //查最后的入库单号
                IList <ProgOptions> Key1 = BLL.ProgOptionsBLL.GetOptions("RKD_LastNumber");
                string s_RKD_LastNo      = Key1[0].OptionsValue;
                string s_RKD_NewNo;
                string s_RKD_LastYM;
                string s_RKD_NewYM;
                int    LastNo;
                int    NewNo;
                string s_NewNo;
                s_RKD_NewYM = DateTime.Today.ToString("yyyyMM");
                if (s_RKD_LastNo.Length == 10)
                {
                    //4位年+2位月+4位顺序号
                    s_RKD_LastYM = s_RKD_LastNo.Substring(0, 6);
                    LastNo       = Convert.ToInt32(s_RKD_LastNo.Substring(6, 4));
                    if (s_RKD_NewYM != s_RKD_LastYM)
                    {
                        NewNo = 1;
                    }
                    else
                    {
                        NewNo = LastNo + 1;
                    }
                }
                else
                {
                    //没有数据
                    NewNo = 1;
                }
                s_NewNo = "0000" + NewNo.ToString();
                s_NewNo = s_NewNo.Substring(s_NewNo.Length - 4, 4);
                //将新的最后编号保存到数据库
                ProgOptions t1 = session.Get <ProgOptions>(Key1[0].OptionsID);
                t1.OptionsID     = Key1[0].OptionsID;
                t1.OptionsKey    = Key1[0].OptionsKey;
                s_RKD_NewNo      = s_RKD_NewYM + s_NewNo;
                t1.OptionsValue  = s_RKD_NewNo;
                t1.OptionsRemark = Key1[0].OptionsRemark;

                //保存入库单
                tReceipt.ReceiptNum = "RKD-" + t1.OptionsValue;
                session.Save(tReceipt);

                //添加应付款

                if (tReceipt.ReceiptDiscAmt != 0)
                {
                    Projects    tP1  = session.Get <Projects>(tReceipt.ProjectID);
                    Companies   tC1  = session.Get <Companies>(tReceipt.CompanyID);
                    AccountBill tAcc = new AccountBill();
                    tAcc.CompanyID     = tReceipt.CompanyID;
                    tAcc.CompanyName   = tC1.CompanyName;
                    tAcc.BillDate      = tReceipt.ReceiptDate;
                    tAcc.BillMemo      = "采购应付款" + tReceipt.ReceiptNum;
                    tAcc.BillType      = 1;
                    tAcc.BillYF        = tReceipt.ReceiptDiscAmt;
                    tAcc.ProjectID     = tReceipt.ProjectID;
                    tAcc.ProjectName   = tP1.ProjectName;
                    tAcc.MoneyTypeID   = 100;                     //材料购买
                    tAcc.MoneyTypeName = "入库单生成";
                    tAcc.BillStatus    = "已记账";
                    session.Save(tAcc);
                }

                foreach (ReceiptItems tRI in tItemsList)
                {
                    //保存进货项
                    ReceiptItems tNewReceiptItems = new ReceiptItems();
                    tNewReceiptItems.GoodsID         = tRI.GoodsID;
                    tNewReceiptItems.ReceiptID       = tReceipt.ReceiptID;
                    tNewReceiptItems.GoodsName       = tRI.GoodsName;
                    tNewReceiptItems.GoodsAmt        = tRI.GoodsAmt;
                    tNewReceiptItems.GoodsPlan       = tRI.GoodsPlan;
                    tNewReceiptItems.GoodsPlanNo     = tRI.GoodsPlanNo;
                    tNewReceiptItems.GoodsPrc        = tRI.GoodsPrc;
                    tNewReceiptItems.GoodsQty        = tRI.GoodsQty;
                    tNewReceiptItems.GoodsSpec       = tRI.GoodsSpec;
                    tNewReceiptItems.GoodsUnit       = tRI.GoodsUnit;
                    tNewReceiptItems.GoodsYF         = tRI.GoodsYF;
                    tNewReceiptItems.GoodsTaxRate    = tRI.GoodsTaxRate;
                    tNewReceiptItems.GoodsNoTaxPrice = tRI.GoodsNoTaxPrice;
                    tNewReceiptItems.MoreSpec        = tRI.MoreSpec;
                    tNewReceiptItems.UsePosition     = tRI.UsePosition;

                    session.Save(tNewReceiptItems);
                }

                //入库单同货品合并
                List <ReceiptItems> mergeList = new List <ReceiptItems>();
                foreach (ReceiptItems tRI in tItemsList)
                {
                    ReceiptItems rtnRItem = mergeList.Find(name =>
                    {
                        if (name.GoodsID == tRI.GoodsID)
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    });
                    if (rtnRItem == null)
                    {
                        mergeList.Add(tRI);
                    }
                    else
                    {
                        rtnRItem.GoodsQty += tRI.GoodsQty;
                        rtnRItem.GoodsAmt += tRI.GoodsAmt;
                        if (rtnRItem.GoodsQty != 0)
                        {
                            rtnRItem.GoodsPrc = rtnRItem.GoodsAmt / rtnRItem.GoodsQty;
                        }
                    }
                }

                //更新库存
                foreach (ReceiptItems tRI in mergeList)
                {
                    PKModel pk = new PKModel();
                    pk.GoodsID     = tRI.GoodsID;
                    pk.WareHouseID = tReceipt.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number    = tRI.GoodsQty;
                        tWHNew.LastPrice = tRI.GoodsPrc;
                        tWHNew.Price     = tRI.GoodsPrc;
                        tWHNew.Amount    = tRI.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number   += tRI.GoodsQty;
                        tWH.Amount   += tRI.GoodsAmt;
                        tWH.LastPrice = tRI.GoodsPrc;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }
                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #7
0
        //修改出库单
        public static void ModifyCKD(OutStock tOutStock, List <OutStockItems> tItemsList)
        {
            //修改出库单
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                //得到未变更的出库单
                OutStock tOldOutStock = new OutStock();
                tOldOutStock = BLL.CKBLL.GetOutStock(tOutStock.OutStockID);

                //删除OutStockItem中的相关项
                List <int> toDelItemsID = BLL.CKBLL.GetItemIDs(tOldOutStock.OutStockID);
                foreach (int i in toDelItemsID)
                {
                    OutStockItems toDel1 = session.Get <OutStockItems>(i);
                    session.Delete(toDel1);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = toDel1.GoodsID;
                    pk.WareHouseID = tOldOutStock.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number = -toDel1.GoodsQty;
                        tWHNew.Price  = toDel1.GoodsPrc;
                        tWHNew.Amount = -toDel1.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number += toDel1.GoodsQty;
                        tWH.Amount += toDel1.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }
                //保存OutStock的修改
                OutStock toModify = session.Get <OutStock>(tOldOutStock.OutStockID);
                toModify.CompanyID     = tOutStock.CompanyID;
                toModify.ProjectID     = tOutStock.ProjectID;
                toModify.OutBillAmt    = tOutStock.OutBillAmt;
                toModify.OutBillRemark = tOutStock.OutBillRemark;
                toModify.WareHouseID   = tOutStock.WareHouseID;
                toModify.OutStockType  = tOutStock.OutStockType;

                //将新的OutStockItem加入
                foreach (OutStockItems tOI in tItemsList)
                {
                    tOI.OutStockID = tOutStock.OutStockID;
                    session.Save(tOI);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = tOI.GoodsID;
                    pk.WareHouseID = tOutStock.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number = -tOI.GoodsQty;
                        tWHNew.Price  = tOI.GoodsPrc;
                        tWHNew.Amount = -tOI.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number -= tOI.GoodsQty;
                        tWH.Amount -= tOI.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }


                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }
예제 #8
0
        //增加出库单
        public static void AddCKD(OutStock tOutStock, List <OutStockItems> tItemsList)
        {
            //保存数据,注意使用事务进行处理
            //1、出库单号的生成与修改;2、出库单的增加;3、出库单项目的增加
            ISession     session = NHibernateHelper.sessionFactory.OpenSession();
            ITransaction tx      = session.BeginTransaction();

            try
            {
                //查最后的出库单号
                IList <ProgOptions> Key1 = BLL.ProgOptionsBLL.GetOptions("CKD_LastNumber");
                string s_CKD_LastNo      = Key1[0].OptionsValue;
                string s_CKD_NewNo;
                string s_CKD_LastYM;
                string s_CKD_NewYM;
                int    LastNo;
                int    NewNo;
                string s_NewNo;
                s_CKD_NewYM = DateTime.Today.ToString("yyyyMM");
                if (s_CKD_LastNo.Length == 10)
                {
                    //4位年+2位月+4位顺序号
                    s_CKD_LastYM = s_CKD_LastNo.Substring(0, 6);
                    LastNo       = Convert.ToInt32(s_CKD_LastNo.Substring(6, 4));
                    if (s_CKD_NewYM != s_CKD_LastYM)
                    {
                        NewNo = 1;
                    }
                    else
                    {
                        NewNo = LastNo + 1;
                    }
                }
                else
                {
                    //没有数据
                    NewNo = 1;
                }
                s_NewNo = "0000" + NewNo.ToString();
                s_NewNo = s_NewNo.Substring(s_NewNo.Length - 4, 4);
                //将新的最后编号保存到数据库
                ProgOptions t1 = session.Get <ProgOptions>(Key1[0].OptionsID);
                t1.OptionsID     = Key1[0].OptionsID;
                t1.OptionsKey    = Key1[0].OptionsKey;
                s_CKD_NewNo      = s_CKD_NewYM + s_NewNo;
                t1.OptionsValue  = s_CKD_NewNo;
                t1.OptionsRemark = Key1[0].OptionsRemark;

                //保存出库单
                tOutStock.OutStockNum = "CKD-" + t1.OptionsValue;
                session.Save(tOutStock);


                //保存出库单项
                foreach (OutStockItems tOI in tItemsList)
                {
                    tOI.OutStockID = tOutStock.OutStockID;
                    session.Save(tOI);
                    //更新库存
                    PKModel pk = new PKModel();
                    pk.GoodsID     = tOI.GoodsID;
                    pk.WareHouseID = tOutStock.WareHouseID;
                    WareHouseStock tWH = session.Get <WareHouseStock>(pk);
                    if (tWH == null)
                    {
                        WareHouseStock tWHNew = new WareHouseStock();
                        tWHNew.Pk = pk;

                        tWHNew.Number = -tOI.GoodsQty;
                        tWHNew.Price  = tOI.GoodsPrc;
                        tWHNew.Amount = -tOI.GoodsAmt;
                        session.Save(tWHNew);
                    }
                    else
                    {
                        tWH.Number -= tOI.GoodsQty;
                        tWH.Amount -= tOI.GoodsAmt;
                        if (tWH.Number != 0)
                        {
                            tWH.Price = Math.Round(tWH.Amount / tWH.Number, 2);
                        }
                        session.Flush();
                    }
                }
                //事务提交
                tx.Commit();
                session.Close();
            }
            catch (Exception e)
            {
                Debug.Assert(false, e.Message);
                tx.Rollback();
                session.Close();
            }
        }