ledger.js 51 KB

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