ledger.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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);
  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. calculateSum() {
  478. if (this.setting.calcSum) {
  479. for (const d of this.datas) {
  480. this.setting.calcSum(d, this.count);
  481. }
  482. }
  483. }
  484. }
  485. class pos {
  486. /**
  487. * 构造函数
  488. * @param {id|Number, masterId|Number} setting
  489. */
  490. constructor (setting) {
  491. // 无索引
  492. this.datas = [];
  493. // 以key为索引
  494. this.items = {};
  495. // 以分类id为索引的有序
  496. this.ledgerPos = {};
  497. // pos设置
  498. this.setting = setting;
  499. }
  500. /**
  501. * 加载部位明细数据
  502. * @param datas
  503. */
  504. loadDatas(datas) {
  505. this.datas = datas;
  506. this.items = {};
  507. this.ledgerPos = {};
  508. for (const data of this.datas) {
  509. const key = itemsPre + data[this.setting.id];
  510. this.items[key] = data;
  511. const masterKey = itemsPre + data[this.setting.ledgerId];
  512. if (!this.ledgerPos[masterKey]) {
  513. this.ledgerPos[masterKey] = [];
  514. }
  515. this.ledgerPos[masterKey].push(data);
  516. }
  517. for (const prop in this.ledgerPos) {
  518. this.resortLedgerPos(this.ledgerPos[prop]);
  519. }
  520. }
  521. getLedgerPos(mid) {
  522. return this.ledgerPos[itemsPre + mid];
  523. }
  524. resortLedgerPos(ledgerPos) {
  525. if (ledgerPos instanceof Array) {
  526. ledgerPos.sort(function (a, b) {
  527. return a.porder - b.porder;
  528. })
  529. }
  530. }
  531. /**
  532. * 计算全部
  533. */
  534. calculateAll(fun) {
  535. const calcFun = fun ? fun : this.setting.calc;
  536. if (!calcFun) return;
  537. for (const pos of this.datas) {
  538. calcFun(pos);
  539. }
  540. }
  541. getDatas () {
  542. return this.datas;
  543. }
  544. }
  545. class checkData {
  546. constructor(ctx, measureType) {
  547. this.ctx = ctx;
  548. this.checkBills = new billsTree(ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  549. this.checkPos = new pos({ id: 'id', ledgerId: 'lid' });
  550. this.checkResult = {
  551. error: [],
  552. source: {
  553. bills: [],
  554. pos: [],
  555. },
  556. };
  557. this.measureType = measureType;
  558. }
  559. _check3f(data, limit, ratio) {
  560. if (limit === 0) {
  561. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  562. }
  563. if (limit === 1) {
  564. if (ratio === 0) {
  565. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  566. } else {
  567. const tp = this.ctx.helper.mul(data.total_price, this.ctx.helper.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  568. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp);
  569. if (tp > checkTp) return 1; // 违规
  570. if (tp < checkTp) return 2; // 漏计
  571. }
  572. }
  573. return 0; // 合法
  574. }
  575. _check3fQty(data, limit, ratio, unit) {
  576. if (limit === 0) {
  577. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  578. }
  579. if (limit === 1) {
  580. if (!ratio || ratio === 0) {
  581. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  582. } else {
  583. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  584. const checkQty = this.ctx.helper.mul(data.quantity, this.ctx.helper.div(ratio, 100, 4), precision.value);
  585. const qty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty);
  586. if (qty > checkQty) return 1; // 违规
  587. if (qty < checkQty) return 2; // 漏计
  588. }
  589. }
  590. return 0; // 合法
  591. }
  592. _getRatio(type, status) {
  593. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  594. const sc = statusConst.find(x => { return x.value === status });
  595. return sc ? sc.ratio : null;
  596. }
  597. _getValid = function (type, status, limit) {
  598. if (limit) {
  599. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  600. const sc = statusConst.find(x => { return x.value === status; });
  601. return sc ? (sc.limit ? 1 : 0) : 0;
  602. } else {
  603. return -1;
  604. }
  605. };
  606. _checkLeafBills3fLimit(checkType, bills, checkInfo) {
  607. const over = [], lost = [];
  608. const posRange = this.checkPos.getLedgerPos(bills.id);
  609. if (posRange && posRange.length > 0) {
  610. for (const p of posRange) {
  611. const posCheckInfo = this.ctx.helper._.assign({}, checkInfo);
  612. for (const ct of checkType) {
  613. if (p[ct + '_limit'] > 0) {
  614. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  615. }
  616. }
  617. for (const ct of checkType) {
  618. const checkResult = this._check3fQty(p, this._getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']), this._getRatio(ct, p[ct+'_status']), bills.unit);
  619. if (checkResult === 1) {
  620. if (over.indexOf(ct) === -1) over.push(ct);
  621. }
  622. if (checkResult === 2) {
  623. if (lost.indexOf(ct) === -1) lost.push(ct);
  624. }
  625. }
  626. }
  627. } else {
  628. for (const ct of checkType) {
  629. const checkResult = bills.is_tp
  630. ? this._check3f(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']))
  631. : this._check3fQty(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']), bills.unit);
  632. if (checkResult === 1) {
  633. if (over.indexOf(ct) === -1) over.push(ct);
  634. }
  635. if (checkResult === 2) {
  636. if (lost.indexOf(ct) === -1) lost.push(ct);
  637. }
  638. }
  639. }
  640. if (over.length + lost.length > 0) {
  641. for (const o of over) {
  642. this.checkResult.error.push({
  643. ledger_id: bills.ledger_id,
  644. b_code: bills.b_code,
  645. name: bills.name,
  646. errorType: 's2b_over_' + o,
  647. });
  648. }
  649. for (const l of lost) {
  650. this.checkResult.error.push({
  651. ledger_id: bills.ledger_id,
  652. b_code: bills.b_code,
  653. name: bills.name,
  654. errorType: 's2b_lost_' + l,
  655. });
  656. }
  657. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === bills.ledger_id})) {
  658. this.checkResult.source.bills.push(bills);
  659. if (posRange && posRange.length > 0) this.checkResult.source.pos.push(...posRange);
  660. }
  661. }
  662. }
  663. _recursiveCheckBills3fLimit(checkType, bills, parentCheckInfo) {
  664. const checkInfo = this.ctx.helper._.assign({}, parentCheckInfo);
  665. for (const ct of checkType) {
  666. if (bills[ct + '_limit'] > 0) {
  667. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  668. }
  669. }
  670. if (bills.children && bills.children.length > 0) {
  671. for (const c of bills.children) {
  672. this._recursiveCheckBills3fLimit(checkType, c, checkInfo);
  673. }
  674. } else {
  675. this._checkLeafBills3fLimit(checkType, bills, checkInfo);
  676. }
  677. }
  678. loadData(bills, pos) {
  679. this.checkBills.loadDatas(bills);
  680. this.checkPos.loadDatas(pos);
  681. }
  682. checkSibling() {
  683. for (const node of this.checkBills.nodes) {
  684. if (!node.children || node.children.length === 0) continue;
  685. let hasXmj, hasGcl;
  686. for (const child of node.children) {
  687. if (child.b_code) hasXmj = true;
  688. if (!child.b_code) hasGcl = true;
  689. }
  690. if (hasXmj && hasGcl) this.checkResult.error.push({
  691. ledger_id: node.ledger_id,
  692. b_code: node.b_code,
  693. name: node.name,
  694. errorType: 'sibling',
  695. });
  696. }
  697. }
  698. checkSameCode() {
  699. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  700. let xmj = [];
  701. const addXmjCheck = function (node) {
  702. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  703. for (const child of node.children) {
  704. addXmjCheck(child);
  705. }
  706. };
  707. for (const topLevel of this.checkBills.children) {
  708. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  709. addXmjCheck(topLevel);
  710. }
  711. const xmjPart = {}, xmjIndex = [];
  712. for (const x of xmj) {
  713. if (!xmjPart[x.code]) {
  714. xmjPart[x.code] = [];
  715. xmjIndex.push(x.code);
  716. }
  717. xmjPart[x.code].push(x);
  718. }
  719. for (const x of xmjIndex) {
  720. if (xmjPart[x].length <= 1) continue;
  721. for (const xp of xmjPart[x]) {
  722. this.checkResult.error.push({
  723. ledger_id: xp.ledger_id,
  724. b_code: xp.b_code,
  725. name: xp.name,
  726. errorType: 'same_code',
  727. })
  728. }
  729. }
  730. let check = null;
  731. while (xmj.length > 0) {
  732. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  733. if (check.length > 1) {
  734. for (const c of check) {
  735. this.checkResult.error.push({
  736. ledger_id: c.ledger_id,
  737. b_code: c.b_code,
  738. name: c.name,
  739. errorType: 'same_code',
  740. })
  741. }
  742. }
  743. }
  744. }
  745. check3fLimit(tender) {
  746. const check = [];
  747. if (tender.s2b_gxby_limit) check.push('gxby');
  748. if (tender.s2b_dagl_limit) check.push('dagl');
  749. if (check.length === 0) return;
  750. for (const b of this.checkBills.children) {
  751. this._recursiveCheckBills3fLimit(check, b, {});
  752. }
  753. }
  754. checkBillsQty(fields) {
  755. for (const b of this.checkBills.nodes) {
  756. if (b.children && b.children.length > 0) continue;
  757. const pr = this.checkPos.getLedgerPos(b.id);
  758. if (!pr || pr.length === 0) continue;
  759. const checkData = {},
  760. calcData = {};
  761. for (const field of fields) {
  762. checkData[field] = b[field] ? b[field] : 0;
  763. }
  764. for (const p of pr) {
  765. for (const field of fields) {
  766. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  767. }
  768. }
  769. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  770. this.checkResult.error.push({
  771. ledger_id: b.ledger_id,
  772. b_code: b.b_code,
  773. name: b.name,
  774. errorType: 'qty',
  775. error: { checkData, calcData },
  776. });
  777. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  778. this.checkResult.source.bills.push(b);
  779. for (const p of pr) {
  780. this.checkResult.source.pos.push(p);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. checkBillsTp(field, decimal, filter) {
  787. for (const b of this.checkBills.nodes) {
  788. if ((b.children && b.children.length > 0) || !b.check_calc) continue;
  789. if (filter && filter(b)) continue;
  790. const checkData = {}, calcData = {};
  791. for (const f of field) {
  792. checkData[f.tp] = b[f.tp] || 0;
  793. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  794. }
  795. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  796. this.checkResult.error.push({
  797. ledger_id: b.ledger_id,
  798. b_code: b.b_code,
  799. name: b.name,
  800. errorType: 'tp',
  801. error: { checkData, calcData },
  802. });
  803. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  804. this.checkResult.source.bills.push(b);
  805. }
  806. }
  807. }
  808. }
  809. _checkBillsOverRange(bills, posRange, isTz) {
  810. // if (isTz && posRange.length > 0) {
  811. // for (const p of posRange) {
  812. // const end_contract_qty = this.add(p.pre_contract_qty, p.contract_qty);
  813. // if (end_contract_qty > p.quantity) return true;
  814. // }
  815. // return false;
  816. // } else {
  817. // const end_qc_qty = this.add(bills.qc_qty, bills.pre_qc_qty);
  818. // const end_qc_tp = this.add(bills.qc_tp, bills.pre_qc_tp);
  819. // const end_gather_qty = this.sum([bills.contract_qty, bills.pre_contract_qty, end_qc_qty]);
  820. // const end_gather_tp = this.sum([bills.contract_tp, bills.pre_contract_tp, end_qc_tp]);
  821. // if (isTz) {
  822. // if (end_gather_qty) {
  823. // return !bills.quantity || Math.abs(end_gather_qty) > Math.abs(this.add(bills.quantity, end_qc_qty));
  824. // } else if (end_gather_tp) {
  825. // return !bills.total_price || Math.abs(end_gather_tp) > Math.abs(this.add(bills.total_price, end_qc_tp));
  826. // }
  827. // } else {
  828. // if (end_gather_qty) {
  829. // return !bills.deal_qty || Math.abs(end_gather_qty) > Math.abs(this.add(bills.deal_qty, end_qc_qty));
  830. // } else if (end_gather_tp) {
  831. // return !bills.deal_tp || Math.abs(end_gather_tp) > Math.abs(this.add(bills.deal_tp, end_qc_tp));
  832. // }
  833. // }
  834. // }
  835. if (isTz && posRange.length > 0) {
  836. if (posRange.length > 0) {
  837. for (const p of posRange) {
  838. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  839. if (!p.quantity && !!end_contract_qty) return true;
  840. if (p.quantity > 0) {
  841. if (end_contract_qty > p.quantity) return true;
  842. } else {
  843. if (end_contract_qty < p.quantity || end_contract_qty > 0) return true;
  844. }
  845. }
  846. return false;
  847. }
  848. } else {
  849. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  850. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  851. if (bills.is_tp) {
  852. const compare_tp = isTz ? bills.total_price : bills.deal_tp;
  853. if (!compare_tp) return !!end_contract_tp;
  854. return compare_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  855. } else {
  856. const compare_qty = isTz ? bills.quantity : bills.deal_qty;
  857. if (!compare_qty) return !!end_contract_qty;
  858. return compare_qty >= 0 ? end_contract_qty > compare_qty : end_contract_qty < compare_qty || end_contract_qty > 0;
  859. }
  860. }
  861. }
  862. checkOverRange() {
  863. const isTz = this.ctx.tender.data.measure_type === this.measureType.tz.value;
  864. for (const b of this.checkBills.nodes) {
  865. if (b.children && b.children.length > 0) continue;
  866. const pr = this.checkPos.getLedgerPos(b.id) || [];
  867. if (this._checkBillsOverRange(b, pr, isTz)) {
  868. this.checkResult.error.push({
  869. ledger_id: b.ledger_id,
  870. b_code: b.b_code,
  871. name: b.name,
  872. errorType: 'over',
  873. });
  874. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  875. this.checkResult.source.bills.push(b);
  876. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. module.exports = {
  883. billsTree,
  884. pos,
  885. filterTree,
  886. filterGatherTree,
  887. gatherTree,
  888. checkData,
  889. };