ledger.js 39 KB

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