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.Models.ScpEntity.ExcelImportEntity; using CK.SCP.Utils; using System.Data.Entity.Core; using CK.SCP.Models.ScpEntity.ExcelExportEnttity; using System.Text.RegularExpressions; namespace CK.SCP.Controller { public class SCP_PO_CONTROLLER { public static void Get_V_TB_PO_List(V_TB_PO p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_PO; 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 == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.ErpBillNum)) { q = q.Where(p => p.ErpBillNum==p_entity.ErpBillNum); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (p_entity.ModType != null) { q = q.Where(p => p.ModType == p_entity.ModType); } if (!string.IsNullOrEmpty(p_entity.Contacter)) { q = q.Where(p => p.Contacter.Contains(p_entity.Contacter)); } 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 (p_entity.State != null) { 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 (!string.IsNullOrEmpty(p_entity.UpdateUser)) { q = q.Where(p => p.UpdateUser.Contains(p_entity.UpdateUser)); } if (!string.IsNullOrEmpty(p_entity.UpdateInfo)) { q = q.Where(p => p.UpdateInfo.Contains(p_entity.UpdateInfo)); } 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 (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 (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.Site)); } if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0) { q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendId)); } if (!string.IsNullOrEmpty(p_entity.SubSite)) { q = q.Where(p => p.SubSite.Contains(p_entity.SubSite)); } if (!string.IsNullOrEmpty(p_entity.Extend1)) { q = q.Where(p => p.Extend1.Contains(p_entity.Extend1)); } if (!string.IsNullOrEmpty(p_entity.Extend2)) { q = q.Where(p => p.Extend2.Contains(p_entity.Extend2)); } if (!string.IsNullOrEmpty(p_entity.Extend3)) { q = q.Where(p => p.Extend3.Contains(p_entity.Extend3)); } //if (!string.IsNullOrEmpty(p_entity.PartCode)) //{ // var _po = db.TB_PO_DETAIL.Where(p => p.PartCode == p_entity.PartCode && p.IsDeleted== false); // if (_po.Count() > 0) // { // List _list = new List(); // _po.ToList().ForEach(itm => // { // _list.Add(itm.PoBillNum); // }); // if (_list.Count() > 0) // { // q = q.Where(p => _list.Contains(p.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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_List", e.Message); throw e; } } 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]; 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 }); p_list.ForEach(p => { _ask.ToList().ForEach(itm => { if (p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.PartCode == itm.PartCode) { p.TempQty = itm.AskSum; } }); }); } } return p_list; } public static List LOAD_PO_DETAIL_SUM(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TB_PO_DETAIL _detail in p_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.State == (int)ReceiveState.Check && 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.State == (int)RejectState.Check && 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 || p.State == (int)AsnState.Receive)) .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(); } }); } } } return p_list; } public static ResultObject Save_TB_PO_STATE(List p_list, PoState p_state) { ResultObject _ret = new ResultObject(); //try //{ // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // if (db.TB_ASN.Where(p => p_list.Contains(p.PoBillNum)).Count() == 0) // { // if(p_state==PoState.Reject) // { // var _askDetailList = db.TB_ASK_DETAIL.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // db.TB_ASK_DETAIL.RemoveRange(_askDetailList);//移除要货看板明细 // var _ls = db.TB_ASK.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // db.TB_ASK.RemoveRange(_ls.ToArray());//移除要货看板 // var _lsPending = db.TB_PENING_ITEMS.Where(p => p_list.Contains(p.ITEM_NO)).ToList(); // db.TB_PENING_ITEMS.RemoveRange(_lsPending.ToArray());//移除代办 // var _list = db.TB_PO.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // int count = _ls.Count(p => p.State == (int)AskState.New || p.State == (int)AskState.Release); // if (_list.Count > 0 && _list.Count == count) // { // _list.ForEach(p => // { // p.State = (int)PoState.Reject; // p.IsDeleted = true; // var _poList = db.TB_PO_DETAIL.Where(itm => itm.PoBillNum == p.PoBillNum).ToList(); // _poList.ForEach(itm => // { // itm.State = (int)PoState.Reject; // itm.IsDeleted = true; // }); // db.TB_PO_DETAIL.AddOrUpdate(itm => itm.UID, _poList.ToArray()); // } // ); // db.TB_PO.AddOrUpdate(p => p.PoBillNum, _list.ToArray()); // } // else // { // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建或发布状态记录!"; // } // } // if (p_state == PoState.Complete) // { // var _list = db.TB_PO.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // _list.ForEach(p => // { // p.State = (int)PoState.Complete; // p.IsDeleted = true; // var _poList = db.TB_PO_DETAIL.Where(itm => itm.PoBillNum == p.PoBillNum).ToList(); // _poList.ForEach(itm => // { // itm.State = (int)PoState.Complete; // }); // db.TB_PO_DETAIL.AddOrUpdate(itm => itm.UID, _poList.ToArray()); // } // ); // db.TB_PO.AddOrUpdate(p => p.PoBillNum, _list.ToArray()); // } // if (p_state == PoState.CompleteForce) // { // var _list = db.TB_PO.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // _list.ForEach(p => // { // p.State = (int)PoState.CompleteForce; // p.IsDeleted = true; // var _poList = db.TB_PO_DETAIL.Where(itm => itm.PoBillNum == p.PoBillNum).ToList(); // _poList.ForEach(itm => // { // itm.State = (int)PoState.CompleteForce; // }); // db.TB_PO_DETAIL.AddOrUpdate(itm => itm.UID, _poList.ToArray()); // } // ); // db.TB_PO.AddOrUpdate(p => p.PoBillNum, _list.ToArray()); // } // if (p_state == PoState.Release) // { // var _list = db.TB_PO.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // int count = _list.Count(p => p.State == (int)AskState.New); // if (_list.Count > 0 && _list.Count == count) // { // _list.ForEach(p => // { // p.State = (int)PoState.Release; // var _poList = db.TB_PO_DETAIL.Where(itm => itm.PoBillNum == p.PoBillNum).ToList(); // _poList.ForEach(itm => // { // itm.State = (int)PoState.Release; // }); // db.TB_PO_DETAIL.AddOrUpdate(itm => itm.UID, _poList.ToArray()); // } // ); // db.TB_PO.AddOrUpdate(p => p.PoBillNum, _list.ToArray()); // } // else // { // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择的记录,有不是新建状态记录!"; // } // } // if (p_state == PoState.New) // { // var _AskDetailList = db.TB_ASK_DETAIL.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // if (_AskDetailList.Count > 0) // { // db.TB_ASK_DETAIL.RemoveRange(_AskDetailList);//移除要货看板明细 // } // var _ls = db.TB_ASK.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // if (_ls.Count > 0) // { // #region 供应商要货代办作废 // var _pList = _ls.Select(p => p.AskBillNum).ToList(); // var _pendingList = db.TB_PENING_ITEMS.Where(itm => _pList.Contains(itm.ITEM_NO)).ToList(); // if (_pendingList.Count > 0) // { // db.TB_PENING_ITEMS.RemoveRange(_pendingList); // } // #endregion // db.TB_ASK.RemoveRange(_ls.ToArray());//移除要货看板 // } // var _list = db.TB_PO.Where(p => p_list.Contains(p.PoBillNum)).ToList(); // int count = _list.Count(p => p.State == (int)AskState.Release); // if (_list.Count > 0 && _list.Count == count) // { // _list.ForEach(p => // { // p.State = (int)PoState.New; // p.IsDeleted = false; // var _poList = db.TB_PO_DETAIL.Where(itm => itm.PoBillNum == p.PoBillNum).ToList(); // _poList.ForEach(itm => // { // itm.State = (int)PoState.New; // }); // db.TB_PO_DETAIL.AddOrUpdate(itm => itm.UID, _poList.ToArray()); // }); // db.TB_PO.AddOrUpdate(p => p.PoBillNum, _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 = "数据更新失败!"; // } // } // else // { // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "该订单已经存在发货单,不能取消发布!"; // } // } //} //catch (Exception e) //{ // _ret.State = ReturnStatus.Failed; // LogHelper.Writlog(LogHelper.LogType.Error, typeof(TB_PO), "Save_TB_PO", 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(p => p.PoBillNum, 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_PO_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_PO_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_PO_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_PO_CONTROLLER), "Save_TB_PO", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } private static List CheckExcelPackQty(ScpEntities db, SCP_PO_EXCEL p_excel) { List ErrorList = new List(); var _ls = db.TA_VEND_PART.Where(p => p.PartCode == p_excel.零件号 && p.VendId == p_excel.供应商编号).ToList(); if (_ls.Count != 0) { if (_ls[0].VendPackQty != 0) { if (Convert.ToDecimal(p_excel.数量) % _ls[0].VendPackQty > 0) { ErrorList.Add(string.Format("分组【{2}】零件号【{0}】行号【{1}】不是整箱!标包数为【{3}】", p_excel.零件号, p_excel.行号, p_excel.分组编号, _ls[0].VendPackQty)); } } } return ErrorList; } private static List CheckExcel(ScpEntities db, SCP_PO_EXCEL p_excel) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.QAD订单号)) { int count = db.TB_PO.Count(p => p.PoBillNum == p_excel.QAD订单号); if (count == 0) { ErrorList.Add(string.Format("订单编号{0}不存在!", p_excel.QAD订单号)); } } else { ErrorList.Add(string.Format("订单编号为空!")); } var _list = db.TB_PO_DETAIL.Where(p => p.PoBillNum == p_excel.QAD订单号 && p.PartCode == p_excel.零件号).ToList(); if ( string.IsNullOrEmpty(p_excel.分组编号) || string.IsNullOrEmpty(p_excel.QAD订单号) || 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.分组编号)); } if (_list.Count == 0) { ErrorList.Add(string.Format("分组【{2}】零件号【{0}】行号【{1}】不存在!", p_excel.零件号, p_excel.行号, p_excel.分组编号)); } else { p_excel.行号 = _list[0].PoLine.ToString(); p_excel.价格= _list[0].Price.ToString(); p_excel.单位 = _list[0].PoUnit.ToString(); p_excel.币种 = _list[0].Currency.ToString(); } return ErrorList; } private static List CheckExcelMode(ScpEntities db, SCP_PO_DETAIL_EXPORT_MODEL p_excel) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.订单编号)) { int count = db.TB_PO.Count(p => p.PoBillNum == p_excel.订单编号); if (count == 0) { ErrorList.Add(string.Format("订单编号{0}不存在!", p_excel.订单编号)); } } else { ErrorList.Add(string.Format("订单编号为空!")); } decimal _result = 0; if (!decimal.TryParse(p_excel.订单数, out _result)) { ErrorList.Add(string.Format("订单编号{0}零件编号{1}数量填写有问题", p_excel.订单编号, p_excel.零件编码)); } var _count = db.TB_PO_DETAIL.Count(p => p.PoBillNum == p_excel.订单编号 && p.PartCode == p_excel.零件编码 && p.PoLine.ToString() == p_excel.行号); 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.订单数) || string.IsNullOrEmpty(p_excel.单位)) { ErrorList.Add(string.Format("分组【{1}】零件名称【{0}】有填写为空!", p_excel.零件名, p_excel.订单编号)); } //else //{ // if (!Regex.IsMatch(p_excel.计划员电话, @"^1[1,9]\d{9}$")) // { // ErrorList.Add(string.Format("订单编号{0}零件编号{1}计划员手机号填写有问题", p_excel.订单编号, p_excel.零件编码)); // } //} if (_count == 0) { ErrorList.Add(string.Format("分组【{2}】零件名称【{0}】行号【{1}】不存在!", p_excel.零件名, p_excel.行号, p_excel.订单编号)); } return ErrorList; } /// /// 供应商零件导入验证 /// /// /// /// private static List CheckExcelMode_VenderPart(ScpEntities db, SCP_VENDER_PART_EXPORT p_excel, string site) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.供应商代码)) { int count = db.TA_VENDER.Count(p => p.VendId == p_excel.供应商代码 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("供应商{0}不存在!", p_excel.供应商代码)); } } else { ErrorList.Add(string.Format("供应商代码为空!")); } if (!string.IsNullOrEmpty(p_excel.零件号)) { int count = db.TA_PART.Count(p => p.PartCode == p_excel.零件号 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("零件编号{0}不存在!", 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.托盘包装数)) { ErrorList.Add(string.Format("供应商代码【{0}】零件编号【{1}】有填写为空!", p_excel.供应商代码, p_excel.零件号)); } return ErrorList; } /// /// 供应商导入验证 /// /// /// /// private static List CheckExcelMode_Vender(ScpEntities db, SCP_VENDER_EXPORT p_excel, string site) { List ErrorList = new List(); if ( string.IsNullOrEmpty(p_excel.供应商编号) || string.IsNullOrEmpty(p_excel.供应商名称)) { ErrorList.Add(string.Format("供应商编号【{0}】有填写为空!", p_excel.供应商编号)); } return ErrorList; } /// /// 价格导入验证 /// /// /// /// /// private static List CheckExcelMode_Price(ScpEntities db, SCP_PRICE_EXPORT p_excel, string site) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.供应商编号)) { int count = db.TA_VENDER.Count(p => p.VendId == p_excel.供应商编号 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("供应商{0}不存在!", p_excel.供应商编号)); } } else { ErrorList.Add(string.Format("供应商代码为空!")); } if (!string.IsNullOrEmpty(p_excel.零件编码)) { int count = db.TA_PART.Count(p => p.PartCode == p_excel.零件编码 && p.Site == site); if (count <= 0) { ErrorList.Add(string.Format("供应商{0}不存在!", 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.价格)) { ErrorList.Add(string.Format("零件编号【{0}】有填写为空!", p_excel.供应商编号)); } return ErrorList; } /// /// 零件导入验证 /// /// /// /// private static List CheckExcelMode_Part(ScpEntities db, SCP_PART_EXPORT p_excel, string site) { List ErrorList = new List(); //if (!string.IsNullOrEmpty(p_excel.零件编号)) //{ // int count = db.TA_PART.Count(p => p.PartCode == p_excel.零件编号&&p.Site== site); // if (count > 0) // { // ErrorList.Add(string.Format("零件编号{0}已经存在!", 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.项目编号)) { ErrorList.Add(string.Format("零件编号【{0}】有填写为空!", p_excel.零件编号)); } return ErrorList; } /// /// 订单导入检查 /// /// /// /// private static List CheckExcelMode_Po(ScpEntities db, SCP_PO_EXPORT_IMPORT_MODEL p_excel) { List ErrorList = new List(); if (!string.IsNullOrEmpty(p_excel.订单编号)) { int count = db.TB_PO.Count(p => p.PoBillNum == p_excel.订单编号); if (count > 0) { ErrorList.Add(string.Format("订单编号{0}已经存在!", p_excel.订单编号)); } } else { ErrorList.Add(string.Format("订单编号为空!")); } decimal _result = 0; if (!decimal.TryParse(p_excel.数量, out _result)) { ErrorList.Add(string.Format("订单编号{0}零件编号{1}数量填写有问题", p_excel.订单编号, p_excel.数量)); } //var _count = db.TA_PART.Count(p => p.PartCode == p_excel.零件号); 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.单价) || string.IsNullOrEmpty(p_excel.订单创建时间)) { ErrorList.Add(string.Format("分组【{1}】零件名称【{0}】有填写为空!", p_excel.零件号, p_excel.订单编号)); } //if (_count == 0) //{ // ErrorList.Add(string.Format("分组【{2}】零件名称【{0}】行号【{1}】不存在!", p_excel.零件号, p_excel.行号, p_excel.订单编号)); //} return ErrorList; } /// /// 北京安通林 /// /// /// /// /// /// /// public static ResultObject EXCEL_PO_TO_ASK_MOD(List p_order_list, string p_creator, string p_buyer, DateTime p_time, BillModType p_modtype, bool IsAutoPublish = false) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { p_order_list.ForEach(p => { if (string.IsNullOrEmpty(p.订单数)) { p.订单数 = "0"; } }); int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list.Where(p => p.订单数 != "0").ToList(); _lst.ForEach (p => { var _ls = CheckExcelMode(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 VenderList = _lst.Select(p => p.供应商编码).Distinct(); var PartCodeList = _lst.Select(p => p.零件编码).Distinct(); var OrderList = _lst.GroupBy(p => p.订单编号).ToList(); OrderList.ForEach((p) => { var list = p.ToList(); if (list.Count > 0) { var _entity = list.FirstOrDefault(); TB_ASK _ask = new TB_ASK(); _ask.AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(); _ask.VendId = _entity.供应商编码; _ask.State = (int)AskState.New; _ask.PoBillNum = _entity.订单编号; _ask.ModType = (int)BillModType.Contract; _ask.Site = _entity.域名; _ask.Remark = _entity.站点; if (IsAutoPublish) { _ask.State = (int)AskState.Release; } else { _ask.State = (int)AskState.New; } _ask.BeginTime = DateTime.Parse(_entity.订货日期); _ask.Buyer = p_buyer; _ask.BuyerPhone = _entity.计划员电话; _ask.ErpBillNum = _entity.项目编号; _ask.EndTime = DateTime.Parse(_entity.到货日期); _ask.CreateTime = DateTime.Now; _ask.CreateUser = p_creator; _ask.VendId = _entity.供应商编码; _ask.GUID = Guid.NewGuid(); _ask.ReceivedPort = _entity.收货口; _ask.IsDeleted = false; db.TB_ASK.Add(_ask); #region 供应商新任务代办接口 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = _ask.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.VEND_ID = _ask.VendId; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = p_buyer; _item.SENDING_TIME = DateTime.Now; db.TB_PENING_ITEMS.Add(_item); #endregion // int _number = 1; List _ls = new List(); list.ForEach((itm) => { var _number = _ls.Count(p1 => p1.PoBillNum == _ask.PoBillNum && p1.PoLine == Int32.Parse(itm.行号) && p1.PartCode == itm.零件编码 && itm.域名 == p1.Remark); if (_number > 0) { _ret.MessageList.Add(string.Format("已经存在订单为{0}行号为{1}零件编码为{2}", itm.订单编号, itm.行号, itm.零件编码)); } TB_ASK_DETAIL _t_ASK_Detail = new TB_ASK_DETAIL(); _t_ASK_Detail.PoLine = Int32.Parse(itm.行号); _t_ASK_Detail.Currency = itm.币种; _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.零件编码; _t_ASK_Detail.BeginTime = DateTime.Parse(itm.订货日期); _t_ASK_Detail.EndTime = DateTime.Parse(itm.到货日期); _t_ASK_Detail.TempQty = decimal.Parse(itm.订单数); _t_ASK_Detail.AskQty = decimal.Parse(itm.订单数); _t_ASK_Detail.IsDeleted = false; _t_ASK_Detail.LocUnit = itm.单位; _t_ASK_Detail.UnConv = 1; _t_ASK_Detail.DockCode = itm.收货口; _t_ASK_Detail.State = _ask.State; _t_ASK_Detail.Price = string.IsNullOrEmpty(itm.价格) ? 0 : decimal.Parse(itm.价格); _t_ASK_Detail.GUID = Guid.NewGuid(); _t_ASK_Detail.Remark = _ask.Site; _t_ASK_Detail.UpdateInfo = itm.备注; db.TB_ASK_DETAIL.Add(_t_ASK_Detail); _ls.Add(_t_ASK_Detail); // _number++; }); } else { _ret.MessageList.Add("ERP订单编号已存在,请重新导入!"); } 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_PO_CONTROLLER), "EXCEL_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), "EXCEL_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), "EXCEL_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), "EXCEL_PO_TO_ASK", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 供应商零件导入 /// /// /// /// /// public static ResultObject EXCEL_VENDER_PART_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _count = _lst.Count(itm => itm.供应商代码 == p.供应商代码 && itm.零件号 == p.零件号); if (_count > 1) { lineError.Add(string.Format("导入的供应商{0}零件编号{1}有重复", p.供应商代码, p.零件号)); ErrorList = lineError; } }); _lst.ForEach(p => { var _ls = CheckExcelMode_VenderPart(db, p, site); 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 { _lst.ForEach(p => { var _venderpart = db.TA_VEND_PART.SingleOrDefault(t => t.VendId == p.供应商代码.ToUpper() && t.PartCode == p.零件号.ToUpper()) ?? new TA_VEND_PART { VendId = p.供应商代码.ToUpper(), PartCode = p.零件号.ToUpper(), State = 1, CreateTime = DateTime.Now, CreateUser = p_creator, IsDeleted = false, GUID = System.Guid.NewGuid(), }; _venderpart.VendPartCode = p.供应商零件号.ToUpper(); _venderpart.VendPackQty = decimal.Parse(p.标包数); _venderpart.MinPackQty = decimal.Parse(p.小包装数); _venderpart.PalletPackQty = decimal.Parse(p.托盘包装数); _venderpart.PoUnit = p.单位; _venderpart.Remark = ""; _venderpart.UpdateTime = DateTime.Now; _venderpart.UpdateUser = p_creator; _venderpart.Site = site; db.TA_VEND_PART.AddOrUpdate(_venderpart); }); 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_PO_CONTROLLER), "EXCEL_VENDER_PART_MOD", 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), "EXCEL_VENDER_PART_MOD", 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), "EXCEL_VENDER_PART_MOD", 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), "EXCEL_VENDER_PART_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 供应商导入 /// /// /// /// /// public static ResultObject EXCEL_VENDER_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _count = _lst.Count(itm => itm.供应商编号 == p.供应商编号); if (_count > 1) { lineError.Add(string.Format("导入零件编号{0}有重复", p.供应商编号)); ErrorList = lineError; } }); _lst.ForEach(p => { var _ls = CheckExcelMode_Vender(db, p, site); 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 { _lst.ForEach(p => { var _vender = db.TA_VENDER.SingleOrDefault(t => t.VendId == p.供应商编号.ToUpper()) ?? new TA_VENDER { VendId = p.供应商编号.ToUpper(), State = 1 }; _vender.VendName = p.供应商名称; _vender.VendAbbCode = string.IsNullOrEmpty(p.供应商缩写) ? "0" : p.供应商缩写; _vender.VendType = p.供应商类型; _vender.Country = p.国家; _vender.City = p.城市; _vender.Currency = p.币种; _vender.Address = p.地址; _vender.ZipCode = p.邮编; _vender.Contacter = p.联系人; _vender.Phone = p.电话; _vender.Fax = p.传真; _vender.Remark = p.备注; _vender.Tax = Convert.ToDecimal(p.税率); _vender.Site = site; db.TA_VENDER.AddOrUpdate(_vender); }); 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_PO_CONTROLLER), "EXCEL_VENDER_MOD", 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), "EXCEL_VENDER_MOD", 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), "EXCEL_VENDER_MOD", 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), "EXCEL_VENDER_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 价格导入 /// /// /// /// /// public static ResultObject EXCEL_PRICE_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach (p => { var _ls = CheckExcelMode_Price(db, p, site); 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 { _lst.ForEach(p => { var _price = new TB_PRICE(); _price.VendId= p.供应商编号.Trim(); _price.PartCode = p.零件编码.Trim(); _price.StartTime = DateTime.Parse( p.开始时间); _price.EndTime = DateTime.Parse(p.结束时间); _price.Curr = p.币种; _price.Unit = p.单位; _price.Amt = decimal.Parse( p.价格); _price.Site = site.Trim(); _price.Remarks = p.备注; db.TB_PRICE.Add(_price); }); 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_PO_CONTROLLER), "EXCEL_PRICE_MOD", 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), "EXCEL_PRICE_MOD", 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), "EXCEL_PRICE_MOD", 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), "EXCEL_PRICE_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 零件导入 /// /// /// /// /// public static ResultObject EXCEL_PART_MOD(List p_order_list, string site, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _count = _lst.Count(itm => itm.零件编号 == p.零件编号); if (_count > 1) { lineError.Add(string.Format("导入零件编号{0}有重复", p.零件编号)); ErrorList = lineError; } }); _lst.ForEach (p => { var _ls = CheckExcelMode_Part(db, p, site); 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 { _lst.ForEach(p => { var _part = db.TA_PART.SingleOrDefault(t => t.PartCode == p.零件编号.ToUpper()) ?? new TA_PART { PartCode = p.零件编号.ToUpper() }; _part.PartCode = p.零件编号; _part.ErpPartCode = p.零件编号; _part.PartDesc1 = p.零件名称; _part.PartDesc2 = ""; _part.ProjectId = p.项目编号; _part.Unit = p.单位; _part.PartGroup = p.项目编号; _part.State = p.状态; _part.Site = site; db.TA_PART.AddOrUpdate(_part); }); 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_PO_CONTROLLER), "EXCEL_PART_MOD", 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), "EXCEL_PART_MOD", 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), "EXCEL_PART_MOD", 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), "EXCEL_PART_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 订单导入 /// /// /// /// /// /// /// public static ResultObject EXCEL_PO_MOD(List p_order_list, string site, string p_creator, DateTime p_time, PoState p_modtype) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int number = 1; List lineError = new List(); List ErrorList = new List(); var _lst = p_order_list; _lst.ForEach(p => { var _count = _lst.Count(itm => itm.订单编号 == p.订单编号 && itm.行号 == p.行号); if (_count > 1) { lineError.Add(string.Format("导入订单编号{0}行号{1}有重复", p.订单编号, p.行号)); ErrorList = lineError; } }); _lst.ForEach(p => { var _count = _lst.Count(itm => itm.订单编号 == p.订单编号 && itm.零件号 == p.零件号 && itm.行号 == p.行号); if (_count > 1) { lineError.Add(string.Format("导入订单编号{0}行号{1}零件编码{2}有重复", p.订单编号,p.行号, p.零件号)); ErrorList = lineError; } }); _lst.ForEach (p => { var _ls = CheckExcelMode_Po(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 OrderList = _lst.GroupBy(p => p.订单编号).ToList(); OrderList.ForEach((p) => { var list = p.ToList(); if (list.Count > 0) { var _entity = list.FirstOrDefault(); TB_PO _po = new TB_PO(); _po.PoBillNum = _entity.订单编号.ToUpper(); _po.ErpBillNum = _entity.订单编号.ToUpper(); _po.VendId = _entity.供应商编号.ToUpper(); _po.ModType = (int)BillModType.Contract; _po.Contacter = _entity.联系人; _po.Site = site; _po.Remark = _entity.备注; _po.State = (int)PoState.Open; _po.GUID = Guid.NewGuid(); _po.IsDeleted = false; _po.CreateTime = DateTime.Parse(_entity.订单创建时间); _po.CreateUser = p_creator; _po.BeginTime = p_time; _po.EndTime = DateTime.Parse(_entity.要求到货日期); _po.Site = site; db.TB_PO.Add(_po); // int _number = 1; list.ForEach((itm) => { TB_PO_DETAIL _PO_Detail = new TB_PO_DETAIL(); _PO_Detail.PoLine = Int32.Parse(itm.行号); _PO_Detail.Currency = "CNY"; _PO_Detail.PoBillNum = itm.订单编号; _PO_Detail.PoUnit = itm.单位; _PO_Detail.CreateTime = DateTime.Parse(itm.订单创建时间); _PO_Detail.CreateUser = p_creator; _PO_Detail.PartCode = itm.零件号.ToUpper(); _PO_Detail.BeginTime = p_time; _PO_Detail.EndTime = DateTime.Parse(itm.要求到货日期); _PO_Detail.PlanQty = decimal.Parse(itm.数量); _PO_Detail.IsDeleted = false; _PO_Detail.LocUnit = itm.单位; _PO_Detail.PoUnit = itm.单位; _PO_Detail.UnConv = 1; _PO_Detail.State = (int)PoState.Open; _PO_Detail.Price = string.IsNullOrEmpty(itm.单价) ? 0 : decimal.Parse(itm.单价); _PO_Detail.GUID = Guid.NewGuid(); _PO_Detail.Site = site; db.TB_PO_DETAIL.Add(_PO_Detail); // _number++; }); } else { _ret.MessageList.Add("ERP订单编号已存在,请重新导入!"); 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_PO_CONTROLLER), "EXCEL_PO_MOD", 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), "EXCEL_PO_MOD", 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), "EXCEL_PO_MOD", 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), "EXCEL_PO_MOD", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static List EXCEL_ASK_Check(List p_order_list) { List ErrorList = new List(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { p_order_list.ForEach (p => { //var _ls = CheckExcel(db, p); var _checkls = CheckExcelPackQty(db, p); if (_checkls.Count > 0) { ErrorList.Add(string.Join("
", _checkls.ToArray())); } }); } return ErrorList; } public static ResultObject EXCEL_PO_TO_ASK(List p_order_list, string p_creator, string p_buyer, DateTime p_time, BillModType p_modtype, string p_site, bool IsAutoPublish = false) { 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 { p_order_list = p_order_list.Where(p => int.Parse(p.数量) != 0).ToList(); //var VenderList = p_order_list.Select(p => p.供应商编号).Distinct(); //var PartCodeList = p_order_list.Select(p => p.零件号).Distinct(); var OrderList = p_order_list.GroupBy(p =>new { 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.New; _ask.PoBillNum = _entity.QAD订单号.ToUpper(); _ask.ModType = (int)BillModType.Contract; _ask.Site = p_site; if (IsAutoPublish) { _ask.State = (int)AskState.Release; } else { _ask.State = (int)AskState.New; } _ask.BeginTime = DateTime.Now; _ask.Buyer = _entity.交货联系人; _ask.BuyerPhone = _entity.交货联系人电话; _ask.ErpBillNum = _entity.QAD订单号.ToUpper(); _ask.EndTime = DateTime.Parse(_entity.要求到货日期); _ask.CreateTime = DateTime.Now; _ask.CreateUser = p_creator; _ask.VendId = _entity.供应商编号.ToUpper(); _ask.GUID = Guid.NewGuid(); _ask.IsDeleted = false; db.TB_ASK.Add(_ask); #region 供应商新任务代办接口 TB_PENING_ITEMS _item = new TB_PENING_ITEMS(); _item.ROLE_NAME = "供应商"; _item.ITEM_NO = _ask.AskBillNum; _item.ITEM_CONTENT = "要货看盘有信息"; _item.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASK; _item.VEND_ID = _ask.VendId; _item.ITEM_STATE = (int)PENDING_STATE.WAITING; _item.SENDER = _entity.交货联系人; _item.SENDING_TIME = DateTime.Now; db.TB_PENING_ITEMS.Add(_item); #endregion // int _number = 1; list.ForEach((itm) => { TB_ASK_DETAIL _t_ASK_Detail = new TB_ASK_DETAIL(); //_t_ASK_Detail.PoLine = _number; _t_ASK_Detail.PoLine = Int32.Parse(itm.行号); _t_ASK_Detail.Currency = itm.币种; _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()).TrimEnd(); _t_ASK_Detail.BeginTime = _ask.BeginTime; _t_ASK_Detail.EndTime = DateTime.Parse(itm.要求到货日期); _t_ASK_Detail.TempQty = decimal.Parse(itm.数量); _t_ASK_Detail.AskQty = decimal.Parse(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(); _t_ASK_Detail.Remark = itm.备注; db.TB_ASK_DETAIL.Add(_t_ASK_Detail); // _number++; }); } else { _ret.MessageList.Add("ERP订单编号已存在,请重新导入!"); 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_PO_CONTROLLER), "EXCEL_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), "EXCEL_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), "EXCEL_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), "EXCEL_PO_TO_ASK", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); if (e.InnerException != null) { _ret.MessageList.Add(e.InnerException.Message); } else { _ret.MessageList.Add(e.Message); } } return _ret; } public static ResultObject Save_PO_TO_ASK(V_TB_PO p_order, List p_order_list , string p_buyer, string p_buyerPhone, string p_creator) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { TB_ASK _t = new TB_ASK(); _t.AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(); _t.PoBillNum = p_order.PoBillNum; _t.VendId = p_order.VendId; _t.IsDeleted = false;//操作员 _t.CreateTime = DateTime.Now; _t.CreateUser = p_creator; _t.Site = p_order.Site; _t.ReceivedPort = p_order.ReceivedPort; _t.Remark = p_order.Remark; _t.State = (int)AskState.New; _t.SubSite = p_order.SubSite; _t.Extend1 = p_order.Extend1; _t.Extend2 = p_order.Extend2; _t.Extend3 = p_order.Extend3; _t.GUID = Guid.NewGuid(); _t.ModType = (int)p_order.ModType; _t.ErpBillNum = p_order.ErpBillNum; _t.BeginTime = (DateTime)p_order.BeginTime; var _date = p_order.BeginTime; if(p_order_list.Count>0) { _date = p_order_list[0].EndTime; } _t.EndTime = _date; _t.Buyer = p_buyer; _t.BuyerPhone = p_buyerPhone; db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _t); List _list = new List(); foreach (var itm in p_order_list) { TB_ASK_DETAIL _tDetail = new TB_ASK_DETAIL(); _tDetail.Remark = _t.Remark; _tDetail.PoBillNum = _t.PoBillNum; _tDetail.AskBillNum = _t.AskBillNum; _tDetail.PoLine = itm.PoLine; _tDetail.PoUnit = itm.PoUnit; _tDetail.CreateTime = DateTime.Now; _tDetail.CreateUser = p_creator; _tDetail.PartCode = itm.PartCode; _tDetail.ReceivedPort = itm.ReceivedPort; _tDetail.BeginTime = itm.BeginTime; _tDetail.EndTime = itm.EndTime; _tDetail.TempQty = itm.TempQty == null ? 0 : (decimal)itm.TempQty; _tDetail.AskQty = (decimal)itm.PublishQty; _tDetail.IsDeleted = false; _tDetail.LocUnit = itm.LocUnit; _tDetail.UnConv = itm.UnConv; _tDetail.State = (int)p_order.State; _tDetail.Price = itm.Price; _tDetail.GUID = Guid.NewGuid(); _tDetail.Currency = itm.Currency; _tDetail.SubSite = itm.SubSite; _tDetail.Extend1 = itm.Extend1; _tDetail.Extend2 = itm.Extend2; _tDetail.Extend3 = itm.Extend3; db.TB_ASK_DETAIL.Add(_tDetail); var _order_detail = db.TB_PO_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); _order_detail.TempQty = itm.TempQty; db.TB_PO_DETAIL.AddOrUpdate(p => p.UID, _order_detail); } 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_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 void Get_V_TB_PO_DETAIL_OUTPUT_List(V_TB_PO_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_PO_DETAIL; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.ErpBillNum)) { q = q.Where(p => p.ErpBillNum.Contains(p_entity.ErpBillNum)); } if (p_entity.BeginTime != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTime); } if (p_entity.EndTime != null) { q = q.Where(p => p.BeginTime <= p_entity.EndTime); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (p_entity.ModType != null) { q = q.Where(p => p.ModType == p_entity.ModType); } if (!string.IsNullOrEmpty(p_entity.ProjectId)) { q = q.Where(p => p.ProjectId.Contains(p_entity.ProjectId)); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (p_entity.State != null) { q = q.Where(p => p.State == p_entity.State); } //q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); //IQueryable _aq = db.TB_ASK_DETAIL; //_aq = _aq.Where(p => p.PoBillNum == p_entity.PoBillNum); //var _a=_aq.GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AskSum = p.Max(itm=>itm .AskQty),PoLine=p.Key }); 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.SubSite)) { q = q.Where(p => p.SubSite == p_entity.SubSite); } if (!string.IsNullOrEmpty(p_entity.Extend1)) { q = q.Where(p => p.Extend1 == p_entity.Extend1); } if (!string.IsNullOrEmpty(p_entity.Extend2)) { q = q.Where(p => p.Extend1 == p_entity.Extend2); } if (!string.IsNullOrEmpty(p_entity.Extend3)) { q = q.Where(p => p.Extend1 == p_entity.Extend3); } if (!string.IsNullOrEmpty(p_entity.SubSite)) { q = q.Where(p => p.SubSite.Contains(p_entity.SubSite)); } if (!string.IsNullOrEmpty(p_entity.Extend1)) { q = q.Where(p => p.Extend1.Contains(p_entity.Extend1)); } if (!string.IsNullOrEmpty(p_entity.Extend2)) { q = q.Where(p => p.Extend2.Contains(p_entity.Extend2)); } if (!string.IsNullOrEmpty(p_entity.Extend3)) { q = q.Where(p => p.Extend3.Contains(p_entity.Extend3)); } _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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_DETAIL_List", e.Message); throw e; } } public static void Get_V_TB_PO_DETAIL_List(V_TB_PO_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_PO_DETAIL; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum == p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.ErpBillNum)) { q = q.Where(p => p.ErpBillNum.Contains(p_entity.ErpBillNum)); } if (p_entity.BeginTime != null) { q = q.Where(p => p.BeginTime >= p_entity.BeginTime); } if (p_entity.EndTime != null) { q = q.Where(p => p.BeginTime <= p_entity.EndTime); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName == p_entity.VendName); } if (p_entity.ModType != null) { q = q.Where(p => p.ModType == p_entity.ModType); } if (!string.IsNullOrEmpty(p_entity.ProjectId)) { q = q.Where(p => p.ProjectId == p_entity.ProjectId); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode == p_entity.PartCode); } if (p_entity.State != null) { q = q.Where(p => p.State == p_entity.State); } //q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); //IQueryable _aq = db.TB_ASK_DETAIL; //_aq = _aq.Where(p => p.PoBillNum == p_entity.PoBillNum); //var _a=_aq.GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode }).Select(p => new { AskSum = p.Max(itm=>itm .AskQty),PoLine=p.Key }); if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_DETAIL_List", e.Message); throw e; } } #region 沈阳金杯 public static ResultObject Save_PO_TO_ASK_SYJB(V_TB_PO p_order, List p_order_list , string p_buyer, string p_buyerPhone) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { TB_ASK _t = new TB_ASK(); _t.AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(); _t.PoBillNum = p_order.PoBillNum; _t.VendId = p_order.VendId; _t.IsDeleted = false;//操作员 _t.CreateTime = DateTime.Now; _t.CreateUser = p_buyer; _t.Site = p_order.Site; _t.Remark = p_order.Remark; _t.State = (int)AskState.Release; _t.Remark = p_order.Remark; _t.GUID = p_order.GUID; _t.ModType = (int)p_order.ModType; _t.ErpBillNum = p_order.ErpBillNum; _t.BeginTime = (DateTime)p_order.BeginTime; _t.EndTime = p_order.BeginTime; _t.Buyer = p_buyer; _t.BuyerPhone = p_buyerPhone; db.TB_ASK.AddOrUpdate(p => p.AskBillNum, _t); List _list = new List(); foreach (var itm in p_order_list) { TB_ASK_DETAIL _tDetail = new TB_ASK_DETAIL(); _tDetail.PoBillNum = _t.PoBillNum; _tDetail.AskBillNum = _t.AskBillNum; _tDetail.PoLine = itm.PoLine; _tDetail.PoUnit = itm.PoUnit; _tDetail.CreateTime = DateTime.Now; _tDetail.CreateUser = p_buyer; _tDetail.PartCode = itm.PartCode; _tDetail.BeginTime = itm.BeginTime; _tDetail.EndTime = itm.EndTime; _tDetail.Currency = itm.Currency; _tDetail.TempQty = itm.TempQty == null ? 0 : (decimal)itm.TempQty; _tDetail.AskQty = (decimal)itm.PlanQty; _tDetail.IsDeleted = false; _tDetail.LocUnit = itm.LocUnit; _tDetail.UnConv = itm.UnConv; _tDetail.State = (int)p_order.State; _tDetail.Price = itm.Price; _tDetail.GUID = itm.GUID; db.TB_ASK_DETAIL.Add(_tDetail); var _order_detail = db.TB_PO_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); _order_detail.TempQty = itm.TempQty; db.TB_PO_DETAIL.AddOrUpdate(p => p.UID, _order_detail); } int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(TB_PO), "Save_PO_TO_ASK", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static void Get_V_TA_VENDER(V_TB_PO_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List result = new List(); IQueryable q = db.V_TA_VENDER; if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) { q = q.Where(p => p_entity.UserInAddress.Contains(p.Site)); } IQueryable q2 = db.TB_ASN; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q2 = q2.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (p_entity.BeginTime != null) { q2 = q2.Where(p => p.ShipTime >= p_entity.BeginTime); } if (p_entity.EndTime != null) { q2 = q2.Where(p => p.ShipTime <= p_entity.EndTime); } //返回全部供应商 if (string.IsNullOrEmpty(p_entity.PoBillNum) && p_entity.BeginTime == null && p_entity.EndTime == null) { result = q.ToList(); } //返回符合条件发货单里的供应商 else { List lsVendId = new List(); q.ToList().ForEach(p => { q2.ToList().ForEach(itm => { if (p.VendId == itm.VendId) { if (!lsVendId.Contains(p.VendId)) { result.Add(p); lsVendId.Add(p.VendId); } } }); }); } _ret.State = ReturnStatus.Succeed; _ret.Result = result.AsQueryable(); 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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_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_PO_CONTROLLER), "Get_V_TB_PO_DETAIL_List", e.Message); throw e; } } public static List LOAD_PO_DETAIL_SUM_PROMPTNESS(V_TB_PO_DETAIL p_entity, List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (p_list.Count > 0) { //var _detail = p_list[0]; foreach (V_TA_VENDER _detail in p_list) { IQueryable q = db.TB_ASN.Where(p => p.VendId == _detail.VendId && p_entity.UserInAddress.Contains(p.Site)); if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } 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); } int asnNo = q.ToList().Count;//是发货数量 int arriveNo = 0; q.ToList().ForEach(p => { TB_ARRIVE q2 = db.TB_ARRIVE.FirstOrDefault(p1 => p1.AsnBillNum == p.AsnBillNum && p_entity.UserInAddress.Contains(p1.Site) && p1.BillType.ToString() == "0"); if (q2 != null && q2.ShipTime != null && p.ShipTime != null && q2.ShipTime <= p.ShipTime) { arriveNo += 1; } }); _detail.ShippedQty = asnNo.ToString();//发货数量 _detail.ArriveQty = arriveNo.ToString();//到货数量 if (asnNo == 0) { _detail.Promptness = "0%";//及时率 } else { double temp = (arriveNo / asnNo) * 100; _detail.Promptness = temp.ToString("0.00") + "% ";//及时率 } //arrive.ToList().ForEach(itm => //{ // if (_detail.PoBillNum == itm.PoBillNum && _detail.PoLine == itm.PoLine && _detail.PartCode == itm.PartCode) // { // _detail.ArriveQty = itm.ArriveSum.ToString(); // } //}); } } } return p_list; } #endregion } }