gcl_gather.js 27 KB

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