rpt_data_analysis.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const changeSort = {
  10. name: '变更令排序',
  11. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  12. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  13. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  14. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  15. /**
  16. *
  17. * @param ctx - context常量
  18. * @param data - 全部数据源{Array}
  19. * @param fieldsKey - 计算字段
  20. */
  21. fun: function(ctx, data, fieldsKey) {
  22. if (!data.change) return;
  23. // 变更令排序
  24. data.change.sort(function (a, b) {
  25. return a.code.localeCompare(b.code);
  26. });
  27. if (data.change_audit_list) {
  28. data.change_audit_list.sort(function (a, b) {
  29. const aCIndex = data.change.findIndex(function (c) {
  30. return c.cid === a.cid;
  31. });
  32. const bCIndex = data.change.findIndex(function (c) {
  33. return c.cid === b.cid;
  34. });
  35. return aCIndex === bCIndex
  36. ? ctx.helper.compareCode(a.code, b.code)
  37. : aCIndex - bCIndex;
  38. });
  39. }
  40. },
  41. };
  42. const gatherGcl = {
  43. name: '汇总工程量清单',
  44. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  45. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  46. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  47. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  48. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  49. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  50. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  51. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  52. fun: function (ctx, data, fieldsKey) {
  53. const gatherFields = function(gcl, data, fields) {
  54. for (const f of fields) {
  55. if (data[f]) {
  56. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  57. }
  58. }
  59. };
  60. if (!data.mem_stage_bills) return;
  61. const fields = ctx.helper._.map(fieldsKey, 'field');
  62. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  63. for (const nf of needFields) {
  64. if (fields.indexOf(nf) === -1) return;
  65. }
  66. const gatherOther = fields.indexOf('chapter') >= 0;
  67. ctx.helper._.pull(data.mem_stage_bills, 'is_leaf');
  68. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  69. for (const b of data.mem_stage_bills) {
  70. if (b.b_code && b.b_code !== '') {
  71. let gcl = gclBills.find(function (g) {
  72. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  73. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  74. });
  75. if (!gcl) {
  76. gcl = {
  77. b_code: b.b_code, name: b.name, unit: b.unit,
  78. unit_price: b.unit_price,
  79. qc_bgl_code: [], chapter: b.chapter,
  80. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  81. };
  82. gclBills.push(gcl);
  83. }
  84. gatherFields(gcl, b, [
  85. 'deal_qty', 'deal_tp',
  86. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  87. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  88. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  89. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  90. 'final_tp'
  91. ]);
  92. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  93. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  94. }
  95. } else if (gatherOther) {
  96. gatherFields(other, b, [
  97. 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price',
  98. 'contract_tp', 'qc_tp', 'gather_tp',
  99. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  100. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  101. 'final_tp'
  102. ]);
  103. }
  104. }
  105. if (gatherOther) gclBills.push(other);
  106. for (const g of gclBills) {
  107. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  108. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  109. }
  110. data.mem_stage_bills = gclBills;
  111. }
  112. };
  113. const sortGcl = {
  114. name: '工程量清单排序',
  115. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  116. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  117. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  118. '特别的,如有"未计入清单章节项": \n' +
  119. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  120. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  121. fun: function (ctx, data, fieldsKey) {
  122. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  123. const code = fieldsKey[0].field;
  124. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  125. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  126. const sortData = data[fieldsKey[0].table];
  127. if (!sortData) return;
  128. sortData.sort(function (a, b) {
  129. if (chapter !== '') {
  130. if (a[name] === '未计入清单章节项') {
  131. return b[chapter] === '100' ? 1 : -1;
  132. } else if (b[name] === '未计入清单章节项') {
  133. return a[chapter] === '100' ? -1 : 1
  134. } else {
  135. return ctx.helper.compareCode(a[code], b[code]);
  136. }
  137. } else {
  138. return ctx.helper.compareCode(a[code], b[code]);
  139. }
  140. });
  141. }
  142. };
  143. const gatherChapter = {
  144. name: '汇总章级数据',
  145. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  146. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  147. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  148. '注意事项:\n' +
  149. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  150. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  151. '示例:\n' +
  152. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  153. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  154. '结果:\n' +
  155. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  156. fun: function (ctx, data, fieldsKey) {
  157. if (!data.tender_info || !data.tender_info.chapter) return;
  158. if (!fieldsKey && fieldsKey.length < 0) return;
  159. const getCalcChapter = function (chapter) {
  160. const gclChapter = [], otherChapter = [];
  161. let serialNo = 1;
  162. for (const c of chapter) {
  163. const cc = { code: c.code, name: c.name, cType: 1 };
  164. cc.serialNo = serialNo++;
  165. cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  166. gclChapter.push(cc);
  167. }
  168. gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo++, });
  169. otherChapter.push({ name: '清单小计(A)', cType: 11, serialNo: serialNo++ });
  170. otherChapter.push({ name: '非清单项费用(B)', cType: 31, serialNo: serialNo++ });
  171. otherChapter.push({ name: '合计(C=A+B)', cType: 41, serialNo: serialNo });
  172. return [gclChapter, otherChapter];
  173. };
  174. const getGclChapter = function (chapter, data) {
  175. for (const c of chapter) {
  176. if (c.filter) {
  177. const reg = new RegExp(c.filter);
  178. if (reg.test(data.b_code)) {
  179. return c;
  180. }
  181. } else {
  182. return c;
  183. }
  184. }
  185. };
  186. const gatherData = function(chapter, data) {
  187. for (const f in data) {
  188. if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) {
  189. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  190. }
  191. }
  192. };
  193. const [gclChapter, otherChapter] = getCalcChapter(data.tender_info.chapter);
  194. const fields = ctx.helper._.map(fieldsKey, 'field');
  195. const needFields = ['b_code', 'is_leaf'];
  196. for (const nf of needFields) {
  197. if (fields.indexOf(nf) === -1) return;
  198. }
  199. const sourceData = data[fieldsKey[0].table];
  200. if (!sourceData) return;
  201. for (const d of sourceData) {
  202. if (!d.is_leaf) continue;
  203. for (const c of otherChapter) {
  204. if (c.cType === 41) {
  205. gatherData(c, d);
  206. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  207. gatherData(c, d, fields);
  208. } else if (c.cType === 11 && (d.b_code)) {
  209. gatherData(c, d, fields);
  210. }
  211. }
  212. if (d.b_code) {
  213. const c = getGclChapter(gclChapter, d);
  214. gatherData(c, d, fields);
  215. }
  216. }
  217. data[fieldsKey[0].table] = gclChapter.concat(otherChapter);
  218. },
  219. };
  220. const analysisObj = {
  221. changeSort,
  222. gatherGcl,
  223. sortGcl,
  224. gatherChapter,
  225. };
  226. const analysisDefine = (function (obj) {
  227. const result = [];
  228. for (const o in obj) {
  229. const analysis = obj[o];
  230. if (analysis.name && analysis.hint && analysis.fun) {
  231. result.push({
  232. name: analysis.name,
  233. key: o,
  234. hint: analysis.hint,
  235. })
  236. }
  237. }
  238. return result;
  239. })(analysisObj);
  240. module.exports = {
  241. analysisObj,
  242. analysisDefine
  243. };