rpt_data_analysis.js 80 KB

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