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.

515 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 TravelExpenseReport_UpdService : ServiceBase
  14. {
  15. public const string PGNAME = "國外差旅費報告書";
  16. public const string SERVICER = nameof(TravelExpenseReport_UpdService);
  17. public ResponseMessage Insert(RequestMessage i_crm)
  18. {
  19. ResponseMessage rm = null;
  20. string sMsg = null;
  21. try
  22. {
  23. rm = SugarBase.ExecTran(db =>
  24. {
  25. do
  26. {
  27. //客戶資料表身
  28. var oEntity = _fetchEntity<OTB_EIP_TravelExpense>(i_crm);
  29. _setEntityBase(oEntity, i_crm);
  30. UpdateTravelFees(oEntity);
  31. var saPm = oEntity.SignedNumber.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  32. oEntity.SignedNumber = SerialNumber.GetMaxNumberByType(saPm[1].ToString(), saPm[2].ToString(), SerialNumber.GetMaxNumberType(saPm[3].ToString()), i_crm.USERID, int.Parse(saPm[4].ToString()), saPm.Length > 5 ? saPm[5] : "", saPm.Length > 6 ? saPm[6] : ""); //獲取最大編號
  33. var iRel = db.Insertable(oEntity).ExecuteReturnEntity();
  34. rm = new SuccessResponseMessage(null, i_crm);
  35. rm.DATA.Add(BLWording.REL, iRel);
  36. } while (false);
  37. return rm;
  38. });
  39. }
  40. catch (Exception ex)
  41. {
  42. sMsg = Util.GetLastExceptionMsg(ex);
  43. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_UpdService), @"請假區間編輯", @"Add(請假區間編輯(新增))", @"", @"", @"");
  44. }
  45. finally
  46. {
  47. if (null != sMsg)
  48. {
  49. rm = new ErrorResponseMessage(sMsg, i_crm);
  50. }
  51. }
  52. return rm;
  53. }
  54. /// <summary>
  55. /// 更新資料
  56. /// TODO:前端提交審核(儲存+審核)可以合併
  57. /// </summary>
  58. /// <param name="i_crm"></param>
  59. /// <returns></returns>
  60. public ResponseMessage Update(RequestMessage i_crm)
  61. {
  62. ResponseMessage rm = null;
  63. string sMsg = null;
  64. try
  65. {
  66. rm = SugarBase.ExecTran(db =>
  67. {
  68. do
  69. {
  70. var oNewEntity = _fetchEntity<OTB_EIP_TravelExpense>(i_crm);
  71. _setEntityBase(oNewEntity, i_crm);
  72. UpdateTravelFees(oNewEntity);
  73. var iRel = db.Updateable(oNewEntity).ExecuteCommand();
  74. rm = new SuccessResponseMessage(null, i_crm);
  75. rm.DATA.Add(BLWording.REL, oNewEntity);
  76. } while (false);
  77. return rm;
  78. });
  79. }
  80. catch (Exception ex)
  81. {
  82. sMsg = Util.GetLastExceptionMsg(ex);
  83. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_UpdService), @"請假區間編輯", @"Update(請假區間編輯(修改))", @"", @"", @"");
  84. }
  85. finally
  86. {
  87. if (null != sMsg)
  88. {
  89. rm = new ErrorResponseMessage(sMsg, i_crm);
  90. }
  91. }
  92. return rm;
  93. }
  94. #region 國外差旅費報告書提交簽核
  95. /// <summary>
  96. /// 國外差旅費報告書提交簽核
  97. /// </summary>
  98. /// <param name="i_crm">todo: describe i_crm parameter on TravelExpenseReportToAudit</param>
  99. /// <returns></returns>
  100. public ResponseMessage TravelExpenseReportToAudit(RequestMessage i_crm)
  101. {
  102. ResponseMessage rm = null;
  103. string sMsg = null;
  104. try
  105. {
  106. rm = SugarBase.ExecTran(db =>
  107. {
  108. do
  109. {
  110. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  111. var sdb = new SimpleClient<OTB_EIP_TravelExpense>(db);
  112. var oEip = sdb.GetById(sId);
  113. if (oEip == null)
  114. {
  115. sMsg = @"系統找不到對應的國外差旅費報告書資料,請核查!";
  116. break;
  117. }
  118. var oApplicant = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == oEip.Applicant);
  119. var sTitle = oApplicant.MemberName + @"的國外差旅費報告書申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  120. if (i_crm.LANG == @"zh")
  121. {
  122. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  123. }
  124. //更新基本資料
  125. var oEipUpd = new OTB_EIP_TravelExpense
  126. {
  127. Status = @"B",
  128. ModifyUser = i_crm.USERID,
  129. ModifyDate = DateTime.Now
  130. };
  131. db.Updateable(oEipUpd)
  132. .UpdateColumns(it => new { it.Status, it.ModifyUser, it.ModifyDate })
  133. .Where(it => it.Guid == sId).ExecuteCommand();
  134. //更新代辦
  135. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  136. var jaCheckFlows = (JArray)JsonConvert.DeserializeObject(oEip.CheckFlows);
  137. var MinOrderToNotice = jaCheckFlows.Min(x => ((JObject)x)["Order"].ToString()).ToString();
  138. var saSignedId = jaCheckFlows.Where(x => ((JObject)x)["Order"].ToString() == MinOrderToNotice).Select(x => ((JObject)x)["SignedId"].ToString()).ToList();
  139. if (saSignedId.Count > 0)
  140. {
  141. foreach (string signedId in saSignedId)
  142. {
  143. //添加提醒消息
  144. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, signedId, @"TravelExpenseReport_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  145. db.Insertable(oTipsAdd).ExecuteCommand();
  146. //添加代辦
  147. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, signedId, sTitle, @"TravelExpenseReport_View", @"?Action=Upd&Guid=" + oEip.Guid, @"B");
  148. db.Insertable(oTaskAdd).ExecuteCommand();
  149. }
  150. }
  151. rm = new SuccessResponseMessage(null, i_crm);
  152. rm.DATA.Add(BLWording.REL, saSignedId);
  153. } while (false);
  154. return rm;
  155. });
  156. }
  157. catch (Exception ex)
  158. {
  159. sMsg = Util.GetLastExceptionMsg(ex);
  160. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TravelExpenseReport_UpdService), @"國外差旅費報告書", @"TravelExpenseReportToAudit(國外差旅費報告書提交簽核)", @"", @"", @"");
  161. }
  162. finally
  163. {
  164. if (null != sMsg)
  165. {
  166. rm = new ErrorResponseMessage(sMsg, i_crm);
  167. }
  168. }
  169. return rm;
  170. }
  171. #endregion 國外差旅費報告書提交簽核
  172. #region 國外差旅費報告書簽核|签辦
  173. /// <summary>
  174. /// 國外差旅費報告書簽核|签辦
  175. /// </summary>
  176. /// <param name="i_crm">todo: describe i_crm parameter on TravelExpenseReportAudit</param>
  177. /// <returns></returns>
  178. public ResponseMessage TravelExpenseReportAudit(RequestMessage i_crm)
  179. {
  180. ResponseMessage rm = null;
  181. string sMsg = null;
  182. try
  183. {
  184. rm = SugarBase.ExecTran(db =>
  185. {
  186. do
  187. {
  188. var sId = _fetchString(i_crm, @"Guid");
  189. var sAction = _fetchString(i_crm, @"Action");
  190. var sGoNext = _fetchString(i_crm, @"GoNext");
  191. var sHandlePerson = _fetchString(i_crm, @"HandlePerson");
  192. var sNextSignedWays = _fetchString(i_crm, @"NextSignedWays");
  193. var sNextUsers = _fetchString(i_crm, @"NextUsers");
  194. var sTipsUsers = _fetchString(i_crm, @"TipsUsers");
  195. var sCheckFlows = _fetchString(i_crm, @"CheckFlows");
  196. var sHandleFlows = _fetchString(i_crm, @"HandleFlows");
  197. var sSignedDecision = _fetchString(i_crm, @"SignedDecision");
  198. var sHandleDecision = _fetchString(i_crm, @"HandleDecision");
  199. var TipsType = WebAppGlobalConstWord.CHECK;
  200. var sdb = new SimpleClient<OTB_EIP_TravelExpense>(db);
  201. var oEip = sdb.GetById(sId);
  202. if (oEip == null)
  203. {
  204. sMsg = @"系統找不到對應的國外差旅費報告書資料,請核查!";
  205. break;
  206. }
  207. var saNextSignedWays = (JArray)JsonConvert.DeserializeObject(sNextSignedWays);
  208. var saNextUsers = (JArray)JsonConvert.DeserializeObject(sNextUsers);
  209. var saTipsUsers = (JArray)JsonConvert.DeserializeObject(sTipsUsers);
  210. if (oEip.Guid == null)
  211. {
  212. sMsg = @"系統找不到對應的國外差旅費報告書資料,請核查!";
  213. break;
  214. }
  215. var oUser_Self = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  216. var oAskTheDummy = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == oEip.Applicant);
  217. var sTitle_Self = @"";
  218. var sTitle_Handle = oUser_Self.MemberName + @"審批了" + oAskTheDummy.MemberName + @"的國外差旅費報告書「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  219. var sTitle_Next = oAskTheDummy.MemberName + @"的國外差旅費報告書申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  220. var sTitle_Notice = @"";
  221. var sStatus = @"B";
  222. if (sAction == @"Signed")
  223. {
  224. sTitle_Self = oUser_Self.MemberName + @"審批了您的國外差旅費報告書「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  225. sTitle_Notice = oAskTheDummy.MemberName + @"的國外差旅費報告書申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber + @",請點擊查看...";
  226. if (sSignedDecision == @"Y")
  227. {
  228. sTitle_Self += @",審批結果:同意";
  229. if (sHandlePerson != @"")
  230. {
  231. sStatus = @"E";
  232. }
  233. }
  234. else if (sSignedDecision == @"N")
  235. {
  236. sTitle_Self += @",審批結果:不同意";
  237. sStatus = @"D-O";
  238. TipsType = WebAppGlobalConstWord.FAIL;
  239. }
  240. else if (sSignedDecision == @"O")
  241. {
  242. sTitle_Self += @",審批結果:先加簽";
  243. }
  244. }
  245. else
  246. {
  247. sTitle_Self = oUser_Self.MemberName + @"签辦了您的國外差旅費報告書申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  248. if (sHandleDecision == @"Y")
  249. {
  250. sTitle_Self += @"簽辦結果:同意";
  251. sStatus = @"H-O";
  252. }
  253. else if (sHandleDecision == @"O")
  254. {
  255. sTitle_Self += @"簽辦結果:先轉呈其他主管審批";
  256. }
  257. }
  258. if (i_crm.LANG == @"zh")
  259. {
  260. sTitle_Self = ChineseStringUtility.ToSimplified(sTitle_Self);
  261. sTitle_Next = ChineseStringUtility.ToSimplified(sTitle_Next);
  262. sTitle_Notice = ChineseStringUtility.ToSimplified(sTitle_Notice);
  263. }
  264. //更新基本資料
  265. var oEipUpd = new OTB_EIP_TravelExpense
  266. {
  267. Status = sStatus,
  268. CheckFlows = sCheckFlows,
  269. HandleFlows = sHandleFlows,
  270. ModifyUser = i_crm.USERID,
  271. ModifyDate = DateTime.Now
  272. };
  273. db.Updateable(oEipUpd)
  274. .UpdateColumns(it => new { it.Status, it.CheckFlows, it.HandleFlows, it.ModifyUser, it.ModifyDate })
  275. .Where(it => it.Guid == sId).ExecuteCommand();
  276. var sOwner = @"";
  277. if (sSignedDecision == @"Y" && sGoNext == @"N")
  278. {
  279. sOwner = i_crm.USERID;
  280. }
  281. //更新代辦
  282. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId, sOwner);
  283. var listTips = new List<OTB_SYS_Tips>();
  284. var listTask = new List<OTB_SYS_Task>();
  285. var listToTips = new List<string>();
  286. if (sStatus != @"D-O")
  287. {
  288. foreach (string flow in saNextSignedWays)
  289. {
  290. if (sGoNext == @"Y")
  291. {
  292. if (flow == @"flow4")//添加通知和提醒給下個流程所有要通知的人
  293. {
  294. foreach (string user in saTipsUsers)
  295. {
  296. //添加提醒消息
  297. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Notice, user, @"TravelExpenseReport_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  298. listTips.Add(oTipsAdd);
  299. listToTips.Add(user);
  300. }
  301. }
  302. else if (flow == @"flow5")//添加通知和提醒給經辦人
  303. {
  304. if (sHandlePerson != @"")
  305. {
  306. if (sHandleDecision != @"N")
  307. {
  308. //添加代辦
  309. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, sHandlePerson, sTitle_Handle, @"TravelExpenseReport_View", @"?Action=Upd&Guid=" + oEip.Guid, @"E");
  310. listTask.Add(oTaskAdd);
  311. //添加提醒消息
  312. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Handle, sHandlePerson, @"TravelExpenseReport_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  313. listTips.Add(oTipsAdd);
  314. listToTips.Add(sHandlePerson);
  315. }
  316. }
  317. }
  318. else
  319. {
  320. foreach (string user in saNextUsers)//添加通知和提醒給下一個審核的人
  321. {
  322. if (sSignedDecision == @"Y" || sSignedDecision == @"O")
  323. {
  324. //添加代辦
  325. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, user, sTitle_Next, @"TravelExpenseReport_View", @"?Action=Upd&Guid=" + oEip.Guid, @"G");
  326. listTask.Add(oTaskAdd);
  327. //添加提醒消息
  328. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Next, user, @"TravelExpenseReport_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  329. listTips.Add(oTipsAdd);
  330. listToTips.Add(user);
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }
  337. //添加提醒消息(給請假的人)
  338. var oTips_AskTheDummy = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Self, oEip.Applicant, @"TravelExpenseReport_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  339. listTips.Add(oTips_AskTheDummy);
  340. listToTips.Add(oEip.Applicant);
  341. if (listTips.Count > 0)
  342. {
  343. db.Insertable(listTips).ExecuteCommand();
  344. }
  345. if (listTask.Count > 0)
  346. {
  347. db.Insertable(listTask).ExecuteCommand();
  348. }
  349. rm = new SuccessResponseMessage(null, i_crm);
  350. rm.DATA.Add(BLWording.REL, listToTips);
  351. } while (false);
  352. return rm;
  353. });
  354. }
  355. catch (Exception ex)
  356. {
  357. sMsg = Util.GetLastExceptionMsg(ex);
  358. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TravelExpenseReport_UpdService), @"國外差旅費報告書", @"TravelExpenseReportAudit(國外差旅費報告書簽核|签辦)", @"", @"", @"");
  359. }
  360. finally
  361. {
  362. if (null != sMsg)
  363. {
  364. rm = new ErrorResponseMessage(sMsg, i_crm);
  365. }
  366. }
  367. return rm;
  368. }
  369. #endregion 國外差旅費報告書簽核|签辦
  370. /// <summary>
  371. /// 更新費用項目
  372. /// 目的解決加總對不上問題
  373. /// </summary>
  374. /// <param name="travelExpense"></param>
  375. public void UpdateTravelFees(OTB_EIP_TravelExpense travelExpense)
  376. {
  377. var Round = 0;
  378. if (travelExpense.OrgID.Equals("SG", StringComparison.OrdinalIgnoreCase))
  379. {
  380. Round = 2;
  381. }
  382. //先處理TG、TE
  383. var travelFeeItems = JsonConvert.DeserializeObject<List<TravelFeeItems>>(travelExpense.TravelFeeItems).OrderBy(c => c.Index).ToList();
  384. var newItemInfo = new TravelFeeInfo();
  385. var travelFeeInfo = JsonConvert.DeserializeObject<TravelFeeInfo>(travelExpense.TravelFeeInfo);
  386. travelFeeInfo.Total = 0;
  387. foreach (var feeItems in travelFeeItems)
  388. {
  389. var total = (feeItems.Amount1 + feeItems.Amount2 + feeItems.Amount3) * feeItems.ExchangeRate;
  390. feeItems.Total = Math.Round(total, Round, MidpointRounding.AwayFromZero);
  391. newItemInfo.Total += Math.Round(feeItems.Total, Round, MidpointRounding.AwayFromZero);
  392. }
  393. newItemInfo.CompanyAdvance = travelFeeInfo.CompanyAdvance;
  394. travelExpense.TravelFeeInfo = JsonConvert.SerializeObject(newItemInfo);
  395. travelExpense.TravelFeeItems = JsonConvert.SerializeObject(travelFeeItems);
  396. }
  397. }
  398. /// <summary>
  399. /// 國外差旅費用項目
  400. /// </summary>
  401. public class TravelFeeItems
  402. {
  403. public string guid { set; get; }
  404. public int Index { set; get; }
  405. /// <summary>
  406. /// 日期
  407. /// </summary>
  408. public string Date { set; get; }
  409. /// <summary>
  410. /// PARTICULARS
  411. /// </summary>
  412. public string PARTICULARS { set; get; }
  413. /// <summary>
  414. /// 幣別
  415. /// </summary>
  416. public string Currency { set; get; }
  417. /// <summary>
  418. /// 日支額
  419. /// </summary>
  420. public decimal Amount1 { set; get; }
  421. /// <summary>
  422. /// 住宿費
  423. /// </summary>
  424. public decimal Amount2 { set; get; }
  425. /// <summary>
  426. /// 其他
  427. /// </summary>
  428. public decimal Amount3 { set; get; }
  429. /// <summary>
  430. /// 匯率
  431. /// </summary>
  432. public decimal ExchangeRate { set; get; }
  433. /// <summary>
  434. /// 總計
  435. /// </summary>
  436. public decimal Total { set; get; }
  437. }
  438. /// <summary>
  439. /// 國外差旅費用總計
  440. /// </summary>
  441. public class TravelFeeInfo
  442. {
  443. /// <summary>
  444. /// TOTAL
  445. /// </summary>
  446. public decimal Total { set; get; }
  447. /// <summary>
  448. /// 公司預支
  449. /// </summary>
  450. public decimal CompanyAdvance { set; get; }
  451. /// <summary>
  452. /// 公司應付
  453. /// </summary>
  454. public decimal CompanyCope
  455. {
  456. get
  457. {
  458. return Total - CompanyAdvance;
  459. }
  460. }
  461. /// <summary>
  462. /// 總計(等同公司應付)
  463. /// </summary>
  464. public decimal Sum
  465. {
  466. get
  467. {
  468. return CompanyCope;
  469. }
  470. }
  471. }
  472. }