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.

323 lines
16 KiB

  1. using CounsellorBL.BLStructure;
  2. using CounsellorBL.Helper;
  3. using MonumentDefine;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using NPOI.HPSF;
  7. using OT.COM.ArsenalDB;
  8. using OT.COM.SignalerMessage;
  9. using SoldierData.EnterprizeV4;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Linq;
  14. using Util = OT.COM.LogisticsUtil.Util;
  15. namespace CounsellorBL.ORD
  16. {
  17. public class GroupReceiveReportService : SingleDataTableTemplate<tb_grp_group>
  18. {
  19. public GroupReceiveReportService()
  20. {
  21. dgReadCommandGenerator = readCommandGenerator;
  22. dgReadCommandPostDataHandler = readCommandPostDataHandler;
  23. }
  24. public new CResponseMessage Read(CRequestMessage i_crmInput) => base.Read(i_crmInput);
  25. protected string readCommandGenerator(CRequestMessage i_crmInput, JArray i_jaData, tb_sys_session i_sSessionUser, out Command o_c,
  26. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  27. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  28. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  29. {
  30. string sMsg;
  31. Command cRes = null;
  32. try
  33. {
  34. do
  35. {
  36. var lsBranch = ProjectHelper.GetUserGroup(i_crmInput);
  37. var authBranch = ProjectHelper.GetUserBranch(i_crmInput);
  38. Dictionary<string, object> dicCondition = GetQueryMasterFirstWhereData(i_crmInput); // 取得condition
  39. QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
  40. QueryJsonElement qjePurchase = lBlocks.GetInst();
  41. qjePurchase.table = tb_ord_purchase.TABLENAME;
  42. qjePurchase.aliascols = new Dictionary<string, List<string>>
  43. {
  44. { tb_ord_purchase.CN_AMOUNT, new List<string>() { tb_ord_purchase.CN_AMOUNT } },
  45. {"' '", new List<string>() { tb_ord_purchase.CN_UID } },
  46. };
  47. List<WhereNode> lwWhereData = new List<WhereNode>();
  48. if (dicCondition.ContainsKey("create_date_start"))
  49. {
  50. if (DateTime.Today.Day > 5)
  51. {
  52. var thisMonth = DateTime.Today.AddDays(-(DateTime.Today.Day - 1));
  53. if (Convert.ToDateTime(dicCondition["create_date_start"]) >= thisMonth)
  54. {
  55. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), dicCondition["create_date_start"]));
  56. }
  57. else
  58. {
  59. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), thisMonth.ToString("yyyy-MM-dd")));
  60. }
  61. }
  62. else
  63. {
  64. var lastMonth = DateTime.Today.AddMonths(-1).AddDays(-(DateTime.Today.Day - 1));
  65. if (Convert.ToDateTime(dicCondition["create_date_start"]) >= lastMonth)
  66. {
  67. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), dicCondition["create_date_start"]));
  68. }
  69. else
  70. {
  71. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), lastMonth.ToString("yyyy-MM-dd")));
  72. }
  73. }
  74. }
  75. if (dicCondition.ContainsKey("create_date_end"))
  76. {
  77. if (!dicCondition.ContainsKey("create_date_start"))
  78. {
  79. if (DateTime.Today.Day > 5)
  80. {
  81. var thisMonth = DateTime.Today.AddDays(-(DateTime.Today.Day - 1));
  82. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), thisMonth.ToString("yyyy-MM-dd")));
  83. }
  84. else
  85. {
  86. var lastMonth = DateTime.Today.AddMonths(-1).AddDays(-(DateTime.Today.Day - 1));
  87. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), lastMonth.ToString("yyyy-MM-dd")));
  88. }
  89. }
  90. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_LT, typeof(tb_ord_purchase), Convert.ToDateTime(dicCondition["create_date_end"]).AddDays(1).ToString("yyyy-MM-dd")));
  91. }
  92. if (!dicCondition.Any())
  93. {
  94. var today = DateTime.Today;
  95. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_GTEQ, typeof(tb_ord_purchase), today));
  96. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_CREATE_DATE, WhereNode.EColumnOperation.EOT_LT, typeof(tb_ord_purchase), today.AddDays(1).ToString("yyyy-MM-dd")));
  97. }
  98. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_ord_purchase), (int)Enums.Flag.Enable));
  99. lwWhereData.Add(new WhereNode(tb_ord_purchase.CN_RECEIVE_BRANCH_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_ord_purchase), authBranch.ToArray()));
  100. if (lwWhereData.Any())
  101. {
  102. qjePurchase.wherecols = new WhereNode(WhereNode.ENodeOperation.ENO_AND, lwWhereData.ToArray());
  103. }
  104. QueryJsonElement qjeBranch = lBlocks.GetInst();
  105. qjeBranch.table = tb_grp_branch.TABLENAME;
  106. qjeBranch.jointype = QueryJsonElement.JOIN;
  107. qjeBranch.jointable = qjePurchase;
  108. qjeBranch.joincols = new Dictionary<string, string>() {
  109. { tb_grp_branch.CN_UID,tb_ord_purchase.CN_RECEIVE_BRANCH_UID }};
  110. qjeBranch.aliascols = new Dictionary<string, List<string>>
  111. {
  112. { tb_grp_branch.CN_UID, new List<string>() { tb_ord_purchase_detail.CN_BRANCH_UID } }
  113. };
  114. QueryJsonElement qjeGroup = lBlocks.GetInst();
  115. qjeGroup.table = tb_grp_group.TABLENAME;
  116. qjeGroup.jointype = QueryJsonElement.JOIN;
  117. qjeGroup.jointable = qjeBranch;
  118. qjeGroup.joincols = new Dictionary<string, string>() {
  119. { tb_grp_group.CN_UID,tb_grp_branch.CN_GROUP_UID }};
  120. if (dicCondition.ContainsKey(tb_grp_article.CN_GROUP_UID))
  121. {
  122. qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_group), dicCondition[tb_grp_article.CN_GROUP_UID]);
  123. }
  124. else
  125. {
  126. qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_UID, WhereNode.EColumnOperation.EOT_IN, typeof(tb_grp_group), lsBranch.ToArray());
  127. }
  128. qjeGroup.aliascols = new Dictionary<string, List<string>>
  129. {
  130. { tb_grp_group.CN_UID, new List<string>() { tb_grp_branch.CN_GROUP_UID } }
  131. };
  132. QueryJsonElement qjeEmp = lBlocks.GetInst();
  133. qjeEmp.table = tb_hr_employee.TABLENAME;
  134. qjeEmp.jointype = QueryJsonElement.JOIN;
  135. qjeEmp.jointable = qjePurchase;
  136. qjeEmp.joincols = new Dictionary<string, string>() {
  137. { tb_hr_employee.CN_UID,tb_ord_purchase.CN_CREATE_USER_UID }};
  138. qjeEmp.aliascols = new Dictionary<string, List<string>>
  139. {
  140. { tb_hr_employee.CN_NAME, new List<string>() { "payee" } }
  141. };
  142. QueryJsonElement qjePurchaseDetail = lBlocks.GetInst();
  143. qjePurchaseDetail.table = tb_ord_purchase_detail.TABLENAME;
  144. qjePurchaseDetail.jointype = QueryJsonElement.JOIN;
  145. qjePurchaseDetail.jointable = qjePurchase;
  146. qjePurchaseDetail.joincols = new Dictionary<string, string>() {
  147. { tb_ord_purchase_detail.CN_PURCHASE_UID,tb_ord_purchase.CN_UID }};
  148. qjePurchaseDetail.aliascols = new Dictionary<string, List<string>>
  149. {
  150. { tb_ord_purchase_detail.CN_PURCHASE_UID, new List<string>() { "purchase_uid" } },
  151. { tb_ord_purchase_detail.CN_QTY, new List<string>() { tb_ord_purchase_detail.CN_QTY } },
  152. };
  153. QueryJsonElement qjeArticle2Product = lBlocks.GetInst();
  154. qjeArticle2Product.table = tb_prd_article2product.TABLENAME;
  155. qjeArticle2Product.jointype = QueryJsonElement.JOIN;
  156. qjeArticle2Product.jointable = qjePurchaseDetail;
  157. qjeArticle2Product.joincols = new Dictionary<string, string>() {
  158. { tb_prd_article2product.CN_UID,tb_ord_purchase_detail.CN_ARTICLE2PRODUCT_UID }};
  159. qjeArticle2Product.displaycols = new List<string>()
  160. {
  161. tb_prd_article2product.CN_COST_PRICE,
  162. tb_prd_article2product.CN_PRD_UID,
  163. };
  164. lBlocks.Add(qjePurchase);
  165. lBlocks.Add(qjeBranch);
  166. lBlocks.Add(qjeGroup);
  167. lBlocks.Add(qjeEmp);
  168. lBlocks.Add(qjePurchaseDetail);
  169. lBlocks.Add(qjeArticle2Product);
  170. sMsg = MakeSelectJoinByBlocks(lBlocks, out cRes);
  171. if (sMsg != null)
  172. {
  173. break;
  174. }
  175. }
  176. while (false);
  177. }
  178. catch (Exception ex)
  179. {
  180. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  181. sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  182. #if DEBUG
  183. System.Diagnostics.Debug.WriteLine(sMsg);
  184. #endif
  185. }
  186. o_c = cRes;
  187. return sMsg;
  188. }
  189. protected string readCommandPostDataHandler(CRequestMessage i_crmInput, ArsenalInterface i_aiArsenal, Command i_cCmd, JArray i_jaData, tb_sys_session i_sSessionUser, out object o_oReault,
  190. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  191. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  192. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  193. {
  194. string sMsg = null;
  195. object oResultData = null;
  196. try
  197. {
  198. do
  199. {
  200. //QueryDataSet qds = i_aiArsenal.RunQueryDataSet(i_cCmd);
  201. ArsenalInterface ai = ArsenalDBMgr.GetInst(i_cCmd);
  202. List<ReportData> lRes = ai.RunQueryList<ReportData>(i_cCmd);
  203. if (!i_cCmd.IsSuccess)
  204. {
  205. sMsg = i_cCmd.LastErrorCode;
  206. break;
  207. }
  208. //找出detail中的成本
  209. var cost_result = lRes.GroupBy(x => new
  210. {
  211. x.prd_uid,
  212. x.payee,
  213. x.group_uid,
  214. x.branch_uid,
  215. x.purchase_uid,
  216. x.amount,
  217. x.cost_price,
  218. x.qty,
  219. })
  220. .Select(x => new ReportData
  221. {
  222. payee = x.Key.payee,
  223. group_uid = x.Key.group_uid,
  224. branch_uid = x.Key.branch_uid,
  225. prd_uid = x.Key.prd_uid,
  226. amount = x.Key.amount,
  227. purchase_uid =x.Key.purchase_uid,
  228. cost_price = x.Key.cost_price,
  229. qty = x.Key.qty,
  230. calCost = x.Sum(x => x.cost_price * x.qty),
  231. });
  232. // 透過group by 加總成本
  233. var report = cost_result.GroupBy(x => new
  234. {
  235. x.payee,
  236. x.group_uid,
  237. x.branch_uid,
  238. x.amount,
  239. x.purchase_uid
  240. }).Select(x => new ReportData
  241. {
  242. payee = x.Key.payee,
  243. group_uid = x.Key.group_uid,
  244. branch_uid = x.Key.branch_uid,
  245. amount = x.Key.amount,
  246. calCost = x.Sum(x => x.calCost),
  247. }).GroupBy(x => new //再group by一次依照收款人分出資料
  248. {
  249. x.payee,
  250. x.group_uid,
  251. x.branch_uid,
  252. }).Select(x => new ReportData
  253. {
  254. payee = x.Key.payee,
  255. group_uid = x.Key.group_uid,
  256. branch_uid = x.Key.branch_uid,
  257. amount = x.Sum(c=>c.amount),
  258. calCost = x.Sum(x => x.calCost),
  259. });
  260. // 最後用SELECT 算出獲利傳回前端
  261. var data = report.Select(x => new ReportData
  262. {
  263. payee = x.payee,
  264. group_uid = x.group_uid,
  265. branch_uid = x.branch_uid,
  266. amount = x.amount,
  267. profit = x.amount - x.calCost,
  268. uid = Guid.NewGuid().ToString()
  269. }).OrderBy(x => x.group_uid).ThenBy(x => x.branch_uid) ;
  270. oResultData = data;
  271. }
  272. while (false);
  273. }
  274. catch (Exception ex)
  275. {
  276. LogHelper.DBLog(Util.GetLastExceptionMsg(ex), i_nCodeLine, i_sMemberName, i_sSourcePath);
  277. sMsg = $"{nameof(readCommandGenerator)} unknwon exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  278. #if DEBUG
  279. System.Diagnostics.Debug.WriteLine(sMsg);
  280. #endif
  281. }
  282. o_oReault = oResultData;
  283. return sMsg;
  284. }
  285. private class ReportData
  286. {
  287. public int amount { get; set; }
  288. public string uid { get; set; }
  289. public string branch_uid { get; set; }
  290. public string group_uid { get; set; }
  291. public string prd_uid { get; set; }
  292. public string payee { get; set; }
  293. public string purchase_uid { get; set; }
  294. public int qty { get; set; }
  295. public int calCost { get; set; }
  296. public int cost_price { get; set; }
  297. public int profit { get; set; }
  298. }
  299. }
  300. }