/// <summary> /// 采购完成后重新领料 /// </summary> /// <param name="orderID"></param> /// <param name="currentUser"></param> /// <returns></returns> public async Task <ResponseObject <NOEntity> > RePick(int orderID, CurrentUser currentUser) { try { TMMProductionOrderMainDbModel tMMProductionOrderMainDbModel = _db.Instance.Queryable <TMMProductionOrderMainDbModel>().Where(p => p.ID == orderID).First(); if (tMMProductionOrderMainDbModel.IsPickAll == true) { throw new Exception("已全部转领料"); } TMMPurchaseApplyMainDbModel purchaseEntity = await _db.Instance.Queryable <TMMPurchaseApplyMainDbModel>().Where(p => p.SourceId == orderID && p.CompanyId == currentUser.CompanyID && p.DeleteFlag == false).FirstAsync(); NOEntity nOEntity = new NOEntity(); if (purchaseEntity == null) { throw new Exception("还没采购,或者无需采购"); } if (purchaseEntity.TransferFlag == true) { throw new Exception("采购流程未完成"); } else { int producePurchaseId = purchaseEntity.ID; if (_db.Instance.Queryable <TPSMPurchaseOrderMainDbModel>().Any(p => p.DeleteFlag == false && p.TransferStatus == true && p.SourceId == purchaseEntity.ID)) { throw new Exception("采购流程未完成"); } var allTWMPurchase = _db.Instance.Queryable <TWMPurchaseDetailDbModel, TWMPurchaseMainDbModel>((t, t1) => new object[] { JoinType.Inner, t.MainId == t1.ID }). Where((t, t1) => t1.DeleteFlag == false && SqlFunc.Subqueryable <TPSMPurchaseOrderMainDbModel>(). Where(p1 => p1.SourceId == producePurchaseId && t1.SourceId == p1.ID).Any()).Select((t, t1) => new { Main = t1, Deatail = t }).ToList(); if (allTWMPurchase.Any(p => p.Main.AuditStatus != 2)) { throw new Exception("采购流程未完成"); } else { var basicMe = BasicCacheGet.GetMaterial(currentUser); var groupSum = allTWMPurchase.Select(p => p.Deatail).GroupBy(x => x.MaterialId); List <TMMPickApplyDetailDbModel> PickApplyList = new List <TMMPickApplyDetailDbModel>(); foreach (var item in groupSum) { var key = item.Key; var itemDeatails = item.ToList(); var firstItem = itemDeatails.FirstOrDefault(); decimal total = item.Sum(p => p.ActualNum); var me = basicMe.Where(p => p.ID == item.Key).FirstOrDefault(); decimal ProduceTotal = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Produce, total); TMMPickApplyDetailDbModel ApplyDetail = new TMMPickApplyDetailDbModel(); ApplyDetail.ApplyNum = ProduceTotal; ApplyDetail.MaterialId = key; ApplyDetail.TransNum = ProduceTotal; PickApplyList.Add(ApplyDetail); } if (PickApplyList.Count() > 0) { TMMPickApplyMainDbModel tMMPickApplyMainDbModel = new TMMPickApplyMainDbModel(); tMMPickApplyMainDbModel.DeleteFlag = false; tMMPickApplyMainDbModel.SourceId = orderID; //tMMPickApplyMainDbModel.PickNo = OrderGenerator.Generator(OrderEnum.PMR, currentUser.CompanyID); string code = _codeMakers.Where(p => p.ProvideName == OrderEnum.PMR.GetDescription()).FirstOrDefault()?.MakeNo(currentUser.CompanyID); tMMPickApplyMainDbModel.PickNo = code; tMMPickApplyMainDbModel.ApplyDate = DateTime.Now; tMMPickApplyMainDbModel.OperatorId = currentUser.UserID; tMMPickApplyMainDbModel.OperatorTime = DateTime.Now; tMMPickApplyMainDbModel.AuditStatus = 2; tMMPickApplyMainDbModel.CompanyId = currentUser.CompanyID; tMMPickApplyMainDbModel.TransferFlag = true; int mId = _db.Instance.Insertable(tMMPickApplyMainDbModel).ExecuteReturnIdentity(); PickApplyList.ForEach((x) => { x.MainId = mId; }); _db.Instance.Insertable(PickApplyList).ExecuteCommand(); _db.Instance.Updateable <TMMProductionOrderMainDbModel>().SetColumns(p => new TMMProductionOrderMainDbModel { IsPickAll = true }).Where(p => p.ID == orderID).ExecuteCommand(); nOEntity.ID = tMMPickApplyMainDbModel.ID; nOEntity.NO = tMMPickApplyMainDbModel.PickNo; } } } _db.Instance.CommitTran(); return(ResponseUtil <NOEntity> .SuccessResult(nOEntity)); } catch (Exception ex) { _db.Instance.RollbackTran(); return(ResponseUtil <NOEntity> .FailResult(null, ex.Message)); } }
/// <summary> /// 转采购 /// </summary> /// <returns></returns> public async Task <ResponseObject <NOEntity> > TransPurchase(int orderID, CurrentUser currentUser) { try { _db.Instance.BeginTran(); NOEntity nOEntity = new NOEntity(); TMMProductionOrderMainDbModel mainEntity = _db.Instance.Queryable <TMMProductionOrderMainDbModel>().Where(p => p.ID == orderID && p.CompanyId == currentUser.CompanyID && p.IsPurchase == true).First(); if (mainEntity == null) { return(ResponseUtil <NOEntity> .FailResult(nOEntity, "生产订单不存在,或者已转生产采购单")); } if (mainEntity.MRPStatus == false) { return(ResponseUtil <NOEntity> .FailResult(nOEntity, "还没算料,无法转单")); } var Allmaterial = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>().Where(p => p.ProOrderId == orderID).ToList(); //生产单BOM List <TMMPurchaseApplyDetailDbModel> PurchaseList = new List <TMMPurchaseApplyDetailDbModel>(); //生产采购申请单明细表 List <TMMProductionOrderBOMSumDbModel> productionList = new List <TMMProductionOrderBOMSumDbModel>(); //生产单BOM foreach (var item in Allmaterial) { if (item.PurchaseNum > 0) //采购 { TMMPurchaseApplyDetailDbModel purchaseDeatail = new TMMPurchaseApplyDetailDbModel(); purchaseDeatail.ApplyNum = item.PurchaseNum; purchaseDeatail.MaterialId = item.MaterialId; purchaseDeatail.TransNum = item.PurchaseNum; PurchaseList.Add(purchaseDeatail); item.PurchaseTransNum = item.PurchaseNum; productionList.Add(item); } } if (PurchaseList.Count() > 0) { TMMPurchaseApplyMainDbModel tMMPurchaseApplyMainDbModel = new TMMPurchaseApplyMainDbModel(); tMMPurchaseApplyMainDbModel.DeleteFlag = false; tMMPurchaseApplyMainDbModel.SourceId = orderID; tMMPurchaseApplyMainDbModel.PurchaseNo = _codeMakers.Where(p => p.ProvideName == OrderEnum.PR.GetDescription()).FirstOrDefault().MakeNo(currentUser.CompanyID); tMMPurchaseApplyMainDbModel.ApplyDate = DateTime.Now; tMMPurchaseApplyMainDbModel.OperatorId = currentUser.UserID; tMMPurchaseApplyMainDbModel.OperatorTime = DateTime.Now; tMMPurchaseApplyMainDbModel.AuditStatus = 2; tMMPurchaseApplyMainDbModel.CompanyId = currentUser.CompanyID; tMMPurchaseApplyMainDbModel.TransferFlag = true; int mId = _db.Instance.Insertable(tMMPurchaseApplyMainDbModel).ExecuteReturnIdentity(); PurchaseList.ForEach((x) => { x.MainId = mId; }); await _db.Instance.Insertable(PurchaseList).ExecuteCommandAsync(); nOEntity.NO = tMMPurchaseApplyMainDbModel.PurchaseNo; nOEntity.ID = mId; } if (productionList.Count() > 0) { _db.Instance.Updateable(productionList).UpdateColumns(p => new { p.PurchaseTransNum }).ExecuteCommand(); } mainEntity.IsPurchase = false; _db.Instance.Updateable(mainEntity).UpdateColumns(p => new { p.IsPurchase }).ExecuteCommand(); _db.Instance.CommitTran(); return(ResponseUtil <NOEntity> .SuccessResult(nOEntity)); } catch (Exception ex) { _db.Instance.RollbackTran(); return(ResponseUtil <NOEntity> .FailResult(null, ex.Message)); } }