gcl_gather.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. gcb.cur_qty = ZhCalc.add(gcb.cur_qty, cb.cur_qty);
  387. gcb.cur_qty_minus = ZhCalc.add(gcb.cur_qty_minus, cb.cur_qty_minus);
  388. gcb.pre_qty = ZhCalc.add(gcb.pre_qty, cb.pre_qty);
  389. gcb.pre_qty_minus = ZhCalc.add(gcb.pre_qty_minus, cb.pre_qty_minus);
  390. }
  391. }
  392. }
  393. function calculateGatherData() {
  394. for (const gcl of gclList) {
  395. gcl.pre_gather_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.pre_qc_qty);
  396. gcl.pre_gather_tp = ZhCalc.add(gcl.pre_contract_tp, gcl.pre_qc_tp);
  397. gcl.gather_qty = ZhCalc.add(gcl.contract_qty, gcl.qc_qty);
  398. gcl.end_contract_qty = ZhCalc.add(gcl.pre_contract_qty, gcl.contract_qty);
  399. gcl.end_qc_qty = ZhCalc.add(gcl.pre_qc_qty, gcl.qc_qty);
  400. gcl.end_gather_qty = ZhCalc.add(gcl.pre_gather_qty, gcl.gather_qty);
  401. gcl.end_qc_minus_qty = ZhCalc.add(gcl.pre_qc_minus_qty, gcl.qc_minus_qty);
  402. gcl.gather_tp = ZhCalc.sum([gcl.contract_tp, gcl.qc_tp, gcl.pc_tp]);
  403. gcl.end_contract_tp = ZhCalc.sum([gcl.pre_contract_tp, gcl.contract_tp, gcl.contract_pc_tp]);
  404. gcl.end_qc_tp = ZhCalc.sum([gcl.pre_qc_tp, gcl.qc_tp, gcl.qc_pc_tp]);
  405. gcl.end_gather_tp = ZhCalc.add(gcl.pre_gather_tp, gcl.gather_tp);
  406. gcl.dgn_price = ZhCalc.round(ZhCalc.div(gcl.total_price, gcl.dgn_qty1), 2);
  407. gcl.end_final_qty = ZhCalc.add(gcl.end_qc_qty, gcl.quantity);
  408. gcl.end_final_tp = ZhCalc.add(gcl.end_qc_tp, gcl.total_price);
  409. gcl.final_qty = ZhCalc.add(gcl.quantity, gcl.change_bills_qty);
  410. gcl.final_tp = ZhCalc.add(gcl.total_price, gcl.change_bills_tp);
  411. gcl.deal_final_qty = ZhCalc.add(gcl.deal_bills_qty, gcl.change_bills_qty);
  412. gcl.deal_final_tp = ZhCalc.add(gcl.deal_bills_tp, gcl.change_bills_tp);
  413. gcl.final_1_qty = ZhCalc.add(gcl.quantity, gcl.end_qc_minus_qty);
  414. gcl.final_1_tp = ZhCalc.mul(gcl.unit_price, gcl.final_1_qty, tpDecimal);
  415. gcl.deal_final_1_qty = ZhCalc.add(gcl.deal_bills_qty, gcl.end_qc_minus_qty);
  416. gcl.deal_final_1_tp = ZhCalc.add(gcl.unit_price, gcl.deal_final_1_qty, tpDecimal);
  417. gcl.end_final_1_qty = ZhCalc.add(gcl.final_1_qty, gcl.end_qc_qty);
  418. gcl.end_final_1_tp = ZhCalc.add(gcl.final_1_tp, gcl.end_qc_tp);
  419. gcl.end_gather_percent = gcl.end_gather_qty && gcl.end_final_qty
  420. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.end_final_qty), 100, 2)
  421. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_tp), 100, 2);
  422. gcl.end_final_1_percent = gcl.end_gather_qty && gcl.end_final_1_qty
  423. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.end_final_1_qty), 100, 2)
  424. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.end_final_1_tp), 100, 2);
  425. gcl.final_percent = gcl.final_qty && gcl.end_gather_qty
  426. ? ZhCalc.mul(ZhCalc.div(gcl.end_gather_qty, gcl.final_qty), 100, 2)
  427. : ZhCalc.mul(ZhCalc.div(gcl.end_gather_tp, gcl.final_tp), 100, 2);
  428. let settle_status = [];
  429. for (const xmj of gcl.leafXmjs) {
  430. if (settle_status.indexOf(xmj.settle_status) < 0) settle_status.push(xmj.settle_status);
  431. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  432. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  433. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  434. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  435. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  436. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  437. xmj.end_qc_minus_qty = ZhCalc.add(xmj.pre_qc_minus_qty, xmj.qc_minus_qty);
  438. xmj.final_1_qty = ZhCalc.add(xmj.quantity, xmj.end_qc_minus_qty);
  439. xmj.end_final_1_qty = ZhCalc.add(xmj.final_1_qty, xmj.end_qc_qty);
  440. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  441. xmj.end_final_1_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_1_qty), 100, 2);
  442. }
  443. for (const c of gcl.change) {
  444. c.cur_tp = ZhCalc.mul(c.cur_qty, gcl.unit_price, tpDecimal);
  445. c.pre_tp = ZhCalc.mul(c.pre_qty, gcl.unit_price, tpDecimal);
  446. c.end_qty = ZhCalc.add(c.cur_qty, c.pre_qty);
  447. c.end_tp = ZhCalc.add(c.cur_tp, c.pre_tp);
  448. c.end_qty_minus = ZhCalc.add(c.cur_qty_minus, c.pre_qty_minus);
  449. }
  450. gcl.settle_status = settle_status.length === 1 ? settle_status[0] : 1;
  451. }
  452. }
  453. function compareCode(str1, str2, symbol = '-') {
  454. if (!str1) {
  455. return 1;
  456. } else if (!str2) {
  457. return -1;
  458. }
  459. function compareSubCode(code1, code2) {
  460. if (numReg.test(code1)) {
  461. if (numReg.test(code2)) {
  462. return parseInt(code1) - parseInt(code2);
  463. } else {
  464. return -1
  465. }
  466. } else {
  467. if (numReg.test(code2)) {
  468. return 1;
  469. } else {
  470. return code1 === code2 ? 0 : (code1 < code2 ? -1 : 1); //code1.localeCompare(code2);
  471. }
  472. }
  473. }
  474. const numReg = /^[0-9]+$/;
  475. const aCodes = str1.split(symbol), bCodes = str2.split(symbol);
  476. for (let i = 0, iLength = Math.min(aCodes.length, bCodes.length); i < iLength; ++i) {
  477. const iCompare = compareSubCode(aCodes[i], bCodes[i]);
  478. if (iCompare !== 0) {
  479. return iCompare;
  480. }
  481. }
  482. return aCodes.length - bCodes.length;
  483. }
  484. /**
  485. * 根据树结构 清单汇总
  486. */
  487. function gatherGclData() {
  488. // 清空旧数据
  489. if (gclList.length > 0) {
  490. gclList.length = 0; //splice(0, gclList.length);
  491. }
  492. recursiveGatherGclData(gsTree.children, null);
  493. gatherDealBillsData();
  494. gatherChangeData();
  495. calculateGatherData();
  496. gclList.sort(function (a, b) {
  497. return compareCode(a.b_code, b.b_code) || ZhCalc.sub(a.unit_price, b.unit_price);
  498. });
  499. return gclList;
  500. }
  501. function checkDiffer(gclList) {
  502. for (const gcl of gclList) {
  503. gcl.differ = false;
  504. }
  505. for (const [i, gcl] of gclList.entries()) {
  506. if (i === gclList.length - 1) continue;
  507. const next = gclList[i+1];
  508. if (gcl.b_code === next.b_code) {
  509. if (gcl.name !== next.name || gcl.unit !== next.unit || !checkZero(gcl.unit_price - next.unit_price)) {
  510. gcl.differ = true;
  511. next.differ = true;
  512. }
  513. }
  514. }
  515. }
  516. function _getCalcChapter(chapter, option) {
  517. const gclChapter = [], otherChapter = {}, gclChapterFilter = [];
  518. let serialNo = 1;
  519. for (const c of chapter) {
  520. const cc = { code: c.code, name: c.name, cType: 1 };
  521. cc.serialNo = serialNo++;
  522. cc.filter = '^[^0-9]*([0-9]{0,2}-)?' + c.code.substr(0, c.code.length - 2) + '[0-9]{2}(-|$)';
  523. gclChapter.push(cc);
  524. }
  525. gclChapter.push({ name: '未计入章节清单合计', cType: 21, serialNo: serialNo+1 });
  526. otherChapter.hj = { name: '合计(C=A+B+Z)', cType: 41, serialNo: serialNo+5, deal_bills_tp: option.zlj.deal_bills_tp };
  527. gclChapterFilter.push({node_type: option.jrg.value});
  528. gclChapterFilter.push({field: 'name', part: option.jrg.text});
  529. const zlChapter = {
  530. name: '暂列金额(Z)', cType: 32, serialNo: serialNo+4,
  531. deal_bills_tp: option.zlj.deal_bills_tp, match: [], matchPath: []
  532. };
  533. zlChapter.match.push({node_type: option.zlj.value});
  534. zlChapter.match.push({field: 'name', part: option.zlj.text});
  535. otherChapter.zlj = zlChapter;
  536. otherChapter.qd = { name: '清单小计(A)', cType: 11, serialNo: serialNo+2 };
  537. otherChapter.fqd = { name: '非清单项费用(B)', cType: 31, serialNo: serialNo+3 };
  538. return [gclChapter, otherChapter, gclChapterFilter];
  539. }
  540. function _gatherChapterFields(chapter, data, fields) {
  541. for (const f of fields) {
  542. chapter[f] = ZhCalc.add(chapter[f], data[f]);
  543. }
  544. }
  545. function _getGclChapter(chapter, data) {
  546. for (const c of chapter) {
  547. if (c.filter) {
  548. const reg = new RegExp(c.filter);
  549. if (reg.test(data.b_code)) {
  550. return c;
  551. }
  552. } else {
  553. return c;
  554. }
  555. }
  556. }
  557. function _checkFilter(d, filter) {
  558. for (const f of filter) {
  559. if (f.node_type && f.node_type === d.node_type) return true;
  560. if (f.field) {
  561. if (f.part && d[f.field] && d[f.field].indexOf(f.part) >= 0) return true;
  562. if (f.all && d[f.all] && d[f.all] === f.all) return true;
  563. }
  564. }
  565. return false;
  566. }
  567. function gatherChapterData(chapter, option, fields) {
  568. const chapterFilterPath = [];
  569. const checkFilterPath = function (data, filterPath) {
  570. for (const fp of filterPath) {
  571. if (data.full_path.indexOf(fp + '-') === 0 || data.full_path === fp) return true;
  572. }
  573. return false;
  574. };
  575. const [gclChapter, otherChapter, gclChapterFilter] = _getCalcChapter(chapter, option);
  576. for (const d of gsTree.nodes) {
  577. if (_checkFilter(d, gclChapterFilter)) chapterFilterPath.push(d.full_path);
  578. if (_checkFilter(d, otherChapter.zlj.match)) otherChapter.zlj.matchPath.push(d.full_path);
  579. if (d.children && d.children.length > 0) continue;
  580. if (checkFilterPath(d,otherChapter.zlj.matchPath)) {
  581. gatherfields(otherChapter.zlj, d, fields);
  582. gatherfields(otherChapter.hj, d, fields);
  583. } else {
  584. gatherfields(otherChapter.hj, d, fields);
  585. if (d.b_code) {
  586. gatherfields(otherChapter.qd, d, fields);
  587. }
  588. if (!d.b_code || d.b_code === '') {
  589. gatherfields(otherChapter.fqd, d, fields);
  590. }
  591. if (d.b_code) {
  592. const c = checkFilterPath(d, chapterFilterPath)
  593. ? gclChapter.find(x => { return x.cType === 21})
  594. : _getGclChapter(gclChapter, d);
  595. gatherfields(c, d, fields);
  596. }
  597. }
  598. }
  599. for (const d of deal) {
  600. if (!d.quantity || !d.unit_price) continue;
  601. otherChapter.hj.deal_bills_tp = ZhCalc.add(otherChapter.hj.deal_bills_tp, d.total_price);
  602. otherChapter.qd.deal_bills_tp = ZhCalc.add(otherChapter.qd.deal_bills_tp, d.total_price);
  603. const c = _getGclChapter(gclChapter, d);
  604. c.deal_bills_tp = ZhCalc.add(c.deal_bills_tp, d.total_price);
  605. }
  606. const result = gclChapter.concat([otherChapter.hj, otherChapter.zlj, otherChapter.qd, otherChapter.fqd]);
  607. result.sort((x, y) => {return x.serialNo - y.serialNo});
  608. return result;
  609. }
  610. function _gatherLeafXmj(gcl, fields = ['bwmx']) {
  611. gcl.gatherLeafXmjs = [];
  612. for (const lx of gcl.leafXmjs) {
  613. const condition = {};
  614. for (const f of fields) {
  615. condition[f] = lx[f];
  616. }
  617. let glx = _.find(gcl.gatherLeafXmjs, condition);
  618. if (!glx) {
  619. glx = JSON.parse(JSON.stringify(lx));
  620. gcl.gatherLeafXmjs.push(glx);
  621. } else {
  622. gatherfields(glx, lx, posGatherFields);
  623. }
  624. }
  625. for (const xmj of gcl.gatherLeafXmjs) {
  626. xmj.pre_gather_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.pre_qc_qty);
  627. xmj.gather_qty = ZhCalc.add(xmj.contract_qty, xmj.qc_qty);
  628. xmj.end_contract_qty = ZhCalc.add(xmj.pre_contract_qty, xmj.contract_qty);
  629. xmj.end_qc_qty = ZhCalc.add(xmj.pre_qc_qty, xmj.qc_qty);
  630. xmj.end_gather_qty = ZhCalc.add(xmj.pre_gather_qty, xmj.gather_qty);
  631. xmj.end_final_qty = ZhCalc.add(xmj.end_qc_qty, xmj.quantity);
  632. xmj.end_qc_minus_qty = ZhCalc.add(xmj.pre_qc_minus_qty, xmj.qc_minus_qty);
  633. xmj.final_1_qty = ZhCalc.add(xmj.quantity, xmj.end_qc_minus_qty);
  634. xmj.end_final_1_qty = ZhCalc.add(xmj.final_1_qty, xmj.end_qc_qty);
  635. xmj.end_gather_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_qty), 100, 2);
  636. xmj.end_final_1_percent = ZhCalc.mul(ZhCalc.div(xmj.end_gather_qty, xmj.end_final_1_qty), 100, 2);
  637. }
  638. }
  639. function reGatherLeafXmj(fields) {
  640. gclList.forEach(g => {
  641. _gatherLeafXmj(g, fields);
  642. });
  643. }
  644. function _gatherAncillaryGcl(gcl, fields = ['jldy']) {
  645. fields.push('is_aux', 'name', 'unit');
  646. gcl.gatherAncGcl = [];
  647. for (const lx of gcl.ancGcl) {
  648. const condition = {};
  649. for (const f of fields) {
  650. condition[f] = lx[f];
  651. }
  652. let glx = _.find(gcl.gatherAncGcl, condition);
  653. if (!glx) {
  654. glx = JSON.parse(JSON.stringify(lx));
  655. gcl.gatherAncGcl.push(glx);
  656. } else {
  657. gatherfields(glx, lx, ancillaryGclGatherFields);
  658. }
  659. }
  660. gcl.gatherAncGcl.sort((a, b) => {
  661. if (a.is_aux === b.is_aux) {
  662. return a.name < b.name ? -1 : 1;
  663. } else {
  664. return a.is_aux - b.is_aux;
  665. }
  666. });
  667. }
  668. function reGatherAncillaryGcl(fields) {
  669. gclList.forEach(g => {
  670. _gatherAncillaryGcl(g, fields);
  671. });
  672. }
  673. return {
  674. loadGatherField,
  675. loadDecimal,
  676. loadLedgerData,
  677. loadPosData,
  678. loadAncillaryGclData,
  679. loadDealBillsData,
  680. loadChangeData,
  681. loadStageChangeData,
  682. gatherGclData,
  683. checkDiffer,
  684. gatherChapterData,
  685. reGatherLeafXmj,
  686. reGatherAncillaryGcl,
  687. };
  688. })();