using System; using System.Collections.Generic; using System.Data.Entity.Migrations; using System.Linq; using System.Text; using System.Threading.Tasks; using CK.SCP.Models; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Utils; using System.Data.Entity.Core; using System.Web; using CK.SCP.Models.AppBoxEntity; using System.Data.SqlClient; using System.Collections; namespace CK.SCP.Controller { public class SCP_ASK_CONTROLLER { public static void Get_V_TB_ASK_List(V_TB_ASK p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASK; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum.Contains(p_entity.AskBillNum)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId == p_entity.VendId); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.Buyer)) { q = q.Where(p => p.Buyer.Contains(p_entity.Buyer)); } if (!string.IsNullOrEmpty(p_entity.BuyerPhone)) { q = q.Where(p => p.BuyerPhone.Contains(p_entity.BuyerPhone)); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (p_entity.BillStateList != null && p_entity.BillStateList.Count > 0) { q = q.Where(p => p_entity.BillStateList.Contains((int)p.State)); } if (p_entity.BeginTime != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTime); } if (p_entity.EndTime != null) { q = q.Where(p => p.EndTime <= p_entity.EndTime); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.State_DESC)) { q = q.Where(p => p.State_DESC.Contains(p_entity.State_DESC)); } if (!string.IsNullOrEmpty(p_entity.ModType_DESC)) { q = q.Where(p => p.ModType_DESC.Contains(p_entity.ModType_DESC)); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); if (p_entity.UserInAddress.FirstOrDefault() != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.Site)); } if (p_entity.UserInSubSite != null && p_entity.UserInSubSite.Count > 0) { q = q.Where(p => p_entity.UserInSubSite.Contains(p.SubSite)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser == p_entity.CreateUser); } if (!string.IsNullOrEmpty( p_entity.SubSite)) { q = q.Where(p => p.SubSite == p_entity.SubSite); } if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0) { q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendId)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", sb.ToString()); if (dbEx.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List(DbEntityValidationException)", LogHelper.GetExceptionMessage(dbEx).Message); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", ex.Message.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List(OptimisticConcurrencyException)", LogHelper.GetExceptionMessage(ex).Message); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", ex.Message.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List(ScpException)", LogHelper.GetExceptionMessage(ex).Message); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", inner.Message.ToString()); throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", ex.InnerException.Message); throw ex.InnerException; } } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List", e.Message); if (e.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_List(Exception)", LogHelper.GetExceptionMessage(e).Message); throw e; } } public static TB_ASK Getask(string ASK) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var site = db.TB_ASK.FirstOrDefault(p => p.AskBillNum == ASK); return site; } } /// /// 要货看板减产数量 /// /// 要货看板业务类 /// public static ResultObject Update_ReduceQty(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { var _entity = db.TB_ASK_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); _entity.ReduceQty = itm.ReduceQty; _entity.Remark = itm.Remark; var asn = db.V_TB_ASN_DETAIL.Where(p => p.PoBillNum == itm.PoBillNum && p.AskBillNum == itm.AskBillNum && p.IsDeleted == false) .GroupBy(p => new { p.PoBillNum, p.AskBillNum, p.PoLine, p.PartCode }) .Select(p => new { AsSum = p.Sum(item => item.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode, AskBillNum = p.Key.AskBillNum }); asn.ToList().ForEach(itm1 => { if (itm.PoBillNum == itm1.PoBillNum && itm.PoLine == itm1.PoLine && itm.PartCode == itm1.PartCode && itm.AskBillNum == itm1.AskBillNum) { if (itm.ReduceQty > itm.AskQty - itm1.AsSum) { _ret.MessageList.Add(string.Format("零件编号{0}零件名称{1}减产数量>要货数量-发货数量", itm.PartCode, itm.PartDesc1 + itm.PartDesc2)); } } }); db.TB_ASK_DETAIL.AddOrUpdate(p => p.UID, _entity); } if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Failed; } else { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static void DEL_V_TB_ASK_DETAIL(List p_list, Action> p_action) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = p_list.Select(itm => itm.UID).ToList(); var _ls = db.TB_ASK_DETAIL.Where(p => _list.Contains(p.UID)).ToList(); db.TB_ASK_DETAIL.RemoveRange(_ls); if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", e.Message); throw e; } } public static void Get_V_TB_ASK_ASN_View_List(V_TB_INCOMPLETE_ASK p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_INCOMPLETE_ASK; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum == p_entity.AskBillNum); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (p_entity.BeginTimeStart != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTimeStart); } if (p_entity.BeginTimeEnd != null) { q = q.Where(p => p.BeginTime <= p_entity.BeginTimeEnd); } if (p_entity.EndTimeStart != null) { q = q.Where(p => p.EndTime >= p_entity.EndTimeStart); } if (p_entity.EndTimeEnd != null) { q = q.Where(p => p.EndTime <= p_entity.EndTimeEnd); } if (p_entity.Buyer != null) { q = q.Where(p => p.Buyer == p_entity.Buyer); } if (p_entity.CreateUser != null) { q = q.Where(p => p.CreateUser == p_entity.CreateUser); } if (p_entity.EndTime != null) { q = q.Where(p => p.EndTime < p_entity.EndTime); } if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.Site)); } if (p_entity.UserInSubSite != null && p_entity.UserInSubSite.Count > 0) { q = q.Where(p => p_entity.UserInSubSite.Contains(p.SubSite)); } if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0) { q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendId)); } //if (p_entity.DayState == 0) //{ // q = q.Where(p => p.ValidityDays <= 0); //} //if (p_entity.DayState > 0) //{ // q = q.Where(p => (p.ValidityDays - p_entity.DayState)<=0 && p.ValidityDays>0); //} if (p_entity.SendState == 1) { q = q.Where(p => p.AskQty != p.ReduceQty + p.Qty); } if (p_entity.SendState == 2) { q = q.Where(p => p.AskQty == p.ReduceQty + p.Qty); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", sb.ToString()); if (dbEx.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List(DbEntityValidationException)", LogHelper.GetExceptionMessage(dbEx).Message); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List(OptimisticConcurrencyException)", LogHelper.GetExceptionMessage(ex).Message); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List(ScpException)", LogHelper.GetExceptionMessage(ex).Message); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", e.Message); if (e.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List(ScpException)", LogHelper.GetExceptionMessage(e).Message); throw e; } } public static void Get_V_TB_ASK_ASN_ARR_View_List(V_TB_INCOMOLETE_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_INCOMOLETE_DETAIL; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode == p_entity.PartCode); } if (!string.IsNullOrEmpty(p_entity.Buyer)) { q = q.Where(p => p.Buyer == p_entity.Buyer); } if (p_entity.BeginTimeStart != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTimeStart); } if (p_entity.BeginTimeEnd != null) { q = q.Where(p => p.BeginTime <= p_entity.BeginTimeEnd); } if (p_entity.EndTimeStart != null) { q = q.Where(p => p.EndTime >= p_entity.EndTimeStart); } if (p_entity.EndTimeEnd != null) { q = q.Where(p => p.EndTime <= p_entity.EndTimeEnd); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId == p_entity.VendId); } if (!string.IsNullOrEmpty(p_entity.SubSite)) { q = q.Where(p => p.SubSite == p_entity.SubSite); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site == p_entity.Site); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List", e.Message); throw e; } } public static List RejectQty_SUM(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_INCOMPLETE_ASK _detail in p_list) { var reject = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.BillType == 3).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { RejectSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); reject.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.Number = _detail.Number + Math.Abs(itm.RejectSum); } }); } } } return p_list; } /// /// 加载货物详细数量信息 /// /// /// public static List Load_Qty_Sum(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_INCOMPLETE_ASK _detail in p_list) { //合格数量 var rec = db.TB_RECEIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ReceiveState.Check && p.BillType == 0) .GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ReceiveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //到货数量 var arrive = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.Qty > 0).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //退货数量 var back = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.Qty < 0).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //开票数量 var invoice = db.V_TB_INVOICE_ASK.Where(p => p.Remark == _detail.AskBillNum && p.State == (int)ReceiveState.Check && p.BillType == 0) .GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { InvoiceQty = p.Sum(itm => itm.InvoiceQty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //寄售退货 var reject = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.BillType == 3).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { RejectSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); reject.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.Number = _detail.Number + Math.Abs(itm.RejectSum); } }); invoice.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.InvoiceQty = itm.InvoiceQty; } }); back.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.RejectQty = itm.ArriveSum; } }); arrive.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ArriveQty = itm.ArriveSum; } }); rec.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ReceivedQty = itm.ReceiveSum; } }); _detail.OnRoadQty = Convert.ToDecimal(_detail.Qty) - _detail.ArriveQty; } } } return p_list; } public static List LOAD_ASK_DETAIL_SUM(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_ASK_DETAIL _detail in p_list) { var _ask = db.TB_ASK_DETAIL.Where(p => p.AskBillNum == _detail.AskBillNum && p.IsDeleted == false).GroupBy(p => new { p.AskBillNum, p.PoLine, p.PartCode }).Select(p => new { AskSum = p.Sum(itm => itm.AskQty), PoLine = p.Key.PoLine, AskBillNum = p.Key.AskBillNum, PartCode = p.Key.PartCode }); //合格 var rec = db.TB_RECEIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ReceiveState.Check && p.BillType == 0) .GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ReceiveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); // var reject = db.TB_RECEIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)RejectState.Check && p.BillType == 1).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { RejectSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark , PartCode = p.Key.PartCode }); //发货 var asn = db.V_TB_ASN_DETAIL.Where(p => p.AskBillNum == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)AsnState.Ship) .GroupBy(p => new { p.AskBillNum, p.PoLine, p.PartCode }) .Select(p => new { AsSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, AskBillNum = p.Key.AskBillNum, PartCode = p.Key.PartCode }); //var arrive = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.BillType == (int)ArriveBillType.Receive).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //到货 var arrive = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.Qty > 0).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); //退货 var reject = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == _detail.AskBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.Qty < 0).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); var invoice = db.V_TB_INVOICE_ASK.Where(p => p.Remark == _detail.AskBillNum && p.State == (int)ReceiveState.Check && p.BillType == 0) .GroupBy(p => new { p.Remark, p.PoLine, p.PartCode }).Select(p => new { InvoiceQty = p.Sum(itm => itm.InvoiceQty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode }); _detail.PartDesc1 = _detail.PartDesc1 + _detail.PartDesc2; rec.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ReceivedQty = itm.ReceiveSum; } }); reject.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.RejectQty = itm.ArriveSum; } }); asn.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.AskBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ShippedQty = itm.AsSum; } }); _ask.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.AskBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.TempQty = itm.AskSum; } }); arrive.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ArriveQty = itm.ArriveSum; } }); invoice.ToList().ForEach(itm => { if (_detail.AskBillNum == itm.Remark && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.InvoiceQty = itm.InvoiceQty; } }); _detail.OnRoadQty = Convert.ToDecimal(_detail.ShippedQty) - _detail.ArriveQty; } } } return p_list; } public static List LOAD_ASK_DETAIL_CAN(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.ToList().Count > 0) { foreach (V_TB_ASK _detail in p_list) { _detail.Month = Convert.ToDateTime(_detail.EndTime).Month.ToString() + "月订单"; if (_detail.State == 0 || _detail.State == 1 || _detail.State == 2) { continue; } if (_detail.State == 6 || _detail.State == -1) { continue; } var _ask = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == _detail.AskBillNum && p.IsDeleted == false).ToList(); var _ask_can = Get_CAN_SHIPQTY(_ask.ToList()).Where(p => p.CanQty > 0).ToList(); if (_ask_can.Count != 0) { _detail.Flag = "1";//未完成 } else { _detail.Flag = "0";//完成 } //_detail.Flag = (_ask_can.Count).ToString()+ "/" +(_ask.Count).ToString(); //_detail.Month = _detail.CreateTime.Month.ToString() + "月订单"; } } } return p_list; } public static ResultObject Save_TB_ASK_DETAIL_DATE(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { string askBill = string.Empty; DateTime beginTime = DateTime.Now; DateTime endTime = DateTime.Now; foreach (var itm in p_entitys) { var _entity = db.TB_ASK_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); askBill = _entity.AskBillNum; _entity.BeginTime = (DateTime)itm.BeginTime; beginTime = (DateTime)itm.BeginTime; _entity.EndTime = (DateTime)itm.EndTime; endTime = (DateTime)itm.EndTime; db.TB_ASK_DETAIL.AddOrUpdate(p => p.UID, _entity); } if (!string.IsNullOrEmpty(askBill)) { var _entity = db.TB_ASK.Where(p => p.AskBillNum == askBill).FirstOrDefault(); _entity.BeginTime = beginTime; _entity.EndTime = endTime; db.TB_ASK.AddOrUpdate(p => p.UID, _entity); } if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Failed; } else { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASK_DETAIL(List p_entitys, AskState p_state) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { itm.State = (int)p_state; var _entity = db.TB_ASK_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); _entity.AskQty = itm.AskQty; db.TB_ASK_DETAIL.AddOrUpdate(p => p.UID, _entity); } if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Failed; } else { if (db.SaveChanges() != -1) { using (ScpEntities db1 = EntitiesFactory.CreateScpInstance()) { var _ls = p_entitys.Select(p => p.UID).ToList(); if (_ls.Count > 0) { var _entityList = db.V_TB_ASK_DETAIL.Where(p => _ls.Contains(p.UID)).ToList(); var _item = _entityList.FirstOrDefault(); if (_item.AskBillNum.Contains("U")) { var _planList = db1.TB_PO_PLAN_EXTEND.Where(p => p.Domain == _item.Site && p.EndTime == _item.EndTime && p.PoBillNum == _item.PoBillNum).ToList(); _planList.ForEach(p => { var _first = _entityList.Where(itm => itm.PoBillNum == p.PoBillNum && itm.PartCode == p.PartCode && itm.EndTime == p.EndTime).FirstOrDefault(); if (_first != null) { p.PlanQty = _first.AskQty; } }); } db1.SaveChanges(); } } _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// /// /// /// public static ResultObject Save_TB_PO(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { db.TB_PO.AddOrUpdate(itm); } if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_PO", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_PO", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_PO", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_PO", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static void test(string p_askBillNum) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == p_askBillNum && p.IsDeleted == false).ToList(); var _lst = Get_CAN_SHIPQTY(_list); if (_lst.Select(p => p.CanQty).Sum() <= 0) { } } } public static ResultObject COPY_ASK(V_TB_ASK p_ask) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ask = db.TB_ASK.Where(p => p.AskBillNum == p_ask.AskBillNum).FirstOrDefault(); _ask = ConvertHelper.GetObjectClone(_ask); _ask.AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(); _ask.GUID = Guid.NewGuid(); _ask.State = (int)AskState.New; db.TB_ASK.Add(_ask); var _askDetailList = db.TB_ASK_DETAIL.Where(p => p.AskBillNum == p_ask.AskBillNum).ToList(); _askDetailList = ConvertHelper.GetObjectClone(_askDetailList); _askDetailList.ForEach(p => { p.AskBillNum = _ask.AskBillNum; p.State = (int)AskState.New; p.GUID = Guid.NewGuid(); db.TB_ASK_DETAIL.Add(p); } ); int state = db.SaveChanges(); if (state != -1) { _ret.MessageList.Add("复制要货单号" + _ask.AskBillNum + ",请去要货明细修改要货单订货日期和要货日期"); _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_ASK_TO_ASN(V_TB_ASK p_ask, List p_order_list, string p_PlateNumber , string p_buyer, string p_buyerPhone) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { TB_ASN _t = new TB_ASN(); string str1 = "N" + p_ask.VendId; _t.AsnBillNum = SCP_BILLCODE_CONTROLLER.MakeASNCode_BJBQ(str1); _t.AskBillNum = p_ask.AskBillNum; _t.PoBillNum = p_ask.PoBillNum; _t.ReceivedPort = p_ask.ReceivedPort; _t.CreateUser = p_buyer; _t.CreateTime = DateTime.Now; _t.UpdateUser = p_buyer; _t.UpdateTime = _t.CreateTime; _t.ReceiveUser = p_ask.Buyer; _t.ReceiveTime = p_ask.BeginTime; _t.VendId = p_ask.VendId; _t.IsDeleted = false;//操作员 _t.CreateTime = DateTime.Now; _t.CreateUser = p_buyer; _t.Site = p_ask.Site; _t.Remark = p_ask.Remark; _t.State = (int)AsnState.New; _t.Remark = p_ask.Remark; _t.GUID = Guid.NewGuid(); _t.PlateNumber = p_PlateNumber; _t.ShipUser = p_ask.ShipUser; _t.ShipTime = p_ask.ShipTime; _t.SubSite = p_ask.SubSite; _t.ReceiveTime = p_ask.ReceiveTime; db.TB_ASN.AddOrUpdate(p => p.AsnBillNum, _t); List _list = new List(); foreach (var itm in p_order_list) { TB_ASN_DETAIL _tDetail = new TB_ASN_DETAIL(); _tDetail.PoBillNum = _t.PoBillNum; _tDetail.AsnBillNum = _t.AsnBillNum; _tDetail.PoLine = itm.PoLine; _tDetail.PoUnit = itm.PoUnit; _tDetail.Batch = itm.Batch; if (!string.IsNullOrEmpty(itm.VendBatch)) { _tDetail.VendBatch = itm.VendBatch; } else { _tDetail.VendBatch = itm.Batch; } _tDetail.PackQty = itm.PackQty == 0 ? 1 : itm.PackQty; _tDetail.PartCode = itm.PartCode; _tDetail.State = (int)AsnState.New; _tDetail.Qty = (decimal)itm.TempQty; _tDetail.Price = itm.Price; _tDetail.EndTime = itm.EndTime; _tDetail.CreateTime = _t.CreateTime; _tDetail.CreateUser = _t.CreateUser; _tDetail.ProduceDate = itm.ProduceDate; _tDetail.IsDeleted = false; _tDetail.DockCode = itm.ReceivedPort; _tDetail.LocUnit = itm.LocUnit; _tDetail.UnConv = itm.UnConv; _tDetail.State = _t.State; _tDetail.Site = p_ask.Site; _tDetail.ReceivedPort = itm.ReceivedPort; _tDetail.SubSite = p_ask.SubSite; _tDetail.Remark = itm.Remark; _tDetail.GUID = Guid.NewGuid(); String str = itm.PartCode.Substring(0, 3); if ((str == "EXP" || str == "SPA") && (_t.SubSite == "ZZ01" || _t.SubSite == "CQ01")) { _tDetail.Batch = "000000"; } String str2 = itm.PartCode.Substring(0, 5); if (str2 == "SPABJ" && _t.Site == "BJBMPT") { _tDetail.Batch = "000000"; } db.TB_ASN_DETAIL.Add(_tDetail); var _order_detail = db.TB_ASK_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); _order_detail.ShippedQty = (decimal)itm.ShippedQty; db.TB_ASK_DETAIL.AddOrUpdate(p => p.UID, _order_detail); } var list_distinct = p_order_list.DistinctBy(x => new { x.PartCode, x.Site }).ToList(); foreach (var _itm in list_distinct) { //新增供应商零件 var _lst = db.TA_VEND_PART.Where(p => p.PartCode == _itm.PartCode && p.Site == p_ask.Site && p.VendId == p_ask.VendId).ToList(); if (_lst.Count > 0) { foreach (var part in _lst) { part.VendPackQty = _itm.PackQty == 0 ? 1 : _itm.PackQty; } } else { _lst.Add(new TA_VEND_PART() { VendPackQty = _itm.PackQty == 0 ? 1 : _itm.PackQty, PartCode = _itm.PartCode, Site = _itm.Site, VendId = p_ask.VendId }); } db.TA_VEND_PART.AddOrUpdate(_lst.ToArray()); } int state = db.SaveChanges(); if (state != -1) { var _askDetailList = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == p_ask.AskBillNum && p.IsDeleted == false).ToList();//查询要货单明细 var _lst = Get_CAN_SHIPQTY(_askDetailList);//算出可发数量 if (_lst.Select(p => p.CanQty).Sum() <= 0)//可发数量为零时,修改状态 { var _ary = db.TB_ASK.Where(p => p.AskBillNum == p_ask.AskBillNum && p.IsDeleted == false).ToList(); _ary.ForEach(p => p.State = (int)AskState.Ship); if (_ary.Count() > 0) { db.TB_ASK.AddOrUpdate(_ary.ToArray()); } } _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 要货看板零件号零件名称模糊查询 /// /// /// /// /// public static List GET_Likesearch(List p_list, string PartCode, string PartCodeDesc) { if (string.IsNullOrEmpty(PartCode) && string.IsNullOrEmpty(PartCodeDesc)) { return p_list; } using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ls = p_list.Select(p => p.AskBillNum).ToArray(); StringBuilder _buffer = new StringBuilder(); _buffer.Append(" SELECT Distinct(A.UID),A.* from V_TB_ASK A LEFT JOIN "); _buffer.Append(" dbo.TB_ASK_DETAIL B on A.AskBillNum = B.AskBillNum and A.Site = B.Site LEFT JOIN "); _buffer.Append(" dbo.TA_PART TP on TP.PartCode = B.PartCode and TP.Site = A.Site "); _buffer.Append(" WHERE A.AskBillNum in ({0}) "); string _str = string.Format(_buffer.ToString(), $"'{string.Join("','", _ls)}'"); if (!string.IsNullOrEmpty(PartCode)) { //_buffer.Append(" and B.PartCode LIKE ('%{1}%')"); _str += " and B.PartCode LIKE '%" + PartCode + "%'"; } if (!string.IsNullOrEmpty(PartCodeDesc)) { _str += " and (TP.PartDesc1+ TP.PartDesc2 ) LIKE '%" + PartCodeDesc + "%'"; } var _list = db.Database.SqlQuery(_str).ToList(); if (_list != null) { var S = _list.OrderBy(A => A.AskBillNum).ToList().Distinct(); return _list; } else { return p_list; } } } public static List GET_IS_COMPLETED_SEND(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ls = p_list.Select(p => p.AskBillNum).ToList(); List numlist = new List(); var askdetailList = db.V_TB_ASK_DETAIL.Where(q => _ls.Contains(q.AskBillNum) && q.IsDeleted == false).ToList().Distinct().ToList(); var asndetailList = db.V_TB_ASN_DETAIL.Where(q => _ls.Contains(q.AskBillNum) && q.IsDeleted == false).ToList(); if (askdetailList.Count() > 0) { if (asndetailList.Count() > 0) { //按照看板号 行号 零件号分组 var list = asndetailList.GroupBy(p => new { p.AskBillNum, p.PoLine, p.PartCode }).Select(p => new { Sum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, AskBillNum = p.Key.AskBillNum, PartCode = p.Key.PartCode }).ToList(); askdetailList.ForEach(p => { var detail = list.FirstOrDefault(q => q.AskBillNum == p.AskBillNum && q.PoLine == p.PoLine && q.PartCode == p.PartCode); if (detail != null) { if (detail.Sum < p.AskQty) { numlist.Add(p.AskBillNum); } } else { numlist.Add(p.AskBillNum); } }); if (numlist.Count() > 0) { p_list.ForEach(p => { if (numlist.Contains(p.AskBillNum)) { p.IsComplete = false; } }); } } else // 全部没有 { p_list.ForEach(p => { p.IsComplete = true; }); } } //StringBuilder _buffer = new StringBuilder(); //_buffer.Append(" select distinct (C.AskBillNum)from V_TB_ASK_DETAIL C"); //_buffer.Append(" left JOIN "); //_buffer.Append(" (select SUM(Qty) as Qty, a.PoLine, a.AskBillNum, a.PartCode from v_tb_asn_detail a where a.IsDeleted=0 "); //_buffer.Append(" group by a.PoLine, a.AskBillNum, a.PartCode) AS D "); //_buffer.Append(" ON C.AskBillNum = D.AskBillNum AND C.PoLine = D.PoLine AND C.PartCode = D.PartCode "); //_buffer.Append(" WHERE C.AskQty>isnull(D.Qty,0)"); //_buffer.Append(" and c.AskBillNum in ('{0}')"); //var _str = string.Format(_buffer.ToString(), string.Join("','", _ls.ToArray())); //var _list = db.Database.SqlQuery(_str).ToList(); //p_list.ForEach(p => //{ // p.IsComplete = true; // if (_list.Contains(p.AskBillNum)) // { // p.IsComplete = false; // } //}); } return p_list; } public static List Get_CAN_SHIPQTY(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //List list = new List(); //var detail = p_list[0]; //foreach (var item in p_list) //{ // var list1 = db.V_TB_ASN_DETAIL.Where(p => p.IsDeleted == false && item.AskBillNum == p.AskBillNum).ToList(); // if (list1!= null) // { // list.AddRange(list1); // } //} //list = db.V_TB_ASN_DETAIL.Where(p => p.IsDeleted == false && detail.AskBillNum == p.AskBillNum).ToList(); var asklist = p_list.Select(q => q.AskBillNum).ToList(); var asnlist = db.V_TB_ASN_DETAIL.Where(p => p.IsDeleted == false && asklist.Contains( p.AskBillNum)).ToList(); var arrlist = db.TB_ARRIVE_DETAIL.Where(pl => asklist.Contains(pl.Remark) && pl.IsDeleted == false && pl.State == (int)ArriveState.OK && pl.Qty < 0 && pl.BillType == 3).ToList(); p_list.ForEach(itm => { //var list = db.V_TB_ASN_DETAIL.Where(p => p.IsDeleted == false && itm.AskBillNum == p.AskBillNum).ToList(); var groupList = asnlist.Where(q=>q.AskBillNum == itm.AskBillNum).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode, p.AskBillNum }).Select( p => new { ShipSum = p.Sum(itm2 => itm2.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode, AskBillNum = p.Key.AskBillNum }).ToList(); groupList.ForEach(p => { if (p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.PartCode == itm.PartCode) { itm.ShippedQty = p.ShipSum; } }); var list_back = arrlist.Where(q=>q.Remark == itm.AskBillNum).GroupBy(p => new { p.Remark, p.PoLine, p.PartCode, p.PoBillNum }).Select(p => new { BackSum = p.Sum(itm1 => itm1.Qty), PoLine = p.Key.PoLine, Remark = p.Key.Remark, PartCode = p.Key.PartCode, PoBillNum = p.Key.PoBillNum }).ToList(); list_back.ForEach(p => { if (p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.PartCode == itm.PartCode) { itm.RejectQty = Math.Abs(p.BackSum);//寄售退货数 } }); decimal a = itm.AskQty - (itm.ShippedQty == null ? 0 : (decimal)itm.ShippedQty) + itm.RejectQty - (itm.ReduceQty == null ? 0 : (decimal)itm.ReduceQty); itm.CanQty = a; itm.PackQty = (itm.PackQty == 0) ? 1 : itm.PackQty; }); } } if (p_list.Count > 0) { return p_list; } return new List(); } public static void Get_V_TB_ASK_DETAIL_List(V_TB_ASK_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASK_DETAIL; if (p_entity.UID != 0) { q = q.Where(p => p.UID == p_entity.UID); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (p_entity.PoLine != 0) { q = q.Where(p => p.PoLine == p_entity.PoLine); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (p_entity.ShippedQty != null && p_entity.ShippedQty != 0) { q = q.Where(p => p.ShippedQty == p_entity.ShippedQty); } if (p_entity.ReceivedQty != 0) { q = q.Where(p => p.ReceivedQty == p_entity.ReceivedQty); } if (!string.IsNullOrEmpty(p_entity.PoUnit)) { q = q.Where(p => p.PoUnit.Contains(p_entity.PoUnit)); } if (p_entity.EndTime != null) { q = q.Where(p => p_entity.EndTime < p.EndTime); } if (!string.IsNullOrEmpty(p_entity.LocUnit)) { q = q.Where(p => p.LocUnit.Contains(p_entity.LocUnit)); } if (p_entity.Price != 0) { q = q.Where(p => p.Price == p_entity.Price); } if (!string.IsNullOrEmpty(p_entity.Currency)) { q = q.Where(p => p.Currency.Contains(p_entity.Currency)); } if (p_entity.PackQty != 0) { q = q.Where(p => p.PackQty == p_entity.PackQty); } if (p_entity.UnConv != 0) { q = q.Where(p => p.UnConv == p_entity.UnConv); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (p_entity.State != 0) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId == p_entity.VendId); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum.Contains(p_entity.AskBillNum)); } if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.Site)); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", sb.ToString()); if (dbEx.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_TB_PRICE_CONTROLLER), "Get_V_TB_ASK_DETAIL_List(DbEntityValidationException)", LogHelper.GetExceptionMessage(dbEx).Message); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List(OptimisticConcurrencyException)", LogHelper.GetExceptionMessage(ex).Message); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List(ScpException)", LogHelper.GetExceptionMessage(ex).Message); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", e.Message); if (e.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List(Exception)", LogHelper.GetExceptionMessage(e).Message); throw e; } } public static void Get_V_TB_ASK_DETAIL_Sum(V_TB_ASK_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASK_DETAIL; if (p_entity.UID != 0) { q = q.Where(p => p.UID == p_entity.UID); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (p_entity.PoLine != 0) { q = q.Where(p => p.PoLine == p_entity.PoLine); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (p_entity.ShippedQty != null && p_entity.ShippedQty != 0) { q = q.Where(p => p.ShippedQty == p_entity.ShippedQty); } if (p_entity.ReceivedQty != 0) { q = q.Where(p => p.ReceivedQty == p_entity.ReceivedQty); } if (!string.IsNullOrEmpty(p_entity.PoUnit)) { q = q.Where(p => p.PoUnit.Contains(p_entity.PoUnit)); } if (!string.IsNullOrEmpty(p_entity.LocUnit)) { q = q.Where(p => p.LocUnit.Contains(p_entity.LocUnit)); } if (p_entity.Price != 0) { q = q.Where(p => p.Price == p_entity.Price); } if (!string.IsNullOrEmpty(p_entity.Currency)) { q = q.Where(p => p.Currency.Contains(p_entity.Currency)); } if (p_entity.PackQty != 0) { q = q.Where(p => p.PackQty == p_entity.PackQty); } if (p_entity.UnConv != 0) { q = q.Where(p => p.UnConv == p_entity.UnConv); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (p_entity.State != 0) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum.Contains(p_entity.AskBillNum)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_List", e.Message); throw e; } } public static ResultObject Save_TB_ASK_STATE_BJBQ(List p_list, AskState p_state, List p_list1) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_state == AskState.Confirm) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p_list1.Contains(p.Site)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Confirm; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum && itm.Site == p.Site).ToList(); _list.ForEach(itm => itm.State = (int)AskState.Confirm); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商确认要货状态完成 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //_pendingList.ForEach(itm => { // itm.ITEM_STATE = (int)PENDING_STATE.COMPLETED; // itm.ITEM_CONTENT = "要货看板已完成!"; //} //); //db.TB_PENING_ITEMS.AddOrUpdate(itm => itm.GUID, _pendingList.ToArray()); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.UID, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.New) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.New; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.New); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } // #region 供应商要货代办取消 // var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //if (_pendingList.Count > 0) //{ // db.TB_PENING_ITEMS.RemoveRange(_pendingList); //} // #endregion ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.Release) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商代办任务新 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = p.CreateUser; _item.VEND_ID = p.VendId; _item.SENDING_TIME = DateTime.Now; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.Reject) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Reject; p.IsDeleted = true; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Reject; itm.IsDeleted = true; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); //#region 供应商要货代办作废 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //db.TB_PENING_ITEMS.RemoveRange(_pendingList); //#endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.CompleteForce) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Confirm || p.State == (int)AskState.Ship); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { if (p.State == (int)AskState.Complete) { p.State = (int)AskState.Reject; p.IsDeleted = true; } // p.State = (int)AskState.CompleteForce; #region 修改发货单 var _asnList = db.TB_ASN.Where(itm => itm.IsDeleted == false && itm.AskBillNum == p.AskBillNum && itm.State == 0).ToList(); _asnList.ForEach(itm => { itm.State = (int)AsnState.Reject; itm.IsDeleted = true; var _asnDetailList = db.TB_ASN_DETAIL.Where(asn => asn.IsDeleted == false && asn.AsnBillNum == itm.AsnBillNum).ToList(); _asnDetailList.ForEach(ad => { itm.State = (int)AsnState.Reject; ad.IsDeleted = true; } ); db.TB_ASN_DETAIL.AddOrUpdate(asndetail => asndetail.UID, _asnDetailList.ToArray()); }); db.TB_ASN.AddOrUpdate(_asnList.ToArray()); #endregion var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum && itm.IsDeleted == false).ToList(); _list.ForEach(itm => { itm.AskQty = 0; var asn = db.V_TB_ASN_DETAIL.Where(ask => ask.AskBillNum == itm.AskBillNum && ask.IsDeleted == false && (ask.State == (int)AsnState.Ship || ask.State == (int)AsnState.Receive)) .GroupBy(ask => new { ask.AskBillNum, ask.PoLine, ask.PartCode }) .Select(ask => new { AsSum = ask.Sum(itm1 => itm1.Qty), PoLine = ask.Key.PoLine, AskBillNum = ask.Key.AskBillNum, PartCode = ask.Key.PartCode }).ToList(); asn.ForEach(itm1 => { if (itm1.AskBillNum == itm.AskBillNum && itm1.PoLine == itm.PoLine && itm1.PartCode == itm.PartCode) { itm.AskQty = itm1.AsSum; } }); //itm.State = (int)AskState.CompleteForce; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是【已确认】或【已发货】状态,【发布状态】请用取消发布或看板作废!"; } } if (p_state == AskState.Receive) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Receive; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Receive; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.NoConfirm) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); if (_ls.Count == 0) { var _askList = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); _askList.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _askList.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录已经有发货单据不能取消!"; } //if (count > 0) //{ // _ls.ForEach(p => // { // p.State = (int)AskState.Receive; // var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); // _list.ForEach(itm => // { // itm.State = (int)AskState.Receive; // } // ); // db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); // } // ); // db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); //} //else //{ // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建或发布状态记录!"; //} } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { if (p_state == AskState.CompleteForce) { using (ScpEntities db1 = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { var _entityList = db.V_TB_ASK_DETAIL.Where(p => p_list.Contains(p.AskBillNum)).ToList(); var _item = _entityList.FirstOrDefault(); if (_item.AskBillNum.Contains("U")) { var _planList = db1.TB_PO_PLAN_EXTEND.Where(p => p.Domain == _item.Site && p.EndTime == _item.EndTime && p.PoBillNum == _item.PoBillNum).ToList(); _planList.ForEach(p => { var _first = _entityList.Where(itm => itm.PoBillNum == p.PoBillNum && itm.PartCode == p.PartCode && itm.EndTime == p.EndTime).FirstOrDefault(); if (_first != null) { p.PlanQty = _first.AskQty; } }); } db1.SaveChanges(); } } } _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASK_STATE(List p_list, AskState p_state) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_state == AskState.Confirm) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Confirm; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.Confirm); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商确认要货状态完成 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //_pendingList.ForEach(itm => { // itm.ITEM_STATE = (int)PENDING_STATE.COMPLETED; // itm.ITEM_CONTENT = "要货看板已完成!"; //} //); //db.TB_PENING_ITEMS.AddOrUpdate(itm => itm.GUID, _pendingList.ToArray()); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.New) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.New; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.New); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } // #region 供应商要货代办取消 // var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //if (_pendingList.Count > 0) //{ // db.TB_PENING_ITEMS.RemoveRange(_pendingList); //} // #endregion ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.Release) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商代办任务新 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = p.CreateUser; _item.VEND_ID = p.VendId; _item.SENDING_TIME = DateTime.Now; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建状态记录!"; } } if (p_state == AskState.Reject) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Reject; p.IsDeleted = true; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Reject; itm.IsDeleted = true; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); //#region 供应商要货代办作废 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //db.TB_PENING_ITEMS.RemoveRange(_pendingList); //#endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.CompleteForce) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Confirm || p.State == (int)AskState.Ship); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { if (p.State == (int)AskState.Complete) { p.State = (int)AskState.Reject; p.IsDeleted = true; } // p.State = (int)AskState.CompleteForce; #region 修改发货单 var _asnList = db.TB_ASN.Where(itm => itm.IsDeleted == false && itm.AskBillNum == p.AskBillNum && itm.State == 0).ToList(); _asnList.ForEach(itm => { itm.State = (int)AsnState.Reject; itm.IsDeleted = true; var _asnDetailList = db.TB_ASN_DETAIL.Where(asn => asn.IsDeleted == false && asn.AsnBillNum == itm.AsnBillNum).ToList(); _asnDetailList.ForEach(ad => { itm.State = (int)AsnState.Reject; ad.IsDeleted = true; } ); db.TB_ASN_DETAIL.AddOrUpdate(asndetail => asndetail.UID, _asnDetailList.ToArray()); }); db.TB_ASN.AddOrUpdate(_asnList.ToArray()); #endregion var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum && itm.IsDeleted == false).ToList(); _list.ForEach(itm => { itm.AskQty = 0; var asn = db.V_TB_ASN_DETAIL.Where(ask => ask.AskBillNum == itm.AskBillNum && ask.IsDeleted == false && (ask.State == (int)AsnState.Ship || ask.State == (int)AsnState.Receive)) .GroupBy(ask => new { ask.AskBillNum, ask.PoLine, ask.PartCode }) .Select(ask => new { AsSum = ask.Sum(itm1 => itm1.Qty), PoLine = ask.Key.PoLine, AskBillNum = ask.Key.AskBillNum, PartCode = ask.Key.PartCode }).ToList(); asn.ForEach(itm1 => { if (itm1.AskBillNum == itm.AskBillNum && itm1.PoLine == itm.PoLine && itm1.PartCode == itm.PartCode) { itm.AskQty = itm1.AsSum; } }); //itm.State = (int)AskState.CompleteForce; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是【已确认】或【已发货】状态,【发布状态】请用取消发布或看板作废!"; } } if (p_state == AskState.Receive) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Receive; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Receive; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.NoConfirm) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); if (_ls.Count == 0) { var _askList = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); _askList.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _askList.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录已经有发货单据不能取消!"; } //if (count > 0) //{ // _ls.ForEach(p => // { // p.State = (int)AskState.Receive; // var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); // _list.ForEach(itm => // { // itm.State = (int)AskState.Receive; // } // ); // db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); // } // ); // db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); //} //else //{ // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建或发布状态记录!"; //} } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { if (p_state == AskState.CompleteForce) { using (ScpEntities db1 = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { var _entityList = db.V_TB_ASK_DETAIL.Where(p => p_list.Contains(p.AskBillNum)).ToList(); var _item = _entityList.FirstOrDefault(); if (_item.AskBillNum.Contains("U")) { var _planList = db1.TB_PO_PLAN_EXTEND.Where(p => p.Domain == _item.Site && p.EndTime == _item.EndTime && p.PoBillNum == _item.PoBillNum).ToList(); _planList.ForEach(p => { var _first = _entityList.Where(itm => itm.PoBillNum == p.PoBillNum && itm.PartCode == p.PartCode && itm.EndTime == p.EndTime).FirstOrDefault(); if (_first != null) { p.PlanQty = _first.AskQty; } }); } db1.SaveChanges(); } } } _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASK_STATE_BMPT(List p_list, AskState p_state, string site) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_state == AskState.Confirm) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Confirm; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.Confirm); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商确认要货状态完成 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //_pendingList.ForEach(itm => { // itm.ITEM_STATE = (int)PENDING_STATE.COMPLETED; // itm.ITEM_CONTENT = "要货看板已完成!"; //} //); //db.TB_PENING_ITEMS.AddOrUpdate(itm => itm.GUID, _pendingList.ToArray()); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.New) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.New; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.New); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } // #region 供应商要货代办取消 // var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //if (_pendingList.Count > 0) //{ // db.TB_PENING_ITEMS.RemoveRange(_pendingList); //} // #endregion ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.Release) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商代办任务新 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = p.CreateUser; _item.VEND_ID = p.VendId; _item.SENDING_TIME = DateTime.Now; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建状态记录!"; } } if (p_state == AskState.Reject) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Reject; p.IsDeleted = true; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Reject; itm.IsDeleted = true; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); //#region 供应商要货代办作废 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //db.TB_PENING_ITEMS.RemoveRange(_pendingList); //#endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.CompleteForce) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false && p.Site == site).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Confirm || p.State == (int)AskState.Ship); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { if (p.State == (int)AskState.Complete) { p.State = (int)AskState.Reject; p.IsDeleted = true; } // p.State = (int)AskState.CompleteForce; #region 修改发货单 var _asnList = db.TB_ASN.Where(itm => itm.IsDeleted == false && itm.AskBillNum == p.AskBillNum && itm.State == 0 && p.Site == site).ToList(); _asnList.ForEach(itm => { itm.State = (int)AsnState.Reject; itm.IsDeleted = true; var _asnDetailList = db.TB_ASN_DETAIL.Where(asn => asn.IsDeleted == false && asn.AsnBillNum == itm.AsnBillNum && asn.Site == site).ToList(); _asnDetailList.ForEach(ad => { itm.State = (int)AsnState.Reject; ad.IsDeleted = true; } ); db.TB_ASN_DETAIL.AddOrUpdate(asndetail => asndetail.UID, _asnDetailList.ToArray()); }); db.TB_ASN.AddOrUpdate(_asnList.ToArray()); #endregion var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum && itm.IsDeleted == false && itm.Site == site).ToList(); _list.ForEach(itm => { itm.AskQty = 0; var asn = db.V_TB_ASN_DETAIL.Where(ask => ask.AskBillNum == itm.AskBillNum && ask.IsDeleted == false && ask.Site == site && (ask.State == (int)AsnState.Ship || ask.State == (int)AsnState.Receive)) .GroupBy(ask => new { ask.AskBillNum, ask.PoLine, ask.PartCode, ask.Site }) .Select(ask => new { AsSum = ask.Sum(itm1 => itm1.Qty), PoLine = ask.Key.PoLine, AskBillNum = ask.Key.AskBillNum, PartCode = ask.Key.PartCode, Site = ask.Key.Site }).ToList(); asn.ForEach(itm1 => { if (itm1.AskBillNum == itm.AskBillNum && itm1.PoLine == itm.PoLine && itm1.PartCode == itm.PartCode) { itm.AskQty = itm1.AsSum; } }); //itm.State = (int)AskState.CompleteForce; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.UID, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是【已确认】或【已发货】状态,【发布状态】请用取消发布或看板作废!"; } } if (p_state == AskState.Receive) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Receive; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Receive; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.NoConfirm) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); if (_ls.Count == 0) { var _askList = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); _askList.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _askList.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录已经有发货单据不能取消!"; } //if (count > 0) //{ // _ls.ForEach(p => // { // p.State = (int)AskState.Receive; // var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); // _list.ForEach(itm => // { // itm.State = (int)AskState.Receive; // } // ); // db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); // } // ); // db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); //} //else //{ // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建或发布状态记录!"; //} } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { if (p_state == AskState.CompleteForce) { using (ScpEntities db1 = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { var _entityList = db.V_TB_ASK_DETAIL.Where(p => p_list.Contains(p.AskBillNum)).ToList(); var _item = _entityList.FirstOrDefault(); if (_item.AskBillNum.Contains("U")) { var _planList = db1.TB_PO_PLAN_EXTEND.Where(p => p.Domain == _item.Site && p.EndTime == _item.EndTime && p.PoBillNum == _item.PoBillNum).ToList(); _planList.ForEach(p => { var _first = _entityList.Where(itm => itm.PoBillNum == p.PoBillNum && itm.PartCode == p.PartCode && itm.EndTime == p.EndTime).FirstOrDefault(); if (_first != null) { p.PlanQty = _first.AskQty; } }); } db1.SaveChanges(); } } } _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASK_STATE_BJBBMPT(List p_list, AskState p_state, User p_user) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_state == AskState.Confirm) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.UpdateUser = p_user.ChineseName; p.UpdateTime = DateTime.Now; p.State = (int)AskState.Confirm; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.Confirm); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商确认要货状态完成 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //_pendingList.ForEach(itm => { // itm.ITEM_STATE = (int)PENDING_STATE.COMPLETED; // itm.ITEM_CONTENT = "要货看板已完成!"; //} //); //db.TB_PENING_ITEMS.AddOrUpdate(itm => itm.GUID, _pendingList.ToArray()); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.New) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.UpdateUser = p_user.ChineseName; p.UpdateTime = DateTime.Now; p.State = (int)AskState.New; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => itm.State = (int)AskState.New); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } // #region 供应商要货代办取消 // var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //if (_pendingList.Count > 0) //{ // db.TB_PENING_ITEMS.RemoveRange(_pendingList); //} // #endregion ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是发布状态记录!"; } } if (p_state == AskState.Release) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.State == (int)AskState.New).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New); if (count > 0) { _ls.ForEach(p => { p.UpdateUser = p_user.ChineseName; p.UpdateTime = DateTime.Now; p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 供应商代办任务新 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = p.CreateUser; _item.VEND_ID = p.VendId; _item.SENDING_TIME = DateTime.Now; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,都不是新建状态记录!"; } } if (p_state == AskState.Reject) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Reject; p.IsDeleted = true; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Reject; itm.IsDeleted = true; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); //#region 供应商要货代办作废 //var _pendingList = db.TB_PENING_ITEMS.Where(itm => itm.ITEM_NO == p.AskBillNum).ToList(); //db.TB_PENING_ITEMS.RemoveRange(_pendingList); //#endregion } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.CompleteForce) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); int count = _ls.Count(p => p.State == (int)AskState.Confirm || p.State == (int)AskState.Ship); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { if (p.State == (int)AskState.Complete) { p.State = (int)AskState.Reject; p.IsDeleted = true; } // p.State = (int)AskState.CompleteForce; #region 修改发货单 var _asnList = db.TB_ASN.Where(itm => itm.IsDeleted == false && itm.AskBillNum == p.AskBillNum && itm.State == 0).ToList(); _asnList.ForEach(itm => { itm.State = (int)AsnState.Reject; itm.IsDeleted = true; var _asnDetailList = db.TB_ASN_DETAIL.Where(asn => asn.IsDeleted == false && asn.AsnBillNum == itm.AsnBillNum).ToList(); _asnDetailList.ForEach(ad => { itm.State = (int)AsnState.Reject; ad.IsDeleted = true; } ); db.TB_ASN_DETAIL.AddOrUpdate(asndetail => asndetail.UID, _asnDetailList.ToArray()); }); db.TB_ASN.AddOrUpdate(_asnList.ToArray()); #endregion var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum && itm.IsDeleted == false).ToList(); _list.ForEach(itm => { itm.AskQty = 0; var asn = db.V_TB_ASN_DETAIL.Where(ask => ask.AskBillNum == itm.AskBillNum && ask.IsDeleted == false && (ask.State == (int)AsnState.Ship || ask.State == (int)AsnState.Receive)) .GroupBy(ask => new { ask.AskBillNum, ask.PoLine, ask.PartCode }) .Select(ask => new { AsSum = ask.Sum(itm1 => itm1.Qty), PoLine = ask.Key.PoLine, AskBillNum = ask.Key.AskBillNum, PartCode = ask.Key.PartCode }).ToList(); asn.ForEach(itm1 => { if (itm1.AskBillNum == itm.AskBillNum && itm1.PoLine == itm.PoLine && itm1.PartCode == itm.PartCode) { itm.AskQty = itm1.AsSum; } }); //itm.State = (int)AskState.CompleteForce; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是【已确认】或【已发货】状态,【发布状态】请用取消发布或看板作废!"; } } if (p_state == AskState.Receive) { var _ls = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); if (count > 0) { _ls.ForEach(p => { p.State = (int)AskState.Receive; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Receive; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是新建或发布状态记录!"; } } if (p_state == AskState.NoConfirm) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); if (_ls.Count == 0) { var _askList = db.TB_ASK.Where(p => p_list.Contains(p.AskBillNum) && p.IsDeleted == false).ToList(); _askList.ForEach(p => { p.State = (int)AskState.Release; var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)AskState.Release; } ); db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _askList.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录已经有发货单据不能取消!"; } //if (count > 0) //{ // _ls.ForEach(p => // { // p.State = (int)AskState.Receive; // var _list = db.TB_ASK_DETAIL.Where(itm => itm.AskBillNum == p.AskBillNum).ToList(); // _list.ForEach(itm => // { // itm.State = (int)AskState.Receive; // } // ); // db.TB_ASK_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); // } // ); // db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _ls.ToArray()); //} //else //{ // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建或发布状态记录!"; //} } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_TB_ASK_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_ASK_MODIF_QTY(V_TB_ASK p_ask, List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == p_ask.AskBillNum && p.IsDeleted == false).ToList(); var _ls = Get_CAN_SHIPQTY(_list); _ls.ForEach(p => { var _entity = p_list.Where(itm => itm.AskBillNum == p.AskBillNum && itm.PoLine == p.PoLine && itm.PartCode == p.PartCode && itm.PoBillNum == p.PoBillNum).FirstOrDefault(); if (_entity != null) { p.AskQty = _entity.AskQty; if (_entity.AskQty < p.ShippedQty) { _ret.MessageList.Add(string.Format("零件编号{0}行号{1},【要货数量】必须大于等于【已发数量】", p.PartCode, p.PoLine.ToString())); } } }); if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Succeed; _ret.Result = false; } else { int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 报表 /// /// /// public static List Get_V_TB_ASK_DETAIL_PO_List(V_TB_ASK_DETAIL_PO p_entity) { List list = new List(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { SqlParameter[] prams = new SqlParameter[7]; string TimeBegin = !p_entity.TimeBegin.HasValue ? "" : DateTime.Parse(p_entity.TimeBegin.ToString()).ToString("yyyy-MM-dd"); string TimeEnd = !p_entity.TimeEnd.HasValue ? "" : DateTime.Parse(p_entity.TimeEnd.ToString()).ToString("yyyy-MM-dd"); string PoBillNum = string.IsNullOrEmpty(p_entity.PoBillNum) ? "" : p_entity.PoBillNum; string AskBillNum = string.IsNullOrEmpty(p_entity.AskBillNum) ? "" : p_entity.AskBillNum; string PartCode = string.IsNullOrEmpty(p_entity.PartCode) ? "" : p_entity.PartCode; string Buyer= string.IsNullOrEmpty(p_entity.Buyer) ? "" : p_entity.Buyer; string VendId = string.IsNullOrEmpty(p_entity.VendId) ? "" : p_entity.VendId; string VendName = string.IsNullOrEmpty(p_entity.VendName) ? "" : p_entity.VendName; string UserInAddress_where = ""; string UserName = p_entity.UserName; if (p_entity.UserInAddress != null) { UserInAddress_where = string.Join(", ", p_entity.UserInAddress); } string str = "exec PROC_test " + " @PONum_where = '" + PoBillNum + "' ,@AskBillNum_where = '" + AskBillNum + "' ,@PartCode_where = '" + PartCode + "' ,@Buyer_where = '" + Buyer + "' ,@BeginDate_where = '" + TimeBegin + "' ,@EndDate_where = '" + TimeEnd + "', @VenderID_where = '" + VendId + "'" + ", @UserInAddress_where = '" + UserInAddress_where + "' ,@UserName = '" + UserName + "',@VendName_where = '"+VendName+"'"; list = db.Database.SqlQuery(str).ToList(); } } catch (Exception e) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_PO_List", e.Message); if (e.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_DETAIL_PO_List(Exception)", LogHelper.GetExceptionMessage(e).Message); throw e; } return list; } /// /// 供应商要货未完成查询 /// /// /// public static void Get_V_TB_ASK_ASN_View_List_Directly(V_TB_INCOMPLETE_ASK p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_INCOMPLETE_ASK; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum == p_entity.AskBillNum); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (p_entity.BeginTimeStart != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTimeStart); } if (p_entity.BeginTimeEnd != null) { q = q.Where(p => p.BeginTime <= p_entity.BeginTimeEnd); } if (p_entity.EndTimeStart != null) { q = q.Where(p => p.EndTime >= p_entity.EndTimeStart); } if (p_entity.EndTimeEnd != null) { q = q.Where(p => p.EndTime <= p_entity.EndTimeEnd); } if (p_entity.Buyer != null) { q = q.Where(p => p.Buyer == p_entity.Buyer); } if (p_entity.CreateUser != null) { q = q.Where(p => p.CreateUser == p_entity.CreateUser); } if (p_entity.EndTime != null) { q = q.Where(p => p.EndTime < p_entity.EndTime); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(P => P.Site == p_entity.Site); } if (!string.IsNullOrEmpty(p_entity.SubSite) ) { q = q.Where(p => p_entity.SubSite == p.SubSite); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId == p_entity.VendId); } if (p_entity.SendState == 1) { q = q.Where(p => p.AskQty != p.ReduceQty + p.Qty); } if (p_entity.SendState == 2) { q = q.Where(p => p.AskQty == p.ReduceQty + p.Qty); } q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly", sb.ToString()); if (dbEx.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly(DbEntityValidationException)", LogHelper.GetExceptionMessage(dbEx).Message); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly(OptimisticConcurrencyException)", LogHelper.GetExceptionMessage(ex).Message); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly", ex.ToString()); if (ex.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly(ScpException)", LogHelper.GetExceptionMessage(ex).Message); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly", e.Message); if (e.InnerException != null) LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASK_ASN_View_List_Directly(ScpException)", LogHelper.GetExceptionMessage(e).Message); throw e; } } } }