gcl_gather.js 16 KB

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