ledger.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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, loadPosFun) {
  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. if (node.children && node.children.length > 0) {
  455. for (const c of node.children) {
  456. this.loadGatherNode(c, cur, loadFun, loadPosFun);
  457. }
  458. } else if (loadPosFun) {
  459. loadPosFun(cur, node);
  460. }
  461. }
  462. generateSortNodes() {
  463. const self = this;
  464. const addSortNode = function (node) {
  465. self.nodes.push(node);
  466. for (const c of node.children) {
  467. addSortNode(c);
  468. }
  469. };
  470. this.nodes = [];
  471. for (const n of this.children) {
  472. addSortNode(n);
  473. }
  474. }
  475. loadGatherTree(sourceTree, loadFun, loadPosFun) {
  476. for (const c of sourceTree.children) {
  477. this.loadGatherNode(c, null, loadFun, loadPosFun);
  478. }
  479. }
  480. resortChildrenByCustom(fun) {
  481. for (const n of this.datas) {
  482. if (n.children && n.children.length > 1) {
  483. n.children.sort(fun);
  484. n.children.forEach((x, i) => { x.order = i + 1; });
  485. }
  486. }
  487. this.generateSortNodes();
  488. }
  489. resortChildrenDefault() {
  490. const helper = this.ctx.helper;
  491. this.resortChildrenByCustom((x, y) => {
  492. const iCode = (x.code || y.code) ? helper.compareCode(x.code, y.code) : helper.compareCode(x.b_code, y.b_code);
  493. if (iCode) return iCode;
  494. if (!x.name) return -1;
  495. if (!y.name) return 1;
  496. return x.name.localeCompare(y.name);
  497. })
  498. }
  499. calculateSum() {
  500. if (this.setting.calcSum) {
  501. for (const d of this.datas) {
  502. this.setting.calcSum(d, this.count);
  503. }
  504. }
  505. }
  506. }
  507. class pos {
  508. /**
  509. * 构造函数
  510. * @param {id|Number, masterId|Number} setting
  511. */
  512. constructor (setting) {
  513. // 无索引
  514. this.datas = [];
  515. // 以key为索引
  516. this.items = {};
  517. // 以分类id为索引的有序
  518. this.ledgerPos = {};
  519. // pos设置
  520. this.setting = setting;
  521. }
  522. /**
  523. * 加载部位明细数据
  524. * @param datas
  525. */
  526. loadDatas(datas) {
  527. this.datas = datas;
  528. this.items = {};
  529. this.ledgerPos = {};
  530. for (const data of this.datas) {
  531. const key = itemsPre + data[this.setting.id];
  532. this.items[key] = data;
  533. const masterKey = itemsPre + data[this.setting.ledgerId];
  534. if (!this.ledgerPos[masterKey]) {
  535. this.ledgerPos[masterKey] = [];
  536. }
  537. this.ledgerPos[masterKey].push(data);
  538. }
  539. for (const prop in this.ledgerPos) {
  540. this.resortLedgerPos(this.ledgerPos[prop]);
  541. }
  542. }
  543. getLedgerPos(mid) {
  544. return this.ledgerPos[itemsPre + mid];
  545. }
  546. resortLedgerPos(ledgerPos) {
  547. if (ledgerPos instanceof Array) {
  548. ledgerPos.sort(function (a, b) {
  549. return a.porder - b.porder;
  550. })
  551. }
  552. }
  553. /**
  554. * 计算全部
  555. */
  556. calculateAll(fun) {
  557. const calcFun = fun ? fun : this.setting.calc;
  558. if (!calcFun) return;
  559. for (const pos of this.datas) {
  560. calcFun(pos);
  561. }
  562. }
  563. getDatas () {
  564. return this.datas;
  565. }
  566. }
  567. class gatherPos extends pos {
  568. loadGatherPos(ledgerId, sourcePosRange, loadFun) {
  569. let posRange = this.getledgerPos(itemsPre + ledgerId);
  570. if (!posRange) {
  571. posRange = [];
  572. this.ledgerPos[itemsPre + ledgerId] = posRange;
  573. }
  574. for (const spr of sourcePosRange) {
  575. let gp = posRange.find(x => { return x.name === spr.name; });
  576. if (!gp) {
  577. gp = { name: spr.name };
  578. gp[this.setting.ledgerId] = ledgerId;
  579. this.datas.push(gp);
  580. posRange.push(gp);
  581. }
  582. loadFun(gp, spr);
  583. }
  584. }
  585. }
  586. class checkData {
  587. constructor(ctx, measureType) {
  588. this.ctx = ctx;
  589. this.checkBills = new billsTree(ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  590. this.checkPos = new pos({ id: 'id', ledgerId: 'lid' });
  591. this.checkResult = {
  592. error: [],
  593. source: {
  594. bills: [],
  595. pos: [],
  596. },
  597. };
  598. this.measureType = measureType;
  599. }
  600. _check3f(data, limit, ratio) {
  601. if (limit === 0) {
  602. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  603. }
  604. if (limit === 1) {
  605. if (ratio === 0) {
  606. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  607. } else {
  608. const tp = this.ctx.helper.mul(data.final_1_tp, this.ctx.helper.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  609. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp);
  610. if (tp > checkTp) return 1; // 违规
  611. if (tp < checkTp) return 2; // 漏计
  612. }
  613. }
  614. return 0; // 合法
  615. }
  616. _check3fQty(data, limit, ratio, unit) {
  617. if (limit === 0) {
  618. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  619. }
  620. if (limit === 1) {
  621. if (!ratio || ratio === 0) {
  622. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  623. } else {
  624. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  625. const checkQty = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(ratio, 100, 4), precision.value);
  626. const qty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty);
  627. if (qty > checkQty) return 1; // 违规
  628. if (qty < checkQty) return 2; // 漏计
  629. }
  630. }
  631. return 0; // 合法
  632. }
  633. _getRatio(type, status) {
  634. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  635. const sc = statusConst.find(x => { return x.value === status });
  636. return sc ? sc.ratio : null;
  637. }
  638. _getValid = function (type, status, limit) {
  639. if (limit) {
  640. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  641. const sc = statusConst.find(x => { return x.value === status; });
  642. return sc ? (sc.limit ? 1 : 0) : 0;
  643. } else {
  644. return -1;
  645. }
  646. };
  647. _checkLeafBills3fLimit(checkType, bills, checkInfo) {
  648. const over = [], lost = [];
  649. const posRange = this.checkPos.getLedgerPos(bills.id);
  650. if (posRange && posRange.length > 0) {
  651. for (const p of posRange) {
  652. const posCheckInfo = this.ctx.helper._.assign({}, checkInfo);
  653. for (const ct of checkType) {
  654. if (p[ct + '_limit'] > 0) {
  655. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  656. }
  657. }
  658. for (const ct of checkType) {
  659. const checkResult = this._check3fQty(p, this._getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']), this._getRatio(ct, p[ct+'_status']), bills.unit);
  660. if (checkResult === 1) {
  661. if (over.indexOf(ct) === -1) over.push(ct);
  662. }
  663. if (checkResult === 2) {
  664. if (lost.indexOf(ct) === -1) lost.push(ct);
  665. }
  666. }
  667. }
  668. } else {
  669. for (const ct of checkType) {
  670. const checkResult = bills.is_tp
  671. ? this._check3f(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']))
  672. : this._check3fQty(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']), bills.unit);
  673. if (checkResult === 1) {
  674. if (over.indexOf(ct) === -1) over.push(ct);
  675. }
  676. if (checkResult === 2) {
  677. if (lost.indexOf(ct) === -1) lost.push(ct);
  678. }
  679. }
  680. }
  681. if (over.length + lost.length > 0) {
  682. for (const o of over) {
  683. this.checkResult.error.push({
  684. ledger_id: bills.ledger_id,
  685. b_code: bills.b_code,
  686. name: bills.name,
  687. errorType: 's2b_over_' + o,
  688. });
  689. }
  690. for (const l of lost) {
  691. this.checkResult.error.push({
  692. ledger_id: bills.ledger_id,
  693. b_code: bills.b_code,
  694. name: bills.name,
  695. errorType: 's2b_lost_' + l,
  696. });
  697. }
  698. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === bills.ledger_id})) {
  699. this.checkResult.source.bills.push(bills);
  700. if (posRange && posRange.length > 0) this.checkResult.source.pos.push(...posRange);
  701. }
  702. }
  703. }
  704. _recursiveCheckBills3fLimit(checkType, bills, parentCheckInfo) {
  705. const checkInfo = this.ctx.helper._.assign({}, parentCheckInfo);
  706. for (const ct of checkType) {
  707. if (bills[ct + '_limit'] > 0) {
  708. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  709. }
  710. }
  711. if (bills.children && bills.children.length > 0) {
  712. for (const c of bills.children) {
  713. this._recursiveCheckBills3fLimit(checkType, c, checkInfo);
  714. }
  715. } else {
  716. this._checkLeafBills3fLimit(checkType, bills, checkInfo);
  717. }
  718. }
  719. loadData(bills, pos) {
  720. this.checkBills.loadDatas(bills);
  721. this.checkPos.loadDatas(pos);
  722. }
  723. checkSibling() {
  724. for (const node of this.checkBills.nodes) {
  725. if (!node.children || node.children.length === 0) continue;
  726. let hasXmj, hasGcl;
  727. for (const child of node.children) {
  728. if (child.b_code) hasXmj = true;
  729. if (!child.b_code) hasGcl = true;
  730. }
  731. if (hasXmj && hasGcl) this.checkResult.error.push({
  732. ledger_id: node.ledger_id,
  733. b_code: node.b_code,
  734. name: node.name,
  735. errorType: 'sibling',
  736. });
  737. }
  738. }
  739. checkSameCode() {
  740. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  741. let xmj = [];
  742. const addXmjCheck = function (node) {
  743. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  744. for (const child of node.children) {
  745. addXmjCheck(child);
  746. }
  747. };
  748. for (const topLevel of this.checkBills.children) {
  749. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  750. addXmjCheck(topLevel);
  751. }
  752. const xmjPart = {}, xmjIndex = [];
  753. for (const x of xmj) {
  754. if (!xmjPart[x.code]) {
  755. xmjPart[x.code] = [];
  756. xmjIndex.push(x.code);
  757. }
  758. xmjPart[x.code].push(x);
  759. }
  760. for (const x of xmjIndex) {
  761. if (xmjPart[x].length <= 1) continue;
  762. for (const xp of xmjPart[x]) {
  763. this.checkResult.error.push({
  764. ledger_id: xp.ledger_id,
  765. b_code: xp.b_code,
  766. name: xp.name,
  767. errorType: 'same_code',
  768. })
  769. }
  770. }
  771. let check = null;
  772. while (xmj.length > 0) {
  773. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  774. if (check.length > 1) {
  775. for (const c of check) {
  776. this.checkResult.error.push({
  777. ledger_id: c.ledger_id,
  778. b_code: c.b_code,
  779. name: c.name,
  780. errorType: 'same_code',
  781. })
  782. }
  783. }
  784. }
  785. }
  786. check3fLimit(tender) {
  787. const check = [];
  788. if (tender.s2b_gxby_limit) check.push('gxby');
  789. if (tender.s2b_dagl_limit) check.push('dagl');
  790. if (check.length === 0) return;
  791. for (const b of this.checkBills.children) {
  792. this._recursiveCheckBills3fLimit(check, b, {});
  793. }
  794. }
  795. checkBillsQty(fields) {
  796. for (const b of this.checkBills.nodes) {
  797. if (b.children && b.children.length > 0) continue;
  798. const pr = this.checkPos.getLedgerPos(b.id);
  799. if (!pr || pr.length === 0) continue;
  800. const checkData = {},
  801. calcData = {};
  802. for (const field of fields) {
  803. checkData[field] = b[field] ? b[field] : 0;
  804. }
  805. for (const p of pr) {
  806. for (const field of fields) {
  807. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  808. }
  809. }
  810. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  811. this.checkResult.error.push({
  812. ledger_id: b.ledger_id,
  813. b_code: b.b_code,
  814. name: b.name,
  815. errorType: 'qty',
  816. error: { checkData, calcData },
  817. });
  818. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  819. this.checkResult.source.bills.push(b);
  820. for (const p of pr) {
  821. this.checkResult.source.pos.push(p);
  822. }
  823. }
  824. }
  825. }
  826. }
  827. checkBillsTp(field, decimal, filter) {
  828. for (const b of this.checkBills.nodes) {
  829. if ((b.children && b.children.length > 0)) continue;
  830. if (filter && filter(b)) continue;
  831. const checkData = {}, calcData = {};
  832. for (const f of field) {
  833. checkData[f.tp] = b[f.tp] || 0;
  834. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  835. }
  836. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  837. this.checkResult.error.push({
  838. ledger_id: b.ledger_id,
  839. b_code: b.b_code,
  840. name: b.name,
  841. errorType: 'tp',
  842. error: { checkData, calcData },
  843. });
  844. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  845. this.checkResult.source.bills.push(b);
  846. }
  847. }
  848. }
  849. }
  850. _checkBillsOverRange(bills, posRange, isTz) {
  851. // if (isTz && posRange.length > 0) {
  852. // for (const p of posRange) {
  853. // const end_contract_qty = this.add(p.pre_contract_qty, p.contract_qty);
  854. // if (end_contract_qty > p.quantity) return true;
  855. // }
  856. // return false;
  857. // } else {
  858. // const end_qc_qty = this.add(bills.qc_qty, bills.pre_qc_qty);
  859. // const end_qc_tp = this.add(bills.qc_tp, bills.pre_qc_tp);
  860. // const end_gather_qty = this.sum([bills.contract_qty, bills.pre_contract_qty, end_qc_qty]);
  861. // const end_gather_tp = this.sum([bills.contract_tp, bills.pre_contract_tp, end_qc_tp]);
  862. // if (isTz) {
  863. // if (end_gather_qty) {
  864. // return !bills.quantity || Math.abs(end_gather_qty) > Math.abs(this.add(bills.quantity, end_qc_qty));
  865. // } else if (end_gather_tp) {
  866. // return !bills.total_price || Math.abs(end_gather_tp) > Math.abs(this.add(bills.total_price, end_qc_tp));
  867. // }
  868. // } else {
  869. // if (end_gather_qty) {
  870. // return !bills.deal_qty || Math.abs(end_gather_qty) > Math.abs(this.add(bills.deal_qty, end_qc_qty));
  871. // } else if (end_gather_tp) {
  872. // return !bills.deal_tp || Math.abs(end_gather_tp) > Math.abs(this.add(bills.deal_tp, end_qc_tp));
  873. // }
  874. // }
  875. // }
  876. if (isTz && posRange.length > 0) {
  877. if (posRange.length > 0) {
  878. for (const p of posRange) {
  879. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  880. if (!p.quantity && !!end_contract_qty) return true;
  881. if (p.quantity > 0) {
  882. if (end_contract_qty > p.final_1_qty) return true;
  883. } else {
  884. if (end_contract_qty < p.final_1_qty || end_contract_qty > 0) return true;
  885. }
  886. }
  887. return false;
  888. }
  889. } else {
  890. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  891. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  892. if (bills.is_tp) {
  893. const compare_tp = isTz ? bills.total_price : bills.deal_tp;
  894. if (!compare_tp) return !!end_contract_tp;
  895. return compare_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  896. } else {
  897. const compare_qty = isTz ? bills.final_1_qty : bills.deal_final_1_qty;
  898. if (!compare_qty) return !!end_contract_qty;
  899. return compare_qty >= 0 ? end_contract_qty > compare_qty : end_contract_qty < compare_qty || end_contract_qty > 0;
  900. }
  901. }
  902. }
  903. checkOverRange() {
  904. const isTz = this.ctx.tender.data.measure_type === this.measureType.tz.value;
  905. for (const b of this.checkBills.nodes) {
  906. if (b.children && b.children.length > 0) continue;
  907. const pr = this.checkPos.getLedgerPos(b.id) || [];
  908. if (this._checkBillsOverRange(b, pr, isTz)) {
  909. this.checkResult.error.push({
  910. ledger_id: b.ledger_id,
  911. b_code: b.b_code,
  912. name: b.name,
  913. errorType: 'over',
  914. });
  915. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  916. this.checkResult.source.bills.push(b);
  917. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  918. }
  919. }
  920. }
  921. }
  922. checkMinusChangeBills(change, changeBills, finalStageChange) {
  923. const error = this.checkResult.error;
  924. const helper = this.ctx.helper;
  925. const changeIndex = {};
  926. change.forEach(c => {
  927. changeIndex[c.cid] = c;
  928. c.bills = [];
  929. c.billsIndex = {};
  930. c.stageChange = [];
  931. });
  932. changeBills.forEach(cb => {
  933. const c = changeIndex[cb.cid];
  934. if (c) c.bills.push(cb);
  935. c.billsIndex[cb.id] = cb;
  936. cb.used_qty = 0;
  937. cb.qty = parseFloat(cb.samount);
  938. });
  939. finalStageChange.forEach(sc => {
  940. if (!sc.qty) return;
  941. const c = changeIndex[sc.cid];
  942. if (c) {
  943. c.used = true;
  944. const cb = c.billsIndex[sc.cbid];
  945. if (cb) cb.used_qty = helper.add(cb.used_qty, sc.qty);
  946. }
  947. });
  948. change.forEach(c => {
  949. if (!c.used) return;
  950. c.bills.forEach(b => {
  951. if (b.qty >= 0) return;
  952. if (!helper.numEqual(b.used_qty, b.qty)) error.push({ b_code: b.code, name: b.name, errorType: 'minus_cb', memo: c.code });
  953. });
  954. });
  955. }
  956. }
  957. class reviseTree extends billsTree {
  958. constructor (ctx, setting) {
  959. super(ctx, setting);
  960. this.price = [];
  961. }
  962. loadRevisePrice(price, decimal) {
  963. this.decimal = decimal;
  964. this.price = price || [];
  965. }
  966. checkRevisePrice(d) {
  967. const helper = this.ctx.helper;
  968. const p = this.price.find(x => {
  969. return x.b_code === d.b_code &&
  970. ((!x.name && !d.name) || x.name === d.name) &&
  971. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  972. helper.checkZero(x.org_price - d.unit_price);
  973. });
  974. if (!p) return false;
  975. d.org_price = p.org_price;
  976. d.unit_price = p.new_price;
  977. d.deal_tp = helper.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  978. d.sgfh_tp = helper.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  979. d.sjcl_tp = helper.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  980. d.qtcl_tp = helper.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  981. d.total_price = helper.mul(d.quantity, d.unit_price, this.decimal.tp);
  982. return true;
  983. }
  984. loadDatas(datas) {
  985. super.loadDatas(datas);
  986. if (this.price.length > 0) {
  987. for (const d of this.datas) {
  988. if (d.children && d.children.length > 0) continue;
  989. if (!d.b_code) continue;
  990. this.checkRevisePrice(d);
  991. }
  992. }
  993. }
  994. getUpdateReviseData() {
  995. return this.datas.map(x => {
  996. if (x.children && x.children.length > 0) {
  997. return {
  998. id: x.id, tender_id: x.tender_id, crid: x.crid,
  999. 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,
  1000. node_type: x.node_type, check_calc: x.check_calc,
  1001. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1002. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1003. unit_price: 0, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1004. quantity: 0, total_price: 0,
  1005. sgfh_qty: 0, sgfh_tp: 0, sgfh_expr: '',
  1006. sjcl_qty: 0, sjcl_tp: 0, sjcl_expr: '',
  1007. qtcl_qty: 0, qtcl_tp: 0, qtcl_expr: '',
  1008. };
  1009. } else {
  1010. return {
  1011. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1012. 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,
  1013. node_type: x.node_type, check_calc: x.check_calc,
  1014. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1015. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1016. unit_price: x.unit_price, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1017. quantity: x.quantity, total_price: x.total_price,
  1018. sgfh_qty: x.sgfh_qty, sgfh_tp: x.sgfh_tp, sgfh_expr: x.sgfh_expr,
  1019. sjcl_qty: x.sjcl_qty, sjcl_tp: x.sjcl_tp, sjcl_expr: x.sjcl_expr,
  1020. qtcl_qty: x.qtcl_qty, qtcl_tp: x.qtcl_tp, qtcl_expr: x.qtcl_expr,
  1021. };
  1022. }
  1023. });
  1024. }
  1025. sum() {
  1026. const result = { total_price: 0 };
  1027. for (const d of this.datas) {
  1028. if (d.children && d.children.length > 0) continue;
  1029. result.total_price = this.ctx.helper.add(result.total_price, d.total_price);
  1030. }
  1031. return result;
  1032. }
  1033. }
  1034. module.exports = {
  1035. billsTree,
  1036. pos,
  1037. filterTree,
  1038. filterGatherTree,
  1039. gatherTree,
  1040. gatherPos,
  1041. checkData,
  1042. reviseTree,
  1043. };