rpt_data_analysis.js 41 KB

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