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.

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