using System; using System.Collections.Generic; using System.Data; using System.Data.Entity.Migrations; using System.Data.Entity.Validation; using System.Linq; using System.Text; using System.Threading.Tasks; using CK.SCP.Models; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Utils; using System.Data.Entity.Core; using CK.SCP.Models.AppBoxEntity; using CK.SCP.Models.ScpEntity.ExcelExportEnttity; using System.Data.SqlClient; using CK.SCP.Models.ScpEntity.ExcelImportEntity; namespace CK.SCP.Controller { public class SCP_ASN_CONTROLLER { public static DataTable Get_CUSTOM_PAGE_List(V_TB_ASN p_entity) { string _sql= "SELECT COUNT(*) 标包数量,SUM(Qty) 发货数量,BillNum 发货单号,PoBillLine 行号,PoUnit 单位,PartDesc1 +PartDesc2 as 零件描述,a.PartCode 零件编号,'' as 分包数量\n" + " from TS_BARCODE_CUSTOM A left join TA_PART B ON A.PartCode=B.PartCode AND A.Site=B.Site where BillNum='{0}' GROUP by a.PartCode,BillNum,PoBillLine,PoUnit,PartDesc1,PartDesc2"; string _sql1 = "select qty FROM [TS_BARCODE_CUSTOM] where billnum ='{0}' and partcode='{1}'"; DataTable dt = 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 cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = string.Format(_sql, p_entity.AsnBillNum); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dt); foreach (DataRow row in dt.Rows) { DataTable dt1 = new DataTable(); SqlCommand cmd1 = new SqlCommand(); cmd1.Connection = conn; cmd1.CommandText = string.Format(_sql1, p_entity.AsnBillNum,row["零件编号"].ToString()); SqlDataAdapter adapter1 = new SqlDataAdapter(cmd1); adapter1.Fill(dt1); if (dt1.Rows.Count > 0) { List _ls = new List(); foreach (DataRow _row in dt1.Rows) { _ls.Add(_row[0].ToString()); } row["分包数量"] = string.Join("("+row["单位"].ToString() +")"+ ";", _ls.ToArray())+ "("+row["单位"].ToString()+")"; } } } catch(Exception e) { conn.Close(); throw e; } return dt; } public static void Get_V_TB_ASN_List(V_TB_ASN p_entity, Action>> p_action,string p_language="CN") { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASN; 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.PoBillNum)) { q = q.Where(p => p.PoBillNum==p_entity.PoBillNum); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId==p_entity.VendId); } if (p_entity.State != null) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.ShipUser)) { q = q.Where(p => p.ShipUser.Contains(p_entity.ShipUser)); } if (!string.IsNullOrEmpty(p_entity.ReceiveUser)) { q = q.Where(p => p.ReceiveUser.Contains(p_entity.ReceiveUser)); } if (!string.IsNullOrEmpty(p_entity.ErpBillNum)) { q = q.Where(p => p.ErpBillNum.Contains(p_entity.ErpBillNum)); } 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 (p_entity.ShipTime != null) { q = q.Where(p => p.ShipTime == p_entity.ShipTime); } if (p_entity.ReceiveTime != null) { q = q.Where(p => p.ReceiveTime == p_entity.ReceiveTime); } if (!string.IsNullOrEmpty(p_entity.Buyer)) { q = q.Where(p => p.Buyer.Contains(p_entity.Buyer)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.State_DESC)) { q = q.Where(p => p.State_DESC.Contains(p_entity.State_DESC)); } if (!string.IsNullOrEmpty(p_entity.ModType_DESC)) { q = q.Where(p => p.ModType_DESC.Contains(p_entity.ModType_DESC)); } 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 (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 == 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.Extend2 == p_entity.Extend2); } if (!string.IsNullOrEmpty(p_entity.Extend3)) { q = q.Where(p => p.Extend3 == 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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_List", e.Message); throw e; } } public static ResultObject Save_Asn_Vendbatch(List p_order_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _list = new List(); var PartList = p_order_list.GroupBy(p => p.PartCode).ToList(); PartList.ForEach(p=> { var list = p.ToList(); var batchList = list.Select(t => t.VendBatch).Distinct(); if(list.Count()!= batchList.Count()) { _ret.MessageList.Add("同一物料号,不同子件物料号的原卷号不能相同"); } }); foreach (var itm in p_order_list) { var detail = db.TA_ASK_SUBPART.FirstOrDefault(P => P.UID == itm.UID); detail.VendBatch = itm.VendBatch; db.TA_ASK_SUBPART.AddOrUpdate(p => p.UID, detail); } if (_ret.MessageList.Count() == 0) { int state = db.SaveChanges(); _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_Asn_ProduceDate", 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_ASN_CONTROLLER), "Save_Asn_ProduceDate", 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_ASN_CONTROLLER), "Save_Asn_ProduceDate", 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_ASN_CONTROLLER), "Save_Asn_ProduceDate", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Delete_asn(List asn) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { { int number = 1; List lineError = new List(); List ErrorList = new List(); foreach (var nn in asn) { var _ls = CheckExce2(db, nn); 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 ASN = db.TB_ASN.Where(p => p.AsnBillNum == nn).FirstOrDefault(); var detail = db.TB_ASN_DETAIL.Where(p => p.AsnBillNum == nn).ToArray(); if (ASN != null) { ASN.State = -1; ASN.IsDeleted = true; db.TB_ASN.AddOrUpdate(ASN); } if (detail.Count() > 0) { foreach (var itm in detail) { itm.IsDeleted = true; itm.State = -1; } db.TB_ASN_DETAIL.AddOrUpdate(detail); } } } } 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), "Delete_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_PO_CONTROLLER), "Delete_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_PO_CONTROLLER), "Delete_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_PO_CONTROLLER), "Delete_asn", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } private static List CheckExce2(ScpEntities db, string lt) { List ErrorList = new List(); var _asn = db.TB_ASN.Count(p => p.AsnBillNum == lt&&p.State>0); if (_asn > 0) { ErrorList.Add(string.Format("发货单【{0}】不是新建状态,不能作废", lt)); } return ErrorList; } public static List SearchCxcount(string asn) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var asndetail= db.TB_ASN_DETAIL.Where(p => p.AsnBillNum==asn&&p.Extend1=="FT").ToList(); return asndetail; } } public static List SearchCxcount1(string asn) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var asndetail = db.TB_ASN_DETAIL.Where(p => p.AsnBillNum== asn && p.Extend1 == "PT").ToList(); return asndetail; } } public static List SearchPartBox(string asn) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var PartBox = db.TB_ASN_PARTBOX.Where(p => p.AsnBillNum == asn && p.Box>0).ToList(); return PartBox; } } public static List SearchBarcode(string asn) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var barcode = db.TS_BARCODE.Where(p => p.BillNum == asn).ToList(); return barcode; } } public static void Get_TB_Pallet_List(V_TB_PALLETS p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_PALLETS; if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (p_entity.Qty != 0) { q = q.Where(p => p.Qty == p_entity.Qty); } 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.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_TB_Pallet_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_ASN_CONTROLLER), "Get_TB_Pallet_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_ASN_CONTROLLER), "Get_TB_Pallet_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_ASN_CONTROLLER), "Get_TB_Pallet_List", e.Message); throw e; } } public static void Get_V_TB_ASN_DETAIL_MODIFY(V_TB_ASN_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASN_DETAIL; if (p_entity.UID!=0) { q = q.Where(p => p.UID==p_entity.UID); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_DETAIL_List", e.Message); throw e; } } public static void Get_V_TB_ASN_DETAIL_List(V_TB_ASN_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASN_DETAIL; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (p_entity.PoLine != 0) { q = q.Where(p => p.PoLine == p_entity.PoLine); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (!string.IsNullOrEmpty(p_entity.PoUnit)) { q = q.Where(p => p.PoUnit.Contains(p_entity.PoUnit)); } if (!string.IsNullOrEmpty(p_entity.LocUnit)) { q = q.Where(p => p.LocUnit.Contains(p_entity.LocUnit)); } if (p_entity.Price != 0) { q = q.Where(p => p.Price == p_entity.Price); } if (!string.IsNullOrEmpty(p_entity.Currency)) { q = q.Where(p => p.Currency.Contains(p_entity.Currency)); } if (p_entity.PackQty != 0) { q = q.Where(p => p.PackQty == p_entity.PackQty); } if (p_entity.UnConv != 0) { q = q.Where(p => p.UnConv == p_entity.UnConv); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (p_entity.State != null) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.CreateUser)) { q = q.Where(p => p.CreateUser.Contains(p_entity.CreateUser)); } 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)); } if (p_entity.IsDeleted != false) { q = q.Where(p => p.IsDeleted == p_entity.IsDeleted); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (!string.IsNullOrEmpty(p_entity.CurrencyDesc)) { q = q.Where(p => p.CurrencyDesc.Contains(p_entity.CurrencyDesc)); } if (!string.IsNullOrEmpty(p_entity.Site)) { q = q.Where(p => p.Site.Contains(p_entity.Site)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (p_entity.Qty != 0) { q = q.Where(p => p.Qty == p_entity.Qty); } if (!string.IsNullOrEmpty(p_entity.VendBatch)) { q = q.Where(p => p.VendBatch.Contains(p_entity.VendBatch)); } if (!string.IsNullOrEmpty(p_entity.Batch)) { q = q.Where(p => p.Batch.Contains(p_entity.Batch)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId.Contains(p_entity.VendId)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_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_ASN_CONTROLLER), "Get_V_TB_ASN_DETAIL_List", e.Message); throw e; } } public static ResultObject Save_TB_ASN_DETAIL_MODIFY(V_TB_ASN_DETAIL p_asn,string p_state) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _ls = db.TB_ASN_DETAIL.FirstOrDefault(p => p.UID==p_asn.UID); if (p_state == "Modify") { if (_ls != null) { _ls.ProduceDate = p_asn.ProduceDate; _ls.PackQty = p_asn.PackQty; _ls.Qty = p_asn.Qty; _ls.VendBatch = p_asn.VendBatch; db.TB_ASN_DETAIL.AddOrUpdate(_ls); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据不存在!"; } } if (p_state == "Delete") { if (_ls != null) { _ls.State =(int)AsnState.Reject; _ls.IsDeleted = true; db.TB_ASN_DETAIL.AddOrUpdate(_ls); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据不存在!"; } } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASN_STATE(List p_list, AsnState p_state) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { if (db.TB_ASN.Where(p => p_list.Contains(p.AsnBillNum) && p.IsDeleted ==false).Count() == 0) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "存在删除记录不能操作!"; } else { if (p_state == AsnState.New) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AsnBillNum)).ToList(); _ls.ForEach(p => p.State = (int)AskState.New); db.TB_ASN.AddOrUpdate(p => p.AsnBillNum, _ls.ToArray()); } //if (p_state == AsnState.Ship) //{ // var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AsnBillNum) && p.IsDeleted == false).ToList(); // if (_ls.Count(p => p.State == (int)ShipState.New) == _ls.Count) // { // var p = _ls[0]; // #region 代办流程提示收货人 // TB_PENING_ITEMS pending = new TB_PENING_ITEMS(); // pending.ITEM_NO = p.AsnBillNum; // pending.ITEM_TYPE = (int)PENDING_MESSAGE_TYPE.NEW_ASN; // pending.ITEM_STATE = (int)PENDING_STATE.WAITING; // pending.GUID = Guid.NewGuid(); // pending.VEND_ID = p.VendId; // pending.ROLE_NAME = "物流人员"; // pending.SENDER = p.CreateUser; // pending.SENDING_TIME = p.CreateTime; // pending.RENEWER = p.CreateUser; // pending.RENEW_TIME = p.CreateTime; // pending.ITEM_CONTENT = string.Format("订单编号{0}发货单编号{1}已发货", p.PoBillNum, p.AsnBillNum); // db.TB_PENING_ITEMS.Add(pending); // #endregion; // } // else // { // _ret.State = ReturnStatus.Failed; // _ret.Result = false; // _ret.Message = "选择数据有不是新建状态单据!"; // } //} if (p_state == AsnState.Reject) { var _ls = db.TB_ASN.Where(p => p_list.Contains(p.AsnBillNum)).ToList(); int count = _ls.Count(p => p.State == (int)AsnState.New); if (_ls.Count == count) { List _askbill = new List(); _ls.ForEach(p => { p.State = (int)AskState.Reject; p.IsDeleted = true; var asnDetail = db.TB_ASN_DETAIL.Where(itm => itm.AsnBillNum == p.AsnBillNum).ToList(); asnDetail.ForEach(itm =>{ itm.IsDeleted = true; itm.State= (int)AskState.Reject; }); db.TB_ASN_DETAIL.AddOrUpdate(itm => itm.UID, asnDetail.ToArray()); }); db.TB_ASN.AddOrUpdate(p => p.AsnBillNum, _ls.ToArray()); } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = string.Format("选择记录中有,不是新建状态不能作废!"); } } if (string.IsNullOrEmpty(_ret.Message)) { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_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_ASN_CONTROLLER), "Save_TB_ASN_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static DataSet ASN_REPORT(string p_AsnBillNum,string CurrentUserPhone) { DataSet ds = new DataSet(); SCP_ASN_CONTROLLER.Get_V_TB_ASN_List(new V_TB_ASN { AsnBillNum = p_AsnBillNum }, (ret) => { if (ret.State == ReturnStatus.Succeed) { if (ret.Result.Count() > 0) { var _asn = ret.Result.FirstOrDefault(); V_ASN_PRINT _print = new V_ASN_PRINT(); _print.FileNo = ""; _print.DeliveryNo = _asn.AsnBillNum; _print.ChineseAddress = _asn.Site_Desc; _print.EnglishAddress = ""; _print.SupplierNo = _asn.VendId; _print.SupplierName = _asn.VendName; _print.SupplierContacter = _asn.ShipUser; _print.DueDeliveryDate = _asn.EndTime.ToShortDateString(); _print.MaterialPlanner = _asn.Buyer; _print.CAAIPhone = ""; _print.CAAIFax = ""; _print.Site = _asn.Site; _print.Total = "0"; _print.CurrentUserPhone = CurrentUserPhone; if (_asn.Site == "U32") { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var Bb = db.TA_Receiving_Information.Count(t => t.Shift == "白班"); if (Bb > 0) { var Bb1 = db.TA_Receiving_Information.FirstOrDefault(t => t.Shift == "白班"); _print.SupplierContacter = Bb1.Name + ":" + Bb1.Phone; } var Yb = db.TA_Receiving_Information.Count(t => t.Shift == "夜班"); if (Bb > 0) { var Yb1 = db.TA_Receiving_Information.FirstOrDefault(t => t.Shift == "夜班"); _print.FileNo = Yb1.Name + ":" + Yb1.Phone; } _print.CAAIFax = _asn.ShipUser; } } using (AppBoxContext _appdb = EntitiesFactory.CreateAppBoxInstance()) { StringBuilder _buffer = new StringBuilder(); var _ls = _appdb.Users.Where(p => p.Roles.Select(itm => itm.Name).Contains("物流人员") && p.TA_FACTORY.Select(itm => itm.ErpDomain).Contains(_asn.Site)).ToList(); _ls.ForEach(itm => { _buffer.Append(itm.ChineseName + "/" + itm.CellPhone + ";"); }); _print.CAAIPhone = _buffer.ToString(); } var dt = ConvertHelper.ToDataTable(new List { _print }); ds.Tables.Add(dt); List _lst = new List(); SCP_PO_CONTROLLER.Get_V_TB_PO_DETAIL_List(new V_TB_PO_DETAIL() { PoBillNum = _asn.PoBillNum },rs=>{ if (rs.State == ReturnStatus.Succeed) { _lst= rs.Result.ToList(); } }); SCP_ASN_CONTROLLER.Get_V_TB_ASN_DETAIL_List(new V_TB_ASN_DETAIL() { AsnBillNum = p_AsnBillNum }, (retobj) => { if (retobj.State == ReturnStatus.Succeed) { List _lsDetail = new List(); var _ls = retobj.Result.OrderByDescending(p=>p.PoLine).ToList(); _ls.ForEach(p => { V_ASN_PRINT_DETAIL _detail = new V_ASN_PRINT_DETAIL(); _detail.PartNo = p.PartCode; _detail.PartDesc = p.PartDesc1; _detail.Unit = p.PoUnit; _detail.STD = p.PackQty.ToString(); _detail.Boxes = (Math.Truncate((p.Qty / p.PackQty)) + (((p.Qty % p.PackQty) > 0) ? 1 : 0)).ToString(); _detail.DeliverQty = p.Qty.ToString(); _detail.ReceiveQty = p.Qty.ToString(); //_detail.PoNo = p.PoBillNum + "." + p.AskBillNum; //if (p.Site == "U32") //{ _detail.PoNo = p.PoBillNum; //} _detail.LineNO = p.PoLine.ToString(); _detail.Memo = p.Remark; _detail.Batch = p.Batch; _detail.VendBatch = p.VendBatch; _detail.DockCode = p.Extend2; using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var Poqty = db.TB_PO_DETAIL.FirstOrDefault(t=>t.PartCode== p.PartCode&&t.PoBillNum==p.PoBillNum&&t.PoLine==p.PoLine); _detail.PoQty = Poqty.PlanQty.ToString(); } // _detail.ProjectId = _lst.Where(itm => itm.PartCode == p.PartCode).FirstOrDefault().ProjectId; _lsDetail.Add(_detail); }); var tb = ConvertHelper.ToDataTable(_lsDetail); ds.Tables.Add(tb); } }); } } }); return ds; } public static DataSet ASN_PALLET_REPORT(string p_AsnBillNum) { DataSet ds = new DataSet(); SCP_ASN_CONTROLLER.Get_V_TB_ASN_List(new V_TB_ASN { AsnBillNum = p_AsnBillNum }, (ret) => { if (ret.State == ReturnStatus.Succeed) { if (ret.Result.Count() > 0) { var _asn = ret.Result.FirstOrDefault(); V_ASN_PRINT _print = new V_ASN_PRINT(); _print.FileNo = ""; _print.DeliveryNo = _asn.AsnBillNum; _print.ChineseAddress = _asn.Site_Desc; _print.EnglishAddress = ""; _print.SupplierNo = _asn.VendId; _print.SupplierName = _asn.VendName; _print.SupplierContacter = _asn.ShipUser; _print.DueDeliveryDate = _asn.ReceiveTime.ToString(); _print.MaterialPlanner = _asn.Buyer; _print.CAAIPhone = ""; _print.CAAIFax = ""; _print.Total = "0"; using (AppBoxContext _appdb = EntitiesFactory.CreateAppBoxInstance()) { StringBuilder _buffer = new StringBuilder(); var _ls = _appdb.Users.Where(p => p.Roles.Select(itm => itm.Name).Contains("物流人员")).ToList(); _ls.ForEach(itm => { _buffer.Append(itm.ChineseName + "/" + itm.CellPhone + ";"); }); _print.CAAIPhone = _buffer.ToString(); } SCP_ASN_CONTROLLER.Get_TB_Pallet_List(new V_TB_PALLETS() { AsnBillNum = p_AsnBillNum }, (retobj) => { if (retobj.State == ReturnStatus.Succeed) { List _lsDetail = new List(); var _ls = retobj.Result.ToList(); decimal count = 0; _ls.ForEach(p => { V_TB_PALLETS _detail = new V_TB_PALLETS(); _detail.PartCode = p.PartCode; _detail.PartName = p.PartName; _detail.PalletNum = p.PalletNum; _detail.Batch = p.Batch; _detail.Qty = p.Qty; _detail.Box = p.Box; count += p.Qty; _lsDetail.Add(_detail); }); _print.Total = count.ToString(); var tb = ConvertHelper.ToDataTable(_lsDetail); ds.Tables.Add(tb); } }); var dt = ConvertHelper.ToDataTable(new List { _print }); ds.Tables.Add(dt); } } }); return ds; } public static DataSet ARRIVE_REPORT(string p_AsnBillNum) { DataSet ds = new DataSet(); SCP_ASN_CONTROLLER.Get_V_TB_ASN_List(new V_TB_ASN { AsnBillNum = p_AsnBillNum }, (ret) => { if (ret.State == ReturnStatus.Succeed) { if (ret.Result.Count() > 0) { var _asn = ret.Result.FirstOrDefault(); V_ARRIVE_PRINT _print = new V_ARRIVE_PRINT(); if (_asn.ShipTime != null) { DateTime dttemp; if (DateTime.TryParse(_asn.ShipTime.ToString(), out dttemp)) { _print.ShipTimeDate = dttemp.ToString("yyyy-MM-dd");//发货日期 } } if (_asn.ReceiveTime != null) { DateTime dttemp; if (DateTime.TryParse(_asn.ReceiveTime.ToString(), out dttemp)) { _print.DueDeliveryDate = dttemp.ToString("yyyy-MM-dd");//到货日期 } } _print.SupplierName = _asn.VendName;//发货单位 _print.SupplierNo = _asn.VendId;//供应商代码 _print.SupplierContacter = _asn.ShipUser;//供应商联系人 _print.DeliveryNo = _asn.AsnBillNum; _print.ChineseAddress = _asn.Site_Desc; _print.EnglishAddress = ""; _print.MaterialPlanner = _asn.Buyer;//采购联系人 _print.MaterialPhone = _asn.BuyerPhone;//采购联系人电话 _print.CAAIPhone = ""; _print.CAAIFax = ""; _print.STDCOUNT = "0"; var dt = ConvertHelper.ToDataTable(new List { _print }); ds.Tables.Add(dt); SCP_ASN_CONTROLLER.Get_V_TB_ASN_DETAIL_List(new V_TB_ASN_DETAIL() { AsnBillNum = p_AsnBillNum }, (retobj) => { decimal stdtemp = 0; if (retobj.State == ReturnStatus.Succeed) { List _lsDetail = new List(); var _ls = retobj.Result.ToList(); _ls.ForEach(p => { if (p.Qty > 0) { V_ARRIVE_PRINT_DETAIL _detail = new V_ARRIVE_PRINT_DETAIL(); _detail.PartNo = p.PartCode; _detail.PartDesc = p.PartDesc1; _detail.Unit = p.PoUnit; _detail.STD = p.PackQty.ToString(); _detail.Boxes = (Math.Truncate((p.Qty / p.PackQty)) + (((p.Qty % p.PackQty) > 0) ? 1 : 0)).ToString(); _detail.DeliverQty = p.Qty.ToString(); _detail.ReceiveQty = p.Qty.ToString(); if (p.Site == "0210") { _detail.PoNo = p.AskBillNum + "(" + p.PoBillNum + ")"; } else { _detail.PoNo = _print.DeliveryNo + "(" + p.PoBillNum + ")"; } _detail.LineNO = p.PoLine.ToString(); _detail.Memo = p.Remark; _detail.DockCode = p.DockCode; _detail.ArriveState = GetArriveState(p.PoBillNum, p.PoLine, p.PartCode, p.Batch); _detail.SpecialState = string.Empty; _detail.VendBatch = p.VendBatch; if (p.ProduceDate!= null) { DateTime dttemp; if (DateTime.TryParse(p.ProduceDate.ToString(), out dttemp)) { _detail.ProduecDate = dttemp.ToString("yyyy-MM-dd");//到货日期 } } // stdtemp +=_detail.Boxes; stdtemp += Convert.ToDecimal( _detail.Boxes); _lsDetail.Add(_detail); } }); ds.Tables[0].Rows[0]["STDCOUNT"] = stdtemp.ToString(); var tb = ConvertHelper.ToDataTable(_lsDetail); ds.Tables.Add(tb); } }); } } }); return ds; } private static string GetArriveState(string PoBillNum, int PoLine, string PartCode, string Batch) { string strResult = "未到货"; using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { TB_ARRIVE_DETAIL det = db.TB_ARRIVE_DETAIL.FirstOrDefault(p => p.PoBillNum == PoBillNum && p.PoLine == PoLine && p.PartCode == PartCode && p.Batch == Batch); if (det != null) { strResult = "已到货"; } } return strResult; } public static ResultObject Del_TB_ASN(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { db.TB_ASN.Remove(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_ASN_CONTROLLER), "Del_TB_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_ASN_CONTROLLER), "Del_TB_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_ASN_CONTROLLER), "Del_TB_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; _ret.Result = false; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Del_TB_ASN", e.Message); throw e; } return _ret; } public static ResultObject Save_TB_CREATE_BARCODE_CUSTOM(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _ls = new List(); if (p_list.Count > 0) { string _asnBillNum = p_list[0].PoBillNum; var _asn = db.TB_PO.Where(p => p.IsDeleted == false && p.PoBillNum == _asnBillNum).FirstOrDefault(); List _asnList = new List(); p_list.ForEach(p => { int _qty = 0; int _packQty = 0; if (int.TryParse(p.Qty.ToString(), out _qty) && int.TryParse(p.PackQty.ToString(), out _packQty)) { int _count = _qty / _packQty; if (_qty / _packQty > 0) { for (int i = 0; i < _count; i++) { V_TB_ASK_DETAIL_PACKAGE _asndetail = new V_TB_ASK_DETAIL_PACKAGE(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _packQty; _asndetail.PoBillNum = p.PoBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.Batch = p.Batch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } if (_qty % _packQty > 0) { V_TB_ASK_DETAIL_PACKAGE _asndetail = new V_TB_ASK_DETAIL_PACKAGE(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty % _packQty; _asndetail.PoBillNum = p.PoBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.Batch = p.Batch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } } else { if (_qty > 0) { V_TB_ASK_DETAIL_PACKAGE _asndetail = new V_TB_ASK_DETAIL_PACKAGE(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty; _asndetail.PoBillNum = p.PoBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.Batch = p.Batch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } } } }); foreach (var itm in _asnList) { var _bar = SCP_BARCODE_CONTROLLER_CUSTOM.CreateBarcode(itm); _ls.Add(_bar); } db.TS_BARCODE_CUSTOM.AddOrUpdate(p => new { p.BarCode, p.BillNum }, _ls.ToArray()); } else { _ret.MessageList.Add("选择的发货明细数量为零!"); } if (_ret.MessageList.Count == 0) { 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_CREATE_SMALLBARCODE(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _ls = new List(); if (p_list.Count > 0) { string _asnBillNum = p_list[0].AsnBillNum; var _asn = db.TB_ASN.Where(p => p.IsDeleted == false && p.AsnBillNum == _asnBillNum).FirstOrDefault(); List _asnList = new List(); p_list.ForEach(p => { var _askDetail = db.TA_VEND_PART.Where(itm => itm.VendId == p.VendId && itm.PartCode == p.PartCode).FirstOrDefault(); if (_askDetail != null) { decimal _qty = 0; decimal _smallpackQty = 0; if (decimal.TryParse(p.Qty.ToString(), out _qty) && decimal.TryParse(_askDetail.MinPackQty.ToString(), out _smallpackQty)) { if (_smallpackQty == 0) { _ret.MessageList.Add("供应商:" + p.VendId + "零件号:" + p.PartCode + "最小包装为0!"); } else { decimal _n = _qty / _smallpackQty; var _count = int.Parse(Math.Truncate(_n).ToString()); if (_qty / _smallpackQty > 0) { for (int i = 0; i < _count; i++) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _smallpackQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = _smallpackQty;//小包装 _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } if (_qty % _smallpackQty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty % _smallpackQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = _smallpackQty;//小包装 _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } } else { if (_qty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = _smallpackQty;//小包装 _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asnList.Add(_asndetail); } } } } } }); foreach (var itm in _asnList) { var _bar = SCP_BARCODE_CONTROLLER.CreateSmallBarcode(itm); _ls.Add(_bar); } db.TS_BARCODE_SMALL.AddOrUpdate(p => new { p.BarCode, p.BillNum }, _ls.ToArray()); } else { _ret.MessageList.Add("选择的发货明细数量为零!"); } if (_ret.MessageList.Count == 0) { 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_CREATE_BARCODE(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _ls = new List(); if (p_list.Count > 0) { string _asnBillNum = p_list[0].AsnBillNum; var _asn = db.TB_ASN.Where(p => p.IsDeleted == false && p.AsnBillNum == _asnBillNum).FirstOrDefault(); var _asn1 = db.TB_ASN_DETAIL.Where(p => p.IsDeleted == false && p.AsnBillNum == _asnBillNum).FirstOrDefault(); List _asnList = new List(); p_list.ForEach(p => { decimal _qty = 0; decimal _packQty = 0; var partcount = p_list.Where(t=>t.PartCode==p.PartCode); if (decimal.TryParse(p.Qty.ToString(), out _qty) && decimal.TryParse(p.PackQty.ToString(), out _packQty)) { decimal _n = _qty / _packQty; var _count=int.Parse( Math.Truncate(_n).ToString()); if (_qty / _packQty > 0) { for (int i = 0; i < _count; i++) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _packQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asndetail.UpdateInfo = p.UpdateInfo; _asndetail.UpdateUser = p.UpdateUser; _asnList.Add(_asndetail); } if (_qty % _packQty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty % _packQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.UpdateInfo = p.UpdateInfo; _asndetail.Site = p.Site; _asndetail.UpdateInfo = p.UpdateInfo; _asndetail.UpdateUser = p.UpdateUser; _asnList.Add(_asndetail); } } else { if (_qty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asndetail.UpdateInfo = p.UpdateInfo; _asndetail.UpdateUser = p.UpdateUser; _asnList.Add(_asndetail); } } } }); if (_asnList.Count>=1000&& _asnList[0].Site== "U32")//本特勒大于1000时采用批量生成方式 { var groups = _asnList.GroupBy(r => new { r.PartCode, r.Batch, r.Site, r.PoBillNum, r.AskBillNum }).ToList(); foreach (var group in groups) { var list = _asnList.Where(r => r.PartCode == group.Key.PartCode && r.Batch == group.Key.Batch && r.Site == group.Key.Site && r.PoBillNum == group.Key.PoBillNum && r.AskBillNum == group.Key.AskBillNum).ToList(); var ret = SCP_BARCODE_CONTROLLER.CreateBatchBarcodeList(list); if (ret.State == ReturnStatus.Succeed) { _ls.AddRange(ret.Result); } else { _ret.MessageList.Add(ret.Message); _ret.State = ReturnStatus.Failed; _ret.Result = false; return _ret; } } db.TS_BARCODE.BulkInsert(_ls.ToArray()); } else { foreach (var itm in _asnList) { var _bar = SCP_BARCODE_CONTROLLER.CreateBarcode(itm); _ls.Add(_bar); } db.TS_BARCODE.AddOrUpdate(p => new { p.BarCode, p.BillNum }, _ls.ToArray()); } } else { _ret.MessageList.Add("选择的发货明细数量为零!"); } if (_ret.MessageList.Count == 0) { 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_PALLET(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _list = new List(); int codenum = 1; //标签计数器 string _oldpartcode = ""; //上一个托盘所装零件编号 decimal _oldbox = 0; //上一个托盘箱数计数 string _oldpalletnum = "";//上一个托盘码 p_list.ForEach(p => { var _askDetail = db.TA_VEND_PART.Where (itm => itm.VendId == p.VendId && itm.PartCode == p.PartCode ).FirstOrDefault(); if (_askDetail != null) { var _palletPack = _askDetail.PalletPackQty; #region 补充未满托盘 if (p.PartCode == _oldpartcode) { decimal _box1 = 0; int _TempQty1 = Convert.ToInt32(p.Qty); int _PackQty1 = Convert.ToInt32(p.PackQty); if (_TempQty1 / _PackQty1 > 0) { int _count = _TempQty1 / _PackQty1; _box1 += _count; if (p.Qty % p.PackQty > 0) { _box1++; } } else { if (_TempQty1 > 0) { _box1++; } } if (_box1 < _oldbox) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_box1); pallet.Qty = p.Qty; pallet.PalletNum = _oldpalletnum; pallet.BarCode = _oldpalletnum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); db.TB_PALLETS.Add(pallet); p.Qty = 0; } else { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_oldbox); pallet.Qty = _oldbox * _PackQty1; pallet.PalletNum = _oldpalletnum; pallet.BarCode = _oldpalletnum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); db.TB_PALLETS.Add(pallet); p.Qty = p.Qty - _oldbox * _PackQty1; } } #endregion #region 新托盘 if (p.Qty != 0) { decimal _box = 0; int _TempQty = Convert.ToInt32(p.Qty); int _PackQty = Convert.ToInt32(p.PackQty); if (_TempQty / _PackQty > 0) { int _count = _TempQty / _PackQty; _box += _count; if (p.Qty % p.PackQty > 0) { _box++; } } else { if (_TempQty > 0) { _box++; } } if (_box <= _palletPack) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_box); pallet.Qty = p.Qty; pallet.PalletNum = SCP_BILLCODE_CONTROLLER.MakePalletCode_QD(codenum); pallet.BarCode = pallet.PalletNum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); db.TB_PALLETS.Add(pallet); _oldpartcode = p.PartCode; _oldbox = Convert.ToInt32(_palletPack) - _box; _oldpalletnum = pallet.PalletNum; codenum++; } else { decimal _pallet = 0; _pallet += Convert.ToInt32(_box) / Convert.ToInt32(_palletPack); if (_box % _palletPack > 0) { _pallet++; } for (int i = 1; i <= _pallet; i++) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.PalletNum = SCP_BILLCODE_CONTROLLER.MakePalletCode_QD(codenum); pallet.BarCode = pallet.PalletNum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); if (i == _pallet) { pallet.Box = Convert.ToInt32(_box - Convert.ToDecimal(_palletPack) * (_pallet - 1)); pallet.Qty = p.Qty - Convert.ToDecimal(_palletPack) * (_pallet - 1) * _PackQty; } else { pallet.Box = Convert.ToInt32(_palletPack); pallet.Qty = Convert.ToDecimal(_palletPack) * _PackQty; } db.TB_PALLETS.Add(pallet); codenum++; } } } #endregion } else { string _str = string.Format("零件号:{0},供应商编号:{1},没有维护托盘标包数!请先维护供应商零件信息", p.PartCode, p.VendBatch); _list.Add(_str); } }); if (_list.Count == 0) { int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.MessageList = _list; } } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.MessageList = _list; } } } 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_ASN_CONTROLLER), "Save_TB_PALLET", 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_ASN_CONTROLLER), "Save_TB_PALLET", 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_ASN_CONTROLLER), "Save_TB_PALLET", 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_ASN_CONTROLLER), "Save_TB_PALLET", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_PALLET_BTL(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int _sumbox = p_list.Sum(p => p.Qty); SqlParameter[] prams = new SqlParameter[1]; prams[0] = new SqlParameter("@a", 105); string lastNumber = db.Database.SqlQuery("exec dbo.P_GetBillNum @a", prams).SingleOrDefault(); if (lastNumber.Length != 8 || !lastNumber.Contains('P')) { _ret.MessageList.Add("托盘生成失败!"); } string str = ""; decimal _qy = 0; foreach (var li in p_list) { var ts = db.TS_BARCODE.OrderBy(T => T.UID).Where(T => string.IsNullOrEmpty(T.PalletNum) && T.BillNum == li.AsnBillNum && T.PartCode == li.PartCode).ToList().Take(li.Qty); var tss = ts.ToList(); decimal _qty = tss.Sum(p => p.Qty); string str1 = li.PartCode + "," + li.Batch+","+ _qty+","+li.PoLine+"#"; tss.ForEach(itm => itm.PalletNum = lastNumber); db.TS_BARCODE.AddOrUpdate(itm => itm.UID, tss.ToArray()); str = str + str1; _qy = _qy + _qty; li.Box = li.Box - li.Qty; li.Qty1 = 0; db.TB_ASN_PARTBOX.AddOrUpdate(li); } var _ls = p_list.First(); TB_PALLETS_BTL pallet = new TB_PALLETS_BTL(); pallet.AsnBillNum = _ls.AsnBillNum; pallet.PalletNum = lastNumber; pallet.PoBillNum = _ls.PoBillNum; pallet.VendId = _ls.VendId; var vend = db.TA_VENDER.FirstOrDefault(p => p.VendId == _ls.VendId); pallet.VendName = vend.VendName; pallet.ShipTime = _ls.ShipTime; pallet.SumBox = _sumbox; pallet.PalletCode = lastNumber+";"+ _ls.AsnBillNum+";" + _ls.PoBillNum+";" + _ls.VendId+";"+ str; pallet.SumQty = _qy; if (p_list.Count() > 0) { int _count = 6 - p_list.Count(); if (_count > 0) { for (int a = 1; a <= _count; a++) { TB_ASN_PARTBOX _partbox = new TB_ASN_PARTBOX(); p_list.Add(_partbox); } } pallet.PackQty = p_list.First().PackQty.ToString(); pallet.PartCode = p_list.First().PartCode.ToString(); pallet.Box = p_list.First().Qty.ToString(); pallet.PackQty1 = p_list[p_list.Count - 5].PackQty==0?"": p_list[p_list.Count - 5].PackQty.ToString(); pallet.PartCode1 = string.IsNullOrEmpty(p_list[p_list.Count - 5].PartCode)?"": p_list[p_list.Count - 5].PartCode; pallet.Box1 = p_list[p_list.Count - 5].Qty==0 ? "": p_list[p_list.Count - 5].Qty.ToString(); pallet.PackQty2 = p_list[p_list.Count - 4].PackQty==0 ?"": p_list[p_list.Count - 4].PackQty.ToString(); pallet.PartCode2 =string.IsNullOrEmpty(p_list[p_list.Count - 4].PartCode)?"": p_list[p_list.Count - 4].PartCode; pallet.Box2 = p_list[p_list.Count - 4].Qty==0?"" : p_list[p_list.Count - 4].Qty.ToString(); pallet.PackQty3 = p_list[p_list.Count - 3].PackQty==0 ? "": p_list[p_list.Count - 3].PackQty.ToString(); pallet.PartCode3 = string.IsNullOrEmpty(p_list[p_list.Count - 3].PartCode)?"": p_list[p_list.Count - 3].PartCode; pallet.Box3 = p_list[p_list.Count - 3].Qty==0?"" : p_list[p_list.Count - 3].Qty.ToString(); pallet.PackQty4 = p_list[p_list.Count - 2].PackQty==0 ? "": p_list[p_list.Count - 2].PackQty.ToString(); pallet.PartCode4 = string.IsNullOrEmpty(p_list[p_list.Count - 2].PartCode)?"": p_list[p_list.Count - 2].PartCode; pallet.Box4 = p_list[p_list.Count - 2].Qty==0 ? "": p_list[p_list.Count - 2].Qty.ToString(); pallet.PackQty5 = p_list.Last().PackQty == 0 ? "" : p_list.Last().PackQty.ToString(); pallet.PartCode5 =string.IsNullOrEmpty(p_list.Last().PartCode) ? "" : p_list.Last().PartCode; pallet.Box5 = p_list.Last().Qty==0 ?"": p_list.Last().Qty.ToString(); pallet.IsDeleted = false; } db.TB_PALLETS_BTL.AddOrUpdate(pallet); 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_ASN_CONTROLLER), "Save_TB_PALLET_BTL", 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_ASN_CONTROLLER), "Save_TB_PALLET_BTL", 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_ASN_CONTROLLER), "Save_TB_PALLET_BTL", 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_ASN_CONTROLLER), "Save_TB_PALLET_BTL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_PALLET_JZJH(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _list = new List(); string _oldpartcode = ""; //上一个托盘所装零件编号 decimal _oldbox = 0; //上一个托盘箱数计数 string _oldpalletnum = "";//上一个托盘码 string _oldpalletfullnum = "";//上一个托盘码 p_list.ForEach(p => { var _askDetail = db.TA_VEND_PART.Where (itm => itm.VendId == p.VendId && itm.PartCode == p.PartCode ).FirstOrDefault(); if (_askDetail != null) { var _palletPack = _askDetail.PalletPackQty; #region 补充未满托盘 if (p.PartCode== _oldpartcode) { decimal _box1 = 0; int _TempQty1 = Convert.ToInt32(p.Qty); int _PackQty1 = Convert.ToInt32(p.PackQty); if (_TempQty1 / _PackQty1 > 0) { int _count = _TempQty1 / _PackQty1; _box1 += _count; if (p.Qty % p.PackQty > 0) { _box1++; } } else { if (_TempQty1 > 0) { _box1++; } } if(_box1< _oldbox) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_box1); pallet.Qty = p.Qty; pallet.PalletNum = _oldpalletnum; pallet.BarCode = _oldpalletnum; pallet.FullBarCode = _oldpalletfullnum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate =Convert.ToDateTime( p.ProduceDate); db.TB_PALLETS.Add(pallet); p.Qty = 0; } else { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_oldbox); pallet.Qty = _oldbox* _PackQty1; pallet.PalletNum = _oldpalletnum; pallet.BarCode = _oldpalletnum; pallet.FullBarCode = _oldpalletfullnum; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); db.TB_PALLETS.Add(pallet); p.Qty = p.Qty- _oldbox * _PackQty1; } } #endregion #region 新托盘 if (p.Qty != 0) { decimal _box = 0; int _TempQty = Convert.ToInt32(p.Qty); int _PackQty = Convert.ToInt32(p.PackQty); if (_TempQty / _PackQty > 0) { int _count = _TempQty / _PackQty; _box += _count; if (p.Qty % p.PackQty > 0) { _box++; } } else { if (_TempQty > 0) { _box++; } } if (_box <= _palletPack) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.Box = Convert.ToInt32(_box); pallet.Qty = p.Qty; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); var ret = SCP_BARCODE_CONTROLLER.CreatePallets(pallet); if (ret.State == ReturnStatus.Succeed) { pallet.BarCode = pallet.PalletNum; db.TB_PALLETS.Add(pallet); _oldpartcode = p.PartCode; _oldbox = Convert.ToInt32(_palletPack) - _box; _oldpalletnum = pallet.PalletNum; _oldpalletfullnum= pallet.FullBarCode; } } else { decimal _pallet = 0; _pallet += Convert.ToInt32(_box) / Convert.ToInt32(_palletPack); if (_box % _palletPack > 0) { _pallet++; } for (int i = 1; i <= _pallet;i++) { TB_PALLETS pallet = new TB_PALLETS(); pallet.AsnBillNum = p.AsnBillNum; pallet.PartCode = p.PartCode; pallet.Batch = p.Batch; pallet.VendId = p.VendId; pallet.VendBatch = p.VendBatch; pallet.PoBillLine = p.PoLine; pallet.PoBillNum = p.PoBillNum; pallet.ProduceDate = Convert.ToDateTime(p.ProduceDate); if (i == _pallet) { pallet.Box = Convert.ToInt32(_box - Convert.ToDecimal(_palletPack) * (_pallet - 1)); pallet.Qty = p.Qty - Convert.ToDecimal(_palletPack) * (_pallet - 1) * _PackQty; } else { pallet.Box = Convert.ToInt32(_palletPack); pallet.Qty = Convert.ToDecimal(_palletPack) * _PackQty; } var ret = SCP_BARCODE_CONTROLLER.CreatePallets(pallet); if (ret.State == ReturnStatus.Succeed) { pallet.BarCode = pallet.PalletNum; db.TB_PALLETS.Add(pallet); } } } } #endregion } else { string _str = string.Format("零件号:{0},供应商编号:{1},没有维护托盘标包数!请先维护供应商零件信息", p.PartCode, p.VendBatch); _list.Add(_str); } }); int state = db.SaveChanges(); if (_list.Count == 0) { if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.MessageList = _list; } } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.MessageList = _list; } } } 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_ASN_CONTROLLER), "Save_TB_PALLET_JZJH", 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_ASN_CONTROLLER), "Save_TB_PALLET_JZJH", 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_ASN_CONTROLLER), "Save_TB_PALLET_JZJH", 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_ASN_CONTROLLER), "Save_TB_PALLET", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_CREATE_BARCODE_SYJB(List p_list) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { List _ls = new List(); if (p_list.Count > 0) { string _asnBillNum = p_list[0].AsnBillNum; var _asn = db.TB_ASN.Where(p => p.IsDeleted == false && p.AsnBillNum == _asnBillNum).FirstOrDefault(); List _asnList = new List(); p_list.ForEach(p => { int _qty = 0; int _packQty = 0; if (int.TryParse(p.Qty.ToString(), out _qty) && int.TryParse(p.PackQty.ToString(), out _packQty)) { int _count = _qty / _packQty; if (_qty / _packQty > 0) { for (int i = 0; i < _count; i++) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _packQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asndetail.AskBillNum=p.AskBillNum; _asnList.Add(_asndetail); } if (_qty % _packQty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty % _packQty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asndetail.AskBillNum = p.AskBillNum; _asnList.Add(_asndetail); } } else { if (_qty > 0) { V_TB_ASN_DETAIL _asndetail = new V_TB_ASN_DETAIL(); _asndetail.PoBillNum = p.PoBillNum; _asndetail.Batch = p.Batch; _asndetail.PartCode = p.PartCode; _asndetail.ProduceDate = p.ProduceDate; _asndetail.Qty = _qty; _asndetail.AsnBillNum = p.AsnBillNum; _asndetail.PoLine = p.PoLine; _asndetail.VendId = p.VendId; _asndetail.VendBatch = p.VendBatch; _asndetail.CreateTime = p.CreateTime; _asndetail.CreateUser = p.CreateUser; _asndetail.Remark = p.Remark; _asndetail.PackQty = p.PackQty; _asndetail.PoUnit = p.PoUnit; _asndetail.Site = p.Site; _asndetail.AskBillNum = p.AskBillNum; _asnList.Add(_asndetail); } } } }); foreach (var itm in _asnList) { var _bar = SCP_BARCODE_CONTROLLER_SYJB.CreateBarcode(itm); _ls.Add(_bar); } db.TS_BARCODE.AddOrUpdate(p => new { p.BarCode, p.BillNum }, _ls.ToArray()); } else { _ret.MessageList.Add("选择的发货明细数量为零!"); } if (_ret.MessageList.Count == 0) { 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_CREATE_BARCODE", 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_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Get_TS_UNI_API(V_TB_ASN p_asn) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _list = db.TS_UNI_API.Where(p => p.BillNum == p_asn.AsnBillNum&&p.State==1).ToList(); if (_list.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.Result = false; } else { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } } } 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Get_TB_ASN_BACH(V_TB_ASN p_asn) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { int i = 0; List asn1 =new List (); var asn = db.V_TB_ASN_DETAIL.Count(p=>p.VendId== p_asn.VendId&& p.State==0&&p.AsnBillNum!=p_asn.AsnBillNum); if (asn > 0) { var _list = db.V_TB_ASN_DETAIL.Where(p => p.AsnBillNum == p_asn.AsnBillNum&& p.VendId == p_asn.VendId).ToList(); foreach(var part in _list) { var pa = db.V_TB_ASN_DETAIL.Count(p => p.PartCode == part.PartCode&&p.State==0&&p.ProduceDate 0) { var pa1 = db.V_TB_ASN_DETAIL.Where(p => p.PartCode == part.PartCode && p.State == 0 && p.ProduceDate < part.ProduceDate && p.VendId == p_asn.VendId).ToList(); foreach(var p in pa1) { if (!asn1.Contains(p.AsnBillNum)) { asn1.Add(p.AsnBillNum); } } i++; } } } string STR = ""; if (asn1.Count() > 0) { STR = String.Join(";", asn1); } if (i > 0) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = STR; } else { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } } } 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", 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_ASN_CONTROLLER), "Get_TS_UNI_API", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Save_TB_ASN_DETAIL(V_TB_ASN p_asn, List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { DateTime now = DateTime.Now; List consumlist = new List(); List _ls = new List(); var _asn =db.TB_ASN.Where(p => p.IsDeleted == false && p.AsnBillNum == p_asn.AsnBillNum).ToList(); if (_asn != null && _asn.Count>0) { _asn.ForEach(p => { p.State = (int)AsnState.Ship; p.CreateTime = now; }); db.TB_ASN.AddOrUpdate(p => p.UID, _asn.ToArray()); var ask = db.TB_ASK.Where(p => p.IsDeleted == false && p.AskBillNum == p_asn.AskBillNum).ToList(); ask.ForEach(p => p.State = (int)AskState.Ship); db.TB_ASK.AddOrUpdate(p => p.UID, ask.ToArray()); var Asn = p_entitys.First(); var po = db.TB_PO.FirstOrDefault(p=>p.PoBillNum==Asn.PoBillNum); if (po.UpdateInfo == "Y"&&po.Site=="W21") { p_entitys.ForEach(p => { var _maxpolist = db.TB_MAXPO_DETAIL.Where(t => t.PartCode == p.PartCode && t.VendId == p.Extend1&&t.AsnQty>0).ToList().OrderBy(t=>t.ContractMonth); decimal sum = 0; if (_maxpolist.Count() > 0) { sum = _maxpolist.Sum(t => t.AsnQty); } decimal Qtyy = p.Qty; if(sum< Qtyy) { _ret.MessageList.Add(string.Format("确认发货失败,零件号{0}大订单剩余数量不足,请联系业务人员维护", p.PartCode)); } else { foreach (var _maxpo in _maxpolist) { if (_maxpo.AsnQty > Qtyy) { _maxpo.AsnQty = _maxpo.AsnQty - Qtyy; TB_MAXPO_CONSUME consumne = new TB_MAXPO_CONSUME(); consumne.PoBillNum = _maxpo.PoBillNum; consumne.BillNum = p.AsnBillNum; consumne.PoQty = Qtyy; consumne.VendId = _maxpo.VendId; consumne.PartCode = _maxpo.PartCode; consumne.ShipTime = _maxpo.CreateTime; consumlist.Add(consumne); db.TB_MAXPO_DETAIL.AddOrUpdate(_maxpo); break; } else if (_maxpo.AsnQty == Qtyy) { TB_MAXPO_CONSUME consumne = new TB_MAXPO_CONSUME(); consumne.PoBillNum = _maxpo.PoBillNum; consumne.BillNum = p.AsnBillNum; consumne.PoQty = Qtyy; consumne.VendId = _maxpo.VendId; consumne.PartCode = _maxpo.PartCode; consumne.ShipTime = _maxpo.CreateTime; consumlist.Add(consumne); _maxpo.AsnQty = 0; db.TB_MAXPO_DETAIL.AddOrUpdate(_maxpo); break; } else if (_maxpo.AsnQty < Qtyy) { TB_MAXPO_CONSUME consumne = new TB_MAXPO_CONSUME(); consumne.PoBillNum = _maxpo.PoBillNum; consumne.BillNum = p.AsnBillNum; consumne.PoQty = _maxpo.AsnQty; consumne.VendId = _maxpo.VendId; consumne.PartCode = _maxpo.PartCode; consumne.ShipTime = _maxpo.CreateTime; Qtyy = Qtyy - _maxpo.AsnQty; consumlist.Add(consumne); _maxpo.AsnQty = 0; db.TB_MAXPO_DETAIL.AddOrUpdate(_maxpo); } } } }); } p_entitys.ForEach(itm => { var _sum = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == p_asn.AskBillNum && p.PartCode == itm.PartCode && p.PoBillNum == p_asn.PoBillNum && p.PoLine == itm.PoLine && p.IsDeleted == false).Select(p => p.AskQty).Sum();//要货单要货数量 var _currSum = db.V_TB_ASN_DETAIL.Where(p => p.AskBillNum == p_asn.AskBillNum && p.PartCode == itm.PartCode && p.PoBillNum == p_asn.PoBillNum && p.PoLine == itm.PoLine && p.IsDeleted == false).Select(p => p.Qty).Sum();//当前存在发货单数量 var _curr = db.V_TB_ASN_DETAIL.Where(p => p.AsnBillNum == p_asn.AsnBillNum && p.PartCode == itm.PartCode && p.PoBillNum == p_asn.PoBillNum && p.PoLine == itm.PoLine && p.IsDeleted == false).Select(p => p.Qty).Sum(); var _rejct = db.TB_ARRIVE_DETAIL.Where(p => p.Remark == p_asn.AskBillNum && p.PartCode == itm.PartCode && p.PoBillNum == p_asn.PoBillNum && p.PoLine == itm.PoLine && p.IsDeleted == false&&p.BillType==3).Select(p => p.Qty).ToList().Sum(); //if (_currSum - _curr + itm.Qty- Math.Abs(_rejct) <= _sum) //{ TB_ASN_DETAIL _detail = new TB_ASN_DETAIL(); _detail.UID = itm.UID; _detail.UnConv = itm.UnConv; _detail.AsnBillNum = itm.AsnBillNum; _detail.DockCode = itm.DockCode; _detail.Currency = itm.Currency; _detail.Batch = itm.Batch; _detail.CreateUser = itm.CreateUser; _detail.CreateTime = now; _detail.PackQty = itm.PackQty; _detail.PartCode = itm.PartCode; _detail.PoLine = itm.PoLine; _detail.PoBillNum = itm.PoBillNum; _detail.Price = itm.Price; _detail.VendBatch = itm.VendBatch; _detail.ProduceDate = itm.ProduceDate; _detail.Remark = itm.Remark; _detail.Qty = itm.Qty; _detail.PoUnit = itm.PoUnit; _detail.LocUnit = itm.LocUnit; _detail.IsDeleted = itm.IsDeleted; _detail.State = (int)AsnState.Ship; _detail.GUID = itm.Guid; _detail.UpdateInfo = itm.UpdateInfo; _detail.UpdateUser = itm.UpdateUser; _detail.Extend1 = itm.Extend1; _ls.Add(_detail); var UNI = db.TS_UNI_API.Count(p=>p.BillNum==itm.AsnBillNum);//多组件订单更新中间表 if (UNI > 0) { var UN = db.TS_UNI_API.Where(p => p.BillNum == itm.AsnBillNum).ToList(); foreach(var u in UN) { u.PutTime = DateTime.Now; u.State = 1; db.TS_UNI_API.AddOrUpdate(u); } } else { TS_UNI_API api = UniApiController.CreateBy(p_asn, itm, UniApiType.Receive); api.State = 1; db.TS_UNI_API.AddOrUpdate(api);//同步更新中间表 } } ); var _asnBillNum = _asn[0].AsnBillNum; //var _barcodeList=db.TS_BARCODE.Where(p => p.BillNum == _asnBillNum).ToList(); //_barcodeList.ForEach(itm => { // TS_UNI_API _api =UniApiController.CreateBy(_asn[0], itm, UniApiType.BarCode); // _api.State = 1; // db.TS_UNI_API.AddOrUpdate(_api); //}); } if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Failed; _ret.Result = false; } else { db.TB_ASN_DETAIL.AddOrUpdate(p => p.UID, _ls.ToArray()); if (consumlist.Count() > 0) { db.TB_MAXPO_CONSUME.AddRange(consumlist); } 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_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Save_TB_ASN_DETAIL", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static ResultObject Del_TB_ASK_DETAIL(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { db.TB_ASK_DETAIL.Remove(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_ASN_CONTROLLER), "Del_TB_ASK_DETAIL", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Del_TB_ASK_DETAIL", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Del_TB_ASK_DETAIL", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Del_TB_ASK_DETAIL", e.Message); throw e; } return _ret; } public static void Get_V_TB_ASN_DETAIL_VIEW_List(V_TB_ASN_DETAIL_VIEW p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_ASN_DETAIL_VIEW; if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (p_entity.PoLine != 0) { q = q.Where(p => p.PoLine == p_entity.PoLine); } if (!string.IsNullOrEmpty(p_entity.PartCode)) { q = q.Where(p => p.PartCode.Contains(p_entity.PartCode)); } if (p_entity.Price != 0) { q = q.Where(p => p.Price == p_entity.Price); } if (!string.IsNullOrEmpty(p_entity.Currency)) { q = q.Where(p => p.Currency.Contains(p_entity.Currency)); } if (p_entity.PackQty != 0) { q = q.Where(p => p.PackQty == p_entity.PackQty); } if (!string.IsNullOrEmpty(p_entity.DockCode)) { q = q.Where(p => p.DockCode.Contains(p_entity.DockCode)); } if (p_entity.State != null) { q = q.Where(p => p.State == p_entity.State); } if (!string.IsNullOrEmpty(p_entity.PartDesc1)) { q = q.Where(p => p.PartDesc1.Contains(p_entity.PartDesc1)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.PartDesc2)) { q = q.Where(p => p.PartDesc2.Contains(p_entity.PartDesc2)); } if (!string.IsNullOrEmpty(p_entity.AsnBillNum)) { q = q.Where(p => p.AsnBillNum.Contains(p_entity.AsnBillNum)); } if (p_entity.Qty != 0) { q = q.Where(p => p.Qty == p_entity.Qty); } if (!string.IsNullOrEmpty(p_entity.VendBatch)) { q = q.Where(p => p.VendBatch.Contains(p_entity.VendBatch)); } if (!string.IsNullOrEmpty(p_entity.Batch)) { q = q.Where(p => p.Batch.Contains(p_entity.Batch)); } if (!string.IsNullOrEmpty(p_entity.VendId)) { q = q.Where(p => p.VendId==p_entity.VendId); } if (!string.IsNullOrEmpty(p_entity.AskBillNum)) { q = q.Where(p => p.AskBillNum.Contains(p_entity.AskBillNum)); } if (!string.IsNullOrEmpty(p_entity.PoBillNum)) { q = q.Where(p => p.PoBillNum.Contains(p_entity.PoBillNum)); } if (!string.IsNullOrEmpty(p_entity.ReceivedPort)) { q = q.Where(p => p.ReceivedPort.Contains(p_entity.ReceivedPort)); } if (!string.IsNullOrEmpty(p_entity.PlateNumber)) { q = q.Where(p => p.PlateNumber.Contains(p_entity.PlateNumber)); } if (!string.IsNullOrEmpty(p_entity.ShipUser)) { q = q.Where(p => p.ShipUser.Contains(p_entity.ShipUser)); } if (!string.IsNullOrEmpty(p_entity.ReceiveUser)) { q = q.Where(p => p.ReceiveUser.Contains(p_entity.ReceiveUser)); } 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 (p_entity.ShipTimeBegin != null) { q = q.Where(p => p.ShipTime>=p_entity.ShipTimeBegin); } if (p_entity.ShipTimeEnd != null) { q = q.Where(p => p.ShipTime <= p_entity.ShipTimeEnd); } if (p_entity.ReceiveTimeBegin != null) { q = q.Where(p => p.ReceiveTime >= p_entity.ReceiveTimeBegin); } if (p_entity.ReceiveTimeEnd != null) { q = q.Where(p => p.ReceiveTime <= p_entity.ReceiveTimeEnd); } 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.Site)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASN_CONTROLLER), "Get_V_TB_ASN__DETAIL_View_List", e.Message); throw e; } } //public static List Get_CAN_SHIPQTY(List p_list) //{ // using (ScpEntities db = EntitiesFactory.CreateScpInstance()) // { // var detail = p_list[0]; // var list = db.V_TB_ASN_DETAIL.Where(p => p.IsDeleted == false && detail.AskBillNum == p.AskBillNum).ToList(); // var groupList = list.GroupBy(p => new { p.PoBillNum, p.PoLine, p.PartCode, p.AskBillNum }).Select( // p => new { // ShipSum = p.Sum(itm => itm.Qty), // PoLine = p.Key.PoLine, // PoBillNum = p.Key.PoBillNum, // PartCode = p.Key.PartCode, // AskBillNum = p.Key.AskBillNum // }).ToList(); // p_list.ForEach(itm => { // groupList.ForEach(p => // { // if (p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.PartCode == itm.PartCode) // { // itm.ShippedQty = p.ShipSum; // } // }); // decimal a = itm.AskQty - (itm.ShippedQty == null ? 0 : (decimal)itm.ShippedQty); // itm.CanQty = a; // itm.PackQty = (itm.PackQty == 0) ? 1 : itm.PackQty; // }); // } // return p_list; //} public static List Get_CAN_PALLET_QTY(List p_list) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var detail = p_list[0]; var list = db.V_TB_PALLET_DETAIL.Where(p => p.IsDeleted == false && detail.AsnBillNum == p.AsnBillNum).ToList(); var groupList=list.GroupBy(p => new { p.AsnBillNum, p.PoBillNum, p.PoLine, p.PartCode }) .Select( p => new { PalletSum = p.Sum(itm => itm.Qty), PoLine = p.Key.PoLine, PoBillNum = p.Key.PoBillNum, PartCode = p.Key.PartCode, AsnBillNum=p.Key.AsnBillNum, } ).ToList(); p_list.ForEach(itm => { decimal palletedNum = 0; groupList.ForEach(p => { if (p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.PartCode == itm.PartCode) { palletedNum = p.PalletSum; } }); decimal a = itm.Qty- palletedNum; itm.CanQty = a; itm.PackQty = (itm.PackQty == 0) ? 1 : itm.PackQty; }); } return p_list; } /// /// 创建发货单 /// /// /// /// /// /// /// public static ResultObject Save_ASN_TO_PALLET(V_TB_ASN p_ask, List p_list, string p_PlateNumber , string p_buyer, string p_buyerPhone) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _asn=db.V_TB_ASN.Where(p => p.AsnBillNum == p_ask.AsnBillNum).FirstOrDefault(); TB_PALLET _t = new TB_PALLET(); _t.AsnBillNum = _asn.AsnBillNum; _t.PalletNum= SCP_BILLCODE_CONTROLLER.MakePalletCode(); _t.AskBillNum = _asn.AskBillNum; _t.PoBillNum = _asn.PoBillNum; _t.ReceivedPort = _asn.ReceivedPort; _t.CreateUser = _asn.CreateUser; _t.CreateTime = _asn.CreateTime; _t.UpdateUser = p_buyer; _t.UpdateTime = _asn.CreateTime; _t.ReceiveUser = _asn.Buyer; _t.ReceiveTime = _asn.ReceiveTime; _t.VendId = _asn.VendId; _t.IsDeleted = false;//操作员 _t.CreateTime = _asn.CreateTime; _t.CreateUser = _asn.CreateUser; _t.Site = _asn.Site; _t.Remark = _asn.Remark; _t.State = (int)AsnState.New; _t.Remark = p_ask.Remark; _t.GUID = Guid.NewGuid(); _t.PlateNumber = string.Empty; _t.ShipUser = p_ask.ShipUser; _t.ShipTime = p_ask.ShipTime; _t.ReceiveTime = p_ask.ReceiveTime; db.TB_PALLET.AddOrUpdate(p => p.PalletNum, _t); List _list = new List(); foreach (var itm in p_list) { var _asndetail=db.V_TB_ASN_DETAIL.Where(p => p.AsnBillNum == itm.AsnBillNum && p.PartCode == itm.PartCode && p.PoBillNum == itm.PoBillNum && p.PoLine == itm.PoLine && p.IsDeleted==false).FirstOrDefault(); TB_PALLET_DETAIL _tDetail = new TB_PALLET_DETAIL(); _tDetail.PoBillNum = _t.PoBillNum; _tDetail.AsnBillNum = _t.AsnBillNum; _tDetail.PalletNum = _t.PalletNum; _tDetail.PoLine = _asndetail.PoLine; _tDetail.PoUnit = _asndetail.PoUnit; _tDetail.Batch = _asndetail.Batch; if (!string.IsNullOrEmpty(_asndetail.VendBatch)) { _tDetail.VendBatch = _asndetail.VendBatch; } else { _tDetail.VendBatch = _asndetail.Batch; } _tDetail.PackQty = _asndetail.PackQty == 0 ? 1 : _asndetail.PackQty; _tDetail.PartCode = _asndetail.PartCode; _tDetail.State = (int)AsnState.New; _tDetail.Qty = (decimal)itm.Qty; _tDetail.Price = _asndetail.Price; _tDetail.EndTime = _asndetail.EndTime; _tDetail.CreateTime = _t.CreateTime; _tDetail.CreateUser = _t.CreateUser; _tDetail.ProduceDate = _asndetail.ProduceDate; _tDetail.IsDeleted = false; _tDetail.DockCode = _asndetail.ReceivedPort; _tDetail.LocUnit = _asndetail.LocUnit; _tDetail.UnConv = _asndetail.UnConv; _tDetail.State = _t.State; _tDetail.ReceivedPort = _asndetail.ReceivedPort; _tDetail.GUID = Guid.NewGuid(); db.TB_PALLET_DETAIL.Add(_tDetail); //var _order_detail = db.TB_ASK_DETAIL.Where(p => p.UID == itm.UID).FirstOrDefault(); //_order_detail.ShippedQty = (decimal)itm.ShippedQty; //db.TB_ASK_DETAIL.AddOrUpdate(p => p.UID, _order_detail); } int state = db.SaveChanges(); if (state != -1) { //var _askDetailList = db.V_TB_ASK_DETAIL.Where(p => p.AskBillNum == p_ask.AskBillNum && p.IsDeleted == false).ToList();//查询要货单明细 //var _lst = Get_CAN_SHIPQTY(_askDetailList);//算出可发数量 //if (_lst.Select(p => p.CanQty).Sum() <= 0)//可发数量为零时,修改状态 //{ // var _ary = db.TB_ASK.Where(p => p.AskBillNum == p_ask.AskBillNum && p.IsDeleted == false).ToList(); // _ary.ForEach(p => p.State = (int)AskState.Ship); // if (_ary.Count() > 0) // { // db.TB_ASK.AddOrUpdate(_ary.ToArray()); // } //} _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_ASK_CONTROLLER), "Save_ASK_TO_ASN", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 数据导入 /// /// /// /// /// /// /// public static ResultObject EXCEL_ASN(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_ASN_CONTROLLER), "EXCEL_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_ASN_CONTROLLER), "EXCEL_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_ASN_CONTROLLER), "EXCEL_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_ASN_CONTROLLER), "EXCEL_ASN", 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== Int32.Parse( 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; } } }