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.

452 lines
24 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyNet;
  3. using Entity.Sugar;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace EasyBL.WEBAPP.EIP
  12. {
  13. public class BillChangeApply_UpdService : ServiceBase
  14. {
  15. private string ProgramID = nameof(BillChangeApply_UpdService);
  16. private string ProgramName = "帳單更改申請單管理";
  17. #region 帳單更改申請單提交簽核
  18. /// <summary>
  19. /// 帳單更改申請單提交簽核
  20. /// </summary>
  21. /// <param name="i_crm">todo: describe i_crm parameter on BillChangeApplyToAudit</param>
  22. /// <returns></returns>
  23. public ResponseMessage BillChangeApplyToAudit(RequestMessage i_crm)
  24. {
  25. ResponseMessage rm = null;
  26. string sMsg = null;
  27. try
  28. {
  29. rm = SugarBase.ExecTran(db =>
  30. {
  31. do
  32. {
  33. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  34. var sdb = new SimpleClient<OTB_EIP_BillChangeApply>(db);
  35. var oEip = sdb.GetById(sId);
  36. if (oEip == null)
  37. {
  38. sMsg = @"系統找不到對應的帳單更改申請單資料,請核查!";
  39. break;
  40. }
  41. var oAskTheDummy = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == oEip.Applicant);
  42. var sTitle = oAskTheDummy.MemberName + @"的帳單更改申請單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  43. if (i_crm.LANG == @"zh")
  44. {
  45. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  46. }
  47. var pm = new Dictionary<string, object>();
  48. var dicEntity_Upd = new Dictionary<string, object>();
  49. var dicEntity_Add = new Dictionary<string, object>();
  50. var dic_Entity = new Dictionary<string, object>();
  51. var dic_vals = new Dictionary<string, object>();
  52. var dic_keys = new Dictionary<string, object>();
  53. //更新基本資料
  54. var oEipUpd = new OTB_EIP_BillChangeApply
  55. {
  56. Status = @"B",
  57. ModifyUser = i_crm.USERID,
  58. ModifyDate = DateTime.Now
  59. };
  60. db.Updateable(oEipUpd)
  61. .UpdateColumns(it => new { it.Status, it.ModifyUser, it.ModifyDate })
  62. .Where(it => it.Guid == sId).ExecuteCommand();
  63. //更新代辦
  64. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  65. var jaCheckFlows = (JArray)JsonConvert.DeserializeObject(oEip.CheckFlows);
  66. var MinOrderToNotice = jaCheckFlows.Min(x => ((JObject)x)["Order"].ToString()).ToString();
  67. var saSignedId = jaCheckFlows.Where(x => ((JObject)x)["Order"].ToString() == MinOrderToNotice).Select(x => ((JObject)x)["SignedId"].ToString()).ToList();
  68. if (saSignedId.Count > 0)
  69. {
  70. foreach (string signedId in saSignedId)
  71. {
  72. //添加提醒消息
  73. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, signedId, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  74. db.Insertable(oTipsAdd).ExecuteCommand();
  75. //添加代辦
  76. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, signedId, sTitle, @"BillChangeApply_View", @"?Action=Upd&Guid=" + oEip.Guid, @"B");
  77. db.Insertable(oTaskAdd).ExecuteCommand();
  78. }
  79. }
  80. rm = new SuccessResponseMessage(null, i_crm);
  81. rm.DATA.Add(BLWording.REL, saSignedId);
  82. } while (false);
  83. return rm;
  84. });
  85. }
  86. catch (Exception ex)
  87. {
  88. sMsg = Util.GetLastExceptionMsg(ex);
  89. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(BillChangeApply_UpdService), @"帳單更改申請單管理", @"BillChangeApplyToAudit(帳單更改申請單提交簽核)", @"", @"", @"");
  90. }
  91. finally
  92. {
  93. if (null != sMsg)
  94. {
  95. rm = new ErrorResponseMessage(sMsg, i_crm);
  96. }
  97. }
  98. return rm;
  99. }
  100. #endregion 帳單更改申請單提交簽核
  101. #region 帳單更改申請單簽核|签辦
  102. /// <summary>
  103. /// 帳單更改申請單簽核|签辦
  104. /// </summary>
  105. /// <param name="i_crm">todo: describe i_crm parameter on BillChangeApplyAudit</param>
  106. /// <returns></returns>
  107. public ResponseMessage BillChangeApplyAudit(RequestMessage i_crm)
  108. {
  109. ResponseMessage rm = null;
  110. string sMsg = null;
  111. try
  112. {
  113. rm = SugarBase.ExecTran(db =>
  114. {
  115. do
  116. {
  117. var sId = _fetchString(i_crm, @"Guid");
  118. var sAction = _fetchString(i_crm, @"Action");
  119. var sGoNext = _fetchString(i_crm, @"GoNext");
  120. var sHandlePerson = _fetchString(i_crm, @"HandlePerson");
  121. var sNextSignedWays = _fetchString(i_crm, @"NextSignedWays");
  122. var sNextUsers = _fetchString(i_crm, @"NextUsers");
  123. var sTipsUsers = _fetchString(i_crm, @"TipsUsers");
  124. var sCheckFlows = _fetchString(i_crm, @"CheckFlows");
  125. var sHandleFlows = _fetchString(i_crm, @"HandleFlows");
  126. var sSignedDecision = _fetchString(i_crm, @"SignedDecision");
  127. var sHandleDecision = _fetchString(i_crm, @"HandleDecision");
  128. var sdb = new SimpleClient<OTB_EIP_BillChangeApply>(db);
  129. var oEip = sdb.GetById(sId);
  130. var TipsType = WebAppGlobalConstWord.CHECK;
  131. if (oEip == null)
  132. {
  133. sMsg = @"系統找不到對應的帳單更改申請單資料,請核查!";
  134. break;
  135. }
  136. var saNextSignedWays = (JArray)JsonConvert.DeserializeObject(sNextSignedWays);
  137. var saNextUsers = (JArray)JsonConvert.DeserializeObject(sNextUsers);
  138. var saTipsUsers = (JArray)JsonConvert.DeserializeObject(sTipsUsers);
  139. if (oEip.Guid == null)
  140. {
  141. sMsg = @"系統找不到對應的請假資料,請核查!";
  142. break;
  143. }
  144. var oUser_Self = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  145. var oAskTheDummy = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == oEip.Applicant);
  146. var sTitle_Self = @"";
  147. var sTitle_Handle = oUser_Self.MemberName + @"審批了" + oAskTheDummy.MemberName + @"的帳單更改申請單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  148. var sTitle_Next = oAskTheDummy.MemberName + @"的帳單更改申請單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  149. var sTitle_Notice = @"";
  150. var sStatus = @"B";
  151. if (sAction == @"Signed")
  152. {
  153. sTitle_Self = oUser_Self.MemberName + @"審批了您的帳單更改申請單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  154. sTitle_Notice = oAskTheDummy.MemberName + @"的帳單更改申請單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber + @",請點擊查看...";
  155. if (sSignedDecision == @"Y")
  156. {
  157. sTitle_Self += @",審批結果:同意";
  158. if (sHandlePerson != @"")
  159. {
  160. sStatus = @"E";
  161. }
  162. }
  163. else if (sSignedDecision == @"N")
  164. {
  165. sTitle_Self += @",審批結果:不同意";
  166. sStatus = @"D-O";
  167. TipsType = WebAppGlobalConstWord.FAIL;
  168. }
  169. else if (sSignedDecision == @"O")
  170. {
  171. sTitle_Self += @",審批結果:先加簽";
  172. }
  173. }
  174. else
  175. {
  176. sTitle_Self = oUser_Self.MemberName + @"签辦了您的帳單更改申請單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  177. if (sHandleDecision == @"Y")
  178. {
  179. sTitle_Self += @"簽辦結果:同意";
  180. sStatus = @"H-O";
  181. }
  182. else if (sHandleDecision == @"O")
  183. {
  184. sTitle_Self += @"簽辦結果:先轉呈其他主管審批";
  185. }
  186. }
  187. if (i_crm.LANG == @"zh")
  188. {
  189. sTitle_Self = ChineseStringUtility.ToSimplified(sTitle_Self);
  190. sTitle_Next = ChineseStringUtility.ToSimplified(sTitle_Next);
  191. sTitle_Notice = ChineseStringUtility.ToSimplified(sTitle_Notice);
  192. }
  193. //更新基本資料
  194. var oEipUpd = new OTB_EIP_BillChangeApply
  195. {
  196. Status = sStatus,
  197. CheckFlows = sCheckFlows,
  198. HandleFlows = sHandleFlows,
  199. ModifyUser = i_crm.USERID,
  200. ModifyDate = DateTime.Now
  201. };
  202. db.Updateable(oEipUpd)
  203. .UpdateColumns(it => new { it.Status, it.CheckFlows, it.HandleFlows, it.ModifyUser, it.ModifyDate })
  204. .Where(it => it.Guid == sId).ExecuteCommand();
  205. var sOwner = @"";
  206. if (sSignedDecision == @"Y" && sGoNext == @"N")
  207. {
  208. sOwner = i_crm.USERID;
  209. }
  210. //更新代辦
  211. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId, sOwner);
  212. var listTips = new List<OTB_SYS_Tips>();
  213. var listTask = new List<OTB_SYS_Task>();
  214. var listToTips = new List<string>();
  215. if (sStatus != @"D-O")
  216. {
  217. foreach (string flow in saNextSignedWays)
  218. {
  219. if (sGoNext == @"Y")
  220. {
  221. if (flow == @"flow4")//添加通知和提醒給下個流程所有要通知的人
  222. {
  223. foreach (string user in saTipsUsers)
  224. {
  225. //添加提醒消息
  226. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Notice, user, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  227. listTips.Add(oTipsAdd);
  228. listToTips.Add(user);
  229. }
  230. }
  231. else if (flow == @"flow5")//添加通知和提醒給經辦人
  232. {
  233. if (sHandlePerson != @"")
  234. {
  235. if (sHandleDecision != @"N")
  236. {
  237. //添加代辦
  238. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, sHandlePerson, sTitle_Handle, @"BillChangeApply_View", @"?Action=Upd&Guid=" + oEip.Guid, @"E");
  239. listTask.Add(oTaskAdd);
  240. //添加提醒消息
  241. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Handle, sHandlePerson, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  242. listTips.Add(oTipsAdd);
  243. listToTips.Add(sHandlePerson);
  244. }
  245. }
  246. }
  247. else
  248. {
  249. foreach (string user in saNextUsers)//添加通知和提醒給下一個審核的人
  250. {
  251. if (sSignedDecision == @"Y" || sSignedDecision == @"O")
  252. {
  253. //添加代辦
  254. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, user, sTitle_Next, @"BillChangeApply_View", @"?Action=Upd&Guid=" + oEip.Guid, @"G");
  255. listTask.Add(oTaskAdd);
  256. //添加提醒消息
  257. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Next, user, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  258. listTips.Add(oTipsAdd);
  259. listToTips.Add(user);
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. //添加提醒消息(給請假的人)
  267. var oTips_Applicant = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Self, oEip.Applicant, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  268. listTips.Add(oTips_Applicant);
  269. listToTips.Add(oEip.Applicant);
  270. if (listTips.Count > 0)
  271. {
  272. db.Insertable(listTips).ExecuteCommand();
  273. }
  274. if (listTask.Count > 0)
  275. {
  276. db.Insertable(listTask).ExecuteCommand();
  277. }
  278. rm = new SuccessResponseMessage(null, i_crm);
  279. rm.DATA.Add(BLWording.REL, listToTips);
  280. } while (false);
  281. return rm;
  282. });
  283. }
  284. catch (Exception ex)
  285. {
  286. sMsg = Util.GetLastExceptionMsg(ex);
  287. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(BillChangeApply_UpdService), @"帳單更改申請單管理", @"BillChangeApplyAudit(帳單更改申請單簽核|签辦)", @"", @"", @"");
  288. }
  289. finally
  290. {
  291. if (null != sMsg)
  292. {
  293. rm = new ErrorResponseMessage(sMsg, i_crm);
  294. }
  295. }
  296. return rm;
  297. }
  298. #endregion 帳單更改申請單簽核|签辦
  299. #region 帳單更改申請單簽核前面所有人
  300. /// <summary>
  301. /// 請款單(廠商)簽核前面所有人
  302. /// </summary>
  303. /// <param name="i_crm">todo: describe i_crm parameter on InvoiceApplyForCustomerAudit</param>
  304. /// <returns></returns>
  305. public ResponseMessage BillChangeApplyAuditAll(RequestMessage i_crm)
  306. {
  307. string SettingItemID = "InvoiceApplySuperAuditor";
  308. string FunctionName = nameof(BillChangeApplyAuditAll);
  309. string FunctionDescription = "帳單更改申請單簽核前面所有人";
  310. ResponseMessage rm = null;
  311. string sMsg = null;
  312. try
  313. {
  314. rm = SugarBase.ExecTran(db =>
  315. {
  316. do
  317. {
  318. var sId = _fetchString(i_crm, @"Guid");
  319. var sAutoSignedDecision = _fetchString(i_crm, @"AutoSignedDecision");
  320. var bAutoSignedDecision = sAutoSignedDecision == "Y" ? true : false;
  321. var sAutoSignedOpinion = _fetchString(i_crm, @"AutoSignedOpinion");
  322. var sdb = new SimpleClient<OTB_EIP_BillChangeApply>(db);
  323. var oEip = sdb.GetById(sId);
  324. var SuperAuditorList = Common.GetSystemSetting(db, i_crm.ORIGID, SettingItemID);
  325. var TipsType = WebAppGlobalConstWord.CHECK;
  326. if (string.IsNullOrWhiteSpace(SuperAuditorList) || !SuperAuditorList.Contains(i_crm.USERID))
  327. {
  328. sMsg = @"該使用者無法執行自動簽核,權限不足或者無設置參數。";
  329. break;
  330. }
  331. if (oEip == null || oEip.Guid == null)
  332. {
  333. sMsg = @"系統找不到對應的帳單更改申請單資料,請核查!";
  334. break;
  335. }
  336. //手動轉換
  337. var InvoiceApply = new OTB_EIP_InvoiceApplyInfo()
  338. {
  339. Payee = "B",
  340. KeyNote = oEip.KeyNote,
  341. SignedNumber = oEip.SignedNumber,
  342. };
  343. var Auditor = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  344. var Applicant = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == oEip.Applicant);
  345. //Eip、Task、Tip
  346. OTB_EIP_InvoiceApplyInfo UpdateInvoiceApplyInfo = null;
  347. var InsertTasks = new List<OTB_SYS_Task>();
  348. var InsertTips = new List<OTB_SYS_Tips>();
  349. var NoticeUserBySignalR = new List<string>();
  350. var NotificationInfo = SuperAuditor.GetNotification(InvoiceApply, Auditor, Applicant, bAutoSignedDecision);
  351. switch (bAutoSignedDecision)
  352. {
  353. case true:
  354. {
  355. TipsType = WebAppGlobalConstWord.CHECK;
  356. var ToNotifications = new List<string>();
  357. UpdateInvoiceApplyInfo = SuperAuditor.AgreeAll(oEip.CheckFlows, i_crm.USERID, sAutoSignedOpinion, out ToNotifications);
  358. var oTask = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, oEip.Handle_Person, NotificationInfo.Item1, @"BillChangeApply_View", @"?Action=Upd&Guid=" + oEip.Guid, @"E");
  359. InsertTasks.Add(oTask);//經辦人員,新增代辦事項
  360. var oTipsToHandlePerson = SYS.Task_QryService.TipsAdd(i_crm, NotificationInfo.Item1, oEip.Handle_Person, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  361. InsertTips.Add(oTipsToHandlePerson); //經辦人員,新增提醒
  362. NoticeUserBySignalR.Add(oEip.Handle_Person);
  363. foreach (var Name in ToNotifications)
  364. {
  365. var _Tip = SYS.Task_QryService.TipsAdd(i_crm, NotificationInfo.Item1 + @",請點擊查看...", Name, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  366. InsertTips.Add(_Tip);//簽核成員,通知
  367. NoticeUserBySignalR.Add(Name);
  368. }
  369. NoticeUserBySignalR.Add(oEip.Applicant);
  370. }
  371. break;
  372. case false:
  373. {
  374. TipsType = WebAppGlobalConstWord.FAIL;
  375. UpdateInvoiceApplyInfo = SuperAuditor.Disagree(oEip.CheckFlows, i_crm.USERID, sAutoSignedOpinion);
  376. }
  377. break;
  378. default:
  379. break;
  380. }
  381. if(UpdateInvoiceApplyInfo !=null)
  382. {
  383. oEip.CheckFlows = UpdateInvoiceApplyInfo.CheckFlows;
  384. oEip.ModifyUser = UpdateInvoiceApplyInfo.ModifyUser;
  385. oEip.ModifyDate = UpdateInvoiceApplyInfo.ModifyDate;
  386. oEip.Status = UpdateInvoiceApplyInfo.Status;
  387. }
  388. var oTipsToApplicant = SYS.Task_QryService.TipsAdd(i_crm, NotificationInfo.Item1 + NotificationInfo.Item2, oEip.Applicant, @"BillChangeApply_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  389. InsertTips.Add(oTipsToApplicant);
  390. NoticeUserBySignalR.Add(oEip.Applicant);
  391. db.Updateable(oEip)
  392. .UpdateColumns(it => new { it.CheckFlows, it.ModifyUser, it.ModifyDate, it.Status })
  393. .Where(it => it.Guid == sId).ExecuteCommand();
  394. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId, "");//清除目前關聯的task
  395. if (InsertTips.Count > 0)
  396. {
  397. db.Insertable(InsertTips).ExecuteCommand();
  398. }
  399. if (InsertTasks.Count > 0)
  400. {
  401. db.Insertable(InsertTasks).ExecuteCommand();
  402. }
  403. NoticeUserBySignalR = NoticeUserBySignalR.Distinct().ToList();
  404. rm = new SuccessResponseMessage(null, i_crm);
  405. rm.DATA.Add(BLWording.REL, NoticeUserBySignalR);
  406. } while (false);
  407. return rm;
  408. });
  409. }
  410. catch (Exception ex)
  411. {
  412. sMsg = Util.GetLastExceptionMsg(ex);
  413. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, ProgramID, ProgramName, FunctionName + FunctionDescription, @"", @"", @"");
  414. }
  415. finally
  416. {
  417. if (null != sMsg)
  418. {
  419. rm = new ErrorResponseMessage(sMsg, i_crm);
  420. }
  421. }
  422. return rm;
  423. }
  424. #endregion
  425. }
  426. }