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.

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