rpt_data_analysis.js 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  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 = '^[^0-9]*([0-9]{0,2}-)?' + 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 + 'contract_expr', target: tPrefix + 'contract_expr' });
  1021. fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });
  1022. fields.push({ source: sPrefix + 'qc_tp', target: tPrefix + 'qc_tp' });
  1023. fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });
  1024. fields.push({ source: sPrefix + 'gather_tp', target: tPrefix + 'gather_tp' });
  1025. }
  1026. for (const d of data) {
  1027. for (const f of fields) {
  1028. d[f.target] = d[f.source];
  1029. }
  1030. }
  1031. },
  1032. _stagePosCompare(data, order) {
  1033. const fields = [];
  1034. for (const [i, o] of order.entries()) {
  1035. const sPrefix = 'r' + o + '_';
  1036. const tPrefix = 'as' + i + '_';
  1037. fields.push({ source: sPrefix + 'contract_qty', target: tPrefix + 'contract_qty' });
  1038. fields.push({ source: sPrefix + 'contract_expr', target: tPrefix + 'contract_expr' });
  1039. fields.push({ source: sPrefix + 'qc_qty', target: tPrefix + 'qc_qty' });
  1040. fields.push({ source: sPrefix + 'gather_qty', target: tPrefix + 'gather_qty' });
  1041. }
  1042. for (const d of data) {
  1043. for (const f of fields) {
  1044. d[f.target] = d[f.source];
  1045. }
  1046. }
  1047. },
  1048. _stagePay(data, order) {
  1049. const fields = [];
  1050. for (const [i, o] of order.entries()) {
  1051. const sPrefix = 'r' + o + '_';
  1052. const tPrefix = 'as' + i + '_';
  1053. fields.push({ source: sPrefix + 'tp', target: tPrefix + 'tp' });
  1054. }
  1055. for (const d of data) {
  1056. for (const f of fields) {
  1057. d[f.target] = d[f.source];
  1058. }
  1059. }
  1060. },
  1061. fun(ctx, data, fieldsKey, options, csRela) {
  1062. if (!ctx.tender || !ctx.stage) return;
  1063. if (!csRela.tplDefine) return;
  1064. const asDefine = csRela.tplDefine.audit_select;
  1065. if (!asDefine || !asDefine.enable || !asDefine.setting || asDefine.setting === '') return;
  1066. const asCustom = csRela.cDefine ? csRela.cDefine.audit_select : null;
  1067. const order = [];
  1068. if (asCustom) {
  1069. for (const asc of asCustom) {
  1070. order.push(asc.sort || asc.order);
  1071. }
  1072. }
  1073. for (const t of options.table) {
  1074. switch (t) {
  1075. case 'mem_stage_bills_compare':
  1076. this._stageBillsCompare(data[t], order);
  1077. break;
  1078. case 'mem_stage_pos_compare':
  1079. this._stagePosCompare(data[t], order);
  1080. break;
  1081. case 'mem_stage_pay':
  1082. this._stagePay(data[t], order);
  1083. break;
  1084. }
  1085. }
  1086. },
  1087. };
  1088. const datetimeFormat = {
  1089. name: '日期格式化',
  1090. hint: '参见帮助',
  1091. defaultSetting: {
  1092. tables: [
  1093. { name: 'mem_stage_bonus', fields: [
  1094. { field: 'real_time', formatter: 'yyyy-MM-DD' },
  1095. { field: 'create_time', formatter: 'yyyy-MM-DD' },
  1096. ] },
  1097. ],
  1098. },
  1099. fun(ctx, data, fieldsKey, options, csRela) {
  1100. if (!options.tables) return;
  1101. const tables = options.tables instanceof Array ? options.tables : [options.tables];
  1102. if (tables.length === 0) return;
  1103. for (const table of tables) {
  1104. const formatData = data[table.name];
  1105. for (const fd of formatData) {
  1106. for (const f of table.fields) {
  1107. fd[f.field] = moment(fd[f.field]).format(f.formatter);
  1108. }
  1109. }
  1110. }
  1111. },
  1112. };
  1113. const gatherSelectConverse = {
  1114. name: '交叉汇总数据',
  1115. hint: '需搭配 用户交互--汇总标段选择 一起使用',
  1116. defaultSetting: {
  1117. table: ['mem_gather_stage_bills'],
  1118. },
  1119. _commonConverse(helper, data, count) {
  1120. const result = [];
  1121. const reg = new RegExp('^t_[0-9]+_');
  1122. for (let i = 0; i < count; i++) {
  1123. const curReg = new RegExp('^t_' + i + '_');
  1124. for (const [i, d] of data.entries()) {
  1125. const nd = { cross_index: i + 1 };
  1126. for (const prop in d) {
  1127. if (reg.test(prop)) {
  1128. if (curReg.test(prop)) {
  1129. nd[prop.replace(curReg, 't_')] = d[prop];
  1130. }
  1131. } else {
  1132. nd[prop] = d[prop];
  1133. }
  1134. }
  1135. result.push(nd);
  1136. }
  1137. }
  1138. // for (const d of data) {
  1139. // for (let i = 0; i < count; i++) {
  1140. // const curReg = new RegExp('^t_' + i + '_');
  1141. // const nd = {};
  1142. // for (const prop in d) {
  1143. // if (reg.test(prop)) {
  1144. // if (curReg.test(prop)) {
  1145. // nd[prop.replace(curReg, 't_')] = d[prop];
  1146. // }
  1147. // } else {
  1148. // nd[prop] = d[prop];
  1149. // }
  1150. // }
  1151. // result.push(nd);
  1152. // }
  1153. // }
  1154. return result;
  1155. },
  1156. fun(ctx, data, fieldsKey, options, csRela) {
  1157. if (!csRela.tplDefine) return;
  1158. const gsDefine = csRela.tplDefine.gather_select;
  1159. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1160. const gsCustom = csRela.cDefine ? csRela.cDefine.gather_select : null;
  1161. const count = gsDefine.setting.special
  1162. ? gsCustom.tenders.length - gsDefine.setting.special.length
  1163. : gsCustom.tenders.length;
  1164. for (const t of options.table) {
  1165. switch (t) {
  1166. case 'mem_gather_stage_bills':
  1167. case 'mem_gather_stage_bills_filter':
  1168. case 'mem_gather_stage_pos':
  1169. case 'mem_gather_stage_pay':
  1170. case 'mem_gather_deal_bills':
  1171. case 'mem_union_data':
  1172. data[t] = this._commonConverse(ctx.helper, data[t], count);
  1173. break;
  1174. }
  1175. }
  1176. },
  1177. };
  1178. const sortPos = {
  1179. name: '计量单元排序',
  1180. hint: '',
  1181. defaultSetting: {
  1182. bills: 'mem_stage_bills',
  1183. pos: 'mem_stage_pos',
  1184. },
  1185. fun(ctx, data, fieldsKey, options, csRela) {
  1186. if (!options || !options.bills || !options.pos) return;
  1187. const bills = data[options.bills];
  1188. const pos = data[options.pos];
  1189. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1190. const billsIndex = {};
  1191. const findBillsIndex = function(billsId) {
  1192. if (!billsIndex[billsId]) {
  1193. billsIndex[billsId] = ctx.helper._.findIndex(bills, function(x) {
  1194. return x.id === billsId;
  1195. });
  1196. }
  1197. return billsIndex[billsId];
  1198. };
  1199. pos.sort(function(x, y) {
  1200. const xIndex = findBillsIndex(x.lid) * 1000 + x.porder;
  1201. const yIndex = findBillsIndex(y.lid) * 1000 + y.porder;
  1202. return xIndex - yIndex;
  1203. });
  1204. },
  1205. };
  1206. const unionPos = {
  1207. name: '拼接计量单元',
  1208. hint: '',
  1209. defaultSetting: {
  1210. bills: 'mem_stage_bills',
  1211. pos: 'mem_stage_pos',
  1212. },
  1213. fun(ctx, data, fieldsKey, options, csRela) {
  1214. if (!options || !options.bills || !options.pos) return;
  1215. const bills = data[options.bills];
  1216. const pos = data[options.pos];
  1217. if (!bills || bills.length === 0 || !pos || pos.length === 0) return;
  1218. const posIndex = new PosIndex({
  1219. id: 'id',
  1220. masterId: 'lid',
  1221. order: 'porder',
  1222. });
  1223. posIndex.loadDatas(pos);
  1224. const unionData = [];
  1225. for (const b of bills) {
  1226. unionData.push(b);
  1227. const posRange = posIndex.getLedgerPos(b.id);
  1228. b.posCount = posRange ? posRange.length : 0;
  1229. if (!posRange || posRange.length === 0) continue;
  1230. for (const p of posRange) {
  1231. unionData.push(p);
  1232. }
  1233. }
  1234. data[options.bills] = unionData;
  1235. },
  1236. };
  1237. const splitXmjCode = {
  1238. name: '拆分项目节编号',
  1239. hint: '',
  1240. defaultSetting: {
  1241. table: 'mem_stage_bills',
  1242. type: '11', // '18', '18-0-1', '18-1', '18-1-1'
  1243. code: 'code',
  1244. targetField: ['xiang', 'mu', 'jie', 'ximu'],
  1245. },
  1246. // 11标准
  1247. _split11Std(data, options, helper) {
  1248. for (const d of data) {
  1249. if (!d[options.code]) continue;
  1250. const splitCodes = d[options.code].split('-');
  1251. if (d.level === 2) {
  1252. const field = options.targetField[0];
  1253. if (field && splitCodes.length > 1) {
  1254. d[field] = helper.transFormToChinese(splitCodes[1]);
  1255. }
  1256. } else if (d.level === 3) {
  1257. const field = options.targetField[1];
  1258. if (field && splitCodes.length > 2) {
  1259. d[field] = splitCodes[2];
  1260. }
  1261. } else if (d.level === 4) {
  1262. const field = options.targetField[2];
  1263. if (field && splitCodes.length > 3) {
  1264. d[field] = splitCodes[3];
  1265. }
  1266. } else if (d.level > 4) {
  1267. const field = options.targetField[3];
  1268. if (field && splitCodes.length > 4) {
  1269. d[field] = splitCodes.reduce(function(s, v, i) { return i >= 4 ? (s ? s + '-' + v : v + '') : ''; });
  1270. }
  1271. }
  1272. }
  1273. },
  1274. _getParentByLevel(data, node, level) {
  1275. if (node.full_path) {
  1276. const pid = node.full_path.split('-')[level - 1];
  1277. return data.find(function(x) {
  1278. return x.level === level && x.ledger_id == pid;
  1279. });
  1280. }
  1281. return null;
  1282. },
  1283. // 18标准
  1284. _split18Std(data, options, helper) {
  1285. const types = options.type.split('-');
  1286. const isSplitSub = !!(types[1] && types[1] === '1');
  1287. const isKeepGD = !!(types[2] && types[2] === '1');
  1288. for (const d of data) {
  1289. if (!d[options.code]) continue;
  1290. let target = '';
  1291. if (d.level === 2) {
  1292. target = options.targetField[0] ? options.targetField[0] : '';
  1293. } else if (d.level === 3) {
  1294. target = options.targetField[1] ? options.targetField[1] : '';
  1295. } else if (d.level === 4) {
  1296. target = options.targetField[2] ? options.targetField[2] : '';
  1297. } else if (d.level > 4) {
  1298. target = options.targetField[3] ? options.targetField[3] : '';
  1299. }
  1300. if (!target) continue;
  1301. if (helper.check18MainCode(d[options.code])) {
  1302. const parent = helper._.find(data, { ledger_id: d.ledger_pid });
  1303. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1304. d[target] = !splitParent
  1305. ? d[options.code]
  1306. : d[options.code].replace(splitParent[options.code], '');
  1307. } else if (helper.check18SubCode(d[options.code]) && isSplitSub) {
  1308. const parent = helper._.find(data, { ledger_id: d.ledger_pid });
  1309. const splitParent = !parent || parent.level < 5 ? parent : this._getParentByLevel(data, d, 4);
  1310. d[target] = !splitParent || helper.check18MainCode(splitParent.code)
  1311. ? d[options.code]
  1312. : d[options.code].replace(splitParent[options.code], '');
  1313. } else {
  1314. d[target] = d[options.code];
  1315. }
  1316. if (!isKeepGD) d[target] = d[target].replace(/^G[D]?/, '');
  1317. }
  1318. },
  1319. // 根据层次填入
  1320. _splitDefault(data, options) {
  1321. for (const d of data) {
  1322. if (!d[options.code]) continue;
  1323. if (d.level === 2) {
  1324. if (options.targetField[0]) d[options.targetField[0]] = d[options.code];
  1325. } else if (d.level === 3) {
  1326. if (options.targetField[1]) d[options.targetField[1]] = d[options.code];
  1327. } else if (d.level === 4) {
  1328. if (options.targetField[2]) d[options.targetField[2]] = d[options.code];
  1329. } else if (d.level > 4) {
  1330. if (options.targetField[3]) d[options.targetField[3]] = d[options.code];
  1331. }
  1332. }
  1333. },
  1334. fun(ctx, data, fieldsKey, options, csRela) {
  1335. if (!options || !options.table || !options.code || !options.targetField) return;
  1336. if (!data[options.table]) return;
  1337. if (options.type === '11') {
  1338. this._split11Std(data[options.table], options, ctx.helper);
  1339. } else if (/^18(-[0,1]){0,2}$/.test(options.type)) {
  1340. this._split18Std(data[options.table], options, ctx.helper);
  1341. } else {
  1342. this._splitDefault(data[options.table], options);
  1343. }
  1344. },
  1345. };
  1346. const stageSelectConverse = {
  1347. name: '交叉多期汇总数据',
  1348. hint: '需搭配 用户交互--单标段多期汇总选择 一起使用',
  1349. defaultSetting: {
  1350. table: ['mem_stage_sum_bills'],
  1351. },
  1352. _commonConverse(helper, data, stages) {
  1353. const result = [];
  1354. const reg = new RegExp('^s_[0-9]+_');
  1355. for (const s of stages) {
  1356. const curReg = new RegExp('^s_' + s + '_');
  1357. for (const [i, d] of data.entries()) {
  1358. const nd = { cross_index: i + 1, s_order: s };
  1359. for (const prop in d) {
  1360. if (reg.test(prop)) {
  1361. if (curReg.test(prop)) {
  1362. nd[prop.replace(curReg, 's_')] = d[prop];
  1363. }
  1364. } else {
  1365. nd[prop] = d[prop];
  1366. }
  1367. }
  1368. result.push(nd);
  1369. }
  1370. }
  1371. return result;
  1372. },
  1373. fun(ctx, data, fieldsKey, options, csRela) {
  1374. if (!csRela.tplDefine) return;
  1375. const gsDefine = csRela.tplDefine.stage_select;
  1376. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1377. const gsCustom = csRela.cDefine ? csRela.cDefine.stage_select : null;
  1378. if (gsCustom) {
  1379. for (const t of options.table) {
  1380. switch (t) {
  1381. case 'mem_stage_sum_bills':
  1382. case 'mem_stage_sum_bills_filter':
  1383. case 'mem_stage_sum_pos':
  1384. case 'mem_stage_sum_pay':
  1385. case 'mem_union_data':
  1386. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.stages);
  1387. break;
  1388. }
  1389. }
  1390. } else {
  1391. console.log('gsCustom is null!');
  1392. }
  1393. },
  1394. };
  1395. const materialSumSelectConverse = {
  1396. name: '交叉【多期材差汇总】数据',
  1397. hint: '需搭配 用户交互【材差-多期汇总选择】 一起使用',
  1398. defaultSetting: {
  1399. table: ['mem_material_sum_gl'],
  1400. },
  1401. _commonConverse(helper, data, materials) {
  1402. const result = [];
  1403. const reg = new RegExp('^m_[0-9]+_');
  1404. for (const s of materials) {
  1405. const curReg = new RegExp('^m_' + s + '_');
  1406. for (const [i, d] of data.entries()) {
  1407. const stageId = `m_${s}_stageId`;
  1408. if (d[stageId]) {
  1409. for (const sid of d[stageId]) {
  1410. const curStageReg = new RegExp(`^m_${s}_s${sid}_`);
  1411. const nd = { cross_index: i + 1, s_order: `${s}_${sid}` };
  1412. for (const prop in d) {
  1413. if (reg.test(prop)) {
  1414. if (curStageReg.test(prop)) {
  1415. nd[prop.replace(curStageReg, 'm_')] = d[prop];
  1416. }
  1417. } else {
  1418. nd[prop] = d[prop];
  1419. }
  1420. }
  1421. result.push(nd);
  1422. }
  1423. } else {
  1424. const nd = { cross_index: i + 1, s_order: s };
  1425. for (const prop in d) {
  1426. if (reg.test(prop)) {
  1427. if (curReg.test(prop)) {
  1428. nd[prop.replace(curReg, 'm_')] = d[prop];
  1429. }
  1430. } else {
  1431. nd[prop] = d[prop];
  1432. }
  1433. }
  1434. result.push(nd);
  1435. }
  1436. }
  1437. }
  1438. return result;
  1439. },
  1440. fun(ctx, data, fieldsKey, options, csRela) {
  1441. if (!csRela.tplDefine) return;
  1442. const gsDefine = csRela.tplDefine.material_sum_select;
  1443. if (!gsDefine || !gsDefine.enable || !gsDefine.setting || gsDefine.setting === '') return;
  1444. const gsCustom = csRela.cDefine ? csRela.cDefine.material_sum_select : null;
  1445. if (gsCustom) {
  1446. for (const t of options.table) {
  1447. switch (t) {
  1448. case 'mem_material_sum_gl':
  1449. data[t] = this._commonConverse(ctx.helper, data[t], gsCustom.materials);
  1450. break;
  1451. }
  1452. }
  1453. } else {
  1454. console.log('gsCustom is null!');
  1455. }
  1456. },
  1457. };
  1458. const loadCooperationData = {
  1459. name: '加载协作数据',
  1460. hint: '须使用台账、计量审批流程、协作数据作为基础',
  1461. defaultSetting: {
  1462. table: 'mem_stage_im_zl',
  1463. co_sign: [0, 1, 2, 3],
  1464. },
  1465. _findSign(relaId, stageCooperation, auditor) {
  1466. if (!stageCooperation) return '';
  1467. if (relaId.length > 0) {
  1468. for (const id of relaId) {
  1469. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1470. if (c) return c.sign_path;
  1471. }
  1472. return '';
  1473. }
  1474. return '';
  1475. },
  1476. _findCooperation(relaId, stageCooperation, auditor) {
  1477. if (!stageCooperation) return null;
  1478. if (relaId.length > 0) {
  1479. for (const id of relaId) {
  1480. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1481. if (c) return c;
  1482. }
  1483. return null;
  1484. }
  1485. return null;
  1486. },
  1487. _completeSign(auditor) {
  1488. if (!auditor) return '';
  1489. if (auditor.ass_user_id) {
  1490. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1491. } else {
  1492. if (!auditor.end_time) return '';
  1493. return auditor.sign_path ? 'public/upload/sign/' + auditor.sign_path : '';
  1494. }
  1495. },
  1496. _loadImCooperationData(ctx, data, options, csRela) {
  1497. const coSignInfo = [];
  1498. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1499. if (csRela.cDefine.audit_select) {
  1500. for (const asc of csRela.cDefine.audit_select) {
  1501. coSignInfo.push({ aid: asc.aid, order: asc.order });
  1502. }
  1503. }
  1504. }
  1505. const stageCooperation = [];
  1506. let finish = true;
  1507. for (const sa of data.stage_audit) {
  1508. if (sa.end_time) stageCooperation.push(data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; }));
  1509. if (finish && !sa.end_time) finish = false;
  1510. }
  1511. for (const d of data[options.table]) {
  1512. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1513. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1514. d.cooperation = [], d.company = [];
  1515. for (const [i, sa] of data.stage_audit.entries()) {
  1516. const co = this._findCooperation(relaId, stageCooperation[i], sa);
  1517. d.cooperation.push(this._findSign(relaId, stageCooperation[i], sa));
  1518. d.company.push(co ? co.company : '');
  1519. }
  1520. if (finish) {
  1521. for (const [i, cs] of coSignOrder.entries()) {
  1522. if (data.stage_audit[cs] && data.stage_audit[cs].end_time) {
  1523. d['co_sign' + (i + 1)] = d.cooperation[cs] || this._completeSign(data.stage_audit[cs]);
  1524. d['co_opinion' + (i + 1)] = data.stage_audit[cs].opinion || '';
  1525. d['co_time' + (i + 1)] = data.stage_audit[cs].end_time;
  1526. d['co_company' + (i + 1)] = d.company[cs] || data.stage_audit[cs].name;
  1527. }
  1528. }
  1529. }
  1530. }
  1531. },
  1532. _findStageAuditAss(relaId, stageAuditAss, auditor) {
  1533. if (!stageAuditAss || stageAuditAss.length === 0) return null;
  1534. if (relaId.length > 0) {
  1535. for (const id of relaId) {
  1536. const c = stageAuditAss.find(x => { return x.ass_ledger_id.indexOf(id + '') >= 0; });
  1537. if (c) return c;
  1538. }
  1539. return null;
  1540. }
  1541. return null;
  1542. },
  1543. _loadImAssistData(ctx, data, options, csRela) {
  1544. const coSignInfo = [];
  1545. if (csRela && csRela.tplDefine && csRela.tplDefine.audit_select && csRela.cDefine && csRela.cDefine.audit_select) {
  1546. if (csRela.cDefine.audit_select) {
  1547. for (const asc of csRela.cDefine.audit_select) {
  1548. coSignInfo.push({ aid: asc.aid, order: asc.order });
  1549. }
  1550. }
  1551. }
  1552. let finish = true;
  1553. for (const sa of data.stage_audit) {
  1554. if (sa.end_time) sa.ass = data.mem_stage_audit_ass.filter(x => { return x.user_id === sa.aid; });
  1555. if (finish && !sa.end_time) finish = false;
  1556. }
  1557. for (const d of data[options.table]) {
  1558. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1559. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1560. for (const [i, cs] of coSignInfo.entries()) {
  1561. const sa = data.stage_audit.find(x => {
  1562. if (x.aid !== undefined) return x.aid === cs.aid && x.order === cs.order;
  1563. return x.order === cs.order;
  1564. });
  1565. if (sa && sa.end_time) {
  1566. const co = this._findStageAuditAss(relaId, sa.ass, sa);
  1567. d['co_sign' + (i + 1)] = this._completeSign(co || sa);
  1568. d['co_opinion' + (i + 1)] = sa.opinion || '';
  1569. d['co_time' + (i + 1)] = sa.end_time;
  1570. d['co_name' + (i + 1)] = co ? co.name : sa.name;
  1571. d['co_company' + (i + 1)] = co ? co.company : sa.company;
  1572. }
  1573. }
  1574. }
  1575. },
  1576. fun(ctx, data, fieldsKey, options, csRela) {
  1577. if (!options || !options.table) return;
  1578. if (!data[options.table]) return;
  1579. if (!data.mem_stage_bills) return;
  1580. if (!data.stage_audit) return;
  1581. if (!data.ledger_cooperation && !data.mem_stage_audit_ass) return;
  1582. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) {
  1583. if (data.mem_stage_audit_ass) {
  1584. this._loadImAssistData(ctx, data, options, csRela);
  1585. } else {
  1586. this._loadImCooperationData(ctx, data, options, csRela);
  1587. }
  1588. }
  1589. },
  1590. };
  1591. const signSelect = {
  1592. name: '签字',
  1593. hint: '协作模式下,才需使用,其他情况勿需使用',
  1594. defaultSetting: {
  1595. table: 'mem_stage_im_zl',
  1596. },
  1597. _findSign(relaId, stageCooperation, auditor) {
  1598. if (!stageCooperation) return '';
  1599. if (relaId.length > 0) {
  1600. for (const id of relaId) {
  1601. const c = stageCooperation.find(x => { return x.ledger_id == id; });
  1602. if (c) return c.sign_path;
  1603. }
  1604. return '';
  1605. }
  1606. return '';
  1607. },
  1608. _loadImCooperationData(ctx, data, options, csRela) {
  1609. const stageCooperation = {};
  1610. for (const sa of data.stage_audit) {
  1611. if (sa.end_time) stageCooperation[sa.aid] = data.ledger_cooperation.filter(x => { return x.user_id === sa.aid; });
  1612. }
  1613. for (const d of data[options.table]) {
  1614. const bills = data.mem_stage_bills.find(x => { return x.id === d.lid; });
  1615. const relaId = bills ? bills.full_path.split('-').reverse() : [];
  1616. for (const ss of data.sign_select) {
  1617. if (!ss.co_sign) ss.co_sign = [];
  1618. ss.co_sign.push(this._findSign(relaId, stageCooperation[ss.id], ss) || 'public/upload/sign/' + ss.sign_path);
  1619. }
  1620. }
  1621. },
  1622. fun(ctx, data, fieldsKey, options, csRela) {
  1623. if (!options || !options.table) return;
  1624. if (!data[options.table]) return;
  1625. if (!data.mem_stage_bills) return;
  1626. if (!data.stage_audit) return;
  1627. if (!data.ledger_cooperation) return;
  1628. if (['mem_stage_im_zl', 'mem_stage_im_tz'].indexOf(options.table) >= 0) { this._loadImCooperationData(ctx, data, options, csRela); }
  1629. },
  1630. };
  1631. const getChangeLedger = {
  1632. name: '获取 工程变更台账表 数据',
  1633. hint: '须提供mem_stage_bills, mem_stage_change_bills作为数据支撑, 结果写入mem_stage_change_ledger',
  1634. defaultSetting: {
  1635. mergeName: true,
  1636. calc: '', // split: 均分
  1637. level: '2-leaf', // 空表示表示保留所有的项目节,2-leaf表示保留第2层和最底层项目节,默认为空
  1638. },
  1639. _getSource(ctx, bills, changeList) {
  1640. const result = [];
  1641. for (const b of bills) {
  1642. if (b.b_code) continue;
  1643. const l = {
  1644. id: b.id,
  1645. code: b.code, name: b.name, unit: b.unit,
  1646. ledger_id: b.ledger_id, ledger_pid: b.ledger_pid, level: b.level, full_path: b.full_path,
  1647. total_price: b.total_price, deal_tp: b.deal_tp,
  1648. contract_tp: b.contract_tp, qc_tp: b.qc_tp, gather_tp: b.gather_tp,
  1649. end_contract_tp: b.end_contract_tp, end_qc_tp: b.end_qc_tp, end_gather_tp: b.end_gather_tp,
  1650. };
  1651. result.push(l);
  1652. const childGcl = bills.filter(x => { return x.ledger_pid === b.ledger_id && x.b_code});
  1653. if (!childGcl || childGcl.length === 0) continue;
  1654. const gcl = bills.filter(x => { return x.full_path.indexOf(l.full_path + '-') === 0 && x.b_code; });
  1655. if (gcl.length === 0) continue;
  1656. const gclIds = gcl.map(x => { return x.id; });
  1657. const changes = changeList.filter(x => { return gclIds.indexOf(x.lid) >= 0; });
  1658. if (!changes || changes.length === 0) continue;
  1659. l.changes = [];
  1660. for (const c of changes) {
  1661. let mc = l.changes.find(x => { return x.cid === c.cid; });
  1662. if (!mc) {
  1663. mc = {
  1664. cid: c.cid,
  1665. c_code: c.c_code, c_name: c.c_name,
  1666. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1667. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time * 1000,
  1668. c_bills: [],
  1669. };
  1670. l.changes.push(mc);
  1671. }
  1672. mc.c_bills.push(c);
  1673. }
  1674. }
  1675. return result;
  1676. },
  1677. _filterChangeLedger(ctx, changeLedger, options) {
  1678. const result = [];
  1679. for (const f of changeLedger) {
  1680. const calc = changeLedger.filter(x => { return x.full_path.indexOf(f.full_path) === 0 && x.changes && x.changes.length > 0; });
  1681. if (!calc || calc.length === 0) continue;
  1682. if (options.level === '2-leaf')
  1683. if (f.level !== 2 && (!f.changes || f.changes.length === 0)) continue;
  1684. result.push({
  1685. code: f.code, name: f.name, unit: f.unit,
  1686. ledger_id: f.ledger_id, ledger_pid: f.ledger_pid, level: f.level, full_path: f.full_path,
  1687. total_price: f.total_price, deal_tp: f.deal_tp,
  1688. contract_tp: f.contract_tp, qc_tp: f.qc_tp, gather_tp: f.gather_tp,
  1689. end_contract_tp: f.end_contract_tp, end_qc_tp: f.end_qc_tp, end_gather_tp: f.end_gather_tp,
  1690. });
  1691. if (f.changes && f.changes.length > 0) {
  1692. for (const c of f.changes) {
  1693. const fc = {
  1694. c_code: c.c_code, c_name: c.c_name,
  1695. c_new_code: c.c_new_code, c_new_name: c.c_new_name,
  1696. c_content: c.c_content, c_basis: c.c_basis, c_cin_time: c.c_cin_time,
  1697. };
  1698. if (options.mergeName) fc.name = fc.c_name;
  1699. result.push(fc);
  1700. }
  1701. }
  1702. }
  1703. return result;
  1704. },
  1705. fun(ctx, data, fieldsKey, options, csRela) {
  1706. if (!options) options = {};
  1707. if (!data.mem_stage_bills) return;
  1708. if (!data.mem_stage_change_bills) return;
  1709. const changeLedger = this._getSource(ctx, data.mem_stage_bills, data.mem_stage_change_bills);
  1710. data.mem_stage_change_ledger = this._filterChangeLedger(ctx, changeLedger, options);
  1711. },
  1712. };
  1713. const treeFilter = {
  1714. name: '树结构数据过滤',
  1715. hint: '过滤时,当父项符合条件时,会自动包含子项',
  1716. defaultSetting: {
  1717. table: 'mem_stage_bills',
  1718. type: 'match/filter',
  1719. subType: 'all/posterity/leaf',
  1720. condition: [
  1721. { field: 'name', type: 'str', value: '建筑项目管理费', isPart: true },
  1722. ],
  1723. },
  1724. _checkPath(matchPath, full_path) {
  1725. for (const mp of matchPath) {
  1726. if (full_path.indexOf(mp.match + '-') === 0) return true;
  1727. if (mp.type === 'all' && full_path === mp.match) return true;
  1728. }
  1729. return false;
  1730. },
  1731. fun(ctx, data, fieldsKey, options, csRela) {
  1732. if (!options || !options.table || !options.condition || !options.type) return;
  1733. const fData = data[options.table],
  1734. self = this;
  1735. if (!fData || fData.length === 0) return;
  1736. if (!fData[0].full_path) return;
  1737. valueCheck.loadTypeFun(options.condition);
  1738. const matchPath = [];
  1739. for (const d of fData) {
  1740. if (this._checkPath(matchPath, d.full_path)) continue;
  1741. if (valueCheck.checkData(ctx, d, options.condition)) {
  1742. matchPath.push({ match: d.full_path, type: options.subType });
  1743. }
  1744. }
  1745. data[options.table] = fData.filter(x => {
  1746. const match = self._checkPath(matchPath, x.full_path) && (options.subType === 'leaf' ? x.is_leaf : true);
  1747. return options.type === 'match' ? match : !match;
  1748. });
  1749. },
  1750. };
  1751. const gatherGcl2 = {
  1752. name: '工程量清单汇总2',
  1753. hint: '会生成新的工程量清单指标&相关项目节指标',
  1754. defaultSetting: {
  1755. billsTable: 'mem_stage_bills',
  1756. posTable: 'mem_stage_pos',
  1757. dealTable: 'deal_bills',
  1758. },
  1759. fun(ctx, data, fieldsKey, options, csRela) {
  1760. if (!options || !options.billsTable || !options.posTable) return;
  1761. const bills = data[options.billsTable];
  1762. if (!bills) return;
  1763. const pos = data[options.posTable];
  1764. if (!pos) return;
  1765. const deal = data[options.dealTable];
  1766. const gclGatherModel = require('./gcl_gather').gclGather;
  1767. const gatherUtil = new gclGatherModel(ctx);
  1768. gatherUtil.gather(bills, pos, deal);
  1769. data.mem_gcl_gather_bills = gatherUtil.gclList;
  1770. data.mem_gcl_gather_xmj = gatherUtil.leafXmjs;
  1771. }
  1772. };
  1773. const gatherMaterialGl = {
  1774. name: '分类汇总调差工料',
  1775. hint: '根据工程量清单分类汇总调差工料,要求先使用【工程量清单汇总2】处理得到工程量清单数据,并引入【调差工料明细】',
  1776. _relaGl(source, data) {
  1777. if (!source) return;
  1778. const gl = source.find(x => {return x.id === data.mb_id;});
  1779. if (gl) {
  1780. data.gl_code = gl.code;
  1781. data.gl_name = gl.name;
  1782. data.gl_unit = gl.unit;
  1783. data.gl_spec = gl.spec;
  1784. data.gl_base_price = gl.base_price;
  1785. data.gl_base_times = gl.base_times;
  1786. data.gl_msg_tp = gl.msg_tp;
  1787. data.gl_msg_times = gl.msg_times;
  1788. data.gl_msg_spread = gl.msg_spread;
  1789. data.gl_m_up_risk = gl.m_up_risk;
  1790. data.gl_m_down_risk = gl.m_down_risk;
  1791. data.gl_m_spread = gl.m_spread;
  1792. }
  1793. },
  1794. fun(ctx, data, fieldsKey, options, csRela) {
  1795. if (!data.mem_gcl_gather_bills || !data.mem_gcl_gather_xmj || !data.mem_material_gl_detail) return;
  1796. const result = [];
  1797. for (const d of data.mem_material_gl_detail) {
  1798. const xmj = data.mem_gcl_gather_xmj.find(x => {
  1799. return d.gcl_id === x.org_gcl_id && d.xmj_id === x.id && (!d.mx_id || d.mx_id === x.mx_id);
  1800. });
  1801. if (!xmj) continue;
  1802. const gcl = data.mem_gcl_gather_bills.find(x => {
  1803. return x.id === xmj.gcl_id;
  1804. });
  1805. if (!gcl) continue;
  1806. const gd = result.find(x => {
  1807. return x.gather_gcl_id === gcl.id && x.mb_id === d.mb_id;
  1808. });
  1809. if (!gd) {
  1810. const newGd = {
  1811. gather_gcl_id: gcl.id,
  1812. mb_id: d.mb_id, gather_qty: d.gather_qty, quantity: d.quantity,
  1813. b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price,
  1814. cur_contract_qty: gcl.contract_qty, cur_contract_tp: gcl.contract_tp,
  1815. cur_qc_qty: gcl.qc_qty, cur_qc_tp: gcl.qc_tp,
  1816. cur_gather_qty: gcl.gather_qty, cur_gather_tp: gcl.gather_tp,
  1817. };
  1818. this._relaGl(data.mem_material_gl, newGd);
  1819. result.push(newGd);
  1820. } else {
  1821. gd.gather_qty = ctx.helper.add(gd.gather_qty, d.gather_qty);
  1822. gd.quantity = ctx.helper.add(gd.quantity, d.quantity);
  1823. }
  1824. }
  1825. data.mem_material_gl_detail = result;
  1826. }
  1827. };
  1828. const masterCross = {
  1829. name: '主从交叉(汇总)数据-通用',
  1830. hint: '此版本,要求表必须是主从关系',
  1831. defaultSetting: {
  1832. main: {
  1833. tableName: 'mem_material_gather_bills',
  1834. prefix: 'm_',
  1835. fields: ['b_code', 'name', 'units', 'unit_price', 'quantity', 'total_price', 'gather_qty', 'gather_tp', 'jiacha'],
  1836. },
  1837. sub: {
  1838. tableName: 'mem_material_gather_gl',
  1839. masterKey: 'id', subKey: 'gather_gcl_id', gatherKey: 'mb_id',
  1840. prefix: 's_',
  1841. fields: ['code', 'name', 'units'],
  1842. quoteFields: ['quantity'],
  1843. gatherFields: [],
  1844. },
  1845. target: 'master_converse',
  1846. base: 'main',
  1847. },
  1848. _converse: function(ctx, options, mainTable, subTable) {
  1849. const data = [];
  1850. const converseData = [];
  1851. subTable.forEach(x => {
  1852. let cd = converseData.find(y => { return x[options.sub.gatherKey] === y[options.sub.gatherKey]});
  1853. if (cd) return;
  1854. cd = {};
  1855. cd[options.sub.gatherKey] = x[options.sub.gatherKey];
  1856. for (const f of options.sub.fields) {
  1857. cd[f] = x[f];
  1858. }
  1859. converseData.push(cd);
  1860. });
  1861. for (const [i, m] of mainTable.entries()) {
  1862. const temp = { cross_index: i+1 };
  1863. for (const f of options.main.fields) {
  1864. temp[options.main.prefix + f] = m[f];
  1865. }
  1866. const rela = subTable.filter(x => { return x[options.sub.subKey] === m[options.sub.masterKey]; });
  1867. if (options.base === 'sub' && rela.length === 0) continue;
  1868. for (const cd of converseData) {
  1869. const nd = JSON.parse(JSON.stringify(temp));
  1870. nd.quote = false;
  1871. for (const prop in cd) {
  1872. nd[options.sub.prefix + prop] = cd[prop];
  1873. }
  1874. rela.forEach(x => {
  1875. if (x[options.sub.gatherKey] !== cd[options.sub.gatherKey]) return;
  1876. if (!nd.quote) {
  1877. for (const f of options.sub.quoteFields) {
  1878. nd[options.sub.prefix + f] = x[f];
  1879. }
  1880. nd.quote = true;
  1881. }
  1882. for (const f of options.sub.gatherFields) {
  1883. nd[options.sub.prefix + f] = ctx.helper.add(nd[options.sub.prefix + f], x[f]);
  1884. }
  1885. });
  1886. data.push(nd);
  1887. }
  1888. }
  1889. return data;
  1890. },
  1891. fun(ctx, data, fieldsKey, options, csRela) {
  1892. if (!options || !options.main || !options.sub || !options.target) return;
  1893. const mainTable = data[options.main.tableName], subTable = data[options.sub.tableName];
  1894. if (!mainTable || !subTable) return;
  1895. data[options.target] = this._converse(ctx, options, mainTable, subTable);
  1896. },
  1897. };
  1898. const orderBy = {
  1899. name: "简单排序",
  1900. hint: "仅提供简单的排序",
  1901. defaultSetting: {
  1902. table: 'mem_material_sum_gl',
  1903. orders: [['m_order', 'asc']],
  1904. },
  1905. fun(ctx, data, fieldsKey, options, csRela) {
  1906. if (!options || !options.table || !options.orders) return;
  1907. const ascSort = function (x, y, field) {
  1908. if (!x[field]) return 1;
  1909. if (!y[field]) return -1;
  1910. return x[field] === y[field] ? 0 : (x[field] > y[field] ? 1 : -1);
  1911. };
  1912. const descSort = function (x, y, field) {
  1913. if (!x[field]) return -1;
  1914. if (!y[field]) return 1;
  1915. return x[field] === y[field] ? 0 : (x[field] > y[field] ? -1 : 1);
  1916. };
  1917. const orderFun = [];
  1918. for (const order of options.orders) {
  1919. if (order.length !== 2) return;
  1920. if (['asc', 'desc'].indexOf(order[1]) === -1) return;
  1921. orderFun.push({ field: order[0], fun: order[1] === 'asc' ? ascSort : descSort });
  1922. }
  1923. data[options.table].sort((x, y) => {
  1924. for (const order of orderFun) {
  1925. const sortResult = order.fun(x, y, order.field);
  1926. if (sortResult !== 0) return sortResult;
  1927. }
  1928. });
  1929. }
  1930. };
  1931. const GroupBy = {
  1932. name: "分类汇总",
  1933. hint: "类似Sql语句中的Group By",
  1934. defaultSetting: {
  1935. table: 'mem_change_bills',
  1936. keyFields: ['cid', 'gcl_id'],
  1937. sumFields: ['qty'],
  1938. outputTable: '',
  1939. },
  1940. fun(ctx, data, fieldsKey, options, csRela) {
  1941. if (!options || !options.table || !options.keyFields) return;
  1942. if (!options.sumFields) options.sumFields = [];
  1943. if (!options.outputTable) options.outputTable = options.table;
  1944. const groupData = [];
  1945. for (const d of data[options.table]) {
  1946. let gd = groupData.find(x => {
  1947. for (const kf of options.keyFields) {
  1948. if (x[kf] !== d[kf]) return false;
  1949. }
  1950. return true;
  1951. });
  1952. if (!gd) {
  1953. gd = JSON.parse(JSON.stringify(d));
  1954. groupData.push(gd);
  1955. } else {
  1956. for (const sf of options.sumFields) {
  1957. gd[sf] = ctx.helper.add(gd[sf], d[sf]);
  1958. }
  1959. }
  1960. }
  1961. data[options.outputTable] = groupData;
  1962. }
  1963. };
  1964. const analysisObj = {
  1965. changeSort,
  1966. gatherGcl,
  1967. sortGcl,
  1968. gatherChapter,
  1969. join,
  1970. getChapterCode,
  1971. filter,
  1972. union,
  1973. gatherStagePay,
  1974. addSumChapter,
  1975. auditSelect,
  1976. datetimeFormat,
  1977. gatherSelectConverse,
  1978. stageSelectConverse,
  1979. materialSumSelectConverse,
  1980. sortPos,
  1981. unionPos,
  1982. splitXmjCode,
  1983. loadCooperationData,
  1984. getChangeLedger,
  1985. treeFilter,
  1986. gatherGcl2,
  1987. gatherMaterialGl,
  1988. masterCross,
  1989. orderBy,
  1990. GroupBy,
  1991. };
  1992. const analysisDefine = (function(obj) {
  1993. const result = [];
  1994. for (const o in obj) {
  1995. const analysis = obj[o];
  1996. if (analysis.name && analysis.fun) {
  1997. result.push({
  1998. name: analysis.name,
  1999. key: o,
  2000. hint: analysis.hint,
  2001. intro: analysis.intro,
  2002. url: '/help?m=report&s=analysis&d=' + o,
  2003. });
  2004. }
  2005. }
  2006. return result;
  2007. })(analysisObj);
  2008. module.exports = {
  2009. analysisObj,
  2010. analysisDefine,
  2011. };