rpt_data_analysis.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const math = require('mathjs');
  10. const standard = require('../const/standard');
  11. // 通用方法
  12. const rdaUtils = {
  13. orderCalc: function (ctx, data, fields) {
  14. const orderMatch = new RegExp('o[0-9]+', 'igm');
  15. for (const c of data) {
  16. if (c.order_calc && c.order_calc !== '') {
  17. const matchs = c.order_calc.match(orderMatch);
  18. const calcMatch = [];
  19. for (const m of matchs) {
  20. const order = m.substring(1, m.length);
  21. const orderData = data.find(function (x) {return (x.order + '') === order});
  22. if (orderData) {
  23. calcMatch.push({match: m, value: orderData})
  24. }
  25. }
  26. for (const f of fields) {
  27. let expr = c.order_calc;
  28. for (const m of calcMatch) {
  29. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  30. }
  31. try {
  32. c[f] = ctx.helper.round(math.eval(expr), 6);
  33. } catch(err) {
  34. }
  35. }
  36. }
  37. }
  38. }
  39. };
  40. // 可提供的数据处理方法
  41. const changeSort = {
  42. name: '变更令排序',
  43. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  44. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  45. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  46. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  47. intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',
  48. /**
  49. *
  50. * @param ctx - context常量
  51. * @param data - 全部数据源{Array}
  52. * @param fieldsKey - 计算字段
  53. * @param options - 计算设置
  54. */
  55. fun: function(ctx, data, fieldsKey, options) {
  56. if (!data.change) return;
  57. // 变更令排序
  58. data.change.sort(function (a, b) {
  59. return a.code.localeCompare(b.code);
  60. });
  61. if (data.change_audit_list) {
  62. data.change_audit_list.sort(function (a, b) {
  63. const aCIndex = data.change.findIndex(function (c) {
  64. return c.cid === a.cid;
  65. });
  66. const bCIndex = data.change.findIndex(function (c) {
  67. return c.cid === b.cid;
  68. });
  69. return aCIndex === bCIndex
  70. ? ctx.helper.compareCode(a.code, b.code)
  71. : aCIndex - bCIndex;
  72. });
  73. }
  74. },
  75. };
  76. const gatherGcl = {
  77. name: '汇总工程量清单',
  78. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  79. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  80. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  81. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  82. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  83. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  84. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  85. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  86. intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',
  87. fun: function (ctx, data, fieldsKey, options) {
  88. const gatherFields = function(gcl, data, fields) {
  89. for (const f of fields) {
  90. if (data[f]) {
  91. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  92. }
  93. }
  94. };
  95. if (!data.mem_stage_bills) return;
  96. const fields = ctx.helper._.map(fieldsKey, 'field');
  97. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  98. for (const nf of needFields) {
  99. if (fields.indexOf(nf) === -1) return;
  100. }
  101. const gatherOther = fields.indexOf('chapter') >= 0;
  102. ctx.helper._.pull(data.mem_stage_bills, 'is_leaf');
  103. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  104. for (const b of data.mem_stage_bills) {
  105. if (b.b_code && b.b_code !== '') {
  106. let gcl = gclBills.find(function (g) {
  107. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  108. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  109. });
  110. if (!gcl) {
  111. gcl = {
  112. b_code: b.b_code, name: b.name, unit: b.unit,
  113. unit_price: b.unit_price,
  114. qc_bgl_code: [], chapter: b.chapter,
  115. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  116. };
  117. gclBills.push(gcl);
  118. }
  119. gatherFields(gcl, b, [
  120. 'deal_qty', 'deal_tp',
  121. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  122. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  123. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  124. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  125. 'final_tp'
  126. ]);
  127. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  128. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  129. }
  130. } else if (gatherOther) {
  131. gatherFields(other, b, [
  132. 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price',
  133. 'contract_tp', 'qc_tp', 'gather_tp',
  134. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  135. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  136. 'final_tp'
  137. ]);
  138. }
  139. }
  140. if (gatherOther) gclBills.push(other);
  141. for (const g of gclBills) {
  142. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  143. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  144. }
  145. data.mem_stage_bills = gclBills;
  146. }
  147. };
  148. const sortGcl = {
  149. name: '工程量清单排序',
  150. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  151. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  152. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  153. '特别的,如有"未计入清单章节项": \n' +
  154. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  155. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  156. intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',
  157. fun: function (ctx, data, fieldsKey, options) {
  158. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  159. const code = fieldsKey[0].field;
  160. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  161. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  162. const sortData = data[fieldsKey[0].table];
  163. if (!sortData) return;
  164. sortData.sort(function (a, b) {
  165. if (chapter !== '') {
  166. if (a[name] === '未计入清单章节项') {
  167. return b[chapter] === '100' ? 1 : -1;
  168. } else if (b[name] === '未计入清单章节项') {
  169. return a[chapter] === '100' ? -1 : 1
  170. } else {
  171. return ctx.helper.compareCode(a[code], b[code]);
  172. }
  173. } else {
  174. return ctx.helper.compareCode(a[code], b[code]);
  175. }
  176. });
  177. }
  178. };
  179. const gatherChapter = {
  180. name: '汇总章级数据',
  181. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  182. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  183. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  184. '注意事项:\n' +
  185. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  186. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  187. '示例:\n' +
  188. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  189. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  190. '结果:\n' +
  191. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  192. intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',
  193. defaultSetting: {
  194. count: 9,
  195. unChapter: {
  196. name: '未计入清单章节合计',
  197. order: 1,
  198. },
  199. gclSum: {
  200. name: '清单小计',
  201. order: 2,
  202. },
  203. unGcl: {
  204. name: '非清单项费用',
  205. order: 3,
  206. },
  207. sum: {
  208. name: '合计',
  209. order: 4,
  210. }
  211. },
  212. customSetting1: {
  213. count: 7,
  214. other: 1,
  215. gclSum: {
  216. name: '第100章至700章清单合计',
  217. order: 1,
  218. },
  219. custom: [
  220. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2},
  221. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  222. {name: '计日工合计', node_type: '计日工', order: 4},
  223. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', part: '暂定金额'}]},
  224. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  225. ],
  226. rela: [
  227. {
  228. table: 'deal_bills', key: 'code',
  229. fields: {source: 'total_price', target: 'ex_value1'},
  230. },
  231. {
  232. table: 'mem_change_bills', key: 'code',
  233. fields: {source: '', target: 'ex_value2'},
  234. }
  235. ]
  236. },
  237. _getCalcChapter: function (chapter, options) {
  238. const gclChapter = [], otherChapter = [], customChapter = [];
  239. let serialNo = 1;
  240. for (const c of chapter) {
  241. const cc = { code: c.code, name: c.name, cType: 1 };
  242. cc.serialNo = serialNo++;
  243. cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  244. gclChapter.push(cc);
  245. if (options.activeFields) {
  246. cc.active = cc.serialNo > options.count;
  247. } else {
  248. if (serialNo > options.count) break;
  249. }
  250. }
  251. if (options.unChapter) {
  252. gclChapter.push({ name: options.unChapter.name, cType: 21, serialNo: serialNo + options.unChapter.order, order: options.unChapter.order });
  253. }
  254. if (options.gclSum) {
  255. otherChapter.push({ name: options.gclSum.name, cType: 11, serialNo: serialNo + options.gclSum.order, order: options.gclSum.order });
  256. }
  257. if (options.unGcl) {
  258. otherChapter.push({ name: options.unGcl.name, cType: 31, serialNo: serialNo + options.unGcl.order, order: options.unGcl.order });
  259. }
  260. if (options.sum) {
  261. otherChapter.push({ name: options.sum.name , cType: 41, serialNo: serialNo + options.sum.order, order: options.sum.order });
  262. }
  263. if (options.custom && options.custom instanceof Array) {
  264. for (const c of options.custom) {
  265. const cc = {
  266. name: c.name, serialNo: serialNo + c.order,
  267. order_calc: c.order_calc,
  268. cType: 5, order: c.order,
  269. };
  270. if (c.match) {
  271. cc.match = JSON.parse(JSON.stringify(c.match));
  272. for (const m of cc.match) {
  273. if (m.node_type && m.node_type !== '') {
  274. const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});
  275. m.node_type = nodeType.value;
  276. }
  277. }
  278. }
  279. customChapter.push(cc);
  280. }
  281. }
  282. return [gclChapter, otherChapter, customChapter];
  283. },
  284. _getGclChapter: function (chapter, data, field) {
  285. for (const c of chapter) {
  286. if (c.filter) {
  287. const reg = new RegExp(c.filter);
  288. if (reg.test(data[field])) {
  289. return c;
  290. }
  291. } else {
  292. return c;
  293. }
  294. }
  295. },
  296. _checkFilter: function (fullPath, filter) {
  297. for (const f of filter) {
  298. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  299. }
  300. return false;
  301. },
  302. _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {
  303. if (!rela) return;
  304. const gatherRelaFields = function (chapter, source, field) {
  305. const fields = field instanceof Array ? field : [field];
  306. for (const f of fields) {
  307. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  308. }
  309. };
  310. const relaBills = rela instanceof Array ? rela : [rela];
  311. for (const rb of relaBills) {
  312. if (!rb.table) continue;
  313. const relaData = data[rb.table];
  314. if (!relaData) continue;
  315. for (const rd of relaData) {
  316. for (const c of otherChapter) {
  317. if (c.cType === 41) {
  318. gatherRelaFields(c, rd, rb.fields);
  319. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  320. gatherRelaFields(c, rd, rb.fields);
  321. } else if (c.cType === 11 && (rd[rb.key])) {
  322. gatherRelaFields(c, rd, rb.fields);
  323. }
  324. }
  325. if (rd[rb.key]) {
  326. const c = this._getGclChapter(gclChapter, rd, rb.key);
  327. gatherRelaFields(c, rd, rb.fields);
  328. }
  329. }
  330. }
  331. },
  332. _orderCalc: function (ctx, chapter, fields) {
  333. const orderMatch = new RegExp('o[0-9]+', 'igm');
  334. for (const c of chapter) {
  335. if (c.order_calc && c.order_calc !== '') {
  336. const matchs = c.order_calc.match(orderMatch);
  337. const calcMatch = [];
  338. for (const m of matchs) {
  339. const order = m.substring(1, m.length);
  340. const orderChapter = chapter.find(function (x) {return x.order == order});
  341. if (orderChapter) {
  342. calcMatch.push({match: m, value: orderChapter})
  343. }
  344. }
  345. for (const f of fields) {
  346. let expr = c.order_calc;
  347. for (const m of calcMatch) {
  348. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  349. }
  350. try {
  351. c[f] = ctx.helper.round(math.eval(expr), 6);
  352. } catch(err) {
  353. }
  354. }
  355. }
  356. }
  357. },
  358. _checkMatch: function (match, d) {
  359. for (const m of match) {
  360. if (m.node_type) {
  361. if (m.node_type === d.node_type) return true;
  362. } else if (m.field) {
  363. const value = d[m.field];
  364. if (m.part) {
  365. if (value.indexOf(m.part) >= 0) return true;
  366. }
  367. }
  368. }
  369. return false;
  370. },
  371. fun: function (ctx, data, fieldsKey, options) {
  372. if (!data.tender_info || !data.tender_info.chapter) return;
  373. if (!fieldsKey && fieldsKey.length < 0) return;
  374. const calcFields = [];
  375. const gatherData = function (chapter, data) {
  376. if (!chapter) return;
  377. for (const f in data) {
  378. if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) {
  379. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  380. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  381. }
  382. }
  383. };
  384. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  385. const fields = ctx.helper._.map(fieldsKey, 'field');
  386. const needFields = ['b_code', 'is_leaf'];
  387. for (const nf of needFields) {
  388. if (fields.indexOf(nf) === -1) return;
  389. }
  390. const sourceData = data[fieldsKey[0].table];
  391. if (!sourceData) return;
  392. const filter = [];
  393. for (const d of sourceData) {
  394. for (const c of customChapter) {
  395. if (c.match && this._checkMatch(c.match, d)) {
  396. gatherData(c, d);
  397. filter.push(d.full_path);
  398. }
  399. }
  400. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  401. for (const c of otherChapter) {
  402. if (c.cType === 41) {
  403. gatherData(c, d);
  404. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  405. gatherData(c, d);
  406. } else if (c.cType === 11 && (d.b_code)) {
  407. gatherData(c, d);
  408. }
  409. }
  410. if (d.b_code) {
  411. const c = this._getGclChapter(gclChapter, d, 'b_code');
  412. gatherData(c, d);
  413. }
  414. }
  415. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  416. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  417. this._orderCalc(ctx, chapter, calcFields);
  418. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  419. data[fieldsKey[0].table] = chapter.filter(function (x) {
  420. if (x.active) {
  421. for (const f of options.activeFields) {
  422. if (!ctx.helper.checkZero(x[f])) return true;
  423. }
  424. return false;
  425. } else {
  426. return true;
  427. }
  428. });
  429. //console.log(data[fieldsKey[0].table]);
  430. },
  431. };
  432. const join = {
  433. name: "连接两张数据表",
  434. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  435. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  436. defaultSetting: {
  437. main: 'mem_stage_bills',
  438. sub: 'deal_bills',
  439. keyFields: [
  440. {main: 'b_code', sub: 'code', type: 'string'},
  441. {main: 'name', sub: 'name',type: 'string'},
  442. {main: 'unit', sub: 'unit',type: 'string'},
  443. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  444. ],
  445. importFields: [
  446. {main: 'ex_value1', sub: 'quantity', type: 'number'},
  447. {main: 'ex_value2', sub: 'total_price', type: 'number'}
  448. ],
  449. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  450. },
  451. fun: function (ctx, data, fields, options) {
  452. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  453. const main = data[options.main];
  454. const sub = data[options.sub];
  455. if (!main || !sub) return;
  456. const _ = ctx.helper._, result = _.cloneDeep(main);
  457. for (const r of result) {
  458. r._join_tag = 'main';
  459. for (const i of options.importFields) {
  460. r[i.main] = null;
  461. }
  462. }
  463. for (const s of sub) {
  464. let r = result.find(function (x) {
  465. for (const k of options.keyFields) {
  466. switch (k.type) {
  467. case 'string':
  468. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  469. break;
  470. case 'number':
  471. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  472. break;
  473. }
  474. }
  475. return true;
  476. });
  477. if (r && r._join_tag === 'main') {
  478. r._join_tag = 'both';
  479. }
  480. if (!r) {
  481. r = {_join_tag: 'sub'};
  482. for (const k of options.keyFields) {
  483. r[k.main] = s[k.sub];
  484. }
  485. result.push(r);
  486. }
  487. for (const i of options.importFields) {
  488. r[i.main] = s[i.sub];
  489. }
  490. }
  491. switch (options.joinType) {
  492. case 'main':
  493. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  494. break;
  495. case 'sub':
  496. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  497. break;
  498. case 'inner':
  499. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  500. break;
  501. case 'outer':
  502. data[options.main] = result;
  503. break;
  504. }
  505. }
  506. };
  507. const getChapterCode = {
  508. name: '获取章级编号',
  509. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  510. hint: '',
  511. defaultSetting: {
  512. table: 'mem_stage_bills',
  513. b_code: 'b_code',
  514. chapter: 'chapter',
  515. },
  516. fun: function (ctx, data, fields, options) {
  517. if (!options || !options.table || !options.b_code || !options.chapter) return;
  518. const cData = data[options.table];
  519. if (!cData) return;
  520. for (const d of cData) {
  521. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  522. }
  523. }
  524. };
  525. const filter = {
  526. name: '数据过滤',
  527. intro: '根据设置过滤数据,会直接修改数据表',
  528. defaultSetting: {
  529. "table": "pay",
  530. "condition": [
  531. {"field": "minus", "type": "bool", "value": "false"},
  532. {"field": "ptype", "type": "num", "operate": "=", "value": 1},
  533. {"field": "name", "type": "str", "operate": "=", "value": "扣回"}
  534. ],
  535. },
  536. _typeFun: {
  537. bool: '_checkBoolean',
  538. str: '_checkString',
  539. num: '_checkNumber',
  540. },
  541. _checkBoolean: function (ctx, value, condition) {
  542. switch (condition.value) {
  543. case 'true':
  544. return value ? true : false;
  545. case 'false':
  546. return value ? false : true;
  547. default:
  548. return true;
  549. }
  550. },
  551. _checkNumber: function (ctx, value, condition) {
  552. //if (ctx.helper._.isNil(value)) value = 0;
  553. switch (condition.operate) {
  554. case 'non-zero':
  555. return !ctx.helper.checkZero(value);
  556. case '=':
  557. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  558. case '>':
  559. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  560. case '<':
  561. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  562. case '>=':
  563. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  564. case '<=':
  565. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  566. default:
  567. return true;
  568. }
  569. },
  570. _checkString: function (ctx, value, condition) {
  571. switch (condition.operate) {
  572. case '=':
  573. return value === condition.value;
  574. case 'part':
  575. return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;
  576. case 'enum':
  577. return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  578. default:
  579. return true;
  580. }
  581. },
  582. fun: function (ctx, data, fields, options) {
  583. if (!options || !options.table || !options.condition) return;
  584. const fData = data[options.table], self = this;
  585. if (!fData) return;
  586. for (const c of options.condition) {
  587. c.fun = this._typeFun[c.type];
  588. }
  589. const result = fData.filter(function (d) {
  590. for (const c of options.condition) {
  591. if (!self[c.fun](ctx, d[c.field], c)) return false;
  592. }
  593. return true;
  594. });
  595. data[options.table] = result;
  596. }
  597. };
  598. const gatherStagePay = {
  599. name: '汇总合同支付数据',
  600. intro: '根据付扣款项等,分类汇总合同支付的数据',
  601. defaultSetting: {
  602. table: 'mem_stage_pay',
  603. custom: [
  604. {name: '本期完成计量', ptype: 4, order: 1, visible: false},
  605. {name: '业主违约罚金', match: '业主违约罚金', order: 2},
  606. {name: '迟付款利息', match: '迟付款利息', order: 3},
  607. {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  608. {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },
  609. {name: '动员预付款', order: 6},
  610. {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},
  611. {name: '扣材料预付款', match: '扣材料预付款', order: 8},
  612. {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},
  613. {name: '保留金', match: '保留金', order: 10},
  614. {name: '税金', match: '税金', order: 11},
  615. {name: '质量保证金', match: '质量保证金', order: 12},
  616. {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},
  617. {name: '扣款合计', minus: 1, rid: [], order: 14},
  618. {name: '支付', ptype: 2, order: 15},
  619. ]
  620. },
  621. _gatherFields: function (ctx, d, source, calcFields) {
  622. let filterData = source;
  623. if (d.ptype) {
  624. filterData = filterData.filter(function (x) {
  625. return x.ptype === d.ptype;
  626. });
  627. }
  628. if (d.match) {
  629. filterData = filterData.filter(function (x) {
  630. if (d.match instanceof Array) {
  631. for (const m of d.match) {
  632. if (x.name.indexOf(m) >= 0) return true;
  633. }
  634. } else {
  635. return x.name.indexOf(d.match) >= 0;
  636. }
  637. });
  638. }
  639. if (!ctx.helper._.isNil(d.minus)) {
  640. filterData = filterData.filter(function (x) {
  641. return d.minus === 1 ? x.minus : !x.minus;
  642. });
  643. }
  644. if (d.rid) {
  645. filterData = filterData.filter(function (x) {
  646. for (const r of d.rid) {
  647. if (x.name.indexOf(r) >= 0) return false;
  648. }
  649. return true;
  650. });
  651. }
  652. for (const fd of filterData) {
  653. for (const prop in fd) {
  654. if (prop.indexOf('tp') >= 0) {
  655. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  656. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  657. }
  658. }
  659. }
  660. },
  661. fun: function (ctx, data, fields, options) {
  662. if (!options || !options.table || !options.custom) return;
  663. const gatherData = data[options.table];
  664. const result = [], calcFields = [];
  665. for (const c of options.custom) {
  666. const cData = JSON.parse(JSON.stringify(c));
  667. if (!cData.order_calc && cData.empty !== 1) {
  668. this._gatherFields(ctx, cData, gatherData, calcFields);
  669. }
  670. result.push(cData);
  671. }
  672. rdaUtils.orderCalc(ctx, result, calcFields);
  673. data[options.table] = result.filter(function (x) {
  674. return x.visible === undefined || x.visible;
  675. });
  676. },
  677. };
  678. const union = {
  679. name: '合并数据',
  680. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  681. defaultSetting: {
  682. union: [
  683. {
  684. table: 'mem_stage_bills',
  685. fields: [
  686. {target: 'str1', source: 'chapter'},
  687. {target: 'str2', source: 'name'},
  688. {target: 'tp1', source: 'total_price'},
  689. {target: 'tp2', source: 'gather_tp'},
  690. {target: 'tp3', source: 'pre_gather_tp'},
  691. {target: 'tp4', source: 'end_gather_tp'},
  692. {target: 'str3', data: '章级合计哟'},
  693. ]
  694. }, {
  695. table: 'mem_stage_pay',
  696. fields: [
  697. {target: 'str2', source: 'name'},
  698. {target: 'tp2', source: 'tp'},
  699. {target: 'tp3', source: 'pre_tp'},
  700. {target: 'tp4', source: 'end_tp'},
  701. {target: 'str3', data: '合同支付哟'},
  702. ]
  703. }
  704. ]
  705. },
  706. fun: function(ctx, data, fields, options) {
  707. if (!options || !options.union) return;
  708. console.log(data.mem_stage_pay);
  709. const result = [];
  710. for (const u of options.union) {
  711. const unionData = data[u.table];
  712. for (const d of unionData) {
  713. const nd = {};
  714. for (const f of u.fields) {
  715. if (f.data) {
  716. nd[f.target] = f.data;
  717. } else if (f.source) {
  718. nd[f.target] = d[f.source];
  719. }
  720. }
  721. result.push(nd);
  722. }
  723. }
  724. data.mem_union_data = result;
  725. //console.log(data.mem_union_data);
  726. }
  727. };
  728. const analysisObj = {
  729. changeSort,
  730. gatherGcl,
  731. sortGcl,
  732. gatherChapter,
  733. join,
  734. getChapterCode,
  735. filter,
  736. union,
  737. gatherStagePay,
  738. };
  739. const analysisDefine = (function (obj) {
  740. const result = [];
  741. for (const o in obj) {
  742. const analysis = obj[o];
  743. if (analysis.name && analysis.fun) {
  744. result.push({
  745. name: analysis.name,
  746. key: o,
  747. hint: analysis.hint,
  748. intro: analysis.intro,
  749. url: '/help?m=report&s=analysis&d=' + o,
  750. })
  751. }
  752. }
  753. return result;
  754. })(analysisObj);
  755. module.exports = {
  756. analysisObj,
  757. analysisDefine
  758. };