rpt_data_analysis.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. const change = options && options.change ? data[options.change] : data.change;
  57. if (!change) return;
  58. // 变更令排序
  59. change.sort(function (a, b) {
  60. return a.code.localeCompare(b.code);
  61. });
  62. const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;
  63. if (changeBills) {
  64. changeBills.sort(function (a, b) {
  65. const aCIndex = data.change.findIndex(function (c) {
  66. return c.cid === a.cid;
  67. });
  68. const bCIndex = data.change.findIndex(function (c) {
  69. return c.cid === b.cid;
  70. });
  71. return aCIndex === bCIndex
  72. ? ctx.helper.compareCode(a.code, b.code)
  73. : aCIndex - bCIndex;
  74. });
  75. }
  76. },
  77. };
  78. const gatherGcl = {
  79. name: '汇总工程量清单',
  80. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  81. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  82. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  83. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  84. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  85. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  86. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  87. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  88. intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',
  89. fun: function (ctx, data, fieldsKey, options) {
  90. const gatherFields = function(gcl, data, fields) {
  91. for (const f of fields) {
  92. if (data[f]) {
  93. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  94. }
  95. }
  96. };
  97. if (!data.mem_stage_bills) return;
  98. const fields = ctx.helper._.map(fieldsKey, 'field');
  99. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  100. for (const nf of needFields) {
  101. if (fields.indexOf(nf) === -1) return;
  102. }
  103. const gatherOther = fields.indexOf('chapter') >= 0;
  104. ctx.helper._.pull(data.mem_stage_bills, 'is_leaf');
  105. const gclBills = [], other = {name: '未计入清单章节项', chapter: '10000'};
  106. for (const b of data.mem_stage_bills) {
  107. if (b.b_code && b.b_code !== '') {
  108. let gcl = gclBills.find(function (g) {
  109. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  110. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  111. });
  112. if (!gcl) {
  113. gcl = {
  114. b_code: b.b_code, name: b.name, unit: b.unit,
  115. unit_price: b.unit_price,
  116. qc_bgl_code: [], chapter: b.chapter,
  117. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  118. };
  119. gclBills.push(gcl);
  120. }
  121. gatherFields(gcl, b, [
  122. 'deal_qty', 'deal_tp',
  123. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  124. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  125. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp',
  126. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp',
  127. 'final_tp'
  128. ]);
  129. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  130. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  131. }
  132. } else if (gatherOther) {
  133. gatherFields(other, b, [
  134. 'deal_tp', 'sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price',
  135. 'contract_tp', 'qc_tp', 'gather_tp',
  136. 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp',
  137. 'end_contract_tp', 'end_qc_tp', 'end_gather_tp',
  138. 'final_tp'
  139. ]);
  140. }
  141. }
  142. if (gatherOther) gclBills.push(other);
  143. for (const g of gclBills) {
  144. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  145. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_ratio), 100);
  146. }
  147. data.mem_stage_bills = gclBills;
  148. }
  149. };
  150. const sortGcl = {
  151. name: '工程量清单排序',
  152. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  153. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  154. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  155. '特别的,如有"未计入清单章节项": \n' +
  156. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  157. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  158. intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',
  159. fun: function (ctx, data, fieldsKey, options) {
  160. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  161. const code = fieldsKey[0].field;
  162. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  163. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  164. const sortData = data[fieldsKey[0].table];
  165. if (!sortData) return;
  166. sortData.sort(function (a, b) {
  167. if (chapter !== '') {
  168. if (a[name] === '未计入清单章节项') {
  169. return b[chapter] === '100' ? 1 : -1;
  170. } else if (b[name] === '未计入清单章节项') {
  171. return a[chapter] === '100' ? -1 : 1
  172. } else {
  173. return ctx.helper.compareCode(a[code], b[code]);
  174. }
  175. } else {
  176. return ctx.helper.compareCode(a[code], b[code]);
  177. }
  178. });
  179. }
  180. };
  181. const gatherChapter = {
  182. name: '汇总章级数据',
  183. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n'+
  184. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  185. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  186. '注意事项:\n' +
  187. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  188. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  189. '示例:\n' +
  190. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  191. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  192. '结果:\n' +
  193. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  194. intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',
  195. defaultSetting: {
  196. count: 9,
  197. unChapter: {
  198. name: '未计入清单章节合计',
  199. order: 1,
  200. },
  201. gclSum: {
  202. name: '清单小计',
  203. order: 2,
  204. },
  205. unGcl: {
  206. name: '非清单项费用',
  207. order: 3,
  208. },
  209. sum: {
  210. name: '合计',
  211. order: 4,
  212. }
  213. },
  214. customSetting1: {
  215. count: 7,
  216. gclSum: {
  217. name: '第100章至700章清单合计',
  218. order: 1,
  219. },
  220. custom: [
  221. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2, visible: false},
  222. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  223. {name: '计日工合计', node_type: '计日工', order: 4},
  224. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', all: '暂定金额'}]},
  225. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  226. ],
  227. rela: [
  228. {
  229. table: 'deal_bills', key: 'code',
  230. fields: {source: 'total_price', target: 'ex_value1'},
  231. },
  232. {
  233. table: 'mem_change_bills', key: 'code',
  234. fields: {source: '', target: 'ex_value2'},
  235. }
  236. ]
  237. },
  238. _getCalcChapter: function (chapter, options) {
  239. const gclChapter = [], otherChapter = [], customChapter = [];
  240. let serialNo = 1;
  241. for (const c of chapter) {
  242. const cc = { code: c.code, name: c.name, cType: 1 };
  243. cc.serialNo = serialNo++;
  244. cc.filter = '^' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  245. //cc.visible = true;
  246. gclChapter.push(cc);
  247. if (options.activeFields) {
  248. cc.active = cc.serialNo > options.count;
  249. } else {
  250. if (serialNo > options.count) break;
  251. }
  252. }
  253. if (options.unChapter) {
  254. gclChapter.push({
  255. name: options.unChapter.name, cType: 21,
  256. serialNo: serialNo + options.unChapter.order, order: options.unChapter.order,
  257. visible: options.unChapter.visible,
  258. });
  259. }
  260. if (options.gclSum) {
  261. otherChapter.push({
  262. name: options.gclSum.name, cType: 11,
  263. serialNo: serialNo + options.gclSum.order, order: options.gclSum.order,
  264. visible: options.gclSum.visible,
  265. });
  266. }
  267. if (options.unGcl) {
  268. otherChapter.push({
  269. name: options.unGcl.name, cType: 31,
  270. serialNo: serialNo + options.unGcl.order, order: options.unGcl.order,
  271. visible: options.unGcl.visible,
  272. });
  273. }
  274. if (options.sum) {
  275. otherChapter.push({
  276. name: options.sum.name , cType: 41,
  277. serialNo: serialNo + options.sum.order, order: options.sum.order,
  278. visible: options.sum.visible,
  279. });
  280. }
  281. if (options.custom && options.custom instanceof Array) {
  282. for (const c of options.custom) {
  283. const cc = {
  284. name: c.name, serialNo: serialNo + c.order,
  285. order_calc: c.order_calc,
  286. cType: 5, order: c.order,
  287. visible: c.visible,
  288. };
  289. if (c.match) {
  290. cc.match = JSON.parse(JSON.stringify(c.match));
  291. for (const m of cc.match) {
  292. if (m.node_type && m.node_type !== '') {
  293. const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});
  294. m.node_type = nodeType.value;
  295. }
  296. }
  297. }
  298. customChapter.push(cc);
  299. }
  300. }
  301. return [gclChapter, otherChapter, customChapter];
  302. },
  303. _getGclChapter: function (chapter, data, field) {
  304. for (const c of chapter) {
  305. if (c.filter) {
  306. const reg = new RegExp(c.filter);
  307. if (reg.test(data[field])) {
  308. return c;
  309. }
  310. } else {
  311. return c;
  312. }
  313. }
  314. },
  315. _checkFilter: function (fullPath, filter) {
  316. for (const f of filter) {
  317. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  318. }
  319. return false;
  320. },
  321. _gatherRela: function (ctx, data, rela, gclChapter, otherChapter) {
  322. if (!rela) return;
  323. const gatherRelaFields = function (chapter, source, field) {
  324. const fields = field instanceof Array ? field : [field];
  325. for (const f of fields) {
  326. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  327. }
  328. };
  329. const relaBills = rela instanceof Array ? rela : [rela];
  330. for (const rb of relaBills) {
  331. if (!rb.table) continue;
  332. const relaData = data[rb.table];
  333. if (!relaData) continue;
  334. for (const rd of relaData) {
  335. for (const c of otherChapter) {
  336. if (c.cType === 41) {
  337. gatherRelaFields(c, rd, rb.fields);
  338. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  339. gatherRelaFields(c, rd, rb.fields);
  340. } else if (c.cType === 11 && (rd[rb.key])) {
  341. gatherRelaFields(c, rd, rb.fields);
  342. }
  343. }
  344. if (rd[rb.key]) {
  345. const c = this._getGclChapter(gclChapter, rd, rb.key);
  346. gatherRelaFields(c, rd, rb.fields);
  347. }
  348. }
  349. }
  350. },
  351. _orderCalc: function (ctx, chapter, fields) {
  352. const orderMatch = new RegExp('o[0-9]+', 'igm');
  353. for (const c of chapter) {
  354. if (c.order_calc && c.order_calc !== '') {
  355. const matchs = c.order_calc.match(orderMatch);
  356. const calcMatch = [];
  357. for (const m of matchs) {
  358. const order = m.substring(1, m.length);
  359. const orderChapter = chapter.find(function (x) {return x.order == order});
  360. if (orderChapter) {
  361. calcMatch.push({match: m, value: orderChapter})
  362. }
  363. }
  364. for (const f of fields) {
  365. let expr = c.order_calc;
  366. for (const m of calcMatch) {
  367. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  368. }
  369. try {
  370. c[f] = ctx.helper.round(math.eval(expr), 6);
  371. } catch(err) {
  372. }
  373. }
  374. }
  375. }
  376. },
  377. _checkMatch: function (match, d) {
  378. for (const m of match) {
  379. if (m.node_type) {
  380. if (m.node_type === d.node_type) return true;
  381. } else if (m.field) {
  382. const value = d[m.field];
  383. if (m.part && value) {
  384. if (value.indexOf(m.part) >= 0) return true;
  385. } else if (m.all && value) {
  386. if (m.all === value) return true;
  387. }
  388. }
  389. }
  390. return false;
  391. },
  392. fun: function (ctx, data, fieldsKey, options) {
  393. if (!data.tender_info || !data.tender_info.chapter) return;
  394. if (!fieldsKey && fieldsKey.length < 0) return;
  395. const calcFields = [];
  396. const gatherData = function (chapter, data) {
  397. if (!chapter) return;
  398. for (const f in data) {
  399. if (data[f] && (f.indexOf('tp') >= 0 || f === 'total_price')) {
  400. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  401. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  402. }
  403. }
  404. };
  405. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  406. const fields = ctx.helper._.map(fieldsKey, 'field');
  407. const needFields = ['b_code', 'is_leaf'];
  408. for (const nf of needFields) {
  409. if (fields.indexOf(nf) === -1) return;
  410. }
  411. const sourceData = data[fieldsKey[0].table];
  412. if (!sourceData) return;
  413. const filter = [];
  414. for (const d of sourceData) {
  415. for (const c of customChapter) {
  416. if (c.match && this._checkMatch(c.match, d)) {
  417. gatherData(c, d);
  418. filter.push(d.full_path);
  419. }
  420. }
  421. if (!d.is_leaf || this._checkFilter(d.full_path, filter)) continue;
  422. for (const c of otherChapter) {
  423. if (c.cType === 41) {
  424. gatherData(c, d);
  425. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  426. gatherData(c, d);
  427. } else if (c.cType === 11 && (d.b_code)) {
  428. gatherData(c, d);
  429. }
  430. }
  431. if (d.b_code) {
  432. const c = this._getGclChapter(gclChapter, d, 'b_code');
  433. if (c) {
  434. gatherData(c, d);
  435. }
  436. }
  437. }
  438. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  439. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  440. this._orderCalc(ctx, chapter, calcFields);
  441. chapter.sort(function (a, b) {return a.serialNo - b.serialNo});
  442. data[fieldsKey[0].table] = chapter.filter(function (x) {
  443. if (x.active) {
  444. for (const f of options.activeFields) {
  445. if (!ctx.helper.checkZero(x[f])) return true;
  446. }
  447. return false;
  448. } else {
  449. return (x.visible !== undefined && x.visible !== null) ? x.visible : true;
  450. }
  451. });
  452. },
  453. };
  454. const join = {
  455. name: "连接两张数据表",
  456. hint: "用于处理类似于关联签约清单的情况,会改变主表的数据",
  457. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  458. defaultSetting: {
  459. main: 'mem_stage_bills',
  460. sub: 'deal_bills',
  461. keyFields: [
  462. {main: 'b_code', sub: 'code', type: 'string'},
  463. {main: 'name', sub: 'name',type: 'string'},
  464. {main: 'unit', sub: 'unit',type: 'string'},
  465. {main: 'unit_price', sub: 'unit_price',type: 'number'},
  466. ],
  467. importFields: [
  468. {main: 'ex_value1', sub: 'quantity', type: 'sum'},
  469. {main: 'ex_value2', sub: 'total_price', type: 'sum'}
  470. ],
  471. joinType: 'outer', //'outer', 'main', 'sub', 'inner',
  472. },
  473. fun: function (ctx, data, fields, options) {
  474. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  475. const main = data[options.main];
  476. const sub = data[options.sub];
  477. if (!main || !sub) return;
  478. const _ = ctx.helper._, result = _.cloneDeep(main);
  479. for (const r of result) {
  480. r._join_tag = 'main';
  481. for (const i of options.importFields) {
  482. r[i.main] = null;
  483. }
  484. }
  485. for (const s of sub) {
  486. let r = result.find(function (x) {
  487. for (const k of options.keyFields) {
  488. switch (k.type) {
  489. case 'string':
  490. if (x[k.main] !== s[k.sub] && (!_.isNil(x[k.main]) || !_.isNil(s[k.sub]))) return false;
  491. break;
  492. case 'number':
  493. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main] - s[k.sub]))) return false;
  494. break;
  495. }
  496. }
  497. return true;
  498. });
  499. if (r && r._join_tag === 'main') {
  500. r._join_tag = 'both';
  501. }
  502. if (!r) {
  503. r = {_join_tag: 'sub'};
  504. for (const k of options.keyFields) {
  505. r[k.main] = s[k.sub];
  506. }
  507. result.push(r);
  508. }
  509. for (const i of options.importFields) {
  510. //r[i.main] = s[i.sub];
  511. if (i.type === 'sum') {
  512. r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);
  513. } else {
  514. r[i.main] = s[i.sub];
  515. }
  516. }
  517. }
  518. switch (options.joinType) {
  519. case 'main':
  520. data[options.main] = _.filter(result, function (r) {return ['main', 'both'].indexOf(r._join_tag) >= 0});
  521. break;
  522. case 'sub':
  523. data[options.main] = _.filter(result, function (r) {return ['sub', 'both'].indexOf(r._join_tag) >= 0});
  524. break;
  525. case 'inner':
  526. data[options.main] = _.filter(result, function (r) {return r._join_tag === 'both'});
  527. break;
  528. case 'outer':
  529. data[options.main] = result;
  530. break;
  531. }
  532. }
  533. };
  534. const getChapterCode = {
  535. name: '获取章级编号',
  536. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  537. hint: '',
  538. defaultSetting: {
  539. table: 'mem_stage_bills',
  540. b_code: 'b_code',
  541. chapter: 'chapter',
  542. },
  543. fun: function (ctx, data, fields, options) {
  544. if (!options || !options.table || !options.b_code || !options.chapter) return;
  545. const cData = data[options.table];
  546. if (!cData) return;
  547. for (const d of cData) {
  548. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  549. }
  550. }
  551. };
  552. const filter = {
  553. name: '数据过滤',
  554. intro: '根据设置过滤数据,会直接修改数据表',
  555. defaultSetting: {
  556. "table": "pay",
  557. "condition": [
  558. {"field": "minus", "type": "bool", "value": "false"},
  559. {"field": "ptype", "type": "num", "operate": "=", "value": 1},
  560. {"field": "name", "type": "str", "operate": "=", "value": "扣回"}
  561. ],
  562. },
  563. _typeFun: {
  564. bool: '_checkBoolean',
  565. str: '_checkString',
  566. num: '_checkNumber',
  567. },
  568. _checkBoolean: function (ctx, value, condition) {
  569. switch (condition.value) {
  570. case 'true':
  571. return value ? true : false;
  572. case 'false':
  573. return value ? false : true;
  574. default:
  575. return true;
  576. }
  577. },
  578. _checkNumber: function (ctx, value, condition) {
  579. //if (ctx.helper._.isNil(value)) value = 0;
  580. switch (condition.operate) {
  581. case 'non-zero':
  582. return !ctx.helper.checkZero(value);
  583. case '=':
  584. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  585. case '>':
  586. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  587. case '<':
  588. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  589. case '>=':
  590. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  591. case '<=':
  592. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  593. default:
  594. return true;
  595. }
  596. },
  597. _checkString: function (ctx, value, condition) {
  598. switch (condition.operate) {
  599. case '=':
  600. return value === condition.value;
  601. case 'part':
  602. return !ctx.helper._.isNil(value) ? value.indexOf(condition.value) >= 0 : false;
  603. case 'enum':
  604. return (!ctx.helper._.isNil(value) && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  605. default:
  606. return true;
  607. }
  608. },
  609. fun: function (ctx, data, fields, options) {
  610. if (!options || !options.table || !options.condition) return;
  611. const fData = data[options.table], self = this;
  612. if (!fData) return;
  613. for (const c of options.condition) {
  614. c.fun = this._typeFun[c.type];
  615. }
  616. const result = fData.filter(function (d) {
  617. for (const c of options.condition) {
  618. if (!self[c.fun](ctx, d[c.field], c)) return false;
  619. }
  620. return true;
  621. });
  622. data[options.table] = result;
  623. }
  624. };
  625. const gatherStagePay = {
  626. name: '汇总合同支付数据',
  627. intro: '根据付扣款项等,分类汇总合同支付的数据',
  628. defaultSetting: {
  629. table: 'mem_stage_pay',
  630. custom: [
  631. {name: '本期完成计量', ptype: 4, order: 1, visible: false},
  632. {name: '业主违约罚金', match: '业主违约罚金', order: 2},
  633. {name: '迟付款利息', match: '迟付款利息', order: 3},
  634. {flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  635. {name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4},
  636. {name: '合计', order: 5, order_calc: 'o1+o2+o3+o4', },
  637. {name: '动员预付款', order: 6},
  638. {name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7},
  639. {name: '扣材料预付款', match: '扣材料预付款', order: 8},
  640. {name: '承包人违约罚金', match: '承包人违约罚金', order: 9},
  641. {name: '保留金', match: '保留金', order: 10},
  642. {name: '税金', match: '税金', order: 11},
  643. {name: '质量保证金', match: '质量保证金', order: 12},
  644. {name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13},
  645. {name: '扣款合计', minus: 1, rid: [], order: 14},
  646. {name: '支付', ptype: 2, order: 15},
  647. ]
  648. },
  649. _filterFields: function (ctx, d, source) {
  650. let filterData = source;
  651. if (d.ptype) {
  652. filterData = filterData.filter(function (x) {
  653. return x.ptype === d.ptype;
  654. });
  655. }
  656. if (d.match) {
  657. filterData = filterData.filter(function (x) {
  658. if (x.name) {
  659. if (d.match instanceof Array) {
  660. for (const m of d.match) {
  661. if (x.name.indexOf(m) >= 0) return true;
  662. }
  663. } else {
  664. return x.name.indexOf(d.match) >= 0;
  665. }
  666. } else {
  667. return false;
  668. }
  669. });
  670. }
  671. if (!ctx.helper._.isNil(d.minus)) {
  672. filterData = filterData.filter(function (x) {
  673. return d.minus === 1 ? x.minus : !x.minus;
  674. });
  675. }
  676. if (d.rid) {
  677. filterData = filterData.filter(function (x) {
  678. if (x.name) {
  679. for (const r of d.rid) {
  680. if (x.name.indexOf(r) >= 0) return false;
  681. }
  682. }
  683. return true;
  684. });
  685. }
  686. return filterData;
  687. },
  688. _gatherFields: function (ctx, d, source, calcFields) {
  689. const filterData = this._filterFields(ctx, d, source);
  690. for (const fd of filterData) {
  691. for (const prop in fd) {
  692. if (prop.indexOf('tp') >= 0) {
  693. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  694. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  695. }
  696. }
  697. }
  698. },
  699. fun: function (ctx, data, fields, options) {
  700. if (!options || !options.table || !options.custom) return;
  701. const gatherData = data[options.table];
  702. const result = [], calcFields = [];
  703. for (const c of options.custom) {
  704. const cData = JSON.parse(JSON.stringify(c));
  705. cData.subOrder = 0;
  706. if (cData.flow) {
  707. const fData = this._filterFields(ctx, cData, gatherData);
  708. for (const [i, f] of fData.entries()) {
  709. f.order = cData.order;
  710. f.subOrder = i;
  711. result.push(fData);
  712. }
  713. } else {
  714. if (!cData.order_calc && cData.empty !== 1) {
  715. this._gatherFields(ctx, cData, gatherData, calcFields);
  716. }
  717. result.push(cData);
  718. }
  719. }
  720. rdaUtils.orderCalc(ctx, result, calcFields);
  721. data[options.table] = result.filter(function (x) {
  722. return x.visible === undefined || x.visible;
  723. });
  724. data[options.table].sort(function (a, b) {
  725. return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;
  726. })
  727. },
  728. };
  729. const union = {
  730. name: '合并数据',
  731. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  732. defaultSetting: {
  733. union: [
  734. {
  735. table: 'mem_stage_bills',
  736. fields: [
  737. {target: 'str1', source: 'chapter'},
  738. {target: 'str2', source: 'name'},
  739. {target: 'tp1', source: 'total_price'},
  740. {target: 'tp2', source: 'gather_tp'},
  741. {target: 'tp3', source: 'pre_gather_tp'},
  742. {target: 'tp4', source: 'end_gather_tp'},
  743. {target: 'str3', data: '章级合计哟'},
  744. ]
  745. }, {
  746. table: 'mem_stage_pay',
  747. fields: [
  748. {target: 'str2', source: 'name'},
  749. {target: 'tp2', source: 'tp'},
  750. {target: 'tp3', source: 'pre_tp'},
  751. {target: 'tp4', source: 'end_tp'},
  752. {target: 'str3', data: '合同支付哟'},
  753. ]
  754. }
  755. ]
  756. },
  757. fun: function(ctx, data, fields, options) {
  758. if (!options || !options.union) return;
  759. const result = [];
  760. for (const u of options.union) {
  761. const unionData = data[u.table];
  762. for (const d of unionData) {
  763. const nd = {};
  764. for (const f of u.fields) {
  765. if (f.data) {
  766. nd[f.target] = f.data;
  767. } else if (f.source) {
  768. nd[f.target] = d[f.source];
  769. }
  770. }
  771. result.push(nd);
  772. }
  773. }
  774. data.mem_union_data = result;
  775. }
  776. };
  777. const addSumChapter = {
  778. name: '添加章级合计',
  779. intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',
  780. defaultSetting: {
  781. table: 'mem_stage_bills',
  782. code: 'b_code',
  783. chapter: 'chapter',
  784. stdChapter: { title: '%s章', sum: '%s章合计'},
  785. otherChapter: { title: '其他章', sum: '其他章合计'},
  786. sum: { name: '总合计' },
  787. fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],
  788. },
  789. fun: function (ctx, data, fields, options) {
  790. if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;
  791. const gclData = data[options.table];
  792. if (!gclData || !data.tender_info) return;
  793. const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));
  794. const finalSum = options.sum ? {name: options.sum.name, chapterNum: 100000, chapterPart: 1} : null;
  795. for (const chapter of chapters) {
  796. const chapterInfo = data.tender_info.chapter.find(function (x) { return x.code === chapter});
  797. const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;
  798. // sum
  799. const chapterGcl = gclData.filter(function (x) {
  800. return x.chapter === chapter;
  801. });
  802. const sum = { chapter: chapter, chapterPart: 3, chapterNum: parseInt(chapter) };
  803. for (const cg of chapterGcl) {
  804. cg.chapterPart = 2;
  805. cg.chapterNum = parseInt(cg[options.chapter]);
  806. for (const f of options.fields) {
  807. sum[f] = ctx.helper.add(sum[f], cg[f]);
  808. if (finalSum) {
  809. finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);
  810. }
  811. }
  812. }
  813. if (chapterOptions.sum !== undefined) {
  814. sum.name = chapterOptions.sum.replace('%s', chapter);
  815. gclData.push(sum);
  816. }
  817. // title
  818. if (chapterOptions.title !== undefined) {
  819. const title = { chapter: chapter, chapterPart: 1, chapterNum: parseInt(chapter) };
  820. if (chapterInfo) {
  821. title[options.code] = options.stdChapter.title.replace('%s', chapter);
  822. title.name = chapterInfo.name;
  823. } else {
  824. title.name = chapterOptions.title;
  825. }
  826. gclData.push(title);
  827. }
  828. }
  829. if (finalSum) {
  830. gclData.push(finalSum);
  831. }
  832. gclData.sort(function (a, b) {
  833. if (a.chapter !== b.chapter) {
  834. return a.chapterNum - b.chapterNum;
  835. } else if (a.chapterPart !== b.chapterPart) {
  836. return a.chapterPart - b.chapterPart;
  837. } else {
  838. return ctx.helper.compareCode(a[options.code], b[options.code]);
  839. }
  840. });
  841. }
  842. };
  843. const auditSelect = {
  844. name: '审批人选择',
  845. hint: '需搭配用户交互--审批人选择一起使用',
  846. defaultSetting: {
  847. table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],
  848. },
  849. _stageBillsCompare(data, order) {
  850. const fields = [];
  851. for (const [i, o] of order.entries()) {
  852. const sPrefix = 'r' + o + '_';
  853. const tPrefix = 'as' + i + '_';
  854. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  855. fields.push({source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp'});
  856. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  857. fields.push({source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp'});
  858. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  859. fields.push({source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp'});
  860. }
  861. for (const d of data) {
  862. for (const f of fields) {
  863. d[f.target] = d[f.source];
  864. }
  865. }
  866. },
  867. _stagePosCompare(data, order) {
  868. const fields = [];
  869. for (const [i, o] of order.entries()) {
  870. const sPrefix = 'r' + o + '_';
  871. const tPrefix = 'as' + i + '_';
  872. fields.push({source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty'});
  873. fields.push({source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty'});
  874. fields.push({source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty'});
  875. }
  876. for (const d of data) {
  877. for (const f of fields) {
  878. d[f.target] = d[f.source];
  879. }
  880. }
  881. },
  882. _stagePay(data, order) {
  883. const fields = [];
  884. for (const [i, o] of order.entries()) {
  885. const sPrefix = 'r' + o + '_';
  886. const tPrefix = 'as' + i + '_';
  887. fields.push({source: sPrefix + 'tp', target: tPrefix + 'tp'});
  888. }
  889. for (const d of data) {
  890. for (const f of fields) {
  891. d[f.target] = d[f.source];
  892. }
  893. }
  894. },
  895. fun: function (ctx, data, fieldsKey, options, csRela) {
  896. if (!ctx.tender || !ctx.stage) return;
  897. if (!csRela.tplDefine) return;
  898. const asDefine = csRela.tplDefine.audit_select;
  899. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  900. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  901. const order = [];
  902. if (asCustom) {
  903. for (const asc of asCustom) {
  904. order.push(asc.order);
  905. }
  906. } else {
  907. const setting = JSON.stringify(asDefine.setting);
  908. for (const [i, s] of setting.select.entries) {
  909. order.push(i);
  910. }
  911. }
  912. for (const t of options.table) {
  913. switch (t) {
  914. case 'mem_stage_bills_compare':
  915. this._stageBillsCompare(data[t], order);
  916. break;
  917. case 'mem_stage_pos_compare':
  918. this._stagePosCompare(data[t], order);
  919. break;
  920. }
  921. }
  922. }
  923. };
  924. const analysisObj = {
  925. changeSort,
  926. gatherGcl,
  927. sortGcl,
  928. gatherChapter,
  929. join,
  930. getChapterCode,
  931. filter,
  932. union,
  933. gatherStagePay,
  934. addSumChapter,
  935. auditSelect,
  936. };
  937. const analysisDefine = (function (obj) {
  938. const result = [];
  939. for (const o in obj) {
  940. const analysis = obj[o];
  941. if (analysis.name && analysis.fun) {
  942. result.push({
  943. name: analysis.name,
  944. key: o,
  945. hint: analysis.hint,
  946. intro: analysis.intro,
  947. url: '/help?m=report&s=analysis&d=' + o,
  948. })
  949. }
  950. }
  951. return result;
  952. })(analysisObj);
  953. module.exports = {
  954. analysisObj,
  955. analysisDefine
  956. };