ledger.js 39 KB

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