ledger.js 39 KB

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