gcl_gather.js 16 KB

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