gcl_gather.js 24 KB

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