ledger.js 40 KB

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