gcl_gather.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. 'use strict';
  2. /**
  3. *
  4. * 清单汇总(需使用 decimal.min.js, zh_calc.js, path_tree.js, lodash.js)
  5. *
  6. * @author Mai
  7. * @date
  8. * @version
  9. */
  10. const gclGatherModel = (function () {
  11. // 需要汇总计算的字段
  12. const ledgerGatherFields = ['quantity', 'total_price', 'deal_qty', 'deal_tp',
  13. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp',
  14. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp',
  15. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp'];
  16. const posGatherFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty',
  17. 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  18. // 初始化 清单树
  19. const gsTreeSetting = {
  20. id: 'ledger_id',
  21. pid: 'ledger_pid',
  22. order: 'order',
  23. level: 'level',
  24. rootId: -1,
  25. keys: ['id', 'tender_id', 'ledger_id'],
  26. stageId: 'id',
  27. };
  28. gsTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'];
  29. const gsTree = createNewPathTree('stage', gsTreeSetting);
  30. // 初始化 部位明细
  31. const posSetting = {
  32. id: 'id', ledgerId: 'lid',
  33. updateFields: ['contract_qty', 'qc_qty'],
  34. };
  35. const gsPos = new StagePosData(posSetting);
  36. let deal = [];
  37. const gclList = [], leafXmjs = [];
  38. const mergeChar = ';';
  39. /**
  40. * 将所有数据加载至树结构
  41. *
  42. * @param ledger - 台账数据
  43. * @param curStage - 当期计量数据
  44. * @param preStage - 截止上期计量数据
  45. * @param bgl - 变更令数据
  46. */
  47. function loadLedgerData (ledger, curStage, preStage, bgl) {
  48. // 加载树结构数据
  49. gsTree.loadDatas(ledger);
  50. // 加载本期计量数据
  51. if (curStage) {
  52. gsTree.loadCurStageData(curStage);
  53. }
  54. if (preStage) {
  55. gsTree.loadPreStageData(preStage);
  56. }
  57. }
  58. function loadPosData(pos, curPos, prePos) {
  59. gsPos.loadDatas(pos);
  60. gsPos.loadCurStageData(curPos);
  61. gsPos.loadPreStageData(prePos);
  62. }
  63. function loadDealBillsData(dealBills) {
  64. deal = dealBills;
  65. }
  66. function gatherfields(obj, src, fields) {
  67. if (obj && src) {
  68. for (const f of fields) {
  69. obj[f] = ZhCalc.add(obj[f], src[f]);
  70. }
  71. }
  72. }
  73. /**
  74. * 新建 清单汇总节点
  75. * @param node - 最底层 工程量清单节点
  76. * @returns {{b_code: *|string[], name, unit, unit_price: *|string[], leafXmjs: Array}}
  77. */
  78. function newGclNode(node) {
  79. const gcl = {
  80. b_code: node.b_code,
  81. name: node.name,
  82. unit: node.unit,
  83. unit_price: node.unit_price,
  84. leafXmjs: [],
  85. };
  86. gclList.push(gcl);
  87. return gcl;
  88. }
  89. /**
  90. * 获取清单汇总节点
  91. *
  92. * @param node - 最底层清单节点
  93. * @returns {*}
  94. */
  95. function getGclNode(node) {
  96. const gcl = gclList.find(function (g) {
  97. return g.b_code === node.b_code &&
  98. (g.name || node.name ? g.name === node.name : true) &&
  99. (g.unit || node.unit ? g.unit === node.unit : true) &&
  100. checkZero(ZhCalc.sub(g.unit_price, node.unit_price));
  101. });
  102. if (gcl) {
  103. return gcl
  104. } else {
  105. return newGclNode(node);
  106. }
  107. }
  108. /**
  109. * 检查 text 是否是Peg
  110. * e.g. K123+000(true) Kab+123(false) K123.234+234(false) K12+324.234(true)
  111. *
  112. * @param text
  113. * @returns {*}
  114. * @constructor
  115. */
  116. function CheckPeg(text) {
  117. const pegReg = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  118. return pegReg.test(text);
  119. }
  120. /**
  121. * 获取 桩号节点
  122. * @param node - 检索起始节点
  123. * @returns {*}
  124. */
  125. function getPegNode (node) {
  126. if (node) {
  127. if (CheckPeg(node.name)) {
  128. return node;
  129. } else {
  130. const parent = gsTree.getParent(node);
  131. return parent ? getPegNode(parent) : null;
  132. }
  133. }
  134. }
  135. /**
  136. * 获取节点的第N层父节点
  137. *
  138. * @param node - 节点(检索起点)
  139. * @param level - 第N层
  140. * @returns {*}
  141. */
  142. function getNodeByLevel(node, level) {
  143. let cur = node;
  144. while (cur && cur.level > level) {
  145. cur = gsTree.getParent(cur);
  146. }
  147. return cur;
  148. }
  149. /**
  150. * 获取 单位工程
  151. *
  152. * @param xmj - 计量单元(最底层项目节)
  153. * @returns {string}
  154. */
  155. function getDwgc(peg, xmj) {
  156. if (peg) {
  157. return peg.name;
  158. } else {
  159. const node = getNodeByLevel(xmj, 2);
  160. return node ? node.name : '';
  161. }
  162. }
  163. /**
  164. * 获取 分部工程
  165. *
  166. * @param peg - 桩号节点
  167. * @param xmj - 计量单元(最底层项目节)
  168. * @returns {string}
  169. */
  170. function getFbgc(peg, xmj) {
  171. if (peg && peg.id !== xmj.id) {
  172. const node = getNodeByLevel(xmj, peg.level + 1);
  173. return node ? node.name : '';
  174. } else {
  175. const node = getNodeByLevel(xmj, 3);
  176. return node ? node.name : '';
  177. }
  178. }
  179. /**
  180. * 获取 分项工程
  181. *
  182. * @param peg - 桩号节点
  183. * @param xmj - 计量单元(最底层项目节)
  184. * @returns {string}
  185. */
  186. function getFxgc(peg, xmj) {
  187. if (!peg) {
  188. const node = getNodeByLevel(xmj, 4);
  189. return node ? node.name : '';
  190. } else if (peg.id === xmj.id) {
  191. if (xmj.level > 4) {
  192. let value = '';
  193. for (let level = 4; level < xmj.level; level++) {
  194. const node = getNodeByLevel(xmj, level);
  195. value = value === '' ? node.name : value + mergeChar + node.name;
  196. }
  197. return value;
  198. } else {
  199. return '';
  200. }
  201. } else {
  202. if (peg.level + 2 < xmj.level) {
  203. let value = '';
  204. for (let level = peg.level + 2; level < xmj.level; level++) {
  205. const node = getNodeByLevel(xmj, level);
  206. value = value === '' ? node.name : value + mergeChar + node.name;
  207. }
  208. return value;
  209. } else {
  210. return '';
  211. }
  212. }
  213. }
  214. /**
  215. * 新建 最底层项目节 缓存数据
  216. * @param leafXmj
  217. * @returns {{id, code: *|string[], jldy, fbgc: string, fxgc: string, dwgc: string, bwmx: string, drawing_code: string}}
  218. */
  219. function newCacheLeafXmj(leafXmj) {
  220. const peg = getPegNode(leafXmj);
  221. const cacheLX = {
  222. id: leafXmj.id,
  223. code: leafXmj.code,
  224. jldy: leafXmj.name,
  225. fbgc: getFbgc(peg, leafXmj),
  226. fxgc: getFxgc(peg, leafXmj),
  227. dwgc: getDwgc(peg, leafXmj),
  228. drawing_code: leafXmj.drawing_code,
  229. };
  230. leafXmjs.push(cacheLX);
  231. return cacheLX;
  232. }
  233. /**
  234. * 获取缓存的最底层项目节数据
  235. *
  236. * @param leafXmj - 最底层项目节
  237. * @returns {*}
  238. */
  239. function getCacheLeafXmj(leafXmj) {
  240. const cacheLX = leafXmjs.find(function (lx) {
  241. return lx.id === leafXmj.id;
  242. });
  243. if (!cacheLX) {
  244. return newCacheLeafXmj(leafXmj);
  245. } else {
  246. return cacheLX;
  247. }
  248. }
  249. /**
  250. * 汇总节点
  251. * @param node - 最底层 工程量清单 节点
  252. * @param leafXmj - 所属 最底层 项目节
  253. */
  254. function loadGatherGclNode(node, leafXmj) {
  255. const gcl = getGclNode(node);
  256. gatherfields(gcl, node, ledgerGatherFields);
  257. const cacheLeafXmj = getCacheLeafXmj(leafXmj);
  258. const posRange = gsPos.getLedgerPos(node.id);
  259. const detail = posRange && posRange.length > 0 ? posRange : [node];
  260. for (const d of detail) {
  261. const dx = _.assign({}, cacheLeafXmj);
  262. gatherfields(dx, d, posGatherFields);
  263. dx.gcl_id = node.id;
  264. if (d.name !== node.name) {
  265. dx.bwmx = d.name;
  266. dx.mx_id = d.id;
  267. }
  268. if (d.drawing_code) {
  269. dx.drawing_code = d.drawing_code;
  270. }
  271. gcl.leafXmjs.push(dx);
  272. }
  273. }
  274. /**
  275. * (递归)汇总树节点
  276. * @param nodes - 汇总节点列表
  277. * @param leafXmj - 汇总节点所属的底层项目节
  278. */
  279. function recursiveGatherGclData(nodes, leafXmj) {
  280. for (const node of nodes) {
  281. if (node.b_code) {
  282. if (node.children.length > 0) {
  283. const gcl = getGclNode(node);
  284. recursiveGatherGclData(node.children, leafXmj);
  285. } else {
  286. loadGatherGclNode(node, leafXmj);
  287. }
  288. } else if (node.children.length > 0) {
  289. recursiveGatherGclData(node.children, node);
  290. }
  291. }
  292. }
  293. function gatherDealBillsData() {
  294. if (deal && deal.length > 0) {
  295. for (const node of deal) {
  296. node.b_code = node.code;
  297. const gcl = getGclNode(node);
  298. if (!node.quantity || !node.unit_price) continue;
  299. gcl.deal_bills_qty = ZhCalc.add(gcl.deal_bills_qty, node.quantity);
  300. gcl.deal_bills_tp = ZhCalc.add(gcl.deal_bills_tp, node.total_price);
  301. }
  302. }
  303. }
  304. function calculateGatherData() {
  305. for (const gcl of gclList) {
  306. gcl.pre_gather_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.pre_qc_qty);
  307. gcl.pre_gather_tp = ZhCalc.add(gcl.pre_contract_tp, gcl.pre_qc_tp);
  308. gcl.gather_qty = ZhCalc.add(gcl.contract_qty, gcl.qc_qty);
  309. gcl.end_contract_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.contract_qty);
  310. gcl.end_qc_qty = ZhCalc.add(gcl.pre_qc_qty, gcl.qc_qty);
  311. gcl.end_gather_qty = ZhCalc.add(gcl.pre_gather_qty, gcl.gather_qty);
  312. gcl.end_final_qty = ZhCalc.add(gcl.end_qc_qty, gcl.quantity);
  313. gcl.gather_tp = ZhCalc.add(gcl.contract_tp, gcl.qc_tp);
  314. gcl.end_contract_tp = ZhCalc.add(gcl.pre_contract_tp, gcl.contract_tp);
  315. gcl.end_qc_tp = ZhCalc.add(gcl.pre_qc_tp, gcl.qc_tp);
  316. gcl.end_gather_tp = ZhCalc.add(gcl.pre_gather_tp, gcl.gather_tp);
  317. gcl.dgn_price = ZhCalc.round(ZhCalc.div(gcl.total_price, gcl.dgn_qty1), 2);
  318. gcl.end_final_qty = ZhCalc.add(gcl.end_qc_qty, gcl.quantity);
  319. gcl.end_final_tp = ZhCalc.add(gcl.end_qc_tp, gcl.total_price);
  320. gcl.end_gather_percent = gcl.end_final_qty && gcl.end_gather_qty
  321. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.end_final_qty), 100, 2)
  322. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_tp), 100, 2);
  323. for (const xmj of gcl.leafXmjs) {
  324. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  325. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  326. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  327. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  328. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  329. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  330. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  331. }
  332. }
  333. }
  334. function compareCode(str1, str2, symbol = '-') {
  335. if (!str1) {
  336. return 1;
  337. } else if (!str2) {
  338. return -1;
  339. }
  340. function compareSubCode(code1, code2) {
  341. if (numReg.test(code1)) {
  342. if (numReg.test(code2)) {
  343. return parseInt(code1) - parseInt(code2);
  344. } else {
  345. return -1
  346. }
  347. } else {
  348. if (numReg.test(code2)) {
  349. return 1;
  350. } else {
  351. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
  352. }
  353. }
  354. }
  355. const numReg = /^[0-9]+$/;
  356. const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
  357. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  358. const iCompare = compareSubCode(aCodes[i], bCodes[i]);
  359. if (iCompare !== 0) {
  360. return iCompare;
  361. }
  362. }
  363. return aCodes.length - bCodes.length;
  364. }
  365. /**
  366. * 根据树结构 清单汇总
  367. */
  368. function gatherGclData() {
  369. // 清空旧数据
  370. if (gclList.length > 0) {
  371. gclList.length = 0; //splice(0, gclList.length);
  372. }
  373. recursiveGatherGclData(gsTree.children, null);
  374. calculateGatherData();
  375. gatherDealBillsData();
  376. gclList.sort(function (a, b) {
  377. return compareCode(a.b_code, b.b_code);
  378. });
  379. return gclList;
  380. }
  381. function checkDiffer(gclList) {
  382. for (const gcl of gclList) {
  383. gcl.differ = false;
  384. }
  385. for (const [i, gcl] of gclList.entries()) {
  386. if (i === gclList.length - 1) continue;
  387. const next = gclList[i+1];
  388. if (gcl.b_code === next.b_code) {
  389. if (gcl.name !== next.name || gcl.unit !== next.unit || !checkZero(gcl.unit_price - next.unit_price)) {
  390. gcl.differ = true;
  391. next.differ = true;
  392. }
  393. }
  394. }
  395. }
  396. function _getCalcChapter(chapter, option) {
  397. const gclChapter = [], otherChapter = {}, gclChapterFilter = [];
  398. let serialNo = 1;
  399. for (const c of chapter) {
  400. const cc = { code: c.code, name: c.name, cType: 1 };
  401. cc.serialNo = serialNo++;
  402. cc.filter = '^[^0-9]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  403. gclChapter.push(cc);
  404. }
  405. gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo+1 });
  406. otherChapter.hj = { name: '合计(C=A+B+Z)', cType: 41, serialNo: serialNo+5, deal_bills_tp: option.zlj.deal_bills_tp };
  407. gclChapterFilter.push({node_type: option.jrg.value});
  408. gclChapterFilter.push({field: 'name', part: option.jrg.text});
  409. const zlChapter = {
  410. name: '暂列金额(Z)', cType: 32, serialNo: serialNo+4,
  411. deal_bills_tp: option.zlj.deal_bills_tp, match: [], matchPath: []
  412. };
  413. zlChapter.match.push({node_type: option.zlj.value});
  414. zlChapter.match.push({field: 'name', part: option.zlj.text});
  415. otherChapter.zlj = zlChapter;
  416. otherChapter.qd = { name: '清单小计(A)', cType: 11, serialNo: serialNo+2 };
  417. otherChapter.fqd = { name: '非清单项费用(B)', cType: 31, serialNo: serialNo+3 };
  418. return [gclChapter, otherChapter, gclChapterFilter];
  419. }
  420. function _gatherChapterFields(chapter, data, fields) {
  421. for (const f of fields) {
  422. chapter[f] = ZhCalc.add(chapter[f], data[f]);
  423. }
  424. }
  425. function _getGclChapter(chapter, data) {
  426. for (const c of chapter) {
  427. if (c.filter) {
  428. const reg = new RegExp(c.filter);
  429. if (reg.test(data.b_code)) {
  430. return c;
  431. }
  432. } else {
  433. return c;
  434. }
  435. }
  436. }
  437. function _checkFilter(d, filter) {
  438. for (const f of filter) {
  439. if (f.node_type && f.node_type === d.node_type) return true;
  440. if (f.field) {
  441. if (f.part && d[f.field] && d[f.field].indexOf(f.part) >= 0) return true;
  442. if (f.all && d[f.all] && d[f.all] === f.all) return true;
  443. }
  444. }
  445. return false;
  446. }
  447. function gatherChapterData(chapter, option, fields) {
  448. const chapterFilterPath = [];
  449. const checkFilterPath = function (data, filterPath) {
  450. for (const fp of filterPath) {
  451. if (data.full_path.indexOf(fp + '-') === 0 || data.full_path === fp) return true;
  452. }
  453. return false;
  454. };
  455. const [gclChapter, otherChapter, gclChapterFilter] = _getCalcChapter(chapter, option);
  456. for (const d of gsTree.nodes) {
  457. if (_checkFilter(d, gclChapterFilter)) chapterFilterPath.push(d.full_path);
  458. if (_checkFilter(d, otherChapter.zlj.match)) otherChapter.zlj.matchPath.push(d.full_path);
  459. if (d.children && d.children.length > 0) continue;
  460. if (checkFilterPath(d,otherChapter.zlj.matchPath)) {
  461. gatherfields(otherChapter.zlj, d, fields);
  462. gatherfields(otherChapter.hj, d, fields);
  463. } else {
  464. gatherfields(otherChapter.hj, d, fields);
  465. if (d.b_code) {
  466. gatherfields(otherChapter.qd, d, fields);
  467. }
  468. if (!d.b_code || d.b_code === '') {
  469. gatherfields(otherChapter.fqd, d, fields);
  470. }
  471. if (d.b_code) {
  472. const c = checkFilterPath(d, chapterFilterPath)
  473. ? gclChapter.find(x => { return x.cType === 21})
  474. : _getGclChapter(gclChapter, d);
  475. gatherfields(c, d, fields);
  476. }
  477. }
  478. }
  479. for (const d of deal) {
  480. if (!d.quantity || !d.unit_price) continue;
  481. otherChapter.hj.deal_bills_tp = ZhCalc.add(otherChapter.hj.deal_bills_tp, d.total_price);
  482. otherChapter.qd.deal_bills_tp = ZhCalc.add(otherChapter.qd.deal_bills_tp, d.total_price);
  483. const c = _getGclChapter(gclChapter, d);
  484. c.deal_bills_tp = ZhCalc.add(c.deal_bills_tp, d.total_price);
  485. }
  486. const result = gclChapter.concat([otherChapter.hj, otherChapter.zlj, otherChapter.qd, otherChapter.fqd]);
  487. result.sort((x, y) => {return x.serialNo - y.serialNo});
  488. return result;
  489. }
  490. return {
  491. loadLedgerData,
  492. loadPosData,
  493. loadDealBillsData,
  494. gatherGclData,
  495. checkDiffer,
  496. gatherChapterData,
  497. };
  498. })();