gcl_gather.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. let ledgerGatherFields = ['quantity', 'total_price', 'deal_qty', 'deal_tp',
  13. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp',
  14. 'pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_qc_minus_qty',
  15. 'end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_qc_minus_qty'];
  16. let posGatherFields = ['quantity', 'contract_qty', 'qc_qty', 'gather_qty','qc_minus_qty',
  17. 'pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty', 'pre_qc_minus_qty',
  18. 'end_contract_qty', 'end_qc_qty', 'end_gather_qty', 'end_qc_minus_qty'];
  19. let ancillaryGclGatherFields = ['quantity'];
  20. // 初始化 清单树
  21. const gsTreeSetting = {
  22. id: 'ledger_id',
  23. pid: 'ledger_pid',
  24. order: 'order',
  25. level: 'level',
  26. rootId: -1,
  27. keys: ['id', 'tender_id', 'ledger_id'],
  28. stageId: 'id',
  29. };
  30. gsTreeSetting.updateFields = ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'contract_pc_tp', 'qc_pc_tp', 'pc_tp', 'qc_minus_qty'];
  31. const gsTree = createNewPathTree('stage', gsTreeSetting);
  32. // 初始化 部位明细
  33. const posSetting = {
  34. id: 'id', ledgerId: 'lid',
  35. updateFields: ['contract_qty', 'qc_qty', 'qc_minus_qty'],
  36. };
  37. const gsPos = new StagePosData(posSetting);
  38. const gsAncGcl = createAncillaryGcl({ id: 'id', masterId: 'lid', sort: [['g_order', 'asc']] });
  39. let deal = [], change;
  40. const gclList = [], leafXmjs = [];
  41. const mergeChar = ';';
  42. let tpDecimal = 0;
  43. function loadDecimal(decimal) {
  44. tpDecimal = decimal.tp;
  45. }
  46. function loadGatherField(ledger, pos) {
  47. if (ledger) ledgerGatherFields = ledger;
  48. if (pos) posGatherFields = pos;
  49. }
  50. /**
  51. * 将所有数据加载至树结构
  52. *
  53. * @param ledger - 台账数据
  54. * @param curStage - 当期计量数据
  55. * @param preStage - 截止上期计量数据
  56. * @param bgl - 变更令数据
  57. */
  58. function loadLedgerData (ledger, curStage, preStage) {
  59. // 加载树结构数据
  60. gsTree.loadDatas(ledger);
  61. // 加载本期计量数据
  62. if (curStage) {
  63. gsTree.loadCurStageData(curStage);
  64. }
  65. if (preStage) {
  66. gsTree.loadPreStageData(preStage);
  67. }
  68. }
  69. function loadPosData(pos, curPos, prePos) {
  70. gsPos.loadDatas(pos);
  71. gsPos.loadCurStageData(curPos);
  72. gsPos.loadPreStageData(prePos);
  73. }
  74. function loadAncillaryGclData(datas) {
  75. gsAncGcl.loadDatas(datas);
  76. }
  77. function loadDealBillsData(dealBills) {
  78. deal = dealBills;
  79. }
  80. function loadChangeData(data) {
  81. change = data;
  82. }
  83. function loadStageChangeData(cur, pre) {
  84. const gatherDatas = function(datas, field) {
  85. for (const d of datas) {
  86. const c = change.find(x => { return x.cid === d.cid; });
  87. if (!c) continue;
  88. const cb = c.bills.find(x => { return x.id === d.cbid; });
  89. if (!cb) continue;
  90. if (d.no_value) {
  91. cb[field + '_minus'] = ZhCalc.add(cb[field + '_minus'], d.qty);
  92. } else {
  93. cb[field] = ZhCalc.add(cb[field], d.qty);
  94. }
  95. }
  96. };
  97. gatherDatas(cur, 'cur_qty');
  98. gatherDatas(pre, 'pre_qty');
  99. }
  100. function gatherfields(obj, src, fields) {
  101. if (obj && src) {
  102. if (fields instanceof Array) {
  103. for (const f of fields) {
  104. obj[f] = ZhCalc.add(obj[f], src[f]);
  105. }
  106. } else if (typeof fields === "function") {
  107. for (const prop in src) {
  108. if (fields(prop)) {
  109. obj[prop] = ZhCalc.add(obj[prop], src[prop]);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * 新建 清单汇总节点
  117. * @param node - 最底层 工程量清单节点
  118. * @returns {{b_code: *|string[], name, unit, unit_price: *|string[], leafXmjs: Array}}
  119. */
  120. function newGclNode(node) {
  121. const gcl = {
  122. b_code: node.b_code,
  123. name: node.name,
  124. unit: node.unit,
  125. unit_price: node.unit_price,
  126. org_price: node.org_price,
  127. leafXmjs: [],
  128. ancGcl: [],
  129. change: [],
  130. };
  131. gclList.push(gcl);
  132. return gcl;
  133. }
  134. /**
  135. * 获取清单汇总节点
  136. *
  137. * @param node - 最底层清单节点
  138. * @returns {*}
  139. */
  140. function getGclNode(node) {
  141. const gcl = gclList.find(function (g) {
  142. return g.b_code === node.b_code &&
  143. (g.name || node.name ? g.name === node.name : true) &&
  144. (g.unit || node.unit ? g.unit === node.unit : true) &&
  145. checkZero(ZhCalc.sub(g.unit_price, node.unit_price));
  146. // && (g.org_price && node.org_price ? g.org_price === node.org_price : true);
  147. });
  148. if (gcl) {
  149. if (node.org_price) gcl.org_price = node.org_price;
  150. return gcl;
  151. } else {
  152. return newGclNode(node);
  153. }
  154. }
  155. /**
  156. * 检查 text 是否是Peg
  157. * e.g. K123+000(true) Kab+123(false) K123.234+234(false) K12+324.234(true)
  158. *
  159. * @param text
  160. * @returns {*}
  161. * @constructor
  162. */
  163. function CheckPeg(text) {
  164. const pegReg = /[a-zA-Z]*[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  165. return pegReg.test(text);
  166. }
  167. /**
  168. * 获取 桩号节点
  169. * @param node - 检索起始节点
  170. * @returns {*}
  171. */
  172. function getPegNode (node) {
  173. if (node) {
  174. if (CheckPeg(node.name)) {
  175. return node;
  176. } else {
  177. const parent = gsTree.getParent(node);
  178. return parent ? getPegNode(parent) : null;
  179. }
  180. }
  181. }
  182. /**
  183. * 获取节点的第N层父节点
  184. *
  185. * @param node - 节点(检索起点)
  186. * @param level - 第N层
  187. * @returns {*}
  188. */
  189. function getNodeByLevel(node, level) {
  190. let cur = node;
  191. while (cur && cur.level > level) {
  192. cur = gsTree.getParent(cur);
  193. }
  194. return cur;
  195. }
  196. /**
  197. * 获取 单位工程
  198. *
  199. * @param xmj - 计量单元(最底层项目节)
  200. * @returns {string}
  201. */
  202. function getDwgc(peg, xmj) {
  203. if (peg) {
  204. return peg.name;
  205. } else {
  206. const node = getNodeByLevel(xmj, 2);
  207. return node ? node.name : '';
  208. }
  209. }
  210. /**
  211. * 获取 分部工程
  212. *
  213. * @param peg - 桩号节点
  214. * @param xmj - 计量单元(最底层项目节)
  215. * @returns {string}
  216. */
  217. function getFbgc(peg, xmj) {
  218. if (peg && peg.id !== xmj.id) {
  219. const node = getNodeByLevel(xmj, peg.level + 1);
  220. return node ? node.name : '';
  221. } else {
  222. const node = getNodeByLevel(xmj, 3);
  223. return node ? node.name : '';
  224. }
  225. }
  226. /**
  227. * 获取 分项工程
  228. *
  229. * @param peg - 桩号节点
  230. * @param xmj - 计量单元(最底层项目节)
  231. * @returns {string}
  232. */
  233. function getFxgc(peg, xmj) {
  234. if (!peg) {
  235. const node = getNodeByLevel(xmj, 4);
  236. return node ? node.name : '';
  237. } else if (peg.id === xmj.id) {
  238. if (xmj.level > 4) {
  239. let value = '';
  240. for (let level = 4; level < xmj.level; level++) {
  241. const node = getNodeByLevel(xmj, level);
  242. value = value === '' ? node.name : value + mergeChar + node.name;
  243. }
  244. return value;
  245. } else {
  246. return '';
  247. }
  248. } else {
  249. if (peg.level + 2 < xmj.level) {
  250. let value = '';
  251. for (let level = peg.level + 2; level < xmj.level; level++) {
  252. const node = getNodeByLevel(xmj, level);
  253. value = value === '' ? node.name : value + mergeChar + node.name;
  254. }
  255. return value;
  256. } else {
  257. return '';
  258. }
  259. }
  260. }
  261. /**
  262. * 新建 最底层项目节 缓存数据
  263. * @param leafXmj
  264. * @returns {{id, code: *|string[], jldy, fbgc: string, fxgc: string, dwgc: string, bwmx: string, drawing_code: string}}
  265. */
  266. function newCacheLeafXmj(leafXmj) {
  267. const peg = getPegNode(leafXmj);
  268. const cacheLX = {
  269. id: leafXmj.id,
  270. code: leafXmj.code,
  271. jldy: leafXmj.name,
  272. fbgc: getFbgc(peg, leafXmj),
  273. fxgc: getFxgc(peg, leafXmj),
  274. dwgc: getDwgc(peg, leafXmj),
  275. drawing_code: leafXmj.drawing_code,
  276. };
  277. leafXmjs.push(cacheLX);
  278. return cacheLX;
  279. }
  280. /**
  281. * 获取缓存的最底层项目节数据
  282. *
  283. * @param leafXmj - 最底层项目节
  284. * @returns {*}
  285. */
  286. function getCacheLeafXmj(leafXmj) {
  287. const cacheLX = leafXmjs.find(function (lx) {
  288. return lx.id === leafXmj.id;
  289. });
  290. if (!cacheLX) {
  291. return newCacheLeafXmj(leafXmj);
  292. } else {
  293. return cacheLX;
  294. }
  295. }
  296. function gatherAncGclData(node, gcl, cacheLeafXmj) {
  297. const gclPart = gsAncGcl.getPartData(node.id);
  298. if (!gclPart || gclPart.length === 0) return;
  299. for (const ag of gclPart) {
  300. const condition = _.assign({ is_aux: ag.is_aux, name: ag.name, unit: ag.unit }, cacheLeafXmj);
  301. let nag = _.find(gcl.ancGcl, condition);
  302. if (!nag) {
  303. nag = condition;
  304. gcl.ancGcl.push(condition);
  305. }
  306. gatherfields(nag, ag, ancillaryGclGatherFields);
  307. }
  308. }
  309. /**
  310. * 汇总节点
  311. * @param node - 最底层 工程量清单 节点
  312. * @param leafXmj - 所属 最底层 项目节
  313. */
  314. function loadGatherGclNode(node, leafXmj) {
  315. const gcl = getGclNode(node);
  316. gatherfields(gcl, node, ledgerGatherFields);
  317. const cacheLeafXmj = getCacheLeafXmj(leafXmj);
  318. const posRange = gsPos.getLedgerPos(node.id);
  319. const detail = posRange && posRange.length > 0 ? posRange : [node];
  320. for (const d of detail) {
  321. const dx = _.assign({}, cacheLeafXmj);
  322. gatherfields(dx, d, posGatherFields);
  323. dx.gcl_id = node.id;
  324. if (d.name !== node.name) {
  325. dx.bwmx = d.name;
  326. dx.mx_id = d.id;
  327. } else if (dx.gcl_id !== d.id) {
  328. dx.mx_id = d.id;
  329. }
  330. if (d.drawing_code) {
  331. dx.drawing_code = d.drawing_code;
  332. }
  333. dx.settle_status = (posRange && posRange.length > 0 ? d.settle_status : node.settle_status) || 0 ;
  334. gcl.leafXmjs.push(dx);
  335. }
  336. gatherAncGclData(node, gcl, cacheLeafXmj);
  337. }
  338. /**
  339. * (递归)汇总树节点
  340. * @param nodes - 汇总节点列表
  341. * @param leafXmj - 汇总节点所属的底层项目节
  342. */
  343. function recursiveGatherGclData(nodes, leafXmj) {
  344. for (const node of nodes) {
  345. if (node.b_code) {
  346. if (node.children.length > 0) {
  347. // const gcl = getGclNode(node);
  348. recursiveGatherGclData(node.children, leafXmj);
  349. } else {
  350. loadGatherGclNode(node, leafXmj);
  351. }
  352. } else if (node.children.length > 0) {
  353. recursiveGatherGclData(node.children, node);
  354. }
  355. }
  356. }
  357. function gatherDealBillsData() {
  358. if (deal && deal.length > 0) {
  359. for (const node of deal) {
  360. node.b_code = node.code;
  361. const gcl = getGclNode(node);
  362. if (!node.quantity || !node.unit_price) continue;
  363. gcl.deal_bills_qty = ZhCalc.add(gcl.deal_bills_qty, node.quantity);
  364. gcl.deal_bills_tp = ZhCalc.add(gcl.deal_bills_tp, node.total_price);
  365. }
  366. }
  367. }
  368. function gatherChangeData() {
  369. if (!change || change.length === 0) return;
  370. for (const c of change) {
  371. const decimal = c.tp_decimal || tpDecimal;
  372. for (const cb of c.bills) {
  373. cb.b_code = cb.code;
  374. const gcl = getGclNode(cb);
  375. gcl.change_bills_qty = ZhCalc.add(gcl.change_bills_qty, cb.checked_amount);
  376. gcl.change_bills_tp = ZhCalc.add(gcl.change_bills_tp, cb.checked_price);
  377. let gcb = gcl.change.find(x => { return x.cid === cb.cid && x.is_valuation === cb.is_valuation; });
  378. if (!gcb) {
  379. gcb = { cid: c.cid, code: c.code, name: c.name, is_valuation: cb.is_valuation };
  380. gcl.change.push(gcb);
  381. }
  382. gcb.org_qty = ZhCalc.add(gcb.org_qty, cb.oamount2);
  383. gcb.org_tp = ZhCalc.add(gcb.org_tp, ZhCalc.mul(cb.oamount2, gcl.unit_price, decimal));
  384. gcb.c_qty = ZhCalc.add(gcb.c_qty, cb.checked_amount);
  385. gcb.c_tp = ZhCalc.add(gcb.c_tp, cb.checked_price);
  386. console.log(cb.code, cb.pre_qty, cb.pre_qty_minus);
  387. gcb.cur_qty = ZhCalc.add(gcb.cur_qty, cb.cur_qty);
  388. gcb.cur_qty_minus = ZhCalc.add(gcb.cur_qty_minus, cb.cur_qty_minus);
  389. gcb.pre_qty = ZhCalc.add(gcb.pre_qty, cb.pre_qty);
  390. gcb.pre_qty_minus = ZhCalc.add(gcb.pre_qty_minus, cb.pre_qty_minus);
  391. }
  392. }
  393. }
  394. function calculateGatherData() {
  395. for (const gcl of gclList) {
  396. gcl.pre_gather_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.pre_qc_qty);
  397. gcl.pre_gather_tp = ZhCalc.add(gcl.pre_contract_tp, gcl.pre_qc_tp);
  398. gcl.gather_qty = ZhCalc.add(gcl.contract_qty, gcl.qc_qty);
  399. gcl.end_contract_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.contract_qty);
  400. gcl.end_qc_qty = ZhCalc.add(gcl.pre_qc_qty, gcl.qc_qty);
  401. gcl.end_gather_qty = ZhCalc.add(gcl.pre_gather_qty, gcl.gather_qty);
  402. gcl.end_qc_minus_qty = ZhCalc.add(gcl.pre_qc_minus_qty, gcl.qc_minus_qty);
  403. gcl.gather_tp = ZhCalc.sum([gcl.contract_tp, gcl.qc_tp, gcl.pc_tp]);
  404. gcl.end_contract_tp = ZhCalc.sum([gcl.pre_contract_tp, gcl.contract_tp, gcl.contract_pc_tp]);
  405. gcl.end_qc_tp = ZhCalc.sum([gcl.pre_qc_tp, gcl.qc_tp, gcl.qc_pc_tp]);
  406. gcl.end_gather_tp = ZhCalc.add(gcl.pre_gather_tp, gcl.gather_tp);
  407. gcl.dgn_price = ZhCalc.round(ZhCalc.div(gcl.total_price, gcl.dgn_qty1), 2);
  408. gcl.end_final_qty = ZhCalc.add(gcl.end_qc_qty, gcl.quantity);
  409. gcl.end_final_tp = ZhCalc.add(gcl.end_qc_tp, gcl.total_price);
  410. gcl.final_qty = ZhCalc.add(gcl.quantity, gcl.change_bills_qty);
  411. gcl.final_tp = ZhCalc.add(gcl.total_price, gcl.change_bills_tp);
  412. gcl.deal_final_qty = ZhCalc.add(gcl.deal_bills_qty, gcl.change_bills_qty);
  413. gcl.deal_final_tp = ZhCalc.add(gcl.deal_bills_tp, gcl.change_bills_tp);
  414. gcl.final_1_qty = ZhCalc.add(gcl.quantity, gcl.end_qc_minus_qty);
  415. gcl.final_1_tp = ZhCalc.mul(gcl.unit_price, gcl.final_1_qty, tpDecimal);
  416. gcl.deal_final_1_qty = ZhCalc.add(gcl.deal_bills_qty, gcl.end_qc_minus_qty);
  417. gcl.deal_final_1_tp = ZhCalc.add(gcl.unit_price, gcl.deal_final_1_qty, tpDecimal);
  418. gcl.end_final_1_qty = ZhCalc.add(gcl.final_1_qty, gcl.end_qc_qty);
  419. gcl.end_final_1_tp = ZhCalc.add(gcl.final_1_tp, gcl.end_qc_tp);
  420. gcl.end_gather_percent = gcl.end_gather_qty && gcl.end_final_qty
  421. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.end_final_qty), 100, 2)
  422. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_tp), 100, 2);
  423. gcl.end_final_1_percent = gcl.end_gather_qty && gcl.end_final_1_qty
  424. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.end_final_1_qty), 100, 2)
  425. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_1_tp), 100, 2);
  426. gcl.final_percent = gcl.final_qty && gcl.end_gather_qty
  427. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.final_qty), 100, 2)
  428. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.final_tp), 100, 2);
  429. let settle_status = [];
  430. for (const xmj of gcl.leafXmjs) {
  431. if (settle_status.indexOf(xmj.settle_status) < 0) settle_status.push(xmj.settle_status);
  432. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  433. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  434. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  435. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  436. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  437. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  438. xmj.end_qc_minus_qty = ZhCalc.add(xmj.pre_qc_minus_qty, xmj.qc_minus_qty);
  439. xmj.final_1_qty = ZhCalc.add(xmj.quantity, xmj.end_qc_minus_qty);
  440. xmj.end_final_1_qty = ZhCalc.add(xmj.final_1_qty, xmj.end_qc_qty);
  441. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  442. xmj.end_final_1_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_1_qty), 100, 2);
  443. }
  444. for (const c of gcl.change) {
  445. c.cur_tp = ZhCalc.mul(c.cur_qty, gcl.unit_price, tpDecimal);
  446. c.pre_tp = ZhCalc.mul(c.pre_qty, gcl.unit_price, tpDecimal);
  447. c.end_qty = ZhCalc.add(c.cur_qty, c.pre_qty);
  448. c.end_tp = ZhCalc.add(c.cur_tp, c.pre_tp);
  449. c.end_qty_minus = ZhCalc.add(c.cur_qty_minus, c.pre_qty_minus);
  450. }
  451. gcl.settle_status = settle_status.length === 1 ? settle_status[0] : 1;
  452. }
  453. }
  454. function compareCode(str1, str2, symbol = '-') {
  455. if (!str1) {
  456. return 1;
  457. } else if (!str2) {
  458. return -1;
  459. }
  460. function compareSubCode(code1, code2) {
  461. if (numReg.test(code1)) {
  462. if (numReg.test(code2)) {
  463. return parseInt(code1) - parseInt(code2);
  464. } else {
  465. return -1
  466. }
  467. } else {
  468. if (numReg.test(code2)) {
  469. return 1;
  470. } else {
  471. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
  472. }
  473. }
  474. }
  475. const numReg = /^[0-9]+$/;
  476. const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
  477. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  478. const iCompare = compareSubCode(aCodes[i], bCodes[i]);
  479. if (iCompare !== 0) {
  480. return iCompare;
  481. }
  482. }
  483. return aCodes.length - bCodes.length;
  484. }
  485. /**
  486. * 根据树结构 清单汇总
  487. */
  488. function gatherGclData() {
  489. // 清空旧数据
  490. if (gclList.length > 0) {
  491. gclList.length = 0; //splice(0, gclList.length);
  492. }
  493. recursiveGatherGclData(gsTree.children, null);
  494. gatherDealBillsData();
  495. gatherChangeData();
  496. calculateGatherData();
  497. gclList.sort(function (a, b) {
  498. return compareCode(a.b_code, b.b_code) || ZhCalc.sub(a.unit_price, b.unit_price);
  499. });
  500. return gclList;
  501. }
  502. function checkDiffer(gclList) {
  503. for (const gcl of gclList) {
  504. gcl.differ = false;
  505. }
  506. for (const [i, gcl] of gclList.entries()) {
  507. if (i === gclList.length - 1) continue;
  508. const next = gclList[i+1];
  509. if (gcl.b_code === next.b_code) {
  510. if (gcl.name !== next.name || gcl.unit !== next.unit || !checkZero(gcl.unit_price - next.unit_price)) {
  511. gcl.differ = true;
  512. next.differ = true;
  513. }
  514. }
  515. }
  516. }
  517. function _getCalcChapter(chapter, option) {
  518. const gclChapter = [], otherChapter = {}, gclChapterFilter = [];
  519. let serialNo = 1;
  520. for (const c of chapter) {
  521. const cc = { code: c.code, name: c.name, cType: 1 };
  522. cc.serialNo = serialNo++;
  523. cc.filter = '^[^0-9]*' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  524. gclChapter.push(cc);
  525. }
  526. gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo+1 });
  527. otherChapter.hj = { name: '合计(C=A+B+Z)', cType: 41, serialNo: serialNo+5, deal_bills_tp: option.zlj.deal_bills_tp };
  528. gclChapterFilter.push({node_type: option.jrg.value});
  529. gclChapterFilter.push({field: 'name', part: option.jrg.text});
  530. const zlChapter = {
  531. name: '暂列金额(Z)', cType: 32, serialNo: serialNo+4,
  532. deal_bills_tp: option.zlj.deal_bills_tp, match: [], matchPath: []
  533. };
  534. zlChapter.match.push({node_type: option.zlj.value});
  535. zlChapter.match.push({field: 'name', part: option.zlj.text});
  536. otherChapter.zlj = zlChapter;
  537. otherChapter.qd = { name: '清单小计(A)', cType: 11, serialNo: serialNo+2 };
  538. otherChapter.fqd = { name: '非清单项费用(B)', cType: 31, serialNo: serialNo+3 };
  539. return [gclChapter, otherChapter, gclChapterFilter];
  540. }
  541. function _gatherChapterFields(chapter, data, fields) {
  542. for (const f of fields) {
  543. chapter[f] = ZhCalc.add(chapter[f], data[f]);
  544. }
  545. }
  546. function _getGclChapter(chapter, data) {
  547. for (const c of chapter) {
  548. if (c.filter) {
  549. const reg = new RegExp(c.filter);
  550. if (reg.test(data.b_code)) {
  551. return c;
  552. }
  553. } else {
  554. return c;
  555. }
  556. }
  557. }
  558. function _checkFilter(d, filter) {
  559. for (const f of filter) {
  560. if (f.node_type && f.node_type === d.node_type) return true;
  561. if (f.field) {
  562. if (f.part && d[f.field] && d[f.field].indexOf(f.part) >= 0) return true;
  563. if (f.all && d[f.all] && d[f.all] === f.all) return true;
  564. }
  565. }
  566. return false;
  567. }
  568. function gatherChapterData(chapter, option, fields) {
  569. const chapterFilterPath = [];
  570. const checkFilterPath = function (data, filterPath) {
  571. for (const fp of filterPath) {
  572. if (data.full_path.indexOf(fp + '-') === 0 || data.full_path === fp) return true;
  573. }
  574. return false;
  575. };
  576. const [gclChapter, otherChapter, gclChapterFilter] = _getCalcChapter(chapter, option);
  577. for (const d of gsTree.nodes) {
  578. if (_checkFilter(d, gclChapterFilter)) chapterFilterPath.push(d.full_path);
  579. if (_checkFilter(d, otherChapter.zlj.match)) otherChapter.zlj.matchPath.push(d.full_path);
  580. if (d.children && d.children.length > 0) continue;
  581. if (checkFilterPath(d,otherChapter.zlj.matchPath)) {
  582. gatherfields(otherChapter.zlj, d, fields);
  583. gatherfields(otherChapter.hj, d, fields);
  584. } else {
  585. gatherfields(otherChapter.hj, d, fields);
  586. if (d.b_code) {
  587. gatherfields(otherChapter.qd, d, fields);
  588. }
  589. if (!d.b_code || d.b_code === '') {
  590. gatherfields(otherChapter.fqd, d, fields);
  591. }
  592. if (d.b_code) {
  593. const c = checkFilterPath(d, chapterFilterPath)
  594. ? gclChapter.find(x => { return x.cType === 21})
  595. : _getGclChapter(gclChapter, d);
  596. gatherfields(c, d, fields);
  597. }
  598. }
  599. }
  600. for (const d of deal) {
  601. if (!d.quantity || !d.unit_price) continue;
  602. otherChapter.hj.deal_bills_tp = ZhCalc.add(otherChapter.hj.deal_bills_tp, d.total_price);
  603. otherChapter.qd.deal_bills_tp = ZhCalc.add(otherChapter.qd.deal_bills_tp, d.total_price);
  604. const c = _getGclChapter(gclChapter, d);
  605. c.deal_bills_tp = ZhCalc.add(c.deal_bills_tp, d.total_price);
  606. }
  607. const result = gclChapter.concat([otherChapter.hj, otherChapter.zlj, otherChapter.qd, otherChapter.fqd]);
  608. result.sort((x, y) => {return x.serialNo - y.serialNo});
  609. return result;
  610. }
  611. function _gatherLeafXmj(gcl, fields = ['bwmx']) {
  612. gcl.gatherLeafXmjs = [];
  613. for (const lx of gcl.leafXmjs) {
  614. const condition = {};
  615. for (const f of fields) {
  616. condition[f] = lx[f];
  617. }
  618. let glx = _.find(gcl.gatherLeafXmjs, condition);
  619. if (!glx) {
  620. glx = JSON.parse(JSON.stringify(lx));
  621. gcl.gatherLeafXmjs.push(glx);
  622. } else {
  623. gatherfields(glx, lx, posGatherFields);
  624. }
  625. }
  626. for (const xmj of gcl.gatherLeafXmjs) {
  627. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  628. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  629. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  630. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  631. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  632. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  633. xmj.end_qc_minus_qty = ZhCalc.add(xmj.pre_qc_minus_qty, xmj.qc_minus_qty);
  634. xmj.final_1_qty = ZhCalc.add(xmj.quantity, xmj.end_qc_minus_qty);
  635. xmj.end_final_1_qty = ZhCalc.add(xmj.final_1_qty, xmj.end_qc_qty);
  636. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  637. xmj.end_final_1_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_1_qty), 100, 2);
  638. }
  639. }
  640. function reGatherLeafXmj(fields) {
  641. gclList.forEach(g => {
  642. _gatherLeafXmj(g, fields);
  643. });
  644. }
  645. function _gatherAncillaryGcl(gcl, fields = ['jldy']) {
  646. fields.push('is_aux', 'name', 'unit');
  647. gcl.gatherAncGcl = [];
  648. for (const lx of gcl.ancGcl) {
  649. const condition = {};
  650. for (const f of fields) {
  651. condition[f] = lx[f];
  652. }
  653. let glx = _.find(gcl.gatherAncGcl, condition);
  654. if (!glx) {
  655. glx = JSON.parse(JSON.stringify(lx));
  656. gcl.gatherAncGcl.push(glx);
  657. } else {
  658. gatherfields(glx, lx, ancillaryGclGatherFields);
  659. }
  660. }
  661. gcl.gatherAncGcl.sort((a, b) => {
  662. if (a.is_aux === b.is_aux) {
  663. return a.name < b.name ? -1 : 1;
  664. } else {
  665. return a.is_aux - b.is_aux;
  666. }
  667. });
  668. }
  669. function reGatherAncillaryGcl(fields) {
  670. gclList.forEach(g => {
  671. _gatherAncillaryGcl(g, fields);
  672. });
  673. }
  674. return {
  675. loadGatherField,
  676. loadDecimal,
  677. loadLedgerData,
  678. loadPosData,
  679. loadAncillaryGclData,
  680. loadDealBillsData,
  681. loadChangeData,
  682. loadStageChangeData,
  683. gatherGclData,
  684. checkDiffer,
  685. gatherChapterData,
  686. reGatherLeafXmj,
  687. reGatherAncillaryGcl,
  688. };
  689. })();