using System; using System.Collections.Generic; using System.Data.Entity.Migrations; using System.Linq; using CK.SCP.Models; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Models.UniApiEntity; using CK.SCP.Utils; using CK.SCP.Controller; using System.Text; using System.Data.Entity.Core; using System.Text.RegularExpressions; using CK.SCP.Models.AppBoxEntity; using System.ComponentModel; using CK.SCP.Common; using System.Configuration; using ChangKeTec.Wms.Controller.Report; using System.Data; using ChangKeTec.Wms.Controller.Bill; using System.Reflection; using CK.SCP.UniApi.Controller; using System.Windows.Documents; using System.Web.UI.MobileControls; using static ChangKeTec.Wms.Controller.Bill.QmStockController; using static CK.SCP.UniApi.Controller.PriceController; using static CK.SCP.UniApi.Controller.Vendercontroller; using static CK.SCP.UniApi.Controller.vendidpartcontroller; using static CK.SCP.UniApi.Controller.PoBillNumcontroller; using static CK.SCP.UniApi.Controller.NetPriceController; namespace CK.SCP.GrupUniApi.Controller { public class TP_TB_PO_PLAN { public string Domain { get; set; } public Nullable EndTime { get; set; } public string PoBillNum { get; set; } public string Site { get; set; } public string VendId { get; set; } public string ProjectId { get; set; } } public static class OdbcApiQadController { public static void GetNewCtrlList() { UniApiEntities idb = EntitiesFactory.CreateUniApiInstance(); idb.Database.CommandTimeout = 60 * 20; var tableNameList = EnumHelper.EnumToList(); //DataTable b = QmStockController.GetInvBatchStock(); // var qadCtrlList = idb.xxqad_ctrl.Where(p => p.xxqad_domain == ScpCache.Config.QAD域 && p.xxqad_scm == "0").ToList(); var qadCtrlList = idb.xxerp_MaxbatchNO.ToList(); foreach (var qadCtrl in qadCtrlList) { try { ScpEntities wdb = EntitiesFactory.CreateScpInstance(); var authdb = EntitiesFactory.CreateAppBoxInstance(); switch (qadCtrl.tablename) { case "part": //物料 if (GlobalVar.ApiConfig.Qad零件) { // Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateParts(idb, wdb, qadCtrl); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); } else continue; break; case "price": //价格 if (GlobalVar.ApiConfig.Qad价格) { // Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdatePrice(idb, wdb, qadCtrl); // qadCtrl.xxqad_scm = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); } else continue; break; case "Netprice": //净格 if (GlobalVar.ApiConfig.Qad净价) { // Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdatePrice1(idb, wdb, qadCtrl); // qadCtrl.xxqad_scm = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); } else continue; break; case "vendid": //供应商 if (GlobalVar.ApiConfig.Qad供应商) { //Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateVender(idb, wdb, authdb, qadCtrl); qadCtrl.Remark = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); var qadDataList = wdb.TA_VENDER.Where(p => p.UID > (qadCtrl.MaxdibatchNO - 100)).ToList(); UpdateVenderExtend(idb, wdb, authdb, qadDataList); EntitiesFactory.SaveDb(authdb); } else continue; break; case "vendidpart": //物料 if (GlobalVar.ApiConfig.Qad供应商零件) { // Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateVendParts(idb, wdb, qadCtrl); // qadCtrl.xxqad_scm = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); } else continue; break; case "PoBillNum": //采购订单 if (GlobalVar.ApiConfig.Qad采购订单) { //Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); // UpdatePurchaseOrders(idb, wdb, qadCtrl.xxqad_seq); UpdatePurchaseOrders(idb, wdb, qadCtrl); //qadCtrl.xxqad_scm = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); EntitiesFactory.SaveDb(authdb); } else continue; break; } } 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); }); } //qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); //qadCtrl.xxqad_rmks += " SCM:" + sb.ToString(); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { //qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); //qadCtrl.xxqad_rmks += " SCM:" + ex.Message+ex.InnerException.Message; } catch (Exception ex) { //qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); //qadCtrl.xxqad_rmks += " SCM:" + ex.Message + ex.InnerException.Message; } finally { EntitiesFactory.SaveDb(idb); } } UpdatePlanToAsk(); } private static void UpdatePlanToAsk() { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { try { var _ls = db.Database.SqlQuery( "SELECT\n" + " [Domain]\n" + " ,[EndTime]\n" + " ,[PoBillNum]\n" + " ,[Site]\n" + " ,[VendId]\n" + "FROM [V_TB_PO_PLAN]\n" + "where ISPLAN = 'P' and ASKBillNum is null\n" + "GROUP BY EndTime, PoBillNum,VendId,[Site],[Domain]" ).ToList(); Console.WriteLine($"发现MRP接口数据:" + _ls.Count().ToString()); List _vendList = new List(); var _detail = db.Database.SqlQuery("select * from v_tb_po_plan where ISPLAN = 'p' and askbillnum is null").ToList(); var _first = _detail.FirstOrDefault(); foreach (var itm in _ls) { TB_ASK _ask = new TB_ASK(); _ask.AskBillNum = SCP_BILLCODE_CONTROLLER.MakePLAN_ASKCode(); _ask.VendId = itm.VendId; _ask.State = (int)AskState.Release; _ask.PoBillNum = itm.PoBillNum; _ask.ModType = (int)BillModType.Contract; _ask.Site = itm.Domain; _ask.Remark = string.Empty; _ask.BeginTime = DateTime.Now; _ask.Buyer = "MRP"; _ask.BuyerPhone = string.Empty; _ask.ErpBillNum = itm.ProjectId; _ask.EndTime = itm.EndTime; _ask.CreateTime = DateTime.Now; _ask.CreateUser = "MRP"; _ask.GUID = Guid.NewGuid(); _ask.ReceivedPort = string.Empty; _ask.IsDeleted = false; db.TB_ASK.Add(_ask); _vendList.Add(_ask.VendId); var _askDetail = _detail.Where(p => p.EndTime == itm.EndTime && p.PoBillNum == itm.PoBillNum && p.VendId == itm.VendId && p.Domain == itm.Domain).ToList(); List _lst = new List(); foreach (var detail in _askDetail) { TB_ASK_DETAIL _t_ASK_Detail = new TB_ASK_DETAIL(); _t_ASK_Detail.PoLine = detail.PoLine; _t_ASK_Detail.Currency = string.Empty; _t_ASK_Detail.AskBillNum = _ask.AskBillNum; _t_ASK_Detail.PoBillNum = _ask.PoBillNum; _t_ASK_Detail.PoUnit = detail.PoUnit; _t_ASK_Detail.CreateTime = DateTime.Now; _t_ASK_Detail.CreateUser = _ask.CreateUser; _t_ASK_Detail.PartCode = detail.PartCode; _t_ASK_Detail.BeginTime = _ask.BeginTime; _t_ASK_Detail.EndTime = _ask.EndTime; _t_ASK_Detail.TempQty = 0; _t_ASK_Detail.AskQty = detail.PlanQty; _t_ASK_Detail.IsDeleted = false; _t_ASK_Detail.LocUnit = detail.PoUnit; _t_ASK_Detail.UnConv = 1; _t_ASK_Detail.DockCode = string.Empty; _t_ASK_Detail.State = _ask.State; _t_ASK_Detail.Price = 0; _t_ASK_Detail.GUID = Guid.NewGuid(); _t_ASK_Detail.Remark = _ask.Site; _lst.Add(detail.UID); db.TB_ASK_DETAIL.Add(_t_ASK_Detail); } var _planList = db.TB_PO_PLAN.Where(p => _lst.Contains(p.UID)).ToList(); _planList.ForEach(p => { p.Extend2 = "F"; }); db.TB_PO_PLAN.AddOrUpdate(_planList.ToArray()); EntitiesFactory.SaveDb(db); } BackgroundWorker _sendMail = new BackgroundWorker(); _sendMail.DoWork += (sender, e) => { foreach (var itm in _vendList) { SendMail(itm, "新看板生成成功", "请去SCP系统查看新看板", _first.Site); } }; _sendMail.RunWorkerCompleted += (sender, e) => { Console.WriteLine($"邮件已发送!"); }; Console.WriteLine($"完成MRP接口数据:" + _ls.Count().ToString()); } catch (Exception e) { Console.WriteLine($"更新MRP出错:{e.Message}"); } } } public static void SendMail(string p_VendId, string p_Content, string p_title, string p_site) { try { using (AppBoxContext db = EntitiesFactory.CreateAppBoxInstance()) { StringBuilder _buffer = new StringBuilder(); _buffer.Append("SELECT b.Email "); _buffer.Append(" FROM VenderUsers AS a INNER JOIN "); _buffer.Append(" Users AS b ON a.UserId = b.ID INNER JOIN "); _buffer.Append(" FactoryUsers AS c ON a.UserId = c.UserID INNER JOIN "); _buffer.Append(" TA_FACTORY AS d ON c.FACTORY_ID = d.ID INNER JOIN "); _buffer.Append(" Users AS f ON c.UserID = f.ID INNER JOIN "); _buffer.Append(" RoleUsers AS g ON a.UserId = g.UserID INNER JOIN "); _buffer.Append(" Roles AS h ON g.RoleID = h.ID "); _buffer.AppendFormat(" WHERE(h.Name = '供应商' and a.VenderId='{0}' and d.FactoryId='{1}' ) ", p_VendId, p_site); var _userList = db.Database.SqlQuery(_buffer.ToString()).ToList(); if (_userList != null && _userList.Count > 0) { foreach (var itm in _userList) { string mails = ""; mails += itm + ","; if (mails != "") { mails = mails.Substring(0, mails.Length - 1); } string mailbody = ""; mailbody += "尊敬的供应商
"; mailbody += "你好!

"; mailbody += "" + p_Content + " ,请到供应商协同网站" + ConfigurationManager.AppSettings["ResetPassWord"] + "打开查看!
"; mailbody += "



日期:" + DateTime.Now; MyWebSmtpMail webmail = new MyWebSmtpMail(); webmail.Send(mails, mailbody, p_title); } } } } catch (Exception e) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(MyWebSmtpMail), "MailManager", e.Message); } } /// ///接收收货单 /// /// /// /// 流水号 /// 0:收货,1:退货 private static void AddReceive(UniApiEntities idb, ScpEntities wdb, string seq, int billtype) { string _rec = "SELECT * FROM [xxqad_prh_det] WHERE xxqad_prh_uid IN\n" + "(\n" + "SELECT\n" + " MAX(xxqad_prh_uid)\n" + " FROM [xxqad_prh_det] where xxqad_prh_seq='{0}' and xxqad_prh_qty_rcvd>=0\n" + " GROUP BY\n" + " [xxqad_prh_receiver]\n" + " ,[xxqad_prh_po_nbr]\n" + " ,[xxqad_prh_po_line]\n" + " ,[xxqad_prh_part]\n" + " ,[xxqad_prh_domain]\n" + ")"; var qadRcvDetailList = idb.Database.SqlQuery(string.Format(_rec, seq)).ToList(); var scpRcvList = new List(); var scpRcveDetailList = new List(); //根据xxwms_rc_po_nbr采购单字段去重 var qadReceiveNbrList = qadRcvDetailList.Select(p => p.xxqad_prh_receiver.ToUpper()).DistinctBy(p => p).ToList(); int j = 0; foreach (var qadReceiveNbr in qadReceiveNbrList) { //qadRcvDetailList同一xxqad_prh_receiver所有记录 var qadDataList = qadRcvDetailList.Where(p => p.xxqad_prh_receiver == qadReceiveNbr).ToList(); if (qadDataList.Count == 0) { continue; } var firstqadData = qadDataList[0]; var scpRcv = wdb.TB_RECEIVE_QAD.SingleOrDefault(p => p.ErpRecvBillNum != null && p.ErpRecvBillNum == qadReceiveNbr && p.Site == firstqadData.xxqad_prh_domain); //判断0100工厂的收货自动审核是否为允许 if (scpRcv == null) { scpRcv = new TB_RECEIVE_QAD { ErpRecvBillNum = qadReceiveNbr,//收货单号 State = (int)FormState.开放, Remark = "", CreateTime = firstqadData.xxqad_prh_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_prh_createdt,//创建日期 CreateUser = firstqadData.xxqad_prh_createur,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BillType = billtype, Site = firstqadData.xxqad_prh_domain, SubSite = firstqadData.xxqad_prh_site, }; } scpRcv.PoBillNum = firstqadData.xxqad_prh_po_nbr;//采购单 //发货单号 // scpRcv.Site = firstqadData.xxqad_prh_domain;//地点 //scpRcv.SubSite = firstqadData.xxqad_prh_site; scpRcv.VendId = firstqadData.xxqad_prh_vend;//供货商名称 scpRcv.ShipTime = firstqadData.xxqad_prh_date_ship;//收货日期 scpRcv.RecvBillNum = firstqadData.xxqad_prh_psnbr; if (scpRcvList.Count(p => p.ErpRecvBillNum == scpRcv.ErpRecvBillNum && p.Site == scpRcv.Site ) == 0) { scpRcvList.Add(scpRcv); } int i = 0; scpRcv.Tax = firstqadData.xxqad_prh_taxc; foreach (var qadData in qadDataList) { try { var poLineNum = Convert.ToInt32(qadData.xxqad_prh_po_line); var scpRcvDetail = wdb.TB_RECEIVE_DETAIL_QAD.FirstOrDefault( p => p.ErpRecvBillNum == qadReceiveNbr && p.PoBillNum == qadData.xxqad_prh_po_nbr && p.PoLine == poLineNum && p.PartCode == qadData.xxqad_prh_part && p.Site == qadData.xxqad_prh_domain && p.Qty >= 0 ); if (scpRcvDetail == null) { scpRcvDetail = new TB_RECEIVE_DETAIL_QAD { ErpRecvBillNum = qadReceiveNbr, RecvBillNum = qadData.xxqad_prh_psnbr,//收货单号 PoBillNum = qadData.xxqad_prh_po_nbr,//采购单 PoLine = poLineNum,//订单行 PartCode = qadData.xxqad_prh_part,//零件号 Batch = qadData.xxqad_prh_vend_batch,//批次 VendBatch = qadData.xxqad_prh_vend_batch,//供应商批号 PoUnit = qadData.xxqad_prh_po_um,//采购单位 LocUnit = qadData.xxqad_prh_loc_um,//基本单位 Qty = qadData.xxqad_prh_qty_rcvd,//收货总量 CurAmt = qadData.xxqad_prh_curr_amt != null ? qadData.xxqad_prh_curr_amt.Value : 0, DockCode = qadData.xxqad_prh_loc,//收货口 State = (int)FormState.开放,// (int)FormState.关闭, CreateTime = qadData.xxqad_prh_createdt == null ? DateTime.Now : (DateTime)qadData.xxqad_prh_createdt,//创建日期 CreateUser = qadData.xxqad_prh_createur,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BillType = billtype, PurCost = qadData.xxqad_prh_pur_cost, StdCost = qadData.xxqad_prh_std_cost != null ? qadData.xxqad_prh_std_cost.Value : 0, Rate = qadData.xxqad_prh_curr_rate != null ? qadData.xxqad_prh_curr_rate.Value : 1, Site = qadData.xxqad_prh_domain, Tax = firstqadData.xxqad_prh_taxc, SubSite = qadData.xxqad_prh_site, }; } scpRcvDetail.RecvBillNum = !string.IsNullOrEmpty(qadData.xxqad_prh_psnbr) ? qadData.xxqad_prh_psnbr : string.Empty; scpRcvDetail.CurAmt = qadData.xxqad_prh_curr_amt != null ? qadData.xxqad_prh_curr_amt.Value : 0; scpRcvDetail.PurCost = qadData.xxqad_prh_pur_cost; scpRcvDetail.StdCost = qadData.xxqad_prh_std_cost != null ? qadData.xxqad_prh_std_cost.Value : 0; scpRcvDetail.Rate = qadData.xxqad_prh_curr_rate != null ? qadData.xxqad_prh_curr_rate.Value : 1; //scpRcvDetail.Site = qadData.xxqad_prh_domain; //scpRcvDetail.SubSite = qadData.xxqad_prh_site; scpRcvDetail.Tax = firstqadData.xxqad_prh_taxc; if (scpRcveDetailList.Count(p => p.RecvBillNum == scpRcvDetail.RecvBillNum && p.ErpRecvBillNum == scpRcvDetail.ErpRecvBillNum && p.PoBillNum == scpRcvDetail.PoBillNum && p.PoLine == scpRcvDetail.PoLine && p.PartCode == scpRcvDetail.PartCode && p.Site == scpRcvDetail.Site ) == 0) { scpRcveDetailList.Add(scpRcvDetail); } i++; qadData.xxqad_prh_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_prh_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_prh_rmks += " SCM:" + ex.Message + i.ToString() + "," + j.ToString(); } qadData.xxqad_prh_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_prh_updatedt = DateTime.Now; } } wdb.TB_RECEIVE_QAD.AddOrUpdate(scpRcvList.ToArray()); wdb.TB_RECEIVE_DETAIL_QAD.AddOrUpdate(scpRcveDetailList.ToArray()); Console.WriteLine($"更新 收货单 数据:{qadRcvDetailList.Count}"); } private static void AddReject(UniApiEntities idb, ScpEntities wdb, string seq, int billtype) { string _rec = "SELECT * FROM [xxqad_prh_det] WHERE xxqad_prh_uid IN\n" + "(\n" + "SELECT\n" + " MAX(xxqad_prh_uid)\n" + " FROM [xxqad_prh_det] where xxqad_prh_seq='{0}' and xxqad_prh_qty_rcvd<0 \n" + " GROUP BY\n" + " [xxqad_prh_receiver]\n" + " ,[xxqad_prh_po_nbr]\n" + " ,[xxqad_prh_po_line]\n" + " ,[xxqad_prh_part]\n" + " ,[xxqad_prh_domain]\n" + ")"; var qadRcvDetailList = idb.Database.SqlQuery(string.Format(_rec, seq)).ToList(); var scpRcvList = new List(); var scpRcveDetailList = new List(); //根据xxwms_rc_po_nbr采购单字段去重 var qadReceiveNbrList = qadRcvDetailList.Select(p => p.xxqad_prh_receiver.ToUpper()).DistinctBy(p => p).ToList(); foreach (var qadReceiveNbr in qadReceiveNbrList) { //qadRcvDetailList同一xxqad_prh_receiver所有记录 var qadDataList = qadRcvDetailList.Where(p => p.xxqad_prh_receiver == qadReceiveNbr).ToList(); if (qadDataList.Count == 0) { continue; } var firstqadData = qadDataList[0]; var scpRcv = wdb.TB_RECEIVE_QAD.FirstOrDefault(p => p.ErpRecvBillNum != null && p.ErpRecvBillNum == qadReceiveNbr && p.Site == firstqadData.xxqad_prh_domain); //判断0100工厂的收货自动审核是否为允许 if (scpRcv == null) { scpRcv = new TB_RECEIVE_QAD { ErpRecvBillNum = qadReceiveNbr,//收货单号 State = (int)FormState.开放, Remark = "", CreateTime = firstqadData.xxqad_prh_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_prh_createdt,//创建日期 CreateUser = firstqadData.xxqad_prh_createur,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BillType = billtype, Site = firstqadData.xxqad_prh_domain, SubSite = firstqadData.xxqad_prh_site, }; } scpRcv.PoBillNum = firstqadData.xxqad_prh_po_nbr;//采购单 //发货单号 //scpRcv.Site = firstqadData.xxqad_prh_domain;//地点 //scpRcv.SubSite = firstqadData.xxqad_prh_site;//地点 scpRcv.VendId = firstqadData.xxqad_prh_vend;//供货商名称 scpRcv.ShipTime = firstqadData.xxqad_prh_date_ship;//收货日期 scpRcv.RecvBillNum = firstqadData.xxqad_prh_psnbr; scpRcvList.Add(scpRcv); scpRcv.Tax = firstqadData.xxqad_prh_taxc; foreach (var qadData in qadDataList) { try { var poLineNum = Convert.ToInt32(qadData.xxqad_prh_po_line); var scpRcvDetail = wdb.TB_RECEIVE_DETAIL_QAD.SingleOrDefault( p => p.ErpRecvBillNum == qadReceiveNbr && p.PoBillNum == qadData.xxqad_prh_po_nbr && p.PoLine == poLineNum && p.PartCode == qadData.xxqad_prh_part && p.Site == qadData.xxqad_prh_domain ); if (scpRcvDetail == null) { scpRcvDetail = new TB_RECEIVE_DETAIL_QAD { ErpRecvBillNum = qadReceiveNbr, RecvBillNum = qadData.xxqad_prh_psnbr,//收货单号 PoBillNum = qadData.xxqad_prh_po_nbr,//采购单 PoLine = poLineNum,//订单行 PartCode = qadData.xxqad_prh_part,//零件号 Batch = qadData.xxqad_prh_vend_batch,//批次 VendBatch = qadData.xxqad_prh_vend_batch,//供应商批号 PoUnit = qadData.xxqad_prh_po_um,//采购单位 LocUnit = qadData.xxqad_prh_loc_um,//基本单位 Qty = qadData.xxqad_prh_qty_rcvd,//收货总量 CurAmt = qadData.xxqad_prh_curr_amt != null ? qadData.xxqad_prh_curr_amt.Value : 0, DockCode = qadData.xxqad_prh_loc,//收货口 State = (int)FormState.开放,// (int)FormState.关闭, CreateTime = qadData.xxqad_prh_createdt == null ? DateTime.Now : (DateTime)qadData.xxqad_prh_createdt,//创建日期 CreateUser = qadData.xxqad_prh_createur,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BillType = billtype, PurCost = qadData.xxqad_prh_pur_cost, StdCost = qadData.xxqad_prh_std_cost != null ? qadData.xxqad_prh_std_cost.Value : 0, Rate = qadData.xxqad_prh_curr_rate != null ? qadData.xxqad_prh_curr_rate.Value : 1, Site = qadData.xxqad_prh_domain, SubSite = qadData.xxqad_prh_site, Tax = firstqadData.xxqad_prh_taxc }; } scpRcvDetail.RecvBillNum = !string.IsNullOrEmpty(qadData.xxqad_prh_psnbr) ? qadData.xxqad_prh_psnbr : string.Empty; scpRcvDetail.PurCost = qadData.xxqad_prh_pur_cost; scpRcvDetail.StdCost = qadData.xxqad_prh_std_cost != null ? qadData.xxqad_prh_std_cost.Value : 0; scpRcvDetail.Rate = qadData.xxqad_prh_curr_rate != null ? qadData.xxqad_prh_curr_rate.Value : 1; scpRcvDetail.CurAmt = qadData.xxqad_prh_curr_amt != null ? qadData.xxqad_prh_curr_amt.Value : 0; //scpRcvDetail.Site = qadData.xxqad_prh_domain; //scpRcvDetail.SubSite = qadData.xxqad_prh_domain; scpRcveDetailList.Add(scpRcvDetail); qadData.xxqad_prh_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_prh_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_prh_rmks += " SCM:" + ex.Message; } qadData.xxqad_prh_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_prh_updatedt = DateTime.Now; } } wdb.TB_RECEIVE_QAD.AddOrUpdate(scpRcvList.ToArray()); wdb.TB_RECEIVE_DETAIL_QAD.AddOrUpdate(scpRcveDetailList.ToArray()); Console.WriteLine($"更新 退货单 数据:{qadRcvDetailList.Count}"); } /// /// 更新采购订单 /// /// /// /// private static void UpdatePPod(UniApiEntities idb, ScpEntities wdb, string seq) { string _sql = "select * from\n" + "(SELECT [xxqad_ppod_uid]\n" + " ,[xxqad_ppod_seq]\n" + " ,[xxqad_ppod_nbr]\n" + " ,[xxqad_ppod_line]\n" + " ,case\n" + " LEN(replace([xxqad_ppod_rev],'-',''))\n" + " when 8 then replace([xxqad_ppod_rev],'-','')+'000'\n" + " when 11 then replace([xxqad_ppod_rev],'-','') end as xxqad_ppod_rev\n" + " ,[xxqad_ppod_site]\n" + " ,[xxqad_ppod_domain]\n" + " ,[xxqad_ppod_part]\n" + " ,[xxqad_ppod_desc]\n" + " ,[xxqad_ppod_type]\n" + " ,[xxqad_ppod_wolot]\n" + " ,[xxqad_ppod_pricum]\n" + " ,[xxqad_ppod_reccum]\n" + " ,[xxqad_ppod_qty_ord]\n" + " ,[xxqad_ppod_qty_std]\n" + " ,[xxqad_ppod_due_date]\n" + " ,[xxqad_ppod_loc_um]\n" + " ,[xxqad_ppod_um]\n" + " ,[xxqad_ppod_um_conv]\n" + " ,[xxqad_ppod_vend]\n" + " ,[xxqad_ppod_buyer]\n" + " ,[xxqad_ppod_buyer_phone]\n" + " ,[xxqad_ppod_status]\n" + " ,[xxqad_ppod_qadread]\n" + " ,[xxqad_ppod_wmsread]\n" + " ,[xxqad_ppod_scmread]\n" + " ,[xxqad_ppod_mesread]\n" + " ,[xxqad_ppod_rmks]\n" + " ,[xxqad_ppod_createdt]\n" + " ,[xxqad_ppod_createur]\n" + " ,[xxqad_ppod_updatedt]\n" + " ,[xxqad_ppod_prj]\n" + //其他地区注释此项 " ,[xxqad_ppod_plan]\n" + //其他地区注释此项 " ,[xxqad_ppod_updateur] from xxqad_ppod_det\n" + " where xxqad_ppod_seq = '{0}' and xxqad_ppod_scmread = '0'\n" + " ) as a\n" + "inner join\n" + "(\n" + "SELECT\n" + " xxqad_ppod_seq\n" + " ,[xxqad_ppod_nbr]\n" + " ,[xxqad_ppod_line]\n" + " ,[xxqad_ppod_domain]\n" + " ,[xxqad_ppod_part]\n" + " ,[xxqad_ppod_due_date]\n" + " ,[xxqad_ppod_vend]\n" + " ,max(\n" + " case\n" + " LEN(replace([xxqad_ppod_rev],'-',''))\n" + " when 8 then replace([xxqad_ppod_rev],'-','')+'000'\n" + " when 11 then replace([xxqad_ppod_rev],'-','') end\n" + " ) as xxqad_ppod_rev\n" + " FROM [xxqad_ppod_det]\n" + " where xxqad_ppod_seq = '{0}' and xxqad_ppod_scmread = '0'\n" + " group by\n" + " xxqad_ppod_seq\n" + " ,[xxqad_ppod_nbr]\n" + " ,[xxqad_ppod_line]\n" + " ,[xxqad_ppod_domain]\n" + " ,[xxqad_ppod_part]\n" + " ,[xxqad_ppod_due_date]\n" + " ,[xxqad_ppod_vend]) as b\n" + " on\n" + " a.xxqad_ppod_seq=b.xxqad_ppod_seq\n" + " and a.xxqad_ppod_domain=b.xxqad_ppod_domain\n" + " and a.xxqad_ppod_due_date=b.xxqad_ppod_due_date\n" + " and a.xxqad_ppod_line=b.xxqad_ppod_line\n" + " and a.xxqad_ppod_nbr=b.xxqad_ppod_nbr\n" + " and a.xxqad_ppod_vend=b.xxqad_ppod_vend\n" + " and a.xxqad_ppod_part=b.xxqad_ppod_part\n" + " and a.xxqad_ppod_rev=b.xxqad_ppod_rev"; _sql = string.Format(_sql, seq); var _ppodList = idb.Database.SqlQuery(_sql).ToList(); List _list = new List(); try { foreach (var itm in _ppodList) { var _plan = wdb.TB_PO_PLAN.Where(p => p.PoBillNum == itm.xxqad_ppod_nbr && p.PartCode == itm.xxqad_ppod_part && p.EndTime == itm.xxqad_ppod_due_date && p.Domain == itm.xxqad_ppod_domain && p.Site == itm.xxqad_ppod_site && p.Version == itm.xxqad_ppod_rev).FirstOrDefault(); if (_plan != null) { _plan.PlanQty = itm.xxqad_ppod_qty_ord.Value; continue; } _plan = new TB_PO_PLAN(); _plan.PoBillNum = itm.xxqad_ppod_nbr; _plan.VendId = itm.xxqad_ppod_vend; _plan.IsDeleted = false;//操作员 _plan.CreateTime = itm.xxqad_ppod_createdt == null ? DateTime.Now : itm.xxqad_ppod_createdt.Value; _plan.CreateUser = itm.xxqad_ppod_createur; _plan.Domain = itm.xxqad_ppod_domain; _plan.Site = itm.xxqad_ppod_site; _plan.PlanType = (int)PoPlanType.New; _plan.ErpBillNum = itm.xxqad_ppod_nbr; _plan.BeginTime = itm.xxqad_ppod_createdt == null ? DateTime.Now : itm.xxqad_ppod_createdt.Value; _plan.EndTime = itm.xxqad_ppod_due_date; _plan.Buyer = itm.xxqad_ppod_buyer; _plan.BuyerPhone = itm.xxqad_ppod_buyer_phone; _plan.PoLine = itm.xxqad_ppod_line; _plan.PoUnit = itm.xxqad_ppod_um; _plan.CreateTime = itm.xxqad_ppod_createdt == null ? DateTime.Now : itm.xxqad_ppod_createdt.Value; _plan.CreateUser = itm.xxqad_ppod_createur; _plan.PartCode = itm.xxqad_ppod_part; _plan.EndTime = itm.xxqad_ppod_due_date; _plan.PlanQty = itm.xxqad_ppod_qty_ord.Value; _plan.IsDeleted = false; _plan.LocUnit = itm.xxqad_ppod_um; _plan.UnConv = itm.xxqad_ppod_um_conv; _plan.State = (int)AskState.New; _plan.GUID = Guid.NewGuid(); _plan.Version = itm.xxqad_ppod_rev; _plan.Extend1 = itm.xxqad_ppod_prj; _plan.Extend2 = itm.xxqad_ppod_plan; _list.Add(_plan); } List _ls = new List(); foreach (var itm in _list) { var _count = _ls.Count(p => p.PoBillNum == itm.PoBillNum && p.PartCode == itm.PartCode && p.EndTime == itm.EndTime && p.Domain == p.Domain && p.Site == itm.Site && p.Version == itm.Version); if (_count == 0) { _ls.Add(itm); } } wdb.TB_PO_PLAN.AddOrUpdate(_ls.ToArray()); } catch (Exception e) { throw e; } Console.WriteLine($"更新看板数据:{_ppodList.Count}"); } /// /// 更新采购订单 /// /// /// /// private static void UpdatePurchaseOrders(UniApiEntities idb, ScpEntities wdb, xxerp_MaxbatchNO item) { try { var max = idb.xxDiMaxes_Po.OrderByDescending(r => r.Di_Batchno).FirstOrDefault(); int a = max.Di_Batchno; var FATCTORY = wdb.TB_FACTORY.FirstOrDefault(); DataTable b = PoBillNumcontroller.GetInvBatchStock(a, FATCTORY.FactoryId); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var wmsPoList = new List(); var wmsPoDetailList = new List(); var wmsAskList = new List(); var wmsAskDetailList = new List(); var xxMaxbath = new List(); var Dlist = list.Where(p => p.ctype == "D"); var Ilist = list.Where(p => p.ctype != "D"); var _li = Dlist.GroupBy(p => p.purDocNO); foreach (var _lii in _li) { var _liii = _lii.ToList(); var _entity = _liii.First(); // 根据 零件号作废 var askdetaill = wdb.TB_ASK_DETAIL.Where(p => p.PoBillNum == _entity.purDocNO && p.PartCode == _entity.mtlNO).ToList(); askdetaill.ForEach(p => { p.State = -1; p.IsDeleted = true; wmsAskDetailList.Add(p); }); } foreach (var qadPoNbr in Ilist) { var qadDataList = wmsPoList.Count(p => p.PoBillNum == qadPoNbr.purDocNO); var wmsPo1 = wdb.TB_PO.Count(p => p.PoBillNum == qadPoNbr.purDocNO); if (qadDataList == 0 && wmsPo1 == 0) { TB_PO wmsPo = new TB_PO { PoBillNum = qadPoNbr.purDocNO,//订单号 ErpBillNum = qadPoNbr.purDocNO, VendId = qadPoNbr.vendorNO,//供应商编号 ModType = 2, CreateUser = "W21-LIUST", Site = "W21",//地点 State = (int)FormState.开放,//状态 CreateTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 IsDeleted = false, GUID = System.Guid.NewGuid(), BeginTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 EndTime = qadPoNbr.itemDeliveryDate,//收货结束时间 Buyer = qadPoNbr.CREATEDBY, BuyerPhone = qadPoNbr.phone, }; wmsPoList.Add(wmsPo); } var qadAskList = wmsAskList.Count(p => p.PoBillNum == qadPoNbr.purDocNO); var wmsAs1 = wdb.TB_ASK.Count(p => p.PoBillNum == qadPoNbr.purDocNO); if (qadAskList == 0 && wmsAs1 == 0) { int cnt = 1; String str = "k"; TB_ASK wmsAs = new TB_ASK { AskBillNum = str + qadPoNbr.purDocNO.Substring(2, 12), PoBillNum = qadPoNbr.purDocNO,//订单号 VendId = qadPoNbr.vendorNO,//供应商编号 Site = "W21", CreateTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 CreateUser = "W21-LIUST",//创建用户 GUID = System.Guid.NewGuid(), ModType = 2, BeginTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 EndTime = qadPoNbr.itemDeliveryDate,//收货结束时间 Buyer = qadPoNbr.CREATEDBY, BuyerPhone = qadPoNbr.phone, ErpBillNum = qadPoNbr.purDocNO,//订单号 SubSite = "W21", }; if (qadPoNbr.returnsItem == "Y") { wmsAs.State = -1; } else { wmsAs.State = 1; } wmsAskList.Add(wmsAs); } var qadASKList = wmsAskDetailList.Count(p => p.PoBillNum == qadPoNbr.purDocNO && p.PartCode == qadPoNbr.mtlNO); var wmsAskDetail1 = wdb.TB_ASK_DETAIL.Count(p => p.PoBillNum == qadPoNbr.purDocNO && p.PartCode == qadPoNbr.mtlNO); // var tapart = wdb.TA_PART.Where(q => q.PartCode == qadPoNbr.mtlNO.Trim()).ToList(); if (qadASKList == 0 && wmsAskDetail1 == 0) { String str = "k"; TB_ASK_DETAIL wmsAskDetail = new TB_ASK_DETAIL { AskBillNum = str + qadPoNbr.purDocNO.Substring(2, 12), PoBillNum = qadPoNbr.purDocNO,//订单号 PoLine = int.Parse(qadPoNbr.purDocItemNO), Site = "W21", CreateTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 CreateUser = "W21-LIUST",//创建用户 GUID = System.Guid.NewGuid(), PartCode = qadPoNbr.mtlNO.ToUpper(),//零件号-物料号 BeginTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 EndTime = qadPoNbr.itemDeliveryDate,//收货结束时间 Price = 0, AskQty = qadPoNbr.quantity, DockCode = tapart.Count()>0 ? tapart.FirstOrDefault().PartGroup:"YC", PoUnit = qadPoNbr.baseUnitMeasure, Currency = qadPoNbr.sizecoL, }; if (qadPoNbr.returnsItem == "Y") { wmsAskDetail.State = -1; } else { wmsAskDetail.State = 1; } wmsAskDetailList.Add(wmsAskDetail); } var maxlist1 = new xxDiMaxes_Po { Di_Batchno = qadPoNbr.DI_BATCHNO, }; xxMaxbath.Add(maxlist1); var qadDataList1 = wmsPoDetailList.Count(p => p.PoBillNum == qadPoNbr.purDocNO && p.PartCode == qadPoNbr.mtlNO); var wmsPoDetail1 = wdb.TB_PO_DETAIL.Count(p => p.PoBillNum == qadPoNbr.purDocNO && p.PartCode == qadPoNbr.mtlNO); if (qadDataList1 == 0 && wmsPoDetail1 == 0) { TB_PO_DETAIL wmsPoDetail = new TB_PO_DETAIL { PoBillNum = qadPoNbr.purDocNO,//订单号 PoLine = int.Parse(qadPoNbr.purDocItemNO), PartCode = qadPoNbr.mtlNO.ToUpper(),//零件号-物料号 PlanQty = qadPoNbr.quantity,//订货数量-采购量 BeginTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//收货开始时间 EndTime = qadPoNbr.itemDeliveryDate,//收货结束时间 PoUnit = qadPoNbr.baseUnitMeasure,//采购单位 Price = 0, Site = "W21",//备注 SubSite = "W21", CreateTime = qadPoNbr.createdDate == null ? DateTime.Now : (DateTime)qadPoNbr.createdDate,//创建日期 CreateUser = qadPoNbr.CREATEDBY,//创建用户 //if (wmsPoDetail.BillQty != 0 && wmsPoDetail.BillQty <= wmsPoDetail.ReceivedQty) // wmsPoDetail.State = (int)FormState.关闭; State = (int)FormState.开放, IsDeleted = false, GUID = System.Guid.NewGuid(), }; wmsPoDetailList.Add(wmsPoDetail); // } } //wmsPo.State = qadDataList.All(p => p.xxqad_pod_status == ((int)FormState.关闭).ToString()) // ? (int)FormState.关闭 // : (int)FormState.开放; } if (wmsPoList.Count == 0) { Console.WriteLine("更新 ERP采购订单 数据:" + "无数据"); return; } item.Remark = ((int)UniApiState.成功).ToString(); //item.MaxdibatchNO = item.MaxdibatchNO + list.Count; idb.xxDiMaxes_Po.AddOrUpdate(xxMaxbath.ToArray()); wdb.TB_PO.AddOrUpdate(wmsPoList.ToArray()); wdb.TB_ASK.AddOrUpdate(wmsAskList.ToArray()); wdb.TB_PO_DETAIL.AddOrUpdate(wmsPoDetailList.ToArray()); wdb.TB_ASK_DETAIL.AddOrUpdate(wmsAskDetailList.ToArray()); Console.WriteLine($"更新 采购订单 数据:{list.Count}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private static void UpdatePurchaseOrders(UniApiEntities idb, ScpEntities wdb, string seq, string p_flag) { var _ls = wdb.TB_FACTORY.Select(p => p.FactoryId).ToList(); var wmsPoList = new List(); var wmsPoDetailList = new List(); var _askList = new List(); var _askDetailList = new List(); var qadPoDetailList = idb.xxqad_pod_det.Where(p => p.xxqad_pod_seq == seq && p.xxqad_pod_scmread == "0").ToList(); List _lst = new List(); foreach (var itm in qadPoDetailList) { var _cnt = _lst.Where(p => p.xxqad_pod_nbr == itm.xxqad_pod_nbr && p.xxqad_pod_domain == itm.xxqad_pod_domain).Count(); if (_cnt == 0) { _lst.Add(itm); } } foreach (var po in _lst) { var firstqadData = po; var qadDataList = qadPoDetailList.Where(p => p.xxqad_pod_nbr == po.xxqad_pod_nbr && p.xxqad_pod_domain == po.xxqad_pod_domain).ToList(); var wmsPo = wdb.TB_PO.SingleOrDefault(p => p.PoBillNum == po.xxqad_pod_nbr && p.Site == po.xxqad_pod_domain); if (wmsPo == null) { int temp; wmsPo = new TB_PO { PoBillNum = po.xxqad_pod_nbr,//订单号 ErpBillNum = po.xxqad_pod_prj,//ERP订单号 VendId = firstqadData.xxqad_pod_vend.ToUpper(),//供应商编号 ModType = Int32.TryParse(firstqadData.xxqad_pod_modtype, out temp) ? temp : (int?)null, //单据类型(1:日程单,2:离散订单) Contacter = firstqadData.xxqad_pod_attn,//联系人 Site = firstqadData.xxqad_pod_domain,//地点 SubSite = firstqadData.xxqad_pod_site, State = (int)FormState.开放,//状态 CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt,//创建日期 CreateUser = firstqadData.xxqad_pod_createur,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BeginTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt,//创建日期 EndTime = firstqadData.xxqad_pod_due_date//收货结束时间 }; wmsPoList.Add(wmsPo); } wmsPo.Contacter = firstqadData.xxqad_pod_attn; wmsPo.Buyer = firstqadData.xxqad_pod_buyer; wmsPo.BuyerPhone = firstqadData.xxqad_pod_buyer_phone; wmsPo.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 wmsPo.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 wmsPo.State = qadDataList.All(p => p.xxqad_pod_status == ((int)FormState.关闭).ToString()) ? (int)FormState.关闭 : (int)FormState.开放; foreach (var qadData in qadDataList) { try { var lineNum = Convert.ToInt32(qadData.xxqad_pod_line); var wmsPoDetail = wdb.TB_PO_DETAIL.SingleOrDefault( p => p.PoBillNum == qadData.xxqad_pod_nbr && p.PoLine == lineNum && p.Site == qadData.xxqad_pod_domain); if (wmsPoDetail == null) { wmsPoDetail = new TB_PO_DETAIL { PoBillNum = qadData.xxqad_pod_nbr,//订单号 PoLine = lineNum,//订单行 IsDeleted = false, GUID = System.Guid.NewGuid() }; wmsPoDetailList.Add(wmsPoDetail); } wmsPoDetail.PartCode = qadData.xxqad_pod_part.ToUpper();//零件号-物料号 wmsPoDetail.PlanQty = qadData.xxqad_pod_qty_ord;//订货数量-采购量 wmsPoDetail.BeginTime = firstqadData.xxqad_pod_createdt;//收货开始时间 wmsPoDetail.EndTime = qadData.xxqad_pod_due_date;//收货结束时间 wmsPoDetail.PoUnit = qadData.xxqad_pod_um;//采购单位 wmsPoDetail.LocUnit = qadData.xxqad_pod_loc_um.ToUpper();//存储单位 wmsPoDetail.DockCode = qadData.xxqad_pod_loc; wmsPoDetail.Price = ScpCache.Config.项目名称 == ProjectName.重庆北汽模塑.ToString() ? 0 : qadData.xxqad_pod_price; wmsPoDetail.Currency = qadData.xxqad_pod_curr;//币种 wmsPoDetail.PackQty = qadData.xxqad_pod_qty_std;//标包数量 wmsPoDetail.UnConv = qadData.xxqad_pod_um_conv;//转换率 wmsPoDetail.DockCode = qadData.xxqad_pod_loc; wmsPoDetail.State = Convert.ToInt32(qadData.xxqad_pod_status);//状态 wmsPoDetail.Site = qadData.xxqad_pod_domain;//备注 wmsPoDetail.SubSite = qadData.xxqad_pod_site; wmsPoDetail.CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt;//创建日期 wmsPoDetail.CreateUser = firstqadData.xxqad_pod_buyer;//创建用户 wmsPoDetail.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 wmsPoDetail.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 if (wmsPoDetail.PlanQty != 0 && wmsPoDetail.PlanQty <= wmsPoDetail.ReceivedQty) wmsPoDetail.State = (int)FormState.关闭; qadData.xxqad_pod_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_pod_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_pod_rmks += " SCM:" + ex.Message; } qadData.xxqad_pod_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_pod_updatedt = DateTime.Now; } if (firstqadData.xxqad_pod_modtype == "2") { if (firstqadData.xxqad_pod_domain == "CNH") { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var _first = db.TA_CONFIG.Where(p => p.ParamName == "自动生成看板" && p.ParamValue == "允许").FirstOrDefault(); if (_first != null) { try { var _task = wdb.TB_ASK.FirstOrDefault(p => p.ErpBillNum == po.xxqad_pod_nbr && p.Site == po.xxqad_pod_domain); if (_task == null) { _task = new TB_ASK() { AskBillNum = SCP_BILLCODE_CONTROLLER.MakeASKCode(), PoBillNum = po.xxqad_pod_nbr,//订单号 ErpBillNum = po.xxqad_pod_prj,//ERP订单号 VendId = firstqadData.xxqad_pod_vend.ToUpper(),//供应商编号 Buyer = firstqadData.xxqad_pod_attn,//联系人 Site = firstqadData.xxqad_pod_domain,//地点 SubSite = firstqadData.xxqad_pod_site, State = (int)AskState.New,//状态 CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt,//创建日期 CreateUser = firstqadData.xxqad_pod_buyer,//创建用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BeginTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt,//创建日期 EndTime = firstqadData.xxqad_pod_due_date,//收货结束时间 ModType = (int)BillModType.Non_Contract, }; _askList.Add(_task); } if (_task.State == 0) { _task.Buyer = firstqadData.xxqad_pod_buyer; _task.BuyerPhone = firstqadData.xxqad_pod_buyer_phone; _task.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 _task.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 _task.State = (int)AskState.New; } foreach (var qadData in qadDataList) { try { var lineNum = Convert.ToInt32(qadData.xxqad_pod_line); var _taskDetail = wdb.TB_ASK_DETAIL.SingleOrDefault( p => p.PoBillNum == qadData.xxqad_pod_nbr && p.PoLine == lineNum && p.Remark == qadData.xxqad_pod_domain); if (_taskDetail == null) { _taskDetail = new TB_ASK_DETAIL { AskBillNum = _task.AskBillNum, PoBillNum = qadData.xxqad_pod_nbr,//订单号 PoLine = lineNum,//订单行 IsDeleted = false, GUID = System.Guid.NewGuid() }; _askDetailList.Add(_taskDetail); } if (_task.State == 0) { _taskDetail.PartCode = qadData.xxqad_pod_part.ToUpper();//零件号-物料号 _taskDetail.AskQty = qadData.xxqad_pod_qty_ord;//订货数量-采购量 _taskDetail.BeginTime = firstqadData.xxqad_pod_createdt;//收货开始时间 _taskDetail.EndTime = qadData.xxqad_pod_due_date;//收货结束时间 _taskDetail.PoUnit = qadData.xxqad_pod_um;//采购单位 _taskDetail.LocUnit = qadData.xxqad_pod_loc_um.ToUpper();//存储单位 _taskDetail.DockCode = qadData.xxqad_pod_loc; _taskDetail.Price = qadData.xxqad_pod_price; _taskDetail.Currency = qadData.xxqad_pod_curr;//币种 _taskDetail.PackQty = qadData.xxqad_pod_qty_std;//标包数量 _taskDetail.UnConv = qadData.xxqad_pod_um_conv;//转换率 _taskDetail.DockCode = qadData.xxqad_pod_loc; _taskDetail.State = Convert.ToInt32(qadData.xxqad_pod_status);//状态 _taskDetail.SubSite = qadData.xxqad_pod_site;//备注 _taskDetail.Site = qadData.xxqad_pod_domain; _taskDetail.CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt;//创建日期 _taskDetail.CreateUser = firstqadData.xxqad_pod_buyer;//创建用户 _taskDetail.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 _taskDetail.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 _taskDetail.State = _task.State; } } catch (Exception ex) { qadData.xxqad_pod_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_pod_rmks += " SCM:" + ex.Message; } qadData.xxqad_pod_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_pod_updatedt = DateTime.Now; } db.TB_ASK.AddOrUpdate(_askList.ToArray()); db.TB_ASK_DETAIL.AddOrUpdate(_askDetailList.ToArray()); if (db.SaveChanges() != 1) { } } catch (Exception e) { Console.WriteLine($"更新错误:{e.Message}"); } } } } } } wdb.TB_PO.AddOrUpdate(wmsPoList.ToArray()); wdb.TB_PO_DETAIL.AddOrUpdate(wmsPoDetailList.ToArray()); Console.WriteLine($"更新 采购订单 数据:{qadPoDetailList.Count}"); } /// /// 更新净价 /// /// /// /// private static void UpdatePrice1(UniApiEntities idb, ScpEntities scpdb, xxerp_MaxbatchNO item) { var max = idb.xxDiMaxes.OrderByDescending(r => r.Di_Batchno_NETprice).FirstOrDefault(); int a = max.Di_Batchno_NETprice; var FATCTORY = scpdb.TB_FACTORY.FirstOrDefault(); DataTable b = NetPriceController.GetInvBatchNetPrice(a, FATCTORY.FactoryId); //DataTable b = NetPriceController.GetInvBatchNetPrice(item.MaxdibatchNO); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var PriceList = new List(); var PriceListUpdate = new List(); var xxMaxbath = new List(); foreach (var itm in list) { var maxlist1 = new xxDiMaxes { Di_Batchno_part = 0, Di_Batchno = 0, Di_Batchno_price = 0, Di_Batchno_NETprice = itm.DI_BATCHNO, }; xxMaxbath.Add(maxlist1); var _part = scpdb.TB_NetPrice.Count(p => p.PartCode == itm.MTLNO.ToUpper() && p.VendId == itm.vendorNO && p.StartTime == itm.validityStartPeriod && p.EndTime == itm.validityEndPeriod); if (_part == 0) { var _price = new TB_NetPrice { VendId = itm.vendorNO, Remarks = itm.purOrg, PartCode = itm.MTLNO, Site = itm.plant, StartTime = itm.validityStartPeriod, EndTime = itm.validityEndPeriod, }; _price.Amt = itm.netPrice; _price.Extend3 = itm.DI_OPERTYPE; PriceList.Add(_price); } //存在更新 else { var enty = scpdb.TB_NetPrice.First(p => p.PartCode == itm.MTLNO.ToUpper() && p.VendId == itm.vendorNO && p.StartTime == itm.validityStartPeriod && p.EndTime == itm.validityEndPeriod); enty.Amt = itm.netPrice; enty.Remarks = itm.purOrg; enty.Extend3 = itm.DI_OPERTYPE; PriceListUpdate.Add(enty); } } if (PriceList.Count == 0) { Console.WriteLine("更新 净价 数据:" + "无数据"); return; } idb.xxDiMaxes.AddOrUpdate(xxMaxbath.ToArray()); item.MaxdibatchNO = item.MaxdibatchNO + list.Count; item.Remark = ((int)UniApiState.失败).ToString(); scpdb.TB_NetPrice.AddOrUpdate(PriceList.ToArray()); scpdb.TB_NetPrice.AddOrUpdate(u =>u.UID ,PriceListUpdate.ToArray()); Console.WriteLine($"更新采购净价数据:{PriceList.Count}"); } /// /// 更新价格 /// /// /// /// private static void UpdatePrice(UniApiEntities idb, ScpEntities scpdb, xxerp_MaxbatchNO item) { var max = idb.xxDiMaxes.OrderByDescending(r => r.Di_Batchno_price).FirstOrDefault(); int a = max.Di_Batchno_price; var FATCTORY = scpdb.TB_FACTORY.FirstOrDefault(); DataTable b = PriceController.GetInvBatchPrice(a, FATCTORY.FactoryId); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var xxMaxbath = new List(); var PriceList = new List(); List _PRICEs = new List(); foreach (var itm in list) { var maxlist1 = new xxDiMaxes { Di_Batchno_part = 0, Di_Batchno = 0, Di_Batchno_price = itm.DI_BATCHNO, Di_Batchno_NETprice = 0, }; xxMaxbath.Add(maxlist1); var _part = scpdb.TB_PRICE.FirstOrDefault(p => p.PartCode == itm.MTLNO.ToUpper() && p.VendId == itm.vendorNO && p.StartTime == itm.validityStartPeriod && p.EndTime == itm.validityEndPeriod); // 不存在添加 if (_part == null) { var _price = new TB_PRICE { VendId = itm.vendorNO, Unit = itm.ordUnit, PartCode = itm.MTLNO, Site = itm.plant, StartTime = itm.validityStartPeriod, EndTime = itm.validityEndPeriod, }; _price.Amt = decimal.Parse(itm.purchasePrice); _price.Extend3 = itm.DI_OPERTYPE; PriceList.Add(_price); } // 存在判断类型进行更新 else { _part.Amt = decimal.Parse(itm.purchasePrice); _part.Extend3 = itm.DI_OPERTYPE; _PRICEs.Add(_part); } } if (PriceList.Count == 0) { Console.WriteLine("更新 采购价格 数据:" + "无数据"); return; } idb.xxDiMaxes.AddOrUpdate(xxMaxbath.ToArray()); item.MaxdibatchNO = item.MaxdibatchNO + list.Count; //item.Remark = ((int)UniApiState.失败).ToString(); scpdb.TB_PRICE.AddOrUpdate(PriceList.ToArray()); scpdb.TB_PRICE.AddOrUpdate(qa=> qa.UID,_PRICEs.ToArray()); Console.WriteLine($"更新采购价格数据:{PriceList.Count + _PRICEs.Count }"); } /// /// 更新供应商零件 /// /// /// /// private static void UpdateVendParts(UniApiEntities idb, ScpEntities wdb, xxerp_MaxbatchNO item) { DataTable b = vendidpartcontroller.GetInvBatchStock(item.MaxdibatchNO.ToString()); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var wmsDataList = new List(); foreach (var qadData in list) { try { if (string.IsNullOrEmpty(qadData.MTLNO)) qadData.MTLNO = qadData.MTLNO; var wmsData = wdb.TA_VEND_PART.SingleOrDefault( p => p.VendId == qadData.VENDORNO.ToUpper() && p.PartCode == qadData.MTLNO.ToUpper() && p.Site == qadData.PLANT ) ?? new TA_VEND_PART { VendId = qadData.VENDORNO.ToUpper(),//供应商编号 PartCode = qadData.MTLNO.ToUpper(),//对应物料号 //VendPartCode = qadData.xxqad_vp_vend_part.ToUpper(),//供应商物料号 //VendPackQty = qadData.xxqad_vp_comment,//供应商物料号 //PoUnit = qadData.,//采购单位 State = 1,//状态(0:失效 1:有效) Remark = "", CreateTime = qadData.created == null ? DateTime.Now : DateTime.Parse(qadData.created),//创建日期 CreateUser = qadData.createdBy,//创建用户 UpdateTime = DateTime.Now,//更新日期 //UpdateUser = qadData.xxqad_vp_updateur,//更新用户 //TransportationTime = qadData.memo, IsDeleted = false, GUID = new Guid() }; wmsData.Site = string.IsNullOrEmpty(wmsData.Site) ? qadData.PLANT : wmsData.Site; // wmsData.VendPartCode = qadData.xxqad_vp_vend_part?.ToUpper() ?? qadData.ToUpper.ToUpper(); //wmsData.VendPackQty = qadData.xxqad_vp_comment; //wmsData.TransportationTime = qadData.xxqad_vp_vend_lead; wmsDataList.Add(wmsData); item.Remark = ((int)UniApiState.成功).ToString(); item.MaxdibatchNO = Convert.ToInt32(qadData.DI_BATCHNO); } catch (Exception ex) { item.Remark = ((int)UniApiState.失败).ToString(); //qadData.xxqad_vp_rmks += " SCM:" + ex.Message; } //qadData.xxqad_vp_updateur = ScpCache.Config.SCP用户名; //qadData.xxqad_vp_updatedt = DateTime.Now; } wdb.TA_VEND_PART.AddOrUpdate(wmsDataList.ToArray()); Console.WriteLine($"更新 供应商零件 数据:{wmsDataList.Count}"); } /// /// 更新供应商 /// /// /// /// private static void UpdateVender(UniApiEntities idb, ScpEntities wdb, AppBoxContext authdb, xxerp_MaxbatchNO item) { var max = idb.xxDiMaxes.OrderByDescending(r => r.Di_Batchno).FirstOrDefault(); int a = max.Di_Batchno; var FATCTORY = wdb.TB_FACTORY.FirstOrDefault(); DataTable b = Vendercontroller.GetInvBatchStock(item.MaxdibatchNO, a, FATCTORY.FactoryId); //DataTable b = Vendercontroller.GetInvBatchStock(item.MaxdibatchNO); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var wmsDataList = new List(); var xxMaxbath = new List(); foreach (var qadData in list) { try { var maxlist1 = new xxDiMaxes { Di_Batchno_NETprice = 0, Di_Batchno_price = 0, Di_Batchno_part = 0, Di_Batchno = qadData.DI_BATCHNO, }; xxMaxbath.Add(maxlist1); var wmsData = wdb.TA_VENDER.FirstOrDefault(p => p.VendId == qadData.VENDORNO.ToUpper() && p.Site == ScpCache.Config.QAD域) ?? new TA_VENDER { VendId = qadData.VENDORNO.ToUpper(), VendAbbCode = "0", State = 1 }; wmsData.VendName = string.IsNullOrEmpty(qadData.NAME1) ? qadData.VENDORNO : qadData.NAME1; wmsData.VendType = qadData.providerType.ToUpper().ToString(); wmsData.Country = qadData.country; wmsData.Phone = qadData.firstTelNO; wmsData.Fax = qadData.FAXNUMBER; wmsData.Site = ScpCache.Config.QAD域; wmsDataList.Add(wmsData); item.Remark = ((int)UniApiState.成功).ToString(); // item.MaxdibatchNO = Convert.ToInt32(qadData.DI_BATCHNO); var vendname = string.Format("{0}@{1}", qadData.VENDORNO.ToUpper(), ScpCache.Config.QAD域); var _first = authdb.Users.Where(p => p.Name == vendname).FirstOrDefault(); var _vend = authdb.Roles.Where(p => p.Name == "供应商").FirstOrDefault(); var _factory = authdb.TA_FACTORY.Where(p => p.ErpDomain == ScpCache.Config.QAD域).FirstOrDefault(); //_first == null && _vend != null && if (_factory != null && _first == null) { User _entity = new User(); _entity.Name = vendname; _entity.ChineseName = string.IsNullOrEmpty(qadData.NAME1) ? qadData.VENDORNO : qadData.NAME1; _entity.Remark = qadData.COMMUADDRESS.ToUpper(); _entity.Password = PasswordUtil.CreateDbPassword("555555"); _entity.Email = wmsData.VendId + "@163.com"; _entity.Gender = "男"; _entity.Enabled = true; _entity.Roles = new List(); authdb.Users.Add(_entity); Role t = authdb.Set().Local.Where(x => x.ID == _vend.ID).FirstOrDefault(); if (t == null) { t = new Role { ID = _vend.ID }; authdb.Set().Attach(t); } _entity.Roles.Add(t); _entity.TA_FACTORY = new List(); TA_FACTORY f = authdb.Set().Local.Where(x => x.ID == _factory.ID).FirstOrDefault(); if (f == null) { f = new TA_FACTORY { ID = _factory.ID }; authdb.Set().Attach(f); } _entity.TA_FACTORY.Add(f); item.Remark = ((int)UniApiState.成功).ToString(); //} } } catch (Exception ex) { item.Remark = ((int)UniApiState.失败).ToString(); } //qadData.xxqad_vd_updateur = ScpCache.Config.SCP用户名; //qadData.xxqad_vd_updatedt = DateTime.Now; } if (wmsDataList.Count == 0) { Console.WriteLine("更新 供应商 数据:" + "无数据"); return; } wdb.TA_VENDER.AddOrUpdate(p => new { p.VendId, p.Site }, wmsDataList.ToArray()); idb.xxDiMaxes.AddOrUpdate(xxMaxbath.ToArray()); item.MaxdibatchNO = item.MaxdibatchNO + list.Count; Console.WriteLine($"更新 供应商 数据:{list.Count}"); } public static DataTable GetInvBatchStock() { string Sql = $"Select * form MMRSendMtlData"; var dt_Part = DBControllerByOracle.GetDataTable(Sql); return dt_Part; } private static void UpdateVenderExtend(UniApiEntities idb, ScpEntities wdb, AppBoxContext authdb, List qadDataList) { var _ls = new List(); foreach (var qadData in qadDataList) { try { var vendname = string.Format("{0}@{1}", qadData.VendId.ToUpper(), ScpCache.Config.QAD域); var _first = authdb.Users.Where(p => p.Name == vendname).FirstOrDefault(); if (_first != null) { var _ef = authdb.VenderUsers.FirstOrDefault(p => p.VenderId == qadData.VendId && p.UserId == _first.ID) ?? new VenderUsers() { UserId = _first.ID, VenderId = qadData.VendId.ToUpper() }; _ls.Add(_ef); } } catch { } } authdb.VenderUsers.AddOrUpdate(_ls.ToArray()); } /// /// 更新零件 /// /// /// /// private static void UpdateParts(UniApiEntities idb, ScpEntities wdb, xxerp_MaxbatchNO item) { var max = idb.xxDiMaxes.OrderByDescending(r => r.Di_Batchno_part).FirstOrDefault(); int a = max.Di_Batchno_part; var FATCTORY = wdb.TB_FACTORY.FirstOrDefault(); DataTable b = QmStockController.GetInvBatchStock(item.MaxdibatchNO, a, FATCTORY.FactoryId); List list = ChangKeTec.Utils.ListHelper.DataTableToList(b); var wmsDataList = new List(); var xxMaxbath = new List(); foreach (var qadData in list) { var maxlist1 = new xxDiMaxes { Di_Batchno_NETprice = 0, Di_Batchno_price = 0, Di_Batchno_part = qadData.DI_BATCHNO, Di_Batchno = 0 }; xxMaxbath.Add(maxlist1); var wmsData = wdb.TA_PART.SingleOrDefault(p => p.ErpPartCode == qadData.MTLNO.ToString() && p.Site == qadData.plant) ?? new TA_PART { PartCode = qadData.MTLNO.ToString(), ErpPartCode = qadData.MTLNO.ToString(), }; wmsData.PartDesc1 = qadData.MTLNODES.ToString(); wmsData.PartDesc2 = qadData.sizes.ToString(); wmsData.ReceivePort = qadData.grossWeight.ToString(); wmsData.PalletSize = qadData.netWeight.ToString(); wmsData.PartGroup = qadData.MTLTYPE.ToString(); wmsData.ProjectId = qadData.MATBRAND; wmsData.Unit = !string.IsNullOrEmpty(qadData.MEASUREBASEUNIT) ? qadData.MEASUREBASEUNIT.ToUpper() : "EA"; wmsData.Remark = qadData.stdPrice.ToString(); wmsData.ValidityDays = qadData.loadQuantity; wmsData.State = !string.IsNullOrEmpty(qadData.SpMtlStatus) ? qadData.SpMtlStatus.ToUpper() : "A"; wmsData.Site = qadData.plant; wmsDataList.Add(wmsData); } if (wmsDataList.Count == 0) { Console.WriteLine("更新 零件 数据:" + "无数据"); return; } item.Remark = ((int)UniApiState.成功).ToString(); item.MaxdibatchNO = item.MaxdibatchNO + list.Count; idb.xxDiMaxes.AddOrUpdate(xxMaxbath.ToArray()); wdb.TA_PART.AddOrUpdate(p => new { p.PartCode, p.Site }, wmsDataList.ToArray()); Console.WriteLine($"更新 零件信息 数据:{list.Count}"); } } }