ledger.js 40 KB

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