gcl_gather.js 19 KB

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