/// <summary> /// 新增T_MM_BOMMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <bool> > PostAsync(RequestPost <TMMBOMMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <bool> .FailResult(false, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //删除以前的数据 int PackageId = requestObject.PostData.PackageId; TMMBOMMainDbModel oldDBMolde = _db.Instance.Queryable <TMMBOMMainDbModel>().Where(p => p.PackageId == PackageId).First(); if (oldDBMolde != null) { _db.Instance.Deleteable <TMMBOMDetailDbModel>().Where(p => p.MainId == PackageId).ExecuteCommand(); _db.Instance.Deleteable <TMMBOMMainDbModel>(oldDBMolde).ExecuteCommand(); } //插入主表数据 var mapMainModel = _mapper.Map <TMMBOMMainDbModel>(requestObject.PostData); var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); var materCache = BasicCacheGet.GetMaterial(currentUser); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => { if (!materCache.Any(p1 => p1.MaterialName == p.MaterialName)) { throw new Exception($"{p.MaterialName}物料不存在"); } p.MainId = mainId; }); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TMMBOMDetailAddModel>, List <TMMBOMDetailDbModel> >(requestObject.PostData.ChildList); var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; //提交事务 currDb.CommitTran(); //返回执行结果 return(result ? ResponseUtil <bool> .SuccessResult(true) : ResponseUtil <bool> .FailResult(false, "新增数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <bool> .FailResult(false, ex.Message)); } }
/// <summary> /// 审核通过后统计当前出库单信息 /// </summary> /// <param name="iMainId"></param> private void OtherWhCountAsync(int iMainId, CurrentUser currentUser) { var mainEntity = _db.Instance.Queryable <TWMProductionMainDbModel>().Where(p => p.ID == iMainId).First(); //获取出库明细 var details = _db.Instance.Queryable <TWMProductionDetailDbModel>() .Where(p => p.MainId == iMainId) .ToList(); //更新出库数量 var detailList = details.GroupBy(p => new { p.MaterialId, p.WarehouseId }).Select(p => new TWMProductionCountDbModel { MaterialId = p.Key.MaterialId, WarehouseId = p.Key.WarehouseId, WhSendNumber = p.Sum(p1 => p1.ActualNum) }).ToList(); var mList = BasicCacheGet.GetMaterial(currentUser); foreach (var item in detailList) { TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.EditID = iMainId; tWMStaQuery.MaterialId = item.MaterialId; tWMStaQuery.WarehouseId = item.WarehouseId; tWMStaQuery.OperateType = OperateEnum.Product; decimal AvaiableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; if (AvaiableNum <= 0) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量可用量不足"); } if (item.WhSendNumber > AvaiableNum) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量不能大于{AvaiableNum}"); } var updateFlag = _db.Instance.Updateable(item) .SetColumns(p => p.WhSendNumber == p.WhSendNumber + item.WhSendNumber) .Where(p => p.MaterialId == item.MaterialId && p.WarehouseId == item.WarehouseId) .ExecuteCommand() > 0; if (!updateFlag) { _db.Instance.Insertable(item).ExecuteCommand(); } } }
public async Task <ResponseObject <SumQueryApiModel> > GetBom(int orderID, CurrentUser currentUser) { try { SumQueryApiModel result = new SumQueryApiModel(); List <TBMMaterialFileCacheModel> MMaterialList = BasicCacheGet.GetMaterial(currentUser); var bomList = GetProcuctBomByOrderID(orderID, currentUser); result.MainList = bomList; result.PackageId = _db.Instance.Queryable <TMMProductionOrderBOMDbModel>().Where(p => p.ProOrderId == orderID).Select(p => p.PackageId).Distinct().ToList(); return(ResponseUtil <SumQueryApiModel> .SuccessResult(result)); } catch (Exception ex) { return(ResponseUtil <SumQueryApiModel> .FailResult(null, ex.Message)); } }
private async Task <ResponseObject <List <TPSMPurchaseOrderDetailQueryModel> > > GetDetialList(int requestObject, CurrentUser currentUser) { try { //查询结果集对象 List <TPSMPurchaseOrderDetailQueryModel> queryData = null; //总记录数 RefAsync <int> totalNumber = -1; var query = _db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel, TBMMaterialFileDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TPSMPurchaseOrderMainDbModel, TBMSupplierFileDbModel>( (t, t0, t1, t2, t3, t4, t5, t6, t7) => new object[] { JoinType.Left, t.MaterialId == t0.ID, JoinType.Left, t0.MaterialTypeId == t1.ID, JoinType.Left, t0.ColorId == t2.ID, JoinType.Left, t0.BaseUnitId == t3.ID, JoinType.Left, t0.PurchaseUnitId == t4.ID, JoinType.Left, t0.WarehouseUnitId == t5.ID, JoinType.Inner, t.MainId == t6.ID, JoinType.Left, t.SupplierId == t7.ID }); //执行查询 queryData = await query .Select((t, t0, t1, t2, t3, t4, t5, t6, t7) => new TPSMPurchaseOrderDetailQueryModel { ID = t.ID, MainId = t.MainId, SupplierId = t.SupplierId, SupplierName = t7.SupplierName, MaterialId = t.MaterialId, MaterialName = t0.MaterialName, MaterialCode = t0.MaterialCode, MaterialTypeId = t0.MaterialTypeId, MaterialTypeName = t1.DicValue, ColorId = t0.ColorId, ColorName = t2.DicValue, Spec = t0.Spec, BaseUnitId = t0.BaseUnitId, BaseUnitName = t3.DicValue, PurchaseUnitId = t0.PurchaseUnitId, PurchaseUnitName = SqlFunc.IsNullOrEmpty(t4.ID) ? t3.DicValue : t4.DicValue, PurchaseRate = t0.PurchaseRate, WarehouseUnitId = t0.WarehouseUnitId, WarehouseUnitName = SqlFunc.IsNullOrEmpty(t5.ID) ? t3.DicValue : t5.DicValue, WarehouseRate = t0.WarehouseRate, UnitPrice = t.UnitPrice, PurchaseAmount = t.PurchaseAmount, PurchaseNum = t.PurchaseNum, DeliveryPeriod = t.DeliveryPeriod, TransferNum = t.TransferNum, Remark = t.Remark, ProduceRate = t0.ProduceRate, ProduceUnitId = t0.ProduceUnitId, AuditStatus = t6.AuditStatus }) .Where(t => t.MainId == requestObject) .ToListAsync(); var mainEntity = _db.Instance.Queryable <TPSMPurchaseOrderMainDbModel>().Where(p => p.ID == requestObject).First(); List <TBMMaterialFileCacheModel> mList = BasicCacheGet.GetMaterial(currentUser); //计算生产采购申请单转生产采购单数量 var prodList = await _db.Instance.Queryable <TMMPurchaseApplyDetailDbModel, TMMPurchaseApplyMainDbModel> ((t, t0) => new object[] { JoinType.Left, t.MainId == t0.ID }) .Where((t, t0) => t0.ID == SqlFunc.Subqueryable <TPSMPurchaseOrderMainDbModel>().Where(p => p.ID == requestObject).Select(p => (int)p.SourceId)) .ToListAsync(); queryData.ForEach(p => { var me = mList.Where(x => x.ID == p.MaterialId).FirstOrDefault(); if (mainEntity.SourceId > 0) { p.ProdTransNum = prodList.Where(p1 => p1.MaterialId == p.MaterialId).First().TransNum; } p.TransferWareNum = UnitChange.TranserUnit(me, UnitType.Purchase, UnitType.Warehouse, p.TransferNum); }); //返回执行结果 return(ResponseUtil <List <TPSMPurchaseOrderDetailQueryModel> > .SuccessResult(queryData, totalNumber)); } catch (Exception ex) { //返回查询异常结果 return(ResponseUtil <List <TPSMPurchaseOrderDetailQueryModel> > .FailResult(null, ex.Message)); } }
public async Task <ResponseObject <bool> > PostAsync(RequestPost <TMMBOMMainAddNewModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <bool> .FailResult(false, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //删除以前的数据 int PackageId = requestObject.PostData.PackageId; TMMBOMMainDbModel oldDBMolde = _db.Instance.Queryable <TMMBOMMainDbModel>().Where(p => p.PackageId == PackageId).First(); if (oldDBMolde != null) { _db.Instance.Deleteable <TMMBOMDetailDbModel>().Where(p => p.MainId == PackageId).ExecuteCommand(); _db.Instance.Deleteable <TMMBOMMainDbModel>(oldDBMolde).ExecuteCommand(); } //插入主表数据 var mapMainModel = _mapper.Map <TMMBOMMainDbModel>(requestObject.PostData); var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); var materCache = BasicCacheGet.GetMaterial(currentUser); #region 处理配色项目 List <string> ItemList = requestObject.PostData.ChildList.Select(p => p.ItemName).Distinct().ToList(); Dictionary <string, int> itemToId = new Dictionary <string, int>(); foreach (string name in ItemList) { var colorItem = _db.Instance.Queryable <TMMPackageColorItemDbModel>().Where(p => p.CompanyId == currentUser.CompanyID && p.ItemName == name && p.PackageId == PackageId).First(); int ItemId = 0; if (colorItem == null) { TMMPackageColorItemDbModel temp = new TMMPackageColorItemDbModel(); temp.CompanyId = currentUser.CompanyID; temp.DeleteFlag = false; temp.PackageId = mapMainModel.PackageId; temp.ItemName = name; ItemId = _db.Instance.Insertable(temp).ExecuteReturnIdentity(); } else { ItemId = colorItem.ID; if (colorItem.DeleteFlag == true) { colorItem.DeleteFlag = false; _db.Instance.Updateable(colorItem).ExecuteCommand(); } } itemToId.Add(name, ItemId); } var itemIDs = _db.Instance.Queryable <TMMPackageColorItemDbModel>().Where(p => p.CompanyId == currentUser.CompanyID && p.PackageId == PackageId && !ItemList.Contains(p.ItemName)).Select(p => p.ID).ToList(); _db.Instance.Updateable <TMMPackageColorItemDbModel>().Where(p => itemIDs.Contains(p.ID)). SetColumns(it => new TMMPackageColorItemDbModel { DeleteFlag = true }).ExecuteCommand(); _db.Instance.Deleteable <TMMColorSolutionDetailDbModel>().Where(p => SqlFunc.Subqueryable <TMMColorSolutionMainDbModel>(). Where(p1 => p1.ID == p.MainId && p1.CompanyId == currentUser.CompanyID && p1.PackageId == PackageId).Any() && itemIDs.Contains(p.ItemId)); #endregion #region 处理部位 List <string> PartList = requestObject.PostData.ChildList.Where(p => !string.IsNullOrWhiteSpace(p.PartName)).Select(p => p.PartName).Distinct().ToList(); Dictionary <string, int> partId = new Dictionary <string, int>(); var dictionaryType = _db.Instance.Queryable <TBMDictionaryTypeDbModel>().Where(p => p.CompanyId == currentUser.CompanyID && p.TypeName == "部位档案").First(); foreach (string name in PartList) { var dicItem = _db.Instance.Queryable <TBMDictionaryDbModel>().Where(p => p.CompanyId == currentUser.CompanyID && p.DicValue == name).First(); int dicID = 0; if (dicItem == null) { TBMDictionaryDbModel temp = new TBMDictionaryDbModel(); temp.CompanyId = currentUser.CompanyID; temp.DeleteFlag = false; temp.TypeId = dictionaryType.ID; temp.DicValue = name; dicID = _db.Instance.Insertable(temp).ExecuteReturnIdentity(); } else { dicID = dicItem.ID; } partId.Add(name, dicID); } #endregion List <TMMBOMDetailDbModel> deatail = new List <TMMBOMDetailDbModel>(); foreach (TMMBOMDetailAddNewModel item in requestObject.PostData.ChildList) { TMMBOMDetailDbModel tMMBOMDetailDbModel = ExpressionGenericMapper <TMMBOMDetailAddNewModel, TMMBOMDetailDbModel> .Trans(item); tMMBOMDetailDbModel.MainId = mainId; tMMBOMDetailDbModel.ItemId = itemToId[item.ItemName]; if (!string.IsNullOrWhiteSpace(item.PartName)) { tMMBOMDetailDbModel.PartId = partId[item.PartName]; } if (!materCache.Any(p1 => p1.MaterialName == item.MaterialName)) { throw new Exception($"{item.MaterialName}物料不存在"); } deatail.Add(tMMBOMDetailDbModel); } //插入从表数据 // var mapDetailModelList = _mapper.Map<List<TMMBOMDetailAddNewModel>, List<TMMBOMDetailDbModel>>(requestObject.PostData.ChildList); var result = await currDb.Insertable(deatail).ExecuteCommandAsync() > 0; //提交事务 currDb.CommitTran(); //返回执行结果 return(result ? ResponseUtil <bool> .SuccessResult(true) : ResponseUtil <bool> .FailResult(false, "新增数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <bool> .FailResult(false, ex.Message)); } }
public async Task <ResponseObject <TWMProductionMainQueryModel> > GetWholeMainData(int iMainId, CurrentUser currentUser) { try { RequestGet requestGet = new RequestGet() { IsPaging = false, QueryConditions = new List <QueryCondition>() { new QueryCondition() { Column = "Id", Condition = ConditionEnum.Equal, Content = iMainId.ToString() } } }; var allMain = await GetMainListAsync(requestGet, currentUser); var mainModel = allMain.Data.FirstOrDefault(); var materList = BasicCacheGet.GetMaterial(currentUser); var dicList = BasicCacheGet.GetDic(currentUser); var detailModels = await _db.Instance.Queryable <TWMProductionDetailDbModel, TWMProductionMainDbModel, TBMMaterialFileDbModel, TMMPickApplyMainDbModel, TMMPickApplyDetailDbModel>( (t, t0, t1, t2, t3) => new object[] { JoinType.Inner, t.MainId == t0.ID, JoinType.Inner, t.MaterialId == t1.ID, JoinType.Inner, t0.SourceId == t2.ID, JoinType.Inner, t.PickApplyDetailId == t3.ID } ).Select((t, t0, t1, t2, t3) => new TWMProductionDetailQueryModel { ID = t.ID, MainId = t.MainId, MaterialId = t.MaterialId, MaterialName = t1.MaterialName, MaterialCode = t1.MaterialCode, WarehouseId = t.WarehouseId, ActualNum = t.ActualNum, PickActualNum = t.PickActualNum, PickApplyDetailId = t.PickApplyDetailId, UnitPrice = t.UnitPrice, Amount = t.Amount, MaterialTypeId = t1.MaterialTypeId, //MaterialTypeName = t1.TypeName, ColorId = t1.ColorId, //ColorName = t2.DicValue, BaseUnitId = t1.BaseUnitId, //BaseUnitName = t3.DicValue, WarehouseUnitId = t1.WarehouseUnitId, //WarehouseUnitName = t4.DicValue, WarehouseRate = t1.WarehouseRate, ProduceRate = t1.ProduceRate, ProduceUnitId = t1.ProduceUnitId, //ProduceUnitName = t5.DicValue, Spec = t1.Spec, Remark = t.Remark, TransNum = t3.TransNum, }) .Where(t => t.MainId == iMainId) .ToListAsync(); detailModels.ForEach((x) => { var thisMaterial = materList.Where(p => p.ID == x.MaterialId).FirstOrDefault(); if (mainModel.AuditStatus != 2) { x.TransNum = x.TransNum + x.PickActualNum; } x.MaterialTypeName = thisMaterial.MaterialTypeName; x.ColorName = thisMaterial.ColorName; x.BaseUnitName = thisMaterial.BaseUnitName; x.WarehouseUnitName = thisMaterial.WarehouseUnitName; x.ProduceUnitName = thisMaterial.ProduceUnitName; TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.MaterialId = x.MaterialId; tWMStaQuery.WarehouseId = x.WarehouseId; if (mainModel.AuditStatus != 2) { tWMStaQuery.EditID = mainModel.ID; tWMStaQuery.OperateType = OperateEnum.Product; } x.AvailableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; }); mainModel.ChildList = detailModels; return(ResponseUtil <TWMProductionMainQueryModel> .SuccessResult(mainModel)); } catch (Exception ex) { return(ResponseUtil <TWMProductionMainQueryModel> .FailResult(null)); } }
/// <summary> /// 自动计算 /// </summary> /// <param name="orderID">生产订单ID</param> /// <param name="Type">1,表示有配色,2表示无配色</param> /// <param name="currentUser">当前用户</param> /// <returns>计算是否成功</returns> public async Task <ResponseObject <MrpResultModel> > AutoComputeMRP(int orderID, int Type, CurrentUser currentUser) { try { TMMProductionOrderMainDbModel mainEntity = _db.Instance.Queryable <TMMProductionOrderMainDbModel>().Where(p => p.ID == orderID && p.CompanyId == currentUser.CompanyID && p.MRPStatus == false).First(); if (mainEntity == null) { return(ResponseUtil <MrpResultModel> .FailResult(null, "生产订单不存在,或MRP已经算过了不能重复计算")); } List <TBMMaterialFileCacheModel> MMaterialList = BasicCacheGet.GetMaterial(currentUser); _db.Instance.BeginTran(); var bomResult = await _iTMMProductionOrderMainService.CreateOrderBom(mainEntity.ID, currentUser); //生成BOM清单 if (!bomResult.Result) { throw new Exception(bomResult.ErrorInfo); } //数据字典 var tBMDictionary = _db.Instance.Queryable <TBMDictionaryDbModel>().Where(p => p.CompanyId == currentUser.CompanyID).ToList().ToDictionary(p => p.ID, p => p.DicValue); //仓库 var warehouseDic = _db.Instance.Queryable <TBMWarehouseFileDbModel>().Where(t => SqlFunc.IsNull(t.DeleteFlag, false) != true && t.CompanyId == currentUser.CompanyID).ToList().ToDictionary(p => p.ID, p => new { Name = p.WarehouseName, Code = p.Code }); List <InventoryResultModel> result = new List <InventoryResultModel>(); //其他出入库 var otherCount = _db.Instance.Queryable <TWMOtherCountDbModel>(). Select(p => new InAndOutModel() { MaterialId = p.MaterialId, WarehouseId = p.WarehouseId, WhNumber = p.WhNumber, WhSendNumber = p.WhSendNumber }); //盘亏盘盈出入库 var pCount = _db.Instance.Queryable <TWMProfitDeficitCountDbModel>(). Select(p => new InAndOutModel() { MaterialId = p.MaterialId, WarehouseId = p.WarehouseId, WhNumber = p.WhNumber, WhSendNumber = p.WhSendNumber }); //销售出入库 var SaleCount = _db.Instance.Queryable <TWMSalesCountDbModel>(). Select(p => new InAndOutModel() { MaterialId = p.MaterialId, WarehouseId = p.WarehouseId, WhNumber = p.WhNumber, WhSendNumber = p.WhSendNumber }); //采购出入库 var PurchaseCount = _db.Instance.Queryable <TWMPurchaseCountDbModel>(). Select(p => new InAndOutModel() { MaterialId = p.MaterialId, WarehouseId = p.WarehouseId, WhNumber = p.WhNumber, WhSendNumber = p.WhSendNumber }); //生产出入库 var ProductCount = _db.Instance.Queryable <TWMProductionCountDbModel>(). Select(p => new InAndOutModel() { MaterialId = p.MaterialId, WarehouseId = p.WarehouseId, WhNumber = p.WhNumber, WhSendNumber = p.WhSendNumber }); //出入库数量 var allCount = _db.Instance.UnionAll(otherCount, pCount, SaleCount, PurchaseCount, ProductCount).GroupBy(p => new { p.MaterialId }). Select(p => new TradeInventoryModel() { TradeNumber = SqlFunc.AggregateSum(p.WhNumber) - SqlFunc.AggregateSum(p.WhSendNumber), MaterialId = p.MaterialId, }).AS("t100"); var materialFileQuery = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>().Where(p => p.ProOrderId == orderID); var ts = _db.Instance.Queryable(materialFileQuery, allCount, JoinType.Inner, (p1, p2) => p1.MaterialId == p2.MaterialId).Select((p1, p2) => new InventoryOut { MaterialId = p2.MaterialId, Amount = p2.TradeNumber }); //销售单 所有物料的出入库数量 var tsout1 = ts.ToList(); var tsOut = tsout1.GroupBy(p => p.MaterialId).Select(p => new InventoryOut() { MaterialId = p.Key, Amount = p.Sum(m => m.Amount) }).ToList(); #region 待出库数量 //其他待出库 var otherToOut = _db.Instance.Queryable <TWMOtherWhSendMainDbModel, TWMOtherWhSendDetailDbModel>((t1, t2) => new object[] { JoinType.Inner, t1.ID == t2.MainId }).Where((t1, t2) => t1.AuditStatus != 2 && t1.DeleteFlag == false).Select((t1, t2) => new InventoryOut { MaterialId = t2.MaterialId, Amount = t2.ActualNumber }); //盘亏出库 var deficitToOut = _db.Instance.Queryable <TWMDeficitMainDbModel, TWMDeficitDetailDbModel>((t1, t2) => new object[] { JoinType.Inner, t1.ID == t2.MainId }).Where((t1, t2) => t1.AuditStatus != 2 && t1.DeleteFlag == false).Select((t1, t2) => new InventoryOut { MaterialId = t2.MaterialId, Amount = t2.ActualNumber }); //销售出库 var saleToOut = _db.Instance.Queryable <TWMSalesMainDbModel, TWMSalesDetailDbModel>((t1, t2) => new object[] { JoinType.Inner, t1.ID == t2.MainId }).Where((t1, t2) => t1.AuditStatus != 2 && t1.DeleteFlag == false).Select((t1, t2) => new InventoryOut { MaterialId = t2.MaterialId, Amount = t2.ActualNum }); //生产待出库 var productToOut = _db.Instance.Queryable <TWMProductionMainDbModel, TWMProductionDetailDbModel>((t1, t2) => new object[] { JoinType.Inner, t1.ID == t2.MainId }).Where((t1, t2) => t1.AuditStatus != 2 && t1.DeleteFlag == false).Select((t1, t2) => new InventoryOut { MaterialId = t2.MaterialId, Amount = t2.ActualNum }); var allToOut = _db.Instance.UnionAll(otherToOut, deficitToOut, saleToOut, productToOut).AS("t101"); var materialFileQuery1 = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>().Where(p => p.ProOrderId == orderID); var tsToOut = _db.Instance.Queryable(materialFileQuery1, allToOut, JoinType.Inner, (p1, p2) => p1.ID == p2.MaterialId).Select((p1, p2) => new InventoryOut { MaterialId = p2.MaterialId, Amount = p2.Amount }); //销售单所有物料的待出库数量 var tsToOutSum = tsToOut.ToList().GroupBy(p => p.MaterialId).Select(p => new InventoryOut() { MaterialId = p.Key, Amount = p.Sum(m => m.Amount) }).ToList(); #endregion //期初 List <InventoryOut> prime = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel, TWMPrimeCountDbModel>((t, t1) => new object[] { JoinType.Inner, t.MaterialId == t1.MaterialId }).Select((t, t1) => t1).Distinct().ToList().GroupBy(p => p.MaterialId).Select(p => new InventoryOut() { MaterialId = p.Key, Amount = p.Sum(m => m.PrimeNum) }).ToList();; var Allmaterial = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>().Where(p => p.ProOrderId == orderID).ToList(); Dictionary <int, string> colorSolution = _db.Instance.Queryable <TMMColorSolutionMainDbModel, TBMPackageDbModel>((t1, t2) => new object[] { JoinType.Inner, t1.PackageId == t2.ID }). Where((t1, t2) => t2.CompanyId == currentUser.CompanyID).ToList().ToDictionary(p => p.ID, p => p.SolutionCode); List <int> colorIDS = colorSolution.Keys.ToList(); //生产订单的所有物料 List <TMMProductionOrderBOMSumDbModel> sumList = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>().Where(p => p.ProOrderId == orderID).ToList(); foreach (var item in sumList) { decimal primeAmount = 0; //期初数量 decimal TradeNumber = 0; //出入库数量 decimal toOutAmount = 0; //待出数量 TBMMaterialFileCacheModel materialFile = MMaterialList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); if (materialFile == null) { throw new Exception($"物料ID:{item.MaterialId},不存在"); } InventoryOut primeEntity = prime.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); if (primeEntity != null) { primeAmount = primeEntity.Amount; } InventoryOut tradeEntity = tsOut.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); if (tradeEntity != null) { TradeNumber = tradeEntity.Amount; } InventoryOut toOutEntity = tsToOutSum.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); if (toOutEntity != null) { toOutAmount = toOutEntity.Amount; } decimal avaibleAmountUnit = primeAmount + TradeNumber - toOutAmount; //仓库数量 decimal avaibleAmount = UnitChange.TranserUnit(materialFile, UnitType.Warehouse, UnitType.Produce, avaibleAmountUnit); //生产数量 if (avaibleAmount >= item.TotalValue) { item.PurchaseNum = 0; item.PickNum = item.TotalValue; } else { if (avaibleAmount < 0) { avaibleAmount = 0; } item.PurchaseNum = item.TotalValue - avaibleAmount; item.PickNum = avaibleAmount; } item.PurchaseTransNum = 0; item.PickTransNum = 0; } if (sumList.Count() > 0) { _db.Instance.Updateable(sumList).ExecuteCommand(); } mainEntity.MRPStatus = true; mainEntity.MRPTime = DateTime.Now; _db.Instance.Updateable(mainEntity).UpdateColumns(p => new { p.MRPStatus, p.MRPTime }).ExecuteCommand(); _db.Instance.CommitTran(); MrpResultModel bomList; if (Type == 1) { bomList = GetProcuctBomByOrderID(orderID, currentUser); } else { bomList = GetProcuctBomByOrderID(orderID, currentUser); } return(ResponseUtil <MrpResultModel> .SuccessResult(bomList)); } catch (Exception ex) { _db.Instance.RollbackTran(); return(ResponseUtil <MrpResultModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 修改T_WM_ProductionWhMain数据 /// </summary> /// <param name="requestObject">Put请求参数</param> /// <returns>返回响应结果对象,包括响应代码,修改操作结果</returns> public async Task <ResponseObject <TWMProductionWhMainQueryModel> > PutAsync(RequestPut <TWMProductionWhMainEditModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null) { return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null, "PostData不能为null")); } if (requestObject.PostData.ChildList == null || requestObject.PostData.ChildList.Count < 1) { return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null, "PostData.ChildList至少包含一条数据")); } //开启事务 currDb.BeginTran(); //修改主表信息 var mainModel = _mapper.Map <TWMProductionWhMainDbModel>(requestObject.PostData); //修改主表信息 mainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNum); mainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); var mainFlag = await currDb.Updateable(mainModel).UpdateColumns(p => new { p.WarehousingDate, p.Number, p.Amount, p.WhAdminId, p.ReceiptId }).ExecuteCommandAsync() > 0; /* * 修改明细逻辑 * 1.根据主单ID查询现有明细数据 * 2.PostData.ChildList中明细ID <= 0的新增 * 3.PostData.ChildList中明细ID > 0的修改 * 4.删除不在PostData.CihldList中的数据 */ var mainEntity = _db.Instance.Queryable <TWMProductionWhMainDbModel>().Where(P => P.ID == requestObject.PostData.ID).First(); var detailFlag = true; var detailModels = _mapper.Map <List <TWMProductionWhDetailEditModel>, List <TWMProductionWhDetailDbModel> >(requestObject.PostData.ChildList); var listItem = _db.Instance.Queryable <TMMWhApplyDetailDbModel>().Where(t => t.MainId == mainEntity.SourceId).ToList(); var materList = BasicCacheGet.GetMaterial(currentUser); List <TMMWhApplyDetailDbModel> toEdit = new List <TMMWhApplyDetailDbModel>(); foreach (var item in detailModels) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); var procudeMaterial = listItem.Where(p => p.ID == item.ProOrderDetailId).FirstOrDefault();//生产单明细 if (!detailFlag) { break; } item.MainId = mainModel.ID; if (item.ID <= 0) //新增 { decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Produce, item.ActualNum); //实际转出数量(生产) decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Produce, UnitType.Warehouse, procudeMaterial.TransNum); //可转的数量(仓库单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 入库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.ProActualNum = procudeMaterial.TransNum; procudeMaterial.TransNum = 0; } else { if (proNum > procudeMaterial.TransNum) { item.ProActualNum = procudeMaterial.TransNum; procudeMaterial.TransNum = 0; } else { item.ProActualNum = proNum; procudeMaterial.TransNum = procudeMaterial.TransNum - proNum; } } toEdit.Add(procudeMaterial); } else //修改 { var dModel = _db.Instance.Queryable <TWMProductionWhDetailDbModel>().Where(p => p.ID == item.ID).First(); if (dModel != null) { if (dModel.ActualNum != item.ActualNum) { decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Produce, item.ActualNum); //实际转出数量(生产单位) decimal TransNum = procudeMaterial.TransNum + dModel.ProActualNum; //可转数量(生产单位) decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Produce, UnitType.Warehouse, TransNum); //可转的数量(仓库单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 入库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.ProActualNum = TransNum; procudeMaterial.TransNum = 0; } else { if (proNum > TransNum) { item.ProActualNum = TransNum; procudeMaterial.TransNum = 0; } else { item.ProActualNum = proNum; procudeMaterial.TransNum = TransNum - proNum; } } toEdit.Add(procudeMaterial); } else { item.ProActualNum = dModel.ProActualNum; } } } //新增或修改明细数据 detailFlag = item.ID <= 0 ? await currDb.Insertable(item).ExecuteCommandIdentityIntoEntityAsync() : await currDb.Updateable(item).ExecuteCommandAsync() > 0; } //删除明细数据 if (detailFlag) { var detailIds = detailModels.Select(p => p.ID).ToList(); var whDetailList = currDb.Queryable <TWMProductionWhDetailDbModel>().Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID).ToList(); foreach (var item in whDetailList) { var dModel = _db.Instance.Queryable <TWMProductionWhDetailDbModel>().Where(p => p.ID == item.ID).First(); var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); var procudeMaterial = listItem.Where(p => p.ID == item.ProOrderDetailId).FirstOrDefault();//生产单明细 procudeMaterial.TransNum = procudeMaterial.TransNum + dModel.ProActualNum; toEdit.Add(procudeMaterial); } detailFlag = currDb.Deleteable <TWMProductionWhDetailDbModel>() .Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID) .ExecuteCommand() >= 0; } if (toEdit != null && toEdit.Count() > 0) { _db.Instance.Updateable <TMMWhApplyDetailDbModel>(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TMMWhApplyDetailDbModel>().Any(p => p.TransNum > 0 && p.MainId == mainEntity.SourceId)) { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == false).ExecuteCommand(); } else { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == true).ExecuteCommand(); } //提交事务 currDb.CommitTran(); var res = await GetWholeMainData(mainModel.ID, currentUser); //返回执行结果 return(ResponseUtil <TWMProductionWhMainQueryModel> .SuccessResult(res.Data)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null, ex.Message)); } }
public async Task <ResponseObject <TWMProductionWhMainQueryModel> > GetWholeMainData(int iMainId, CurrentUser currentUser) { try { RequestGet requestGet = new RequestGet() { IsPaging = false, QueryConditions = new List <QueryCondition>() { new QueryCondition() { Column = "Id", Condition = ConditionEnum.Equal, Content = iMainId.ToString() } } }; var allMain = await GetMainListAsync(requestGet, currentUser); var mainModel = allMain.Data.FirstOrDefault(); var materList = BasicCacheGet.GetMaterial(currentUser); var dicList = BasicCacheGet.GetDic(currentUser); var detailModels = await _db.Instance.Queryable <TWMProductionWhDetailDbModel, TWMProductionWhMainDbModel, TBMMaterialFileDbModel, TMMWhApplyMainDbModel, TMMWhApplyDetailDbModel, TMMProductionOrderDetailDbModel, TMMColorSolutionMainDbModel, TBMPackageDbModel, TBMDictionaryDbModel, TSMUserAccountDbModel, TBMDictionaryDbModel>( (t, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) => new object[] { JoinType.Inner, t.MainId == t0.ID, JoinType.Inner, t.MaterialId == t1.ID, JoinType.Inner, t0.SourceId == t2.ID, JoinType.Inner, t.ProOrderDetailId == t3.ID, JoinType.Inner, t3.ProOrderDetailId == t4.ID, JoinType.Left, t4.ColorSolutionId == t5.ID, JoinType.Left, t4.PackageId == t6.ID, JoinType.Left, t.WorkshopId == t7.ID, JoinType.Left, t4.PrincipalId == t8.ID, JoinType.Left, t.SiteId == t9.ID } ).Select((t, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) => new TWMProductionWhDetailQueryModel { ID = t.ID, MainId = t.MainId, MaterialId = t.MaterialId, MaterialName = t1.MaterialName, MaterialCode = t1.MaterialCode, WarehouseId = t.WarehouseId, ActualNum = t.ActualNum, ProActualNum = t.ProActualNum, UnitPrice = t.UnitPrice, Amount = t.Amount, MaterialTypeId = t1.MaterialTypeId, //MaterialTypeName = t1.TypeName, ColorId = t1.ColorId, //ColorName = t2.DicValue, BaseUnitId = t1.BaseUnitId, //BaseUnitName = t3.DicValue, WarehouseUnitId = t1.WarehouseUnitId, //WarehouseUnitName = t4.DicValue, WarehouseRate = t1.WarehouseRate, ProduceRate = t1.ProduceRate, ProduceUnitId = t1.ProduceUnitId, //ProduceUnitName = t5.DicValue, Spec = t1.Spec, Remark = t.Remark, SourceCode = t2.WhApplyNo, TransNum = t3.TransNum, PackageId = t4.PackageId, ColorSolutionId = t4.ColorSolutionId, ColorSolutionName = t5.SolutionCode, PackageCode = t6.DicCode, PackageName = t6.DicValue, WorkshopName = t7.DicValue, PrincipalName = t8.AccountName, ProOrderDetailId = t.ProOrderDetailId, SiteName = t9.DicValue }) .Where(t => t.MainId == iMainId) .ToListAsync(); detailModels.ForEach((x) => { var thisMaterial = materList.Where(p => p.ID == x.MaterialId).FirstOrDefault(); if (mainModel.AuditStatus != 2) { x.TransNum = x.TransNum + x.ProActualNum; } x.MaterialTypeName = thisMaterial.MaterialTypeName; x.ColorName = thisMaterial.ColorName; x.BaseUnitName = thisMaterial.BaseUnitName; x.WarehouseUnitName = thisMaterial.WarehouseUnitName; x.ProduceUnitName = thisMaterial.ProduceUnitName; }); mainModel.ChildList = detailModels; return(ResponseUtil <TWMProductionWhMainQueryModel> .SuccessResult(mainModel)); } catch (Exception ex) { return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null)); } }
/// <summary> /// 修改T_WM_SalesMain数据 /// </summary> /// <param name="requestObject">Put请求参数</param> /// <returns>返回响应结果对象,包括响应代码,修改操作结果</returns> public async Task <ResponseObject <TWMSalesMainQueryModel> > PutAsync(RequestPut <TWMSalesMainEditModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //#if DEBUG //#else // return null; //#endif if (requestObject.PostData == null) { return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null, "PostData不能为null")); } if (requestObject.PostData.ChildList == null || requestObject.PostData.ChildList.Count < 1) { return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null, "PostData.ChildList至少包含一条数据")); } //开启事务 currDb.BeginTran(); //修改主表信息 var mainModel = _mapper.Map <TWMSalesMainDbModel>(requestObject.PostData); mainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNum); mainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); var mainFlag = await currDb.Updateable(mainModel).UpdateColumns(p => new { p.WhSendDate, p.Number, p.Amount, p.SendId, p.WhAdminId, p.ReceiptAddress }).ExecuteCommandAsync() > 0; /* * 修改明细逻辑 * 1.根据主单ID查询现有明细数据 * 2.PostData.ChildList中明细ID <= 0的新增 * 3.PostData.ChildList中明细ID > 0的修改 * 4.删除不在PostData.CihldList中的数据 */ var detailFlag = true; var detailModels = _mapper.Map <List <TWMSalesDetailEditModel>, List <TWMSalesDetailDbModel> >(requestObject.PostData.ChildList); var mainEntity = _db.Instance.Queryable <TWMSalesMainDbModel>().Where(p => p.ID == mainModel.ID).First(); var listItem = _db.Instance.Queryable <TSSMSalesOrderDetailDbModel>().Where(t => t.MainId == mainEntity.SourceId).ToList(); var materList = BasicCacheGet.GetMaterial(currentUser); List <TSSMSalesOrderDetailDbModel> toEdit = new List <TSSMSalesOrderDetailDbModel>(); foreach (var item in detailModels) { if (!detailFlag) { break; } item.MainId = mainModel.ID; var procudeMaterial = listItem.Where(p => p.ID == item.SalesOrderDetailId).FirstOrDefault();//销售合同明细 var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); if (!detailFlag) { break; } item.MainId = mainModel.ID; if (item.ID <= 0) //新增 { decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Sales, UnitType.Warehouse, procudeMaterial.TransferNum);//可转的数量(仓库单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 出库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.SalesOrderActualNum = procudeMaterial.TransferNum; procudeMaterial.TransferNum = 0; } else { if (proNum > procudeMaterial.TransferNum) { item.SalesOrderActualNum = procudeMaterial.TransferNum; procudeMaterial.TransferNum = 0; } else { item.SalesOrderActualNum = proNum; procudeMaterial.TransferNum = procudeMaterial.TransferNum - proNum; } } toEdit.Add(procudeMaterial); //decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); //var m = listItem.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); //if (m != null) //{ // if (m.TransferNum < proNum) // { // throw new Exception($"物料代码{me.MaterialCode} 出库数量不能大于{proNum }"); // } // m.TransferNum = m.TransferNum - proNum; // toEdit.Add(m); //} } else //修改 { var dModel = _db.Instance.Queryable <TWMSalesDetailDbModel>().Where(p => p.ID == item.ID).First(); if (dModel != null) { if (dModel.ActualNum != item.ActualNum) { #region ////原出库数量(销售单位) //decimal oldActualNumPro = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, dModel.ActualNum); //decimal TransNum = procudeMaterial.TransferNum + item.SalesOrderActualNum; //可转数量(生产单位) ////现出库数量(销售单位) //decimal nowActualNumPro = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); //if (item.ActualNum > nowActualNumPro) //{ // throw new Exception($"物料代码{me.MaterialCode} 入库数量不能大于{nowActualNumPro }"); //} //else if (item.ActualNum == nowActualNumPro) //{ // item.SalesOrderActualNum = TransNum; // procudeMaterial.TransferNum = 0; //} //else //{ // if (oldActualNumPro > TransNum) // { // item.SalesOrderActualNum = TransNum; // procudeMaterial.TransferNum = 0; // } // else // { // item.SalesOrderActualNum = oldActualNumPro; // procudeMaterial.TransferNum = TransNum - oldActualNumPro; // } //} #endregion decimal saleNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); //实际转出数量(销售单位) decimal TransNum = procudeMaterial.TransferNum + dModel.SalesOrderActualNum; //可转数量(生产单位) decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Sales, UnitType.Warehouse, TransNum); //可转的数量(仓库单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 入库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.SalesOrderActualNum = TransNum; procudeMaterial.TransferNum = 0; } else { if (saleNum > TransNum) { item.SalesOrderActualNum = TransNum; procudeMaterial.TransferNum = 0; } else { item.SalesOrderActualNum = saleNum; procudeMaterial.TransferNum = TransNum - saleNum; } } toEdit.Add(procudeMaterial); //var m = listItem.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); //if (m != null) //{ // if (m.TransferNum + oldActualNumPro < nowActualNumPro) // { // throw new Exception($"物料代码{me.MaterialCode} 出库数量不能大于{m.TransferNum + oldActualNumPro }"); // } // m.TransferNum = m.TransferNum + oldActualNumPro - nowActualNumPro; // toEdit.Add(m); //} } else { item.SalesOrderActualNum = dModel.SalesOrderActualNum; } } } //新增或修改明细数据 detailFlag = item.ID <= 0 ? await currDb.Insertable(item).ExecuteCommandIdentityIntoEntityAsync() : await currDb.Updateable(item).ExecuteCommandAsync() > 0; } //删除明细数据 if (detailFlag) { var detailIds = detailModels.Select(p => p.ID).ToList(); var whDetailList = currDb.Queryable <TWMSalesDetailDbModel>().Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID).ToList(); foreach (var item in whDetailList) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); //decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); var m = listItem.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); if (m != null) { m.TransferNum = m.TransferNum + item.SalesOrderActualNum; toEdit.Add(m); } } detailFlag = currDb.Deleteable <TWMSalesDetailDbModel>() .Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID) .ExecuteCommand() >= 0; } #region 检查出库是否超过可用量 int?iMainId = mainModel.ID; var mList = BasicCacheGet.GetMaterial(currentUser); var details = requestObject.PostData.ChildList.GroupBy(p => new { p.MaterialId, p.WarehouseId }).Select(p => new TWMOtherCountDbModel { MaterialId = p.Key.MaterialId, WarehouseId = p.Key.WarehouseId, WhSendNumber = p.Sum(p1 => p1.ActualNum) }).ToList(); foreach (var item in details) { TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.EditID = iMainId; tWMStaQuery.MaterialId = item.MaterialId; tWMStaQuery.WarehouseId = item.WarehouseId; decimal AvaiableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; if (AvaiableNum < 0) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料可用量不足"); } if (item.WhSendNumber > AvaiableNum) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量不能大于{AvaiableNum}"); } } #endregion if (toEdit.Count() > 0) { _db.Instance.Updateable(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TSSMSalesOrderDetailDbModel>().Any(p => p.TransferNum > 0 && p.MainId == mainEntity.SourceId)) { _db.Instance.Updateable <TSSMSalesOrderMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferStatus == false).ExecuteCommand(); } else { _db.Instance.Updateable <TSSMSalesOrderMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferStatus == true).ExecuteCommand(); } //提交事务 currDb.CommitTran(); var resultTemp = await GetWholeMainData(requestObject.PostData.ID, currentUser); return(ResponseUtil <TWMSalesMainQueryModel> .SuccessResult(resultTemp.Data)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 新增T_WM_SalesMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <TWMSalesMainQueryModel> > PostAsync(RequestPost <TWMSalesMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //插入主表数据 var mapMainModel = _mapper.Map <TWMSalesMainDbModel>(requestObject.PostData); mapMainModel.CompanyId = currentUser.CompanyID; mapMainModel.OperatorId = currentUser.UserID; mapMainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNum); mapMainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); mapMainModel.AuditStatus = 0; var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => p.MainId = mainId); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TWMSalesDetailAddModel>, List <TWMSalesDetailDbModel> >(requestObject.PostData.ChildList); #region 检查出库是否超过可用量 int?iMainId = mainId; var materList = BasicCacheGet.GetMaterial(currentUser); var details = requestObject.PostData.ChildList.GroupBy(p => new { p.MaterialId, p.WarehouseId }).Select(p => new TWMOtherCountDbModel { MaterialId = p.Key.MaterialId, WarehouseId = p.Key.WarehouseId, WhSendNumber = p.Sum(p1 => p1.ActualNum) }).ToList(); foreach (var item in details) { TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.EditID = iMainId; tWMStaQuery.MaterialId = item.MaterialId; tWMStaQuery.WarehouseId = item.WarehouseId; decimal AvaiableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; if (AvaiableNum < 0) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料可用量不足"); } if (item.WhSendNumber > AvaiableNum) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量不能大于{AvaiableNum}"); } } #endregion #region 处理销售单的可转单数量 var listItem = _db.Instance.Queryable <TSSMSalesOrderDetailDbModel>().Where(t => t.MainId == mapMainModel.SourceId).ToList();//来源单据 List <TSSMSalesOrderDetailDbModel> toEdit = new List <TSSMSalesOrderDetailDbModel>(); foreach (var item in mapDetailModelList) { var procudeMaterial = listItem.Where(p => p.ID == item.SalesOrderDetailId).FirstOrDefault(); //销售合同明细 var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); //物料 decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Sales, item.ActualNum); decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Sales, UnitType.Warehouse, procudeMaterial.TransferNum);//可转的数量(仓库单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 出库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.SalesOrderActualNum = procudeMaterial.TransferNum; procudeMaterial.TransferNum = 0; } else { if (proNum > procudeMaterial.TransferNum) { item.SalesOrderActualNum = procudeMaterial.TransferNum; procudeMaterial.TransferNum = 0; } else { item.SalesOrderActualNum = proNum; procudeMaterial.TransferNum = procudeMaterial.TransferNum - proNum; } } toEdit.Add(procudeMaterial); //var m = listItem.Where(p => p.MaterialId == item.MaterialId).FirstOrDefault(); //if (m != null) //{ // if (m.TransferNum < proNum) // { // throw new Exception($"物料代码{me.MaterialCode} 出库数量不能大于{proNum }"); // } // m.TransferNum = m.TransferNum - proNum; // toEdit.Add(m); //} } if (toEdit.Count() > 0) { _db.Instance.Updateable(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TSSMSalesOrderDetailDbModel>().Any(p => p.TransferNum > 0 && p.MainId == mapMainModel.SourceId)) { _db.Instance.Updateable <TSSMSalesOrderMainDbModel>().Where(p => p.ID == mapMainModel.SourceId).SetColumns(p => p.TransferStatus == false).ExecuteCommand(); } #endregion var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; //提交事务 currDb.CommitTran(); //返回执行结果 var resultTemp = await GetWholeMainData(mainId, currentUser); return(ResponseUtil <TWMSalesMainQueryModel> .SuccessResult(resultTemp.Data)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null, ex.Message)); } }
public async Task <ResponseObject <TWMSalesMainQueryModel> > GetWholeMainData(int iMainId, CurrentUser currentUser) { try { RequestGet requestGet = new RequestGet() { IsPaging = false, QueryConditions = new List <QueryCondition>() { new QueryCondition() { Column = "Id", Condition = ConditionEnum.Equal, Content = iMainId.ToString() } } }; var allMain = await GetMainListAsync(requestGet, currentUser); var mainModel = allMain.Data.FirstOrDefault(); var detailModelsSql = _db.Instance.Queryable <TWMSalesDetailDbModel, TBMMaterialFileDbModel, TBMDictionaryTypeDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TWMSalesMainDbModel, TSSMSalesOrderDetailDbModel , TBMPackageDbModel, TMMColorSolutionMainDbModel>( (t, t0, t1, t2, t3, t4, t5, t6, t7, t8) => new object[] { JoinType.Left, t.MaterialId == t0.ID, JoinType.Left, t0.MaterialTypeId == t1.ID, JoinType.Left, t0.ColorId == t2.ID, JoinType.Left, t0.BaseUnitId == t3.ID, JoinType.Left, t0.WarehouseUnitId == t4.ID, JoinType.Inner, t.MainId == t5.ID, JoinType.Inner, t.SalesOrderDetailId == t6.ID, JoinType.Left, t0.PackageID == t7.ID, JoinType.Left, t6.ColorSolutionId == t8.ID }) .Select((t, t0, t1, t2, t3, t4, t5, t6, t7, t8) => new TWMSalesDetailQueryModel { ID = t.ID, MainId = t.MainId, MaterialId = t.MaterialId, MaterialName = t0.MaterialName, MaterialCode = t0.MaterialCode, WarehouseId = t.WarehouseId, ActualNum = t.ActualNum, SalesOrderActualNum = t.SalesOrderActualNum, SalesOrderDetailId = t.SalesOrderDetailId, UnitPrice = t.UnitPrice, Amount = t.Amount, MaterialTypeId = t0.MaterialTypeId, MaterialTypeName = t1.TypeName, ColorId = t0.ColorId, ColorName = t2.DicValue, BaseUnitId = t0.BaseUnitId, BaseUnitName = t3.DicValue, WarehouseUnitId = t0.WarehouseUnitId, WarehouseUnitName = SqlFunc.IsNullOrEmpty(t4.ID) ? t3.DicValue : t4.DicValue, SalesUnitId = t0.SalesUnitId, SalesRate = t0.SalesRate, WarehouseRate = t0.WarehouseRate, Spec = t0.Spec, Remark = t.Remark, SalesNum = t6.SalesNum, ShouldSaleNum = t6.TransferNum, PackageCode = t7.DicCode, PackageName = t7.DicValue, ColorSolutionName = t8.SolutionCode }) .Where(t => t.MainId == iMainId).OrderBy(t => t.ID); var detailModels = await detailModelsSql.ToListAsync(); //物料 List <TBMMaterialFileCacheModel> mList = BasicCacheGet.GetMaterial(currentUser); detailModels.ForEach(p => { TBMMaterialFileCacheModel me = mList.Where(x => x.ID == p.MaterialId).FirstOrDefault(); if (me == null) { throw new Exception($"物料{p.MaterialId},不存在"); } TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.MaterialId = p.MaterialId; tWMStaQuery.WarehouseId = p.WarehouseId; if (mainModel.AuditStatus != 2) { //p.WaitNum = p.WaitNum - p.ActualNum; tWMStaQuery.EditID = mainModel.ID; tWMStaQuery.OperateType = OperateEnum.Sale; // p.ShouldNum = p.SalesOrderActualNum + p.ActualNum; p.ShouldSaleNum = p.ShouldSaleNum + p.SalesOrderActualNum; } p.SalesNumOnInventory = GetInveroryFromSaleNum(me, p.SalesNum); p.AvailableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; }); mainModel.ChildList = detailModels; return(ResponseUtil <TWMSalesMainQueryModel> .SuccessResult(mainModel)); } catch (Exception ex) { return(ResponseUtil <TWMSalesMainQueryModel> .FailResult(null)); } }
public async Task <TMMPickApplyMainQueryModel> GetTopMainData(int iMainId, CurrentUser currentUser) { try { var mainModel = await _db.Instance.Queryable <TMMPickApplyMainDbModel, TSMUserAccountDbModel, TSMUserAccountDbModel>( (t, t1, t2) => new object[] { JoinType.Left, t.OperatorId == t1.ID, JoinType.Left, t.AuditId == t2.ID }).Where((t, t1, t2) => t.DeleteFlag == false && t.CompanyId == currentUser.CompanyID) .Select((t, t1, t2) => new TMMPickApplyMainQueryModel { ID = t.ID, SourceId = t.SourceId, PickNo = t.PickNo, ApplyDate = t.ApplyDate, OperatorId = t.OperatorId, OperatorName = t1.AccountName, OperatorTime = t.OperatorTime, AuditId = t.AuditId, AuditName = t2.AccountName, AuditStatus = t.AuditStatus, AuditTime = t.AuditTime, CompanyId = t.CompanyId, DeleteFlag = t.DeleteFlag, TransferFlag = t.TransferFlag, IsShowEdit = (t.AuditStatus != 2 && t.OperatorId == currentUser.UserID) ? true : false }) .Where(t => t.ID == iMainId) .FirstAsync(); var detailModels = await _db.Instance.Queryable <TMMPickApplyDetailDbModel, TBMMaterialFileDbModel, TBMDictionaryTypeDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel, TBMDictionaryDbModel>( (t, t0, t1, t2, t3, t4) => new object[] { JoinType.Left, t.MaterialId == t0.ID, JoinType.Left, t0.MaterialTypeId == t1.ID, JoinType.Left, t0.ColorId == t2.ID, JoinType.Left, t0.BaseUnitId == t3.ID, JoinType.Left, t0.WarehouseUnitId == t4.ID }).Where((t, t0, t1, t2, t3, t4) => t.MainId == iMainId && t.TransNum > 0) .Select((t, t0, t1, t2, t3, t4) => new TMMPickApplyDetailQueryModel { MaterialId = t.MaterialId, TransNum = t.TransNum, MaterialName = t0.MaterialName, MaterialCode = t0.MaterialCode, MaterialTypeId = t0.MaterialTypeId, //MaterialTypeName = t1.TypeName, ColorId = t0.ColorId, //ColorName = t2.DicValue, BaseUnitId = t0.BaseUnitId, //BaseUnitName = t3.DicValue, WarehouseUnitId = t0.WarehouseUnitId, //WarehouseUnitName = t4.DicValue, WarehouseRate = t0.WarehouseRate, ProduceRate = t0.ProduceRate, ProduceUnitId = t0.ProduceUnitId, Spec = t0.Spec, }) .ToListAsync(); var materList = BasicCacheGet.GetMaterial(currentUser); var dicList = BasicCacheGet.GetDic(currentUser); detailModels.ForEach((x) => { var thisMaterial = materList.Where(p => p.ID == x.MaterialId).FirstOrDefault(); x.MaterialTypeName = thisMaterial.MaterialTypeName; x.ColorName = thisMaterial.ColorName; x.BaseUnitName = thisMaterial.BaseUnitName; x.WarehouseUnitName = thisMaterial.WarehouseUnitName; x.ProduceUnitName = thisMaterial.ProduceUnitName; }); mainModel.ChildList = detailModels; return(mainModel); } catch (Exception ex) { return(null); } }
/// <summary> /// 新增T_PSM_PurchaseOrderMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <param name="currentUser"></param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <TPSMPurchaseOrderMainQueryModel> > PostAsync(RequestPost <TPSMPurchaseOrderMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //插入主表数据 var mapMainModel = _mapper.Map <TPSMPurchaseOrderMainDbModel>(requestObject.PostData); mapMainModel.OperatorId = currentUser.UserID; mapMainModel.CompanyId = currentUser.CompanyID; mapMainModel.PurchaseNum = requestObject.PostData.ChildList.Sum(p => p.PurchaseNum); mapMainModel.PurchaseAmount = requestObject.PostData.ChildList.Sum(p => p.PurchaseAmount); var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => p.MainId = mainId); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TPSMPurchaseOrderDetailAddModel>, List <TPSMPurchaseOrderDetailDbModel> >(requestObject.PostData.ChildList); var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; //如果是生产采购申请单转采购单,需要更新转单及数量信息 if (mapMainModel.SourceId > 0) { var transList = await GetTransDetailsAsync((int)mapMainModel.SourceId, mapDetailModelList, currentUser); foreach (var item in transList) { await _db.Instance.Updateable <TMMPurchaseApplyDetailDbModel>() .SetColumns(p => p.TransNum == p.TransNum - item.TransNum) .Where(p => p.MainId == item.MainId && p.MaterialId == item.MaterialId) .ExecuteCommandAsync(); } FalseTMMPurchaseApplyMain(mapMainModel.SourceId.Value); } else { var detailModelLists = _db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel>().Where(p => p.MainId == requestObject.PostData.ID).ToList(); var groupEntity = from p in detailModelLists group p by new { p.MaterialId } into g select new { g.Key, MaxPrice = g.Max(p => p.UnitPrice), MinPrice = g.Min(p => p.UnitPrice) }; var differentEntity = groupEntity.Where(p => p.MaxPrice != p.MinPrice).ToList(); if (differentEntity.Count() > 0) { var meList = BasicCacheGet.GetMaterial(currentUser); string error = string.Join(",", differentEntity.Select(p => meList.Where(p1 => p1.ID == p.Key.MaterialId).FirstOrDefault()?.MaterialName)); throw new Exception($"{error}商品的单价不一致,请保证相同商品价格一致性"); } } //提交事务 currDb.CommitTran(); TPSMPurchaseOrderMainQueryModel returnObject = null; if (result) { var tempreturnObject = await GetWholeMainData(mainId, currentUser); returnObject = tempreturnObject.Data; } //返回执行结果 return(result ? ResponseUtil <TPSMPurchaseOrderMainQueryModel> .SuccessResult(returnObject) : ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, "新增数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 新增T_WM_OtherWhSendMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <param name="currentUser"></param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <TWMOtherWhSendMainQueryModel> > PostAsync(RequestPost <TWMOtherWhSendMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, "PostData不能为null")); } //开启事务 currDb.BeginTran(); #region 检查出库是否超过可用量 int?iMainId = null; var mList = BasicCacheGet.GetMaterial(currentUser); var details = requestObject.PostData.ChildList.GroupBy(p => new { p.MaterialId, p.WarehouseId }).Select(p => new TWMOtherCountDbModel { MaterialId = p.Key.MaterialId, WarehouseId = p.Key.WarehouseId, WhSendNumber = p.Sum(p1 => p1.ActualNumber) }).ToList(); foreach (var item in details) { TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.EditID = iMainId; tWMStaQuery.MaterialId = item.MaterialId; tWMStaQuery.WarehouseId = item.WarehouseId; decimal AvaiableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; if (AvaiableNum < 0) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料可用量不足"); } if (item.WhSendNumber > AvaiableNum) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量不能大于{AvaiableNum}"); } } #endregion //插入主表数据 var mapMainModel = _mapper.Map <TWMOtherWhSendMainDbModel>(requestObject.PostData); mapMainModel.CompanyId = currentUser.CompanyID; mapMainModel.OperatorId = currentUser.UserID; mapMainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNumber); mapMainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => p.MainId = mainId); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TWMOtherWhSendDetailAddModel>, List <TWMOtherWhSendDetailDbModel> >(requestObject.PostData.ChildList); var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; var returnData = await GetMainData(mainId); //提交事务 currDb.CommitTran(); //返回执行结果 return(result ? ResponseUtil <TWMOtherWhSendMainQueryModel> .SuccessResult(returnData) : ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, "新增数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 修改T_PSM_PurchaseOrderMain数据 /// </summary> /// <param name="requestObject">Put请求参数</param> /// <param name="currentUser"></param> /// <returns>返回响应结果对象,包括响应代码,修改操作结果</returns> public async Task <ResponseObject <TPSMPurchaseOrderMainQueryModel> > PutAsync(RequestPut <TPSMPurchaseOrderMainEditModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null) { return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, "PostData不能为null")); } if (requestObject.PostData.ChildList == null || requestObject.PostData.ChildList.Count < 1) { return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, "PostData.ChildList至少包含一条数据")); } //开启事务 currDb.BeginTran(); //修改主表信息 var mainModel = _mapper.Map <TPSMPurchaseOrderMainDbModel>(requestObject.PostData); mainModel.PurchaseNum = requestObject.PostData.ChildList.Sum(p => p.PurchaseNum); mainModel.PurchaseAmount = requestObject.PostData.ChildList.Sum(p => p.PurchaseAmount); var mainFlag = await currDb.Updateable(mainModel) .UpdateColumns(p => new { p.PurchaseNum, p.PurchaseAmount, p.OrderDate, p.OrderTypeId, p.SettlementTypeId, p.ContactName, p.ContactNumber, p.BuyerId, p.Currency }) .Where(p => (SqlFunc.IsNullOrEmpty(p.AuditStatus) || p.AuditStatus != 2) && p.ID == mainModel.ID) .ExecuteCommandAsync() > 0; //查询编辑前明细数据 var beforEditDetailData = await _db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel>() .Where(p => p.MainId == mainModel.ID) .ToListAsync(); /* * 修改明细逻辑 * 1.根据主单ID查询现有明细数据 * 2.PostData.ChildList中明细ID <= 0的新增 * 3.PostData.ChildList中明细ID > 0的修改 * 4.删除不在PostData.CihldList中的数据 */ var detailFlag = true; var detailModels = _mapper.Map <List <TPSMPurchaseOrderDetailEditModel>, List <TPSMPurchaseOrderDetailDbModel> >(requestObject.PostData.ChildList); //未审核可转数据都是0 detailModels.ForEach(p => p.TransferNum = 0); foreach (var item in detailModels) { if (!detailFlag) { break; } item.MainId = mainModel.ID; //新增或修改明细数据 detailFlag = item.ID <= 0 ? await currDb.Insertable(item).ExecuteCommandIdentityIntoEntityAsync() : await currDb.Updateable(item).ExecuteCommandAsync() > 0; } //删除明细数据 if (detailFlag) { var detailIds = detailModels.Select(p => p.ID).ToList(); detailFlag = currDb.Deleteable <TPSMPurchaseOrderDetailDbModel>() .Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID) .ExecuteCommand() >= 0; } //如果有源单号 if (mainModel.SourceId > 0) { var transList = await GetTransDetailsAsync((int)mainModel.SourceId, detailModels, beforEditDetailData, currentUser); foreach (var item in transList) { await _db.Instance.Updateable <TMMPurchaseApplyDetailDbModel>() .SetColumns(p => p.TransNum == p.TransNum - item.TransNum) .Where(p => p.MainId == item.MainId && p.MaterialId == item.MaterialId) .ExecuteCommandAsync(); } FalseTMMPurchaseApplyMain(mainModel.SourceId.Value); } else { var detailModelLists = _db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel>().Where(p => p.MainId == requestObject.PostData.ID).ToList(); var groupEntity = from p in detailModelLists group p by new { p.MaterialId } into g select new { g.Key, MaxPrice = g.Max(p => p.UnitPrice), MinPrice = g.Min(p => p.UnitPrice) }; var differentEntity = groupEntity.Where(p => p.MaxPrice != p.MinPrice).ToList(); if (differentEntity.Count() > 0) { var meList = BasicCacheGet.GetMaterial(currentUser); string error = string.Join(",", differentEntity.Select(p => meList.Where(p1 => p1.ID == p.Key.MaterialId).FirstOrDefault()?.MaterialName)); throw new Exception($"{error}商品的单价不一致,请保证相同商品价格一致性"); } } //提交事务 currDb.CommitTran(); //返回执行结果 TPSMPurchaseOrderMainQueryModel returnObject = null; var tempreturnObject = await GetWholeMainData(mainModel.ID, currentUser); returnObject = tempreturnObject.Data; return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .SuccessResult(returnObject)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TPSMPurchaseOrderMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 修改T_WM_OtherWhSendMain数据 /// </summary> /// <param name="requestObject">Put请求参数</param> /// <param name="currentUser"></param> /// <returns>返回响应结果对象,包括响应代码,修改操作结果</returns> public async Task <ResponseObject <TWMOtherWhSendMainQueryModel> > PutAsync(RequestPut <TWMOtherWhSendMainEditModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null) { return(ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, "PostData不能为null")); } if (requestObject.PostData.ChildList == null || requestObject.PostData.ChildList.Count < 1) { return(ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, "PostData.ChildList至少包含一条数据")); } //开启事务 currDb.BeginTran(); //修改主表信息 var mainModel = _mapper.Map <TWMOtherWhSendMainDbModel>(requestObject.PostData); mainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNumber); mainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); await currDb.Updateable(mainModel) .UpdateColumns(p => new { p.WhSendType, p.WhSendDate, p.Number, p.Amount, p.ReceiptId }) .ExecuteCommandAsync(); /* * 修改明细逻辑 * 1.根据主单ID查询现有明细数据 * 2.PostData.ChildList中明细ID <= 0的新增 * 3.PostData.ChildList中明细ID > 0的修改 * 4.删除不在PostData.CihldList中的数据 */ var detailModels = _mapper.Map <List <TWMOtherWhSendDetailEditModel>, List <TWMOtherWhSendDetailDbModel> >(requestObject.PostData.ChildList); foreach (var item in detailModels) { item.MainId = mainModel.ID; if (item.ID <= 0) { await currDb.Insertable(item).ExecuteCommandIdentityIntoEntityAsync(); } else { await currDb.Updateable(item).ExecuteCommandAsync(); } } //删除明细数据 var detailIds = detailModels.Select(p => p.ID).ToList(); currDb.Deleteable <TWMOtherWhSendDetailDbModel>() .Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID) .ExecuteCommand(); #region 检查出库是否超过可用量 int?iMainId = mainModel.ID; var mList = BasicCacheGet.GetMaterial(currentUser); var details = requestObject.PostData.ChildList.GroupBy(p => new { p.MaterialId, p.WarehouseId }).Select(p => new TWMOtherCountDbModel { MaterialId = p.Key.MaterialId, WarehouseId = p.Key.WarehouseId, WhSendNumber = p.Sum(p1 => p1.ActualNumber) }).ToList(); foreach (var item in details) { TWMStaQuery tWMStaQuery = new TWMStaQuery(); tWMStaQuery.EditID = iMainId; tWMStaQuery.MaterialId = item.MaterialId; tWMStaQuery.WarehouseId = item.WarehouseId; tWMStaQuery.OperateType = OperateEnum.Other; decimal AvaiableNum = _staticInventory.GeTWMCountModel(tWMStaQuery).AvaiableNum; if (AvaiableNum < 0) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料可用量不足"); } if (item.WhSendNumber > AvaiableNum) { var me = mList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); throw new Exception($"物料代码:{me.MaterialCode},物料名称{me.MaterialName}的物料出库数量不能大于{AvaiableNum}"); } } #endregion var returnData = await GetMainData(mainModel.ID); //提交事务 currDb.CommitTran(); //返回执行结果 return(ResponseUtil <TWMOtherWhSendMainQueryModel> .SuccessResult(returnData)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMOtherWhSendMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 新增T_WM_ProductionWhMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <TWMProductionWhMainQueryModel> > PostAsync(RequestPost <TWMProductionWhMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //插入主表数据 var mapMainModel = _mapper.Map <TWMProductionWhMainDbModel>(requestObject.PostData); mapMainModel.CompanyId = currentUser.CompanyID; mapMainModel.OperatorId = currentUser.UserID; mapMainModel.Number = requestObject.PostData.ChildList.Sum(p => p.ActualNum); mapMainModel.Amount = requestObject.PostData.ChildList.Sum(p => p.Amount); mapMainModel.AuditStatus = 0; mapMainModel.WarehousingType = 1; var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => p.MainId = mainId); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TWMProductionWhDetailAddModel>, List <TWMProductionWhDetailDbModel> >(requestObject.PostData.ChildList); var listItem = _db.Instance.Queryable <TMMWhApplyDetailDbModel>().Where(t => t.MainId == mapMainModel.SourceId).ToList(); var materList = BasicCacheGet.GetMaterial(currentUser); List <TMMWhApplyDetailDbModel> toEdit = new List <TMMWhApplyDetailDbModel>(); foreach (var item in mapDetailModelList) { var procudeMaterial = listItem.Where(p => p.ID == item.ProOrderDetailId).FirstOrDefault(); //生产单明细 var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); //物料 decimal wareTransNum = UnitChange.TranserUnit(me, UnitType.Produce, UnitType.Warehouse, procudeMaterial.TransNum); //可转的数量(仓库单位) decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Produce, item.ActualNum); //实际转数量(生产单位) if (item.ActualNum > wareTransNum) { throw new Exception($"物料代码{me.MaterialCode} 入库数量不能大于{wareTransNum }"); } else if (item.ActualNum == wareTransNum) { item.ProActualNum = procudeMaterial.TransNum; procudeMaterial.TransNum = 0; } else { if (proNum > procudeMaterial.TransNum) { item.ProActualNum = procudeMaterial.TransNum; procudeMaterial.TransNum = 0; } else { item.ProActualNum = proNum; procudeMaterial.TransNum = procudeMaterial.TransNum - proNum; } } toEdit.Add(procudeMaterial); } if (toEdit != null && toEdit.Count() > 0) { _db.Instance.Updateable <TMMWhApplyDetailDbModel>(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TMMWhApplyDetailDbModel>().Any(p => p.TransNum > 0 && p.MainId == mapMainModel.SourceId)) { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mapMainModel.SourceId).SetColumns(p => p.TransferFlag == false).ExecuteCommand(); } var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; //提交事务 currDb.CommitTran(); var res = await GetWholeMainData(mainId, currentUser); //返回执行结果 return(ResponseUtil <TWMProductionWhMainQueryModel> .SuccessResult(res.Data)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TWMProductionWhMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 新增T_SSM_SalesOrderMain数据 /// </summary> /// <param name="requestObject">Post请求参数</param> /// <param name="currentUser">当前操作用户</param> /// <returns>返回响应结果对象,包括响应代码,新增操作结果</returns> public async Task <ResponseObject <TSSMSalesOrderMainQueryModel> > PostAsync(RequestPost <TSSMSalesOrderMainAddModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { //没有新增数据,返回错误信息 if (requestObject.PostData == null) { return(ResponseUtil <TSSMSalesOrderMainQueryModel> .FailResult(null, "PostData不能为null")); } //开启事务 currDb.BeginTran(); //插入主表数据 var mapMainModel = _mapper.Map <TSSMSalesOrderMainDbModel>(requestObject.PostData); mapMainModel.OperatorId = currentUser.UserID; mapMainModel.CompanyId = currentUser.CompanyID; mapMainModel.SalesNum = requestObject.PostData.ChildList.Sum(p => p.SalesNum); mapMainModel.SalesAmount = requestObject.PostData.ChildList.Sum(p => p.SalesAmount); var mainId = await currDb.Insertable(mapMainModel).ExecuteReturnIdentityAsync(); //更新明细表外键ID值 requestObject.PostData.ChildList.ForEach(p => p.MainId = mainId); //插入从表数据 var mapDetailModelList = _mapper.Map <List <TSSMSalesOrderDetailAddModel>, List <TSSMSalesOrderDetailDbModel> >(requestObject.PostData.ChildList); var cache = BasicCacheGet.GetMaterial(currentUser); mapDetailModelList.ForEach(x => { TBMMaterialFileCacheModel tBMMaterialFileCacheModel = GetMaterialFileByPackageColor(cache, currentUser, x.PackageId, x.ColorSolutionId); x.MaterialId = tBMMaterialFileCacheModel.ID; }); var groupEntity = from p in mapDetailModelList group p by new { p.GoodsCode, p.GoodsName, p.MaterialId } into g select new{ g.Key, MaxPrice = g.Max(p => p.UnitPrice), MinPrice = g.Min(p => p.UnitPrice) }; var differentEntity = groupEntity.Where(p => p.MaxPrice != p.MinPrice).ToList(); if (differentEntity.Count() > 0) { string error = string.Join(",", differentEntity.Select(p => p.Key.GoodsCode)); throw new Exception($"{error}商品的单价不一致,请保证相同商品价格一致性"); } var result = await currDb.Insertable(mapDetailModelList).ExecuteCommandAsync() > 0; //提交事务 currDb.CommitTran(); TSSMSalesOrderMainQueryModel returnObject = null; if (result) { returnObject = await GetMainQueryModel(mainId); } //返回执行结果 return(result ? ResponseUtil <TSSMSalesOrderMainQueryModel> .SuccessResult(returnObject) : ResponseUtil <TSSMSalesOrderMainQueryModel> .FailResult(null, "新增数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <TSSMSalesOrderMainQueryModel> .FailResult(null, ex.Message)); } }
/// <summary> /// 删除T_WM_ProductionWhMain数据 /// </summary> /// <param name="requestObject">Delete请求参数</param> /// <returns>返回响应结果对象,包括响应代码,删除操作结果</returns> public async Task <ResponseObject <bool> > DeleteAsync(RequestDelete <DeleteModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null && requestObject.PostDataList == null) { return(ResponseUtil <bool> .FailResult(false, "PostData、PostDataList不能都为null")); } //开启事务 currDb.BeginTran(); //删除标识 var mainFlag = true; var detailFlag = true; var materList = BasicCacheGet.GetMaterial(currentUser); if (requestObject.PostDataList != null && requestObject.PostDataList.Count > 0) { //批量删除 var mainIds = requestObject.PostDataList.Select(p => p.ID).ToList(); foreach (int ids in mainIds) { List <TMMWhApplyDetailDbModel> toEdit = new List <TMMWhApplyDetailDbModel>(); var mainEntity = _db.Instance.Queryable <TWMProductionMainDbModel>().Where(P => P.ID == ids).First(); var listItem = _db.Instance.Queryable <TMMWhApplyDetailDbModel>().Where(t => t.MainId == mainEntity.SourceId).ToList(); var whDetailList = currDb.Queryable <TWMProductionWhDetailDbModel>().Where(p => p.MainId == ids).ToList(); foreach (var item in whDetailList) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); var m = listItem.Where(p => p.ID == item.ProOrderDetailId).FirstOrDefault(); m.TransNum = m.TransNum + item.ProActualNum; toEdit.Add(m); } if (toEdit != null && toEdit.Count() > 0) { _db.Instance.Updateable <TMMWhApplyDetailDbModel>(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TMMWhApplyDetailDbModel>().Any(p => p.TransNum > 0 && p.MainId == mainEntity.SourceId)) { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == false).ExecuteCommand(); } else { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == true).ExecuteCommand(); } } mainFlag = await _db.Instance.Updateable <TWMProductionWhMainDbModel>() .SetColumns(p => p.DeleteFlag == true) .Where(p => mainIds.Contains(p.ID)) .ExecuteCommandAsync() > 0; } else { List <TMMWhApplyDetailDbModel> toEdit = new List <TMMWhApplyDetailDbModel>(); var mainEntity = _db.Instance.Queryable <TWMProductionWhMainDbModel>().Where(P => P.ID == requestObject.PostData.ID).First(); var listItem = _db.Instance.Queryable <TMMWhApplyDetailDbModel>().Where(t => t.MainId == mainEntity.SourceId).ToList(); var whDetailList = currDb.Queryable <TWMProductionWhDetailDbModel>().Where(p => p.MainId == requestObject.PostData.ID).ToList(); foreach (var item in whDetailList) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); var m = listItem.Where(p => p.ID == item.ProOrderDetailId).FirstOrDefault(); if (m != null) { m.TransNum = m.TransNum + item.ProActualNum; toEdit.Add(m); } } if (toEdit != null && toEdit.Count() > 0) { _db.Instance.Updateable <TMMWhApplyDetailDbModel>(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TMMWhApplyDetailDbModel>().Any(p => p.TransNum > 0 && p.MainId == mainEntity.SourceId)) { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == false).ExecuteCommand(); } else { _db.Instance.Updateable <TMMWhApplyMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferFlag == true).ExecuteCommand(); } //单条删除 mainFlag = await _db.Instance.Updateable <TWMProductionWhMainDbModel>() .SetColumns(p => p.DeleteFlag == true) .Where(p => p.ID == requestObject.PostData.ID) .ExecuteCommandAsync() > 0; } //提交事务 currDb.CommitTran(); //返回执行结果 return(mainFlag && detailFlag ? ResponseUtil <bool> .SuccessResult(true) : ResponseUtil <bool> .FailResult(false, "删除数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <bool> .FailResult(false, ex.Message)); } }
/// <summary> /// 修改T_SSM_SalesOrderMain数据 /// </summary> /// <param name="requestObject">Put请求参数</param> /// <returns>返回响应结果对象,包括响应代码,修改操作结果</returns> public async Task <ResponseObject <bool> > PutAsync(RequestPut <TSSMSalesOrderMainEditModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null) { return(ResponseUtil <bool> .FailResult(false, "PostData不能为null")); } if (requestObject.PostData.ChildList == null || requestObject.PostData.ChildList.Count < 1) { return(ResponseUtil <bool> .FailResult(false, "PostData.ChildList至少包含一条数据")); } //开启事务 currDb.BeginTran(); //修改主表信息 var mainModel = _mapper.Map <TSSMSalesOrderMainDbModel>(requestObject.PostData); mainModel.SalesNum = requestObject.PostData.ChildList.Sum(p => p.SalesNum); mainModel.SalesAmount = requestObject.PostData.ChildList.Sum(p => p.SalesAmount); var mainFlag = await currDb.Updateable(mainModel) .UpdateColumns(p => new { p.CustomerId, p.SalesmanId, p.OrderTypeId, p.SettlementTypeId, p.ReceiptAddress, p.OrderDate, p.ContactName, p.ContactNumber, p.SalesAmount, p.SalesNum }) .Where(p => (SqlFunc.IsNullOrEmpty(p.AuditStatus) || p.AuditStatus != 2) && p.ID == mainModel.ID) .ExecuteCommandAsync() > 0; /* * 修改明细逻辑 * 1.根据主单ID查询现有明细数据 * 2.PostData.ChildList中明细ID <= 0的新增 * 3.PostData.ChildList中明细ID > 0的修改 * 4.删除不在PostData.CihldList中的数据 */ var detailFlag = true; var detailModels = _mapper.Map <List <TSSMSalesOrderDetailEditModel>, List <TSSMSalesOrderDetailDbModel> >(requestObject.PostData.ChildList); var cache = BasicCacheGet.GetMaterial(currentUser); detailModels.ForEach(x => { TBMMaterialFileCacheModel tBMMaterialFileCacheModel = GetMaterialFileByPackageColor(cache, currentUser, x.PackageId, x.ColorSolutionId); x.MaterialId = tBMMaterialFileCacheModel.ID; }); foreach (var item in detailModels) { if (!detailFlag) { break; } item.MainId = mainModel.ID; //新增或修改明细数据 detailFlag = item.ID <= 0 ? await currDb.Insertable(item).ExecuteCommandIdentityIntoEntityAsync() : await currDb.Updateable(item).ExecuteCommandAsync() > 0; } //删除明细数据 if (detailFlag) { var detailIds = detailModels.Select(p => p.ID).ToList(); detailFlag = currDb.Deleteable <TSSMSalesOrderDetailDbModel>() .Where(p => !detailIds.Contains(p.ID) && p.MainId == mainModel.ID) .ExecuteCommand() >= 0; } var detailModelLists = _db.Instance.Queryable <TSSMSalesOrderDetailDbModel>().Where(p => p.MainId == requestObject.PostData.ID).ToList(); var groupEntity = from p in detailModelLists group p by new { p.GoodsCode, p.GoodsName, p.MaterialId } into g select new { g.Key, MaxPrice = g.Max(p => p.UnitPrice), MinPrice = g.Min(p => p.UnitPrice) }; var differentEntity = groupEntity.Where(p => p.MaxPrice != p.MinPrice).ToList(); if (differentEntity.Count() > 0) { string error = string.Join(",", differentEntity.Select(p => p.Key.GoodsCode)); throw new Exception($"{error}商品的单价不一致,请保证相同商品价格一致性"); } //提交事务 currDb.CommitTran(); //返回执行结果 return(mainFlag && detailFlag ? ResponseUtil <bool> .SuccessResult(true) : ResponseUtil <bool> .FailResult(false, "修改数据失败!")); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <bool> .FailResult(false, ex.Message)); } }
private MrpResultModel GetProcuctBomByOrderID(int orderID, CurrentUser currentUser) { MrpResultModel result = new MrpResultModel(); #region 明细 List <TMMProductionOrderBOMQueryModel> deatail = new List <TMMProductionOrderBOMQueryModel>(); List <TBMMaterialFileCacheModel> MMaterialList = BasicCacheGet.GetMaterial(currentUser); var query = _db.Instance.Queryable <TMMProductionOrderBOMDbModel, TMMColorSolutionMainDbModel, TBMMaterialFileDbModel, TBMDictionaryDbModel, TBMPackageDbModel>((t, t1, t2, t3, t4) => new object[] { JoinType.Left, t.ColorSolutionId == t1.ID, JoinType.Inner, t.MaterialId == t2.ID, JoinType.Inner, t2.ColorId == t3.ID, JoinType.Inner, t4.ID == t.PackageId }).Where((t, t1, t2, t3, t4) => t.ProOrderId == orderID).Select((t, t1, t2, t3, t4) => new TMMProductionOrderBOMQueryModel() { ID = t.ID, ProOrderId = t.ProOrderId, MaterialId = t.MaterialId, ColorSolutionCode = SqlFunc.IsNull(t1.SolutionCode, "无配色"), MaterialName = t2.MaterialName, SingleValue = t.SingleValue, ProductionNum = t.ProductionNum, ColorId = t2.ColorId, PackageId = t.PackageId, ColorName = SqlFunc.IsNull(t3.DicValue, "无色"), PackageName = t4.DicValue }); List <TMMProductionOrderBOMQueryModel> bomList = query.ToList(); var packBomGroup = bomList.GroupBy(p => new { p.PackageName, p.PackageId }); foreach (var item in packBomGroup) { var PackageName = item.Key.PackageName; var packageId = item.Key.PackageId; var itemDeatails = item.ToList(); var MaterialGroup = itemDeatails.GroupBy(p => new { p.MaterialName, p.MaterialId, p.ColorName, p.ColorId }); foreach (var itemChild in MaterialGroup) { var MaterialName = itemChild.Key.MaterialName; var materialID = itemChild.Key.MaterialId; var ColorName = itemChild.Key.ColorName; var colorId = itemChild.Key.ColorId; var singleGroup = itemChild.ToList().GroupBy(p => p.SingleValue); foreach (var itemChildChild in singleGroup) { var single = itemChildChild.Key; var list = itemChildChild.ToList(); TMMProductionOrderBOMQueryModel model = new TMMProductionOrderBOMQueryModel(); model.PackageId = packageId; model.MaterialId = materialID; model.MaterialName = MaterialName; model.PackageName = PackageName; model.ColorName = ColorName; model.ColorId = colorId; model.SingleValue = single; model.ProductionNum = list.Sum(p => p.ProductionNum); model.ColorSolutionCode = string.Join(",", list.Select(p => p.ColorSolutionCode).Distinct()); deatail.Add(model); } } } deatail.ForEach((x) => { TBMMaterialFileCacheModel item = MMaterialList.Where(p => p.ID == x.MaterialId).FirstOrDefault(); if (item != null) { x.MaterialName = item.MaterialName; x.MaterialCode = item.MaterialCode; x.BaseUnitId = item.BaseUnitId; x.BaseUnitName = item.BaseUnitName; x.ProduceUnitId = item.ProduceUnitId; x.ProduceUnitName = item.ProduceUnitName; } }); #endregion List <TMMProductionOrderBOMSumQueryModel> bomListSummary = _db.Instance.Queryable <TMMProductionOrderBOMSumDbModel>(). Where((t) => t.ProOrderId == orderID).Select((t) => new TMMProductionOrderBOMSumQueryModel() { ID = t.ID, ProOrderId = t.ProOrderId, MaterialId = t.MaterialId, ColorSolutionCode = t.ColorSolutionCode, TotalValue = t.TotalValue, PurchaseNum = t.PurchaseNum, PurchaseTransNum = t.PurchaseTransNum, PickNum = t.PickNum, PickTransNum = t.PickTransNum, PickTotalNum = t.PickTotalNum, }).ToList(); bomListSummary.ForEach((x) => { TBMMaterialFileCacheModel item = MMaterialList.Where(p => p.ID == x.MaterialId).FirstOrDefault(); if (item != null) { x.MaterialName = item.MaterialName; x.MaterialCode = item.MaterialCode; x.BaseUnitId = item.BaseUnitId; x.BaseUnitName = item.BaseUnitName; x.ProduceUnitId = item.ProduceUnitId; x.ProduceUnitName = item.ProduceUnitName; } }); result.deatails = deatail; result.summary = bomListSummary; return(result); }
/// <summary> /// 获取物料 /// </summary> /// <param name="currentUser"></param> /// <param name="PackageID"></param> /// <param name="ColorSolutionID"></param> /// <returns></returns> public TBMMaterialFileCacheModel GetMaterialFileByPackageColor(CurrentUser currentUser, int PackageID, int?ColorSolutionID) { var cache = BasicCacheGet.GetMaterial(currentUser); return(GetMaterialFileByPackageColor(cache, currentUser, PackageID, ColorSolutionID)); }
/// <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> /// 删除T_WM_PurchaseMain数据 /// </summary> /// <param name="requestObject">Delete请求参数</param> /// <returns>返回响应结果对象,包括响应代码,删除操作结果</returns> public async Task <ResponseObject <bool> > DeleteAsync(RequestDelete <DeleteModel> requestObject, CurrentUser currentUser) { var currDb = _db.Instance;//事务需要使用同一个 SqlSugarClient对象实例 try { if (requestObject.PostData == null && requestObject.PostDataList == null) { return(ResponseUtil <bool> .FailResult(false, "PostData、PostDataList不能都为null")); } //开启事务 currDb.BeginTran(); //删除标识 List <int> IDS = new List <int>(); var materList = BasicCacheGet.GetMaterial(currentUser); if (requestObject.PostDataList != null && requestObject.PostDataList.Count > 0) { IDS = requestObject.PostDataList.Select(p => p.ID).ToList(); //批量删除 var mainIds = requestObject.PostDataList.Select(p => p.ID).ToList(); await _db.Instance.Updateable <TWMPurchaseMainDbModel>() .SetColumns(p => p.DeleteFlag == true) .Where(p => mainIds.Contains(p.ID)) .ExecuteCommandAsync(); } else { IDS.Add(requestObject.PostData.ID); //单条删除 await _db.Instance.Updateable <TWMPurchaseMainDbModel>() .SetColumns(p => p.DeleteFlag == true) .Where(p => p.ID == requestObject.PostData.ID) .ExecuteCommandAsync(); } foreach (int ids in IDS) { List <TPSMPurchaseOrderDetailDbModel> toEdit = new List <TPSMPurchaseOrderDetailDbModel>(); //可采购售单详细信息 var mainEntity = _db.Instance.Queryable <TWMPurchaseMainDbModel>().Where(P => P.ID == ids).First(); //可采购售单主表 var listItem = _db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel>(). Where(t => t.MainId == mainEntity.SourceId).ToList(); //可采购售单详细信息 var whDetailList = currDb.Queryable <TWMPurchaseDetailDbModel>().Where(p => p.MainId == ids).ToList(); foreach (var item in whDetailList) { var me = materList.Where(p => p.ID == item.MaterialId).FirstOrDefault(); decimal proNum = UnitChange.TranserUnit(me, UnitType.Warehouse, UnitType.Purchase, item.ActualNum); var m = listItem.Where(p => p.ID == item.PurchaseDetailId).FirstOrDefault(); if (m != null) { m.TransferNum = m.TransferNum + proNum; toEdit.Add(m); } } if (toEdit.Count() > 0) { _db.Instance.Updateable(toEdit).ExecuteCommand(); } if (!_db.Instance.Queryable <TPSMPurchaseOrderDetailDbModel>().Any(p => p.TransferNum > 0 && p.MainId == mainEntity.SourceId)) { _db.Instance.Updateable <TPSMPurchaseOrderMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferStatus == false).ExecuteCommand(); } else { _db.Instance.Updateable <TPSMPurchaseOrderMainDbModel>().Where(p => p.ID == mainEntity.SourceId).SetColumns(p => p.TransferStatus == true).ExecuteCommand(); } } //提交事务 currDb.CommitTran(); //返回执行结果 return(ResponseUtil <bool> .SuccessResult(true)); } catch (Exception ex) { //回滚事务 currDb.RollbackTran(); //返回异常结果 return(ResponseUtil <bool> .FailResult(false, ex.Message)); } }