Esempio n. 1
0
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="newData">数据</param>
        public void AddData(StockInModel newData)
        {
            Sto_StockBusiness _stockBus = new Sto_StockBusiness();

            if (newData.Id.IsNullOrEmpty())
            {
                newData.Id = Guid.NewGuid().ToSequentialGuid();
                //生成入库单编号
                this.BeginTransaction();
                this._StockInItemBusiness.BeginTransaction();
                _stockBus.BeginTransaction();

                foreach (Sto_StockInItem item in newData.StockInItems)
                {
                    item.Id   = Guid.NewGuid().ToSequentialGuid();
                    item.InNo = newData.InNo;
                    //检查是否存在期初库存,如没有期初库存记录,默认添加期初为0的期初库存
                    var count = _stockBus.GetIQueryable().Where(p => p.MatNo == item.MatNo).Count();
                    if (count == 0)
                    {
                        Sto_Stock stock = new Sto_Stock()
                        {
                            Id          = Guid.NewGuid().ToSequentialGuid(),
                            MatNo       = item.MatNo,
                            GuiGe       = item.GuiGe,
                            MatName     = item.MatName,
                            Quantity    = 0,
                            StoreId     = newData.StoreId,
                            UnitNo      = item.UnitNo,
                            StoreUnitId = "",
                            Price       = item.Price,
                            UpToTime    = DateTime.Now.AddDays(-1),
                            BigClass    = item.BigClass
                        };
                        _stockBus.Insert(stock);
                    }
                }

                this._StockInItemBusiness.BulkInsert(newData.StockInItems);
                Sto_StockIn stockIn = new Sto_StockIn()
                {
                    Id        = newData.Id,
                    InNo      = newData.InNo,
                    InDate    = newData.InDate,
                    InOperID  = newData.InOperID,
                    State     = newData.State,
                    StoreId   = newData.StoreId,
                    AuditDate = newData.AuditDate,
                    Auditor   = newData.Auditor,
                    Context   = newData.Context,
                    InType    = newData.InType
                };

                Insert(stockIn);
                if (this._StockInItemBusiness.EndTransaction())
                {
                    if (!this.EndTransaction())
                    {
                        foreach (Sto_StockInItem item in newData.StockInItems)
                        {
                            this._StockInItemBusiness.Delete(item);
                        }

                        throw new Exception("保存数据失败");
                    }
                    _stockBus.EndTransaction();
                }
            }
            else
            {
                UpdateData(newData);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 更新数据
        /// </summary>
        public void UpdateData(StockInModel theData)
        {
            Sto_StockBusiness _stockBus = new Sto_StockBusiness();

            var           oldStockInItems = this._StockInItemBusiness.GetIQueryable().Where(p => p.InNo == theData.InNo).ToList();
            var           itemIds         = (from p in oldStockInItems select p.Id).ToList <string>();
            List <string> thisItemIds     = new List <string>();
            var           stockIn         = this.GetEntity(theData.Id);
            var           copyIn          = stockIn.DeepClone();

            if (stockIn == null)
            {
                throw new Exception("要修改的入库单不存在,可能已被删除");
            }
            if (stockIn.State != 0)
            {
                throw new Exception("非录入状态不能修改");
            }

            this.BeginTransaction();
            this._StockInItemBusiness.BeginTransaction();
            _stockBus.BeginTransaction();

            foreach (Sto_StockInItem item in theData.StockInItems)
            {
                if (item.Id.IsNullOrEmpty())
                {
                    item.Id   = Guid.NewGuid().ToSequentialGuid();
                    item.InNo = theData.InNo;
                    this._StockInItemBusiness.Insert(item);
                    //检查是否存在期初库存,如没有期初库存记录,默认添加期初为0的期初库存
                    var count = _stockBus.GetIQueryable().Where(p => p.MatNo == item.MatNo).Count();
                    if (count == 0)
                    {
                        Sto_Stock stock = new Sto_Stock()
                        {
                            Id          = Guid.NewGuid().ToSequentialGuid(),
                            MatNo       = item.MatNo,
                            GuiGe       = item.GuiGe,
                            MatName     = item.MatName,
                            Quantity    = 0,
                            StoreId     = theData.StoreId,
                            UnitNo      = item.UnitNo,
                            StoreUnitId = "",
                            Price       = item.Price,
                            UpToTime    = DateTime.Now.AddDays(-1),
                            BigClass    = item.BigClass
                        };
                        _stockBus.Insert(stock);
                    }
                }
                else
                {
                    thisItemIds.Add(item.Id);
                    item.InNo = theData.InNo;
                    this._StockInItemBusiness.Update(item);
                }
            }
            var exceptList = itemIds.Except(thisItemIds).ToList();

            this._StockInItemBusiness.Delete(exceptList);
            stockIn.InNo      = theData.InNo;
            stockIn.InDate    = theData.InDate;
            stockIn.InOperID  = theData.InOperID;
            stockIn.State     = theData.State;
            stockIn.StoreId   = theData.StoreId;
            stockIn.AuditDate = theData.AuditDate;
            stockIn.Auditor   = theData.Auditor;
            stockIn.Context   = theData.Context;

            Update(stockIn);
            if (this.EndTransaction())
            {
                if (!this._StockInItemBusiness.EndTransaction())
                {
                    stockIn.InNo      = copyIn.InNo;
                    stockIn.InDate    = copyIn.InDate;
                    stockIn.InOperID  = copyIn.InOperID;
                    stockIn.State     = copyIn.State;
                    stockIn.StoreId   = copyIn.StoreId;
                    stockIn.AuditDate = copyIn.AuditDate;
                    stockIn.Auditor   = copyIn.Auditor;
                    stockIn.Context   = copyIn.Context;
                    this.Update(stockIn);
                    throw new Exception("修改入库单失败。");
                }
                _stockBus.EndTransaction();
            }
        }