Beispiel #1
0
 public void TestSave()
 {
     try
     {
         _purchasingDetailManager.Save(null);
     }
     catch (Exception ex)
     {
         Assert.IsTrue(!string.IsNullOrEmpty(ex.Message));
     }
 }
        // 采购中
        protected void IbnPurchasing_Click(object sender, EventArgs eventArgs)
        {
            bool isHave      = false;
            var  codeManager = new CodeManager();

            foreach (GridDataItem dataItem in RG_DebitNote.Items)
            {
                var purchasingId      = new Guid(dataItem.GetDataKeyValue("PurchasingId").ToString());
                var personResponsible = new Guid(dataItem.GetDataKeyValue("PersonResponsible").ToString());
                var cbCheck           = (CheckBox)dataItem.FindControl("CB_Check");
                if (cbCheck.Checked)
                {
                    using (var ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        isHave = true;
                        _debitNoteDao.UpdateDebitNoteStateByPurchasingId(purchasingId, (int)DebitNoteState.Purchasing);
                        //生成采购单
                        DebitNoteInfo debitNoteInfo = _debitNoteDao.GetDebitNoteInfo(purchasingId) ?? new DebitNoteInfo();
                        IList <DebitNoteDetailInfo> debitNoteDetailList = _debitNoteDao.GetDebitNoteDetailList(purchasingId);
                        CompanyCussentInfo          companyCussentInfo  = CompanyCussentList.FirstOrDefault(w => w.CompanyId == debitNoteInfo.CompanyId);
                        //var warehouseInfo = WarehouseManager.Get(debitNoteInfo.WarehouseId);
                        PersonnelInfo  personnelInfo     = PersonnelList.FirstOrDefault(w => w.PersonnelId == debitNoteInfo.PersonResponsible) ?? new PersonnelInfo(null);
                        PurchasingInfo oldPurchasingInfo = _purchasing.GetPurchasingById(purchasingId);
                        var            realName          = CurrentSession.Personnel.Get().RealName;
                        var            filialeId         = string.IsNullOrWhiteSpace(debitNoteInfo.PurchasingNo) || debitNoteInfo.PurchasingNo == "-"?FilialeManager.GetHeadList().First(ent => ent.Name.Contains("可得")).ID
                            : _purchasing.GetPurchasingList(debitNoteInfo.PurchasingNo).FilialeID;
                        var pInfo = new PurchasingInfo
                        {
                            PurchasingID    = Guid.NewGuid(),
                            PurchasingNo    = codeManager.GetCode(CodeType.PH),
                            CompanyID       = debitNoteInfo.CompanyId,
                            CompanyName     = companyCussentInfo == null ? "" : companyCussentInfo.CompanyName,
                            FilialeID       = filialeId,
                            WarehouseID     = debitNoteInfo.WarehouseId,
                            PurchasingState = (int)PurchasingState.Purchasing,
                            PurchasingType  = (int)PurchasingType.Custom,
                            StartTime       = DateTime.Now,
                            EndTime         = DateTime.MaxValue,
                            //Description = "[采购类别:{0}赠品借记单][对应采购单号" + debitNoteInfo.PurchasingNo + "]" + CurrentSession.Personnel.Get().RealName,
                            Description         = string.Format("[采购类别:{0},赠品借记单对应采购单号{1};采购人:{2}]", EnumAttribute.GetKeyName(PurchasingType.Custom), debitNoteInfo.PurchasingNo, realName),
                            PmId                = personnelInfo.PersonnelId,
                            PmName              = personnelInfo.RealName,
                            ArrivalTime         = oldPurchasingInfo.ArrivalTime,
                            PersonResponsible   = personResponsible,
                            PurchasingFilialeId = filialeId
                        };
                        IList <PurchasingDetailInfo> purchasingDetailList = new List <PurchasingDetailInfo>();
                        if (debitNoteDetailList.Count > 0)
                        {
                            List <Guid> goodsIdOrRealGoodsIdList  = debitNoteDetailList.Select(w => w.GoodsId).Distinct().ToList();
                            Dictionary <Guid, GoodsInfo> dicGoods = _goodsCenterSao.GetGoodsBaseListByGoodsIdOrRealGoodsIdList(goodsIdOrRealGoodsIdList);
                            if (dicGoods != null && dicGoods.Count > 0)
                            {
                                foreach (var debitNoteDetailInfo in debitNoteDetailList)
                                {
                                    bool hasKey = dicGoods.ContainsKey(debitNoteDetailInfo.GoodsId);
                                    if (hasKey)
                                    {
                                        GoodsInfo goodsBaseInfo = dicGoods.FirstOrDefault(w => w.Key == debitNoteDetailInfo.GoodsId).Value;
                                        // 获取商品的60、30、11天销量
                                        var purchasingDetailInfo = _purchasingDetail.GetChildGoodsSale(debitNoteDetailInfo.GoodsId, debitNoteInfo.WarehouseId, DateTime.Now, pInfo.PurchasingFilialeId) ?? new PurchasingDetailInfo();
                                        var durchasingDetailInfo = new PurchasingDetailInfo
                                        {
                                            PurchasingID        = pInfo.PurchasingID,
                                            PurchasingGoodsID   = Guid.NewGuid(),
                                            GoodsID             = debitNoteDetailInfo.GoodsId,
                                            GoodsName           = debitNoteDetailInfo.GoodsName,
                                            GoodsCode           = goodsBaseInfo.GoodsCode,
                                            Specification       = debitNoteDetailInfo.Specification,
                                            CompanyID           = pInfo.CompanyID,
                                            Price               = debitNoteDetailInfo.Price,
                                            PlanQuantity        = debitNoteDetailInfo.GivingCount,
                                            RealityQuantity     = 0,
                                            State               = 0,
                                            Description         = "",
                                            Units               = goodsBaseInfo.Units,
                                            PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                            SixtyDaySales       = purchasingDetailInfo.SixtyDaySales,
                                            ThirtyDaySales      = purchasingDetailInfo.ThirtyDaySales,
                                            ElevenDaySales      = purchasingDetailInfo.ElevenDaySales == 0 ? 0 : purchasingDetailInfo.ElevenDaySales, // 11 //日均销量(11天)
                                            CPrice              = debitNoteDetailInfo.Price
                                        };
                                        purchasingDetailList.Add(durchasingDetailInfo);
                                    }
                                }
                            }
                        }
                        if (purchasingDetailList.Count > 0)
                        {
                            _debitNoteDao.UpdateDebitNoteNewPurchasingIdByPurchasingId(purchasingId, pInfo.PurchasingID);
                            _purchasing.PurchasingInsert(pInfo);
                            _purchasing.PurchasingUpdateIsOut(pInfo.PurchasingID);
                            //报备管理生成采购单操作记录添加
                            WebControl.AddOperationLog(personnelInfo.PersonnelId, personnelInfo.RealName, pInfo.PurchasingID, pInfo.PurchasingNo,
                                                       OperationPoint.ReportManage.DebitToAddPurchasingList.GetBusinessInfo(), string.Empty);
                            var purchasingDetailManager = new PurchasingDetailManager(_purchasingDetail, _purchasing);
                            purchasingDetailManager.Save(purchasingDetailList);
                        }
                        ts.Complete();
                    }
                }
            }
            if (isHave == false)
            {
                RAM.Alert("未勾选借记单!");
            }
            else
            {
                RAM.ResponseScripts.Add("setTimeout(function(){ refreshGrid(); }, " + GlobalConfig.PageAutoRefreshDelayTime + ");");
            }
        }
Beispiel #3
0
        private static void PurchasingPromotion(Dictionary <PurchasingInfo, List <PurchasingDetailInfo> > dictPurchase, Dictionary <Guid, GoodsInfo> dicGoods, IList <PurchasingGoods> pg)
        {
            if (dictPurchase.Count > 0)
            {
                //IList<PurchaseSetInfo> purchaseSetList = _purchaseSetBll.GetPurchaseSetList();
                foreach (KeyValuePair <PurchasingInfo, List <PurchasingDetailInfo> > keyValuePair in dictPurchase)
                {
                    keyValuePair.Key.PurchasingNo = DataAccessor.GetCode(BaseInfo.CodeType.PH);
                    var pInfo = keyValuePair.Key;
                    IList <PurchasingDetailInfo> plist = keyValuePair.Value;
                    //pInfo.IsOut = true;
                    _purchasing.PurchasingInsert(pInfo);

                    //非赠品采购商品
                    IList <PurchasingDetailInfo> plist2 = plist.Where(w => w.PurchasingGoodsType != (int)PurchasingGoodsType.Gift).ToList();

                    #region [现返]
                    //如果赠送方式为总数量赠送时使用  key 主商品ID  value 额外赠送
                    var dics           = new Dictionary <string, int>();    //需赠送
                    var debitExtraDics = new Dictionary <string, int>();    //借计单已送
                    var addDetailsList = new List <PurchasingDetailInfo>(); //添加赠品
                                                                            //处理原理:
                                                                            //赠品数量=原采购数量/(买几个+送几个)*送几个
                                                                            //购买数量=原采购数量-赠品数量
                    foreach (var pdInfo in plist2)
                    {
                        var goodsBaseInfo = new GoodsInfo();
                        if (dicGoods != null)
                        {
                            bool hasKey = dicGoods.ContainsKey(pdInfo.GoodsID);
                            if (hasKey)
                            {
                                goodsBaseInfo = dicGoods.FirstOrDefault(w => w.Key == pdInfo.GoodsID).Value;
                            }
                        }
                        PurchasingGoods purchaseSetInfo = pg.FirstOrDefault(w => w.GoodsId == goodsBaseInfo.GoodsId && w.WarehouseId == pInfo.WarehouseID);
                        if (purchaseSetInfo == null)
                        {
                            continue;
                        }
                        IList <PurchasePromotionInfo> ppList = _purchasePromotionBll.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, pInfo.PurchasingFilialeId, (int)PurchasePromotionType.Back);
                        PurchasePromotionInfo         ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                        if (ppInfo != null)
                        {
                            #region  新增
                            if (!ppInfo.IsSingle)  //按商品总数量进行赠送
                            {
                                if (!dics.ContainsKey(goodsBaseInfo.GoodsName))
                                {
                                    var dataList = plist2.Where(act => act.GoodsName == goodsBaseInfo.GoodsName).OrderByDescending(act => act.PlanQuantity).ToList();
                                    //余数
                                    var extra = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) % (ppInfo.BuyCount + ppInfo.GivingCount);
                                    if (extra == ppInfo.BuyCount)
                                    {
                                        dataList[0].PlanQuantity += ppInfo.GivingCount;
                                    }
                                    else
                                    {
                                        //应赠送次数
                                        int actquantity = extra % (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (actquantity >= (ppInfo.BuyCount / float.Parse("2.0")))
                                        {
                                            dataList[0].PlanQuantity = dataList[0].PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                        }
                                    }
                                    //总商品赠送商品总数
                                    var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) / (ppInfo.BuyCount + ppInfo.GivingCount);
                                    dics.Add(goodsBaseInfo.GoodsName, sumTotal);
                                    if (dics[goodsBaseInfo.GoodsName] > 0)
                                    {
                                        foreach (var item in dataList.OrderByDescending(act => act.PlanQuantity))
                                        {
                                            var count = dics[item.GoodsName] - Convert.ToInt32(item.PlanQuantity);
                                            if (count > 0)  //赠品数>购买数
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                dics[item.GoodsName]     = count;
                                            }
                                            else if (count < 0)
                                            {
                                                addDetailsList.Add(new PurchasingDetailInfo
                                                {
                                                    PurchasingGoodsID   = Guid.NewGuid(),
                                                    PurchasingID        = item.PurchasingID,
                                                    GoodsID             = item.GoodsID,
                                                    GoodsName           = item.GoodsName,
                                                    GoodsCode           = item.GoodsCode,
                                                    Specification       = item.Specification,
                                                    CompanyID           = item.CompanyID,
                                                    Price               = 0,
                                                    PlanQuantity        = dics[item.GoodsName],
                                                    PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                                    RealityQuantity     = 0,
                                                    State               = item.State,
                                                    Description         = "",
                                                    Units               = item.Units,
                                                    SixtyDaySales       = item.SixtyDaySales,
                                                    ThirtyDaySales      = item.ThirtyDaySales,
                                                    ElevenDaySales      = item.ElevenDaySales,
                                                    CPrice              = 0
                                                });
                                                item.RealityQuantity = 0;
                                                item.PlanQuantity    = Math.Abs(count);
                                                dics[item.GoodsName] = 0;
                                            }
                                            else
                                            {
                                                item.Price = 0;
                                                item.PurchasingGoodsType = (int)PurchasingGoodsType.Gift;
                                                item.RealityQuantity     = 0;
                                                item.CPrice          = 0;
                                                dics[item.GoodsName] = 0;
                                            }
                                            if (dics[item.GoodsName] == 0)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else  //按单光度
                            {
                                //应赠余数
                                var actquantity = pdInfo.PlanQuantity % (ppInfo.BuyCount + ppInfo.GivingCount);
                                if (actquantity > 0)
                                {
                                    if (actquantity >= ((ppInfo.BuyCount + ppInfo.GivingCount) / float.Parse("2.0")))
                                    {
                                        pdInfo.PlanQuantity = pdInfo.PlanQuantity + (ppInfo.BuyCount + ppInfo.GivingCount) - actquantity;
                                    }
                                }
                                int pQuantity = int.Parse(pdInfo.PlanQuantity.ToString(CultureInfo.InvariantCulture));
                                //赠品数量=原采购数量/(买几个+送几个)*送几个
                                int quantity = pQuantity / (ppInfo.BuyCount + ppInfo.GivingCount) * ppInfo.GivingCount;
                                if (quantity > 0)
                                {
                                    var oldPurchasingDetailInfo = plist.FirstOrDefault(w => w.PurchasingGoodsID == pdInfo.PurchasingGoodsID);
                                    if (oldPurchasingDetailInfo != null)
                                    {
                                        //购买数量=原采购数量-赠品数量
                                        oldPurchasingDetailInfo.PlanQuantity -= (quantity);
                                    }

                                    var purchasingDetailInfo = plist.FirstOrDefault(w => w.GoodsID == pdInfo.GoodsID && w.PurchasingGoodsType == (int)PurchasingGoodsType.Gift);
                                    if (purchasingDetailInfo != null)
                                    {
                                        //在原赠品数量累加
                                        purchasingDetailInfo.PlanQuantity += (quantity);
                                    }
                                    else
                                    {
                                        purchasingDetailInfo = new PurchasingDetailInfo
                                        {
                                            PurchasingGoodsID   = Guid.NewGuid(),
                                            PurchasingID        = pInfo.PurchasingID,
                                            GoodsID             = pdInfo.GoodsID,
                                            GoodsName           = pdInfo.GoodsName,
                                            GoodsCode           = pdInfo.GoodsCode,
                                            Specification       = pdInfo.Specification,
                                            CompanyID           = pdInfo.CompanyID,
                                            Price               = 0,
                                            PlanQuantity        = (quantity),
                                            PurchasingGoodsType = (int)PurchasingGoodsType.Gift,
                                            RealityQuantity     = 0,
                                            State               = 0,
                                            Description         = "",
                                            Units               = pdInfo.Units,
                                            SixtyDaySales       = pdInfo.SixtyDaySales,
                                            ThirtyDaySales      = pdInfo.ThirtyDaySales,
                                            ElevenDaySales      = pdInfo.ElevenDaySales
                                        };
                                        addDetailsList.Add(purchasingDetailInfo);
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion

                    #region [非现返生成借记单]

                    var debitNoteDetailList = new List <DebitNoteDetailInfo>();
                    foreach (var pdInfo in plist2)
                    {
                        var goodsBaseInfo = new GoodsInfo();
                        if (dicGoods != null)
                        {
                            bool hasKey = dicGoods.ContainsKey(pdInfo.GoodsID);
                            if (hasKey)
                            {
                                goodsBaseInfo = dicGoods.FirstOrDefault(w => w.Key == pdInfo.GoodsID).Value;
                            }
                        }
                        PurchasingGoods purchaseSetInfo = pg.FirstOrDefault(w => w.GoodsId == goodsBaseInfo.GoodsId && w.WarehouseId == pInfo.WarehouseID);
                        if (purchaseSetInfo != null)
                        {
                            IList <PurchasePromotionInfo> ppList = _purchasePromotionBll.GetPurchasePromotionList(purchaseSetInfo.PromotionId, purchaseSetInfo.GoodsId, purchaseSetInfo.WarehouseId, pInfo.PurchasingFilialeId, (int)PurchasePromotionType.NoBack);
                            PurchasePromotionInfo         ppInfo = ppList.FirstOrDefault(w => w.GivingCount > 0 && w.StartDate <= DateTime.Now && w.EndDate >= DateTime.Now);
                            if (ppInfo != null)
                            {
                                int pQuantity = Convert.ToInt32(pdInfo.PlanQuantity);
                                //赠品数量=原采购数量/买几个*送几个
                                #region  新增
                                //按商品总数量进行赠送
                                if (!ppInfo.IsSingle && dicGoods != null)
                                {
                                    if (!dics.ContainsKey(goodsBaseInfo.GoodsName))
                                    {
                                        var dataList = plist2.Where(act => act.GoodsName == goodsBaseInfo.GoodsName).ToList();
                                        //单光度赠送商品总数
                                        var total = dataList.Sum(act => (Convert.ToInt32(act.PlanQuantity) / (ppInfo.BuyCount + ppInfo.GivingCount)));
                                        //总商品赠送商品总数
                                        var sumTotal = dataList.Sum(act => Convert.ToInt32(act.PlanQuantity)) / (ppInfo.BuyCount + ppInfo.GivingCount);
                                        if (sumTotal > total)
                                        {
                                            dics.Add(goodsBaseInfo.GoodsName, sumTotal);
                                        }
                                    }
                                }
                                #endregion
                                int quantity = pQuantity / ppInfo.BuyCount * ppInfo.GivingCount;
                                if (quantity > 0)
                                {
                                    var debitNoteDetailInfo = new DebitNoteDetailInfo
                                    {
                                        PurchasingId  = pInfo.PurchasingID,
                                        GoodsId       = pdInfo.GoodsID,
                                        GoodsName     = pdInfo.GoodsName,
                                        Specification = pdInfo.Specification,
                                        GivingCount   = quantity,
                                        ArrivalCount  = 0,
                                        Price         = pdInfo.Price,
                                        State         = 0,
                                        Amount        = quantity * pdInfo.Price,
                                        Memo          = "",
                                        Id            = Guid.NewGuid()
                                    };
                                    debitNoteDetailList.Add(debitNoteDetailInfo);
                                }
                            }
                        }
                    }

                    if (addDetailsList.Count > 0)
                    {
                        foreach (var purchasingDetailInfo in addDetailsList)
                        {
                            purchasingDetailInfo.Price = 0;
                            plist.Add(purchasingDetailInfo);
                        }
                    }
                    #endregion

                    #region 处理额外赠送商品
                    foreach (var dic in dics)
                    {
                        KeyValuePair <string, int> dic1 = dic;
                        if (debitExtraDics.ContainsKey(dic.Key))
                        {
                            var total = dic1.Value - debitExtraDics[dic.Key];
                            if (total > 0)
                            {
                                var data = debitNoteDetailList.Where(act => act.GoodsName == dic1.Key).OrderByDescending(act => act.GivingCount).ToList();
                                for (int i = 0; i < total; i++)
                                {
                                    data[i].GivingCount += 1;
                                    data[i].Amount       = data[i].Price * data[i].GivingCount;
                                }
                            }
                        }
                    }
                    #endregion

                    foreach (var item in plist.Where(act => act.Price == 0 && act.PurchasingGoodsType != (int)PurchasingGoodsType.Gift))
                    {
                        var info = plist.FirstOrDefault(act => act.GoodsName == item.GoodsName && act.Price > 0);
                        item.Price = info != null ? info.Price : 0;
                    }
                    //保存采购单明细
                    _purchasingDetailBll.Save(plist);

                    //添加借记单
                    if (debitNoteDetailList.Count > 0)
                    {
                        var debitNoteInfo = new DebitNoteInfo
                        {
                            PurchasingId      = pInfo.PurchasingID,
                            PurchaseGroupId   = pInfo.PurchaseGroupId,
                            PurchasingNo      = pInfo.PurchasingNo,
                            CompanyId         = pInfo.CompanyID,
                            PresentAmount     = debitNoteDetailList.Sum(w => w.Amount),
                            CreateDate        = DateTime.Now,
                            FinishDate        = DateTime.MinValue,
                            State             = (int)DebitNoteState.ToPurchase,
                            WarehouseId       = pInfo.WarehouseID,
                            Memo              = "",
                            PersonResponsible = pInfo.PersonResponsible,
                            NewPurchasingId   = Guid.Empty
                        };
                        _debitNoteBll.AddPurchaseSetAndDetail(debitNoteInfo, debitNoteDetailList);
                    }
                }

                if (dicGoods != null && dicGoods.Count > 0)
                {
                    var completeGoodsIds = dicGoods.Select(keyValuePair => keyValuePair.Value.GoodsId).ToList();
                    foreach (var item in pg.Where(ent => completeGoodsIds.Contains(ent.GoodsId)))
                    {
                        DataAccessor.UpdateLastPurchasingDate(item.WarehouseId, item.HostingFilialeId, item.GoodsId, DateTime.Now);
                    }
                }
            }
        }