ledger.js 41 KB

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