Exemple #1
0
        public static void RunTask()
        {
            var now  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            int week = GetWeek(now);

            if (week == 1 || week == 3)
            {
                #region --> 今天周一或周三
                var pg                      = DataAccessor.GetPurchasingGoodsList(TaskType.All);
                var goodsIdList             = pg.Select(w => w.GoodsId).Distinct().ToList();
                IList <GoodsInfo> goodsList = _goodsCenterSao.GetGoodsListByGoodsIds(goodsIdList).ToList();
                if (goodsList.Count == 0)
                {
                    return;
                }

                var dictPurchase   = new Dictionary <PurchasingInfo, List <PurchasingDetailInfo> >();
                var purchasingSets = new List <PurchasingGoods>();
                pg = pg.Where(w => w.StockUpDay == week && w.HostingFilialeId != Guid.Empty).ToList();
                var goodsDics     = goodsList.ToDictionary(k => k.GoodsId, v => v);
                var realGoodsDics = new Dictionary <Guid, GoodsInfo>();

                Dictionary <Guid, List <Guid> > goodsWithRealGoods = new Dictionary <Guid, List <Guid> >();
                foreach (var warehouseIdGroup in pg.GroupBy(act => new { act.WarehouseId, act.HostingFilialeId }))
                {
                    var warehouseId          = warehouseIdGroup.Key.WarehouseId;
                    var hostingFilialeId     = warehouseIdGroup.Key.HostingFilialeId;
                    var stockStatistics      = WMSSao.GetStockStatisticsDtosForAuto(warehouseId, hostingFilialeId);
                    var planPurchasingGoods1 = DataAccessor.GetAllSumPurchasingQuantity(warehouseId, hostingFilialeId).ToDictionary(k => k.GoodsID, v => v.PurchasingQuantity);
                    foreach (var companyIdGroup in warehouseIdGroup.GroupBy(act => act.CompanyId))
                    {
                        var purchaseGroupIds = companyIdGroup.Select(w => w.PurchaseGroupId).Distinct().ToList();
                        foreach (var purchaseGroupId in purchaseGroupIds)
                        {
                            //采购分组
                            var pgList             = companyIdGroup.Where(w => w.PurchaseGroupId == purchaseGroupId).ToList();
                            var personResponsibles = pgList.Select(w => w.PersonResponsible).Distinct().ToList();
                            foreach (var personResponsible in personResponsibles)
                            {
                                var prList = pgList.Where(w => w.PersonResponsible == personResponsible).ToList();
                                if (prList.Count > 0)
                                {
                                    foreach (var prInfo in prList.Where(ent => goodsDics.ContainsKey(ent.GoodsId)))
                                    {
                                        GoodsInfo goodsInfo = goodsDics[prInfo.GoodsId];
                                        if (goodsInfo == null)
                                        {
                                            continue;
                                        }
                                        prInfo.GoodsName = goodsInfo.GoodsName;
                                        prInfo.GoodsCode = goodsInfo.GoodsCode;
                                        prInfo.Units     = goodsInfo.Units;
                                        if (goodsInfo.ExpandInfo != null)
                                        {
                                            prInfo.PackQuantity = goodsInfo.ExpandInfo.PackCount;
                                        }
                                        List <Guid> realGoodsIdList;
                                        if (goodsWithRealGoods.ContainsKey(prInfo.GoodsId))
                                        {
                                            realGoodsIdList = goodsWithRealGoods[prInfo.GoodsId];
                                        }
                                        else
                                        {
                                            realGoodsIdList = _goodsCenterSao.GetRealGoodsIdsByGoodsId(prInfo.GoodsId).ToList();
                                            if (realGoodsIdList.Count == 0)
                                            {
                                                realGoodsIdList.Add(prInfo.GoodsId);
                                            }
                                            goodsWithRealGoods.Add(prInfo.GoodsId, realGoodsIdList);
                                        }

                                        int stockUpDays = prInfo.FilingForm == 1 ? GetStockUpDays(prInfo) : prInfo.FilingTrigger;
                                        if (stockUpDays == 0)
                                        {
                                            continue;
                                        }
                                        var childGoodsSaleAll = GetChildGoodsSaleTotalByDays(realGoodsIdList, new List <Guid> {
                                            prInfo.WarehouseId
                                        }, stockUpDays, now);

                                        if (childGoodsSaleAll.Count > 0)
                                        {
                                            foreach (var sale in childGoodsSaleAll)
                                            {
                                                sale.PlanPurchasingquantity = sale.WeightedAverageSaleQuantity * stockUpDays;
                                                if (sale.PlanPurchasingquantity > 0)
                                                {
                                                    //个位数0<x<=5向上取为5,个位数为6<x<=9向上取整为10
                                                    char[] temp       = sale.PlanPurchasingquantity.ToString(CultureInfo.InvariantCulture).ToArray();
                                                    int    unitsDigit = Convert.ToInt32(temp[temp.Length - 1]);
                                                    if (unitsDigit > 0 && unitsDigit < 5)
                                                    {
                                                        sale.PlanPurchasingquantity += 5 - unitsDigit;
                                                    }
                                                    else if (unitsDigit > 5)
                                                    {
                                                        sale.PlanPurchasingquantity += 10 - unitsDigit;
                                                    }
                                                }

                                                //计算当前的采购商品数量,包含扣除的已经采购完成和部分采购完成和赠品类型
                                                sale.SubtractPurchasingQuantity = planPurchasingGoods1.ContainsKey(sale.GoodsId)? planPurchasingGoods1[sale.GoodsId] : 0;

                                                //计算当前仓库存货数量
                                                sale.NonceWarehouseStockQuantity = stockStatistics.Where(ent => ent.RealGoodsId == sale.GoodsId).Sum(info => info.CurrentStock + info.UppingQuantity - info.RequireQuantity - info.SubtotalQuantity);
                                                if (prInfo.FilingForm == 2)
                                                {
                                                    //2触发报备
                                                    //计算是否满足不足数量
                                                    double planQuantity    = sale.WeightedAverageSaleQuantity * prInfo.Insufficient;
                                                    double realityQuantity = Math.Ceiling(planQuantity - sale.SubtractPurchasingQuantity - sale.NonceWarehouseStockQuantity);
                                                    if (realityQuantity <= 0)
                                                    {
                                                        //无需报备
                                                        continue;
                                                    }
                                                }
                                                if (sale.RealityNeedPurchasingQuantity > 0)
                                                {
                                                    var            salesinfo = DataAccessor.GetChildGoodsSale(sale.GoodsId, warehouseId, sale.HostingFilialeId, DateTime.Now);
                                                    PurchasingInfo existingPurchasingInfo = null;
                                                    bool           isExist = false;
                                                    if (dictPurchase.Keys.Count > 0)
                                                    {
                                                        existingPurchasingInfo = dictPurchase.Keys.FirstOrDefault(act => act.PersonResponsible == personResponsible && act.PurchasingFilialeId == hostingFilialeId && act.WarehouseID == warehouseId && companyIdGroup.Key == act.CompanyID);
                                                    }
                                                    var detailList = new List <PurchasingDetailInfo>();
                                                    if (existingPurchasingInfo == null)
                                                    {
                                                        existingPurchasingInfo = new PurchasingInfo
                                                        {
                                                            PurchasingID        = Guid.NewGuid(),
                                                            CompanyID           = companyIdGroup.Key,
                                                            CompanyName         = prList[0].CompanyName,
                                                            WarehouseID         = warehouseId,
                                                            PurchasingState     = (int)PurchasingState.NoSubmit,
                                                            PurchasingType      = (int)PurchasingType.AutoStock,
                                                            PersonResponsible   = personResponsible,
                                                            PurchaseGroupId     = purchaseGroupId,
                                                            StartTime           = DateTime.Now,
                                                            EndTime             = DateTime.MaxValue,
                                                            Description         = string.Format("[采购类别:{0};系统自动报备]", EnumAttribute.GetKeyName(PurchasingType.Custom)),
                                                            FilialeID           = hostingFilialeId,
                                                            PurchasingFilialeId = hostingFilialeId
                                                        };
                                                    }
                                                    else
                                                    {
                                                        isExist    = true;
                                                        detailList = dictPurchase[existingPurchasingInfo];
                                                    }
                                                    var purchasingDetailInfo = new PurchasingDetailInfo
                                                    {
                                                        PurchasingID      = existingPurchasingInfo.PurchasingID,
                                                        GoodsID           = sale.GoodsId,
                                                        GoodsName         = prInfo.GoodsName,
                                                        GoodsCode         = prInfo.GoodsCode,
                                                        Specification     = sale.Specification,
                                                        CompanyID         = companyIdGroup.Key,
                                                        Price             = prInfo.Price,
                                                        PlanQuantity      = sale.RealityNeedPurchasingQuantity,
                                                        RealityQuantity   = 0,
                                                        State             = 0,
                                                        Units             = prInfo.Units,
                                                        PurchasingGoodsID = Guid.NewGuid(),
                                                        SixtyDaySales     = salesinfo.SixtyDaySales,
                                                        ThirtyDaySales    = salesinfo.ThirtyDaySales,
                                                        ElevenDaySales    = salesinfo.ElevenDaySales,
                                                        CPrice            = prInfo.Price
                                                    };
                                                    detailList.Add(purchasingDetailInfo);

                                                    if (!realGoodsDics.ContainsKey(sale.GoodsId))
                                                    {
                                                        realGoodsDics.Add(sale.GoodsId, goodsInfo);
                                                    }
                                                    if (detailList.Count > 0 && !isExist)
                                                    {
                                                        dictPurchase.Add(existingPurchasingInfo, detailList);
                                                    }
                                                }
                                            }
                                            if (!purchasingSets.Any(act => act.WarehouseId == warehouseId && act.HostingFilialeId == hostingFilialeId && act.GoodsId == prInfo.GoodsId))
                                            {
                                                purchasingSets.Add(prInfo);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                PurchasingPromotion(dictPurchase, realGoodsDics, purchasingSets);
                #endregion
            }
        }