ledger.js 34 KB

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