using System; using System.Collections.Generic; using System.Linq; using System.Text; using Bowin.Common.Linq.Entity; using Bowin.Common.Linq; using EMIS.ViewModel.TeachingMaterial; using EMIS.DataLogic.Common.TeachingMaterial; using EMIS.DataLogic.Repositories; using EMIS.Entities; using EMIS.ViewModel; using EMIS.DataLogic.Common.Students; using EMIS.ViewModel.Students; using EMIS.CommonLogic.UniversityManage.SpecialtyClassManage; using System.Linq.Expressions; using EMIS.CommonLogic.CalendarManage; namespace EMIS.CommonLogic.TeachingMaterial { public class StudentDistributeServices : BaseServices, IStudentDistributeServices { public StudentsOrderDAL StudentsOrderDAL { get; set; } public StudentDistributeDAL StudentDistributeDAL { get; set; } public StudentsDAL StudentsDAL { get; set; } public StudentDistributeRepository StudentDistributeRepository { get; set; } public TeachingMaterialPoolRepository TeachingMaterialPoolRepository { get; set; } public SchoolyearRepository SchoolyearRepository { get; set; } public UserRepository UserRepository { get; set; } public CoursematerialRepository CoursematerialRepository { get; set; } public SpecialtyPlanRepository SpecialtyPlanRepository { get; set; } public CollegeRepository CollegeRepository { get; set; } public DictionaryItemRepository DictionaryItemRepository { get; set; } public GrademajorRepository GrademajorRepository { get; set; } public FacultymajorRepository FacultymajorRepository { get; set; } public SpecialtyCourseRepository SpecialtyCourseRepository { get; set; } public ClassmajorRepository ClassmajorRepository { get; set; } public PublishRepository PublishRepository { get; set; } public IClassmajorServices classmajorServices { get; set; } public StudentRepository StudentRepository { get; set; } public TeachingMateriaInventoryRepository TeachingMateriaInventoryRepository { get; set; } public ISchoolYearServices schoolYearServices { get; set; } public IStockOutServices stockOutServices { get; set; } /// /// 查询学生发放记录 /// public IGridResultSet GetStudentDistributeViewGrid(ViewModel.ConfiguretView configuretView, Guid? campusID, Guid? collegeID, int? years, Guid? grademajorID, Guid? coursematerialID, int? courseCategoryID, Guid? teachingMaterialPoolID, int? isDistribute, Guid? schoolyearID, int? standardID, int pageIndex, int pageSize) { Expression> exp = x => x.StockOutType == (int)CF_StockOutType.StudentPutOut; if (standardID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.StandardID == standardID); } if (teachingMaterialPoolID.HasValue) { exp = exp.And(x => x.CF_TeachingMaterialPool.TeachingMaterialPoolID == teachingMaterialPoolID); } if (schoolyearID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID); } if (campusID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CampusID == campusID); } if (collegeID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CollegeID == collegeID); } if (years.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.GradeID == years); } if (grademajorID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.GrademajorID == grademajorID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.EM_Coursematerial.CoursematerialID == coursematerialID); } if (courseCategoryID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CourseCategoryID == courseCategoryID); } if (isDistribute.HasValue && isDistribute>-1) { exp = exp.And(x => x.RecordStatus == isDistribute); } var query = StudentDistributeDAL.GetStudentDistributeGridView(exp); //var tmiquery = StudentDistributeDAL.GetInventoryNotOnlyOne(); //if (tmiquery != null) //{ // List teachingMaterialPoolIDList = tmiquery.Select(x => x.TeachingMaterialPoolID).ToList(); // List sdList = query.Where(x => teachingMaterialPoolIDList.Contains((Guid)x.TeachingMaterialPoolID)).ToList(); //} if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.SchoolyearCode). ThenBy(x => x.CollegeNo).ThenBy(x => x.GrademajorCode).ToGridResultSet(pageIndex, pageSize); return this.GetQueryByDataRangeByCollege(query).OrderByDescending(x => x.SchoolyearCode). ThenBy(x => x.CollegeNo).ThenBy(x => x.GrademajorCode).ToGridResultSet(pageIndex, pageSize); } #region 2.0 生成专业计划 /// /// 生成征订计划 /// /// public void CreateStudentDistribute(Guid schoolyearID, Guid userID) { try { #region 0.0 废弃 //var planList = StudentDistributeDAL.GetStudentDistributeRepositoryBySpecialtyPlan(schoolyearID); //foreach (var planView in planList) //{ // var studentDistributeList = (from a in StudentDistributeRepository.Entities.Where(x => x.SpecialtyPlanID == planView.SpecialtyPlanID && x.TeachingMaterialPoolID == planView.TeachingMaterialPoolID) // group a by new { a.TeachingMaterialPoolID, a.SpecialtyPlanID, a.RecordStatus } into g // select new { SpecialtyPlanID = g.Key.SpecialtyPlanID, TeachingMaterialPoolID = g.Key.TeachingMaterialPoolID, g.Key.RecordStatus }).ToList(); // //如果该专业计划对应的课程信息 已提交、 那么可以再次生成、 // bool IsCreate = true; // foreach (var studentDistribute in studentDistributeList) // { // if (studentDistribute.RecordStatus != 1)//已提交 // { // IsCreate = false; // } // } // if (IsCreate) // { // var StudentDistributeEntity = StudentDistributeRepository.Entities.Where(x => x.SpecialtyPlanID == planView.SpecialtyPlanID && x.RecordStatus == 0).FirstOrDefault(); // if (StudentDistributeEntity != null && planView.TeachingMaterialPoolID != null) // { // StudentDistributeEntity.TeachingMaterialPoolID = planView.TeachingMaterialPoolID; // StudentDistributeEntity.ModifyTime = DateTime.Now; // StudentDistributeRepository.UnitOfWork.Update(StudentDistributeEntity); // } // else // { // var studentDistribute = new ET_StudentDistribute // { // StudentDistributeID = Guid.NewGuid(), // RecordStatus = (int)CF_GeneralPurpose.IsNo, // StockOutType = (int)CF_StockOutType.StudentPutOut, // DistributeQty = (int)planView.OrderQty,//发放人数、 // SpecialtyPlanID = planView.SpecialtyPlanID, // TeachingMaterialPoolID = planView.TeachingMaterialPoolID, // CreateTime = DateTime.Now, // CreateUserID = userID, // ModifyTime = DateTime.Now, // Remark = "学生发放" // }; // StudentDistributeRepository.UnitOfWork.Add(studentDistribute); // } // } //} //StudentDistributeRepository.UnitOfWork.Commit(); #endregion #region 1.0废弃 //1.0 查询出该学年对应下的专业计划 //var planIdsList = StudentDistributeDAL.GetStudentDistributeIDsBySpecialtyPlan(schoolyearID); //var specialtyIdsList = planIdsList.Select(x => x.SpecialtyPlanID).ToList(); //if (specialtyIdsList.Count > 0) //{ // UnitOfWork.Delete(x => specialtyIdsList.Contains((Guid)x.SpecialtyPlanID));//先删除、在生成、防止教材没有更新进去 //} //var studentDistributeQuery = StudentDistributeDAL.GetStudentDistributeRepositoryBySpecialtyPlan(schoolyearID); //List planList = this.GetQueryByDataRangeByCollege(studentDistributeQuery).ToList(); //List listStudentDistribute = new List();//学生发放集合 //foreach (var planView in planList) //{ // var studentDistribute = new ET_StudentDistribute // { // StudentDistributeID = Guid.NewGuid(), // RecordStatus = (int)CF_GeneralPurpose.IsNo, // StockOutType = (int)CF_StockOutType.StudentPutOut, // DistributeQty = (int)planView.OrderQty,//发放人数、 // SpecialtyPlanID = planView.SpecialtyPlanID, // TeachingMaterialPoolID = planView.TeachingMaterialPoolID, // CreateTime = DateTime.Now, // CreateUserID = userID, // ModifyUserID = userID, // ModifyTime = DateTime.Now // }; // listStudentDistribute.Add(studentDistribute); // // StudentDistributeRepository.UnitOfWork.Add(studentDistribute); //} //UnitOfWork.BulkInsert(listStudentDistribute); #endregion #region 2.0使用 //生成的数据来源于学生用书里的征订生成中已提交的数据 var distributeList = StudentDistributeDAL.GetStudentDistributeGenerateView(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID && x.IsOrdered == true).ToList(); var needDeleteDistributeIDList = distributeList.Where(x => x.OldStudentDistributeID != null).Select(x => x.OldStudentDistributeID).Distinct().ToList(); #endregion if (needDeleteDistributeIDList.Count > 0) { UnitOfWork.Delete(x => needDeleteDistributeIDList.Contains(x.StudentDistributeID)); } //var studentDistributeQuery = StudentDistributeDAL.GetStudentDistributeRepositoryBySpecialtyPlan(schoolyearID); List listStudentDistribute = new List();//学生发放集合 foreach (var distribute in distributeList) { var studentDistribute = new ET_StudentDistribute { StudentDistributeID = Guid.NewGuid(), RecordStatus = (int)CF_GeneralPurpose.IsNo, StockOutType = (int)CF_StockOutType.StudentPutOut, DistributeQty = (int)distribute.StudentCount,//学生人数、 SpecialtyPlanID = distribute.SpecialtyPlanID, TeachingMaterialPoolID = distribute.TeachingMaterialPoolID, CreateTime = DateTime.Now, CreateUserID = userID, ModifyUserID = userID, ModifyTime = DateTime.Now }; listStudentDistribute.Add(studentDistribute); // StudentDistributeRepository.UnitOfWork.Add(studentDistribute); } UnitOfWork.BulkInsert(listStudentDistribute); //StudentDistributeRepository.UnitOfWork.Commit(); } catch (Exception ex) { throw ex; } } #endregion #region 3.0 确认发放 /// /// 确认发放 /// /// /// /// public void ComfirmStudentDistribute(List studentDistributeID, Guid userID) { var studentDistributes = StudentDistributeDAL.GetStudentDistributeListByStudentDistributeIDs(studentDistributeID); if (studentDistributes.Any(x => x.TeachingMaterialPoolID == null)) { throw new Exception("勾选的征订计划中,存在教材未指定。"); } if (studentDistributes.Any(x => x.RecordStatus == 1))//存在已提交、不能重复提交 { throw new Exception("勾选的征订计划中,存在已发放状态。"); } foreach (var studentDistribute in studentDistributes) { //studentDistribute.RecordStatus = (int)CF_GeneralPurpose.IsYes; studentDistribute.ModifyTime = DateTime.Now; studentDistribute.ModifyUserID = userID; } foreach (var studentDistribute in studentDistributes.ToList()) { StudentDistributeRepository.UnitOfWork.Update(studentDistribute); } StudentDistributeRepository.UnitOfWork.Commit(); } #endregion #region 4.0 删除发放信息 //删除发放信息 public void DeleteStudentDistribute(List studentDistributeIDs) { try { var studentDistributeList = StudentDistributeRepository.GetList(x => studentDistributeIDs.Contains(x.StudentDistributeID)).ToList(); foreach (var studentDistribute in studentDistributeList) { if (studentDistribute.RecordStatus == (int)SYS_STATUS.USABLE) { throw new Exception("勾选信息包含已发放状态,不能删除!"); } } TeachingMaterialPoolRepository.UnitOfWork.Delete(x => studentDistributeIDs.Contains(x.StudentDistributeID)); } catch (Exception ex) { throw ex; } } #endregion #region 5.0 查询学生信息 public IGridResultSet GetStudentViewGrid(ConfiguretView configuretView, int pageIndex, int pageSize) { var query = StudentsDAL.GetStudentQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); } #endregion #region 6.0 保存未发学生信息 public void SaveNotStudentDistribute(StudentDistributeView studentDistributeView, IList studentsList, Guid? userId) { throw new NotImplementedException(); } #endregion #region 7.0 保存发放清单 public void SaveStudentReleaseInventory(StudentDistributeView studentDistributeView, IList studentsList, Guid? userId) { throw new NotImplementedException(); } #endregion #region 8.0 保存未发放清单 public void SaveStudentUnpublishedInventory(StudentDistributeView studentDistributeView, IList studentsList, Guid? userId) { try { #region 对用户先去的数进行重复排除 //对数据进行分组、单价、教材ID一致、数量累加 var studentsListGroupBy = (from sd in studentsList group new { sd } by new { sd.UserID, } into h select new { UserID = h.Key.UserID }); List userIdList = new List(); foreach (var item in studentsListGroupBy) { userIdList.Add(item.UserID); } #endregion var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current; ET_StudentDistribute studentDistributeEntity = StudentDistributeRepository .GetSingle(x => x.StudentDistributeID == studentDistributeView.StudentDistributeID, x => x.ET_StudentDistribute_CF_Student);//有多对多关系、一定要指定属性 if (studentDistributeEntity != null) { studentDistributeEntity.ET_StudentDistribute_CF_Student = new HashSet();//该学生发放ID、对应的所有数据 var queryStudent = StudentRepository.Entities.Where(x => userIdList.Contains(x.UserID)); foreach (var student in queryStudent) { studentDistributeEntity.ET_StudentDistribute_CF_Student.Add(student); } ////用学生人数和~~~减去未发人数~~ //studentDistributeEntity.DistributeQty = studentDistributeView.StudentQty - queryStudent.Count(); studentDistributeEntity.ModifyUserID = curUser.UserID; UnitOfWork.Update(studentDistributeEntity);//更新发放数量 UnitOfWork.Commit(); } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region 9.0 根据发放ID、加载未发学生信息 public IList GetStudentDistributeByStudentViewList(Guid studentDistributeID) { return StudentDistributeDAL.GetStudentQueryable(x => x.StudentDistributeID == studentDistributeID).OrderBy(x=>x.LoginID.Length).ThenBy(x=>x.LoginID).ToList(); } #endregion #region 10.0 查询学生发放Excel导出 /// /// 查询学生发放记录 /// public IList GetStudentDistributeViewList(ViewModel.ConfiguretView configuretView, Guid? campusID, Guid? collegeID, int? years, Guid? grademajorID, Guid? coursematerialID, int? courseCategoryID, Guid? teachingMaterialPoolID, int? isDistribute, Guid? schoolyearID, int? standardID) { Expression> exp = x => x.StockOutType == (int)CF_StockOutType.StudentPutOut; if (standardID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.StandardID == standardID); } if (teachingMaterialPoolID.HasValue) { exp = exp.And(x => x.CF_TeachingMaterialPool.TeachingMaterialPoolID == teachingMaterialPoolID); } if (schoolyearID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.SchoolyearID == schoolyearID); } if (campusID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CampusID == campusID); } if (collegeID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.CF_Facultymajor.CF_College.CollegeID == collegeID); } if (years.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.GradeID == years); } if (grademajorID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CF_Grademajor.GrademajorID == grademajorID); } if (coursematerialID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.EM_Coursematerial.CoursematerialID == coursematerialID); } if (courseCategoryID.HasValue) { exp = exp.And(x => x.EM_SpecialtyPlan.CourseCategoryID == courseCategoryID); } if (isDistribute.HasValue && isDistribute > -1) { exp = exp.And(x => x.RecordStatus == isDistribute); } var query = StudentDistributeDAL.GetStudentDistributeGridView(exp); if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderByDescending(x => x.SchoolyearCode). ThenBy(x => x.CollegeNo).ThenBy(x => x.GrademajorCode).ToList(); return this.GetQueryByDataRangeByCollege(query).OrderByDescending(x => x.SchoolyearCode). ThenBy(x => x.CollegeNo).ThenBy(x => x.GrademajorCode).ToList(); } #endregion #region 11.0 获取编辑数据 //获取编辑数据 public StudentDistributeView GetSingleStudentDistribute(Guid studentDistributeID) { return StudentDistributeDAL.GetSingleStudentDistribute(x => x.StudentDistributeID == studentDistributeID); } #endregion #region 12.0 获取未发放学生列表 //获取未发放学生列表 public IGridResultSet GetStudentDistributeByUserViewList(ConfiguretView configuretView, Guid? campusID, Guid? collegeID, int? yearID, int? standardID, int? learningformID, Guid? studentDistributeID, int? IsGraduation, int? inSchoolStatusID, Guid? grademajorID, int pageIndex, int pageSize) { ET_StudentDistribute studentDistributeEntity = StudentDistributeRepository .GetSingle(x => x.StudentDistributeID == studentDistributeID, (x => x.ET_StudentDistribute_CF_Student));//排除已经选取的学生 var query = StudentDistributeDAL.GetStudentQueryable_User(x => x.StudentDistributeID == studentDistributeID); if (studentDistributeEntity.ET_StudentDistribute_CF_Student.Count > 0) { List IdList = studentDistributeEntity.ET_StudentDistribute_CF_Student.Select(x => x.UserID).ToList(); query = query.Where(x => !IdList.Contains(x.UserID));//排除已经选取的学生 } if (!string.IsNullOrEmpty(configuretView.ConditionValue)) return query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue).OrderBy(x => x.LoginID.Length).ThenBy(x=>x.LoginID).ToGridResultSet(pageIndex, pageSize); return query.OrderBy(x => x.LoginID.Length).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); } #endregion #region 13.0保存并提交学生放发信息 //保存并提交学生放发信息 public void Save(List studentDistributeID, StudentDistributeView studentDistributeView) { try { if (StudentDistributeRepository.Entities.Where(x => x.StudentDistributeNo == studentDistributeView.StudentDistributeNo).Count() > 0) { throw new Exception("出库单号已经存在,请重新输入!"); } var studentDistributeEntityList = StudentDistributeRepository.Entities.Where(x => studentDistributeID.Contains(x.StudentDistributeID)).ToList(); if (studentDistributeEntityList.Any(x => x.RecordStatus == (int)CF_GeneralPurpose.IsYes)) { throw new Exception("该信息已经提交,请勿重复提交!"); } foreach (var studentDistributeEntity in studentDistributeEntityList) { ET_StudentDistribute StuDisEntityLists = StudentDistributeRepository.GetSingle(x => x.StudentDistributeID == studentDistributeEntity.StudentDistributeID, (x => x.ET_StudentDistribute_CF_Student)); var query = StudentDistributeDAL.GetInventory((Guid)studentDistributeEntity.TeachingMaterialPoolID); List inList = query.OrderBy(x => x.CreateTime).ToList(); int presentInventory = 0; int distributeQty = studentDistributeEntity.DistributeQty - studentDistributeEntity.ET_StudentDistribute_CF_Student.Count();//发放人数=用学生总人数-未发人数 //学生征订提交时生成出库单 ET_StockOut stockOut = new ET_StockOut(); List stockOutDetailList = new List(); Guid schoolyearID = schoolYearServices.GetSchoolYearIsCurrent(true).SchoolyearID; stockOut.StockOutID = Guid.NewGuid(); stockOut.OutNumber = distributeQty;//发放人数 stockOut.SchoolyearID = schoolyearID; stockOut.StockOutNo = studentDistributeView.StudentDistributeNo;//出库单据号暂时采用ID编号字符串 stockOut.GetBookNo = studentDistributeView.RecipientUser; stockOut.StockOutType = (int)CF_StockOutType.StudentPutOut; stockOut.RecipientUserID = studentDistributeView.RecipientUserID; stockOut.StockOutTime = DateTime.Now; stockOut.Desc = studentDistributeView.Remark; SetNewStatus(stockOut); if (inList.Sum(x => x.PresentInventory) >= distributeQty) { //stockOutEntity.RecordStatus = (int)CF_GeneralPurpose.IsYes; //stockOutEntity.ModifyUserID = userId; //StockOutRepository.UnitOfWork.Update(stockOutEntity); // StockOutRepository.UnitOfWork.Commit(); List stockOutDetailListsave = new List(); //1.0 提取当前库存量、 //foreach (var stockOutDetailEntity in stockOutDetailList) //{ int? stockOutQuantity = distributeQty; List teachingMateriaInventorylist = stockOutServices.GetTeachingMateriaInventory(studentDistributeEntity.TeachingMaterialPoolID, 0).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) { ET_StockOutDetail stockoutDetail = new ET_StockOutDetail(); stockoutDetail.StockOutDetailID = Guid.NewGuid(); stockoutDetail.StockOutID = stockOut.StockOutID; stockoutDetail.TeachingMaterialPoolID = teachingMateriaInventory.TeachingMaterialPoolID; stockoutDetail.StockOutQuantity = distributeQty; stockoutDetail.Discount = teachingMateriaInventory.DiscountPrice;//出库明细折合价 stockoutDetail.DiscountPrice = teachingMateriaInventory.Discount;//出库明细折扣率 SetModifyStatus(stockoutDetail); stockOut.ET_StockOutDetail.Add(stockoutDetail); 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; ET_StockOutDetail stockoutDetail = new ET_StockOutDetail(); stockoutDetail.StockOutDetailID = Guid.NewGuid(); stockoutDetail.StockOutID = stockOut.StockOutID; stockoutDetail.TeachingMaterialPoolID = teachingMateriaInventory.TeachingMaterialPoolID; stockoutDetail.StockOutQuantity = (view.PresentInventory - needQuantity) <= 0 ? view.PresentInventory : needQuantity; stockoutDetail.Discount = view.DiscountPrice; stockoutDetail.DiscountPrice = view.Discount; //Math.Round((decimal)(view.Discount / view.CF_TeachingMaterialPool.Price), 2); SetModifyStatus(stockoutDetail); stockOut.ET_StockOutDetail.Add(stockoutDetail); 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("出库数量大于当前库存量!"); } //for (int i = 0; i < inList.Count; i++) //{ // InventoryView inventoryView = inList[i]; // presentInventory = presentInventory + (int)inList[i].PresentInventory; // if (presentInventory >= distributeQty) // { // if (i == 0) // { // ET_TeachingMateriaInventory teachingMateriaInventoryEntity = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == inventoryView.TeachingMaterialPoolID && x.CreateTime == inventoryView.CreateTime && x.PresentInventory > 0,(x => x.CF_TeachingMaterialPool)); // //出库单详情 // ET_StockOutDetail stockoutDetail = new ET_StockOutDetail(); // stockoutDetail.StockOutDetailID = Guid.NewGuid(); // stockoutDetail.StockOutID = stockOut.StockOutID; // stockoutDetail.TeachingMaterialPoolID = inventoryView.TeachingMaterialPoolID; // stockoutDetail.StockOutQuantity = distributeQty; // stockoutDetail.Discount = teachingMateriaInventoryEntity.Discount; // stockoutDetail.DiscountPrice = Math.Round((decimal)(teachingMateriaInventoryEntity.Discount/teachingMateriaInventoryEntity.CF_TeachingMaterialPool.Price),2); // SetModifyStatus(stockoutDetail); // stockOut.ET_StockOutDetail.Add(stockoutDetail); // teachingMateriaInventoryEntity.PresentInventory = teachingMateriaInventoryEntity.PresentInventory - distributeQty; // teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; // break; // } // else // { // InventoryView inventoryViewold = inList[i - 1]; // ET_TeachingMateriaInventory teachingMateriaInventoryEntity = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == inventoryViewold.TeachingMaterialPoolID && x.CreateTime == inventoryViewold.CreateTime && x.PresentInventory > 0, (x => x.CF_TeachingMaterialPool)); // int pre = (int)teachingMateriaInventoryEntity.PresentInventory;//已入库数量 // //出库单详情 // ET_StockOutDetail stockoutDetail = new ET_StockOutDetail(); // stockoutDetail.StockOutDetailID = Guid.NewGuid(); // stockoutDetail.StockOutID = stockOut.StockOutID; // stockoutDetail.TeachingMaterialPoolID = inventoryViewold.TeachingMaterialPoolID; // stockoutDetail.StockOutQuantity = pre; // stockoutDetail.Discount = teachingMateriaInventoryEntity.Discount; // stockoutDetail.DiscountPrice = Math.Round((decimal)(teachingMateriaInventoryEntity.Discount / teachingMateriaInventoryEntity.CF_TeachingMaterialPool.Price), 2); // SetNewStatus(stockoutDetail); // stockOut.ET_StockOutDetail.Add(stockoutDetail); // teachingMateriaInventoryEntity.PresentInventory = 0; // teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; // ET_TeachingMateriaInventory teachingMateriaInventoryEntitytwo = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == inventoryView.TeachingMaterialPoolID && x.CreateTime == inventoryView.CreateTime && x.PresentInventory > 0, (x => x.CF_TeachingMaterialPool)); // //出库单详情 // ET_StockOutDetail stockoutDetailtwo = new ET_StockOutDetail(); // stockoutDetailtwo.StockOutDetailID = Guid.NewGuid(); // stockoutDetailtwo.StockOutID = stockOut.StockOutID; // stockoutDetailtwo.TeachingMaterialPoolID = inventoryView.TeachingMaterialPoolID; // stockoutDetailtwo.StockOutQuantity = distributeQty - pre; // stockoutDetailtwo.Discount = teachingMateriaInventoryEntity.Discount; // stockoutDetail.DiscountPrice = Math.Round((decimal)(teachingMateriaInventoryEntity.Discount / teachingMateriaInventoryEntity.CF_TeachingMaterialPool.Price), 2); // SetNewStatus(stockoutDetailtwo); // stockOut.ET_StockOutDetail.Add(stockoutDetailtwo); // teachingMateriaInventoryEntitytwo.PresentInventory = teachingMateriaInventoryEntitytwo.PresentInventory - (distributeQty - pre); // teachingMateriaInventoryEntitytwo.ModifyTime = DateTime.Now; // break; // } // } // else // { // continue; // } //} } else { throw new Exception("征订数量大于当前库存量!"); } //ET_TeachingMateriaInventory teachingMateriaInventoryEntity = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == studentDistributeEntity.TeachingMaterialPoolID); //if (teachingMateriaInventoryEntity != null) //{ // int distributeQty = studentDistributeEntity.DistributeQty - studentDistributeEntity.CF_Student.Count();//用学生总人数-未发人数 // if (teachingMateriaInventoryEntity.PresentInventory <= 0) // { // throw new Exception("该教材库存量为0,不能进行出库!"); // } // if (teachingMateriaInventoryEntity.PresentInventory < distributeQty) // { // throw new Exception("征订数量大于当前库存量!"); // } // //2.0 当前库存量+入库数量 当前库存量-出库数量 // teachingMateriaInventoryEntity.PresentInventory = teachingMateriaInventoryEntity.PresentInventory - distributeQty; // teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; // UnitOfWork.Update(teachingMateriaInventoryEntity);//更新库存数量 //} // BalanceInventory(studentDistributeEntity); if (studentDistributeEntity != null) { studentDistributeEntity.RecipientUser = studentDistributeView.RecipientUser;//领书人 studentDistributeEntity.StudentDistributeNo = studentDistributeView.StudentDistributeNo;//出库单据号 studentDistributeEntity.RecordStatus = (int)CF_GeneralPurpose.IsYes;//设置状态已提交! studentDistributeEntity.Remark = studentDistributeView.Remark; } UnitOfWork.Add(stockOut); } UnitOfWork.Commit(); } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region 14.0 平衡教材库存数量(适用与出库、入库) public void BalanceInventory(ET_StudentDistribute studentDistribute) { ET_StudentDistribute studentDistributeEntity = StudentDistributeRepository.GetSingle(x => x.StudentDistributeID == studentDistribute.StudentDistributeID, (x => x.ET_StudentDistribute_CF_Student)); ET_TeachingMateriaInventory teachingMateriaInventoryEntity = TeachingMateriaInventoryRepository.GetSingle(x => x.TeachingMaterialPoolID == studentDistribute.TeachingMaterialPoolID); if (teachingMateriaInventoryEntity != null) { int distributeQty = studentDistributeEntity.DistributeQty - studentDistributeEntity.ET_StudentDistribute_CF_Student.Count();//用学生总人数-未发人数 if (teachingMateriaInventoryEntity.PresentInventory <= 0) { throw new Exception("该教材库存量为0,不能进行出库!"); } if (teachingMateriaInventoryEntity.PresentInventory < distributeQty) { throw new Exception("征订数量大于当前库存量!"); } //2.0 当前库存量+入库数量 当前库存量-出库数量 teachingMateriaInventoryEntity.PresentInventory = teachingMateriaInventoryEntity.PresentInventory - distributeQty; teachingMateriaInventoryEntity.ModifyTime = DateTime.Now; UnitOfWork.Update(teachingMateriaInventoryEntity);//更新库存数量 UnitOfWork.Commit(); } } #endregion //发放清单列表 public IGridResultSet GetStudentDistributeDetailViewList(Guid? studentDistributeID, int pageIndex, int pageSize) { var query = StudentDistributeDAL.GetStudentQueryableByStudentDistributeID(x => x.StudentDistributeID == studentDistributeID); query = query.Where(x => x.StudentNo != null); return query.OrderBy(x => x.StudentNo.Length).ThenBy(x => x.StudentNo).ToGridResultSet(pageIndex, pageSize); } public IList GetStudentDistributeDetailQuery(Guid? studentDistributeID) { var query = StudentDistributeDAL.GetStudentQueryableByStudentDistributeID(x => x.StudentDistributeID == studentDistributeID); query = query.Where(x => x.StudentNo != null); return query.OrderBy(x => x.StudentNo.Length).ThenBy(x => x.StudentNo).ToList(); } } }