using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMIS.DataLogic.Common.TeachingMaterial; using EMIS.ViewModel.TeachingMaterial; using Bowin.Common.Linq.Entity; using EMIS.ViewModel; using EMIS.Entities; using EMIS.DataLogic.Repositories; using EMIS.DataLogic.UniversityManage.TeacherManage; using System.Linq.Expressions; using System.Web; using Bowin.Common.Linq; using EMIS.CommonLogic.SystemServices; using System.Text.RegularExpressions; namespace EMIS.CommonLogic.TeachingMaterial { public class StockOutServices : BaseServices, IStockOutServices { public TeachersOrderRepository TeachersOrderRepository { get; set; } public StockOutRepository StockOutRepository { get; set; } public StockOutDetailRepository StockOutDetailRepository { get; set; } public SchoolyearRepository SchoolyearRepository { get; set; } public UserRepository UserRepository { get; set; } public Lazy staffDAL { get; set; } public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; } public CoursematerialRepository CoursematerialRepository { get; set; } public PublishRepository PublishRepository { get; set; } public CollegeRepository CollegeRepository { get; set; } public DictionaryItemRepository DictionaryItemRepository { get; set; } public TeachingMateriaInventoryRepository TeachingMateriaInventoryRepository { get; set; } public StockOutDAL StockOutDAL { get; set; } public StockOutDetailDAL StockOutDetailDAL { get; set; } public Lazy ParameterServices { get; set; } public Lazy SerialNumberServices { get; set; } #region 1.0 查询出库信息 public IGridResultSet GetStockOutViewGrid(ViewModel.ConfiguretView configuretView, Guid? schoolyearID, IList stockOutTypeList, Guid? supplierID, DateTime? stockOutTimeStart, DateTime? stockOutTimeEnd, int pageIndex, int pageSize) { if (stockOutTimeStart == null) { stockOutTimeStart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); } var query = StockOutDAL.GetStockOutGridView(x => stockOutTypeList.Contains(x.StockOutType)); if (schoolyearID.HasValue) query = query.Where(x => x.SchoolyearID == schoolyearID); if (supplierID.HasValue) query = query.Where(x => x.SupplierID == supplierID); if (stockOutTimeStart.HasValue) query = query.Where(x => x.StockOutTime >= stockOutTimeStart); if (stockOutTimeEnd.HasValue) { DateTime newStockOutTimeEnd = stockOutTimeEnd.Value.AddDays(1);//取起止日期一天、 query = query.Where(x => x.StockOutTime <= newStockOutTimeEnd); } if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.StockOutTime).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.SchoolyearName).OrderBy(x => x.StockOutNo).OrderByDescending(x => x.StockOutTime).ToGridResultSet(pageIndex, pageSize); } #endregion #region 2.0 查询出库Excel导出信息 //查询并导出 public IList GetStockOutViewList(ViewModel.ConfiguretView configuretView, Guid? schoolyearID, IList stockOutTypeList, Guid? supplierID, DateTime? stockOutTimeStart, DateTime? stockOutTimeEnd) { DateTime newStockOutTimeEnd = stockOutTimeEnd.Value.AddDays(1);//取起止日期一天、 var query = StockOutDAL.GetStockOutGridView(x => stockOutTypeList.Contains(x.StockOutType)); if (schoolyearID.HasValue) query = query.Where(x => x.SchoolyearID == schoolyearID); if (supplierID.HasValue) query = query.Where(x => x.SupplierID == supplierID); if (stockOutTimeStart.HasValue) query = query.Where(x => x.StockOutTime >= stockOutTimeStart); if (stockOutTimeEnd.HasValue) query = query.Where(x => x.StockOutTime <= newStockOutTimeEnd); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.StockOutTime).ToList(); return query.OrderBy(x => x.StockOutTime).ToList(); } #endregion #region 3.0 删除出库信息 //删除出库信息 public void Delete(List stockOutIDs) { try { bool? IsOrdered = false;//是否包含出库信息! var stockOutList = StockOutRepository.GetList(x => stockOutIDs.Contains(x.StockOutID)).ToList(); foreach (var stockOut in stockOutList) { if (stockOut.RecordStatus == (int)SYS_STATUS.USABLE) { throw new Exception("选择的信息已出库,无法删除。"); } } if (stockOutIDs.Count > 0) { //删除入库数据、及该单号下挂钩的明细数据 StockOutDetailRepository.UnitOfWork.Delete(x => stockOutIDs.Contains(x.StockOutID)); StockOutRepository.UnitOfWork.Delete(x => stockOutIDs.Contains(x.StockOutID)); } } catch (Exception ex) { throw ex; } } #endregion #region 4.0 根据ID、获取出库编辑信息 public StockOutView GetSingleStockOut(Guid stockOutID) { return StockOutDAL.GetSingleStockOut(stockOutID); } #endregion #region 5.0 检查编号唯一 //验证出库单据号是否存在 public bool IsExistStockOutNo(string stockOutNo) { return StockOutDAL.IsExistStockOutNo(stockOutNo); } #endregion #region 6.0 出库信息保存 //注:折合价=单价*折扣率、码洋=单价*数量、总价(实洋)=折合价*数量(码洋*折扣率、单价*折扣率*数量) public void Save(StockOutView stockOutView, IList tmList, Guid? userId, string type) { //var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;//提取当前用户信息 //Expression> exp = (x => true); //if (curUser.CollegeID.HasValue) //{ // exp = exp.And(x => x.CollegeID == curUser.CollegeID); //} //var staffInfo =staffDAL.Value.GetStaffViewQueryable(exp);//所选用户对应院系的所有老师、 if (this.StockOutRepository.GetList(x => x.StockOutNo == stockOutView.StockOutNo && x.StockOutID != stockOutView.StockOutID).Count() > 0) { throw new Exception("出库单号已经存在,请重新输入!"); } int RecordStatusType = (int)CF_GeneralPurpose.IsNo; Guid? GetBookUserId = stockOutView.RecipientUserID; if ((int)stockOutView.StockOutTypeID == (int)CF_StockOutType.TeachingMaterialRetail) { GetBookUserId = Guid.Empty;//如果是教材零售 } if (type == "Submit")//如果是Save 那么状态默认为0、 如果是Submit 那么状态默认1 { RecordStatusType = (int)CF_GeneralPurpose.IsYes; } ET_StockOut stockOutEntity = GetStockOut(stockOutView.StockOutID); if (stockOutEntity != null)//更新数据 { stockOutEntity.SchoolyearID = (Guid)stockOutView.SchoolyearID; stockOutEntity.StockOutNo = stockOutView.StockOutNo; stockOutEntity.StockOutType = (int)stockOutView.StockOutTypeID; stockOutEntity.Desc = stockOutView.Desc; stockOutEntity.RecipientUserID = GetBookUserId; stockOutEntity.StockOutTime = stockOutView.StockOutTime; stockOutView.RecordStatus = RecordStatusType; stockOutEntity.GetBookNo = stockOutView.GetBookNo; StockOutRepository.UnitOfWork.Update(stockOutEntity); //StockOutRepository.UnitOfWork.Commit();//更新数据 //删除出库明细数据、重新插入一次 StockOutDetailRepository.UnitOfWork.Delete(x => x.StockOutID == stockOutView.StockOutID); //对数据进行分组、单价、教材ID一致、数量累加 GetTMPList(tmList, userId, stockOutView.StockOutID); } else { ET_StockOut stockOut = new ET_StockOut() { StockOutID = Guid.NewGuid(), Desc = stockOutView.Desc, StockOutType = (int)stockOutView.StockOutTypeID, CreateTime = DateTime.Now, CreateUserID = userId, SchoolyearID = (Guid)stockOutView.SchoolyearID, ModifyTime = DateTime.Now, ModifyUserID = userId, RecordStatus = RecordStatusType, GetBookNo = stockOutView.GetBookNo, RecipientUserID = GetBookUserId == Guid.Empty ? userId : GetBookUserId, StockOutTime = stockOutView.StockOutTime, OutNumber = stockOutView.OutNumber, StockOutNo = stockOutView.StockOutNo }; StockOutRepository.UnitOfWork.Add(stockOut);//添加出库信息 UnitOfWork.Commit(); GetTMPList(tmList, userId, stockOut.StockOutID); // UnitOfWork.BulkInsert(tpList);//统一写入 } } #endregion #region 7.0 获取出库的教材信息 public IList GetStockOutByTMViewList(Guid stockOutID) { return StockOutDAL.GetTeachingMaterialView(stockOutID); } #endregion #region 8.0 提交出库单 //提交出库单 public void Submit(List stockOutIDs, Guid userId) { try { if (stockOutIDs.Count > 0) { IList stockOutList = StockOutRepository.GetList(x => stockOutIDs.Contains(x.StockOutID), (x => x.ET_StockOutDetail)).ToList(); foreach (var stockOutEntity in stockOutList) { if (stockOutEntity.RecordStatus == (int)CF_GeneralPurpose.IsYes) { throw new Exception("选择的信息已提交,无法重复提交。"); } } foreach (var stockOutEntity in stockOutList) { stockOutEntity.RecordStatus = (int)CF_GeneralPurpose.IsYes; stockOutEntity.ModifyUserID = userId; StockOutRepository.UnitOfWork.Update(stockOutEntity); // StockOutRepository.UnitOfWork.Commit(); IList stockOutDetailList = stockOutEntity.ET_StockOutDetail.ToList(); //1.0 提取当前库存量、 foreach (var stockOutDetailEntity in stockOutDetailList) { int? stockOutQuantity = stockOutDetailEntity.StockOutQuantity; List teachingMateriaInventorylist = GetTeachingMateriaInventory(stockOutDetailEntity.TeachingMaterialPoolID, stockOutDetailEntity.Discount).OrderBy(x => x.CreateTime).ToList(); int count = 0; int Inventorysum = 0; int needQuantity = (int)stockOutQuantity; int oldInventory = 0; if (teachingMateriaInventorylist.Count > 0) { foreach (ET_TeachingMateriaInventory teachingMateriaInventory in teachingMateriaInventorylist) { Inventorysum = Inventorysum + (teachingMateriaInventory.PresentInventory ?? 0); count++; if (Inventorysum >= stockOutQuantity) { if (count == 1) { teachingMateriaInventory.PresentInventory = teachingMateriaInventory.PresentInventory - stockOutQuantity; teachingMateriaInventory.ModifyTime = DateTime.Now; TeachingMateriaInventoryRepository.UnitOfWork.Update(teachingMateriaInventory);//更新库存数量 break; } else { for (int i = 0; i < count; i++) { var view = teachingMateriaInventorylist[i]; oldInventory = (int)view.PresentInventory; view.PresentInventory = (view.PresentInventory - needQuantity) <= 0 ? 0 : (view.PresentInventory - needQuantity); view.ModifyTime = DateTime.Now; TeachingMateriaInventoryRepository.UnitOfWork.Update(view);//更新库存数量 if (oldInventory - needQuantity <= 0) { needQuantity = needQuantity - oldInventory; } } break; } } } } if (Inventorysum < stockOutQuantity) { throw new Exception("出库数量大于当前库存量!"); } } //BalanceInventory(stockInDetailList, userId);//提交平衡库存 } } UnitOfWork.Commit(); } catch (Exception ex) { throw ex; } } #endregion #region 9.0 查询出库明细列表 //出库明细列表 public IGridResultSet GetStockOutDetailViewGrid(Guid? stockOutId, int pageIndex, int pageSize) { var query = StockOutDetailDAL.GetStockOutDetailGridView(x => x.StockOutID == stockOutId); return query.OrderBy(x => x.StockOutTime).ToGridResultSet(pageIndex, pageSize); } #endregion #region 10. 出库明细列表导出Excel //出库明细Excel导出 public IList GetStockOutDetailViewList(Guid? stockOutId) { var query = StockOutDetailDAL.GetStockOutDetailGridView(x => x.StockOutID == stockOutId); return query.OrderBy(x => x.StockOutTime).ToList(); } #endregion #region 11. 获取教材信息 public IGridResultSet GetStockInByTeachingMaterialPoolViewGrid(ViewModel.ConfiguretView configuretView, bool? isLate, Guid? teachingMaterialPoolID, Guid? coursematerialID, bool isSelectMax, int pageIndex, int pageSize) { Expression> exp = (x => true); if (teachingMaterialPoolID.HasValue) { exp = exp.And(x => x.TeachingMaterialPoolID == teachingMaterialPoolID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID)); } var query = StockOutDAL.GetTeachersOrderByTeachingMaterial(exp); //if (isLate.HasValue) //{ // query = query.Where(x => x.IsLate == isLate); //} if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); var result = query.OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); if (isSelectMax) { result.rows.ForEach(x => x.OrderQty = x.PresentInventory); } return result; } #endregion public IGridResultSet GetTeachingMaterialViewForRefundGrid(ViewModel.ConfiguretView configuretView, bool? isLate, Guid? teachingMaterialPoolID, Guid? coursematerialID, bool isSelectMax, int pageIndex, int pageSize) { Expression> exp = (x => true); if (teachingMaterialPoolID.HasValue) { exp = exp.And(x => x.TeachingMaterialPoolID == teachingMaterialPoolID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.EM_Coursematerial.Any(w => w.CoursematerialID == coursematerialID)); } //if (isLate.HasValue) //{ // exp = exp.And(x => x.IsLate == isLate); //} var query = StockOutDAL.GetTeachersOrderByTeachingMaterial(exp); //if (isLate.HasValue) //{ // query = query.Where(x => x.IsLate == isLate); //} query = query.Where(x => x.PresentInventory > 0); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); var result = query.OrderBy(x => x.TeachingMaterialCode.Length).ThenBy(x => x.TeachingMaterialCode).ToGridResultSet(pageIndex, pageSize); if (isSelectMax) { result.rows.ForEach(x => x.OrderQty = x.PresentInventory); } return result; } #region 13.0 平衡教材库存数量(适用与出库、入库) //public void BalanceInventory(IList stockOutDetailList, Guid userId) //{ // //1.0 提取当前库存量、 // foreach (var stockOutDetailEntity in stockOutDetailList) // { // int? stockOutQuantity = stockOutDetailEntity.StockOutQuantity; // ET_TeachingMateriaInventory teachingMateriaInventoryEntity = GetTeachingMateriaInventory(stockOutDetailEntity.TeachingMaterialPoolID, stockOutDetailEntity.Discount); // if (teachingMateriaInventoryEntity != null) // { // if (teachingMateriaInventoryEntity.PresentInventory <= 0) // { // throw new Exception("该教材库存量为0,不能进行出库!"); // } // if (teachingMateriaInventoryEntity.PresentInventory < stockOutQuantity) // { // throw new Exception("出库数量大于当前库存量!"); // } // //2.0 当前库存量+入库数量 当前库存量-出库数量 // teachingMateriaInventoryEntity.PresentInventory = teachingMateriaInventoryEntity.PresentInventory - stockOutQuantity; // teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; // TeachingMateriaInventoryRepository.UnitOfWork.Update(teachingMateriaInventoryEntity);//更新库存数量 // } // } // UnitOfWork.Commit(); //} #endregion #region 14.0 教材分组 public void GetTMPList(IList tmList, Guid? userId, Guid? stockOutID) { //对数据进行分组、单价、教材ID一致、数量累加 var tmListGroupBy = (from tm in tmList group new { tm } by new { tm.TeachingMaterialPoolID, tm.TeachingMaterialCode, tm.TeachingMaterialName, tm.ISBN, tm.PublishTime, tm.PublishName, tm.Author, tm.Price, tm.Discount, tm.DiscountPrice } into h select new TeachingMaterialPoolView { TeachingMaterialPoolID = h.Key.TeachingMaterialPoolID, TeachingMaterialCode = h.Key.TeachingMaterialCode, TeachingMaterialName = h.Key.TeachingMaterialName, ISBN = h.Key.ISBN, PublishTime = h.Key.PublishTime, PublishName = h.Key.PublishName, Author = h.Key.Author, Price = h.Key.Price, Discount = h.Key.Discount, DiscountPrice = h.Key.DiscountPrice, OrderQty = h.Sum(x => x.tm.OrderQty) }).ToList(); List stockOutDetailList = new List(); foreach (var item in tmListGroupBy) { ET_StockOutDetail stockOutDetail = new ET_StockOutDetail() { CreateTime = DateTime.Now, CreateUserID = userId, DiscountPrice = item.Discount,//折扣率 Discount = item.DiscountPrice,//折合价 ModifyTime = DateTime.Now, ModifyUserID = userId, RecordStatus = 1, StockOutDetailID = Guid.NewGuid(), StockOutID = (Guid)stockOutID, StockOutQuantity = item.OrderQty, TeachingMaterialPoolID = item.TeachingMaterialPoolID }; this.UnitOfWork.Add(stockOutDetail); } UnitOfWork.Commit(); } #endregion #region 15.0 检查库存 //验证出库单据号是否存在 public bool CheckInventoryQty(int qty, Guid teachingMaterialPoolID) { return StockOutDAL.CheckInventoryQty(qty, teachingMaterialPoolID); } #endregion public ET_StockOut GetStockOut(Guid? stockOutID) { //查询条件 System.Linq.Expressions.Expression> expression = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); expression = (x => x.StockOutID == stockOutID); return StockOutDAL.StockOutRepository.GetSingle(expression); } public ET_StockOutDetail GetStockOutDetail(Guid? stockOutDetailID) { //查询条件 System.Linq.Expressions.Expression> expression = (x => true); expression = (x => x.StockOutDetailID == stockOutDetailID); return StockOutDAL.StockOutDetailRepository.GetSingle(expression); } public List GetTeachingMateriaInventory(Guid? teachingMaterialPoolID, decimal? price) { //查询条件 System.Linq.Expressions.Expression> expression = (x => true); expression = (x => x.TeachingMaterialPoolID == teachingMaterialPoolID && x.PresentInventory > 0);//出库不按折合价去扣减库存量 return StockOutDAL.TeachingMateriaInventoryRepository.GetList(expression,(x => x.CF_TeachingMaterialPool)).ToList(); } public string GetStockOutNo(CF_StockOutType stockOutType) { string prefix = ""; DateTime nowTime = DateTime.Now; switch (stockOutType) { case CF_StockOutType.StudentBook: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.StudentMaterialStockOutPrifix); break; case CF_StockOutType.TeacherBook: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.TeacherMaterialStockOutPrifix); break; case CF_StockOutType.StudentPutOut: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.DistributeMaterialStockOutPrifix); break; case CF_StockOutType.TeacherPutOut: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.TeacherDistributeMaterialStockOutPrifix); break; case CF_StockOutType.TeachingMaterialRetail: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.MaterialSaleStockOutPrifix); break; case CF_StockOutType.Refund: prefix = ParameterServices.Value.GetParameterValue(CF_ParameterType.MaterialRefundStockOutPrifix); break; } var matchFormatterList = Regex.Matches(prefix, @"\{[\S]*\}"); for (int i = 0; i < matchFormatterList.Count; i++) { var formatter = matchFormatterList[i].Value.TrimStart('{').TrimEnd('}'); var dateString = nowTime.ToString(formatter); prefix = prefix.Replace(matchFormatterList[i].Value, dateString); } string stockOutNo = SerialNumberServices.Value.SetSN(prefix, 4); return stockOutNo; } } }