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