ledger.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const itemsPre = 'id_';
  10. class baseTree {
  11. /**
  12. * 构造函数
  13. */
  14. constructor (ctx, setting) {
  15. this.ctx = ctx;
  16. // 无索引
  17. this.datas = [];
  18. // 以key为索引
  19. this.items = {};
  20. // 以排序为索引
  21. this.nodes = [];
  22. // 根节点
  23. this.children = [];
  24. // 树设置
  25. this.setting = setting;
  26. if (!this.setting.isLeaf) this.setting.isLeaf = 'is_leaf';
  27. if (!this.setting.fullPath) this.setting.fullPath = 'full_path';
  28. }
  29. clear() {
  30. // 无索引
  31. this.datas = [];
  32. // 以key为索引
  33. this.items = {};
  34. // 以排序为索引
  35. this.nodes = [];
  36. // 根节点
  37. this.children = [];
  38. }
  39. /**
  40. * 根据id获取树结构节点数据
  41. * @param {Number} id
  42. * @returns {Object}
  43. */
  44. getItems (id) {
  45. return this.items[itemsPre + id];
  46. };
  47. /**
  48. * 查找node的parent
  49. * @param {Object} node
  50. * @returns {Object}
  51. */
  52. getParent (node) {
  53. return this.getItems(node[this.setting.pid]);
  54. };
  55. getTopParent(node) {
  56. const parents = this.getAllParents(node);
  57. return parents[0];
  58. };
  59. getAllParents(node) {
  60. const parents = [];
  61. if (!node) return parents;
  62. if (node[this.setting.fullPath] && node[this.setting.fullPath] !== '') {
  63. const parentIds = node[this.setting.fullPath].split('-');
  64. for (const id of parentIds) {
  65. if (id !== node[this.setting.id]) {
  66. parents.push(this.getItems(id));
  67. }
  68. }
  69. } else {
  70. let vP = this.getParent(node);
  71. while (vP) {
  72. parents.unshift(vP);
  73. vP = this.getParent(vP);
  74. }
  75. }
  76. return parents;
  77. }
  78. /**
  79. * 查询node的已下载子节点
  80. * @param {Object} node
  81. * @returns {Array}
  82. */
  83. getChildren (node) {
  84. const setting = this.setting;
  85. const pid = node ? node[setting.id] : setting.rootId;
  86. const children = this.datas.filter(function (x) {
  87. return x[setting.pid] === pid;
  88. });
  89. children.sort(function (a, b) {
  90. return a[setting.order] - b[setting.order];
  91. });
  92. return children;
  93. };
  94. /**
  95. * 获取节点的 index
  96. * @param node
  97. * @returns {number}
  98. */
  99. getNodeSerialNo(node) {
  100. return this.nodes.indexOf(node);
  101. }
  102. /**
  103. * 树结构根据显示排序
  104. */
  105. sortTreeNode (isResort) {
  106. const self = this;
  107. const setting = this.setting;
  108. const addSortNodes = function (nodes) {
  109. if (!nodes) { return }
  110. for (let i = 0; i < nodes.length; i++) {
  111. self.nodes.push(nodes[i]);
  112. nodes[i].index = self.nodes.length - 1;
  113. if (!isResort) {
  114. nodes[i].children = self.getChildren(nodes[i]);
  115. } else {
  116. nodes[i].children.sort(function (a, b) {
  117. return a[setting.order] - b[setting.order];
  118. })
  119. }
  120. addSortNodes(nodes[i].children);
  121. }
  122. };
  123. this.nodes = [];
  124. if (!isResort) {
  125. this.children = this.getChildren();
  126. } else {
  127. this.children.sort(function (a, b) {
  128. return a[setting.order] - b[setting.order];
  129. })
  130. }
  131. addSortNodes(this.children);
  132. }
  133. /**
  134. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  135. * @param datas
  136. */
  137. loadDatas (datas) {
  138. // 清空旧数据
  139. this.items = {};
  140. this.nodes = [];
  141. this.datas = [];
  142. this.children = [];
  143. const setting = this.setting;
  144. // 加载全部数据
  145. datas.sort(function (a, b) {
  146. return a[setting.level] - b[setting.level];
  147. });
  148. for (const data of datas) {
  149. const keyName = itemsPre + data[this.setting.id];
  150. if (!this.items[keyName]) {
  151. const item = JSON.parse(JSON.stringify(data));
  152. item.children = [];
  153. item.expanded = true;
  154. item.visible = true;
  155. this.items[keyName] = item;
  156. this.datas.push(item);
  157. if (item[this.setting.pid] === this.setting.rootId) {
  158. this.children.push(item);
  159. } else {
  160. const parent = this.getParent(item);
  161. if (parent) {
  162. parent.children.push(item);
  163. }
  164. }
  165. }
  166. }
  167. this.children.sort(function (a, b) {
  168. return a[setting.order] - b[setting.order];
  169. });
  170. this.sortTreeNode(true);
  171. }
  172. /**
  173. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  174. * @param node
  175. * @returns {*}
  176. * @private
  177. */
  178. _recursiveGetPosterity (node) {
  179. let posterity = node.children;
  180. for (const c of node.children) {
  181. posterity = posterity.concat(this._recursiveGetPosterity(c));
  182. }
  183. return posterity;
  184. };
  185. /**
  186. * 查询node的已下载的全部后代
  187. * @param {Object} node
  188. * @returns {Array}
  189. */
  190. getPosterity (node) {
  191. const self = this;
  192. let posterity;
  193. if (node.full_path !== '') {
  194. const reg = new RegExp('^' + node.full_path + '-');
  195. posterity = this.datas.filter(function (x) {
  196. return reg.test(x.full_path);
  197. });
  198. } else {
  199. posterity = this._recursiveGetPosterity(node);
  200. }
  201. posterity.sort(function (x, y) {
  202. return self.getNodeSerialNo(x) - self.getNodeSerialNo(y);
  203. });
  204. return posterity;
  205. };
  206. /**
  207. * 根据 字段名称 获取数据
  208. * @param fields
  209. * @returns {Array}
  210. */
  211. getDatas (fields) {
  212. const datas = [];
  213. for (const node of this.nodes) {
  214. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  215. node.is_leaf = !node.children || node.children.length === 0;
  216. const data = {};
  217. for (const field of fields) {
  218. data[field] = node[field];
  219. }
  220. datas.push(data);
  221. }
  222. return datas;
  223. }
  224. /**
  225. * 排除 某些字段 获取数据
  226. * @param fields
  227. * @returns {Array}
  228. */
  229. getDatasWithout (fields, filter) {
  230. const datas = [];
  231. for (const node of this.nodes) {
  232. if (filter && filter(node)) {
  233. continue;
  234. }
  235. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  236. node.is_leaf = !node.children || node.children.length === 0;
  237. const data = {};
  238. for (const field in node) {
  239. if (fields.indexOf(field) === -1) {
  240. data[field] = node[field];
  241. }
  242. }
  243. datas.push(data);
  244. }
  245. return datas;
  246. }
  247. /**
  248. * 获取默认数据 剔除一些树结构需要的缓存数据
  249. * @returns {Array}
  250. */
  251. getDefaultDatas(filter) {
  252. return this.getDatasWithout(['expanded', 'visible', 'children', 'index'], filter);
  253. }
  254. /**
  255. * 获取默认数据 剔除一些树结构需要的缓存数据
  256. * @returns {Array}
  257. */
  258. getDefaultDatasByLevel(level) {
  259. const levelField = this.setting.level;
  260. return this.getDatasWithout(['expanded', 'visible', 'children', 'index'], function(node) {
  261. switch(level) {
  262. case "2":
  263. case "3":
  264. case "4":
  265. case "5":
  266. return node[levelField] > parseInt(level);
  267. case "last":
  268. return false;
  269. }
  270. });
  271. }
  272. _mapTreeNode () {
  273. let map = {}, maxLevel = 0;
  274. const levelField = this.setting.level;
  275. for (const node of this.nodes) {
  276. let levelArr = map[node[levelField]];
  277. if (!levelArr) {
  278. levelArr = [];
  279. map[node[levelField]] = levelArr;
  280. }
  281. if (node[levelField] > maxLevel) {
  282. maxLevel = node[levelField];
  283. }
  284. levelArr.push(node);
  285. }
  286. return [maxLevel, map];
  287. }
  288. _calculateNode (node, fun) {
  289. const self = this;
  290. if (node.children && node.children.length > 0) {
  291. const gather = node.children.reduce(function (rst, x) {
  292. const result = {};
  293. for (const cf of self.setting.calcFields) {
  294. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  295. }
  296. return result;
  297. });
  298. // 汇总子项
  299. for (const cf of this.setting.calcFields) {
  300. if (gather[cf]) {
  301. node[cf] = gather[cf];
  302. } else {
  303. node[cf] = null;
  304. }
  305. }
  306. }
  307. // 自身运算
  308. if (fun) {
  309. fun(node);
  310. } else if (this.setting.calc) {
  311. this.setting.calc(node, this.ctx.helper, this.ctx.tender.info.decimal);
  312. }
  313. }
  314. calculateAll(fun) {
  315. const [maxLevel, levelMap] = this._mapTreeNode();
  316. for (let i = maxLevel; i >= 0; i--) {
  317. const levelNodes = levelMap[i];
  318. if (levelNodes && levelNodes.length > 0) {
  319. for (const node of levelNodes) {
  320. this._calculateNode(node, fun);
  321. }
  322. }
  323. }
  324. }
  325. initNodeData(field, defaultValue, calcFun) {
  326. if (!field || !calcFun) return;
  327. const initNode = function (node) {
  328. if (node[field] === undefined) node[field] = defaultValue;
  329. if (node.children && node.children.length > 0) {
  330. const values = [];
  331. for (const child of node.children) {
  332. initNode(child);
  333. if (values.indexOf(child[field]) < 0) values.push(child[field]);
  334. }
  335. node[field] = calcFun(values, defaultValue);
  336. }
  337. };
  338. for (const node of this.children) {
  339. initNode(node);
  340. }
  341. }
  342. }
  343. class billsTree extends baseTree {
  344. /**
  345. * 检查节点是否是最底层项目节
  346. * @param node
  347. * @returns {boolean}
  348. */
  349. isLeafXmj(node) {
  350. if (node.b_code && node.b_code !== '') {
  351. return false;
  352. }
  353. for (const child of node.children) {
  354. if (!child.b_code || child.b_code === '') {
  355. return false;
  356. }
  357. }
  358. return true;
  359. }
  360. /**
  361. * 查询最底层项目节(本身或父项)
  362. * @param {Object} node - 查询节点
  363. * @returns {Object}
  364. */
  365. getLeafXmjParent(node) {
  366. let parent = node;
  367. while (parent) {
  368. if (this.isLeafXmj(parent)) {
  369. return parent;
  370. } else {
  371. parent = this.getParent(parent);
  372. }
  373. }
  374. return null;
  375. }
  376. }
  377. class filterTree extends baseTree {
  378. addData(data, fields) {
  379. const item = {};
  380. for (const prop in data) {
  381. if (fields.indexOf(prop) >= 0) {
  382. item[prop] = data[prop];
  383. }
  384. }
  385. const keyName = itemsPre + item[this.setting.id];
  386. if (!this.items[keyName]) {
  387. item.children = [];
  388. item.is_leaf = true;
  389. item.expanded = true;
  390. item.visible = true;
  391. this.items[keyName] = item;
  392. this.datas.push(item);
  393. if (item[this.setting.pid] === this.setting.rootId) {
  394. this.children.push(item);
  395. } else {
  396. const parent = this.getParent(item);
  397. if (parent) {
  398. parent.is_leaf = false;
  399. parent.children.push(item);
  400. }
  401. }
  402. } else {
  403. return this.items[keyName];
  404. }
  405. return item;
  406. }
  407. }
  408. class filterGatherTree extends baseTree {
  409. clearDatas() {
  410. this.items = {};
  411. this.nodes = [];
  412. this.datas = [];
  413. this.children = [];
  414. }
  415. get newId() {
  416. if (!this._maxId) {
  417. this._maxId = 0;
  418. }
  419. this._maxId++;
  420. return this._maxId;
  421. }
  422. addNode(data, parent) {
  423. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  424. let item = this.ctx.helper._.find(this.items, data);
  425. if (item) return item;
  426. item = data;
  427. item.drawing_code = [];
  428. item.memo = [];
  429. item.ex_memo1 = [];
  430. item.ex_memo2 = [];
  431. item.ex_memo3 = [];
  432. item.postil = [];
  433. item[this.setting.id] = this.newId;
  434. const keyName = itemsPre + item[this.setting.id];
  435. item.children = [];
  436. item.is_leaf = true;
  437. item.expanded = true;
  438. item.visible = true;
  439. this.items[keyName] = item;
  440. this.datas.push(item);
  441. if (parent) {
  442. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  443. item[this.setting.level] = parent[this.setting.level] + 1;
  444. item[this.setting.order] = parent.children.length + 1;
  445. parent.is_leaf = false;
  446. parent.children.push(item);
  447. } else {
  448. item[this.setting.fullPath] = '' + item[this.setting.id];
  449. item[this.setting.level] = 1;
  450. item[this.setting.order] = this.children.length + 1;
  451. this.children.push(item);
  452. }
  453. return item;
  454. }
  455. generateSortNodes() {
  456. const self = this;
  457. const addSortNode = function (node) {
  458. self.nodes.push(node);
  459. for (const c of node.children) {
  460. addSortNode(c);
  461. }
  462. };
  463. this.nodes = [];
  464. for (const n of this.children) {
  465. addSortNode(n);
  466. }
  467. }
  468. sortTreeNodeCustom(fun) {
  469. const sortNodes = function (nodes) {
  470. nodes.sort(fun);
  471. for (const [i, node] of nodes.entries()) {
  472. node.order = i + 1;
  473. }
  474. for (const node of nodes) {
  475. if (node.children && node.children.length > 1) {
  476. sortNodes(node.children);
  477. }
  478. }
  479. };
  480. this.nodes = [];
  481. this.children = this.getChildren(null);
  482. sortNodes(this.children);
  483. this.generateSortNodes();
  484. }
  485. }
  486. class gatherTree extends baseTree {
  487. constructor(ctx, setting) {
  488. super(ctx, setting);
  489. this._newId = 1;
  490. }
  491. get newId() {
  492. return this._newId++;
  493. }
  494. loadGatherNode(node, parent, loadFun, loadPosFun) {
  495. const siblings = parent ? parent.children : this.children;
  496. let cur = siblings.find(function (x) {
  497. return node.b_code
  498. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  499. : x.code === node.code && x.name === node.name;
  500. });
  501. if (!cur) {
  502. const id = this.newId;
  503. cur = {
  504. id: id,
  505. pid: parent ? parent.id : this.setting.rootId,
  506. full_path: parent ? parent.full_path + '-' + id : '' + id,
  507. level: parent ? parent.level + 1 : 1,
  508. order: siblings.length + 1,
  509. children: [],
  510. code: node.code, b_code: node.b_code, name: node.name,
  511. unit: node.unit, unit_price: node.unit_price, drawing_code: node.drawing_code,
  512. };
  513. siblings.push(cur);
  514. this.datas.push(cur);
  515. }
  516. if (!cur.memo && !!node.memo) cur.memo = node.memo;
  517. if (!cur.ex_memo1 && !!node.ex_memo1) cur.ex_memo1 = node.ex_memo1;
  518. if (!cur.ex_memo2 && !!node.ex_memo2) cur.ex_memo2 = node.ex_memo2;
  519. if (!cur.ex_memo3 && !!node.ex_memo3) cur.ex_memo3 = node.ex_memo3;
  520. loadFun(cur, node);
  521. if (node.children && node.children.length > 0) {
  522. for (const c of node.children) {
  523. this.loadGatherNode(c, cur, loadFun, loadPosFun);
  524. }
  525. } else if (loadPosFun) {
  526. loadPosFun(cur, node);
  527. }
  528. }
  529. generateSortNodes() {
  530. const self = this;
  531. const addSortNode = function (node) {
  532. self.nodes.push(node);
  533. for (const c of node.children) {
  534. addSortNode(c);
  535. }
  536. };
  537. this.nodes = [];
  538. for (const n of this.children) {
  539. addSortNode(n);
  540. }
  541. }
  542. loadGatherTree(sourceTree, loadFun, loadPosFun) {
  543. for (const c of sourceTree.children) {
  544. this.loadGatherNode(c, null, loadFun, loadPosFun);
  545. }
  546. }
  547. resortChildrenByCustom(fun) {
  548. for (const n of this.datas) {
  549. if (n.children && n.children.length > 1) {
  550. n.children.sort(fun);
  551. n.children.forEach((x, i) => { x.order = i + 1; });
  552. }
  553. }
  554. this.generateSortNodes();
  555. }
  556. resortChildrenDefault() {
  557. const helper = this.ctx.helper;
  558. this.resortChildrenByCustom((x, y) => {
  559. const iCode = (x.code || y.code) ? helper.compareCode(x.code, y.code) : helper.compareCode(x.b_code, y.b_code);
  560. if (iCode) return iCode;
  561. if (!x.name) return -1;
  562. if (!y.name) return 1;
  563. return x.name.localeCompare(y.name);
  564. })
  565. }
  566. calculateSum() {
  567. if (this.setting.calcSum) {
  568. for (const d of this.datas) {
  569. this.setting.calcSum(d, this.count);
  570. }
  571. }
  572. }
  573. }
  574. class pos {
  575. /**
  576. * 构造函数
  577. * @param {id|Number, masterId|Number} setting
  578. */
  579. constructor (setting) {
  580. // 无索引
  581. this.datas = [];
  582. // 以key为索引
  583. this.items = {};
  584. // 以分类id为索引的有序
  585. this.ledgerPos = {};
  586. // pos设置
  587. this.setting = setting;
  588. }
  589. /**
  590. * 加载部位明细数据
  591. * @param datas
  592. */
  593. loadDatas(datas) {
  594. this.datas = datas;
  595. this.items = {};
  596. this.ledgerPos = {};
  597. for (const data of this.datas) {
  598. const key = itemsPre + data[this.setting.id];
  599. this.items[key] = data;
  600. const masterKey = itemsPre + data[this.setting.ledgerId];
  601. if (!this.ledgerPos[masterKey]) {
  602. this.ledgerPos[masterKey] = [];
  603. }
  604. this.ledgerPos[masterKey].push(data);
  605. }
  606. for (const prop in this.ledgerPos) {
  607. this.resortLedgerPos(this.ledgerPos[prop]);
  608. }
  609. }
  610. getPos(id) {
  611. return this.items[itemsPre + id];
  612. }
  613. getLedgerPosKey() {
  614. const result = [];
  615. for (const prop in this.ledgerPos) {
  616. result.push(prop);
  617. }
  618. return result;
  619. }
  620. getLedgerPos(mid) {
  621. return this.ledgerPos[itemsPre + mid];
  622. }
  623. resortLedgerPos(ledgerPos) {
  624. if (ledgerPos instanceof Array) {
  625. ledgerPos.sort(function (a, b) {
  626. return a.porder - b.porder;
  627. })
  628. }
  629. }
  630. /**
  631. * 计算全部
  632. */
  633. calculateAll(fun) {
  634. const calcFun = fun ? fun : this.setting.calc;
  635. if (!calcFun) return;
  636. for (const pos of this.datas) {
  637. calcFun(pos);
  638. }
  639. }
  640. getDatas () {
  641. return this.datas;
  642. }
  643. }
  644. class gatherPos extends pos {
  645. loadGatherPos(ledgerId, sourcePosRange, loadFun) {
  646. let posRange = this.getLedgerPos(itemsPre + ledgerId);
  647. if (!posRange) {
  648. posRange = [];
  649. this.ledgerPos[itemsPre + ledgerId] = posRange;
  650. }
  651. for (const spr of sourcePosRange) {
  652. let gp = posRange.find(x => { return x.name === spr.name; });
  653. if (!gp) {
  654. gp = { name: spr.name };
  655. gp[this.setting.ledgerId] = ledgerId;
  656. this.datas.push(gp);
  657. posRange.push(gp);
  658. }
  659. if (!gp.ex_memo1 && !!spr.ex_memo1) gp.ex_memo1 = spr.ex_memo1;
  660. if (!gp.ex_memo2 && !!spr.ex_memo2) gp.ex_memo2 = spr.ex_memo2;
  661. if (!gp.ex_memo3 && !!spr.ex_memo3) gp.ex_memo3 = spr.ex_memo3;
  662. loadFun(gp, spr);
  663. }
  664. }
  665. }
  666. class checkData {
  667. constructor(ctx, measureType) {
  668. this.ctx = ctx;
  669. this.checkBills = new billsTree(ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  670. this.checkPos = new pos({ id: 'id', ledgerId: 'lid' });
  671. this.checkResult = {
  672. error: [],
  673. source: {
  674. bills: [],
  675. pos: [],
  676. },
  677. };
  678. this.measureType = measureType;
  679. }
  680. _check3f(data, limit, ratio) {
  681. if (limit === 0) {
  682. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  683. }
  684. if (limit === 1) {
  685. if (ratio === 0) {
  686. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  687. } else {
  688. const tp = this.ctx.helper.mul(data.final_1_tp, this.ctx.helper.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  689. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp);
  690. if (tp > checkTp) return 1; // 违规
  691. if (tp < checkTp) return 2; // 漏计
  692. }
  693. }
  694. return 0; // 合法
  695. }
  696. _check3fQty(data, limit, ratio, unit) {
  697. if (limit === 0) {
  698. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  699. }
  700. if (limit === 1) {
  701. if (!ratio || ratio === 0) {
  702. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  703. } else {
  704. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  705. const checkQty = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(ratio, 100, 4), precision.value);
  706. const qty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty);
  707. if (qty > checkQty) return 1; // 违规
  708. if (qty < checkQty) return 2; // 漏计
  709. }
  710. }
  711. return 0; // 合法
  712. }
  713. _getRatio(type, status) {
  714. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  715. const sc = statusConst.find(x => { return x.value === status });
  716. return sc ? sc.ratio : null;
  717. }
  718. _getValid = function (type, status, limit) {
  719. if (limit) {
  720. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  721. const sc = statusConst.find(x => { return x.value === status; });
  722. return sc ? (sc.limit ? 1 : 0) : 0;
  723. } else {
  724. return -1;
  725. }
  726. };
  727. _checkLeafBills3fLimit(checkType, bills, checkInfo) {
  728. const over = [], lost = [];
  729. const posRange = this.checkPos.getLedgerPos(bills.id);
  730. if (posRange && posRange.length > 0) {
  731. for (const p of posRange) {
  732. const posCheckInfo = this.ctx.helper._.assign({}, checkInfo);
  733. for (const ct of checkType) {
  734. if (p[ct + '_limit'] > 0) {
  735. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  736. }
  737. }
  738. for (const ct of checkType) {
  739. const checkResult = this._check3fQty(p, this._getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']), this._getRatio(ct, p[ct+'_status']), bills.unit);
  740. if (checkResult === 1) {
  741. if (over.indexOf(ct) === -1) over.push(ct);
  742. }
  743. if (checkResult === 2) {
  744. if (lost.indexOf(ct) === -1) lost.push(ct);
  745. }
  746. }
  747. }
  748. } else {
  749. for (const ct of checkType) {
  750. const checkResult = bills.is_tp
  751. ? this._check3f(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']))
  752. : this._check3fQty(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']), bills.unit);
  753. if (checkResult === 1) {
  754. if (over.indexOf(ct) === -1) over.push(ct);
  755. }
  756. if (checkResult === 2) {
  757. if (lost.indexOf(ct) === -1) lost.push(ct);
  758. }
  759. }
  760. }
  761. if (over.length + lost.length > 0) {
  762. for (const o of over) {
  763. this.checkResult.error.push({
  764. ledger_id: bills.ledger_id,
  765. b_code: bills.b_code,
  766. name: bills.name,
  767. errorType: 's2b_over_' + o,
  768. });
  769. }
  770. for (const l of lost) {
  771. this.checkResult.error.push({
  772. ledger_id: bills.ledger_id,
  773. b_code: bills.b_code,
  774. name: bills.name,
  775. errorType: 's2b_lost_' + l,
  776. });
  777. }
  778. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === bills.ledger_id})) {
  779. this.checkResult.source.bills.push(bills);
  780. if (posRange && posRange.length > 0) this.checkResult.source.pos.push(...posRange);
  781. }
  782. }
  783. }
  784. _recursiveCheckBills3fLimit(checkType, bills, parentCheckInfo) {
  785. const checkInfo = this.ctx.helper._.assign({}, parentCheckInfo);
  786. for (const ct of checkType) {
  787. if (bills[ct + '_limit'] > 0) {
  788. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  789. }
  790. }
  791. if (bills.children && bills.children.length > 0) {
  792. for (const c of bills.children) {
  793. this._recursiveCheckBills3fLimit(checkType, c, checkInfo);
  794. }
  795. } else {
  796. this._checkLeafBills3fLimit(checkType, bills, checkInfo);
  797. }
  798. }
  799. loadData(bills, pos) {
  800. this.checkBills.loadDatas(bills);
  801. this.checkPos.loadDatas(pos);
  802. }
  803. checkSibling() {
  804. for (const node of this.checkBills.nodes) {
  805. if (!node.children || node.children.length === 0) continue;
  806. let hasXmj, hasGcl;
  807. for (const child of node.children) {
  808. if (child.b_code) hasXmj = true;
  809. if (!child.b_code) hasGcl = true;
  810. }
  811. if (hasXmj && hasGcl) this.checkResult.error.push({
  812. ledger_id: node.ledger_id,
  813. b_code: node.b_code,
  814. name: node.name,
  815. errorType: 'sibling',
  816. });
  817. }
  818. }
  819. checkSameCode() {
  820. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  821. let xmj = [];
  822. const addXmjCheck = function (node) {
  823. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  824. for (const child of node.children) {
  825. addXmjCheck(child);
  826. }
  827. };
  828. for (const topLevel of this.checkBills.children) {
  829. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  830. addXmjCheck(topLevel);
  831. }
  832. const xmjPart = {}, xmjIndex = [];
  833. for (const x of xmj) {
  834. if (!xmjPart[x.code]) {
  835. xmjPart[x.code] = [];
  836. xmjIndex.push(x.code);
  837. }
  838. xmjPart[x.code].push(x);
  839. }
  840. for (const x of xmjIndex) {
  841. if (xmjPart[x].length <= 1) continue;
  842. for (const xp of xmjPart[x]) {
  843. this.checkResult.error.push({
  844. ledger_id: xp.ledger_id,
  845. b_code: xp.b_code,
  846. name: xp.name,
  847. errorType: 'same_code',
  848. })
  849. }
  850. }
  851. let check = null;
  852. while (xmj.length > 0) {
  853. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  854. if (check.length > 1) {
  855. for (const c of check) {
  856. this.checkResult.error.push({
  857. ledger_id: c.ledger_id,
  858. b_code: c.b_code,
  859. name: c.name,
  860. errorType: 'same_code',
  861. })
  862. }
  863. }
  864. }
  865. }
  866. check3fLimit(tender) {
  867. const check = [];
  868. if (tender.s2b_gxby_limit) check.push('gxby');
  869. if (tender.s2b_dagl_limit) check.push('dagl');
  870. if (check.length === 0) return;
  871. for (const b of this.checkBills.children) {
  872. this._recursiveCheckBills3fLimit(check, b, {});
  873. }
  874. }
  875. checkBillsQty(fields) {
  876. for (const b of this.checkBills.nodes) {
  877. if (b.children && b.children.length > 0) continue;
  878. const pr = this.checkPos.getLedgerPos(b.id);
  879. if (!pr || pr.length === 0) continue;
  880. const checkData = {},
  881. calcData = {};
  882. for (const field of fields) {
  883. checkData[field] = b[field] ? b[field] : 0;
  884. }
  885. for (const p of pr) {
  886. for (const field of fields) {
  887. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  888. }
  889. }
  890. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  891. this.checkResult.error.push({
  892. ledger_id: b.ledger_id,
  893. b_code: b.b_code,
  894. name: b.name,
  895. errorType: 'qty',
  896. error: { checkData, calcData },
  897. });
  898. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  899. this.checkResult.source.bills.push(b);
  900. for (const p of pr) {
  901. this.checkResult.source.pos.push(p);
  902. }
  903. }
  904. }
  905. }
  906. }
  907. checkBillsTp(field, decimal, filter) {
  908. for (const b of this.checkBills.nodes) {
  909. if ((b.children && b.children.length > 0)) continue;
  910. if (filter && filter(b)) continue;
  911. const checkData = {}, calcData = {};
  912. for (const f of field) {
  913. checkData[f.tp] = b[f.tp] || 0;
  914. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  915. }
  916. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  917. this.checkResult.error.push({
  918. ledger_id: b.ledger_id,
  919. b_code: b.b_code,
  920. name: b.name,
  921. errorType: 'tp',
  922. error: { checkData, calcData },
  923. });
  924. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  925. this.checkResult.source.bills.push(b);
  926. }
  927. }
  928. }
  929. }
  930. _checkPosOverRange(p) {
  931. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  932. if (!p.quantity) return !!end_contract_qty;
  933. return p.quantity > 0
  934. ? end_contract_qty > p.final_1_qty
  935. : (p.final_1_qty > 0 ? true : end_contract_qty < p.final_1_qty || end_contract_qty > 0);
  936. }
  937. _checkBillsOverRange(bills, posRange, isTz) {
  938. if (isTz && posRange.length > 0) {
  939. for (const p of posRange) {
  940. if (this._checkPosOverRange(p)) return true;
  941. }
  942. }
  943. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  944. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  945. if (bills.is_tp) {
  946. const compare_tp = isTz ? bills.total_price : bills.deal_tp;
  947. if (!compare_tp) return !!end_contract_tp;
  948. return compare_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  949. } else {
  950. const compare_qty1 = isTz ? bills.quantity : bills.deal_qty;
  951. const compare_qty2 = isTz ? bills.final_1_qty : bills.deal_final_1_qty;
  952. if (!compare_qty1) return !!end_contract_qty;
  953. return compare_qty1 > 0
  954. ? end_contract_qty > compare_qty2
  955. : (compare_qty2 > 0 ? true : end_contract_qty < compare_qty2 || end_contract_qty > 0);
  956. }
  957. }
  958. checkOverRange() {
  959. const isTz = this.ctx.tender.data.measure_type === this.measureType.tz.value;
  960. for (const b of this.checkBills.nodes) {
  961. if (b.children && b.children.length > 0) continue;
  962. const pr = this.checkPos.getLedgerPos(b.id) || [];
  963. if (this._checkBillsOverRange(b, pr, isTz)) {
  964. this.checkResult.error.push({
  965. ledger_id: b.ledger_id,
  966. b_code: b.b_code,
  967. name: b.name,
  968. errorType: 'over',
  969. });
  970. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  971. this.checkResult.source.bills.push(b);
  972. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  973. }
  974. }
  975. }
  976. }
  977. checkMinusChangeBills(change, changeBills, finalStageChange) {
  978. const error = this.checkResult.error;
  979. const helper = this.ctx.helper;
  980. const changeIndex = {};
  981. change.forEach(c => {
  982. changeIndex[c.cid] = c;
  983. c.bills = [];
  984. c.billsIndex = {};
  985. c.stageChange = [];
  986. });
  987. changeBills.forEach(cb => {
  988. const c = changeIndex[cb.cid];
  989. if (c) c.bills.push(cb);
  990. c.billsIndex[cb.id] = cb;
  991. cb.used_qty = 0;
  992. cb.qty = parseFloat(cb.samount);
  993. });
  994. finalStageChange.forEach(sc => {
  995. if (!sc.qty) return;
  996. const c = changeIndex[sc.cid];
  997. if (c) {
  998. c.used = true;
  999. const cb = c.billsIndex[sc.cbid];
  1000. if (cb) cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1001. }
  1002. });
  1003. change.forEach(c => {
  1004. if (!c.used) return;
  1005. c.bills.forEach(b => {
  1006. if (b.qty >= 0) return;
  1007. if (!helper.numEqual(b.used_qty, b.qty)) error.push({ b_code: b.code, name: b.name, errorType: 'minus_cb', memo: c.code });
  1008. });
  1009. });
  1010. }
  1011. checkChangeBillsOver(change, changeBills, finalStageChange, curStageId) {
  1012. const error = this.checkResult.error;
  1013. const helper = this.ctx.helper;
  1014. const changeIndex = {};
  1015. change.forEach(c => {
  1016. changeIndex[c.cid] = c;
  1017. c.bills = [];
  1018. c.billsIndex = {};
  1019. c.stageChange = [];
  1020. });
  1021. changeBills.forEach(cb => {
  1022. const c = changeIndex[cb.cid];
  1023. if (c) c.bills.push(cb);
  1024. c.billsIndex[cb.id] = cb;
  1025. cb.used_qty = 0;
  1026. cb.qty = parseFloat(cb.samount);
  1027. });
  1028. finalStageChange.forEach(sc => {
  1029. if (!sc.qty) return;
  1030. const c = changeIndex[sc.cid];
  1031. if (c) {
  1032. c.used = true;
  1033. const cb = c.billsIndex[sc.cbid];
  1034. if (cb) {
  1035. cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1036. if (sc.sid === curStageId) {
  1037. cb.cur_used = true;
  1038. cb.lid = sc.lid;
  1039. }
  1040. }
  1041. }
  1042. });
  1043. change.forEach(c => {
  1044. if (!c.used) return;
  1045. c.bills.forEach(b => {
  1046. if (!b.cur_used) return;
  1047. const qtyDecimal = helper.findDecimal(b.unit);
  1048. const limitQty = helper.mul(b.qty, helper.div(b.delimit, 100, 2), qtyDecimal);
  1049. if (Math.abs(b.used_qty) > Math.abs(limitQty)) error.push({ b_code: b.code, name: b.name, errorType: 'change_over', memo: c.code, lid: b.lid, used_qty: b.used_qty, limit_qty: limitQty });
  1050. });
  1051. });
  1052. }
  1053. checkSettle() {
  1054. const settleStatus = this.ctx.service.settle.settleStatus;
  1055. for (const b of this.checkBills.nodes) {
  1056. if (b.children && b.children.length > 0) continue;
  1057. if (!b.settleStatus) continue;
  1058. const pr = this.checkPos.getLedgerPos(b.id);
  1059. if (!pr || pr.length === 0) {
  1060. if (b.settleStatus !== settleStatus.finish) continue;
  1061. if (b.contract_qty || b.contract_tp || b.qc_qty || b.qc_minus_qty || b.positive_qc_qty || b.negative_qc_qty) {
  1062. this.checkResult.error.push({
  1063. ledger_id: b.ledger_id,
  1064. b_code: b.b_code,
  1065. name: b.name,
  1066. errorType: 'settle',
  1067. });
  1068. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1069. this.checkResult.source.bills.push(b);
  1070. }
  1071. }
  1072. } else {
  1073. for (const p of pr) {
  1074. if (p.settle_status !== settleStatus.finish) continue;
  1075. if (p.contract_qty || p.qc_qty || p.qc_minus_qty || p.positive_qc_qty || p.negative_qc_qty) {
  1076. this.checkResult.error.push({
  1077. ledger_id: b.ledger_id,
  1078. b_code: b.b_code,
  1079. name: b.name,
  1080. errorType: 'settle',
  1081. });
  1082. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1083. this.checkResult.source.bills.push(b);
  1084. for (const p of pr) {
  1085. this.checkResult.source.pos.push(p);
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. class reviseTree extends billsTree {
  1095. constructor (ctx, setting) {
  1096. super(ctx, setting);
  1097. this.price = [];
  1098. }
  1099. loadRevisePrice(price, decimal) {
  1100. this.decimal = decimal;
  1101. this.price = price || [];
  1102. this.rela_price = [];
  1103. this.common_price = [];
  1104. this.price.forEach(x => {
  1105. if (x.rela_lid) {
  1106. x.rela_lid = x.rela_lid.split(',');
  1107. this.rela_price.push(x);
  1108. } else {
  1109. this.common_price.push(x);
  1110. }
  1111. });
  1112. }
  1113. checkRevisePrice(d) {
  1114. if (d.settle_status) return false;
  1115. const helper = this.ctx.helper;
  1116. const setting = this.setting;
  1117. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1118. const checkRela = function(rela_lid) {
  1119. if (!rela_lid || rela_lid.length === 0) return false;
  1120. for (const lid of rela_lid) {
  1121. if (pid.indexOf(lid) >= 0) return true;
  1122. }
  1123. return false;
  1124. };
  1125. let p = this.rela_price.find(x => {
  1126. return x.b_code === d.b_code &&
  1127. ((!x.name && !d.name) || x.name === d.name) &&
  1128. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1129. helper.checkZero(x.org_price - d.unit_price) &&
  1130. checkRela(x.rela_lid);
  1131. });
  1132. if (!p) p = this.common_price.find(x => {
  1133. return x.b_code === d.b_code &&
  1134. ((!x.name && !d.name) || x.name === d.name) &&
  1135. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1136. helper.checkZero(x.org_price - d.unit_price);
  1137. });
  1138. if (!p) return false;
  1139. d.org_price = p.org_price;
  1140. d.unit_price = p.new_price;
  1141. d.deal_tp = helper.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1142. d.sgfh_tp = helper.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1143. d.sjcl_tp = helper.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1144. d.qtcl_tp = helper.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1145. d.total_price = helper.mul(d.quantity, d.unit_price, this.decimal.tp);
  1146. return true;
  1147. }
  1148. loadDatas(datas) {
  1149. super.loadDatas(datas);
  1150. if (this.price.length > 0) {
  1151. for (const d of this.datas) {
  1152. if (d.children && d.children.length > 0) continue;
  1153. if (!d.b_code) continue;
  1154. this.checkRevisePrice(d);
  1155. }
  1156. }
  1157. }
  1158. getUpdateReviseData() {
  1159. return this.datas.map(x => {
  1160. if (x.children && x.children.length > 0) {
  1161. return {
  1162. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1163. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 0,
  1164. node_type: x.node_type, check_calc: x.check_calc,
  1165. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1166. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1167. unit_price: 0, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1168. quantity: 0, total_price: 0,
  1169. sgfh_qty: 0, sgfh_tp: 0, sgfh_expr: '',
  1170. sjcl_qty: 0, sjcl_tp: 0, sjcl_expr: '',
  1171. qtcl_qty: 0, qtcl_tp: 0, qtcl_expr: '',
  1172. deal_qty: 0, deal_tp: 0,
  1173. };
  1174. } else {
  1175. return {
  1176. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1177. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 1,
  1178. node_type: x.node_type, check_calc: x.check_calc,
  1179. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1180. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1181. unit_price: x.unit_price, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1182. quantity: x.quantity, total_price: x.total_price,
  1183. sgfh_qty: x.sgfh_qty, sgfh_tp: x.sgfh_tp, sgfh_expr: x.sgfh_expr,
  1184. sjcl_qty: x.sjcl_qty, sjcl_tp: x.sjcl_tp, sjcl_expr: x.sjcl_expr,
  1185. qtcl_qty: x.qtcl_qty, qtcl_tp: x.qtcl_tp, qtcl_expr: x.qtcl_expr,
  1186. deal_qty: x.deal_qty, deal_tp: x.deal_tp,
  1187. };
  1188. }
  1189. });
  1190. }
  1191. sum() {
  1192. const result = { total_price: 0 };
  1193. for (const d of this.datas) {
  1194. if (d.children && d.children.length > 0) continue;
  1195. result.total_price = this.ctx.helper.add(result.total_price, d.total_price);
  1196. }
  1197. return result;
  1198. }
  1199. }
  1200. module.exports = {
  1201. billsTree,
  1202. pos,
  1203. filterTree,
  1204. filterGatherTree,
  1205. gatherTree,
  1206. gatherPos,
  1207. checkData,
  1208. reviseTree,
  1209. };