gcl_gather.js 25 KB

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