using FineUIPro; using Model; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLL { public static class TwInputmasterService { #region 获取列表 /// /// 记录数 /// public static int Count { get; set; } public static IQueryable GetModle(Model.Tw_InOutMasterOutput table) { var q = from x in Funs.DB.Tw_InputMaster join person in Funs.DB.Person_Persons on x.CreateMan equals person.PersonId into persons from person in persons.DefaultIfEmpty() join auditperson in Funs.DB.Person_Persons on x.AuditMan equals auditperson.PersonId into auditpersons from auditperson in auditpersons.DefaultIfEmpty() join warehouseperson in Funs.DB.Person_Persons on x.WarehouseMan equals warehouseperson.PersonId into warehousepersons from warehouseperson in warehousepersons.DefaultIfEmpty() join unit in Funs.DB.Base_Unit on x.ReqUnitId equals unit.UnitId into units from unit in units.DefaultIfEmpty() where (string.IsNullOrEmpty(table.Id) || x.Id.Contains(table.Id)) && (string.IsNullOrEmpty(table.InOutPlanMasterId) || x.InOutPlanMasterId.Contains(table.InOutPlanMasterId)) && (string.IsNullOrEmpty(table.ProjectId) || x.ProjectId.Contains(table.ProjectId)) && (string.IsNullOrEmpty(table.CusBillCode) || x.CusBillCode.Contains(table.CusBillCode)) && (string.IsNullOrEmpty(table.WarehouseCode) || x.WarehouseCode.Contains(table.WarehouseCode)) && (string.IsNullOrEmpty(table.CreateMan) || x.CreateMan.Contains(table.CreateMan)) && (string.IsNullOrEmpty(table.ReqUnitId) || x.ReqUnitId.Contains(table.ReqUnitId)) && (table.TypeInt == null || x.TypeInt == table.TypeInt) && (table.Category == null || x.Category == table.Category) && (table.State == null || x.State == table.State) select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, InOutPlanMasterId = x.InOutPlanMasterId, WarehouseCode = x.WarehouseCode, Source = x.Source, Category = x.Category, TypeInt = x.TypeInt, State = x.State, CreateMan = x.CreateMan, CreateManName = person.PersonName, CreateDate = x.CreateDate, ReqUnitId = x.ReqUnitId, ReqUnitName = unit.UnitName, AuditMan = x.AuditMan, AuditManName = auditperson.PersonName, AuditDate = x.AuditDate, Remark = x.Remark, WarehouseMan = x.WarehouseMan, WarehouseManName = warehouseperson.PersonName, WarehouseDate = x.WarehouseDate }; return q; } /// /// 获取分页列表 /// /// /// /// public static IEnumerable GetListData(Model.Tw_InOutMasterOutput table, Grid grid1) { var q = GetModle(table); Count = q.Count(); if (Count == 0) { return null; } var result = q.Skip(grid1.PageSize * grid1.PageIndex).Take(grid1.PageSize).ToList(); // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize); return from x in result select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, InOutPlanMasterId = x.InOutPlanMasterId, WarehouseCode = x.WarehouseCode, Source = x.Source, TypeInt = x.TypeInt, State = x.State, CreateMan = x.CreateMan, CreateManName = x.CreateManName, CreateDate = x.CreateDate, ReqUnitId = x.ReqUnitId, ReqUnitName = x.ReqUnitName, Category = x.Category, CategoryString = TwConst.CategoryMap.FirstOrDefault(y => y.Value == x.Category).Key, TypeString = TwConst.TypeIntMap.FirstOrDefault(y => y.Value == x.TypeInt).Key, StateString = TwConst.StateMap.FirstOrDefault(y => y.Value == x.State).Key, AuditMan = x.AuditMan, AuditManName = x.AuditManName, AuditDate = x.AuditDate, Remark=x.Remark, WarehouseMan = x.WarehouseMan, WarehouseDate = x.WarehouseDate, WarehouseManName= x.WarehouseManName }; } public static List GetListData(Model.Tw_InOutMasterOutput table ) { var q = GetModle(table); Count = q.Count(); if (Count == 0) { return null; } var result = q.ToList(); // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize); return (from x in result select new Model.Tw_InOutMasterOutput { Id = x.Id, ProjectId = x.ProjectId, CusBillCode = x.CusBillCode, InOutPlanMasterId = x.InOutPlanMasterId, WarehouseCode = x.WarehouseCode, Source = x.Source, TypeInt = x.TypeInt, State = x.State, CreateMan = x.CreateMan, CreateManName = x.CreateManName, CreateDate = x.CreateDate, ReqUnitId = x.ReqUnitId, ReqUnitName = x.ReqUnitName, Category = x.Category, CategoryString = TwConst.CategoryMap.FirstOrDefault(y => y.Value == x.Category).Key, TypeString = TwConst.TypeIntMap.FirstOrDefault(y => y.Value == x.TypeInt).Key, StateString = TwConst.StateMap.FirstOrDefault(y => y.Value == x.State).Key, AuditMan = x.AuditMan, AuditManName = x.AuditManName, AuditDate = x.AuditDate, Remark = x.Remark, WarehouseMan = x.WarehouseMan, WarehouseDate = x.WarehouseDate, WarehouseManName= x.WarehouseManName }).ToList(); } #endregion public static Model.Tw_InputMaster GetById(string Id) { return Funs.DB.Tw_InputMaster.FirstOrDefault(x => x.Id == Id); } public static void Add(Model.Tw_InputMaster newtable) { Model.Tw_InputMaster table = new Model.Tw_InputMaster { Id = newtable.Id, ProjectId = newtable.ProjectId, InOutPlanMasterId = newtable.InOutPlanMasterId, CusBillCode = newtable.CusBillCode, WarehouseCode = newtable.WarehouseCode, Category = newtable.Category, Source = newtable.Source, TypeInt = newtable.TypeInt, State = newtable.State, CreateMan = newtable.CreateMan, CreateDate = newtable.CreateDate, ReqUnitId = newtable.ReqUnitId, AuditMan= newtable.AuditMan, AuditDate = newtable.AuditDate, Remark = newtable.Remark, WarehouseMan= newtable.WarehouseMan, WarehouseDate = newtable.WarehouseDate }; Funs.DB.Tw_InputMaster.InsertOnSubmit(table); Funs.DB.SubmitChanges(); } public static void Update(Model.Tw_InputMaster newtable) { Model.Tw_InputMaster table = Funs.DB.Tw_InputMaster.FirstOrDefault(x => x.Id == newtable.Id); if (table != null) { table.Id = newtable.Id; table.ProjectId = newtable.ProjectId; table.InOutPlanMasterId = newtable.InOutPlanMasterId; table.CusBillCode = newtable.CusBillCode; table.WarehouseCode = newtable.WarehouseCode; table.Category = newtable.Category; table.Source = newtable.Source; table.TypeInt = newtable.TypeInt; table.State = newtable.State; table.CreateMan = newtable.CreateMan; table.CreateDate = newtable.CreateDate; table.ReqUnitId = newtable.ReqUnitId; table.AuditMan = newtable.AuditMan; table.AuditDate = newtable.AuditDate; table.Remark = newtable.Remark; table.WarehouseMan = newtable.WarehouseMan; table.WarehouseDate = newtable.WarehouseDate; Funs.DB.SubmitChanges(); } } public static void DeleteById(string Id) { Model.Tw_InputMaster table = Funs.DB.Tw_InputMaster.FirstOrDefault(x => x.Id == Id); if (table != null) { Funs.DB.Tw_InputMaster.DeleteOnSubmit(table); Funs.DB.SubmitChanges(); } } /// /// 根据计划单生成入库单 /// public static void GenInMasterByPlanId(string planId, List detailLists,string remark) { //获取计划单 var planQueryModel = new Tw_InOutMasterOutput(); planQueryModel.Id = planId; var plan = TwInOutplanmasterService.GetListData(planQueryModel).FirstOrDefault(); if (plan == null || plan.State!= (int)TwConst.State.已审核) { return; } //判断是否已经生成过入库单 var queryIsExitInMaster = new Tw_InOutMasterOutput(); queryIsExitInMaster.InOutPlanMasterId = planId; queryIsExitInMaster.TypeInt = plan.TypeInt; queryIsExitInMaster.Category = plan.Category; var IsExitInMaster = GetModle(queryIsExitInMaster).FirstOrDefault(); if (IsExitInMaster != null) { return; } //生成入库单 var master = new Model.Tw_InputMaster() { Id = Guid.NewGuid().ToString(), InOutPlanMasterId = plan.Id, ProjectId = plan.ProjectId, WarehouseCode = plan.WarehouseCode, Source = plan.Source, TypeInt = plan.TypeInt, Category = plan.Category, State = (int)TwConst.State.已完成, CreateMan = plan.CreateMan, CreateDate = plan.CreateDate, ReqUnitId = plan.ReqUnitId, AuditMan = plan.AuditMan, AuditDate = plan.AuditDate, Remark= remark, WarehouseMan = plan.WarehouseMan, WarehouseDate = plan.WarehouseDate }; if ((TwConst.TypeInt)plan.TypeInt!= TwConst.TypeInt.采购入库) { master.CusBillCode = GetCusBillCodeByTaskCode(plan.WeldTaskCode, (TwConst.TypeInt)plan.TypeInt, (BLL.TwConst.Category)plan.Category); } else { master.CusBillCode= plan.CusBillCode.Replace("-AP", ""); } Add(master); //生成明细 foreach (var detail in detailLists) { var detailTable = new Model.Tw_InputDetail() { Id = Guid.NewGuid().ToString(), InputMasterId = master.Id, MaterialCode = detail.MaterialCode, PlanNum = detail.PlanNum, ActNum = detail.ActNum, SortIndex = detail.SortIndex, }; TwInputdetailService.Add(detailTable); TwMaterialstockService.UpdateStockNum(master.ProjectId, detail.MaterialCode, master.WarehouseCode, TwConst.InOutType.入库, detailTable.ActNum); } plan.State= (int)TwConst.State.已完成; TwInOutplanmasterService.Update(plan); } /// /// 根据计划单撤销入库单 /// /// public static void RevokeGenInMasterByPlanId(string planId) { //校验计划单状态 var plan = TwInOutplanmasterService.GetById(planId); if (plan == null || plan.State != (int)TwConst.State.已完成) { return; } //获取入库单 Tw_InOutMasterOutput query = new Tw_InOutMasterOutput(); query.InOutPlanMasterId = planId; var master = GetModle(query).FirstOrDefault(); if (master == null) { return; } DeleteById(master.Id); //删除入库单 //删除明细 Tw_InOutDetailOutput queryDetail = new Tw_InOutDetailOutput(); queryDetail.InputMasterId = master.Id; var details = TwInputdetailService.GetByModle(queryDetail).ToList(); foreach (var detail in details) { TwInputdetailService.DeleteById(detail.Id); //撤销入库,即减去库存 TwMaterialstockService.UpdateStockNum(master.ProjectId,detail.MaterialCode, master.WarehouseCode,TwConst.InOutType.出库, detail.ActNum); } plan.State = (int)TwConst.State.已审核; TwInOutplanmasterService.Update(plan); } public static string GetCusBillCodeByTaskCode(string taskCode, TwConst.TypeInt typeInt, TwConst.Category category) { string cusBillCode = ""; switch (typeInt) { case TwConst.TypeInt.退料入库: if (category == TwConst.Category.管段) { cusBillCode = taskCode + "-GI-P01-RE01"; } else if (category == TwConst.Category.管件) { cusBillCode = taskCode + "-GI-PF01-RE01"; } break; } return cusBillCode; } } }