ledger.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. }
  326. class billsTree extends baseTree {
  327. /**
  328. * 检查节点是否是最底层项目节
  329. * @param node
  330. * @returns {boolean}
  331. */
  332. isLeafXmj(node) {
  333. if (node.b_code && node.b_code !== '') {
  334. return false;
  335. }
  336. for (const child of node.children) {
  337. if (!child.b_code || child.b_code === '') {
  338. return false;
  339. }
  340. }
  341. return true;
  342. }
  343. /**
  344. * 查询最底层项目节(本身或父项)
  345. * @param {Object} node - 查询节点
  346. * @returns {Object}
  347. */
  348. getLeafXmjParent(node) {
  349. let parent = node;
  350. while (parent) {
  351. if (this.isLeafXmj(parent)) {
  352. return parent;
  353. } else {
  354. parent = this.getParent(parent);
  355. }
  356. }
  357. return null;
  358. }
  359. }
  360. class filterTree extends baseTree {
  361. addData(data, fields) {
  362. const item = {};
  363. for (const prop in data) {
  364. if (fields.indexOf(prop) >= 0) {
  365. item[prop] = data[prop];
  366. }
  367. }
  368. const keyName = itemsPre + item[this.setting.id];
  369. if (!this.items[keyName]) {
  370. item.children = [];
  371. item.is_leaf = true;
  372. item.expanded = true;
  373. item.visible = true;
  374. this.items[keyName] = item;
  375. this.datas.push(item);
  376. if (item[this.setting.pid] === this.setting.rootId) {
  377. this.children.push(item);
  378. } else {
  379. const parent = this.getParent(item);
  380. if (parent) {
  381. parent.is_leaf = false;
  382. parent.children.push(item);
  383. }
  384. }
  385. } else {
  386. return this.items[keyName];
  387. }
  388. return item;
  389. }
  390. }
  391. class filterGatherTree extends baseTree {
  392. clearDatas() {
  393. this.items = {};
  394. this.nodes = [];
  395. this.datas = [];
  396. this.children = [];
  397. }
  398. get newId() {
  399. if (!this._maxId) {
  400. this._maxId = 0;
  401. }
  402. this._maxId++;
  403. return this._maxId;
  404. }
  405. addNode(data, parent) {
  406. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  407. let item = this.ctx.helper._.find(this.items, data);
  408. if (item) return item;
  409. item = data;
  410. item.drawing_code = [];
  411. item.memo = [];
  412. item.ex_memo1 = [];
  413. item.ex_memo2 = [];
  414. item.ex_memo3 = [];
  415. item.postil = [];
  416. item[this.setting.id] = this.newId;
  417. const keyName = itemsPre + item[this.setting.id];
  418. item.children = [];
  419. item.is_leaf = true;
  420. item.expanded = true;
  421. item.visible = true;
  422. this.items[keyName] = item;
  423. this.datas.push(item);
  424. if (parent) {
  425. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  426. item[this.setting.level] = parent[this.setting.level] + 1;
  427. item[this.setting.order] = parent.children.length + 1;
  428. parent.is_leaf = false;
  429. parent.children.push(item);
  430. } else {
  431. item[this.setting.fullPath] = '' + item[this.setting.id];
  432. item[this.setting.level] = 1;
  433. item[this.setting.order] = this.children.length + 1;
  434. this.children.push(item);
  435. }
  436. return item;
  437. }
  438. generateSortNodes() {
  439. const self = this;
  440. const addSortNode = function (node) {
  441. self.nodes.push(node);
  442. for (const c of node.children) {
  443. addSortNode(c);
  444. }
  445. };
  446. this.nodes = [];
  447. for (const n of this.children) {
  448. addSortNode(n);
  449. }
  450. }
  451. sortTreeNodeCustom(fun) {
  452. const sortNodes = function (nodes) {
  453. nodes.sort(fun);
  454. for (const [i, node] of nodes.entries()) {
  455. node.order = i + 1;
  456. }
  457. for (const node of nodes) {
  458. if (node.children && node.children.length > 1) {
  459. sortNodes(node.children);
  460. }
  461. }
  462. };
  463. this.nodes = [];
  464. this.children = this.getChildren(null);
  465. sortNodes(this.children);
  466. this.generateSortNodes();
  467. }
  468. }
  469. class gatherTree extends baseTree {
  470. constructor(ctx, setting) {
  471. super(ctx, setting);
  472. this._newId = 1;
  473. }
  474. get newId() {
  475. return this._newId++;
  476. }
  477. loadGatherNode(node, parent, loadFun, loadPosFun) {
  478. const siblings = parent ? parent.children : this.children;
  479. let cur = siblings.find(function (x) {
  480. return node.b_code
  481. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  482. : x.code === node.code && x.name === node.name;
  483. });
  484. if (!cur) {
  485. const id = this.newId;
  486. cur = {
  487. id: id,
  488. pid: parent ? parent.id : this.setting.rootId,
  489. full_path: parent ? parent.full_path + '-' + id : '' + id,
  490. level: parent ? parent.level + 1 : 1,
  491. order: siblings.length + 1,
  492. children: [],
  493. code: node.code, b_code: node.b_code, name: node.name,
  494. unit: node.unit, unit_price: node.unit_price, drawing_code: node.drawing_code,
  495. };
  496. siblings.push(cur);
  497. this.datas.push(cur);
  498. }
  499. if (!cur.memo && !!node.memo) cur.memo = node.memo;
  500. if (!cur.ex_memo1 && !!node.ex_memo1) cur.ex_memo1 = node.ex_memo1;
  501. if (!cur.ex_memo2 && !!node.ex_memo2) cur.ex_memo2 = node.ex_memo2;
  502. if (!cur.ex_memo3 && !!node.ex_memo3) cur.ex_memo3 = node.ex_memo3;
  503. loadFun(cur, node);
  504. if (node.children && node.children.length > 0) {
  505. for (const c of node.children) {
  506. this.loadGatherNode(c, cur, loadFun, loadPosFun);
  507. }
  508. } else if (loadPosFun) {
  509. loadPosFun(cur, node);
  510. }
  511. }
  512. generateSortNodes() {
  513. const self = this;
  514. const addSortNode = function (node) {
  515. self.nodes.push(node);
  516. for (const c of node.children) {
  517. addSortNode(c);
  518. }
  519. };
  520. this.nodes = [];
  521. for (const n of this.children) {
  522. addSortNode(n);
  523. }
  524. }
  525. loadGatherTree(sourceTree, loadFun, loadPosFun) {
  526. for (const c of sourceTree.children) {
  527. this.loadGatherNode(c, null, loadFun, loadPosFun);
  528. }
  529. }
  530. resortChildrenByCustom(fun) {
  531. for (const n of this.datas) {
  532. if (n.children && n.children.length > 1) {
  533. n.children.sort(fun);
  534. n.children.forEach((x, i) => { x.order = i + 1; });
  535. }
  536. }
  537. this.generateSortNodes();
  538. }
  539. resortChildrenDefault() {
  540. const helper = this.ctx.helper;
  541. this.resortChildrenByCustom((x, y) => {
  542. const iCode = (x.code || y.code) ? helper.compareCode(x.code, y.code) : helper.compareCode(x.b_code, y.b_code);
  543. if (iCode) return iCode;
  544. if (!x.name) return -1;
  545. if (!y.name) return 1;
  546. return x.name.localeCompare(y.name);
  547. })
  548. }
  549. calculateSum() {
  550. if (this.setting.calcSum) {
  551. for (const d of this.datas) {
  552. this.setting.calcSum(d, this.count);
  553. }
  554. }
  555. }
  556. }
  557. class pos {
  558. /**
  559. * 构造函数
  560. * @param {id|Number, masterId|Number} setting
  561. */
  562. constructor (setting) {
  563. // 无索引
  564. this.datas = [];
  565. // 以key为索引
  566. this.items = {};
  567. // 以分类id为索引的有序
  568. this.ledgerPos = {};
  569. // pos设置
  570. this.setting = setting;
  571. }
  572. /**
  573. * 加载部位明细数据
  574. * @param datas
  575. */
  576. loadDatas(datas) {
  577. this.datas = datas;
  578. this.items = {};
  579. this.ledgerPos = {};
  580. for (const data of this.datas) {
  581. const key = itemsPre + data[this.setting.id];
  582. this.items[key] = data;
  583. const masterKey = itemsPre + data[this.setting.ledgerId];
  584. if (!this.ledgerPos[masterKey]) {
  585. this.ledgerPos[masterKey] = [];
  586. }
  587. this.ledgerPos[masterKey].push(data);
  588. }
  589. for (const prop in this.ledgerPos) {
  590. this.resortLedgerPos(this.ledgerPos[prop]);
  591. }
  592. }
  593. getLedgerPosKey() {
  594. const result = [];
  595. for (const prop in this.ledgerPos) {
  596. result.push(prop);
  597. }
  598. return result;
  599. }
  600. getLedgerPos(mid) {
  601. return this.ledgerPos[itemsPre + mid];
  602. }
  603. resortLedgerPos(ledgerPos) {
  604. if (ledgerPos instanceof Array) {
  605. ledgerPos.sort(function (a, b) {
  606. return a.porder - b.porder;
  607. })
  608. }
  609. }
  610. /**
  611. * 计算全部
  612. */
  613. calculateAll(fun) {
  614. const calcFun = fun ? fun : this.setting.calc;
  615. if (!calcFun) return;
  616. for (const pos of this.datas) {
  617. calcFun(pos);
  618. }
  619. }
  620. getDatas () {
  621. return this.datas;
  622. }
  623. }
  624. class gatherPos extends pos {
  625. loadGatherPos(ledgerId, sourcePosRange, loadFun) {
  626. let posRange = this.getLedgerPos(itemsPre + ledgerId);
  627. if (!posRange) {
  628. posRange = [];
  629. this.ledgerPos[itemsPre + ledgerId] = posRange;
  630. }
  631. for (const spr of sourcePosRange) {
  632. let gp = posRange.find(x => { return x.name === spr.name; });
  633. if (!gp) {
  634. gp = { name: spr.name };
  635. gp[this.setting.ledgerId] = ledgerId;
  636. this.datas.push(gp);
  637. posRange.push(gp);
  638. }
  639. if (!gp.ex_memo1 && !!spr.ex_memo1) gp.ex_memo1 = spr.ex_memo1;
  640. if (!gp.ex_memo2 && !!spr.ex_memo2) gp.ex_memo2 = spr.ex_memo2;
  641. if (!gp.ex_memo3 && !!spr.ex_memo3) gp.ex_memo3 = spr.ex_memo3;
  642. loadFun(gp, spr);
  643. }
  644. }
  645. }
  646. class checkData {
  647. constructor(ctx, measureType) {
  648. this.ctx = ctx;
  649. this.checkBills = new billsTree(ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  650. this.checkPos = new pos({ id: 'id', ledgerId: 'lid' });
  651. this.checkResult = {
  652. error: [],
  653. source: {
  654. bills: [],
  655. pos: [],
  656. },
  657. };
  658. this.measureType = measureType;
  659. }
  660. _check3f(data, limit, ratio) {
  661. if (limit === 0) {
  662. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  663. }
  664. if (limit === 1) {
  665. if (ratio === 0) {
  666. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  667. } else {
  668. const tp = this.ctx.helper.mul(data.final_1_tp, this.ctx.helper.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  669. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp);
  670. if (tp > checkTp) return 1; // 违规
  671. if (tp < checkTp) return 2; // 漏计
  672. }
  673. }
  674. return 0; // 合法
  675. }
  676. _check3fQty(data, limit, ratio, unit) {
  677. if (limit === 0) {
  678. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  679. }
  680. if (limit === 1) {
  681. if (!ratio || ratio === 0) {
  682. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  683. } else {
  684. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  685. const checkQty = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(ratio, 100, 4), precision.value);
  686. const qty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty);
  687. if (qty > checkQty) return 1; // 违规
  688. if (qty < checkQty) return 2; // 漏计
  689. }
  690. }
  691. return 0; // 合法
  692. }
  693. _getRatio(type, status) {
  694. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  695. const sc = statusConst.find(x => { return x.value === status });
  696. return sc ? sc.ratio : null;
  697. }
  698. _getValid = function (type, status, limit) {
  699. if (limit) {
  700. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  701. const sc = statusConst.find(x => { return x.value === status; });
  702. return sc ? (sc.limit ? 1 : 0) : 0;
  703. } else {
  704. return -1;
  705. }
  706. };
  707. _checkLeafBills3fLimit(checkType, bills, checkInfo) {
  708. const over = [], lost = [];
  709. const posRange = this.checkPos.getLedgerPos(bills.id);
  710. if (posRange && posRange.length > 0) {
  711. for (const p of posRange) {
  712. const posCheckInfo = this.ctx.helper._.assign({}, checkInfo);
  713. for (const ct of checkType) {
  714. if (p[ct + '_limit'] > 0) {
  715. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  716. }
  717. }
  718. for (const ct of checkType) {
  719. const checkResult = this._check3fQty(p, this._getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']), this._getRatio(ct, p[ct+'_status']), bills.unit);
  720. if (checkResult === 1) {
  721. if (over.indexOf(ct) === -1) over.push(ct);
  722. }
  723. if (checkResult === 2) {
  724. if (lost.indexOf(ct) === -1) lost.push(ct);
  725. }
  726. }
  727. }
  728. } else {
  729. for (const ct of checkType) {
  730. const checkResult = bills.is_tp
  731. ? this._check3f(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']))
  732. : this._check3fQty(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']), bills.unit);
  733. if (checkResult === 1) {
  734. if (over.indexOf(ct) === -1) over.push(ct);
  735. }
  736. if (checkResult === 2) {
  737. if (lost.indexOf(ct) === -1) lost.push(ct);
  738. }
  739. }
  740. }
  741. if (over.length + lost.length > 0) {
  742. for (const o of over) {
  743. this.checkResult.error.push({
  744. ledger_id: bills.ledger_id,
  745. b_code: bills.b_code,
  746. name: bills.name,
  747. errorType: 's2b_over_' + o,
  748. });
  749. }
  750. for (const l of lost) {
  751. this.checkResult.error.push({
  752. ledger_id: bills.ledger_id,
  753. b_code: bills.b_code,
  754. name: bills.name,
  755. errorType: 's2b_lost_' + l,
  756. });
  757. }
  758. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === bills.ledger_id})) {
  759. this.checkResult.source.bills.push(bills);
  760. if (posRange && posRange.length > 0) this.checkResult.source.pos.push(...posRange);
  761. }
  762. }
  763. }
  764. _recursiveCheckBills3fLimit(checkType, bills, parentCheckInfo) {
  765. const checkInfo = this.ctx.helper._.assign({}, parentCheckInfo);
  766. for (const ct of checkType) {
  767. if (bills[ct + '_limit'] > 0) {
  768. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  769. }
  770. }
  771. if (bills.children && bills.children.length > 0) {
  772. for (const c of bills.children) {
  773. this._recursiveCheckBills3fLimit(checkType, c, checkInfo);
  774. }
  775. } else {
  776. this._checkLeafBills3fLimit(checkType, bills, checkInfo);
  777. }
  778. }
  779. loadData(bills, pos) {
  780. this.checkBills.loadDatas(bills);
  781. this.checkPos.loadDatas(pos);
  782. }
  783. checkSibling() {
  784. for (const node of this.checkBills.nodes) {
  785. if (!node.children || node.children.length === 0) continue;
  786. let hasXmj, hasGcl;
  787. for (const child of node.children) {
  788. if (child.b_code) hasXmj = true;
  789. if (!child.b_code) hasGcl = true;
  790. }
  791. if (hasXmj && hasGcl) this.checkResult.error.push({
  792. ledger_id: node.ledger_id,
  793. b_code: node.b_code,
  794. name: node.name,
  795. errorType: 'sibling',
  796. });
  797. }
  798. }
  799. checkSameCode() {
  800. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  801. let xmj = [];
  802. const addXmjCheck = function (node) {
  803. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  804. for (const child of node.children) {
  805. addXmjCheck(child);
  806. }
  807. };
  808. for (const topLevel of this.checkBills.children) {
  809. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  810. addXmjCheck(topLevel);
  811. }
  812. const xmjPart = {}, xmjIndex = [];
  813. for (const x of xmj) {
  814. if (!xmjPart[x.code]) {
  815. xmjPart[x.code] = [];
  816. xmjIndex.push(x.code);
  817. }
  818. xmjPart[x.code].push(x);
  819. }
  820. for (const x of xmjIndex) {
  821. if (xmjPart[x].length <= 1) continue;
  822. for (const xp of xmjPart[x]) {
  823. this.checkResult.error.push({
  824. ledger_id: xp.ledger_id,
  825. b_code: xp.b_code,
  826. name: xp.name,
  827. errorType: 'same_code',
  828. })
  829. }
  830. }
  831. let check = null;
  832. while (xmj.length > 0) {
  833. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  834. if (check.length > 1) {
  835. for (const c of check) {
  836. this.checkResult.error.push({
  837. ledger_id: c.ledger_id,
  838. b_code: c.b_code,
  839. name: c.name,
  840. errorType: 'same_code',
  841. })
  842. }
  843. }
  844. }
  845. }
  846. check3fLimit(tender) {
  847. const check = [];
  848. if (tender.s2b_gxby_limit) check.push('gxby');
  849. if (tender.s2b_dagl_limit) check.push('dagl');
  850. if (check.length === 0) return;
  851. for (const b of this.checkBills.children) {
  852. this._recursiveCheckBills3fLimit(check, b, {});
  853. }
  854. }
  855. checkBillsQty(fields) {
  856. for (const b of this.checkBills.nodes) {
  857. if (b.children && b.children.length > 0) continue;
  858. const pr = this.checkPos.getLedgerPos(b.id);
  859. if (!pr || pr.length === 0) continue;
  860. const checkData = {},
  861. calcData = {};
  862. for (const field of fields) {
  863. checkData[field] = b[field] ? b[field] : 0;
  864. }
  865. for (const p of pr) {
  866. for (const field of fields) {
  867. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  868. }
  869. }
  870. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  871. this.checkResult.error.push({
  872. ledger_id: b.ledger_id,
  873. b_code: b.b_code,
  874. name: b.name,
  875. errorType: 'qty',
  876. error: { checkData, calcData },
  877. });
  878. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  879. this.checkResult.source.bills.push(b);
  880. for (const p of pr) {
  881. this.checkResult.source.pos.push(p);
  882. }
  883. }
  884. }
  885. }
  886. }
  887. checkBillsTp(field, decimal, filter) {
  888. for (const b of this.checkBills.nodes) {
  889. if ((b.children && b.children.length > 0)) continue;
  890. if (filter && filter(b)) continue;
  891. const checkData = {}, calcData = {};
  892. for (const f of field) {
  893. checkData[f.tp] = b[f.tp] || 0;
  894. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  895. }
  896. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  897. this.checkResult.error.push({
  898. ledger_id: b.ledger_id,
  899. b_code: b.b_code,
  900. name: b.name,
  901. errorType: 'tp',
  902. error: { checkData, calcData },
  903. });
  904. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  905. this.checkResult.source.bills.push(b);
  906. }
  907. }
  908. }
  909. }
  910. _checkBillsOverRange(bills, posRange, isTz) {
  911. // if (isTz && posRange.length > 0) {
  912. // for (const p of posRange) {
  913. // const end_contract_qty = this.add(p.pre_contract_qty, p.contract_qty);
  914. // if (end_contract_qty > p.quantity) return true;
  915. // }
  916. // return false;
  917. // } else {
  918. // const end_qc_qty = this.add(bills.qc_qty, bills.pre_qc_qty);
  919. // const end_qc_tp = this.add(bills.qc_tp, bills.pre_qc_tp);
  920. // const end_gather_qty = this.sum([bills.contract_qty, bills.pre_contract_qty, end_qc_qty]);
  921. // const end_gather_tp = this.sum([bills.contract_tp, bills.pre_contract_tp, end_qc_tp]);
  922. // if (isTz) {
  923. // if (end_gather_qty) {
  924. // return !bills.quantity || Math.abs(end_gather_qty) > Math.abs(this.add(bills.quantity, end_qc_qty));
  925. // } else if (end_gather_tp) {
  926. // return !bills.total_price || Math.abs(end_gather_tp) > Math.abs(this.add(bills.total_price, end_qc_tp));
  927. // }
  928. // } else {
  929. // if (end_gather_qty) {
  930. // return !bills.deal_qty || Math.abs(end_gather_qty) > Math.abs(this.add(bills.deal_qty, end_qc_qty));
  931. // } else if (end_gather_tp) {
  932. // return !bills.deal_tp || Math.abs(end_gather_tp) > Math.abs(this.add(bills.deal_tp, end_qc_tp));
  933. // }
  934. // }
  935. // }
  936. if (isTz && posRange.length > 0) {
  937. for (const p of posRange) {
  938. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  939. if (!p.quantity && !!end_contract_qty) return true;
  940. if (p.quantity > 0) {
  941. if (end_contract_qty > p.final_1_qty) return true;
  942. } else {
  943. if (end_contract_qty < p.final_1_qty || end_contract_qty > 0) return true;
  944. }
  945. }
  946. }
  947. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  948. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  949. if (bills.is_tp) {
  950. const compare_tp = isTz ? bills.total_price : bills.deal_tp;
  951. if (!compare_tp) return !!end_contract_tp;
  952. return compare_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  953. } else {
  954. const compare_qty = isTz ? bills.final_1_qty : bills.deal_final_1_qty;
  955. if (!compare_qty) return !!end_contract_qty;
  956. return compare_qty >= 0 ? end_contract_qty > compare_qty : end_contract_qty < compare_qty || end_contract_qty > 0;
  957. }
  958. }
  959. checkOverRange() {
  960. const isTz = this.ctx.tender.data.measure_type === this.measureType.tz.value;
  961. for (const b of this.checkBills.nodes) {
  962. if (b.children && b.children.length > 0) continue;
  963. const pr = this.checkPos.getLedgerPos(b.id) || [];
  964. if (this._checkBillsOverRange(b, pr, isTz)) {
  965. this.checkResult.error.push({
  966. ledger_id: b.ledger_id,
  967. b_code: b.b_code,
  968. name: b.name,
  969. errorType: 'over',
  970. });
  971. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  972. this.checkResult.source.bills.push(b);
  973. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  974. }
  975. }
  976. }
  977. }
  978. checkMinusChangeBills(change, changeBills, finalStageChange) {
  979. const error = this.checkResult.error;
  980. const helper = this.ctx.helper;
  981. const changeIndex = {};
  982. change.forEach(c => {
  983. changeIndex[c.cid] = c;
  984. c.bills = [];
  985. c.billsIndex = {};
  986. c.stageChange = [];
  987. });
  988. changeBills.forEach(cb => {
  989. const c = changeIndex[cb.cid];
  990. if (c) c.bills.push(cb);
  991. c.billsIndex[cb.id] = cb;
  992. cb.used_qty = 0;
  993. cb.qty = parseFloat(cb.samount);
  994. });
  995. finalStageChange.forEach(sc => {
  996. if (!sc.qty) return;
  997. const c = changeIndex[sc.cid];
  998. if (c) {
  999. c.used = true;
  1000. const cb = c.billsIndex[sc.cbid];
  1001. if (cb) cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1002. }
  1003. });
  1004. change.forEach(c => {
  1005. if (!c.used) return;
  1006. c.bills.forEach(b => {
  1007. if (b.qty >= 0) return;
  1008. if (!helper.numEqual(b.used_qty, b.qty)) error.push({ b_code: b.code, name: b.name, errorType: 'minus_cb', memo: c.code });
  1009. });
  1010. });
  1011. }
  1012. }
  1013. class reviseTree extends billsTree {
  1014. constructor (ctx, setting) {
  1015. super(ctx, setting);
  1016. this.price = [];
  1017. }
  1018. loadRevisePrice(price, decimal) {
  1019. this.decimal = decimal;
  1020. this.price = price || [];
  1021. this.rela_price = [];
  1022. this.common_price = [];
  1023. this.price.forEach(x => {
  1024. if (x.rela_lid) {
  1025. x.rela_lid = x.rela_lid.split(',');
  1026. this.rela_price.push(x);
  1027. } else {
  1028. this.common_price.push(x);
  1029. }
  1030. });
  1031. }
  1032. checkRevisePrice(d) {
  1033. const helper = this.ctx.helper;
  1034. const setting = this.setting;
  1035. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1036. const checkRela = function(rela_lid) {
  1037. if (!rela_lid || rela_lid.length === 0) return false;
  1038. for (const lid of rela_lid) {
  1039. if (pid.indexOf(lid) >= 0) return true;
  1040. }
  1041. return false;
  1042. };
  1043. let p = this.rela_price.find(x => {
  1044. return x.b_code === d.b_code &&
  1045. ((!x.name && !d.name) || x.name === d.name) &&
  1046. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1047. helper.checkZero(x.org_price - d.unit_price) &&
  1048. checkRela(x.rela_lid);
  1049. });
  1050. if (!p) p = this.common_price.find(x => {
  1051. return x.b_code === d.b_code &&
  1052. ((!x.name && !d.name) || x.name === d.name) &&
  1053. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1054. helper.checkZero(x.org_price - d.unit_price);
  1055. });
  1056. if (!p) return false;
  1057. d.org_price = p.org_price;
  1058. d.unit_price = p.new_price;
  1059. d.deal_tp = helper.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1060. d.sgfh_tp = helper.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1061. d.sjcl_tp = helper.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1062. d.qtcl_tp = helper.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1063. d.total_price = helper.mul(d.quantity, d.unit_price, this.decimal.tp);
  1064. return true;
  1065. }
  1066. loadDatas(datas) {
  1067. super.loadDatas(datas);
  1068. if (this.price.length > 0) {
  1069. for (const d of this.datas) {
  1070. if (d.children && d.children.length > 0) continue;
  1071. if (!d.b_code) continue;
  1072. this.checkRevisePrice(d);
  1073. }
  1074. }
  1075. }
  1076. getUpdateReviseData() {
  1077. return this.datas.map(x => {
  1078. if (x.children && x.children.length > 0) {
  1079. return {
  1080. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1081. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: x.is_leaf,
  1082. node_type: x.node_type, check_calc: x.check_calc,
  1083. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1084. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1085. unit_price: 0, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1086. quantity: 0, total_price: 0,
  1087. sgfh_qty: 0, sgfh_tp: 0, sgfh_expr: '',
  1088. sjcl_qty: 0, sjcl_tp: 0, sjcl_expr: '',
  1089. qtcl_qty: 0, qtcl_tp: 0, qtcl_expr: '',
  1090. deal_qty: 0, deal_tp: 0,
  1091. };
  1092. } else {
  1093. return {
  1094. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1095. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: x.is_leaf,
  1096. node_type: x.node_type, check_calc: x.check_calc,
  1097. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1098. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1099. unit_price: x.unit_price, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1100. quantity: x.quantity, total_price: x.total_price,
  1101. sgfh_qty: x.sgfh_qty, sgfh_tp: x.sgfh_tp, sgfh_expr: x.sgfh_expr,
  1102. sjcl_qty: x.sjcl_qty, sjcl_tp: x.sjcl_tp, sjcl_expr: x.sjcl_expr,
  1103. qtcl_qty: x.qtcl_qty, qtcl_tp: x.qtcl_tp, qtcl_expr: x.qtcl_expr,
  1104. deal_qty: x.deal_qty, deal_tp: x.deal_tp,
  1105. };
  1106. }
  1107. });
  1108. }
  1109. sum() {
  1110. const result = { total_price: 0 };
  1111. for (const d of this.datas) {
  1112. if (d.children && d.children.length > 0) continue;
  1113. result.total_price = this.ctx.helper.add(result.total_price, d.total_price);
  1114. }
  1115. return result;
  1116. }
  1117. }
  1118. module.exports = {
  1119. billsTree,
  1120. pos,
  1121. filterTree,
  1122. filterGatherTree,
  1123. gatherTree,
  1124. gatherPos,
  1125. checkData,
  1126. reviseTree,
  1127. };