rpt_data_analysis.js 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  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. const fs = require('fs');
  14. const valueCheck = {
  15. _typeFun: {
  16. bool: '_checkBoolean',
  17. str: '_checkString',
  18. num: '_checkNumber',
  19. },
  20. _checkBoolean(ctx, value, condition) {
  21. switch (condition.value) {
  22. case 'true':
  23. return !!value;
  24. case 'false':
  25. return !value;
  26. default:
  27. return true;
  28. }
  29. },
  30. _checkNumber(ctx, value, condition) {
  31. // if (ctx.helper._.isNil(value)) value = 0;
  32. switch (condition.operate) {
  33. case 'non-zero':
  34. return !ctx.helper.checkZero(value);
  35. case '=':
  36. return !ctx.helper._.isNil(value) ? value === condition.value : false;
  37. case '>':
  38. return !ctx.helper._.isNil(value) ? value > condition.value : false;
  39. case '<':
  40. return !ctx.helper._.isNil(value) ? value < condition.value : false;
  41. case '>=':
  42. return !ctx.helper._.isNil(value) ? value >= condition.value : false;
  43. case '<=':
  44. return !ctx.helper._.isNil(value) ? value <= condition.value : false;
  45. default:
  46. return true;
  47. }
  48. },
  49. _checkString(ctx, value, condition) {
  50. switch (condition.operate) {
  51. case '=':
  52. return value === condition.value;
  53. case 'part':
  54. return value ? value.indexOf(condition.value) >= 0 : false;
  55. case 'enum':
  56. return (value && condition.value instanceof Array) ? condition.value.indexOf(value) >= 0 : false;
  57. default:
  58. return true;
  59. }
  60. },
  61. loadTypeFun(condition) {
  62. for (const c of condition) {
  63. c.fun = this._typeFun[c.type];
  64. }
  65. },
  66. checkData(ctx, data, condition) {
  67. if (condition.length > 0) {
  68. let con = condition[0];
  69. let result = this[con.fun](ctx, data[con.field], con);
  70. for (let i = 1, iLen = condition.length; i < iLen; i++) {
  71. con = condition[i];
  72. result = (con.rela && con.rela === 'or')
  73. ? (result || this[con.fun](ctx, data[con.field], con))
  74. : (result && this[con.fun](ctx, data[con.field], con));
  75. }
  76. return result;
  77. }
  78. return true;
  79. },
  80. };
  81. class PosIndex {
  82. /**
  83. * 构造函数
  84. * @param {id|String, masterId|String, order|String} setting
  85. */
  86. constructor(setting) {
  87. // 无索引
  88. this.datas = [];
  89. // 以key为索引
  90. this.items = {};
  91. // 以分类id为索引的有序
  92. this.ledgerPos = {};
  93. // pos设置
  94. this.setting = setting;
  95. }
  96. /**
  97. * 加载部位明细数据
  98. * @param datas
  99. */
  100. loadDatas(datas) {
  101. this.datas = datas;
  102. this.items = {};
  103. this.ledgerPos = {};
  104. for (const data of this.datas) {
  105. const key = data[this.setting.id];
  106. this.items[key] = data;
  107. const masterKey = data[this.setting.masterId];
  108. if (!this.ledgerPos[masterKey]) {
  109. this.ledgerPos[masterKey] = [];
  110. }
  111. this.ledgerPos[masterKey].push(data);
  112. }
  113. for (const prop in this.ledgerPos) {
  114. this.resortLedgerPos(this.ledgerPos[prop]);
  115. }
  116. }
  117. getLedgerPos(mid) {
  118. return this.ledgerPos[mid];
  119. }
  120. resortLedgerPos(ledgerPos) {
  121. const self = this;
  122. if (ledgerPos instanceof Array) {
  123. ledgerPos.sort(function(a, b) {
  124. return a[self.setting.order] - b[self.setting.order];
  125. });
  126. }
  127. }
  128. }
  129. // 通用方法
  130. const rdaUtils = {
  131. orderCalc(ctx, data, fields) {
  132. const orderMatch = new RegExp('o[0-9]+', 'igm');
  133. for (const c of data) {
  134. if (c.order_calc && c.order_calc !== '') {
  135. const matchs = c.order_calc.match(orderMatch);
  136. const calcMatch = [];
  137. for (const m of matchs) {
  138. const order = m.substring(1, m.length);
  139. const orderData = data.find(function(x) { return (x.order + '') === order; });
  140. if (orderData) {
  141. calcMatch.push({ match: m, value: orderData });
  142. }
  143. }
  144. for (const f of fields) {
  145. let expr = c.order_calc;
  146. for (const m of calcMatch) {
  147. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  148. }
  149. try {
  150. c[f] = ctx.helper.round(parseFloat(math.eval(expr)), 6);
  151. } catch (err) {
  152. }
  153. }
  154. }
  155. }
  156. },
  157. };
  158. // 可提供的数据处理方法
  159. const changeSort = {
  160. name: '变更令排序',
  161. hint: '默认的变更令排序,同时对变更令,变更清单进行排序\n' +
  162. '变更令排序勿需勾选任何预定义处理指标,但是在指标映射中,需要添加如下指标:' +
  163. '1. 如果只有变更令数据,则必须添加,"变更令(change)"下的"变更令号"' +
  164. '2. 如果有变更清单需要排序,则必须在1的基础上添加,变更令(change)"下的"变更令uuid"和"变更清单(chang_audit_list)"下的"所属变更令uuid"&"清单编号"',
  165. intro: '报表对于主从表的排序有要求,解决变更令数据中的变更令、变更清单排序问题',
  166. /**
  167. *
  168. * @param ctx - context常量
  169. * @param data - 全部数据源{Array}
  170. * @param fieldsKey - 计算字段
  171. * @param options - 计算设置
  172. */
  173. fun(ctx, data, fieldsKey, options) {
  174. const change = options && options.change ? data[options.change] : data.change;
  175. if (!change) return;
  176. // 变更令排序
  177. change.sort(function(a, b) {
  178. return a.code.localeCompare(b.code);
  179. });
  180. const changeBills = options && options.changeBills ? data[options.changeBills] : data.change_audit_list;
  181. if (changeBills) {
  182. changeBills.sort(function(a, b) {
  183. const aCIndex = data.change.findIndex(function(c) {
  184. return c.cid === a.cid;
  185. });
  186. const bCIndex = data.change.findIndex(function(c) {
  187. return c.cid === b.cid;
  188. });
  189. return aCIndex === bCIndex
  190. ? ctx.helper.compareCode(a.code, b.code)
  191. : aCIndex - bCIndex;
  192. });
  193. }
  194. },
  195. };
  196. const gatherGcl = {
  197. name: '汇总工程量清单',
  198. hint: '请使用mem_stage_bills下指标,注意事项:\n' +
  199. '1. 以下字段,不管报表是否实际使用,均应添加至指标映射,且在此处应勾选(不要求顺序):\n' +
  200. ' 清单编号(b_code), 名称(name), 单位(unit), 单价(unit_price), 树结构-是否子项(is_leaf)\n' +
  201. '2. 汇总后,以下字段,均会失效, 请勿使用:\n' +
  202. ' 台账ID(id), 树结构-ID(ledger_id), 树结构父项-ID(ledger_pid),\n' +
  203. ' 树结构-层级(level), 树结构-同层排序(order), 树结构-完整路径(full_path),\n' +
  204. ' 图册号(drawing_code), 备注(memo), 节点类型(node_type), 总额计量(is_tp)\n' +
  205. '3. 如需汇总"未计入清单章节项",请勾选"章节编号(chapter)"字段\n',
  206. intro: '根据三级清单树结构,汇总平面工程量清单,目前仅支持mem_stage_bills表',
  207. _gatherCalcField(ctx, gcl, data) {
  208. for (const prop in data) {
  209. if (((prop === 'total_price' || prop.indexOf('tp') > 0) ||
  210. (prop === 'quantity' || prop.indexOf('qty') > 0)) && data[prop]) {
  211. gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);
  212. }
  213. }
  214. },
  215. _gatherTpField(ctx, gcl, data) {
  216. for (const prop in data) {
  217. if ((prop === 'total_price' || prop.indexOf('tp') > 0) && data[prop]) {
  218. gcl[prop] = ctx.helper.add(gcl[prop], data[prop]);
  219. }
  220. }
  221. },
  222. _loadGatherInfo(gcl, data) {
  223. for (const prop in data) {
  224. if (/^t_[0-9]+_(id|name)$/.test(prop)) {
  225. gcl[prop] = data[prop];
  226. }
  227. }
  228. },
  229. _loadStageSumInfo(gcl, data) {
  230. for (const prop in data) {
  231. if (/^s_[0-9]+_order$/.test(prop)) {
  232. gcl[prop] = data[prop];
  233. }
  234. }
  235. },
  236. _gatherFields(gcl, data, fields) {
  237. for (const f of fields) {
  238. if (data[f]) {
  239. gcl[f] = ctx.helper.add(gcl[f], data[f]);
  240. }
  241. }
  242. },
  243. fun(ctx, data, fieldsKey, options) {
  244. const tableName = options ? options.table : 'mem_stage_bills';
  245. const gatherData = data[tableName];
  246. if (!gatherData || gatherData.length === 0) return;
  247. const fields = ctx.helper._.map(fieldsKey, 'field');
  248. const needFields = ['b_code', 'name', 'unit', 'unit_price', 'is_leaf'];
  249. for (const nf of needFields) {
  250. if (fields.indexOf(nf) === -1) return;
  251. }
  252. const gatherOther = fields.indexOf('chapter') >= 0;
  253. const gclBills = [],
  254. other = { name: '未计入清单章节项', chapter: '10000' };
  255. if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(other, gatherData[0]);
  256. if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(other, gatherData[0]);
  257. for (const b of gatherData) {
  258. const child = ctx.helper._.find(gatherData, { ledger_pid: b.ledger_id });
  259. if (child) continue;
  260. if (b.b_code && b.b_code !== '') {
  261. let gcl = gclBills.find(function(g) {
  262. return g.b_code === b.b_code && g.name === b.name && g.unit === b.unit
  263. && ctx.helper.checkZero(ctx.helper.sub(g.unit_price, b.unit_price));
  264. });
  265. if (!gcl) {
  266. gcl = {
  267. b_code: b.b_code, name: b.name, unit: b.unit,
  268. unit_price: b.unit_price,
  269. qc_bgl_code: [], chapter: b.chapter,
  270. deal_bills_qty: b.deal_bills_qty, deal_bills_tp: b.deal_bills_tp,
  271. };
  272. if (tableName.indexOf('mem_gather_') >= 0) this._loadGatherInfo(gcl, b);
  273. if (tableName.indexOf('mem_stage_sum_') >= 0) this._loadStageSumInfo(gcl, b);
  274. gclBills.push(gcl);
  275. }
  276. this._gatherCalcField(ctx, gcl, b);
  277. if (b.qc_bgl_code && b.qc_bgl_code !== '') {
  278. gcl.qc_bgl_code = gcl.qc_bgl_code.concat(b.qc_bgl_code.split(';'));
  279. }
  280. } else if (gatherOther) {
  281. this._gatherTpField(ctx, other, b);
  282. }
  283. }
  284. if (gatherOther) gclBills.push(other);
  285. for (const g of gclBills) {
  286. if (g.qc_bgl_code) g.qc_bgl_code = g.qc_bgl_code.join(';');
  287. g.final_ratio = ctx.helper.mul(ctx.helper.div(g.end_gather_tp, g.final_tp), 100);
  288. }
  289. data[tableName] = gclBills;
  290. },
  291. };
  292. const sortGcl = {
  293. name: '工程量清单排序',
  294. hint: '只对一张表,进行工程量清单排序,排序哪张表,根据勾选的清单编号字段决定:\n' +
  295. 'e.g.1 要对mem_stage_bills排序,需要勾选mem_stage_bills下的"清单编号(b_code)"字段\n' +
  296. 'e.g.2 要对mem_stage_im_zl排序,需要勾选mem_stage_im_zl下的"中间计量总量信息_编号(code)"字段\n' +
  297. '特别的,如有"未计入清单章节项": \n' +
  298. ' 1. 默认"未计入清单章节项"排列在最后\n' +
  299. ' 2. 如须"未计入清单章节项"排在100章之后,请在清单编号字段后,依次勾选"章节编号(chapter)", "名称(name)"\n',
  300. intro: '根据选择列,对数据进行工程量清单排序,兼容1000章后清单,以及403-1-a类的非纯数字编号排序',
  301. fun(ctx, data, fieldsKey, options) {
  302. if (fieldsKey.length !== 1 && fieldsKey.length !== 3) return;
  303. const code = fieldsKey[0].field;
  304. const chapter = fieldsKey.length > 1 ? fieldsKey[1].field : '';
  305. const name = fieldsKey.length > 2 ? fieldsKey[2].field : '';
  306. const sortData = data[fieldsKey[0].table];
  307. if (!sortData) return;
  308. sortData.sort(function(a, b) {
  309. if (chapter !== '') {
  310. if (a[name] === '未计入清单章节项') {
  311. return b[chapter] === '100' ? 1 : -1;
  312. } else if (b[name] === '未计入清单章节项') {
  313. return a[chapter] === '100' ? -1 : 1;
  314. }
  315. return ctx.helper.compareCode(a[code], b[code]);
  316. }
  317. return ctx.helper.compareCode(a[code], b[code]);
  318. });
  319. },
  320. };
  321. const gatherChapter = {
  322. name: '汇总章级数据',
  323. hint: '请使用mem_stage_bills/mem_stage_bills_compare/ledger,仅对一张表进行汇总,并生成数据:\n' +
  324. '1. 因为是汇总章级数据,必须在离散数据中添加"章节代码"&"章节名称"\n' +
  325. '2. 需勾选"清单编号(b_code)", "树结构-是否子项(is_leaf)"字段,可以对任何含有这些字段的表汇总\n' +
  326. '注意事项:\n' +
  327. '1. 算法对数据表没有要求,保证有上述字段,且按顺序勾选即可, 仅汇总金额\n' +
  328. '2. 算法计算后,原数据表中非数字类型的字段全部失效(除清单编号、名称外),请勿在指标映射中添加\n' +
  329. '示例:\n' +
  330. 'e.g.1 要对mem_stage_bills汇总,须勾选mem_stage_bills下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  331. 'e.g.2 要对mem_stage_bills_compare汇总,须勾选mem_stage_bills_compare下的"清单编号(b_code)", "树结构-是否子项((is_leaf)"字段\n' +
  332. '结果:\n' +
  333. '汇总结果可参照 清单汇总--章节合计,但是不过滤1000-1300章数据',
  334. intro: '用于得到类似“清单汇总--章级合计”的数据,可通过options自定义数据',
  335. defaultSetting: {
  336. count: 9,
  337. unChapter: {
  338. name: '未计入清单章节合计',
  339. order: 1,
  340. },
  341. gclSum: {
  342. name: '清单小计',
  343. order: 2,
  344. },
  345. unGcl: {
  346. name: '非清单项费用',
  347. order: 3,
  348. },
  349. sum: {
  350. name: '合计',
  351. order: 4,
  352. },
  353. filter: [{node_type: standard.nodeType.find(function (x) {return x.text === '计日工'}).value}, {field: 'name', part: '计日工'}],
  354. },
  355. customSetting1: {
  356. count: 7,
  357. gclSum: {
  358. name: '第100章至700章清单合计',
  359. order: 1,
  360. },
  361. custom: [
  362. {name: '已包含在清单合计中的材料、工程设备、专业工程暂估价', order: 2, visible: false},
  363. {name: '清单合计减去材料、工程设备、专业工程暂估价(即8-9=10)', order_calc: 'o1-o2', order: 3},
  364. {name: '计日工合计', node_type: '计日工', order: 4},
  365. {name: '暂列金额(不含计日工总额)(即10×暂列金额比列)', order: 5, match: [{node_type: '暂列金额'}, {field: 'name', part: '暂列金额'}, {field: 'name', all: '暂定金额'}]},
  366. {name: '投标报价、台账价(8+11+12)=13', order_calc: 'o1+o4+o5', order: 6},
  367. ],
  368. rela: [
  369. {
  370. table: 'deal_bills', key: 'code',
  371. fields: { source: 'total_price', target: 'ex_value1' },
  372. },
  373. {
  374. table: 'mem_change_bills', key: 'code',
  375. fields: { source: '', target: 'ex_value2' },
  376. },
  377. ],
  378. },
  379. _getCalcChapter(chapter, options) {
  380. const gclChapter = [],
  381. otherChapter = [],
  382. customChapter = [];
  383. let serialNo = 1;
  384. for (const c of chapter) {
  385. const cc = { code: c.code, name: c.name, cType: 1 };
  386. cc.serialNo = serialNo++;
  387. cc.filter = '^[\\D]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  388. // cc.visible = true;
  389. gclChapter.push(cc);
  390. if (options.activeFields) {
  391. cc.active = cc.serialNo > options.count;
  392. } else {
  393. if (serialNo > options.count) break;
  394. }
  395. }
  396. if (options.unChapter) {
  397. gclChapter.push({
  398. name: options.unChapter.name, cType: 21,
  399. serialNo: serialNo + options.unChapter.order, order: options.unChapter.order,
  400. visible: options.unChapter.visible,
  401. });
  402. }
  403. if (options.gclSum) {
  404. otherChapter.push({
  405. name: options.gclSum.name, cType: 11,
  406. serialNo: serialNo + options.gclSum.order, order: options.gclSum.order,
  407. visible: options.gclSum.visible,
  408. });
  409. }
  410. if (options.unGcl) {
  411. otherChapter.push({
  412. name: options.unGcl.name, cType: 31,
  413. serialNo: serialNo + options.unGcl.order, order: options.unGcl.order,
  414. visible: options.unGcl.visible,
  415. });
  416. }
  417. if (options.sum) {
  418. otherChapter.push({
  419. name: options.sum.name , cType: 41,
  420. serialNo: serialNo + options.sum.order, order: options.sum.order,
  421. visible: options.sum.visible,
  422. });
  423. }
  424. if (options.custom && options.custom instanceof Array) {
  425. for (const c of options.custom) {
  426. const cc = {
  427. name: c.name, serialNo: serialNo + c.order,
  428. order_calc: c.order_calc,
  429. cType: 5, order: c.order,
  430. visible: c.visible,
  431. };
  432. if (c.match) {
  433. cc.match = JSON.parse(JSON.stringify(c.match));
  434. for (const m of cc.match) {
  435. if (m.node_type && m.node_type !== '') {
  436. const nodeType = standard.nodeType.find(function (x) {return x.text === m.node_type});
  437. m.node_type = nodeType.value;
  438. }
  439. }
  440. }
  441. customChapter.push(cc);
  442. }
  443. }
  444. return [gclChapter, otherChapter, customChapter];
  445. },
  446. _completeGatherInfo(chapters, source) {
  447. for (const f in source) {
  448. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  449. for (const chapter of chapters) {
  450. chapter[f] = source[f];
  451. }
  452. }
  453. }
  454. },
  455. _completeStageSumInfo(chapters, source) {
  456. for (const f in source) {
  457. if (/^s_[0-9]*_order$/.test(f)) {
  458. for (const chapter of chapters) {
  459. chapter[f] = source[f];
  460. }
  461. }
  462. }
  463. },
  464. _getGclChapter(chapter, data, field) {
  465. for (const c of chapter) {
  466. if (c.filter) {
  467. const reg = new RegExp(c.filter);
  468. if (reg.test(data[field])) {
  469. return c;
  470. }
  471. } else {
  472. return c;
  473. }
  474. }
  475. },
  476. _checkFilter(fullPath, filter) {
  477. for (const f of filter) {
  478. if (fullPath.indexOf(f + '-') === 0 || fullPath === f) return true;
  479. }
  480. return false;
  481. },
  482. _gatherRela(ctx, data, rela, gclChapter, otherChapter) {
  483. if (!rela) return;
  484. const gatherRelaFields = function(chapter, source, field) {
  485. const fields = field instanceof Array ? field : [field];
  486. for (const f of fields) {
  487. chapter[f.target] = ctx.helper.add(chapter[f.target], source[f.source]);
  488. }
  489. };
  490. const relaBills = rela instanceof Array ? rela : [rela];
  491. for (const rb of relaBills) {
  492. if (!rb.table) continue;
  493. const relaData = data[rb.table];
  494. if (!relaData) continue;
  495. for (const rd of relaData) {
  496. for (const c of otherChapter) {
  497. if (c.cType === 41) {
  498. gatherRelaFields(c, rd, rb.fields);
  499. } else if (c.cType === 31 && (!rd[rb.key] || rd[rb.key] === '')) {
  500. gatherRelaFields(c, rd, rb.fields);
  501. } else if (c.cType === 11 && (rd[rb.key])) {
  502. gatherRelaFields(c, rd, rb.fields);
  503. }
  504. }
  505. if (rd[rb.key]) {
  506. const c = this._getGclChapter(gclChapter, rd, rb.key);
  507. if (c) gatherRelaFields(c, rd, rb.fields);
  508. }
  509. }
  510. }
  511. },
  512. _orderCalc(ctx, chapter, fields) {
  513. const orderMatch = new RegExp('o[0-9]+', 'igm');
  514. for (const c of chapter) {
  515. if (c.order_calc && c.order_calc !== '') {
  516. const matchs = c.order_calc.match(orderMatch);
  517. const calcMatch = [];
  518. for (const m of matchs) {
  519. const order = m.substring(1, m.length);
  520. const orderChapter = chapter.find(function(x) { return x.order == order; });
  521. if (orderChapter) {
  522. calcMatch.push({ match: m, value: orderChapter });
  523. }
  524. }
  525. for (const f of fields) {
  526. let expr = c.order_calc;
  527. for (const m of calcMatch) {
  528. expr = expr.replace(m.match, m.value[f] ? m.value[f] : 0);
  529. }
  530. try {
  531. c[f] = ctx.helper.round(parseFloat(math.eval(expr), 6));
  532. } catch (err) {
  533. }
  534. }
  535. }
  536. }
  537. },
  538. _checkMatch(match, d) {
  539. for (const m of match) {
  540. if (m.node_type) {
  541. if (m.node_type === d.node_type) return true;
  542. } else if (m.field) {
  543. const value = d[m.field];
  544. if (m.part && value) {
  545. if (value.indexOf(m.part) >= 0) return true;
  546. } else if (m.all && value) {
  547. if (m.all === value) return true;
  548. }
  549. }
  550. }
  551. return false;
  552. },
  553. fun(ctx, data, fieldsKey, options) {
  554. if (!data.tender_info || !data.tender_info.chapter) return;
  555. if (!fieldsKey && fieldsKey.length < 0) return;
  556. const calcFields = [];
  557. const gatherData = function(chapter, data) {
  558. if (!chapter) return;
  559. for (const f in data) {
  560. if (!data[f]) continue;
  561. if ((f.indexOf('tp') >= 0 || f === 'total_price')) {
  562. chapter[f] = ctx.helper.add(chapter[f], data[f]);
  563. if (calcFields.indexOf(f) === -1) calcFields.push(f);
  564. }
  565. }
  566. };
  567. const fields = ctx.helper._.map(fieldsKey, 'field');
  568. const needFields = ['b_code', 'is_leaf'];
  569. for (const nf of needFields) {
  570. if (fields.indexOf(nf) === -1) return;
  571. }
  572. const sourceData = data[fieldsKey[0].table];
  573. if (!sourceData) return;
  574. const [gclChapter, otherChapter, customChapter] = this._getCalcChapter(data.tender_info.chapter, options ? options : this.defaultSetting);
  575. if (fieldsKey[0].table.indexOf('mem_gather') >= 0) {
  576. this._completeGatherInfo(gclChapter, sourceData[0]);
  577. this._completeGatherInfo(otherChapter, sourceData[0]);
  578. this._completeGatherInfo(customChapter, sourceData[0]);
  579. }
  580. if (fieldsKey[0].table.indexOf('mem_stage_sum_') >= 0) {
  581. this._completeStageSumInfo(gclChapter, sourceData[0]);
  582. this._completeStageSumInfo(otherChapter, sourceData[0]);
  583. this._completeStageSumInfo(customChapter, sourceData[0]);
  584. }
  585. const filter = [],
  586. defaultFilter = [];
  587. for (const d of sourceData) {
  588. if (this._checkMatch(this.defaultSetting.filter, d)) defaultFilter.push(d.full_path);
  589. if (this._checkFilter(d.full_path, filter)) continue;
  590. for (const c of customChapter) {
  591. if (c.match && this._checkMatch(c.match, d)) {
  592. gatherData(c, d);
  593. filter.push(d.full_path);
  594. }
  595. }
  596. if (!d.is_leaf) continue;
  597. for (const c of otherChapter) {
  598. if (c.cType === 41) {
  599. gatherData(c, d);
  600. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  601. gatherData(c, d);
  602. } else if (c.cType === 11 && (d.b_code)) {
  603. gatherData(c, d);
  604. }
  605. }
  606. if (d.b_code) {
  607. const c = this._checkFilter(d.full_path, defaultFilter)
  608. ? gclChapter.find(x => { return x.cType === 21; })
  609. : this._getGclChapter(gclChapter, d, 'b_code');
  610. if (c) {
  611. gatherData(c, d);
  612. }
  613. }
  614. }
  615. this._gatherRela(ctx, data, options.rela, gclChapter, otherChapter);
  616. const chapter = gclChapter.concat(otherChapter).concat(customChapter);
  617. this._orderCalc(ctx, chapter, calcFields);
  618. chapter.sort(function(a, b) { return a.serialNo - b.serialNo; });
  619. data[fieldsKey[0].table] = chapter.filter(function(x) {
  620. if (x.active) {
  621. for (const f of options.activeFields) {
  622. if (!ctx.helper.checkZero(x[f])) return true;
  623. }
  624. return false;
  625. }
  626. return (x.visible !== undefined && x.visible !== null) ? x.visible : true;
  627. });
  628. },
  629. };
  630. const join = {
  631. name: '连接两张数据表',
  632. hint: '用于处理类似于关联签约清单的情况,会改变主表的数据',
  633. intro: '用于处理类似于关联签约清单的情况,会根据关联表(sub)改变主表(main)的数据',
  634. defaultSetting: {
  635. main: 'mem_stage_bills',
  636. sub: 'deal_bills',
  637. keyFields: [
  638. { main: 'b_code', sub: 'code', type: 'string' },
  639. { main: 'name', sub: 'name', type: 'string' },
  640. { main: 'unit', sub: 'unit', type: 'string' },
  641. { main: 'unit_price', sub: 'unit_price', type: 'number' },
  642. ],
  643. importFields: [
  644. { main: 'ex_value1', sub: 'quantity', type: 'sum' },
  645. { main: 'ex_value2', sub: 'total_price', type: 'sum' },
  646. ],
  647. joinType: 'outer', // 'outer', 'main', 'sub', 'inner',
  648. },
  649. fun(ctx, data, fields, options) {
  650. if (!options || !options.main || !options.sub || !options.keyFields || options.keyFields.length === 0) return;
  651. const main = data[options.main];
  652. const sub = data[options.sub];
  653. if (!main || !sub) return;
  654. const _ = ctx.helper._,
  655. result = _.cloneDeep(main);
  656. for (const r of result) {
  657. r._join_tag = 'main';
  658. for (const i of options.importFields) {
  659. r[i.main] = null;
  660. }
  661. }
  662. for (const s of sub) {
  663. let subR = result.filter(function(x) {
  664. for (const k of options.keyFields) {
  665. switch (k.type) {
  666. case 'string':
  667. if ((x[k.main] || '') !== (s[k.sub] || '')) return false;
  668. break;
  669. case 'number':
  670. if (!ctx.helper.checkZero(ctx.helper.sub(x[k.main], s[k.sub]))) return false;
  671. break;
  672. }
  673. }
  674. return true;
  675. });
  676. for (let r of subR) {
  677. if (r && r._join_tag === 'main') {
  678. r._join_tag = 'both';
  679. }
  680. if (!r) {
  681. r = { _join_tag: 'sub' };
  682. for (const k of options.keyFields) {
  683. r[k.main] = s[k.sub];
  684. }
  685. result.push(r);
  686. }
  687. for (const i of options.importFields) {
  688. // r[i.main] = s[i.sub];
  689. if (i.type === 'sum') {
  690. r[i.main] = ctx.helper.add(r[i.main], s[i.sub]);
  691. } else {
  692. r[i.main] = s[i.sub];
  693. }
  694. }
  695. }
  696. }
  697. switch (options.joinType) {
  698. case 'main':
  699. data[options.main] = _.filter(result, function(r) { return ['main', 'both'].indexOf(r._join_tag) >= 0; });
  700. break;
  701. case 'sub':
  702. data[options.main] = _.filter(result, function(r) { return ['sub', 'both'].indexOf(r._join_tag) >= 0; });
  703. break;
  704. case 'inner':
  705. data[options.main] = _.filter(result, function(r) { return r._join_tag === 'both'; });
  706. break;
  707. case 'outer':
  708. data[options.main] = result;
  709. break;
  710. }
  711. },
  712. };
  713. const getChapterCode = {
  714. name: '获取章级编号',
  715. intro: '根据清单编号,计算章级编号,并写入指定字段,适用于未提供chapter字段的数据,例如签约清单',
  716. hint: '',
  717. defaultSetting: {
  718. table: 'mem_stage_bills',
  719. b_code: 'b_code',
  720. chapter: 'chapter',
  721. },
  722. fun(ctx, data, fields, options) {
  723. if (!options || !options.table || !options.b_code || !options.chapter) return;
  724. const cData = data[options.table];
  725. if (!cData) return;
  726. for (const d of cData) {
  727. d[options.chapter] = ctx.helper.getChapterCode(d[options.b_code]);
  728. }
  729. },
  730. };
  731. const filter = {
  732. name: '数据过滤',
  733. intro: '根据设置过滤数据,会直接修改数据表',
  734. defaultSetting: {
  735. table: 'pay',
  736. condition: [
  737. { field: 'minus', type: 'bool', value: 'false' },
  738. { field: 'ptype', type: 'num', operate: '=', value: 1, rela: 'and' },
  739. { field: 'name', type: 'str', operate: '=', value: '扣回', rela: 'or' },
  740. ],
  741. f_type: "or'",
  742. },
  743. _typeFun: {
  744. bool: '_checkBoolean',
  745. str: '_checkString',
  746. num: '_checkNumber',
  747. },
  748. fun(ctx, data, fields, options) {
  749. if (!options || !options.table || !options.condition) return;
  750. const fData = data[options.table];
  751. if (!fData) return;
  752. valueCheck.loadTypeFun(options.condition);
  753. data[options.table] = fData.filter(function(d) {
  754. if (options.condition.length > 0) {
  755. let con = options.condition[0];
  756. let result = valueCheck[con.fun](ctx, d[con.field], con);
  757. for (let i = 1, iLen = options.condition.length; i < iLen; i++) {
  758. con = options.condition[i];
  759. result = (con.rela && con.rela === 'or')
  760. ? (result || valueCheck[con.fun](ctx, d[con.field], con))
  761. : (result && valueCheck[con.fun](ctx, d[con.field], con));
  762. }
  763. return result;
  764. }
  765. return true;
  766. });
  767. },
  768. };
  769. const gatherStagePay = {
  770. name: '汇总合同支付数据',
  771. intro: '根据付扣款项等,分类汇总合同支付的数据',
  772. defaultSetting: {
  773. table: 'mem_stage_pay',
  774. custom: [
  775. { name: '本期完成计量', ptype: 4, order: 1, visible: false },
  776. { name: '业主违约罚金', match: '业主违约罚金', order: 2 },
  777. { name: '迟付款利息', match: '迟付款利息', order: 3 },
  778. { flow: true, minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4 },
  779. { name: '其他付款', minus: 0, rid: ['业主违约罚金', '迟付款利息'], order: 4 },
  780. { name: '合计', order: 5, order_calc: 'o1+o2+o3+o4' },
  781. { name: '动员预付款', order: 6 },
  782. { name: '扣动员预付款', match: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款'], order: 7 },
  783. { name: '扣材料预付款', match: '扣材料预付款', order: 8 },
  784. { name: '承包人违约罚金', match: '承包人违约罚金', order: 9 },
  785. { name: '保留金', match: '保留金', order: 10 },
  786. { name: '税金', match: '税金', order: 11 },
  787. { name: '质量保证金', match: '质量保证金', order: 12 },
  788. { name: '其他扣款', minus: 1, rid: ['扣动员预付款', '扣回动员预付款', '扣开工预付款', '扣回开工预付款', '扣材料预付款', '承包人违约罚金', '保留金', '税金', '质量保证金'], order: 13 },
  789. { name: '扣款合计', minus: 1, rid: [], order: 14 },
  790. { name: '支付', ptype: 2, order: 15 },
  791. ],
  792. },
  793. _filterFields(ctx, d, source) {
  794. let filterData = source;
  795. if (d.ptype) {
  796. filterData = filterData.filter(function(x) {
  797. return x.ptype === d.ptype;
  798. });
  799. }
  800. if (d.match) {
  801. filterData = filterData.filter(function(x) {
  802. if (x.name) {
  803. if (d.match instanceof Array) {
  804. for (const m of d.match) {
  805. if (x.name.indexOf(m) >= 0) return true;
  806. }
  807. } else {
  808. return x.name.indexOf(d.match) >= 0;
  809. }
  810. } else {
  811. return false;
  812. }
  813. });
  814. }
  815. if (!ctx.helper._.isNil(d.minus)) {
  816. filterData = filterData.filter(function(x) {
  817. return d.minus === 1 ? x.minus : !x.minus;
  818. });
  819. }
  820. if (d.rid) {
  821. filterData = filterData.filter(function(x) {
  822. if (x.name) {
  823. for (const r of d.rid) {
  824. if (x.name.indexOf(r) >= 0) return false;
  825. }
  826. }
  827. return true;
  828. });
  829. }
  830. return filterData;
  831. },
  832. _gatherFields(ctx, d, source, calcFields) {
  833. const filterData = this._filterFields(ctx, d, source);
  834. for (const fd of filterData) {
  835. for (const prop in fd) {
  836. if (prop.indexOf('tp') >= 0) {
  837. d[prop] = ctx.helper.add(d[prop], fd[prop]);
  838. if (calcFields.indexOf(prop) === -1) calcFields.push(prop);
  839. }
  840. }
  841. }
  842. },
  843. _completeGatherInfo(d, source) {
  844. for (const f in source) {
  845. if (/^t_[0-9]*_(id|name)$/.test(f)) {
  846. d[f] = source[f];
  847. }
  848. }
  849. },
  850. _completeStageSumInfo(d, source) {
  851. for (const f in source) {
  852. if (/^t_[0-9]*_order$/.test(f)) {
  853. d[f] = source[f];
  854. }
  855. }
  856. },
  857. fun(ctx, data, fields, options) {
  858. if (!options || !options.table || !options.custom) return;
  859. const gatherData = data[options.table];
  860. const result = [],
  861. calcFields = [];
  862. for (const c of options.custom) {
  863. const cData = JSON.parse(JSON.stringify(c));
  864. cData.subOrder = 0;
  865. if (cData.flow) {
  866. const fData = this._filterFields(ctx, cData, gatherData);
  867. for (const [i, f] of fData.entries()) {
  868. f.order = cData.order;
  869. f.subOrder = i;
  870. result.push(f);
  871. }
  872. } else {
  873. if (!cData.order_calc && cData.empty !== 1) {
  874. this._gatherFields(ctx, cData, gatherData, calcFields);
  875. }
  876. if (options.table.indexOf('mem_gather') >= 0) this._completeGatherInfo(cData, gatherData[0]);
  877. if (options.table.indexOf('mem_stage_sum_') >= 0) this._completeStageSumInfo(cData, gatherData[0]);
  878. result.push(cData);
  879. }
  880. }
  881. rdaUtils.orderCalc(ctx, result, calcFields);
  882. data[options.table] = result.filter(function(x) {
  883. return x.visible === undefined || x.visible;
  884. });
  885. data[options.table].sort(function(a, b) {
  886. return a.order === b.order ? a.subOrder - b.subOrder : a.order - b.order;
  887. });
  888. },
  889. };
  890. const union = {
  891. name: '合并数据',
  892. intro: '类似sql的union,可合并任意数据,合并结果写到"预留扩展数据-合并(mem_union_data)"中',
  893. defaultSetting: {
  894. union: [
  895. {
  896. table: 'mem_stage_bills',
  897. fields: [
  898. { target: 'str1', source: 'chapter' },
  899. { target: 'str2', source: 'name' },
  900. { target: 'tp1', source: 'total_price' },
  901. { target: 'tp2', source: 'gather_tp' },
  902. { target: 'tp3', source: 'pre_gather_tp' },
  903. { target: 'tp4', source: 'end_gather_tp' },
  904. { target: 'str3', data: '章级合计哟' },
  905. ],
  906. }, {
  907. table: 'mem_stage_pay',
  908. fields: [
  909. { target: 'str2', source: 'name' },
  910. { target: 'tp2', source: 'tp' },
  911. { target: 'tp3', source: 'pre_tp' },
  912. { target: 'tp4', source: 'end_tp' },
  913. { target: 'str3', data: '合同支付哟' },
  914. ],
  915. },
  916. ],
  917. },
  918. fun(ctx, data, fields, options) {
  919. if (!options || !options.union) return;
  920. const result = [];
  921. for (const u of options.union) {
  922. const unionData = data[u.table];
  923. for (const d of unionData) {
  924. const nd = {};
  925. for (const f of u.fields) {
  926. if (f.data) {
  927. nd[f.target] = f.data;
  928. } else if (f.source) {
  929. nd[f.target] = d[f.source];
  930. }
  931. }
  932. result.push(nd);
  933. }
  934. }
  935. data.mem_union_data = result;
  936. },
  937. };
  938. const addSumChapter = {
  939. name: '添加章级合计',
  940. intro: '在排序好的工程量清单列表中,根据清单所属章级,添加章级标题行、合计行',
  941. defaultSetting: {
  942. table: 'mem_stage_bills',
  943. code: 'b_code',
  944. chapter: 'chapter',
  945. stdChapter: { title: '%s章', sum: '%s章合计' },
  946. otherChapter: { title: '其他章', sum: '其他章合计' },
  947. sum: { name: '总合计' },
  948. fields: ['ex_value1', 'ex_value2', 'end_gather_tp', 'pre_gather_tp', 'gather_tp'],
  949. },
  950. fun(ctx, data, fields, options) {
  951. if (!options || !options.table || !options.code || !options.chapter || !options.stdChapter) return;
  952. const gclData = data[options.table];
  953. if (!gclData || !data.tender_info) return;
  954. const chapters = ctx.helper._.uniq(ctx.helper._.map(gclData, options.chapter));
  955. const finalSum = options.sum ? { name: options.sum.name, chapterNum: 100000, chapterPart: 1 } : null;
  956. for (const chapter of chapters) {
  957. const chapterInfo = data.tender_info.chapter.find(function(x) { return x.code === chapter; });
  958. const chapterOptions = chapterInfo ? options.stdChapter : options.otherChapter;
  959. // sum
  960. const chapterGcl = gclData.filter(function(x) {
  961. return x.chapter === chapter;
  962. });
  963. const sum = { chapter, chapterPart: 3, chapterNum: parseInt(chapter) };
  964. for (const cg of chapterGcl) {
  965. cg.chapterPart = 2;
  966. cg.chapterNum = parseInt(cg[options.chapter]);
  967. for (const f of options.fields) {
  968. sum[f] = ctx.helper.add(sum[f], cg[f]);
  969. if (finalSum) {
  970. finalSum[f] = ctx.helper.add(finalSum[f], cg[f]);
  971. }
  972. }
  973. }
  974. if (chapterOptions.sum !== undefined) {
  975. sum.name = chapterOptions.sum.replace('%s', chapter);
  976. gclData.push(sum);
  977. }
  978. // title
  979. if (chapterOptions.title !== undefined) {
  980. const title = { chapter, chapterPart: 1, chapterNum: parseInt(chapter) };
  981. if (chapterInfo) {
  982. title[options.code] = options.stdChapter.title.replace('%s', chapter);
  983. title.name = chapterInfo.name;
  984. } else {
  985. title.name = chapterOptions.title;
  986. }
  987. gclData.push(title);
  988. }
  989. }
  990. if (finalSum) {
  991. gclData.push(finalSum);
  992. }
  993. gclData.sort(function(a, b) {
  994. if (a.chapter !== b.chapter) {
  995. return a.chapterNum - b.chapterNum;
  996. } else if (a.chapterPart !== b.chapterPart) {
  997. return a.chapterPart - b.chapterPart;
  998. }
  999. return ctx.helper.compareCode(a[options.code], b[options.code]);
  1000. });
  1001. },
  1002. };
  1003. const auditSelect = {
  1004. name: '审批人选择',
  1005. hint: '需搭配用户交互--审批人选择一起使用',
  1006. defaultSetting: {
  1007. table: ['mem_stage_bills_compare', 'mem_stage_pos_compare', 'mem_stage_pay'],
  1008. },
  1009. _stageBillsCompare(data, order) {
  1010. const fields = [];
  1011. for (const [i, o] of order.entries()) {
  1012. const sPrefix = 'r' + o + '_';
  1013. const tPrefix = 'as' + i + '_';
  1014. fields.push({ source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty' });
  1015. fields.push({ source: sPrefix + 'contract_tp', target: tPrefix + 'contract_tp' });
  1016. fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });
  1017. fields.push({ source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp' });
  1018. fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });
  1019. fields.push({ source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp' });
  1020. }
  1021. for (const d of data) {
  1022. for (const f of fields) {
  1023. d[f.target] = d[f.source];
  1024. }
  1025. }
  1026. },
  1027. _stagePosCompare(data, order) {
  1028. const fields = [];
  1029. for (const [i, o] of order.entries()) {
  1030. const sPrefix = 'r' + o + '_';
  1031. const tPrefix = 'as' + i + '_';
  1032. fields.push({ source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty' });
  1033. fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });
  1034. fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });
  1035. }
  1036. for (const d of data) {
  1037. for (const f of fields) {
  1038. d[f.target] = d[f.source];
  1039. }
  1040. }
  1041. },
  1042. _stagePay(data, order) {
  1043. const fields = [];
  1044. for (const [i, o] of order.entries()) {
  1045. const sPrefix = 'r' + o + '_';
  1046. const tPrefix = 'as' + i + '_';
  1047. fields.push({ source: sPrefix + 'tp', target: tPrefix + 'tp' });
  1048. }
  1049. for (const d of data) {
  1050. for (const f of fields) {
  1051. d[f.target] = d[f.source];
  1052. }
  1053. }
  1054. },
  1055. fun(ctx, data, fieldsKey, options, csRela) {
  1056. if (!ctx.tender || !ctx.stage) return;
  1057. if (!csRela.tplDefine) return;
  1058. const asDefine = csRela.tplDefine.audit_select;
  1059. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  1060. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  1061. const order = [];
  1062. if (asCustom) {
  1063. for (const asc of asCustom) {
  1064. order.push(asc.sort || asc.order);
  1065. }
  1066. }
  1067. for (const t of options.table) {
  1068. switch (t) {
  1069. case 'mem_stage_bills_compare':
  1070. this._stageBillsCompare(data[t], order);
  1071. break;
  1072. case 'mem_stage_pos_compare':
  1073. this._stagePosCompare(data[t], order);
  1074. break;
  1075. case 'mem_stage_pay':
  1076. this._stagePay(data[t], order);
  1077. break;
  1078. }
  1079. }
  1080. },
  1081. };
  1082. const datetimeFormat = {
  1083. name: '日期格式化',
  1084. hint: '参见帮助',
  1085. defaultSetting: {
  1086. tables: [
  1087. { name: 'mem_stage_bonus', fields: [
  1088. { field: 'real_time', formatter: 'yyyy-MM-DD' },
  1089. { field: 'create_time', formatter: 'yyyy-MM-DD' },
  1090. ] },
  1091. ],
  1092. },
  1093. fun(ctx, data, fieldsKey, options, csRela) {
  1094. if (!options.tables) return;
  1095. const tables = options.tables instanceof Array ? options.tables : [options.tables];
  1096. if (tables.length === 0) return;
  1097. for (const table of tables) {
  1098. const formatData = data[table.name];
  1099. for (const fd of formatData) {
  1100. for (const f of table.fields) {
  1101. fd[f.field] = moment(fd[f.field]).format(f.formatter);
  1102. }
  1103. }
  1104. }
  1105. },
  1106. };
  1107. const gatherSelectConverse = {
  1108. name: '交叉汇总数据',
  1109. hint: '需搭配 用户交互--汇总标段选择 一起使用',
  1110. defaultSetting: {
  1111. table: ['mem_gather_stage_bills'],
  1112. },
  1113. _commonConverse(helper, data, count) {
  1114. const result = [];
  1115. const reg = new RegExp('^t_[0-9]+_');
  1116. for (let i = 0; i < count; i++) {
  1117. const curReg = new RegExp('^t_' + i + '_');
  1118. for (const [i, d] of data.entries()) {
  1119. const nd = { cross_index: i + 1 };
  1120. for (const prop in d) {
  1121. if (reg.test(prop)) {
  1122. if (curReg.test(prop)) {
  1123. nd[prop.replace(curReg, 't_')] = d[prop];
  1124. }
  1125. } else {
  1126. nd[prop] = d[prop];
  1127. }
  1128. }
  1129. result.push(nd);
  1130. }
  1131. }
  1132. // for (const d of data) {
  1133. // for (let i = 0; i < count; i++) {
  1134. // const curReg = new RegExp('^t_' + i + '_');
  1135. // const nd = {};
  1136. // for (const prop in d) {
  1137. // if (reg.test(prop)) {
  1138. // if (curReg.test(prop)) {
  1139. // nd[prop.replace(curReg, 't_')] = d[prop];
  1140. // }
  1141. // } else {
  1142. // nd[prop] = d[prop];
  1143. // }
  1144. // }
  1145. // result.push(nd);
  1146. // }
  1147. // }
  1148. return result;
  1149. },
  1150. fun(ctx, data, fieldsKey, options, csRela) {
  1151. if (!csRela.tplDefine) return;
  1152. const gsDefine = csRela.tplDefine.gather_select;
  1153. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1154. const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;
  1155. const count = gsDefine.setting.special
  1156. ? gsCustom.tenders.length - gsDefine.setting.special.length
  1157. : gsCustom.tenders.length;
  1158. for (const t of options.table) {
  1159. switch (t) {
  1160. case 'mem_gather_stage_bills':
  1161. case 'mem_gather_stage_pos':
  1162. case 'mem_gather_stage_pay':
  1163. case 'mem_gather_deal_bills':
  1164. case 'mem_union_data':
  1165. data[t] = this._commonConverse(ctx.helper, data[t], count);
  1166. break;
  1167. }
  1168. }
  1169. },
  1170. };
  1171. const sortPos = {
  1172. name: '计量单元排序',
  1173. hint: '',
  1174. defaultSetting: {
  1175. bills: 'mem_stage_bills',
  1176. pos: 'mem_stage_pos',
  1177. },
  1178. fun(ctx, data, fieldsKey, options, csRela) {
  1179. if (!options || !options.bills || !options.pos) return;
  1180. const bills = data[options.bills];
  1181. const pos = data[options.pos];
  1182. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1183. const billsIndex = {};
  1184. const findBillsIndex = function(billsId) {
  1185. if (!billsIndex[billsId]) {
  1186. billsIndex[billsId] = ctx.helper._.findIndex(bills, function(x) {
  1187. return x.id === billsId;
  1188. });
  1189. }
  1190. return billsIndex[billsId];
  1191. };
  1192. pos.sort(function(x, y) {
  1193. const xIndex = findBillsIndex(x.lid) * 1000 + x.porder;
  1194. const yIndex = findBillsIndex(y.lid) * 1000 + y.porder;
  1195. return xIndex - yIndex;
  1196. });
  1197. },
  1198. };
  1199. const unionPos = {
  1200. name: '拼接计量单元',
  1201. hint: '',
  1202. defaultSetting: {
  1203. bills: 'mem_stage_bills',
  1204. pos: 'mem_stage_pos',
  1205. },
  1206. fun(ctx, data, fieldsKey, options, csRela) {
  1207. if (!options || !options.bills || !options.pos) return;
  1208. const bills = data[options.bills];
  1209. const pos = data[options.pos];
  1210. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1211. const posIndex = new PosIndex({
  1212. id: 'id',
  1213. masterId: 'lid',
  1214. order: 'porder',
  1215. });
  1216. posIndex.loadDatas(pos);
  1217. const unionData = [];
  1218. for (const b of bills) {
  1219. unionData.push(b);
  1220. const posRange = posIndex.getLedgerPos(b.id);
  1221. b.posCount = posRange ? posRange.length : 0;
  1222. if (!posRange || posRange.length === 0) continue;
  1223. for (const p of posRange) {
  1224. unionData.push(p);
  1225. }
  1226. }
  1227. data[options.bills] = unionData;
  1228. },
  1229. };
  1230. const splitXmjCode = {
  1231. name: '拆分项目节编号',
  1232. hint: '',
  1233. defaultSetting: {
  1234. table: 'mem_stage_bills',
  1235. type: '11', // '18', '18-0-1', '18-1', '18-1-1'
  1236. code: 'code',
  1237. targetField: ['xiang', 'mu', 'jie', 'ximu'],
  1238. },
  1239. // 11标准
  1240. _split11Std(data, options, helper) {
  1241. for (const d of data) {
  1242. if (!d[options.code]) continue;
  1243. const splitCodes = d[options.code].split('-');
  1244. if (d.level === 2) {
  1245. const field = options.targetField[0];
  1246. if (field && splitCodes.length > 1) {
  1247. d[field] = helper.transFormToChinese(splitCodes[1]);
  1248. }
  1249. } else if (d.level === 3) {
  1250. const field = options.targetField[1];
  1251. if (field && splitCodes.length > 2) {
  1252. d[field] = splitCodes[2];
  1253. }
  1254. } else if (d.level === 4) {
  1255. const field = options.targetField[2];
  1256. if (field && splitCodes.length > 3) {
  1257. d[field] = splitCodes[3];
  1258. }
  1259. } else if (d.level > 4) {
  1260. const field = options.targetField[3];
  1261. if (field && splitCodes.length > 4) {
  1262. d[field] = splitCodes.reduce(function(s, v, i) { return i >= 4 ? (s ? s + '-' + v : v + '') : ''; });
  1263. }
  1264. }
  1265. }
  1266. },
  1267. _getParentByLevel(data, node, level) {
  1268. if (node.full_path) {
  1269. const pid = node.full_path.split('-')[level - 1];
  1270. return data.find(function(x) {
  1271. return x.level === level && x.ledger_id == pid;
  1272. });
  1273. }
  1274. return null;
  1275. },
  1276. // 18标准
  1277. _split18Std(data, options, helper) {
  1278. const types = options.type.split('-');
  1279. const isSplitSub = !!(types[1] && types[1] === '1');
  1280. const isKeepGD = !!(types[2] && types[2] === '1');
  1281. for (const d of data) {
  1282. if (!d[options.code]) continue;
  1283. let target = '';
  1284. if (d.level === 2) {
  1285. target = options.targetField[0] ? options.targetField[0] : '';
  1286. } else if (d.level === 3) {
  1287. target = options.targetField[1] ? options.targetField[1] : '';
  1288. } else if (d.level === 4) {
  1289. target = options.targetField[2] ? options.targetField[2] : '';
  1290. } else if (d.level > 4) {
  1291. target = options.targetField[3] ? options.targetField[3] : '';
  1292. }
  1293. if (!target) continue;
  1294. if (helper.check18MainCode(d[options.code])) {
  1295. const parent = helper._.find(data, { ledger_id: d.ledger_pid });
  1296. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1297. d[target] = !splitParent
  1298. ? d[options.code]
  1299. : d[options.code].replace(splitParent[options.code], '');
  1300. } else if (helper.check18SubCode(d[options.code]) && isSplitSub) {
  1301. const parent = helper._.find(data, { ledger_id: d.ledger_pid });
  1302. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1303. d[target] = !splitParent || helper.check18MainCode(splitParent.code)
  1304. ? d[options.code]
  1305. : d[options.code].replace(splitParent[options.code], '');
  1306. } else {
  1307. d[target] = d[options.code];
  1308. }
  1309. if (!isKeepGD) d[target] = d[target].replace(/^G[D]?/, '');
  1310. }
  1311. },
  1312. // 根据层次填入
  1313. _splitDefault(data, options) {
  1314. for (const d of data) {
  1315. if (!d[options.code]) continue;
  1316. if (d.level === 2) {
  1317. if (options.targetField[0]) d[options.targetField[0]] = d[options.code];
  1318. } else if (d.level === 3) {
  1319. if (options.targetField[1]) d[options.targetField[1]] = d[options.code];
  1320. } else if (d.level === 4) {
  1321. if (options.targetField[2]) d[options.targetField[2]] = d[options.code];
  1322. } else if (d.level > 4) {
  1323. if (options.targetField[3]) d[options.targetField[3]] = d[options.code];
  1324. }
  1325. }
  1326. },
  1327. fun(ctx, data, fieldsKey, options, csRela) {
  1328. if (!options || !options.table || !options.code || !options.targetField) return;
  1329. if (!data[options.table]) return;
  1330. if (options.type === '11') {
  1331. this._split11Std(data[options.table], options, ctx.helper);
  1332. } else if (/^18(-[0,1]){0,2}$/.test(options.type)) {
  1333. this._split18Std(data[options.table], options, ctx.helper);
  1334. } else {
  1335. this._splitDefault(data[options.table], options);
  1336. }
  1337. },
  1338. };
  1339. const stageSelectConverse = {
  1340. name: '交叉多期汇总数据',
  1341. hint: '需搭配 用户交互--单标段多期汇总选择 一起使用',
  1342. defaultSetting: {
  1343. table: ['mem_stage_sum_bills'],
  1344. },
  1345. _commonConverse(helper, data, stages) {
  1346. const result = [];
  1347. const reg = new RegExp('^s_[0-9]+_');
  1348. for (const s of stages) {
  1349. const curReg = new RegExp('^s_' + s + '_');
  1350. for (const [i, d] of data.entries()) {
  1351. const nd = { cross_index: i + 1, s_order: s };
  1352. for (const prop in d) {
  1353. if (reg.test(prop)) {
  1354. if (curReg.test(prop)) {
  1355. nd[prop.replace(curReg, 's_')] = d[prop];
  1356. }
  1357. } else {
  1358. nd[prop] = d[prop];
  1359. }
  1360. }
  1361. result.push(nd);
  1362. }
  1363. }
  1364. return result;
  1365. },
  1366. fun(ctx, data, fieldsKey, options, csRela) {
  1367. if (!csRela.tplDefine) return;
  1368. const gsDefine = csRela.tplDefine.stage_select;
  1369. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1370. const gsCustom = csRela.cDefine ? csRela.cDefine.stage_select : null;
  1371. if (gsCustom) {
  1372. for (const t of options.table) {
  1373. switch (t) {
  1374. case 'mem_stage_sum_bills':
  1375. case 'mem_stage_sum_pos':
  1376. case 'mem_stage_sum_pay':
  1377. case 'mem_union_data':
  1378. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.stages);
  1379. break;
  1380. }
  1381. }
  1382. } else {
  1383. console.log('gsCustom is null!');
  1384. }
  1385. },
  1386. };
  1387. const materialSumSelectConverse = {
  1388. name: '交叉【多期材差汇总】数据',
  1389. hint: '需搭配 用户交互【材差-多期汇总选择】 一起使用',
  1390. defaultSetting: {
  1391. table: ['mem_material_sum_gl'],
  1392. },
  1393. _commonConverse(helper, data, materials) {
  1394. const result = [];
  1395. const reg = new RegExp('^m_[0-9]+_');
  1396. for (const s of materials) {
  1397. const curReg = new RegExp('^m_' + s + '_');
  1398. for (const [i, d] of data.entries()) {
  1399. const nd = { cross_index: i + 1, s_order: s };
  1400. for (const prop in d) {
  1401. if (reg.test(prop)) {
  1402. if (curReg.test(prop)) {
  1403. nd[prop.replace(curReg, 'm_')] = d[prop];
  1404. }
  1405. } else {
  1406. nd[prop] = d[prop];
  1407. }
  1408. }
  1409. result.push(nd);
  1410. }
  1411. }
  1412. return result;
  1413. },
  1414. fun(ctx, data, fieldsKey, options, csRela) {
  1415. if (!csRela.tplDefine) return;
  1416. const gsDefine = csRela.tplDefine.material_sum_select;
  1417. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1418. const gsCustom = csRela.cDefine ? csRela.cDefine.material_sum_select : null;
  1419. if (gsCustom) {
  1420. for (const t of options.table) {
  1421. switch (t) {
  1422. case 'mem_material_sum_gl':
  1423. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.materials);
  1424. break;
  1425. }
  1426. }
  1427. } else {
  1428. console.log('gsCustom is null!');
  1429. }
  1430. },
  1431. };
  1432. const loadCooperationData = {
  1433. name: '加载协作数据',
  1434. hint: '须使用台账、计量审批流程、协作数据作为基础',
  1435. defaultSetting: {
  1436. table: 'mem_stage_im_zl',
  1437. co_sign: [0, 1, 2, 3],
  1438. },
  1439. _findSign(relaId, stageCooperation, auditor) {
  1440. if (!stageCooperation) return '';
  1441. if (relaId.length > 0) {
  1442. for (const id of relaId) {
  1443. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1444. if (c) return c.sign_path;
  1445. }
  1446. return '';
  1447. }
  1448. return '';
  1449. },
  1450. _findCooperation(relaId, stageCooperation, auditor) {
  1451. if (!stageCooperation) return null;
  1452. if (relaId.length > 0) {
  1453. for (const id of relaId) {
  1454. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1455. if (c) return c;
  1456. }
  1457. return null;
  1458. }
  1459. return null;
  1460. },
  1461. _completeSign(auditor) {
  1462. if (!auditor) return '';
  1463. if (auditor.ass_user_id) {
  1464. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1465. } else {
  1466. if (!auditor.end_time) return '';
  1467. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1468. }
  1469. },
  1470. _loadImCooperationData(ctx, data, options, csRela) {
  1471. let coSignOrder = [];
  1472. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1473. if (csRela.cDefine.audit_select) {
  1474. for (const asc of csRela.cDefine.audit_select) {
  1475. coSignOrder.push(asc.order);
  1476. }
  1477. }
  1478. } else {
  1479. coSignOrder = options.co_sign || [];
  1480. }
  1481. const stageCooperation = [];
  1482. let finish = true;
  1483. for (const sa of data.stage_audit) {
  1484. if (sa.end_time) stageCooperation.push(data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; }));
  1485. if (finish && !sa.end_time) finish = false;
  1486. }
  1487. for (const d of data[options.table]) {
  1488. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1489. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1490. d.cooperation = [], d.company = [];
  1491. for (const [i, sa] of data.stage_audit.entries()) {
  1492. const co = this._findCooperation(relaId, stageCooperation[i], sa);
  1493. d.cooperation.push(this._findSign(relaId, stageCooperation[i], sa));
  1494. d.company.push(co ? co.company : '');
  1495. }
  1496. if (finish) {
  1497. for (const [i, cs] of coSignOrder.entries()) {
  1498. if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {
  1499. d['co_sign' + (i + 1)] = d.cooperation[cs] || this._completeSign(data.stage_audit[cs]);
  1500. d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';
  1501. d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;
  1502. d['co_company' + (i + 1)] = d.company[cs] || data.stage_audit[cs].name;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. },
  1508. _findStageAuditAss(relaId, stageAuditAss, auditor) {
  1509. if (!stageAuditAss || stageAuditAss.length === 0) return null;
  1510. if (relaId.length > 0) {
  1511. for (const id of relaId) {
  1512. const c = stageAuditAss.find(x => { return x.ass_ledger_id.indexOf(id + '') >= 0; });
  1513. if (c) return c;
  1514. }
  1515. return null;
  1516. }
  1517. return null;
  1518. },
  1519. _loadImAssistData(ctx, data, options, csRela) {
  1520. let coSignOrder = [];
  1521. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1522. if (csRela.cDefine.audit_select) {
  1523. for (const asc of csRela.cDefine.audit_select) {
  1524. coSignOrder.push(asc.order);
  1525. }
  1526. }
  1527. } else {
  1528. coSignOrder = options.co_sign || [];
  1529. }
  1530. const stageAuditAss = [];
  1531. let finish = true;
  1532. for (const sa of data.stage_audit) {
  1533. if (sa.end_time) stageAuditAss.push(data.mem_stage_audit_ass.filter(x => { return x.user_id === sa.aid; }));
  1534. if (finish && !sa.end_time) finish = false;
  1535. }
  1536. for (const d of data[options.table]) {
  1537. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1538. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1539. d.ass = [];
  1540. for (const [i, sa] of data.stage_audit.entries()) {
  1541. const co = this._findStageAuditAss(relaId, stageAuditAss[i], sa);
  1542. d.ass.push(co);
  1543. }
  1544. for (const [i, cs] of coSignOrder.entries()) {
  1545. if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {
  1546. d['co_sign' + (i + 1)] = this._completeSign(d.ass[cs] || data.stage_audit[cs]);
  1547. d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';
  1548. d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;
  1549. d['co_name' + (i + 1)] = d.ass[cs] ? d.ass[cs].name : data.stage_audit[cs].name;
  1550. d['co_company' + (i + 1)] = d.ass[cs] ? d.ass[cs].company : data.stage_audit[cs].company;
  1551. }
  1552. }
  1553. }
  1554. },
  1555. fun(ctx, data, fieldsKey, options, csRela) {
  1556. if (!options || !options.table) return;
  1557. if (!data[options.table]) return;
  1558. if (!data.mem_stage_bills) return;
  1559. if (!data.stage_audit) return;
  1560. if (!data.ledger_cooperation && !data.mem_stage_audit_ass) return;
  1561. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) {
  1562. if (data.mem_stage_audit_ass) {
  1563. this._loadImAssistData(ctx, data, options, csRela);
  1564. } else {
  1565. this._loadImCooperationData(ctx, data, options, csRela);
  1566. }
  1567. }
  1568. },
  1569. };
  1570. const signSelect = {
  1571. name: '签字',
  1572. hint: '协作模式下,才需使用,其他情况勿需使用',
  1573. defaultSetting: {
  1574. table: 'mem_stage_im_zl',
  1575. },
  1576. _findSign(relaId, stageCooperation, auditor) {
  1577. if (!stageCooperation) return '';
  1578. if (relaId.length > 0) {
  1579. for (const id of relaId) {
  1580. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1581. if (c) return c.sign_path;
  1582. }
  1583. return '';
  1584. }
  1585. return '';
  1586. },
  1587. _loadImCooperationData(ctx, data, options, csRela) {
  1588. const stageCooperation = {};
  1589. for (const sa of data.stage_audit) {
  1590. if (sa.end_time) stageCooperation[sa.aid] = data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; });
  1591. }
  1592. for (const d of data[options.table]) {
  1593. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1594. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1595. for (const ss of data.sign_select) {
  1596. if (!ss.co_sign) ss.co_sign = [];
  1597. ss.co_sign.push(this._findSign(relaId, stageCooperation[ss.id], ss) || 'public/upload/sign/' + ss.sign_path);
  1598. }
  1599. }
  1600. },
  1601. fun(ctx, data, fieldsKey, options, csRela) {
  1602. if (!options || !options.table) return;
  1603. if (!data[options.table]) return;
  1604. if (!data.mem_stage_bills) return;
  1605. if (!data.stage_audit) return;
  1606. if (!data.ledger_cooperation) return;
  1607. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) { this._loadImCooperationData(ctx, data, options, csRela); }
  1608. },
  1609. };
  1610. const getChangeLedger = {
  1611. name: '获取 工程变更台账表 数据',
  1612. hint: '须提供mem_stage_bills, mem_stage_change_bills作为数据支撑, 结果写入mem_stage_change_ledger',
  1613. defaultSetting: {
  1614. mergeName: true,
  1615. calc: '', // split: 均分
  1616. level: '2-leaf', // 空表示表示保留所有的项目节,2-leaf表示保留第2层和最底层项目节,默认为空
  1617. },
  1618. _getSource(ctx, bills, changeList) {
  1619. const result = [];
  1620. for (const b of bills) {
  1621. if (b.b_code) continue;
  1622. const l = {
  1623. id: b.id,
  1624. code: b.code, name: b.name, unit: b.unit,
  1625. ledger_id: b.ledger_id, ledger_pid: b.ledger_pid, level: b.level, full_path: b.full_path,
  1626. total_price: b.total_price, deal_tp: b.deal_tp,
  1627. contract_tp: b.contract_tp, qc_tp: b.qc_tp, gather_tp: b.gather_tp,
  1628. end_contract_tp: b.end_contract_tp, end_qc_tp: b.end_qc_tp, end_gather_tp: b.end_gather_tp,
  1629. };
  1630. result.push(l);
  1631. const childGcl = bills.filter(x => { return x.ledger_pid === b.ledger_id && x.b_code});
  1632. if (!childGcl || childGcl.length === 0) continue;
  1633. const gcl = bills.filter(x => { return x.full_path.indexOf(l.full_path + '-') === 0 && x.b_code; });
  1634. if (gcl.length === 0) continue;
  1635. const gclIds = gcl.map(x => { return x.id; });
  1636. const changes = changeList.filter(x => { return gclIds.indexOf(x.lid) >= 0; });
  1637. if (!changes || changes.length === 0) continue;
  1638. l.changes = [];
  1639. for (const c of changes) {
  1640. let mc = l.changes.find(x => { return x.cid === c.cid; });
  1641. if (!mc) {
  1642. mc = {
  1643. cid: c.cid,
  1644. c_code: c.c_code, c_name: c.c_name,
  1645. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1646. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time * 1000,
  1647. c_bills: [],
  1648. };
  1649. l.changes.push(mc);
  1650. }
  1651. mc.c_bills.push(c);
  1652. }
  1653. }
  1654. return result;
  1655. },
  1656. _filterChangeLedger(ctx, changeLedger, options) {
  1657. const result = [];
  1658. for (const f of changeLedger) {
  1659. const calc = changeLedger.filter(x => { return x.full_path.indexOf(f.full_path) === 0 && x.changes && x.changes.length > 0; });
  1660. if (!calc || calc.length === 0) continue;
  1661. if (options.level === '2-leaf')
  1662. if (f.level !== 2 && (!f.changes || f.changes.length === 0)) continue;
  1663. result.push({
  1664. code: f.code, name: f.name, unit: f.unit,
  1665. ledger_id: f.ledger_id, ledger_pid: f.ledger_pid, level: f.level, full_path: f.full_path,
  1666. total_price: f.total_price, deal_tp: f.deal_tp,
  1667. contract_tp: f.contract_tp, qc_tp: f.qc_tp, gather_tp: f.gather_tp,
  1668. end_contract_tp: f.end_contract_tp, end_qc_tp: f.end_qc_tp, end_gather_tp: f.end_gather_tp,
  1669. });
  1670. if (f.changes && f.changes.length > 0) {
  1671. for (const c of f.changes) {
  1672. const fc = {
  1673. c_code: c.c_code, c_name: c.c_name,
  1674. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1675. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time,
  1676. };
  1677. if (options.mergeName) fc.name = fc.c_name;
  1678. result.push(fc);
  1679. }
  1680. }
  1681. }
  1682. return result;
  1683. },
  1684. fun(ctx, data, fieldsKey, options, csRela) {
  1685. if (!options) options = {};
  1686. if (!data.mem_stage_bills) return;
  1687. if (!data.mem_stage_change_bills) return;
  1688. const changeLedger = this._getSource(ctx, data.mem_stage_bills, data.mem_stage_change_bills);
  1689. data.mem_stage_change_ledger = this._filterChangeLedger(ctx, changeLedger, options);
  1690. },
  1691. };
  1692. const treeFilter = {
  1693. name: '树结构数据过滤',
  1694. hint: '过滤时,当父项符合条件时,会自动包含子项',
  1695. defaultSetting: {
  1696. table: 'mem_stage_bills',
  1697. type: 'match/filter',
  1698. subType: 'all/posterity',
  1699. condition: [
  1700. { field: 'name', type: 'str', value: '建筑项目管理费', isPart: true },
  1701. ],
  1702. },
  1703. _checkPath(matchPath, full_path) {
  1704. for (const mp of matchPath) {
  1705. if (full_path.indexOf(mp) === 0) return true;
  1706. }
  1707. return false;
  1708. },
  1709. fun(ctx, data, fieldsKey, options, csRela) {
  1710. if (!options || !options.table || !options.condition || !options.type) return;
  1711. const fData = data[options.table],
  1712. self = this;
  1713. if (!fData || fData.length === 0) return;
  1714. if (!fData[0].full_path) return;
  1715. valueCheck.loadTypeFun(options.condition);
  1716. const matchPath = [];
  1717. for (const d of fData) {
  1718. if (this._checkPath(matchPath, d.full_path)) continue;
  1719. if (valueCheck.checkData(ctx, d, options.condition)) {
  1720. matchPath.push(d.full_path + (options.subType === 'posterity' ? '-' : ''));
  1721. }
  1722. }
  1723. data[options.table] = fData.filter(x => {
  1724. const match = self._checkPath(matchPath, x.full_path) && (options.subType === 'leaf' ? x.is_leaf : true);
  1725. return options.type === 'match' ? match : !match;
  1726. });
  1727. },
  1728. };
  1729. const gatherGcl2 = {
  1730. name: '工程量清单汇总2',
  1731. hint: '会生成新的工程量清单指标&相关项目节指标',
  1732. defaultSetting: {
  1733. billsTable: 'mem_stage_bills',
  1734. posTable: 'mem_stage_pos',
  1735. dealTable: 'deal_bills',
  1736. },
  1737. fun(ctx, data, fieldsKey, options, csRela) {
  1738. if (!options || !options.billsTable || !options.posTable) return;
  1739. const bills = data[options.billsTable];
  1740. if (!bills) return;
  1741. const pos = data[options.posTable];
  1742. if (!pos) return;
  1743. const deal = data[options.dealTable];
  1744. const gclGatherModel = require('./gcl_gather').gclGather;
  1745. const gatherUtil = new gclGatherModel(ctx);
  1746. gatherUtil.gather(bills, pos, deal);
  1747. data.mem_gcl_gather_bills = gatherUtil.gclList;
  1748. data.mem_gcl_gather_xmj = gatherUtil.leafXmjs;
  1749. }
  1750. };
  1751. const gatherMaterialGl = {
  1752. name: '分类汇总调差工料',
  1753. hint: '根据工程量清单分类汇总调差工料,要求先使用【工程量清单汇总2】处理得到工程量清单数据,并引入【调差工料明细】',
  1754. _relaGl(source, data) {
  1755. if (!source) return;
  1756. const gl = source.find(x => {return x.id === data.mb_id;});
  1757. if (gl) {
  1758. data.gl_code = gl.code;
  1759. data.gl_name = gl.name;
  1760. data.gl_unit = gl.unit;
  1761. data.gl_spec = gl.spec;
  1762. data.gl_base_price = gl.base_price;
  1763. data.gl_base_times = gl.base_times;
  1764. data.gl_msg_tp = gl.msg_tp;
  1765. data.gl_msg_times = gl.msg_times;
  1766. data.gl_msg_spread = gl.msg_spread;
  1767. data.gl_m_up_risk = gl.m_up_risk;
  1768. data.gl_m_down_risk = gl.m_down_risk;
  1769. data.gl_m_spread = gl.m_spread;
  1770. }
  1771. },
  1772. fun(ctx, data, fieldsKey, options, csRela) {
  1773. if (!data.mem_gcl_gather_bills || !data.mem_gcl_gather_xmj || !data.mem_material_gl_detail) return;
  1774. const result = [];
  1775. for (const d of data.mem_material_gl_detail) {
  1776. const xmj = data.mem_gcl_gather_xmj.find(x => {
  1777. return d.gcl_id === x.org_gcl_id && d.xmj_id === x.id && (!d.mx_id || d.mx_id === x.mx_id);
  1778. });
  1779. if (!xmj) continue;
  1780. const gcl = data.mem_gcl_gather_bills.find(x => {
  1781. return x.id === xmj.gcl_id;
  1782. });
  1783. if (!gcl) continue;
  1784. const gd = result.find(x => {
  1785. return x.gather_gcl_id === gcl.id && x.mb_id === d.mb_id;
  1786. });
  1787. if (!gd) {
  1788. const newGd = {
  1789. gather_gcl_id: gcl.id,
  1790. mb_id: d.mb_id, gather_qty: d.gather_qty, quantity: d.quantity,
  1791. b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price,
  1792. cur_contract_qty: gcl.contract_qty, cur_contract_tp: gcl.contract_tp,
  1793. cur_qc_qty: gcl.qc_qty, cur_qc_tp: gcl.qc_tp,
  1794. cur_gather_qty: gcl.gather_qty, cur_gather_tp: gcl.gather_tp,
  1795. };
  1796. this._relaGl(data.mem_material_gl, newGd);
  1797. result.push(newGd);
  1798. } else {
  1799. gd.gather_qty = ctx.helper.add(gd.gather_qty, d.gather_qty);
  1800. gd.quantity = ctx.helper.add(gd.quantity, d.quantity);
  1801. }
  1802. }
  1803. data.mem_material_gl_detail = result;
  1804. }
  1805. };
  1806. const masterCross = {
  1807. name: '主从交叉(汇总)数据-通用',
  1808. hint: '此版本,要求表必须是主从关系',
  1809. defaultSetting: {
  1810. main: {
  1811. tableName: 'mem_material_gather_bills',
  1812. prefix: 'm_',
  1813. fields: ['b_code', 'name', 'units', 'unit_price', 'quantity', 'total_price', 'gather_qty', 'gather_tp', 'jiacha'],
  1814. },
  1815. sub: {
  1816. tableName: 'mem_material_gather_gl',
  1817. masterKey: 'id', subKey: 'gather_gcl_id', gatherKey: 'mb_id',
  1818. prefix: 's_',
  1819. fields: ['code', 'name', 'units'],
  1820. quoteFields: ['quantity'],
  1821. gatherFields: [],
  1822. },
  1823. target: 'master_converse',
  1824. base: 'main',
  1825. },
  1826. _converse: function(ctx, options, mainTable, subTable) {
  1827. const data = [];
  1828. const converseData = [];
  1829. subTable.forEach(x => {
  1830. let cd = converseData.find(y => { return x[options.sub.gatherKey] === y[options.sub.gatherKey]});
  1831. if (cd) return;
  1832. cd = {};
  1833. cd[options.sub.gatherKey] = x[options.sub.gatherKey];
  1834. for (const f of options.sub.fields) {
  1835. cd[f] = x[f];
  1836. }
  1837. converseData.push(cd);
  1838. });
  1839. for (const [i, m] of mainTable.entries()) {
  1840. const temp = { cross_index: i+1 };
  1841. for (const f of options.main.fields) {
  1842. temp[options.main.prefix + f] = m[f];
  1843. }
  1844. const rela = subTable.filter(x => { return x[options.sub.subKey] === m[options.sub.masterKey]; });
  1845. if (options.base === 'sub' && rela.length === 0) continue;
  1846. for (const cd of converseData) {
  1847. const nd = JSON.parse(JSON.stringify(temp));
  1848. nd.quote = false;
  1849. for (const prop in cd) {
  1850. nd[options.sub.prefix + prop] = cd[prop];
  1851. }
  1852. rela.forEach(x => {
  1853. if (x[options.sub.gatherKey] !== cd[options.sub.gatherKey]) return;
  1854. if (!nd.quote) {
  1855. for (const f of options.sub.quoteFields) {
  1856. nd[options.sub.prefix + f] = x[f];
  1857. }
  1858. nd.quote = true;
  1859. }
  1860. for (const f of options.sub.gatherFields) {
  1861. nd[options.sub.prefix + f] = ctx.helper.add(nd[options.sub.prefix + f], x[f]);
  1862. }
  1863. });
  1864. data.push(nd);
  1865. }
  1866. }
  1867. return data;
  1868. },
  1869. fun(ctx, data, fieldsKey, options, csRela) {
  1870. if (!options || !options.main || !options.sub || !options.target) return;
  1871. const mainTable = data[options.main.tableName], subTable = data[options.sub.tableName];
  1872. if (!mainTable || !subTable) return;
  1873. data[options.target] = this._converse(ctx, options, mainTable, subTable);
  1874. },
  1875. };
  1876. const analysisObj = {
  1877. changeSort,
  1878. gatherGcl,
  1879. sortGcl,
  1880. gatherChapter,
  1881. join,
  1882. getChapterCode,
  1883. filter,
  1884. union,
  1885. gatherStagePay,
  1886. addSumChapter,
  1887. auditSelect,
  1888. datetimeFormat,
  1889. gatherSelectConverse,
  1890. stageSelectConverse,
  1891. materialSumSelectConverse,
  1892. sortPos,
  1893. unionPos,
  1894. splitXmjCode,
  1895. loadCooperationData,
  1896. getChangeLedger,
  1897. treeFilter,
  1898. gatherGcl2,
  1899. gatherMaterialGl,
  1900. masterCross
  1901. };
  1902. const analysisDefine = (function(obj) {
  1903. const result = [];
  1904. for (const o in obj) {
  1905. const analysis = obj[o];
  1906. if (analysis.name && analysis.fun) {
  1907. result.push({
  1908. name: analysis.name,
  1909. key: o,
  1910. hint: analysis.hint,
  1911. intro: analysis.intro,
  1912. url: '/help?m=report&s=analysis&d=' + o,
  1913. });
  1914. }
  1915. }
  1916. return result;
  1917. })(analysisObj);
  1918. module.exports = {
  1919. analysisObj,
  1920. analysisDefine,
  1921. };