锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

722 lines
38 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSSoft.Frame.Data.Entity;
  6. using System.Data;
  7. using ICSSoft.Base.Config.DBHelper;
  8. using ICSSoft.Base.Config.AppConfig;
  9. using ICSSoft.Frame.Data.Entity.NcApiEntity;
  10. using Newtonsoft.Json;
  11. using System.Net;
  12. namespace ICSSoft.Frame.Data.DAL
  13. {
  14. public class ICSDeliveryPlanDAL
  15. {
  16. private static string APIURL = System.Configuration.ConfigurationSettings.AppSettings["APIURL"].ToString();
  17. private static string urlCreatDeliveryPlan = APIURL + "sendSaleDispatch";//生成销售发货单
  18. private static string erp = AppConfig.GetDataBaseConnectStringByKey("[DB.ERP]");
  19. #region 新增和修改
  20. public static void Add(ICSDeliveryPlan Info, DataTable dt, string Version, string dsconn)
  21. {
  22. FramDataContext db = new FramDataContext(dsconn);
  23. db.Connection.Open();
  24. db.Transaction = db.Connection.BeginTransaction();
  25. try
  26. {
  27. var has = db.ICSDeliveryPlan.Where(a => a.DeliveryPlanNO == Info.DeliveryPlanNO && a.Version == Info.Version && a.ID != Info.ID);
  28. if (has != null && has.Count() > 0)
  29. throw new Exception("单据号:" + Info.DeliveryPlanNO + " 已存在!");
  30. var line = db.ICSDeliveryPlan.SingleOrDefault(a => a.ID == Info.ID);
  31. if (line == null)
  32. db.ICSDeliveryPlan.InsertOnSubmit(Info);
  33. else if (string.IsNullOrWhiteSpace(Version) && line.Checker != null)
  34. {
  35. throw new Exception("单据号:" + Info.DeliveryPlanNO + " 已审核,不能修改!");
  36. }
  37. else if (!string.IsNullOrWhiteSpace(Version))
  38. {
  39. if (line.Checker == null)
  40. throw new Exception("单据号:" + Info.DeliveryPlanNO + " 未审核,不能变更!");
  41. else
  42. {
  43. ICSDeliveryPlan lineNew = new ICSDeliveryPlan();
  44. Info.ID = AppConfig.GetGuid();
  45. lineNew.ID = Info.ID;
  46. lineNew.DeliveryPlanNO = Info.DeliveryPlanNO;
  47. lineNew.Version = GetVersion(line.Version);// (Convert.ToInt32(line.Version) + 10).ToString();
  48. lineNew.Customer = Info.Customer;
  49. lineNew.Org = Info.Org;
  50. lineNew.Enable = true;
  51. lineNew.WorkPoint = AppConfig.WorkPointCode;
  52. lineNew.EATTRIBUTE1 = null;
  53. lineNew.MUSER = AppConfig.UserCode;
  54. lineNew.MUSERName = AppConfig.UserName;
  55. lineNew.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  56. lineNew.CreateUser = lineNew.MUSER;
  57. lineNew.CreateDateTime = lineNew.MTIME;
  58. db.ICSDeliveryPlan.InsertOnSubmit(lineNew);
  59. line.Enable = false;
  60. }
  61. }
  62. else
  63. {
  64. line.MUSER = Info.MUSER;
  65. line.MUSERName = Info.MUSERName;
  66. line.MTIME = Info.MTIME;
  67. }
  68. db.SubmitChanges();
  69. var delete = db.ICSDeliveryPlanDetail.Where(a => a.DeliveryPlanID == Info.ID);
  70. db.ICSDeliveryPlanDetail.DeleteAllOnSubmit(delete);
  71. db.SubmitChanges();
  72. int count = 0;
  73. string invcode = "";
  74. foreach (DataRow dr in dt.Rows)
  75. {
  76. if (string.IsNullOrWhiteSpace(dr["InvCode"].ToString()))
  77. {
  78. dr["InvCode"] = invcode;
  79. if (count == 0)
  80. throw new Exception("首行物料编码不能为空!");
  81. }
  82. //if (string.IsNullOrWhiteSpace(dr["InvCode"].ToString()))
  83. // throw new Exception("物料编码不能为空!");
  84. //if (string.IsNullOrWhiteSpace(dr["SOCode"].ToString()))
  85. // throw new Exception("销售订单号不能为空!");
  86. //foreach (DataColumn dc in dt.Columns)
  87. //{
  88. bool isNew = false;
  89. ICSDeliveryPlanDetail detail = null;// db.ICSDeliveryPlanDetail.SingleOrDefault(a => a.DeliveryPlanID == Info.ID && a.InvCode == dr["InvCode"].ToString() && a.SOCode == dr["SOCode"].ToString());
  90. if (detail == null)
  91. {
  92. isNew = true;
  93. detail = new ICSDeliveryPlanDetail();
  94. detail.ID = AppConfig.GetGuid();
  95. detail.DeliveryPlanID = Info.ID;
  96. detail.InvCode = dr["InvCode"].ToString();
  97. detail.SOCode = dr["SOCode"].ToString();
  98. detail.WorkPoint = Info.WorkPoint;
  99. }
  100. detail.Sequence = ++count;// string.IsNullOrWhiteSpace(dr["Sequence"].ToString()) ? 0 : Convert.ToInt32(dr["Sequence"]);
  101. detail.Quantity = string.IsNullOrWhiteSpace(dr["Quantity"].ToString()) ? 0 : Convert.ToDecimal(dr["Quantity"]);
  102. detail.PreDate = Convert.ToDateTime(dr["PreDate"]);
  103. detail.MUSER = Info.MUSER;
  104. detail.MUSERName = Info.MUSERName;
  105. detail.MTIME = Info.MTIME;
  106. if (isNew)
  107. db.ICSDeliveryPlanDetail.InsertOnSubmit(detail);
  108. //}
  109. db.SubmitChanges();
  110. invcode = dr["InvCode"].ToString();
  111. }
  112. #region 校验是否超销售订单行创建数据
  113. //var parent = dt.DefaultView.ToTable(true, "SOCode","vbillcode","crowno","SOQTY");
  114. //foreach (DataRow drP in parent.Rows)
  115. //{
  116. // string socode = drP["SOCode"].ToString();
  117. // var detail = db.ICSDeliveryPlanDetail.Where(a => a.SOCode == socode).Select(a => a.Quantity).Sum();
  118. // if (detail != null && detail > Convert.ToDecimal(drP["SOQTY"]))
  119. // {
  120. // throw new Exception("销售订单:" + drP["vbillcode"].ToString() +"_"+ drP["crowno"].ToString() + " 已超出订单数量!");
  121. // }
  122. //}
  123. #endregion
  124. db.Transaction.Commit();
  125. }
  126. catch (Exception ex)
  127. {
  128. db.Transaction.Rollback();
  129. throw ex;
  130. }
  131. }
  132. #endregion
  133. #region 导入
  134. public static void AddList(DataTable dt, string dsconn)
  135. {
  136. FramDataContext db = new FramDataContext(dsconn);
  137. db.Connection.Open();
  138. db.Transaction = db.Connection.BeginTransaction();
  139. try
  140. {
  141. DateTime mtime = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  142. string date = "LH" + mtime.ToString("yyyyMMdd").Substring(2);
  143. string version = mtime.ToString("yyyyMM");
  144. var parent = dt.DefaultView.ToTable(true, "客户编码");
  145. foreach (DataRow drP in parent.Rows)
  146. {
  147. bool isNewP = false;
  148. string customer = drP["客户编码"].ToString();
  149. string DeliveryPlanNO = date + "001";
  150. var dp = db.ICSDeliveryPlan.Where(a => a.DeliveryPlanNO.Substring(0, date.Length) == date).OrderByDescending(a => a.DeliveryPlanNO).FirstOrDefault();
  151. if (dp != null)
  152. {
  153. DeliveryPlanNO = date + (Convert.ToInt32(dp.DeliveryPlanNO.Substring(date.Length)) + 1).ToString().PadLeft(3, '0');
  154. }
  155. var line = db.ICSDeliveryPlan.SingleOrDefault(a => a.DeliveryPlanNO == DeliveryPlanNO && a.Enable == true);
  156. ICSDeliveryPlan lineNew = new ICSDeliveryPlan();
  157. if (line == null)
  158. {
  159. isNewP = true;
  160. lineNew.Version = version + "A";
  161. lineNew.ID = AppConfig.GetGuid();
  162. lineNew.DeliveryPlanNO = DeliveryPlanNO;
  163. lineNew.Enable = true;
  164. lineNew.WorkPoint = AppConfig.WorkPointCode;
  165. lineNew.EATTRIBUTE1 = null;
  166. lineNew.CreateUser = AppConfig.UserCode;
  167. lineNew.CreateDateTime = mtime;
  168. lineNew.Customer = customer;
  169. }
  170. else if (line.Checker != null)
  171. {
  172. isNewP = true;
  173. lineNew.Version = GetVersion(line.Version);// (Convert.ToInt32(line.Version) + 10).ToString();
  174. lineNew.ID = AppConfig.GetGuid();
  175. lineNew.DeliveryPlanNO = DeliveryPlanNO;
  176. lineNew.Enable = true;
  177. lineNew.WorkPoint = AppConfig.WorkPointCode;
  178. lineNew.EATTRIBUTE1 = null;
  179. lineNew.CreateUser = AppConfig.UserCode;
  180. lineNew.CreateDateTime = mtime;
  181. line.Enable = false;
  182. }
  183. else
  184. {
  185. lineNew = line;
  186. }
  187. lineNew.MUSER = AppConfig.UserCode;
  188. lineNew.MUSERName = AppConfig.UserName;
  189. lineNew.MTIME = mtime;
  190. if (isNewP)
  191. db.ICSDeliveryPlan.InsertOnSubmit(lineNew);
  192. else
  193. {
  194. var delete = db.ICSDeliveryPlanDetail.Where(a => a.DeliveryPlanID == lineNew.ID);
  195. db.ICSDeliveryPlanDetail.DeleteAllOnSubmit(delete);
  196. db.SubmitChanges();
  197. }
  198. db.SubmitChanges();
  199. DataRow[] drs = dt.Select("客户编码='" + customer + "'");
  200. //DataRow[] drs = dt.Select("单据号='" + DeliveryPlanNO + "'");
  201. int count = 0;
  202. foreach (DataRow dr in drs)
  203. {
  204. lineNew.Org = dr["客户名称"].ToString();
  205. //if (string.IsNullOrWhiteSpace(dr["客户编码"].ToString()))
  206. // throw new Exception("单据号:" + DeliveryPlanNO + " 客户编码不能为空!");
  207. if (string.IsNullOrWhiteSpace(dr["物料编码"].ToString()))
  208. throw new Exception("客户编码:" + customer + " 物料编码不能为空!");
  209. //foreach (DataColumn dc in dt.Columns)
  210. //{
  211. //if (dc.Caption == "单据号" || dc.Caption == "客户编码" || dc.Caption == "物料编码")
  212. // continue;
  213. bool isNew = false;
  214. var detail = db.ICSDeliveryPlanDetail.SingleOrDefault(a => a.DeliveryPlanID == lineNew.ID && a.InvCode == dr["物料编码"].ToString() && a.SOCode == dr["客户订单号"].ToString());
  215. if (detail == null)
  216. {
  217. isNew = true;
  218. detail = new ICSDeliveryPlanDetail();
  219. detail.ID = AppConfig.GetGuid();
  220. detail.DeliveryPlanID = lineNew.ID;//ICSINVENTORY.INVMODELGROUP
  221. detail.InvCode = dr["物料编码"].ToString();
  222. detail.SOCode = dr["客户订单号"].ToString();
  223. detail.WorkPoint = lineNew.WorkPoint;
  224. }
  225. detail.Sequence = ++count;
  226. //int s = 0;
  227. //if (int.TryParse(string.IsNullOrWhiteSpace(dr["行号"].ToString()) ? "0" : dr["行号"].ToString(), out s))
  228. //{
  229. // detail.Sequence = s;
  230. //}
  231. //else
  232. //{
  233. // throw new Exception("单据号:" + DeliveryPlanNO + " 行号格式不正确!");
  234. //}
  235. decimal t = 0m;
  236. if (decimal.TryParse(string.IsNullOrWhiteSpace(dr["数量"].ToString()) ? "0" : dr["数量"].ToString(), out t))
  237. {
  238. detail.Quantity = t;
  239. }
  240. else
  241. {
  242. throw new Exception("单据号:" + DeliveryPlanNO + " 数量格式不正确!");
  243. }
  244. detail.PreDate = Convert.ToDateTime(dr["要求交期"]);
  245. detail.MUSER = lineNew.MUSER;
  246. detail.MUSERName = lineNew.MUSERName;
  247. detail.MTIME = lineNew.MTIME;
  248. if (isNew)
  249. db.ICSDeliveryPlanDetail.InsertOnSubmit(detail);
  250. }
  251. db.SubmitChanges();
  252. }
  253. //}
  254. db.Transaction.Commit();
  255. }
  256. catch (Exception ex)
  257. {
  258. db.Transaction.Rollback();
  259. throw ex;
  260. }
  261. }
  262. #endregion
  263. private static string GetVersion(string version)
  264. {
  265. string ver = version.Substring(6);
  266. List<string> str = new List<string> { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
  267. int index = str.IndexOf(ver) + 1;
  268. if (index >= str.Count())
  269. throw new Exception("超出变更次数!");
  270. return version.Substring(0, 6) + str[index];
  271. }
  272. #region 通过ID查询
  273. public static DataSet SearchInfoByID(string ID, string dsconn)
  274. {
  275. string sql = "";
  276. if (string.IsNullOrWhiteSpace(ID))
  277. {
  278. sql = @"DECLARE @MaxNO INT,@date varchar(20)='LH'+SUBSTRING(CONVERT(varchar(8), GETDATE(), 112), 3, 6)
  279. SELECT @MaxNO=SUBSTRING(MAX(DeliveryPlanNO),LEN(@date)+1,LEN(MAX(DeliveryPlanNO))-LEN(@date))+1 FROM ICSDeliveryPlan WHERE SUBSTRING(DeliveryPlanNO, 1, LEN(@date))=@date
  280. IF @MaxNO IS NULL
  281. BEGIN
  282. SELECT @date+'001' AS DeliveryPlanNO,CONVERT(VARCHAR(6),GETDATE(),112)+'A' AS Version
  283. END
  284. ELSE
  285. BEGIN
  286. SELECT @date+REPLICATE('0',3-LEN(@MaxNO))+CAST(@MaxNO AS nvarchar(10)) AS DeliveryPlanNO,CONVERT(VARCHAR(6),GETDATE(),112)+'A' AS Version
  287. END ";
  288. }
  289. else
  290. {
  291. sql = @"SELECT ID,DeliveryPlanNO,Version,Org,Enable,Customer,CreateUser,CreateDateTime,Checker,CheckDateTime,Reviewer,ReviewDateTime,MUSER,MUSERName,MTIME,WorkPoint
  292. FROM ICSDeliveryPlan WHERE ID='{0}' AND WorkPoint='{1}' ";
  293. }
  294. sql += @"declare @sql varchar(8000)
  295. SELECT @sql='['+DBIpAddress+'].'+DBName FROM dbo.Sys_DataBase WHERE WorkCode = '{1}' AND DBSourceName = 'ERP'
  296. set @sql='SELECT DeliveryPlanID,Sequence,SOCode,PreDate,InvCode,Quantity,inv.name,inv.materialspec,inv.materialmnecode,d.vcooppohcode,d.vbillcode,c.crowno,c.nastnum AS SOQTY
  297. FROM ICSDeliveryPlanDetail b
  298. LEFT JOIN '+@sql+'.dbo.so_saleorder_b c ON b.SOCode=c.csaleorderbid
  299. LEFT JOIN '+@sql+'.dbo.so_saleorder d ON c.csaleorderid=d.csaleorderid
  300. LEFT JOIN '+@sql+'.dbo.bd_material inv ON b.InvCode=inv.code
  301. WHERE DeliveryPlanID=''{0}'' AND WorkPoint=''{1}'' '
  302. exec(@sql)";
  303. sql = string.Format(sql, ID, AppConfig.WorkPointCode);
  304. DataSet ds = DBHelper.ExecuteDataset(dsconn, CommandType.Text, sql);
  305. return ds;
  306. }
  307. #endregion
  308. #region 删除
  309. public static void deleteInfo(List<string> IDList, string dsconn)
  310. {
  311. FramDataContext db = new FramDataContext(dsconn);
  312. db.Connection.Open();
  313. db.Transaction = db.Connection.BeginTransaction();
  314. try
  315. {
  316. var lines = db.ICSDeliveryPlan.Where(a => IDList.Contains(a.ID));
  317. foreach (ICSDeliveryPlan line in lines)
  318. {
  319. line.MUSER = AppConfig.UserCode;
  320. line.MUSERName = AppConfig.UserName;
  321. line.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  322. line.Enable = false;
  323. line.EATTRIBUTE1 = "delete";
  324. db.SubmitChanges();
  325. }
  326. db.Transaction.Commit();
  327. }
  328. catch (Exception ex)
  329. {
  330. db.Transaction.Rollback();
  331. throw ex;
  332. }
  333. }
  334. #endregion
  335. #region 审核
  336. public static void Check(List<string> IDList, string dsconn)
  337. {
  338. FramDataContext db = new FramDataContext(dsconn);
  339. db.Connection.Open();
  340. db.Transaction = db.Connection.BeginTransaction();
  341. try
  342. {
  343. var lines = db.ICSDeliveryPlan.Where(a => IDList.Contains(a.ID));
  344. foreach (ICSDeliveryPlan line in lines)
  345. {
  346. if (line.Checker != null)
  347. {
  348. throw new Exception("单据号:" + line.DeliveryPlanNO + " 已审核,不能再次审核!");
  349. }
  350. line.MUSER = AppConfig.UserCode;
  351. line.MUSERName = AppConfig.UserName;
  352. line.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  353. line.Checker = AppConfig.UserCode;
  354. line.CheckDateTime = line.MTIME;
  355. db.SubmitChanges();
  356. }
  357. db.Transaction.Commit();
  358. }
  359. catch (Exception ex)
  360. {
  361. db.Transaction.Rollback();
  362. throw ex;
  363. }
  364. }
  365. #endregion
  366. #region 弃审
  367. public static void RefuseCheck(List<string> IDList, string dsconn)
  368. {
  369. FramDataContext db = new FramDataContext(dsconn);
  370. db.Connection.Open();
  371. db.Transaction = db.Connection.BeginTransaction();
  372. try
  373. {
  374. var lines = db.ICSDeliveryPlan.Where(a => IDList.Contains(a.ID));
  375. foreach (ICSDeliveryPlan line in lines)
  376. {
  377. if (line.Checker == null)
  378. {
  379. throw new Exception("单据号:" + line.DeliveryPlanNO + " 不是审核状态,不能弃审!");
  380. }
  381. else if (line.Reviewer != null)
  382. {
  383. throw new Exception("单据号:" + line.DeliveryPlanNO + " 已复审,不能弃审!");
  384. }
  385. line.MUSER = AppConfig.UserCode;
  386. line.MUSERName = AppConfig.UserName;
  387. line.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  388. line.Checker = null;
  389. line.CheckDateTime = null;
  390. line.EATTRIBUTE1 = "check";
  391. db.SubmitChanges();
  392. }
  393. db.Transaction.Commit();
  394. }
  395. catch (Exception ex)
  396. {
  397. db.Transaction.Rollback();
  398. throw ex;
  399. }
  400. }
  401. #endregion
  402. #region 复审
  403. public static void Review(List<string> IDList, string dsconn)
  404. {
  405. FramDataContext db = new FramDataContext(dsconn);
  406. db.Connection.Open();
  407. db.Transaction = db.Connection.BeginTransaction();
  408. try
  409. {
  410. var lines = db.ICSDeliveryPlan.Where(a => IDList.Contains(a.ID));
  411. foreach (ICSDeliveryPlan line in lines)
  412. {
  413. if (line.Checker == null)
  414. {
  415. throw new Exception("单据号:" + line.DeliveryPlanNO + " 未审核,不能复审!");
  416. }
  417. else if (line.Reviewer != null)
  418. {
  419. throw new Exception("单据号:" + line.DeliveryPlanNO + " 已复审,不能再次复审!");
  420. }
  421. line.MUSER = AppConfig.UserCode;
  422. line.MUSERName = AppConfig.UserName;
  423. line.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  424. line.Reviewer = AppConfig.UserCode;
  425. line.ReviewDateTime = line.MTIME;
  426. db.SubmitChanges();
  427. }
  428. db.Transaction.Commit();
  429. }
  430. catch (Exception ex)
  431. {
  432. db.Transaction.Rollback();
  433. throw ex;
  434. }
  435. }
  436. #endregion
  437. #region 取消复审
  438. public static void ReviewBack(List<string> IDList, string dsconn)
  439. {
  440. FramDataContext db = new FramDataContext(dsconn);
  441. db.Connection.Open();
  442. db.Transaction = db.Connection.BeginTransaction();
  443. try
  444. {
  445. var lines = db.ICSDeliveryPlan.Where(a => IDList.Contains(a.ID));
  446. foreach (ICSDeliveryPlan line in lines)
  447. {
  448. if (line.Reviewer == null)
  449. {
  450. throw new Exception("单据号:" + line.DeliveryPlanNO + " 不是复审状态,不能取消复审!");
  451. }
  452. line.MUSER = AppConfig.UserCode;
  453. line.MUSERName = AppConfig.UserName;
  454. line.MTIME = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss");
  455. line.Reviewer = null;
  456. line.ReviewDateTime = null;
  457. line.EATTRIBUTE1 = "review";
  458. db.SubmitChanges();
  459. }
  460. db.Transaction.Commit();
  461. }
  462. catch (Exception ex)
  463. {
  464. db.Transaction.Rollback();
  465. throw ex;
  466. }
  467. }
  468. #endregion
  469. public static DataSet GetInvInfoAndCust(string pk_org)
  470. {
  471. string sqlDB = "SELECT '['+DBIpAddress+'].'+DBName FROM dbo.Sys_DataBase WHERE WorkCode = '{0}' AND DBSourceName = 'ERP'";
  472. sqlDB = string.Format(sqlDB, AppConfig.WorkPointCode);
  473. var DB = DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sqlDB).Tables[0].Rows[0][0].ToString();
  474. string sql = @"SELECT a.code AS [物料编码] ,a.name AS [物料名称] ,a.materialspec AS [规格型号],a.materialmnecode as [客户料号]
  475. FROM {1}.dbo.bd_material a
  476. -- INNER JOIN org_orgs b ON a.pk_org =b.pk_org
  477. WHERE a.pk_org='{0}' --b.code='Ahwit'
  478. ORDER BY a.code
  479. SELECT b.csaleorderbid AS [],a.vcooppohcode AS [],a.vbillcode AS [],b.crowno AS [],d.code AS [] ,d.name AS [] ,d.materialmnecode as []
  480. ,e.code AS [],ISNULL(b.vbdef5, b.dreceivedate) AS [],b.nastnum-ISNULL(x.Quantity, 0) AS []
  481. FROM {1}.dbo.so_saleorder a
  482. INNER JOIN {1}.dbo.so_saleorder_b b ON a.csaleorderid=b.csaleorderid
  483. INNER JOIN {1}.dbo.so_saleorder_exe c ON b.csaleorderbid=c.csaleorderbid
  484. INNER JOIN {1}.dbo.bd_material d ON b.cmaterialvid = d.pk_material
  485. INNER JOIN {1}.dbo.bd_customer e ON a.ccustomerid = e.pk_customer
  486. INNER JOIN {1}.dbo.bd_billtype f ON a.ctrantypeid = f.pk_billtypeid
  487. LEFT JOIN (SELECT a.SOCode,SUM(a.Quantity) AS Quantity
  488. FROM ICSDeliveryPlanDetail a
  489. INNER JOIN ICSDeliveryPlan b ON a.DeliveryPlanID=b.ID
  490. WHERE Enable=1
  491. GROUP BY a.SOCode) x ON x.SOCode=b.csaleorderbid
  492. WHERE a.dr=0 AND a.fstatusflag=2 AND bbinvoicendflag='N' AND bboutendflag='N' AND bbsendendflag='N'AND b.dr=0 AND c.dr=0 AND f.pk_billtypecode IN('30-Cxx-08','30-Cxx-09')
  493. ORDER BY a.vbillcode,b.crowno
  494. SELECT cus.code AS [],cus.name AS [],cus.mnecode as [],cus.shortname as [] FROM {1}.dbo.bd_customer cus
  495. INNER JOIN {1}.dbo.org_orgs org ON cus.pk_org=org.pk_org
  496. ORDER BY cus.code";//--WHERE org.code='{1}'
  497. sql = string.Format(sql, pk_org, DB);
  498. return DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql);
  499. }
  500. public static DataSet GetPlanInfo(string vourchs)
  501. {
  502. string sql = @"SELECT a.SOCode,SUM(a.Quantity) AS Quantity
  503. FROM ICSDeliveryPlanDetail a
  504. INNER JOIN ICSDeliveryPlan b ON a.DeliveryPlanID=b.ID
  505. WHERE Enable=1
  506. GROUP BY a.SOCode
  507. ";
  508. //sql = string.Format(sql, vourchs);
  509. return DBHelper.ExecuteDataset(AppConfig.AppConnectString, CommandType.Text, sql);
  510. }
  511. public static void UploadErp(string ids, string dsconn)
  512. {
  513. string sql = "";
  514. FramDataContext db = new FramDataContext(dsconn);
  515. db.Connection.Open();
  516. db.Transaction = db.Connection.BeginTransaction();
  517. db.CommandTimeout = 6000;
  518. try
  519. {
  520. sql = "UPDATE ICSDeliveryPlan SET UploadErp=1 WHERE ID IN ({0})";
  521. sql = string.Format(sql, ids);
  522. db.ExecuteCommand(sql);
  523. db.SubmitChanges();
  524. #region 表头
  525. sql = @"select DISTINCT 'Ahwit' AS pk_group,x.code as pk_org,z.busicode as cbiztypeid,CONVERT(varchar(100), GETDATE(), 120) AS dbilldate,CONVERT(varchar(100), GETDATE(), 120) AS creationtime,CONVERT(varchar(100), GETDATE(), 120) AS dmakedate
  526. from {1}.dbo.so_saleorder_b b
  527. join {1}.dbo.so_saleorder a on a.csaleorderid = b.csaleorderid
  528. join {1}.dbo.bd_busitype z on z.pk_busitype = a. cbiztypeid
  529. join {1}.dbo.org_trafficorg x on x.pk_trafficorg = b.ctrafficorgid
  530. WHERE b.csaleorderbid in (SELECT SOCode FROM ICSDeliveryPlanDetail WHERE DeliveryPlanID IN ({0}))
  531. ";
  532. #endregion
  533. //string sqlDB="SELECT '['+DBIpAddress+'].'+DBName FROM dbo.Sys_DataBase WHERE WorkCode = '{0}' AND DBSourceName = 'ERP'";
  534. //sqlDB=string.Format(sqlDB,AppConfig.WorkPointCode);
  535. //var info = db.ExecuteQuery<string>(sqlDB).ToString();
  536. var infoERP = db.Sys_DataBase.SingleOrDefault(a => a.DBSourceName == "ERP" && a.WorkCode == AppConfig.WorkPointCode);
  537. var infoMES = db.Sys_DataBase.SingleOrDefault(a => a.DBSourceName == "SYS" && a.WorkCode == AppConfig.WorkPointCode);
  538. string DB = (infoERP.DBIpAddress == infoMES.DBIpAddress ? "" : "[" + infoERP.DBIpAddress + "].") + infoERP.DBName;
  539. sql = string.Format(sql, ids, DB);
  540. var DeliveryPlan = db.ExecuteQuery<CreateSaleDispatchEntity>(sql).ToList();
  541. var infoApiUser = db.Sys_DataBase.SingleOrDefault(a => a.DBSourceName == "UserNCAPI" && a.WorkCode == AppConfig.WorkPointCode);
  542. if (infoApiUser == null)
  543. throw new Exception("数据源中未维护调用NC接口的用户:UserNCAPI");
  544. string ApiUserCode = infoApiUser.DBUser;
  545. string ApiUserPassword = AppConfig.FromMd5(infoApiUser.DBPwd);
  546. #region 表体
  547. sql = @"select b.csaleorderbid,b.vsrcrowno as crowno ,c.code as cmaterialid, d.name as castunitid, nastnum,b.vchangerate as vchangerate,
  548. e.name as cqtunitid , b.nqtunitnum as nqtunitnum , b.vqtunitrate as vqtunitrate ,f.code as corigcurrencyid , b.nexchangerate as nexchangerate ,
  549. j.code as ccurrencyid , h.code as ctaxcodeid ,b.ntaxrate as ntaxrate ,b.ncaltaxmny as ncaltaxmny , b.ndiscountrate as ndiscountrate,
  550. b.nitemdiscountrate as nitemdiscountrate , b.norigtaxprice ,b.norigprice ,b.norigtaxnetprice,
  551. b.norignetprice , b.nqtorigtaxprice , b.nqtorigprice , b.nqtorigtaxnetprc ,b.nqtorignetprice,
  552. b.nqtorigprice AS norigmny ,b.nqtorigtaxprice AS norigtaxmny ,ISNULL(b.norigdiscount ,0 )as norigdiscount,b.ntaxprice ,b.nprice,
  553. b.ntaxnetprice,b.nnetprice ,b.nqttaxprice ,b.nqtprice,b.nqttaxnetprice,
  554. b.nqtnetprice ,b.ntax ,b.nmny ,b.ntaxmny ,b.ndiscount,
  555. a.vbillcode AS vfirstcode, b.crowno AS vfirstrowno ,b.csaleorderid AS cfirstid , b.csaleorderbid AS cfirstbid ,a.vbillcode AS vsrccode ,
  556. b.crowno AS vsrcrowno , b.csaleorderid AS csrcid ,b.csaleorderbid AS csrcbid ,i.code as csaleorgid,g.code as csaleorgvid,
  557. k.code as csendstockorgid ,l.code as csendstockorgvid ,ISNULL(o.code ,'~' )as csendstordocid ,p.code as creceivecustid , CONVERT(varchar(100),b.dsenddate,120) as dsenddate,
  558. CONVERT(varchar(100),b.dreceivedate,120) as dreceivedate , q.code as carorgvid ,r.code as csettleorgid ,s.code as cdeptid , t.code as cdeptvid,
  559. u.code as crececountryid ,v.code as csendcountryid ,w.code as ctaxcountryid ,CONVERT(varchar(100),b.dbilldate,120) as vfirstbilldate,CONVERT(varchar(100),GETDATE(),120) as dbilldate,x.code as pk_org,
  560. y.code as cordercustid, z.busicode as cbiztypeid,a.cinvoicecustid as cinvoicecustid
  561. INTO #TempSale
  562. from {1}.dbo.so_saleorder_b b
  563. join {1}.dbo.so_saleorder a on a.csaleorderid = b.csaleorderid
  564. join {1}.dbo.bd_busitype z on z.pk_busitype = a. cbiztypeid
  565. join {1}.dbo.bd_material c on b.cmaterialvid = c.pk_material
  566. join {1}.dbo.bd_measdoc d on d.pk_measdoc = b.castunitid
  567. join {1}.dbo.bd_measdoc e on e.pk_measdoc = b.cqtunitid
  568. join {1}.dbo.bd_currtype f on f.pk_currtype = a.corigcurrencyid
  569. join {1}.dbo.bd_currtype j on j.pk_currtype = b.ccurrencyid
  570. join {1}.dbo.bd_taxcode h on h.pk_taxcode = b.ctaxcodeid
  571. join {1}.dbo.org_salesorg i on i.pk_salesorg = a.pk_org
  572. join {1}.dbo.org_salesorg_v g on g.pk_vid = a. pk_org_v
  573. join {1}.dbo.org_stockorg k on k.pk_stockorg = b.csendstockorgid
  574. join {1}.dbo.org_stockorg_v l on l.pk_vid = b.csendstockorgvid
  575. left join {1}.dbo.bd_stordoc o on o.pk_stordoc = b.csendstordocid
  576. join {1}.dbo.bd_customer p on p.pk_customer= b.creceivecustid
  577. join {1}.dbo.org_financeorg_v q on q.pk_vid = b.carorgvid
  578. join {1}.dbo.org_financeorg r on r.pk_financeorg =b.csettleorgid
  579. join {1}.dbo.org_dept s on s.pk_dept = a.cdeptid
  580. join {1}.dbo.org_dept_v t on t.pk_vid = a.cdeptvid
  581. join {1}.dbo.bd_countryzone u on u.pk_country =b.crececountryid
  582. join {1}.dbo.bd_countryzone v on v.pk_country = b.csendcountryid
  583. join {1}.dbo.bd_countryzone w on w.pk_country = b.ctaxcountryid
  584. join {1}.dbo.org_trafficorg x on x.pk_trafficorg = b.ctrafficorgid
  585. join {1}.dbo.bd_customer y on y.pk_customer = a.ccustomerid
  586. WHERE b.csaleorderbid in (SELECT SOCode FROM ICSDeliveryPlanDetail WHERE DeliveryPlanID IN ({0}))
  587. select crowno, cmaterialid, castunitid, pl.Quantity as nastnum, vchangerate,
  588. cqtunitid, nqtunitnum, vqtunitrate, corigcurrencyid, nexchangerate,
  589. ccurrencyid, ctaxcodeid, ntaxrate, ncaltaxmny, ndiscountrate,
  590. nitemdiscountrate, norigtaxprice, norigprice, norigtaxnetprice,
  591. norignetprice, nqtorigtaxprice, nqtorigprice, nqtorigtaxnetprc, nqtorignetprice,
  592. CAST(b.norigmny*pl.Quantity AS DECIMAL(18,2)) AS norigmny,CAST(b.norigtaxmny*pl.Quantity AS DECIMAL(18,2)) AS norigtaxmny, norigdiscount, ntaxprice, nprice,
  593. ntaxnetprice, nnetprice, nqttaxprice, nqtprice, nqttaxnetprice,
  594. nqtnetprice,CAST(b.norigtaxmny*pl.Quantity AS DECIMAL(18,2))-CAST(b.norigmny*pl.Quantity AS DECIMAL(18,2)) AS ntax, nmny, ntaxmny, ndiscount,
  595. vfirstcode, vfirstrowno, cfirstid, cfirstbid, vsrccode,
  596. vsrcrowno, csrcid, csrcbid, csaleorgid, csaleorgvid,
  597. csendstockorgid, csendstockorgvid, csendstordocid, creceivecustid,CONVERT(varchar(10),pl.PreDate,23) as dsenddate,
  598. dreceivedate, carorgvid, csettleorgid, cdeptid, cdeptvid,
  599. crececountryid, csendcountryid, ctaxcountryid, vfirstbilldate, dbilldate,
  600. pk_org, cordercustid, cbiztypeid
  601. from ICSDeliveryPlanDetail pl
  602. join #TempSale b on pl.SOCode = b.csaleorderbid
  603. WHERE pl.DeliveryPlanID IN ({0})
  604. DROP TABLE #TempSale
  605. ";
  606. #endregion
  607. sql = string.Format(sql, ids, DB);
  608. var query = db.ExecuteQuery<CreateSaleDispatchDetailEntity>(sql).ToList();
  609. NcApiInputArguments inputInfo = new NcApiInputArguments();
  610. inputInfo.datasource = "design";
  611. inputInfo.usercode = ApiUserCode;// AppConfig.UserCode;
  612. inputInfo.password = ApiUserPassword;// AppConfig.FromMd5(AppConfig.UserPwd);
  613. inputInfo.list = new List<object>();
  614. //CreateSaleDispatchEntity DeliveryPlan = new CreateSaleDispatchEntity();
  615. //DeliveryPlan.pk_group = "Ahwit";
  616. //string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  617. //DeliveryPlan.dbilldate = time;
  618. //DeliveryPlan.creationtime = time;
  619. //DeliveryPlan.dmakedate = time;
  620. //DeliveryPlan.list = query;
  621. ////待修改
  622. //if (query != null && query.Count > 0)
  623. //{
  624. // DeliveryPlan.cbiztypeid = query[0].cbiztypeid;
  625. // DeliveryPlan.pk_org = query[0].pk_org;
  626. //}
  627. foreach (CreateSaleDispatchEntity item in DeliveryPlan)
  628. {
  629. var detail = query.Where(a => a.pk_org == item.pk_org && a.cbiztypeid == item.cbiztypeid);
  630. var dates = detail.Select(a => a.dsenddate).Distinct();
  631. foreach (string date in dates)
  632. {
  633. CreateSaleDispatchEntity info = item;
  634. var det = detail.Where(a => a.dsenddate == date).ToList();
  635. info.list = det;
  636. inputInfo.list.Add(info);
  637. }
  638. }
  639. string inputJson = JsonConvert.SerializeObject(inputInfo);
  640. string resStrCreatDeliveryPlan = HttpPost(urlCreatDeliveryPlan, inputJson);
  641. NcApiOutArguments res = new NcApiOutArguments();
  642. res = JsonConvert.DeserializeObject<NcApiOutArguments>(resStrCreatDeliveryPlan);
  643. if (res.flat == "0")
  644. {
  645. throw new Exception("生成拉货计划单失败:" + res.list_info);
  646. }
  647. db.Transaction.Commit();
  648. }
  649. catch (Exception ex)
  650. {
  651. db.Transaction.Rollback();
  652. throw ex;
  653. }
  654. finally
  655. {
  656. db.Connection.Close();
  657. }
  658. }
  659. public static string HttpPost(string url, string body)
  660. {
  661. try
  662. {
  663. Encoding encoding = Encoding.UTF8;
  664. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  665. request.Method = "POST";
  666. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  667. request.ContentType = "application/json; charset=utf-8";
  668. byte[] buffer = encoding.GetBytes(body);
  669. request.ContentLength = buffer.Length;
  670. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  671. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  672. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  673. {
  674. return reader.ReadToEnd();
  675. }
  676. }
  677. catch (WebException ex)
  678. {
  679. throw new Exception(ex.Message);
  680. }
  681. }
  682. }
  683. public class TEMP_DeliveryPlanDetail
  684. {
  685. public string InvCode { get; set; }
  686. public string Period { get; set; }
  687. public decimal Quantity { get; set; }
  688. public string cDate { get; set; }
  689. }
  690. }