gcl_gather.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 = node.quantity;
  300. 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_tp = ZhCalc.add(gcl.end_qc_tp, gcl.total_price);
  319. gcl.end_gather_percent = ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_tp), 100, 2);
  320. for (const xmj of gcl.leafXmjs) {
  321. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  322. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  323. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  324. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  325. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  326. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  327. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  328. }
  329. }
  330. }
  331. function compareCode(str1, str2, symbol = '-') {
  332. if (!str1) {
  333. return 1;
  334. } else if (!str2) {
  335. return -1;
  336. }
  337. function compareSubCode(code1, code2) {
  338. if (numReg.test(code1)) {
  339. if (numReg.test(code2)) {
  340. return parseInt(code1) - parseInt(code2);
  341. } else {
  342. return -1
  343. }
  344. } else {
  345. if (numReg.test(code2)) {
  346. return 1;
  347. } else {
  348. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
  349. }
  350. }
  351. }
  352. const numReg = /^[0-9]+$/;
  353. const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
  354. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  355. const iCompare = compareSubCode(aCodes[i], bCodes[i]);
  356. if (iCompare !== 0) {
  357. return iCompare;
  358. }
  359. }
  360. return aCodes.length - bCodes.length;
  361. }
  362. /**
  363. * 根据树结构 清单汇总
  364. */
  365. function gatherGclData() {
  366. // 清空旧数据
  367. if (gclList.length > 0) {
  368. gclList.length = 0; //splice(0, gclList.length);
  369. }
  370. recursiveGatherGclData(gsTree.children, null);
  371. calculateGatherData();
  372. gatherDealBillsData();
  373. gclList.sort(function (a, b) {
  374. return compareCode(a.b_code, b.b_code);
  375. });
  376. return gclList;
  377. }
  378. function checkDiffer(gclList) {
  379. for (const gcl of gclList) {
  380. gcl.differ = false;
  381. }
  382. for (const [i, gcl] of gclList.entries()) {
  383. if (i === gclList.length - 1) continue;
  384. const next = gclList[i+1];
  385. if (gcl.b_code === next.b_code) {
  386. if (gcl.name !== next.name || gcl.unit !== next.unit || !checkZero(gcl.unit_price - next.unit_price)) {
  387. gcl.differ = true;
  388. next.differ = true;
  389. }
  390. }
  391. }
  392. }
  393. function _getCalcChapter(chapter) {
  394. const gclChapter = [], otherChapter = [];
  395. let serialNo = 1;
  396. for (const c of chapter) {
  397. const cc = { code: c.code, name: c.name, cType: 1 };
  398. cc.serialNo = serialNo++;
  399. cc.filter = '^[^0-9]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}-';
  400. gclChapter.push(cc);
  401. }
  402. gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo++, });
  403. otherChapter.push({ name: '清单小计(A)', cType: 11, serialNo: serialNo++ });
  404. otherChapter.push({ name: '非清单项费用(B)', cType: 31, serialNo: serialNo++ });
  405. otherChapter.push({ name: '合计(C=A+B)', cType: 41, serialNo: serialNo });
  406. return [gclChapter, otherChapter];
  407. }
  408. function _gatherChapterFields(chapter, data, fields) {
  409. for (const f of fields) {
  410. chapter[f] = ZhCalc.add(chapter[f], data[f]);
  411. }
  412. }
  413. function _getGclChapter(chapter, data) {
  414. for (const c of chapter) {
  415. if (c.filter) {
  416. const reg = new RegExp(c.filter);
  417. if (reg.test(data.b_code)) {
  418. return c;
  419. }
  420. } else {
  421. return c;
  422. }
  423. }
  424. }
  425. function gatherChapterData(chapter, fields) {
  426. const [gclChapter, otherChapter] = _getCalcChapter(chapter);
  427. for (const d of gsTree.datas) {
  428. if (d.children && d.children.length > 0) continue;
  429. for (const c of otherChapter) {
  430. if (c.cType === 41) {
  431. gatherfields(c, d, fields);
  432. } else if (c.cType === 31 && (!d.b_code || d.b_code === '')) {
  433. gatherfields(c, d, fields);
  434. } else if (c.cType === 11 && (d.b_code)) {
  435. gatherfields(c, d, fields);
  436. }
  437. }
  438. if (d.b_code) {
  439. const c = _getGclChapter(gclChapter, d);
  440. gatherfields(c, d, fields);
  441. }
  442. }
  443. for (const d of deal) {
  444. if (!d.quantity || !d.unit_price) continue;
  445. for (const c of otherChapter) {
  446. if (c.cType === 41 || c.cType === 11) {
  447. c.deal_bills_tp = ZhCalc.add(c.deal_bills_tp, d.total_price);
  448. }
  449. }
  450. const c = _getGclChapter(gclChapter, d);
  451. c.deal_bills_tp = ZhCalc.add(c.deal_bills_tp, d.total_price);
  452. }
  453. return gclChapter.concat(otherChapter);
  454. }
  455. return {
  456. loadLedgerData,
  457. loadPosData,
  458. loadDealBillsData,
  459. gatherGclData,
  460. checkDiffer,
  461. gatherChapterData,
  462. };
  463. })();