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.

277 lines
14 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Entity.ViewModels;
  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.Data;
  11. namespace EasyBL.WEBAPP.EIP
  12. {
  13. public class InvoiceApplyForPersonal_QryService : ServiceBase
  14. {
  15. #region 請款單(個人)分頁查詢
  16. /// <summary>
  17. /// 請款單(個人)分頁查詢
  18. /// </summary>
  19. /// <param name="i_crm"></param>
  20. /// <returns></returns>
  21. public ResponseMessage QueryPage(RequestMessage i_crm)
  22. {
  23. ResponseMessage rm = null;
  24. string sMsg = null;
  25. var db = SugarBase.GetIntance();
  26. try
  27. {
  28. do
  29. {
  30. var pml = new PageModel
  31. {
  32. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  33. PageSize = _fetchInt(i_crm, @"pageSize")
  34. };
  35. var iPageCount = 0;
  36. var sSortField = _fetchString(i_crm, @"sortField");
  37. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  38. var sKeyNote = _fetchString(i_crm, @"KeyNote");
  39. var sApplicant = _fetchString(i_crm, @"Applicant");
  40. var sImportant = _fetchString(i_crm, @"Important");
  41. var sPaymentWay = _fetchString(i_crm, @"PaymentWay");
  42. var sStatus = _fetchString(i_crm, @"Status");
  43. var sRoles = _fetchString(i_crm, @"Roles");
  44. var sPaymentTimeStart = _fetchString(i_crm, @"PaymentTimeStart");
  45. var sPaymentTimeEnd = _fetchString(i_crm, @"PaymentTimeEnd");
  46. var bExcel = _fetchBool(i_crm, @"Excel");
  47. var rPaymentTimeStart = new DateTime();
  48. var rPaymentTimeEnd = new DateTime();
  49. if (!string.IsNullOrEmpty(sPaymentTimeStart))
  50. {
  51. rPaymentTimeStart = SqlFunc.ToDate(sPaymentTimeStart);
  52. }
  53. if (!string.IsNullOrEmpty(sPaymentTimeEnd))
  54. {
  55. rPaymentTimeEnd = SqlFunc.ToDate(sPaymentTimeEnd).AddDays(1);
  56. }
  57. pml.DataList = db.Queryable<OTB_EIP_InvoiceApplyInfo, OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Members, OTB_SYS_Members>
  58. ((t1, t2, t3, t4, t5) =>
  59. new object[] {
  60. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Applicant == t2.MemberID,
  61. JoinType.Inner, t2.OrgID == t3.OrgID && t2.DepartmentID == t3.DepartmentID,
  62. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Handle_Person == t4.MemberID,
  63. JoinType.Left, t1.OrgID == t5.OrgID && t1.Payee == t5.MemberID
  64. }
  65. )
  66. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID && t1.KeyNote.Contains(sKeyNote) && sImportant.Contains(t1.Important) && sPaymentWay.Contains(t1.PaymentWay) && t1.PayeeType == "P")
  67. .WhereIF(!string.IsNullOrEmpty(sApplicant), (t1, t2, t3, t4, t5) => t1.Applicant == sApplicant)
  68. .WhereIF(!string.IsNullOrEmpty(sStatus), (t1, t2, t3, t4, t5) => sStatus.Contains(t1.Status))
  69. .WhereIF(!(sRoles.Contains("Admin") || sRoles.Contains("EipManager") || sRoles.Contains("EipView")), (t1, t2, t3, t4, t5) => (t1.Applicant == i_crm.USERID || t1.Handle_Person == i_crm.USERID || t1.CheckFlows.Contains(i_crm.USERID)))
  70. .WhereIF(!string.IsNullOrEmpty(sPaymentTimeStart), (t1, t2, t3, t4, t5) => t1.PaymentTime >= rPaymentTimeStart.Date)
  71. .WhereIF(!string.IsNullOrEmpty(sPaymentTimeEnd), (t1, t2, t3, t4, t5) => t1.PaymentTime <= rPaymentTimeEnd.Date)
  72. .Select((t1, t2, t3, t4, t5) => new View_EIP_InvoiceApplyInfo
  73. {
  74. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  75. ApplicantName = t2.MemberName,
  76. Handle_PersonName = t4.MemberName,
  77. PayeeCode = t5.WenZhongAcount,
  78. DeptName = t3.DepartmentName
  79. })
  80. .MergeTable()
  81. .OrderBy(sSortField, sSortOrder)
  82. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  83. pml.Total = iPageCount;
  84. rm = new SuccessResponseMessage(null, i_crm);
  85. if (bExcel)
  86. {
  87. const string sFileName = "各項獎金";
  88. var oHeader = new Dictionary<string, string>
  89. {
  90. { "RowIndex", "項次" },
  91. { "PayeeCode", "員工代號" },
  92. { "PayeeName", "員工姓名" },
  93. { "EffectTime", "生效時間" },
  94. { "Currency", "幣別" },
  95. { "ExchangeRate", "匯率" },
  96. { "FeeItemName", "事由" },
  97. { "TicketCost", "機票費用" },
  98. { "TravelCost", "差旅費" },
  99. { "ActingMatCost", "代墊款項" },
  100. { "OtherCost", "代墊款項" }
  101. };
  102. var dt_new = new DataTable();
  103. dt_new.Columns.Add("RowIndex");
  104. dt_new.Columns.Add("PayeeCode");
  105. dt_new.Columns.Add("PayeeName");
  106. dt_new.Columns.Add("EffectTime");
  107. dt_new.Columns.Add("Currency");
  108. dt_new.Columns.Add("ExchangeRate");
  109. dt_new.Columns.Add("FeeItemName");
  110. dt_new.Columns.Add("TicketCost");
  111. dt_new.Columns.Add("TravelCost");
  112. dt_new.Columns.Add("ActingMatCost");
  113. dt_new.Columns.Add("OtherCost");
  114. var listMerge = new List<Dictionary<string, int>>();
  115. var saInvoiceApplyInfo = pml.DataList as List<View_EIP_InvoiceApplyInfo>;
  116. foreach (var item in saInvoiceApplyInfo)
  117. {
  118. var jaPayeeInfo = (JArray)JsonConvert.DeserializeObject(item.PayeeInfo);
  119. foreach (JObject payeeinfo in jaPayeeInfo)
  120. {
  121. var row_new = dt_new.NewRow();
  122. var sCurrency = payeeinfo["Currency"].ToString();
  123. var iAmount = Convert.ToDouble(payeeinfo["Amount"].ToString());
  124. row_new["RowIndex"] = item.RowIndex;
  125. row_new["PayeeCode"] = item.PayeeCode;
  126. row_new["PayeeName"] = item.PayeeName;
  127. row_new["EffectTime"] = (item.EffectTime == null) ? "" : Convert.ToDateTime(item.EffectTime).ToString("yyyy/MM/dd");
  128. row_new["Currency"] = sCurrency;
  129. row_new["ExchangeRate"] = 1;
  130. row_new["FeeItemName"] = payeeinfo["FeeItemName"];
  131. if (payeeinfo["FeeType"].ToString() == "001")
  132. {
  133. row_new["TicketCost"] = sCurrency == "NTD" ? $"{iAmount:N0}" : $"{iAmount:N2}";
  134. row_new["TravelCost"] = "";
  135. row_new["ActingMatCost"] = "";
  136. row_new["OtherCost"] = "";
  137. }
  138. else if (payeeinfo["FeeType"].ToString() == "002")
  139. {
  140. row_new["TicketCost"] = "";
  141. row_new["TravelCost"] = sCurrency == "NTD" ? $"{iAmount:N0}" : $"{iAmount:N2}";
  142. row_new["ActingMatCost"] = "";
  143. row_new["OtherCost"] = "";
  144. }
  145. else if (payeeinfo["FeeType"].ToString() == "003")
  146. {
  147. row_new["TicketCost"] = "";
  148. row_new["TravelCost"] = "";
  149. row_new["ActingMatCost"] = sCurrency == "NTD" ? $"{iAmount:N0}" : $"{iAmount:N2}";
  150. row_new["OtherCost"] = "";
  151. }
  152. else if (payeeinfo["FeeType"].ToString() == "004")
  153. {
  154. row_new["TicketCost"] = "";
  155. row_new["TravelCost"] = "";
  156. row_new["ActingMatCost"] = "";
  157. row_new["OtherCost"] = sCurrency == "NTD" ? $"{iAmount:N0}" : $"{iAmount:N2}";
  158. }
  159. dt_new.Rows.Add(row_new);
  160. }
  161. }
  162. var dicAlain = ExcelService.GetExportAlain(oHeader, "Currency,ExchangeRate,PayeeCode", "TicketCost,TravelCost,ActingMatCost,OtherCost");
  163. var bOk = new ExcelService().CreateExcelByTb(dt_new, out string sPath, oHeader, dicAlain, listMerge, sFileName);
  164. rm.DATA.Add(BLWording.REL, sPath);
  165. }
  166. else
  167. {
  168. rm.DATA.Add(BLWording.REL, pml);
  169. }
  170. } while (false);
  171. }
  172. catch (Exception ex)
  173. {
  174. sMsg = Util.GetLastExceptionMsg(ex);
  175. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.InvoiceApplyForPersonal_QryService", "", "QueryPage(請款單(個人)分頁查詢)", "", "", "");
  176. }
  177. finally
  178. {
  179. if (null != sMsg)
  180. {
  181. rm = new ErrorResponseMessage(sMsg, i_crm);
  182. }
  183. }
  184. return rm;
  185. }
  186. #endregion 請款單(個人)分頁查詢
  187. #region 請款單(個人)單筆查詢
  188. /// <summary>
  189. /// 請款單(個人)單筆查詢
  190. /// </summary>
  191. /// <param name="i_crm"></param>
  192. /// <returns></returns>
  193. public ResponseMessage QueryOne(RequestMessage i_crm)
  194. {
  195. ResponseMessage rm = null;
  196. string sMsg = null;
  197. var db = SugarBase.GetIntance();
  198. try
  199. {
  200. do
  201. {
  202. var sId = _fetchString(i_crm, @"Guid");
  203. var oEntity = db.Queryable<OTB_EIP_InvoiceApplyInfo, OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Members, OTB_SYS_Members>
  204. ((t1, t2, t3, t4, t5) =>
  205. new object[] {
  206. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Applicant == t2.MemberID,
  207. JoinType.Inner, t2.OrgID == t3.OrgID && t2.DepartmentID == t3.DepartmentID,
  208. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Handle_Person == t4.MemberID,
  209. JoinType.Left, t1.OrgID == t5.OrgID && t1.Payee == t5.MemberID
  210. }
  211. )
  212. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID && t1.Guid == sId)
  213. .Select((t1, t2, t3, t4, t5) => new View_EIP_InvoiceApplyInfo
  214. {
  215. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  216. ApplicantName = t2.MemberName,
  217. Handle_PersonName = t4.MemberName,
  218. PayeeCode = t5.WenZhongAcount,
  219. DeptName = t3.DepartmentName
  220. }).Single();
  221. if (!string.IsNullOrEmpty(oEntity.RelationId))
  222. {
  223. var oRelation = db.Queryable<OTB_EIP_InvoiceApplyInfo>().Single(x => x.OrgID == i_crm.ORIGID && x.Guid == oEntity.RelationId);
  224. if (oRelation != null)
  225. {
  226. oEntity.ExFeild1 = oRelation.KeyNote;
  227. }
  228. }
  229. //重新抓取CheckFlow
  230. var oFlow = db.Queryable<OTB_EIP_CheckFlow>().Single(x => x.OrgID == i_crm.ORIGID && x.Guid == oEntity.FlowId);
  231. if (oFlow != null)
  232. {
  233. oEntity.Flows_Lock = oFlow.Flows_Lock ?? "N";
  234. oEntity.Handle_Lock = oFlow.Handle_Lock ?? "N";
  235. oEntity.CheckOrder = oFlow.Flows;
  236. oEntity.Handle_DeptID = oFlow.Handle_DeptID;
  237. oEntity.Handle_Person = oFlow.Handle_Person;
  238. }
  239. else {
  240. sMsg = "抓取流程資料異常";
  241. }
  242. rm = new SuccessResponseMessage(null, i_crm);
  243. rm.DATA.Add(BLWording.REL, oEntity);
  244. } while (false);
  245. }
  246. catch (Exception ex)
  247. {
  248. sMsg = Util.GetLastExceptionMsg(ex);
  249. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.InvoiceApplyForPersonal_QryService", "", "QueryOne(請款單(個人)單筆查詢)", "", "", "");
  250. }
  251. finally
  252. {
  253. if (null != sMsg)
  254. {
  255. rm = new ErrorResponseMessage(sMsg, i_crm);
  256. }
  257. }
  258. return rm;
  259. }
  260. #endregion 請款單(個人)單筆查詢
  261. }
  262. }