Exemplo n.º 1
0
        /// <summary>绑定数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridGoodsStock_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GridGoodsStock.MasterTableView.Columns[2].Display = IsPostBack;
            IList <GoodsStockSearchInfo> goodsStockSearchList = new List <GoodsStockSearchInfo>();
            var pageIndex  = GridGoodsStock.CurrentPageIndex + 1;
            int pageSize   = GridGoodsStock.PageSize;
            int totalCount = 0;

            if (IsPostBack)
            {
                if (WarehouseId == Guid.Empty)
                {
                    RAM.Alert("您没有授权的仓库!");
                }
                else
                {
                    var goodsIdList = new List <Guid>();
                    if (CompanyId != Guid.Empty)
                    {
                        var purchaseings = _purchaseSet.GetPurchaseSetListByWarehouseIdAndCompanyId(WarehouseId, CompanyId);
                        if (purchaseings.Count == 0)
                        {
                            RAM.Alert("系统提示:当前选择供应商没有设置相关商品,无法为您查询库存信息!");
                            return;
                        }
                        goodsIdList = purchaseings.Select(act => act.GoodsId).ToList();
                    }
                    var goodsInfoList = _goodsCenterSao.GetGoodsStockGridList(GoodsClassId, SearchText, goodsIdList, pageIndex, pageSize, out totalCount).ToList();
                    if (goodsInfoList.Count > 0)
                    {
                        var goodsIds       = goodsInfoList.Select(ent => ent.GoodsId).ToList();
                        var childGoodsList = _goodsCenterSao.GetRealGoodsListByGoodsId(goodsIds).ToList();
                        var list           = WMSSao.StockSearchList(childGoodsList.Select(ent => ent.RealGoodsId), WarehouseId, HostingFilialeId);
                        FilialeDic =
                            (from item in list.GroupBy(ent => ent.FilialeId) let filiale = item.First() select filiale)
                            .ToDictionary(k => k.FilialeId, v => v.FilialeName);
                        Dictionary <Guid, List <Guid> >            dics        = new Dictionary <Guid, List <Guid> >();
                        Dictionary <Guid, Dictionary <Guid, int> > goodsStocks = new Dictionary <Guid, Dictionary <Guid, int> >();
                        //根据仓库Id获取供应商Id
                        var dicGoodsIdAndCompanyId = _purchaseSet.GetAllPurchaseSet(WarehouseId);
                        //获取商品的最后一次进货价信息
                        var goodsPurchaseLastPriceInfoList = _storageRecordDao.GetGoodsPurchaseLastPriceInfoByWarehouseId(WarehouseId);
                        goodsPurchaseLastPriceInfoList = goodsPurchaseLastPriceInfoList.Where(p => goodsIds.Contains(p.GoodsId)).ToList();

                        foreach (var info in goodsInfoList)
                        {
                            var realGoodsIds = childGoodsList.Where(ent => ent.GoodsId == info.GoodsId).Select(ent => ent.RealGoodsId);
                            dics.Add(info.GoodsId, realGoodsIds.ToList());
                            goodsStocks.Add(info.GoodsId, new Dictionary <Guid, int>());
                            if (childGoodsList.Count > 0)
                            {
                                var goodsStockSearchInfo = new GoodsStockSearchInfo();
                                //根据商品id获取供应商
                                var companyIds = dicGoodsIdAndCompanyId.Where(ent => ent.GoodsId == info.GoodsId).Select(ent => ent.CompanyId);

                                decimal unitPrice = 0;
                                GoodsPurchaseLastPriceInfo goodsPurchaseLastPriceInfo = null;
                                if (goodsPurchaseLastPriceInfoList.Count > 0)
                                {
                                    goodsPurchaseLastPriceInfo = goodsPurchaseLastPriceInfoList.FirstOrDefault(p => p.GoodsId.Equals(info.GoodsId) && companyIds.Contains(p.ThirdCompanyId));
                                    unitPrice = goodsPurchaseLastPriceInfo != null ? goodsPurchaseLastPriceInfo.UnitPrice : 0;
                                }
                                goodsStockSearchInfo.RecentCDate   = goodsPurchaseLastPriceInfo != null ? goodsPurchaseLastPriceInfo.LastPriceDate : DateTime.MinValue;
                                goodsStockSearchInfo.RecentInPrice = unitPrice;

                                if (list != null && list.Count > 0)
                                {
                                    int quantity = 0;
                                    foreach (var item in list)
                                    {
                                        var filiaeQuantity = item.StockQuantity.Where(ent => realGoodsIds.Contains(ent.Key)).Sum(ent => ent.Value);
                                        quantity += filiaeQuantity;
                                        goodsStocks[info.GoodsId].Add(item.FilialeId, filiaeQuantity);
                                    }
                                    goodsStockSearchInfo.GoodsId         = info.GoodsId;
                                    goodsStockSearchInfo.GoodsName       = info.GoodsName;
                                    goodsStockSearchInfo.GoodsCode       = info.GoodsCode;
                                    goodsStockSearchInfo.IsScarcity      = info.IsStockScarcity;
                                    goodsStockSearchInfo.IsOnShelf       = info.IsOnShelf;
                                    goodsStockSearchInfo.CurrentQuantity = quantity;
                                    goodsStockSearchInfo.CurrentSumPrice = quantity * unitPrice;
                                }
                                else
                                {
                                    goodsStockSearchInfo.GoodsId    = info.GoodsId;
                                    goodsStockSearchInfo.GoodsName  = info.GoodsName;
                                    goodsStockSearchInfo.GoodsCode  = info.GoodsCode;
                                    goodsStockSearchInfo.IsScarcity = info.IsStockScarcity;
                                    goodsStockSearchInfo.IsOnShelf  = info.IsOnShelf;
                                }
                                goodsStockSearchList.Add(goodsStockSearchInfo);
                            }
                        }
                        StockSearchDic = goodsStocks;
                    }
                }
            }
            #region FooterText 统计显示
            var recentCDate = GridGoodsStock.MasterTableView.Columns.FindByUniqueName("RecentCDate");
            var isOnShelf   = GridGoodsStock.MasterTableView.Columns.FindByUniqueName("IsOnShelf");
            if (goodsStockSearchList.Count > 0)
            {
                var currentSumPrice = goodsStockSearchList.Sum(ent => ent.CurrentSumPrice);
                recentCDate.FooterText = "库存金额总计:";
                isOnShelf.FooterText   = WebControl.NumberSeparator(currentSumPrice.ToString("N"));
            }
            else
            {
                recentCDate.FooterText = "库存金额总计:-";
            }
            #endregion

            GridGoodsStock.DataSource       = goodsStockSearchList.OrderByDescending(w => w.CurrentQuantity).ToList();
            GridGoodsStock.VirtualItemCount = totalCount;
        }