ledger.js 37 KB

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