rpt_data_analysis.js 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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_ratio), 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_pay':
  1376. case 'mem_union_data':
  1377. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.stages);
  1378. break;
  1379. }
  1380. }
  1381. } else {
  1382. console.log('gsCustom is null!');
  1383. }
  1384. },
  1385. };
  1386. const materialSumSelectConverse = {
  1387. name: '交叉【多期材差汇总】数据',
  1388. hint: '需搭配 用户交互【材差-多期汇总选择】 一起使用',
  1389. defaultSetting: {
  1390. table: ['mem_material_sum_gl'],
  1391. },
  1392. _commonConverse(helper, data, materials) {
  1393. const result = [];
  1394. const reg = new RegExp('^m_[0-9]+_');
  1395. for (const s of materials) {
  1396. const curReg = new RegExp('^m_' + s + '_');
  1397. for (const [i, d] of data.entries()) {
  1398. const nd = { cross_index: i + 1, s_order: s };
  1399. for (const prop in d) {
  1400. if (reg.test(prop)) {
  1401. if (curReg.test(prop)) {
  1402. nd[prop.replace(curReg, 'm_')] = d[prop];
  1403. }
  1404. } else {
  1405. nd[prop] = d[prop];
  1406. }
  1407. }
  1408. result.push(nd);
  1409. }
  1410. }
  1411. return result;
  1412. },
  1413. fun(ctx, data, fieldsKey, options, csRela) {
  1414. if (!csRela.tplDefine) return;
  1415. const gsDefine = csRela.tplDefine.material_sum_select;
  1416. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1417. const gsCustom = csRela.cDefine ? csRela.cDefine.material_sum_select : null;
  1418. if (gsCustom) {
  1419. for (const t of options.table) {
  1420. switch (t) {
  1421. case 'mem_material_sum_gl':
  1422. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.materials);
  1423. break;
  1424. }
  1425. }
  1426. } else {
  1427. console.log('gsCustom is null!');
  1428. }
  1429. },
  1430. };
  1431. const loadCooperationData = {
  1432. name: '加载协作数据',
  1433. hint: '须使用台账、计量审批流程、协作数据作为基础',
  1434. defaultSetting: {
  1435. table: 'mem_stage_im_zl',
  1436. co_sign: [0, 1, 2, 3],
  1437. },
  1438. _findSign(relaId, stageCooperation, auditor) {
  1439. if (!stageCooperation) return '';
  1440. if (relaId.length > 0) {
  1441. for (const id of relaId) {
  1442. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1443. if (c) return c.sign_path;
  1444. }
  1445. return '';
  1446. }
  1447. return '';
  1448. },
  1449. _findCooperation(relaId, stageCooperation, auditor) {
  1450. if (!stageCooperation) return null;
  1451. if (relaId.length > 0) {
  1452. for (const id of relaId) {
  1453. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1454. if (c) return c;
  1455. }
  1456. return null;
  1457. }
  1458. return null;
  1459. },
  1460. _completeSign(auditor) {
  1461. if (!auditor) return '';
  1462. if (auditor.ass_user_id) {
  1463. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1464. } else {
  1465. if (!auditor.end_time) return '';
  1466. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1467. }
  1468. },
  1469. _loadImCooperationData(ctx, data, options, csRela) {
  1470. let coSignOrder = [];
  1471. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1472. if (csRela.cDefine.audit_select) {
  1473. for (const asc of csRela.cDefine.audit_select) {
  1474. coSignOrder.push(asc.order);
  1475. }
  1476. }
  1477. } else {
  1478. coSignOrder = options.co_sign || [];
  1479. }
  1480. const stageCooperation = [];
  1481. let finish = true;
  1482. for (const sa of data.stage_audit) {
  1483. if (sa.end_time) stageCooperation.push(data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; }));
  1484. if (finish && !sa.end_time) finish = false;
  1485. }
  1486. for (const d of data[options.table]) {
  1487. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1488. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1489. d.cooperation = [], d.company = [];
  1490. for (const [i, sa] of data.stage_audit.entries()) {
  1491. const co = this._findCooperation(relaId, stageCooperation[i], sa);
  1492. d.cooperation.push(this._findSign(relaId, stageCooperation[i], sa));
  1493. d.company.push(co ? co.company : '');
  1494. }
  1495. if (finish) {
  1496. for (const [i, cs] of coSignOrder.entries()) {
  1497. if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {
  1498. d['co_sign' + (i + 1)] = d.cooperation[cs] || this._completeSign(data.stage_audit[cs]);
  1499. d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';
  1500. d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;
  1501. d['co_company' + (i + 1)] = d.company[cs] || data.stage_audit[cs].name;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. },
  1507. _findStageAuditAss(relaId, stageAuditAss, auditor) {
  1508. if (!stageAuditAss || stageAuditAss.length === 0) return null;
  1509. if (relaId.length > 0) {
  1510. for (const id of relaId) {
  1511. const c = stageAuditAss.find(x => { return x.ass_ledger_id.indexOf(id + '') >= 0; });
  1512. if (c) return c;
  1513. }
  1514. return null;
  1515. }
  1516. return null;
  1517. },
  1518. _loadImAssistData(ctx, data, options, csRela) {
  1519. let coSignOrder = [];
  1520. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1521. if (csRela.cDefine.audit_select) {
  1522. for (const asc of csRela.cDefine.audit_select) {
  1523. coSignOrder.push(asc.order);
  1524. }
  1525. }
  1526. } else {
  1527. coSignOrder = options.co_sign || [];
  1528. }
  1529. const stageAuditAss = [];
  1530. let finish = true;
  1531. for (const sa of data.stage_audit) {
  1532. if (sa.end_time) stageAuditAss.push(data.mem_stage_audit_ass.filter(x => { return x.user_id === sa.aid; }));
  1533. if (finish && !sa.end_time) finish = false;
  1534. }
  1535. for (const d of data[options.table]) {
  1536. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1537. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1538. d.ass = [];
  1539. for (const [i, sa] of data.stage_audit.entries()) {
  1540. const co = this._findStageAuditAss(relaId, stageAuditAss[i], sa);
  1541. d.ass.push(co);
  1542. }
  1543. for (const [i, cs] of coSignOrder.entries()) {
  1544. if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {
  1545. d['co_sign' + (i + 1)] = this._completeSign(d.ass[cs] || data.stage_audit[cs]);
  1546. d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';
  1547. d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;
  1548. d['co_name' + (i + 1)] = d.ass[cs] ? d.ass[cs].name : data.stage_audit[cs].name;
  1549. d['co_company' + (i + 1)] = d.ass[cs] ? d.ass[cs].company : data.stage_audit[cs].company;
  1550. }
  1551. }
  1552. }
  1553. },
  1554. fun(ctx, data, fieldsKey, options, csRela) {
  1555. if (!options || !options.table) return;
  1556. if (!data[options.table]) return;
  1557. if (!data.mem_stage_bills) return;
  1558. if (!data.stage_audit) return;
  1559. if (!data.ledger_cooperation && !data.mem_stage_audit_ass) return;
  1560. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) {
  1561. if (data.mem_stage_audit_ass) {
  1562. this._loadImAssistData(ctx, data, options, csRela);
  1563. } else {
  1564. this._loadImCooperationData(ctx, data, options, csRela);
  1565. }
  1566. }
  1567. },
  1568. };
  1569. const signSelect = {
  1570. name: '签字',
  1571. hint: '协作模式下,才需使用,其他情况勿需使用',
  1572. defaultSetting: {
  1573. table: 'mem_stage_im_zl',
  1574. },
  1575. _findSign(relaId, stageCooperation, auditor) {
  1576. if (!stageCooperation) return '';
  1577. if (relaId.length > 0) {
  1578. for (const id of relaId) {
  1579. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1580. if (c) return c.sign_path;
  1581. }
  1582. return '';
  1583. }
  1584. return '';
  1585. },
  1586. _loadImCooperationData(ctx, data, options, csRela) {
  1587. const stageCooperation = {};
  1588. for (const sa of data.stage_audit) {
  1589. if (sa.end_time) stageCooperation[sa.aid] = data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; });
  1590. }
  1591. for (const d of data[options.table]) {
  1592. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1593. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1594. for (const ss of data.sign_select) {
  1595. if (!ss.co_sign) ss.co_sign = [];
  1596. ss.co_sign.push(this._findSign(relaId, stageCooperation[ss.id], ss) || 'public/upload/sign/' + ss.sign_path);
  1597. }
  1598. }
  1599. },
  1600. fun(ctx, data, fieldsKey, options, csRela) {
  1601. if (!options || !options.table) return;
  1602. if (!data[options.table]) return;
  1603. if (!data.mem_stage_bills) return;
  1604. if (!data.stage_audit) return;
  1605. if (!data.ledger_cooperation) return;
  1606. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) { this._loadImCooperationData(ctx, data, options, csRela); }
  1607. },
  1608. };
  1609. const getChangeLedger = {
  1610. name: '获取 工程变更台账表 数据',
  1611. hint: '须提供mem_stage_bills, mem_stage_change_bills作为数据支撑, 结果写入mem_stage_change_ledger',
  1612. defaultSetting: {
  1613. mergeName: true,
  1614. calc: '', // split: 均分
  1615. level: '2-leaf', // 空表示表示保留所有的项目节,2-leaf表示保留第2层和最底层项目节,默认为空
  1616. },
  1617. _getSource(ctx, bills, changeList) {
  1618. const result = [];
  1619. for (const b of bills) {
  1620. if (b.b_code) continue;
  1621. const l = {
  1622. id: b.id,
  1623. code: b.code, name: b.name, unit: b.unit,
  1624. ledger_id: b.ledger_id, ledger_pid: b.ledger_pid, level: b.level, full_path: b.full_path,
  1625. total_price: b.total_price, deal_tp: b.deal_tp,
  1626. contract_tp: b.contract_tp, qc_tp: b.qc_tp, gather_tp: b.gather_tp,
  1627. end_contract_tp: b.end_contract_tp, end_qc_tp: b.end_qc_tp, end_gather_tp: b.end_gather_tp,
  1628. };
  1629. result.push(l);
  1630. const childGcl = bills.filter(x => { return x.ledger_pid === b.ledger_id && x.b_code});
  1631. if (!childGcl || childGcl.length === 0) continue;
  1632. const gcl = bills.filter(x => { return x.full_path.indexOf(l.full_path + '-') === 0 && x.b_code; });
  1633. if (gcl.length === 0) continue;
  1634. const gclIds = gcl.map(x => { return x.id; });
  1635. const changes = changeList.filter(x => { return gclIds.indexOf(x.lid) >= 0; });
  1636. if (!changes || changes.length === 0) continue;
  1637. l.changes = [];
  1638. for (const c of changes) {
  1639. let mc = l.changes.find(x => { return x.cid === c.cid; });
  1640. if (!mc) {
  1641. mc = {
  1642. cid: c.cid,
  1643. c_code: c.c_code, c_name: c.c_name,
  1644. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1645. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time * 1000,
  1646. c_bills: [],
  1647. };
  1648. l.changes.push(mc);
  1649. }
  1650. mc.c_bills.push(c);
  1651. }
  1652. }
  1653. return result;
  1654. },
  1655. _filterChangeLedger(ctx, changeLedger, options) {
  1656. const result = [];
  1657. for (const f of changeLedger) {
  1658. const calc = changeLedger.filter(x => { return x.full_path.indexOf(f.full_path) === 0 && x.changes && x.changes.length > 0; });
  1659. if (!calc || calc.length === 0) continue;
  1660. if (options.level === '2-leaf')
  1661. if (f.level !== 2 && (!f.changes || f.changes.length === 0)) continue;
  1662. result.push({
  1663. code: f.code, name: f.name, unit: f.unit,
  1664. ledger_id: f.ledger_id, ledger_pid: f.ledger_pid, level: f.level, full_path: f.full_path,
  1665. total_price: f.total_price, deal_tp: f.deal_tp,
  1666. contract_tp: f.contract_tp, qc_tp: f.qc_tp, gather_tp: f.gather_tp,
  1667. end_contract_tp: f.end_contract_tp, end_qc_tp: f.end_qc_tp, end_gather_tp: f.end_gather_tp,
  1668. });
  1669. if (f.changes && f.changes.length > 0) {
  1670. for (const c of f.changes) {
  1671. const fc = {
  1672. c_code: c.c_code, c_name: c.c_name,
  1673. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1674. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time,
  1675. };
  1676. if (options.mergeName) fc.name = fc.c_name;
  1677. result.push(fc);
  1678. }
  1679. }
  1680. }
  1681. return result;
  1682. },
  1683. fun(ctx, data, fieldsKey, options, csRela) {
  1684. if (!options) options = {};
  1685. if (!data.mem_stage_bills) return;
  1686. if (!data.mem_stage_change_bills) return;
  1687. const changeLedger = this._getSource(ctx, data.mem_stage_bills, data.mem_stage_change_bills);
  1688. data.mem_stage_change_ledger = this._filterChangeLedger(ctx, changeLedger, options);
  1689. },
  1690. };
  1691. const treeFilter = {
  1692. name: '树结构数据过滤',
  1693. hint: '过滤时,当父项符合条件时,会自动包含子项',
  1694. defaultSetting: {
  1695. table: 'mem_stage_bills',
  1696. type: 'match/filter',
  1697. subType: 'all/posterity',
  1698. condition: [
  1699. { field: 'name', type: 'str', value: '建筑项目管理费', isPart: true },
  1700. ],
  1701. },
  1702. _checkPath(matchPath, full_path) {
  1703. for (const mp of matchPath) {
  1704. if (full_path.indexOf(mp) === 0) return true;
  1705. }
  1706. return false;
  1707. },
  1708. fun(ctx, data, fieldsKey, options, csRela) {
  1709. if (!options || !options.table || !options.condition || !options.type) return;
  1710. const fData = data[options.table],
  1711. self = this;
  1712. if (!fData || fData.length === 0) return;
  1713. if (!fData[0].full_path) return;
  1714. valueCheck.loadTypeFun(options.condition);
  1715. const matchPath = [];
  1716. for (const d of fData) {
  1717. if (this._checkPath(matchPath, d.full_path)) continue;
  1718. if (valueCheck.checkData(ctx, d, options.condition)) {
  1719. matchPath.push(d.full_path + (options.subType === 'posterity' ? '-' : ''));
  1720. }
  1721. }
  1722. data[options.table] = fData.filter(x => {
  1723. const match = self._checkPath(matchPath, x.full_path) && (options.subType === 'leaf' ? x.is_leaf : true);
  1724. return options.type === 'match' ? match : !match;
  1725. });
  1726. },
  1727. };
  1728. const gatherGcl2 = {
  1729. name: '工程量清单汇总2',
  1730. hint: '会生成新的工程量清单指标&相关项目节指标',
  1731. defaultSetting: {
  1732. billsTable: 'mem_stage_bills',
  1733. posTable: 'mem_stage_pos',
  1734. dealTable: 'deal_bills',
  1735. },
  1736. fun(ctx, data, fieldsKey, options, csRela) {
  1737. if (!options || !options.billsTable || !options.posTable) return;
  1738. const bills = data[options.billsTable];
  1739. if (!bills) return;
  1740. const pos = data[options.posTable];
  1741. if (!pos) return;
  1742. const deal = data[options.dealTable];
  1743. const gclGatherModel = require('./gcl_gather').gclGather;
  1744. const gatherUtil = new gclGatherModel(ctx);
  1745. gatherUtil.gather(bills, pos, deal);
  1746. data.mem_gcl_gather_bills = gatherUtil.gclList;
  1747. data.mem_gcl_gather_xmj = gatherUtil.leafXmjs;
  1748. }
  1749. };
  1750. const gatherMaterialGl = {
  1751. name: '分类汇总调差工料',
  1752. hint: '根据工程量清单分类汇总调差工料,要求先使用【工程量清单汇总2】处理得到工程量清单数据,并引入【调差工料明细】',
  1753. _relaGl(source, data) {
  1754. if (!source) return;
  1755. const gl = source.find(x => {return x.id === data.mb_id;});
  1756. if (gl) {
  1757. data.gl_code = gl.code;
  1758. data.gl_name = gl.name;
  1759. data.gl_unit = gl.unit;
  1760. data.gl_spec = gl.spec;
  1761. data.gl_base_price = gl.base_price;
  1762. data.gl_base_times = gl.base_times;
  1763. data.gl_msg_tp = gl.msg_tp;
  1764. data.gl_msg_times = gl.msg_times;
  1765. data.gl_msg_spread = gl.msg_spread;
  1766. data.gl_m_up_risk = gl.m_up_risk;
  1767. data.gl_m_down_risk = gl.m_down_risk;
  1768. data.gl_m_spread = gl.m_spread;
  1769. }
  1770. },
  1771. fun(ctx, data, fieldsKey, options, csRela) {
  1772. if (!data.mem_gcl_gather_bills || !data.mem_gcl_gather_xmj || !data.mem_material_gl_detail) return;
  1773. const result = [];
  1774. for (const d of data.mem_material_gl_detail) {
  1775. const xmj = data.mem_gcl_gather_xmj.find(x => {
  1776. return d.gcl_id === x.org_gcl_id && d.xmj_id === x.id && (!d.mx_id || d.mx_id === x.mx_id);
  1777. });
  1778. if (!xmj) continue;
  1779. const gcl = data.mem_gcl_gather_bills.find(x => {
  1780. return x.id === xmj.gcl_id;
  1781. });
  1782. if (!gcl) continue;
  1783. const gd = result.find(x => {
  1784. return x.gather_gcl_id === gcl.id && x.mb_id === d.mb_id;
  1785. });
  1786. if (!gd) {
  1787. const newGd = {
  1788. gather_gcl_id: gcl.id,
  1789. mb_id: d.mb_id, gather_qty: d.gather_qty, quantity: d.quantity,
  1790. b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price,
  1791. cur_contract_qty: gcl.contract_qty, cur_contract_tp: gcl.contract_tp,
  1792. cur_qc_qty: gcl.qc_qty, cur_qc_tp: gcl.qc_tp,
  1793. cur_gather_qty: gcl.gather_qty, cur_gather_tp: gcl.gather_tp,
  1794. };
  1795. this._relaGl(data.mem_material_gl, newGd);
  1796. result.push(newGd);
  1797. } else {
  1798. gd.gather_qty = ctx.helper.add(gd.gather_qty, d.gather_qty);
  1799. gd.quantity = ctx.helper.add(gd.quantity, d.quantity);
  1800. }
  1801. }
  1802. data.mem_material_gl_detail = result;
  1803. }
  1804. };
  1805. const analysisObj = {
  1806. changeSort,
  1807. gatherGcl,
  1808. sortGcl,
  1809. gatherChapter,
  1810. join,
  1811. getChapterCode,
  1812. filter,
  1813. union,
  1814. gatherStagePay,
  1815. addSumChapter,
  1816. auditSelect,
  1817. datetimeFormat,
  1818. gatherSelectConverse,
  1819. stageSelectConverse,
  1820. materialSumSelectConverse,
  1821. sortPos,
  1822. unionPos,
  1823. splitXmjCode,
  1824. loadCooperationData,
  1825. getChangeLedger,
  1826. treeFilter,
  1827. gatherGcl2,
  1828. gatherMaterialGl,
  1829. };
  1830. const analysisDefine = (function(obj) {
  1831. const result = [];
  1832. for (const o in obj) {
  1833. const analysis = obj[o];
  1834. if (analysis.name && analysis.fun) {
  1835. result.push({
  1836. name: analysis.name,
  1837. key: o,
  1838. hint: analysis.hint,
  1839. intro: analysis.intro,
  1840. url: '/help?m=report&s=analysis&d=' + o,
  1841. });
  1842. }
  1843. }
  1844. return result;
  1845. })(analysisObj);
  1846. module.exports = {
  1847. analysisObj,
  1848. analysisDefine,
  1849. };