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.ScpEntity; using CK.SCP.Utils; using CK.SCP.Controller; using CK.SCP.Models.Enums; using CK.SCP.Models.AppBoxEntity; using System.Data.Entity.Core; using CK.SCP.Models.ScpEntity.ExcelImportEntity; using System.Data; using System.Data.SqlClient; using System.Text.RegularExpressions; namespace CK.SCP.Controller { public class SCP_RECIVECE_CONTROLLER { public static void Get_V_TB_RECEIVE_GG_List(V_TB_RECEIVE p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_RECEIVE; if (!string.IsNullOrEmpty(p_entity.RecvBillNum)) { q = q.Where(p => p.RecvBillNum.Contains(p_entity.RecvBillNum)); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } 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.UserInAddress.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.State == 0 || p_entity.State == 1 || p_entity.State == 2) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.OperName)) { q = q.Where(p => p.OperName.Contains(p_entity.OperName)); } if (!string.IsNullOrEmpty(p_entity.Site_Desc)) { q = q.Where(p => p.Site_Desc.Contains(p_entity.Site_Desc)); } if (!string.IsNullOrEmpty(p_entity.BillType_DESC)) { q = q.Where(p => p.BillType_DESC.Contains(p_entity.BillType_DESC)); } //if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.Address)); //} _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_List", e.Message); throw e; } } public static void Get_V_TB_RECEIVE_List(V_TB_RECEIVE p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_RECEIVE; if (!string.IsNullOrEmpty(p_entity.RecvBillNum)) { q = q.Where(p => p.RecvBillNum.Contains(p_entity.RecvBillNum)); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (p_entity.State == 0 || p_entity.State == 1 || p_entity.State == 2) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.OperName)) { q = q.Where(p => p.OperName.Contains(p_entity.OperName)); } if (!string.IsNullOrEmpty(p_entity.Site_Desc)) { q = q.Where(p => p.Site_Desc.Contains(p_entity.Site_Desc)); } if (!string.IsNullOrEmpty(p_entity.BillType_DESC)) { q = q.Where(p => p.BillType_DESC.Contains(p_entity.BillType_DESC)); } //if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.Address)); //} _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_List", e.Message); throw e; } } public static void Get_V_TB_RECEIVE_DETAIL_List(List p_list, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ary=p_list.Select(p => "'"+p.RecvBillNum+"'").ToList().ToArray(); if (_ary.Count() > 0) { string str = string.Join(",", _ary); string _sql = string.Format("SELECT * FROM [V_TB_RECEIVE_DETAIL] where RecvBillNum in ({0})",str); IQueryable q = db.Database.SqlQuery(_sql).AsQueryable(); _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_DETAIL_List", e.Message); throw e; } } /// /// 保存到货 /// /// /// /// /// public static ResultObject Save_TB_RECEIVE(V_TB_ASN_DETAIL p_entity, List p_list,string currentuser) { ResultObject _ret = new ResultObject(); var scpRcvList = new List(); var scpRcveDetailList = new List(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { DateTime now = DateTime.Now; var _asn = db.TB_ASN.Where(p => p.IsDeleted == false && p.AsnBillNum == p_entity.AsnBillNum).ToList(); if (_asn != null && _asn.Count > 0) { _asn.ForEach(p => { p.State = (int)AsnState.Receive; p.CreateTime = now; }); db.TB_ASN.AddOrUpdate(p => p.UID, _asn.ToArray()); var _asndetail = db.TB_ASN_DETAIL.Where(p => p.IsDeleted == false && p.AsnBillNum == p_entity.AsnBillNum).ToList(); _asndetail.ForEach(p => { p.State = (int)AsnState.Receive; p.CreateTime = now; }); db.TB_ASN_DETAIL.AddOrUpdate(p => p.UID, _asndetail.ToArray()); TB_RECEIVE scpRcv = new TB_RECEIVE(); scpRcv.RecvBillNum = SCP_BILLCODE_CONTROLLER.MakeReviceCode_ASN();//收货单号 scpRcv.State = (int)FormState.开放; scpRcv.Remark = ""; scpRcv.CreateTime = DateTime.Now;//创建日期 scpRcv.CreateUser = currentuser;//创建用户 scpRcv.IsDeleted = false; scpRcv.GUID = System.Guid.NewGuid(); scpRcv.BillType = 0; scpRcv.PoBillNum = p_entity.PoBillNum;//采购单 scpRcv.AsnBillNum = p_entity.AsnBillNum;//发货单号 scpRcv.Site = p_entity.Site;//地点 scpRcv.VendId = p_entity.VendId;//供货商名称 scpRcv.ShipTime = now;//收货日期 scpRcvList.Add(scpRcv); db.TB_RECEIVE.AddOrUpdate(p => p.UID,scpRcvList.ToArray()); p_list.ForEach(item => { TB_RECEIVE_DETAIL scpRcvDetail = new TB_RECEIVE_DETAIL(); scpRcvDetail.RecvBillNum = SCP_BILLCODE_CONTROLLER.MakeReviceCode_ASN();//收货单号 scpRcvDetail.PoBillNum = item.PoBillNum;//采购单 scpRcvDetail.PoLine = item.PoLine;//订单行 scpRcvDetail.PartCode = item.PartCode;//零件号 scpRcvDetail.Batch = item.Batch;//批次 scpRcvDetail.VendBatch = item.VendBatch;//供应商批号 scpRcvDetail.PoUnit = item.PoUnit;//采购单位 scpRcvDetail.LocUnit = item.LocUnit;//基本单位 scpRcvDetail.Qty = item.Qty;//收货总量 //scpRcvDetail.DockCode = "";//收货口 scpRcvDetail.State = (int)FormState.开放;// (int)FormState.关闭, scpRcvDetail.Remark = item.Remark; scpRcvDetail.CreateTime = DateTime.Now;//创建日期 scpRcvDetail.CreateUser = currentuser;//创建用户 scpRcvDetail.IsDeleted = false; scpRcvDetail.GUID = System.Guid.NewGuid(); scpRcvDetail.BillType = 0; scpRcveDetailList.Add(scpRcvDetail); }); db.TB_RECEIVE_DETAIL.AddOrUpdate(p => p.UID,scpRcveDetailList.ToArray()); } 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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE", 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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE", 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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE", 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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static void Get_V_TB_RECEIVE_DETAIL_List(V_TB_RECEIVE_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_RECEIVE_DETAIL; if (!string.IsNullOrEmpty(p_entity.RecvBillNum)) { q = q.Where(p => p.RecvBillNum == p_entity.RecvBillNum); } 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 (!string.IsNullOrEmpty(p_entity.Batch)) { q = q.Where(p => p.Batch.Contains(p_entity.Batch)); } if (p_entity.Qty != 0) { q = q.Where(p => p.Qty == p_entity.Qty); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (!string.IsNullOrEmpty(p_entity.OperName)) { q = q.Where(p => p.OperName.Contains(p_entity.OperName)); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (p_entity.State != 0) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } //if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.Address)); //} _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_DETAIL_List", e.Message); throw e; } } public static ResultObject Save_TB_RECEIVE_STATE(List p_list, ReceiveState p_state, string userName) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_state == ReceiveState.Check) { var _ls = db.V_TB_RECEIVE.Where(p => p_list.Contains(p.RecvBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)ReceiveState.Receive); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.OperName = userName; p.State = (int)ReceiveState.Check; var _list = db.TB_RECEIVE_DETAIL.Where(itm => itm.RecvBillNum == p.RecvBillNum).ToList(); _list.ForEach(itm => itm.State = (int)ReceiveState.Check); db.TB_RECEIVE_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 收货审核提醒供应商提起发票申请 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.RecvBillNum; _item.ITEM_CONTENT = string.Format("收货单号{0}发货单号{1},已收货!请申请发票", p.RecvBillNum, _ls[0].AsnBillNum); _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.PO_AUDIT_PASS; _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_Desc; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "选择的记录,有不是收货状态记录!"; } if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } if (p_state == ReceiveState.Reject) { var _ls = db.V_TB_RECEIVE.Where(p => p_list.Contains(p.RecvBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)ReceiveState.Receive); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.OperName = userName; p.State = (int)ReceiveState.Reject; p.IsDeleted = true; var _list = db.TB_RECEIVE_DETAIL.Where(itm => itm.RecvBillNum == p.RecvBillNum).ToList(); _list.ForEach(itm => { itm.State = (int)ReceiveState.Reject; itm.IsDeleted = true; } ); db.TB_RECEIVE_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); } ); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE_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_RECIVECE_CONTROLLER), "Save_TB_RECEIVE_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static List Get_V_TB_RECEIVE_LIST(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { p_list.ForEach(itm => { //var _sum= db.TB_INVOICE_DETAIL.Where(p => p.RecvBillNum == itm.RecvBillNum && p.PartCode == itm.PartCode && p.Batch==itm.Batch && p.IsDeleted==false).Select(p=>p.Qty).Sum(); var _sum = db.TB_INVOICE_DETAIL.Where(p => p.RecvBillNum == itm.RecvBillNum && p.PartCode == itm.PartCode && p.PoBillNum==itm.PoBillNum && p.PoLineNum==itm.PoLine && p.Batch == itm.Batch && p.IsDeleted == false && p.Site==itm.Site && p.SubSite==itm.SubSite). ToArray().Sum(p => new decimal?(p.Qty)).GetValueOrDefault(); itm.CanQty = itm.Qty - _sum; }); } return p_list; } public static List Get_V_TB_INVOICE_LIST(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { p_list.ForEach(itm => { try { var _recSum = db.V_TB_RECEIVE_DETAIL.Where(p => p.RecvBillNum == itm.RecvBillNum && p.PartCode == itm.PartCode && p.Batch == itm.Batch && p.IsDeleted == false).Select(p => p.Qty).Sum(); if (itm.RecvBillNum.Substring(0, 1) == "R") { _recSum = 0 - Math.Abs(_recSum); } var _sum = db.V_TB_INVOICE_DETAIL.Where(p => p.RecvBillNum == itm.RecvBillNum && p.PartCode == itm.PartCode && p.Batch == itm.Batch && p.IsDeleted == false).Select(p => p.Qty).Sum(); if (_recSum - _sum <= 0) { itm.CanQty = 0; } else { itm.CanQty = _recSum - _sum; } } catch { itm.CanQty = 0; } }); } return p_list; } public static DataTable Get_TB_RECEIVE_PLAN(V_TB_ARRIVE_DETAIL p_plan, string p_date,int p_State=1) { string _sql = string.Empty; if (p_State == 1) { _sql = "SELECT D.PROJECTID,b.VENDNAME, c.* FROM(SELECT partcode,PartDesc1, Qty, CONVERT(varchar(100), CreateTime, 23) as endtime,PoUnit,SubSite,VendId FROM V_TB_ARRIVE_DETAIL where BillType_DESC='到货单'\n" + " ) A\n" + " pivot(sum(Qty) for a.endtime in ( {1}\n" + "\n" + " )\n" + " ) as C left join TA_VENDER B ON C.VENDID=B.VENDID LEFT JOIN TA_PART D ON C.PARTCODE=D.PARTCODE where {0};"; } if (p_State == 2) { _sql = "SELECT D.PROJECTID,b.VENDNAME, c.* FROM(SELECT partcode,PartDesc1, Qty, CONVERT(varchar(100), CreateTime, 23) as endtime,PoUnit,SubSite,VendId FROM V_TB_ARRIVE_DETAIL where BillType_DESC='退货单'\n" + " ) A\n" + " pivot(sum(Qty) for a.endtime in ( {1}\n" + "\n" + " )\n" + " ) as C left join TA_VENDER B ON C.VENDID=B.VENDID LEFT JOIN TA_PART D ON C.PARTCODE=D.PARTCODE where {0};"; } string _sqlDate = "select '[' + convert(varchar(10), DATEADD(day, number, CAST('{0}' as datetime)), 23) + ']' endtime\n" + " from master.dbo.spt_values\n" + " where type = 'p'\n" + " AND number<= DATEDIFF(day, CAST('{0}' as datetime), DATEADD(DAY, -1, DATEADD(MM, DATEDIFF(MM, 0, CAST('{0}' as datetime)) + 1, 0)))"; DataTable dt = new DataTable(); DataTable _tbDate = new DataTable(); var dbSetting = GlobalConfig.ScpDatabase; SqlConnection conn = new System.Data.SqlClient.SqlConnection(); try { var strConn = EntitiesFactory.GetEfConnectionString(dbSetting); conn.ConnectionString = strConn; if (conn.State != ConnectionState.Open) { conn.Open(); } SqlCommand cmdDate = new SqlCommand(); cmdDate.Connection = conn; cmdDate.CommandText = string.Format(_sqlDate, p_date); SqlDataAdapter adapter1 = new SqlDataAdapter(cmdDate); adapter1.Fill(_tbDate); List _dateList = new List(); foreach (DataRow row in _tbDate.Rows) { _dateList.Add(row["endtime"].ToString()); } SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; string where1 = " 1=1 "; if (!string.IsNullOrEmpty(p_plan.PartCode)) { where1 += string.Format(" and C.partcode='{0}' ", p_plan.PartCode); } if (!string.IsNullOrEmpty(p_plan.VendId)) { where1 += string.Format(" and C.VendId='{0}' ", p_plan.VendId); } if (!string.IsNullOrEmpty(p_plan.ProjectId)) { where1 += string.Format(" and ProjectId like '{0}%' ", p_plan.ProjectId); } if (!string.IsNullOrEmpty(p_plan.SubSite)) { where1 += string.Format(" and C.SUBSITE = '{0}' ", p_plan.SubSite); } //if (!string.IsNullOrEmpty(p_plan.PoBillNum)) //{ // where1 += string.Format(" and PoBillNum='{0}' ", p_plan.PoBillNum); //} //if (p_plan.EndTime_Begin != null) //{ // where1 += string.Format(" and EndTime>='{0}'", p_plan.EndTime_Begin.Value.ToString("yyyy-MM-dd")); //} //else //{ // where1 += string.Format(" and EndTime>='{0}'", DateTime.Now.ToString("yyyy-MM-dd")); //} //if (p_plan.EndTime_End != null) //{ // where1 += string.Format(" and EndTime<='{0}'", p_plan.EndTime_End.Value.ToString("yyyy-MM-dd")); //} //else //{ // where1 += string.Format(" and EndTime<='{0}'", DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd")); //} cmd.CommandText = string.Format(_sql, where1, string.Join(",", _dateList.ToArray())); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dt); dt.Columns.Add("合计"); dt.Columns["PartCode"].ColumnName = "零件编号"; dt.Columns["VendId"].ColumnName = "供应商编号"; dt.Columns["VendName"].ColumnName = "供应商名称"; dt.Columns["PartDesc1"].ColumnName = "零件名称"; dt.Columns["SubSite"].ColumnName = "分厂地点"; dt.Columns["PoUnit"].ColumnName = "单位"; dt.Columns["Projectid"].ColumnName = "项目编号"; foreach (DataRow row in dt.Rows) { decimal num = 0; foreach (DataColumn column in dt.Columns) { var _reg = Regex.Match(column.ColumnName, @"\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}"); if (!string.IsNullOrEmpty(_reg.Value)) { if (row[column.ColumnName] != null && !string.IsNullOrEmpty(row[column.ColumnName].ToString())) { num += decimal.Parse(row[column.ColumnName].ToString()); } } } row["合计"] = num; } conn.Close(); } catch (SqlException ex) { conn.Close(); throw new Exception($"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}"); } return dt; } public static ResultObject> Get_V_TB_RECEIVE_LIST_List(V_TB_RECEIVE_LIST p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_RECEIVE_LIST; if (!string.IsNullOrEmpty(p_entity.ProjectId)) { q = q.Where(p => p.ProjectId.Contains(p_entity.ProjectId)); } if (!string.IsNullOrEmpty(p_entity.RecvBillNum)) { q = q.Where(p => p.RecvBillNum == p_entity.RecvBillNum); } 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 (!string.IsNullOrEmpty(p_entity.Batch)) { q = q.Where(p => p.Batch.Contains(p_entity.Batch)); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (p_entity.BeginTime != null) { q = q.Where(p => p.ShipTime >= p_entity.BeginTime); } if (p_entity.Site != null) { q = q.Where(p => p.Site == p_entity.Site); } if (p_entity.SubSite != null) { q = q.Where(p => p.SubSite == p_entity.SubSite); } if (p_entity.EndTime != null) { q = q.Where(p => p.ShipTime <= p_entity.EndTime); } if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0) { q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendId)); } if (p_entity.UIDList != null && p_entity.UIDList.Count > 0) { q = q.Where(p => p_entity.UIDList.Contains(p.UID)); } 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)); } _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_List", e.Message); throw e; } return _ret; } public static ResultObject> Get_V_TB_RECEIVE_LIST_PRICE(string p_vendId,List p_list, Action>> p_action) { var _ls = p_list.Select(itm => itm.PartCode); StringBuilder _buffer = new StringBuilder(); _buffer.Append(" SELECT * FROM TB_PRICE WHERE UID IN"); _buffer.Append("( SELECT MAX(UID) AS UID FROM TB_PRICE AS A"); _buffer.AppendFormat(" where A.VendId='{0}' AND A.PartCode IN ('{1}') ",p_vendId,String.Join("','",_ls)); _buffer.Append(" GROUP BY VendId, Site,SubSite, PartCode, StartTime, EndTime)"); ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.Database.SqlQuery(_buffer.ToString()).AsQueryable(); _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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_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_RECIVECE_CONTROLLER), "Get_V_TB_RECEIVE_LIST_List", e.Message); throw e; } return _ret; } public static void Get_V_TB_ARRIVE_List(V_TB_ARRIVE p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ARRIVE; if (p_entity.BeginTime != null) { q = q.Where(p => p.ShipTime >= p_entity.BeginTime); } if (p_entity.EndTime != null) { q = q.Where(p => p.ShipTime <= p_entity.EndTime); } if (!string.IsNullOrEmpty(p_entity.ArrvBillNum)) { q = q.Where(p => p.ArrvBillNum==p_entity.ArrvBillNum); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum==p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum==p_entity.AsnBillNum); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum == p_entity.AskBillNum); } 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.SubSite==p_entity.SubSite); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId==p_entity.VendId); } if (!string.IsNullOrEmpty(p_entity.BillType_DESC)) { q = q.Where(p => p.BillType_DESC == p_entity.BillType_DESC); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.OperName)) { q = q.Where(p => p.OperName.Contains(p_entity.OperName)); } if (!string.IsNullOrEmpty(p_entity.Site_Desc)) { q = q.Where(p => p.Site_Desc.Contains(p_entity.Site_Desc)); } 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.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.Address)); //} _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_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; //Console.WriteLine(inner?.StateEntries[0].EntitySet.Name); 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_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_List", e.Message); throw e; } } public static ResultObject Save_TB_ARRIVE_STATE(List p_list, ReceiveState p_state, string userName) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ls = db.V_TB_ARRIVE.Where(p => p_list.Contains(p.ArrvBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)ReceiveState.Receive); if (count == _ls.Count && _ls.Count > 0) { _ls.ForEach(p => { p.OperName = userName; p.State = (int)ReceiveState.Check; var _list = db.TB_ARRIVE_DETAIL.Where(itm => itm.ArrvBillNum == p.ArrvBillNum).ToList(); _list.ForEach(itm => itm.State = (int)ReceiveState.Check); db.TB_ARRIVE_DETAIL.AddOrUpdate(itm => itm.UID, _list.ToArray()); #region 收货审核提醒供应商提起发票申请 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = p.ArrvBillNum; _item.ITEM_CONTENT = string.Format("到货单号{0}发货单号{1},已到货!请申请发票", p.ArrvBillNum, _ls[0].AsnBillNum); _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.PO_AUDIT_PASS; _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_Desc; _item.ITEM_ADDRESS = p.Site; _item.GUID = Guid.NewGuid(); db.TB_PENING_ITEMS.Add(_item); #endregion } ); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _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_RECIVECE_CONTROLLER), "Save_TB_ARRIVE_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Save_TB_ARRIVE_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Save_TB_ARRIVE_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; //Console.WriteLine(inner?.StateEntries[0].EntitySet.Name); 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_RECIVECE_CONTROLLER), "Save_TB_ARRIVE_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static void GetNumber(string p_asnBillNum) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = db.TB_ASN.Where(p => p.AsnBillNum == p_asnBillNum && p.IsDeleted == false).ToList(); if (_list.Count > 0) { _list.ForEach(itm => itm.State = (int)AsnState.Receive); db.TB_ASN.AddOrUpdate(p => p.UID, _list.ToArray()); var _entity = _list.FirstOrDefault(); var _asnList = db.TB_ASN.Where(p => p.AskBillNum == _entity.AskBillNum && p.IsDeleted == false); var _count = _asnList.Count(p => p.State == (int)AsnState.Receive); if (_asnList.Count() == _count) { var _askList = db.TB_ASK.Where(p => p.AskBillNum == _entity.AskBillNum && p.IsDeleted == false).ToList(); _askList.ForEach(itm => itm.State = (int)AskState.Receive); db.TB_ASK.AddOrUpdate(p => p.UID, _askList.ToArray()); } } } } public static void Get_V_TB_ARRIVE_DETAIL_List(V_TB_ARRIVE_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ARRIVE_DETAIL; if (!string.IsNullOrEmpty(p_entity.ArrvBillNum)) { q = q.Where(p => p.ArrvBillNum == p_entity.ArrvBillNum); } 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 (!string.IsNullOrEmpty(p_entity.Batch)) { q = q.Where(p => p.Batch.Contains(p_entity.Batch)); } if (p_entity.Qty != 0) { q = q.Where(p => p.Qty == p_entity.Qty); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (!string.IsNullOrEmpty(p_entity.Remark)) { q = q.Where(p => p.Remark.Contains(p_entity.Remark)); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.SubSite)) { q = q.Where(p => p.SubSite.Contains(p_entity.SubSite)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (!string.IsNullOrEmpty(p_entity.OperName)) { q = q.Where(p => p.OperName.Contains(p_entity.OperName)); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } _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_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_DETAIL_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_DETAIL_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { Console.WriteLine(ex.ToString()); _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_DETAIL_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; //Console.WriteLine(inner?.StateEntries[0].EntitySet.Name); 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_RECIVECE_CONTROLLER), "Get_V_TB_ARRIVE_DETAIL_List", e.Message); throw e; } } public static List COMPARE_DETAIL_SUM(string p_number) { List _ls = new List(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list=db.V_TB_PO_DETAIL.Where(p => p.PoBillNum == p_number).ToList(); if (_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_PO_DETAIL _detail in _list) { //var _ask = db.TB_ASK_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AskSum = p.Sum(itm => itm.AskQty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var rec = db.TB_RECEIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.BillType == 0).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { ReceiveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var reject = db.TB_RECEIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.BillType == 1).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { RejectSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var asn = db.TB_ASN_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.State == (int)AsnState.Ship).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AsSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); //var arrive = db.TB_ARRIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.BillType == (int)ArriveBillType.Receive).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); _detail.PartDesc1 = _detail.PartDesc1 + _detail.PartDesc2; rec.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ReceivedQty = itm.ReceiveSum; } }); reject.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.RejectQty = itm.RejectSum.ToString(); } }); asn.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ShippedQty = itm.AsSum; } }); //_ask.ToList().ForEach(itm => //{ // if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) // { // _detail.TempQty = itm.AskSum; // } //}); //arrive.ToList().ForEach(itm => //{ // if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) // { // _detail.ArriveQty = itm.ArriveSum.ToString(); // } //}); } } _ls=_list; } return _ls; } public static List COMPARE_PO(string p_number) { List _ls = new List(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = db.V_TB_PO_DETAIL.Where(p => p.PoBillNum == p_number).ToList(); if (_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_PO_DETAIL _detail in _list) { //var _ask = db.TB_ASK_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AskSum = p.Sum(itm => itm.AskQty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var rec = db.TB_RECEIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.BillType == 0).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { ReceiveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var reject = db.TB_RECEIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.BillType == 1).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { RejectSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); var asn = db.TB_ASN_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.State == (int)AsnState.Ship).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AsSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); //var arrive = db.TB_ARRIVE_DETAIL.Where(p => p.PoBillNum == _detail.PoBillNum && p.IsDeleted == false && p.State == (int)ArriveState.OK && p.BillType == (int)ArriveBillType.Receive).GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { ArriveSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode }); _detail.PartDesc1 = _detail.PartDesc1 + _detail.PartDesc2; rec.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ReceivedQty = itm.ReceiveSum; } }); reject.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.RejectQty = itm.RejectSum.ToString(); } }); asn.ToList().ForEach(itm => { if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) { _detail.ShippedQty = itm.AsSum; } }); //_ask.ToList().ForEach(itm => //{ // if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) // { // _detail.TempQty = itm.AskSum; // } //}); //arrive.ToList().ForEach(itm => //{ // if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) // { // _detail.ArriveQty = itm.ArriveSum.ToString(); // } //}); } } _ls = _list; } return _ls; } public static ResultObject Save_Reject(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ls = p_list.Select(p => p.UID).ToList(); var _list=db.TB_RECEIVE_DETAIL.Where(p => _ls.Contains(p.UID)).ToList(); _list.ForEach(p => { var _entity=p_list.Where(itm => itm.UID == p.UID).FirstOrDefault(); if (_entity != null) { p.Remark = _entity.Remark; //p.AsnBillNum = _entity.AsnBillNum; } }); db.TB_RECEIVE_DETAIL.AddOrUpdate(_list.ToArray()); 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_ASN_DETAIL(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { p_list.ForEach(itm => { var _sum= db.V_TB_RECEIVE_DETAIL.Where(p => p.AsnBillNum == itm.AsnBillNum && p.PartCode == itm.PartCode && p.Batch == itm.Batch && p.IsDeleted == false&&p.VendBatch==itm.VendBatch&&p.BillType_DESC== "退货单").Select(p => p.Qty).DefaultIfEmpty().Sum(); itm.CanRejectQty = itm.Qty - _sum; }); } return p_list; } /// /// 数据导入 /// /// /// /// /// /// /// public static ResultObject EXCEL_RECEIVE(List p_order_list, string p_creator, string p_buyer, string p_site, DateTime p_time) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); p_order_list.ForEach (p => { var _ls = CheckExcel(db, p); if (_ls.Count > 0) { lineError.Add(number.ToString()); ErrorList.Add(string.Join("
", _ls.ToArray())); } number++; }); if (lineError.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.MessageList.AddRange(ErrorList); _ret.Result = false; } else { var GroupList = p_order_list.GroupBy(p => new { p.采购订单号, p.行号, p.零件编号, p.价格, p.供应商编码, p.单位 }).Select(p => new { 数量 = p.Sum(itm => decimal.Parse(itm.数量)), 行号 = p.Key.行号, 采购订单号 = p.Key.采购订单号, p.Key.零件编号, p.Key.价格, p.Key.供应商编码, p.Key.单位 }).ToList(); var OrderList = GroupList.GroupBy(p => p.采购订单号).ToList(); OrderList.ForEach((p) => { var list_distinct = p.GroupBy(t => new { t.零件编号, t.行号 }).ToList(); var list = p.ToList(); if (list_distinct.Count < list.Count) { _ret.MessageList.Add("相同组号,存在同一编号、行号的数据"); return; } if (list.Count > 0) { var _entity = list.FirstOrDefault(); TB_ASK _ask = new TB_ASK(); _ask.AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(); _ask.State = (int)AskState.Receive; _ask.PoBillNum = _entity.采购订单号.ToUpper(); _ask.ModType = (int)BillModType.Contract; _ask.Site = p_site; _ask.BeginTime = p_time; _ask.Buyer = p_creator; _ask.BuyerPhone = ""; _ask.ErpBillNum = _entity.采购订单号.ToUpper(); _ask.EndTime = p_time; _ask.CreateTime = p_time; _ask.CreateUser = p_creator; _ask.VendId = _entity.供应商编码.ToUpper(); _ask.GUID = Guid.NewGuid(); _ask.IsDeleted = false; db.TB_ASK.Add(_ask); TB_ASN _asn = new TB_ASN(); _asn.AsnBillNum = SCP_BILLCODE_CONTROLLER.MakeASNCode(); _asn.AskBillNum = _ask.AskBillNum; _asn.VendId = _entity.供应商编码.ToUpper(); ; _asn.State = (int)AsnState.Receive; _asn.PoBillNum = _entity.采购订单号.ToUpper(); _asn.Site = p_site; _asn.CreateTime = p_time; _asn.CreateUser = p_creator; _asn.UpdateTime = p_time; _asn.UpdateUser = p_creator; _asn.ShipTime = p_time; _asn.ShipUser = p_creator; _asn.ReceiveTime = p_time; _asn.ReceiveUser = p_creator; _asn.GUID = Guid.NewGuid(); _asn.IsDeleted = false; db.TB_ASN.Add(_asn); list.ForEach((itm) => { TB_ASK_DETAIL _t_ASK_Detail = new TB_ASK_DETAIL(); _t_ASK_Detail.PoLine = Int32.Parse(itm.行号); _t_ASK_Detail.Currency = ""; _t_ASK_Detail.AskBillNum = _ask.AskBillNum; _t_ASK_Detail.PoBillNum = _ask.PoBillNum; _t_ASK_Detail.PoUnit = itm.单位; _t_ASK_Detail.CreateTime = DateTime.Now; _t_ASK_Detail.CreateUser = _ask.CreateUser; _t_ASK_Detail.PartCode = itm.零件编号.ToUpper(); _t_ASK_Detail.BeginTime = p_time; _t_ASK_Detail.EndTime = p_time; _t_ASK_Detail.TempQty = itm.数量; _t_ASK_Detail.AskQty = itm.数量; _t_ASK_Detail.IsDeleted = false; _t_ASK_Detail.LocUnit = itm.单位; _t_ASK_Detail.UnConv = 1; _t_ASK_Detail.State = _ask.State; _t_ASK_Detail.Price = string.IsNullOrEmpty(itm.价格) ? 0 : decimal.Parse(itm.价格); _t_ASK_Detail.GUID = Guid.NewGuid(); db.TB_ASK_DETAIL.Add(_t_ASK_Detail); TB_ASN_DETAIL _t_ASN_Detail = new TB_ASN_DETAIL(); _t_ASN_Detail.AsnBillNum = _asn.AsnBillNum; _t_ASN_Detail.PoLine = Int32.Parse(itm.行号); _t_ASN_Detail.Currency = ""; _t_ASN_Detail.PoBillNum = _asn.PoBillNum; _t_ASN_Detail.PoUnit = itm.单位; _t_ASN_Detail.CreateTime = DateTime.Now; _t_ASN_Detail.CreateUser = _asn.CreateUser; _t_ASN_Detail.PartCode = itm.零件编号.ToUpper(); _t_ASN_Detail.Batch = p_time.ToString("yyyyMMdd"); _t_ASN_Detail.VendBatch = p_time.ToString("yyyyMMdd"); _t_ASN_Detail.Qty = itm.数量; _t_ASN_Detail.State = (int)AsnState.Receive; _t_ASN_Detail.CreateTime = p_time; _t_ASN_Detail.CreateUser = p_creator; _t_ASN_Detail.IsDeleted = false; _t_ASN_Detail.GUID = Guid.NewGuid(); _t_ASN_Detail.PoUnit = itm.单位; _t_ASN_Detail.ProduceDate = p_time; db.TB_ASN_DETAIL.Add(_t_ASN_Detail); var _reclist = p_order_list.Where(t => t.零件编号 == itm.零件编号 && t.采购订单号 == itm.采购订单号 && t.行号 == itm.行号).GroupBy(t => t.收货单号).ToList(); _reclist.ForEach((q) => { var reclist = q.FirstOrDefault(); TB_RECEIVE _rec = new TB_RECEIVE(); _rec.RecvBillNum = reclist.收货单号; _rec.State = (int)FormState.开放; _rec.Remark = _ask.AskBillNum; _rec.CreateTime = p_time; _rec.CreateUser = p_creator; _rec.IsDeleted = false; _rec.GUID = System.Guid.NewGuid(); _rec.BillType = 0; _rec.PoBillNum = reclist.采购订单号.ToUpper(); _rec.AsnBillNum = reclist.采购订单号.ToUpper(); _rec.Site = p_site; _rec.VendId = reclist.供应商编码.ToUpper(); _rec.ShipTime = p_time; db.TB_RECEIVE.Add(_rec); var _recdetaillist = p_order_list.Where(f => f.收货单号 == reclist.收货单号).ToList(); _recdetaillist.ForEach((n) => { TB_RECEIVE_DETAIL _t_RECEIVE_Detail = new TB_RECEIVE_DETAIL(); _t_RECEIVE_Detail.RecvBillNum = n.收货单号; _t_RECEIVE_Detail.PoBillNum = n.采购订单号.ToUpper(); _t_RECEIVE_Detail.PoLine = Int32.Parse(n.行号); _t_RECEIVE_Detail.PartCode = n.零件编号.ToUpper(); _t_RECEIVE_Detail.Batch = n.批次; _t_RECEIVE_Detail.VendBatch = n.供应商批次; _t_RECEIVE_Detail.PoUnit = n.单位; _t_RECEIVE_Detail.LocUnit = ""; _t_RECEIVE_Detail.Qty = decimal.Parse(n.数量); _t_RECEIVE_Detail.State = (int)FormState.开放; _t_RECEIVE_Detail.Remark = _ask.AskBillNum; _t_RECEIVE_Detail.CreateTime = p_time; _t_RECEIVE_Detail.CreateUser = p_creator; _t_RECEIVE_Detail.IsDeleted = false; _t_RECEIVE_Detail.GUID = System.Guid.NewGuid(); _t_RECEIVE_Detail.BillType = 0; db.TB_RECEIVE_DETAIL.Add(_t_RECEIVE_Detail); }); }); }); } else { _ret.MessageList.Add("请检查数据后,重新导入!"); return; } if (_ret.MessageList.Count == 0) { int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } 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_RECIVECE_CONTROLLER), "EXCEL_RECEIVE", 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_RECIVECE_CONTROLLER), "EXCEL_RECEIVE", 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_RECIVECE_CONTROLLER), "EXCEL_RECEIVE", 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_RECIVECE_CONTROLLER), "EXCEL_RECEIVE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } private static List CheckExcel(ScpEntities db, SCP_ASN_EXCEL p_excel) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.采购订单号)) { int count = db.TB_PO_DETAIL.Count(p => p.PoBillNum == p_excel.采购订单号 && p.PartCode == p_excel.零件编号 && p.PoLine.ToString() == p_excel.行号); if (count == 0) { ErrorList.Add(string.Format("订单编号{0}零件号{1}行号{2}不存在!", p_excel.采购订单号, p_excel.零件编号, p_excel.行号)); } } else { ErrorList.Add(string.Format("订单编号为空!")); } if ( string.IsNullOrEmpty(p_excel.收货单号) || string.IsNullOrEmpty(p_excel.采购订单号) || string.IsNullOrEmpty(p_excel.供应商编码) || string.IsNullOrEmpty(p_excel.价格) || string.IsNullOrEmpty(p_excel.零件编号) || string.IsNullOrEmpty(p_excel.行号) || string.IsNullOrEmpty(p_excel.批次) || string.IsNullOrEmpty(p_excel.数量) || string.IsNullOrEmpty(p_excel.单位) || string.IsNullOrEmpty(p_excel.价格)) { ErrorList.Add(string.Format("收货单号【{1}】零件号【{0}】有填写为空!", p_excel.零件编号, p_excel.收货单号)); } return ErrorList; } } }