예제 #1
0
        /// <summary>绑定数据源
        /// </summary>
        protected void GridGoodsStock_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            IList <StockTurnOverInfo> stockTurnOverList = new List <StockTurnOverInfo>();

            if (!WarehouseId.Equals(Guid.Empty))
            {
                //选择了责任人,则获取当前责任人负责的商品ID集合
                var goodsIds = new List <Guid>();
                if (PersonnelId != Guid.Empty || CompanyId != Guid.Empty)
                {
                    goodsIds = _purchaseSet.GetGoodsIdByPersonnelId(PersonnelId, CompanyId).ToList();
                }
                //根据条件获取商品信息
                List <GoodsPerformance> goodsList = _goodsCenterSao.GetGoodsPerformanceList(GoodsClassId, goodsIds,
                                                                                            GoodsNameOrCode, CB_IsPerformance.Checked);
                var tempGoodsIds = new List <Guid>();
                //var isNeedStock = true;
                switch (State)
                {
                case 0:     //全部
                    tempGoodsIds = goodsList.Select(ent => ent.GoodsId).ToList();
                    break;

                case 1:     //下架缺货有库存
                            //备注:  PurchaseState 0下架, 1上架
                    tempGoodsIds.AddRange(from item in goodsList
                                          where item.IsScarcity || item.PurchaseState == 0
                                          select item.GoodsId);
                    //isNeedStock = true;
                    break;

                case 2:     //无销售商品
                    tempGoodsIds.AddRange(from item in goodsList
                                          where !_salesDic.ContainsKey(item.GoodsId)
                                          select item.GoodsId);
                    var newGoodsList =
                        tempGoodsIds.Select(goodsId => goodsList.FirstOrDefault(ent => ent.GoodsId == goodsId)).ToList();
                    goodsList.Clear();
                    goodsList.AddRange(newGoodsList);
                    break;
                }

                //获取商品库存信息
                var goodsStockList = WMSSao.StockSearchByGoodsIds(tempGoodsIds, WarehouseId, FilialeId) ?? new Dictionary <Guid, int>();
                //获取所有的商品采购设置
                var allGoodsPurchaseSet = _purchaseSet.GetAllPurchaseSet(WarehouseId);

                var companyIdList = allGoodsPurchaseSet.Select(p => p.CompanyId).Distinct().ToList();
                //获取商品的最后一次进货价信息
                var goodsPurchaseLastPriceInfoList = _storageRecordDao.GetGoodsPurchaseLastPriceInfoByWarehouseId(WarehouseId);
                goodsPurchaseLastPriceInfoList = goodsPurchaseLastPriceInfoList.Where(p => tempGoodsIds.Contains(p.GoodsId) && companyIdList.Contains(p.ThirdCompanyId)).ToList();

                foreach (var goodsInfo in goodsList)
                {
                    // ReSharper disable once UseObjectOrCollectionInitializer
                    var info = new StockTurnOverInfo();
                    info.GoodsID   = goodsInfo.GoodsId;
                    info.GoodsName = goodsInfo.GoodsName;
                    info.GoodsCode = goodsInfo.GoodsCode;
                    info.IsStatisticalPerformance    = goodsInfo.IsStatisticalPerformance;
                    info.IsStatisticalPerformanceStr = goodsInfo.IsStatisticalPerformance ? "√" : string.Empty;
                    info.IsScarcity    = goodsInfo.IsScarcity;
                    info.IsScarcityStr = goodsInfo.IsScarcity ? "√" : string.Empty;
                    info.State         = goodsInfo.PurchaseState == 0;
                    info.IsStateStr    = goodsInfo.PurchaseState == 0 ? "√" : string.Empty;
                    //库存信息获取
                    var stockQuantity = goodsStockList.ContainsKey(goodsInfo.GoodsId)
                        ? goodsStockList[goodsInfo.GoodsId]
                        : 0;
                    if (stockQuantity <= 0)
                    {
                        continue;
                    }
                    info.StockNums = stockQuantity;
                    if (!_salesDic.ContainsKey(goodsInfo.GoodsId))
                    {
                        info.StockNumSort = stockQuantity;
                    }

                    //责任人供应商信息获取
                    var tempGoodsPurchaseSetInfo = allGoodsPurchaseSet.FirstOrDefault(ent => ent.GoodsId == goodsInfo.GoodsId);

                    //根据商品id获取供应商
                    var companyId = tempGoodsPurchaseSetInfo != null ? tempGoodsPurchaseSetInfo.CompanyId : Guid.Empty;

                    decimal unitPrice = 0;
                    GoodsPurchaseLastPriceInfo goodsPurchaseLastPriceInfo = null;
                    if (goodsPurchaseLastPriceInfoList.Count > 0)
                    {
                        goodsPurchaseLastPriceInfo = goodsPurchaseLastPriceInfoList.FirstOrDefault(p => p.GoodsId.Equals(goodsInfo.GoodsId) && p.ThirdCompanyId.Equals(companyId));
                        unitPrice = goodsPurchaseLastPriceInfo != null ? goodsPurchaseLastPriceInfo.UnitPrice : 0;
                    }
                    info.RecentInPrice = unitPrice;
                    info.RecentCDate   = (goodsPurchaseLastPriceInfo != null ? goodsPurchaseLastPriceInfo.LastPriceDate : DateTime.MinValue).ToString("yyyy-MM-dd");

                    if (tempGoodsPurchaseSetInfo != null)
                    {
                        info.PersonResponsible = tempGoodsPurchaseSetInfo.PersonResponsible;
                        var firstOrDefault = PersonnelList.FirstOrDefault(ent => ent.PersonnelId == tempGoodsPurchaseSetInfo.PersonResponsible);
                        if (firstOrDefault != null)
                        {
                            info.PersonResponsibleName = firstOrDefault.RealName;
                        }
                        info.CompanyId   = tempGoodsPurchaseSetInfo.CompanyId;
                        info.CompanyName = tempGoodsPurchaseSetInfo.CompanyName;
                    }
                    //销售数量
                    if (_salesDic.ContainsKey(goodsInfo.GoodsId))
                    {
                        info.SaleNums     = _salesDic[goodsInfo.GoodsId];
                        info.SaleNumSort  = 1;
                        info.StockNumSort = 0;
                    }

                    #region [计算商品库存周转情况(天数)]
                    if (info.State && info.StockNums == 0)
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "下架";
                    }
                    else if (info.State && info.StockNums != 0)
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "下架有库存";
                    }
                    else if (info.IsScarcity && info.StockNums == 0)
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "缺货";
                    }
                    else if (info.IsScarcity && info.StockNums != 0)
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "缺货有库存";
                    }
                    else if (info.StockNums == 0)
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "0天";
                    }
                    else if (info.SaleNums > 0)
                    {
                        var tempTurnOverDays = info.StockNums * 30 / info.SaleNums;
                        info.TurnOverDays = tempTurnOverDays;
                        info.TurnOverStr  = tempTurnOverDays + "天";
                    }
                    else
                    {
                        info.TurnOverDays = 0;
                        info.TurnOverStr  = "无销售";
                    }
                    #endregion

                    #region [计算商品报备周转天数]

                    if (_weightedAverageSaleDic.ContainsKey(info.GoodsID))
                    {
                        var weightedAverageSale = _weightedAverageSaleDic[info.GoodsID];
                        if (_weightedAverageSaleDic[info.GoodsID] != 0)
                        {
                            info.TurnOverByFiling = info.StockNums * 30 / weightedAverageSale + "天";
                        }
                    }

                    #endregion

                    stockTurnOverList.Add(info);
                }
            }
            var pageIndex = GridGoodsStock.CurrentPageIndex;
            var pageSize  = GridGoodsStock.PageSize;
            stockTurnOverList               = stockTurnOverList.OrderBy(ent => ent.SaleNumSort).ThenByDescending(ent => ent.StockNumSort).ThenByDescending(ent => ent.TurnOverDays).ToList();
            GridGoodsStock.DataSource       = stockTurnOverList.Skip(pageIndex * pageSize).Take(pageSize).ToList();
            GridGoodsStock.VirtualItemCount = stockTurnOverList.Count;
        }