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.

235 lines
12 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 InvoiceApplyForCustomer_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 sStatus = _fetchString(i_crm, @"Status");
  42. var sRoles = _fetchString(i_crm, @"Roles");
  43. var sCreateDateStart = _fetchString(i_crm, @"CreateDateStart");
  44. var sCreateDateEnd = _fetchString(i_crm, @"CreateDateEnd");
  45. var bExcel = _fetchBool(i_crm, @"Excel");
  46. var rCreateDateStart = new DateTime();
  47. var rCreateDateEnd = new DateTime();
  48. if (!string.IsNullOrEmpty(sCreateDateStart))
  49. {
  50. rCreateDateStart = SqlFunc.ToDate(sCreateDateStart);
  51. }
  52. if (!string.IsNullOrEmpty(sCreateDateEnd))
  53. {
  54. rCreateDateEnd = SqlFunc.ToDate(sCreateDateEnd).AddDays(1);
  55. }
  56. pml.DataList = db.Queryable<OTB_EIP_InvoiceApplyInfo, OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Members, OTB_CRM_Customers>
  57. ((t1, t2, t3, t4, t5) =>
  58. new object[] {
  59. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Applicant == t2.MemberID,
  60. JoinType.Inner, t2.OrgID == t3.OrgID && t2.DepartmentID == t3.DepartmentID,
  61. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Handle_Person == t4.MemberID,
  62. JoinType.Left, t1.OrgID == t5.OrgID && t1.Payee == t5.guid
  63. }
  64. )
  65. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID && t1.KeyNote.Contains(sKeyNote) && sImportant.Contains(t1.Important) && t1.PayeeType == "C")
  66. .WhereIF(!string.IsNullOrEmpty(sApplicant), (t1, t2, t3, t4, t5) => t1.Applicant == sApplicant)
  67. .WhereIF(!string.IsNullOrEmpty(sStatus), (t1, t2, t3, t4, t5) => sStatus.Contains(t1.Status))
  68. .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)))
  69. .WhereIF(!string.IsNullOrEmpty(sCreateDateStart), (t1, t2, t3, t4, t5) => t1.CreateDate >= rCreateDateStart.Date)
  70. .WhereIF(!string.IsNullOrEmpty(sCreateDateEnd), (t1, t2, t3, t4, t5) => t1.CreateDate <= rCreateDateEnd.Date)
  71. .Select((t1, t2, t3, t4, t5) => new View_EIP_InvoiceApplyInfo
  72. {
  73. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  74. ApplicantName = t2.MemberName,
  75. Handle_PersonName = t4.MemberName,
  76. CustomerNO = t5.CustomerNO,
  77. DeptName = t3.DepartmentName
  78. })
  79. .MergeTable()
  80. .OrderBy(sSortField, sSortOrder)
  81. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  82. pml.Total = iPageCount;
  83. rm = new SuccessResponseMessage(null, i_crm);
  84. if (bExcel)
  85. {
  86. const string sFileName = "請款單(廠商)費用明細";
  87. var oHeader = new Dictionary<string, string>
  88. {
  89. { "RowIndex", "項次" },
  90. { "PayeeCode", "受款人代號" },
  91. { "Payee", "受款人" },
  92. { "FeeItemName", "費用名稱" },
  93. { "BillNO", "帳單號碼" },
  94. { "PrjCode", "專案代號" },
  95. { "Currency", "幣別" },
  96. { "Amount", "金額" },
  97. { "PaymentWay", "付款方式" },
  98. { "PaymentTime", "支付時間" }
  99. };
  100. var dt_new = new DataTable();
  101. dt_new.Columns.Add("RowIndex");
  102. dt_new.Columns.Add("PayeeCode");
  103. dt_new.Columns.Add("Payee");
  104. dt_new.Columns.Add("FeeItemName");
  105. dt_new.Columns.Add("BillNO");
  106. dt_new.Columns.Add("PrjCode");
  107. dt_new.Columns.Add("Currency");
  108. dt_new.Columns.Add("Amount");
  109. dt_new.Columns.Add("PaymentWay");
  110. dt_new.Columns.Add("PaymentTime");
  111. var listMerge = new List<Dictionary<string, int>>();
  112. var saInvoiceApplyInfo = pml.DataList as List<View_EIP_InvoiceApplyInfo>;
  113. foreach (var item in saInvoiceApplyInfo)
  114. {
  115. var jaPayeeInfo = (JArray)JsonConvert.DeserializeObject(item.PayeeInfo);
  116. var sPaymentWay = item.PaymentWay;
  117. foreach (JObject payeeinfo in jaPayeeInfo)
  118. {
  119. var row_new = dt_new.NewRow();
  120. var sCurrency = payeeinfo["Currency"].ToString();
  121. var iAmount = Convert.ToDouble(payeeinfo["Amount"].ToString());
  122. row_new["RowIndex"] = item.RowIndex;
  123. row_new["PayeeCode"] = item.CustomerNO;
  124. row_new["Payee"] = item.PayeeName;
  125. row_new["FeeItemName"] = payeeinfo["FeeItemName"];
  126. row_new["BillNO"] = payeeinfo["BillNO"];
  127. row_new["PrjCode"] = (payeeinfo["PrjCode"] ?? "") + " " + (payeeinfo["PrjName"] ?? "");
  128. row_new["Currency"] = sCurrency;
  129. row_new["Amount"] = sCurrency == "NTD" ? $"{iAmount:N0}" : $"{iAmount:N2}";
  130. row_new["PaymentWay"] = sPaymentWay == "A" ? "現金" : sPaymentWay == "B" ? "預支現金" : sPaymentWay == "C" ? "支票" : sPaymentWay == "D" ? "轉賬/匯款" : "依公司規定";
  131. row_new["PaymentTime"] = item.PaymentTime == null ? "" : Convert.ToDateTime(item.PaymentTime).ToString("yyyy/MM/dd");
  132. dt_new.Rows.Add(row_new);
  133. }
  134. }
  135. var dicAlain = ExcelService.GetExportAlain(oHeader, new string[] { "Currency", "PayeeCode", "BillNO" }, "Amount");
  136. var bOk = new ExcelService().CreateExcelByTb(dt_new, out string sPath, oHeader, dicAlain, listMerge, sFileName);
  137. rm.DATA.Add(BLWording.REL, sPath);
  138. }
  139. else
  140. {
  141. rm.DATA.Add(BLWording.REL, pml);
  142. }
  143. } while (false);
  144. }
  145. catch (Exception ex)
  146. {
  147. sMsg = Util.GetLastExceptionMsg(ex);
  148. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.InvoiceApplyForCustomer_QryService", "", "QueryPage(請款單(廠商)分頁查詢)", "", "", "");
  149. }
  150. finally
  151. {
  152. if (null != sMsg)
  153. {
  154. rm = new ErrorResponseMessage(sMsg, i_crm);
  155. }
  156. }
  157. return rm;
  158. }
  159. #endregion 請款單(廠商)分頁查詢
  160. #region 請款單(廠商)單筆查詢
  161. /// <summary>
  162. /// 請款單(廠商)單筆查詢
  163. /// </summary>
  164. /// <param name="i_crm"></param>
  165. /// <returns></returns>
  166. public ResponseMessage QueryOne(RequestMessage i_crm)
  167. {
  168. ResponseMessage rm = null;
  169. string sMsg = null;
  170. var db = SugarBase.GetIntance();
  171. try
  172. {
  173. do
  174. {
  175. var sId = _fetchString(i_crm, @"Guid");
  176. var oEntity = db.Queryable<OTB_EIP_InvoiceApplyInfo, OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Members, OTB_CRM_Customers>
  177. ((t1, t2, t3, t4, t5) =>
  178. new object[] {
  179. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Applicant == t2.MemberID,
  180. JoinType.Inner, t2.OrgID == t3.OrgID && t2.DepartmentID == t3.DepartmentID,
  181. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Handle_Person == t4.MemberID,
  182. JoinType.Left, t1.OrgID == t5.OrgID && t1.Payee == t5.guid
  183. }
  184. )
  185. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID && t1.Guid == sId)
  186. .Select((t1, t2, t3, t4, t5) => new View_EIP_InvoiceApplyInfo
  187. {
  188. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  189. ApplicantName = t2.MemberName,
  190. Handle_PersonName = t4.MemberName,
  191. CustomerNO = t5.CustomerNO,
  192. DeptName = t3.DepartmentName
  193. }).Single();
  194. if (!string.IsNullOrEmpty(oEntity.RelationId))
  195. {
  196. var oRelation = db.Queryable<OTB_EIP_InvoiceApplyInfo>().Single(x => x.OrgID == i_crm.ORIGID && x.Guid == oEntity.RelationId);
  197. if (oRelation != null)
  198. {
  199. oEntity.ExFeild1 = oRelation.KeyNote;
  200. }
  201. }
  202. rm = new SuccessResponseMessage(null, i_crm);
  203. rm.DATA.Add(BLWording.REL, oEntity);
  204. } while (false);
  205. }
  206. catch (Exception ex)
  207. {
  208. sMsg = Util.GetLastExceptionMsg(ex);
  209. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.InvoiceApplyForCustomer_QryService", "", "QueryOne(請款單(廠商)單筆查詢)", "", "", "");
  210. }
  211. finally
  212. {
  213. if (null != sMsg)
  214. {
  215. rm = new ErrorResponseMessage(sMsg, i_crm);
  216. }
  217. }
  218. return rm;
  219. }
  220. #endregion 請款單(廠商)單筆查詢
  221. }
  222. }