gcl_gather.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. 'use strict';
  2. /**
  3. * 清单汇总(需使用path_tree.js, lodash.js)
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const gclGatherModel = (function () {
  10. // 需要汇总计算的字段
  11. const ledgerGatherFields = ['quantity', 'total_price', 'deal_qty', 'deal_tp',
  12. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp',
  13. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp'];
  14. const posGatherFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty', 'end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  15. // 初始化 清单树
  16. const gsTreeSetting = {
  17. id: 'ledger_id',
  18. pid: 'ledger_pid',
  19. order: 'order',
  20. level: 'level',
  21. rootId: -1,
  22. keys: ['id', 'tender_id', 'ledger_id'],
  23. stageId: 'id',
  24. };
  25. gsTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'];
  26. gsTreeSetting.calcFields = ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'end_contract_tp', 'end_qc_tp', 'end_gather_tp'];
  27. gsTreeSetting.calcFun = function (node) {
  28. if (node.children && node.children.length === 0) {
  29. node.gather_qty = ZhCalc.plus(node.contract_qty, node.qc_qty);
  30. node.end_contract_qty = ZhCalc.plus(node.pre_contract_qty, node.contract_qty);
  31. node.end_qc_qty = ZhCalc.plus(node.pre_qc_qty, node.qc_qty);
  32. node.end_gather_qty = ZhCalc.plus(node.pre_gather_qty, node.gather_qty);
  33. }
  34. node.gather_tp = ZhCalc.plus(node.contract_tp, node.qc_tp);
  35. node.end_contract_tp = ZhCalc.plus(node.pre_contract_tp, node.contract_tp);
  36. node.end_qc_tp = ZhCalc.plus(node.pre_qc_tp, node.qc_tp);
  37. node.end_gather_tp = ZhCalc.plus(node.pre_gather_tp, node.gather_tp);
  38. node.dgn_price = ZhCalc.round(ZhCalc.divide(node.total_price, node.dgn_qty1), 2);
  39. };
  40. const gsTree = createNewPathTree('stage', gsTreeSetting);
  41. // 初始化 部位明细
  42. const posSetting = {
  43. id: 'id', ledgerId: 'lid',
  44. updateFields: ['contract_qty', 'qc_qty'],
  45. };
  46. posSetting.calcFun = function (pos) {
  47. pos.gather_qty = ZhCalc.plus(pos.contract_qty, pos.qc_qty);
  48. };
  49. const gsPos = new StagePosData(posSetting);
  50. let deal = [];
  51. const gclList = [], leafXmjs = [];
  52. /**
  53. * 将所有数据加载至树结构
  54. *
  55. * @param ledger - 台账数据
  56. * @param curStage - 当期计量数据
  57. * @param preStage - 截止上期计量数据
  58. * @param bgl - 变更令数据
  59. */
  60. function loadLedgerData (ledger, curStage, preStage, bgl) {
  61. // 加载树结构数据
  62. gsTree.loadDatas(ledger);
  63. // 加载本期计量数据
  64. if (curStage) {
  65. gsTree.loadCurStageData(curStage);
  66. }
  67. // todo 加载截止上期计量数据
  68. // todo 加载变更令数据
  69. // 根据设置 计算 台账树结构
  70. treeCalc.calculateAll(gsTree);
  71. }
  72. function loadPosData(pos, curPos, prePos) {
  73. gsPos.loadDatas(pos);
  74. gsPos.loadCurStageData(curPos);
  75. gsPos.loadPreStageData(prePos);
  76. }
  77. function loadDealBillsData(dealBills) {
  78. deal = dealBills;
  79. }
  80. function gatherfields(obj, src, fields) {
  81. if (obj && src) {
  82. for (const f of fields) {
  83. obj[f] = ZhCalc.plus(obj[f], src[f]);
  84. }
  85. }
  86. }
  87. /**
  88. * 新建 清单汇总节点
  89. * @param node - 最底层 工程量清单节点
  90. * @returns {{b_code: *|string[], name, unit, unit_price: *|string[], leafXmjs: Array}}
  91. */
  92. function newGclNode(node) {
  93. const gcl = {
  94. b_code: node.b_code,
  95. name: node.name,
  96. unit: node.unit,
  97. unit_price: node.unit_price,
  98. leafXmjs: [],
  99. };
  100. gclList.push(gcl);
  101. return gcl;
  102. }
  103. /**
  104. * 获取清单汇总节点
  105. *
  106. * @param node - 最底层清单节点
  107. * @returns {*}
  108. */
  109. function getGclNode(node) {
  110. const gcl = gclList.find(function (g) {
  111. return g.b_code === node.b_code && g.name === node.name && g.unit === node.unit && checkZero(g.unit_price - node.unit_price);
  112. });
  113. if (gcl) {
  114. return gcl
  115. } else {
  116. return newGclNode(node);
  117. }
  118. }
  119. /**
  120. * 检查 text 是否是Peg
  121. * e.g. K123+000(true) Kab+123(false) K123.234+234(false) K12+324.234(true)
  122. *
  123. * @param text
  124. * @returns {*}
  125. * @constructor
  126. */
  127. function CheckPeg(text) {
  128. const pegReg = /[kK][0-9][++][0-9]{3}/;
  129. return pegReg.test(text);
  130. }
  131. /**
  132. * 获取 桩号节点
  133. * @param node - 检索起始节点
  134. * @returns {*}
  135. */
  136. function getPegNode (node) {
  137. if (node) {
  138. if (CheckPeg(node.name)) {
  139. return node;
  140. } else {
  141. const parent = gsTree.getParent(node);
  142. return parent ? getPegNode(parent) : null;
  143. }
  144. }
  145. }
  146. /**
  147. * 获取节点的第N层父节点
  148. *
  149. * @param node - 节点(检索起点)
  150. * @param level - 第N层
  151. * @returns {*}
  152. */
  153. function getNodeByLevel(node, level) {
  154. let cur = node;
  155. while (cur && cur.level > level) {
  156. cur = gsTree.getParent(cur);
  157. }
  158. return cur;
  159. }
  160. /**
  161. * 获取 单位工程
  162. *
  163. * @param xmj - 计量单元(最底层项目节)
  164. * @returns {string}
  165. */
  166. function getDwgc(xmj) {
  167. const node = getNodeByLevel(xmj, 2);
  168. return node ? node.name : '';
  169. }
  170. /**
  171. * 获取 分部工程
  172. *
  173. * @param peg - 桩号节点
  174. * @param xmj - 计量单元(最底层项目节)
  175. * @returns {string}
  176. */
  177. function getFbgc(peg, xmj) {
  178. if (peg && peg.id !== xmj.id) {
  179. const node = getNodeByLevel(xmj, peg.level + 1);
  180. return node ? node.name : '';
  181. } else {
  182. const node = getNodeByLevel(xmj, 3);
  183. return node ? node.name : '';
  184. }
  185. }
  186. /**
  187. * 获取 分项工程
  188. *
  189. * @param peg - 桩号节点
  190. * @param xmj - 计量单元(最底层项目节)
  191. * @returns {string}
  192. */
  193. function getFxgc(peg, xmj) {
  194. if (!peg) {
  195. const node = getNodeByLevel(xmj, 4);
  196. return node ? node.name : '';
  197. } else if (peg.id === xmj.id) {
  198. if (xmj.level > 4) {
  199. let value = '';
  200. for (let level = 4; level < xmj.level; level++) {
  201. const node = getNodeByLevel(xmj, level);
  202. value = value === '' ? node.name : node.name + '-' + value;
  203. }
  204. return value;
  205. } else {
  206. return '';
  207. }
  208. } else {
  209. if (peg.level - 2 > xmj.level) {
  210. let value = '';
  211. for (let level = peg.level + 2; level < xmj.level; level++) {
  212. const node = getNodeByLevel(xmj, level);
  213. value = value === '' ? node.name : node.name + '-' + value;
  214. }
  215. return value;
  216. } else {
  217. return '';
  218. }
  219. }
  220. }
  221. /**
  222. * 新建 最底层项目节 缓存数据
  223. * @param leafXmj
  224. * @returns {{id, code: *|string[], jldy, fbgc: string, fxgc: string, dwgc: string, bwmx: string, drawing_code: string}}
  225. */
  226. function newCacheLeafXmj(leafXmj) {
  227. const peg = getPegNode(leafXmj);
  228. const cacheLX = {
  229. id: leafXmj.id,
  230. code: leafXmj.code,
  231. jldy: leafXmj.name,
  232. fbgc: getFbgc(peg, leafXmj),
  233. fxgc: getFxgc(peg, leafXmj),
  234. dwgc: getDwgc(leafXmj),
  235. drawing_code: leafXmj.drawing_code,
  236. };
  237. leafXmjs.push(cacheLX);
  238. return cacheLX;
  239. }
  240. /**
  241. * 获取缓存的最底层项目节数据
  242. *
  243. * @param leafXmj - 最底层项目节
  244. * @returns {*}
  245. */
  246. function getCacheLeafXmj(leafXmj) {
  247. const cacheLX = leafXmjs.find(function (lx) {
  248. return lx.id === leafXmj.id;
  249. });
  250. if (!cacheLX) {
  251. return newCacheLeafXmj(leafXmj);
  252. } else {
  253. return cacheLX;
  254. }
  255. }
  256. /**
  257. * 汇总节点
  258. * @param node - 最底层 工程量清单 节点
  259. * @param leafXmj - 所属 最底层 项目节
  260. */
  261. function loadGatherGclNode(node, leafXmj) {
  262. const gcl = getGclNode(node);
  263. gatherfields(gcl, node, ledgerGatherFields);
  264. const cacheLeafXmj = getCacheLeafXmj(leafXmj);
  265. const posRange = gsPos.getLedgerPos(node.id);
  266. const detail = posRange && posRange.length > 0 ? posRange : [node];
  267. for (const d of detail) {
  268. const dx = _.assign({}, cacheLeafXmj);
  269. gatherfields(dx, d, posGatherFields);
  270. dx.gcl_id = node.id;
  271. if (d.name !== node.name) {
  272. dx.bwmx = d.name;
  273. }
  274. if (d.drawing_code) {
  275. dx.drawaing_code = d.drawing_code;
  276. }
  277. gcl.leafXmjs.push(dx);
  278. }
  279. }
  280. /**
  281. * (递归)汇总树节点
  282. * @param nodes - 汇总节点列表
  283. * @param leafXmj - 汇总节点所属的底层项目节
  284. */
  285. function recursiveGatherGclData(nodes, leafXmj) {
  286. for (const node of nodes) {
  287. if (node.b_code) {
  288. if (node.children.length > 0) {
  289. recursiveGatherGclData(node.children, leafXmj);
  290. } else {
  291. loadGatherGclNode(node, leafXmj);
  292. }
  293. } else if (node.children.length > 0) {
  294. recursiveGatherGclData(node.children, node);
  295. }
  296. }
  297. }
  298. function gatherDealBillsData() {
  299. if (deal && deal.length > 0) {
  300. for (const node of deal) {
  301. node.b_code = node.code;
  302. const gcl = getGclNode(node);
  303. gcl.deal_bills_qty = node.quantity;
  304. gcl.deal_bills_tp = node.total_price;
  305. }
  306. }
  307. }
  308. /**
  309. * 根据树结构 清单汇总
  310. */
  311. function gatherGclData() {
  312. // 清空旧数据
  313. if (gclList.length > 0) {
  314. gclList.length = 0; //splice(0, gclList.length);
  315. }
  316. recursiveGatherGclData(gsTree.children, null);
  317. gatherDealBillsData();
  318. gclList.sort(function (a, b) {
  319. function compareCode(code1, code2) {
  320. if (numReg.test(code1)) {
  321. if (numReg.test(code2)) {
  322. return _.toNumber(code1) - _.toNumber(code2);
  323. } else {
  324. return -1
  325. }
  326. } else {
  327. if (numReg.test(code2)) {
  328. return 1;
  329. } else {
  330. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1);
  331. }
  332. }
  333. }
  334. const numReg = /^[0-9]+$/;
  335. const aCodes = a.b_code.split('-'), bCodes = b.b_code.split('-');
  336. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  337. const iCompare = compareCode(aCodes[i], bCodes[i]);
  338. if (iCompare !== 0) {
  339. return iCompare;
  340. }
  341. }
  342. });
  343. return gclList;
  344. }
  345. return {
  346. loadLedgerData,
  347. loadPosData,
  348. loadDealBillsData,
  349. gatherGclData,
  350. };
  351. })();