ledger.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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(data, condition) {
  844. let result = true;
  845. for (const c of condition) {
  846. result = c.rela && c.rela === 'or'
  847. ? (result || this.valueCheck[c.check](data[c.field], c.value, c.operation))
  848. : (result && this.valueCheck[c.check](data[c.field], c.value, c.operation));
  849. }
  850. return result;
  851. }
  852. _check3fMultiTp(data, limit, range) {
  853. if (limit === 0) {
  854. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  855. }
  856. if (limit === 1) {
  857. const lower = this.ctx.helper.mul(data.total_price, this.ctx.helper.div(range.lower, 100, 4), this.ctx.tender.info.decimal.tp);
  858. const upper = this.ctx.helper.mul(data.total_price, this.ctx.helper.div(range.upper, 100, 4), this.ctx.tender.info.decimal.tp);
  859. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp) || 0;
  860. if (checkTp > upper) return 1; // 违规
  861. if (checkTp < lower) return 2; // 漏计
  862. }
  863. return 0; // 合法
  864. }
  865. _check3fMultiQty(data, limit, range, unit) {
  866. if (limit === 0) {
  867. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  868. }
  869. if (limit === 1) {
  870. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  871. const lower = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(range.lower, 100, 4), precision.value);
  872. const upper = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(range.upper, 100, 4), precision.value);
  873. const checkQty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty) || 0;
  874. if (checkQty > upper) return 1; // 违规
  875. if (checkQty < lower) return 2; // 漏计
  876. }
  877. return 0; // 合法
  878. }
  879. _checkMulti3f(checkData, statusData, limitOption){
  880. if (!this._checkMultiCondition(statusData, limitOption.condition)) return;
  881. if (checkData.is_tp) return this._check3fMultiTp(checkData, limitOption.limit, limitOption);
  882. return this._check3fMultiQty(checkData, limitOption.limit, limitOption, checkData.unit);
  883. }
  884. _checkLeafPos3fMultiLimit(xmj, leafXmj, pos, posTree, multiLimit) {
  885. if (!multiLimit) return;
  886. const limitOption = this.limits.find(x => { return x.limit_id === multiLimit; });
  887. if (!limitOption) return;
  888. const ctNode = leafXmj || xmj;
  889. for (const lo of limitOption.options) {
  890. const checkResult = this._checkMulti3f(pos, ctNode, lo);
  891. if (checkResult > 0) {
  892. const errorType = checkResult === 1 ? '超计' : '漏计';
  893. if (!lo.rangeHint) lo.rangeHint = lo.limit ? (lo.lower === lo.upper ? `${lo.lower}%` : `${lo.lower}%~${lo.upper}%`) : '不允许计量';
  894. this.checkResult.error.push({
  895. xid: xmj.ledger_id, pid: pos.tree_id, errorType: 's2b_multi',
  896. code: pos.code, b_code: pos.b_code, name: pos.name, data: pos,
  897. memo: `${errorType}:${lo.hint}(${lo.rangeHint})`,
  898. });
  899. }
  900. }
  901. }
  902. _recursiveCheckPos3fMultiLimit(xmj, leafXmj, pos, posTree, parentLimit) {
  903. if (pos.children && pos.children.length > 0) {
  904. for (const p of pos.children) {
  905. this._recursiveCheckPos3fMultiLimit(xmj, pos.b_code ? leafXmj : pos, p, posTree, parentLimit);
  906. }
  907. } else {
  908. this._checkLeafPos3fMultiLimit(xmj, leafXmj, pos, posTree, parentLimit);
  909. }
  910. }
  911. _checkLeafXmj3fMultiLimit(xmj, multiLimit) {
  912. const posRange = this.checkPos.minorTree(xmj.id);
  913. if (posRange && posRange.nodes.length > 0) {
  914. for (const p of posRange.children) {
  915. this._recursiveCheckPos3fMultiLimit(xmj, null, p, posRange, multiLimit);
  916. }
  917. }
  918. }
  919. _recursiveCheckXmj3fMultiLimit(xmj, parentLimit = '') {
  920. const limit = xmj.multi_limit || parentLimit;
  921. if (xmj.children && xmj.children.length > 0) {
  922. for (const c of xmj.children) {
  923. this._recursiveCheckXmj3fMultiLimit(c, limit);
  924. }
  925. } else {
  926. this._checkLeafXmj3fMultiLimit(xmj, limit);
  927. }
  928. }
  929. loadData(bills, pos) {
  930. this.checkBills.loadDatas(bills);
  931. this.checkPos.loadDatas(pos);
  932. }
  933. checkSibling() {
  934. for (const node of this.checkBills.nodes) {
  935. if (!node.children || node.children.length === 0) continue;
  936. let hasXmj, hasGcl;
  937. for (const child of node.children) {
  938. if (child.b_code) hasXmj = true;
  939. if (!child.b_code) hasGcl = true;
  940. }
  941. if (hasXmj && hasGcl) this.checkResult.error.push({
  942. ledger_id: node.ledger_id,
  943. b_code: node.b_code,
  944. name: node.name,
  945. errorType: 'sibling',
  946. });
  947. }
  948. }
  949. checkSameCode() {
  950. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  951. let xmj = [];
  952. const addXmjCheck = function (node) {
  953. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  954. for (const child of node.children) {
  955. addXmjCheck(child);
  956. }
  957. };
  958. for (const topLevel of this.checkBills.children) {
  959. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  960. addXmjCheck(topLevel);
  961. }
  962. const xmjPart = {}, xmjIndex = [];
  963. for (const x of xmj) {
  964. if (!xmjPart[x.code]) {
  965. xmjPart[x.code] = [];
  966. xmjIndex.push(x.code);
  967. }
  968. xmjPart[x.code].push(x);
  969. }
  970. for (const x of xmjIndex) {
  971. if (xmjPart[x].length <= 1) continue;
  972. for (const xp of xmjPart[x]) {
  973. this.checkResult.error.push({
  974. ledger_id: xp.ledger_id,
  975. b_code: xp.b_code,
  976. name: xp.name,
  977. errorType: 'same_code',
  978. })
  979. }
  980. }
  981. let check = null;
  982. while (xmj.length > 0) {
  983. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  984. if (check.length > 1) {
  985. for (const c of check) {
  986. this.checkResult.error.push({
  987. ledger_id: c.ledger_id,
  988. b_code: c.b_code,
  989. name: c.name,
  990. errorType: 'same_code',
  991. })
  992. }
  993. }
  994. }
  995. }
  996. check3fLimit(tender) {
  997. const check = [];
  998. if (tender.s2b_gxby_limit) check.push('gxby');
  999. if (tender.s2b_dagl_limit) check.push('dagl');
  1000. if (check.length === 0) return;
  1001. for (const b of this.checkBills.children) {
  1002. this._recursiveCheckBills3fLimit(check, b, {});
  1003. }
  1004. }
  1005. check3fMultiLimit(tender, limits) {
  1006. this.limits = limits;
  1007. if (!tender.s2b_multi_limit) return;
  1008. this.valueCheck = new ValueCheck(this.ctx);
  1009. for (const b of this.checkBills.children) {
  1010. this._recursiveCheckXmj3fMultiLimit(b);
  1011. }
  1012. }
  1013. checkBillsQty(fields) {
  1014. for (const b of this.checkBills.nodes) {
  1015. if (b.children && b.children.length > 0) continue;
  1016. const pr = this.checkPos.getLedgerPos(b.id);
  1017. if (!pr || pr.length === 0) continue;
  1018. const checkData = {},
  1019. calcData = {};
  1020. for (const field of fields) {
  1021. checkData[field] = b[field] ? b[field] : 0;
  1022. }
  1023. for (const p of pr) {
  1024. for (const field of fields) {
  1025. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  1026. }
  1027. }
  1028. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  1029. this.checkResult.error.push({
  1030. ledger_id: b.ledger_id,
  1031. b_code: b.b_code,
  1032. name: b.name,
  1033. errorType: 'qty',
  1034. error: { checkData, calcData },
  1035. });
  1036. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1037. this.checkResult.source.bills.push(b);
  1038. for (const p of pr) {
  1039. this.checkResult.source.pos.push(p);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. checkBillsTp(field, decimal, filter) {
  1046. for (const b of this.checkBills.nodes) {
  1047. if ((b.children && b.children.length > 0)) continue;
  1048. if (filter && filter(b)) continue;
  1049. const checkData = {}, calcData = {};
  1050. for (const f of field) {
  1051. checkData[f.tp] = b[f.tp] || 0;
  1052. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  1053. }
  1054. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  1055. this.checkResult.error.push({
  1056. ledger_id: b.ledger_id,
  1057. b_code: b.b_code,
  1058. name: b.name,
  1059. errorType: 'tp',
  1060. error: { checkData, calcData },
  1061. });
  1062. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1063. this.checkResult.source.bills.push(b);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. _checkPosOverRangeTz(p, coe) {
  1069. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  1070. const base_qty = p.quantity;
  1071. const compare_qty = this.ctx.helper.mul(p.final_1_qty, coe);
  1072. if (!base_qty) return !!end_contract_qty;
  1073. return base_qty > 0
  1074. ? end_contract_qty > compare_qty
  1075. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1076. }
  1077. _checkPosOverRange(p, checkInfo) {
  1078. const checkTz = checkInfo.checkTz ? this._checkPosOverRangeTz(p, checkInfo.coe) : false;
  1079. const checkDeal = false;
  1080. return checkTz || checkDeal;
  1081. }
  1082. _checkBillsOverRangeTz(bills, coe) {
  1083. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  1084. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  1085. if (bills.is_tp) {
  1086. const base_tp = bills.total_price;
  1087. const compare_tp = this.ctx.helper.mul(base_tp, coe);
  1088. if (!base_tp) return !!end_contract_tp;
  1089. return base_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  1090. } else {
  1091. const base_qty = bills.quantity;
  1092. const compare_qty = this.ctx.helper.mul(bills.final_1_qty, coe);
  1093. if (!base_qty) return !!end_contract_qty;
  1094. return base_qty > 0
  1095. ? end_contract_qty > compare_qty
  1096. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1097. }
  1098. }
  1099. _checkBillsOverRangeDeal(bills, coe) {
  1100. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  1101. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  1102. if (bills.is_tp) {
  1103. const base_tp = bills.deal_tp;
  1104. const compare_tp = this.ctx.helper.mul(base_tp, coe);
  1105. if (!base_tp) return !!end_contract_tp;
  1106. return base_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  1107. } else {
  1108. const base_qty = bills.deal_qty;
  1109. const compare_qty = this.ctx.helper.mul(bills.deal_final_1_qty, coe);
  1110. if (!base_qty) return !!end_contract_qty;
  1111. return base_qty > 0
  1112. ? end_contract_qty > compare_qty
  1113. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1114. }
  1115. }
  1116. _checkBillsOverRange(bills, posRange, checkInfo) {
  1117. if (checkInfo.hasPosCheckPos && posRange.length > 0) {
  1118. for (const p of posRange) {
  1119. if (this._checkPosOverRange(p, checkInfo)) return true;
  1120. }
  1121. }
  1122. if (checkInfo.hasPosCheckBills || posRange.length === 0) {
  1123. const checkTz = checkInfo.checkTz ? this._checkBillsOverRangeTz(bills, checkInfo.coe) : false;
  1124. const checkDeal = checkInfo.checkDeal ? this._checkBillsOverRangeDeal(bills, checkInfo.coe) : false;
  1125. return checkTz || checkDeal;
  1126. }
  1127. return false;
  1128. }
  1129. checkOverRange(checkInfo) {
  1130. for (const b of this.checkBills.nodes) {
  1131. if (b.children && b.children.length > 0) continue;
  1132. const pr = this.checkPos.getLedgerPos(b.id) || [];
  1133. if (this._checkBillsOverRange(b, pr, checkInfo)) {
  1134. this.checkResult.error.push({
  1135. ledger_id: b.ledger_id,
  1136. b_code: b.b_code,
  1137. name: b.name,
  1138. errorType: 'over',
  1139. });
  1140. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1141. this.checkResult.source.bills.push(b);
  1142. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  1143. }
  1144. }
  1145. }
  1146. }
  1147. checkMinusChangeBills(change, changeBills, finalStageChange) {
  1148. const error = this.checkResult.error;
  1149. const helper = this.ctx.helper;
  1150. const changeIndex = {};
  1151. change.forEach(c => {
  1152. changeIndex[c.cid] = c;
  1153. c.bills = [];
  1154. c.billsIndex = {};
  1155. c.stageChange = [];
  1156. });
  1157. changeBills.forEach(cb => {
  1158. const c = changeIndex[cb.cid];
  1159. if (c) c.bills.push(cb);
  1160. c.billsIndex[cb.id] = cb;
  1161. cb.used_qty = 0;
  1162. cb.qty = parseFloat(cb.samount);
  1163. });
  1164. finalStageChange.forEach(sc => {
  1165. if (!sc.qty) return;
  1166. const c = changeIndex[sc.cid];
  1167. if (c) {
  1168. c.used = true;
  1169. const cb = c.billsIndex[sc.cbid];
  1170. if (cb) cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1171. }
  1172. });
  1173. change.forEach(c => {
  1174. if (!c.used) return;
  1175. c.bills.forEach(b => {
  1176. if (b.qty >= 0) return;
  1177. if (!helper.numEqual(b.used_qty, b.qty)) error.push({ b_code: b.code, name: b.name, errorType: 'minus_cb', memo: c.code });
  1178. });
  1179. });
  1180. }
  1181. checkChangeBillsOver(change, changeBills, finalStageChange, curStageId) {
  1182. const error = this.checkResult.error;
  1183. const helper = this.ctx.helper;
  1184. const changeIndex = {};
  1185. change.forEach(c => {
  1186. changeIndex[c.cid] = c;
  1187. c.bills = [];
  1188. c.billsIndex = {};
  1189. c.stageChange = [];
  1190. });
  1191. changeBills.forEach(cb => {
  1192. const c = changeIndex[cb.cid];
  1193. if (c) c.bills.push(cb);
  1194. c.billsIndex[cb.id] = cb;
  1195. cb.used_qty = 0;
  1196. cb.qty = parseFloat(cb.samount);
  1197. });
  1198. finalStageChange.forEach(sc => {
  1199. if (!sc.qty) return;
  1200. const c = changeIndex[sc.cid];
  1201. if (c) {
  1202. c.used = true;
  1203. const cb = c.billsIndex[sc.cbid];
  1204. if (cb) {
  1205. cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1206. if (sc.sid === curStageId) {
  1207. cb.cur_used = true;
  1208. cb.lid = sc.lid;
  1209. }
  1210. }
  1211. }
  1212. });
  1213. change.forEach(c => {
  1214. if (!c.used) return;
  1215. c.bills.forEach(b => {
  1216. if (!b.cur_used) return;
  1217. const qtyDecimal = helper.findDecimal(b.unit);
  1218. const limitQty = helper.mul(b.qty, helper.div(b.delimit, 100, 2), qtyDecimal);
  1219. 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 });
  1220. });
  1221. });
  1222. }
  1223. checkSettle() {
  1224. const settleStatus = this.ctx.service.settle.settleStatus;
  1225. for (const b of this.checkBills.nodes) {
  1226. if (b.children && b.children.length > 0) continue;
  1227. if (!b.settleStatus) continue;
  1228. const pr = this.checkPos.getLedgerPos(b.id);
  1229. if (!pr || pr.length === 0) {
  1230. if (b.settleStatus !== settleStatus.finish) continue;
  1231. if (b.contract_qty || b.contract_tp || b.qc_qty || b.qc_minus_qty || b.positive_qc_qty || b.negative_qc_qty) {
  1232. this.checkResult.error.push({
  1233. ledger_id: b.ledger_id,
  1234. b_code: b.b_code,
  1235. name: b.name,
  1236. errorType: 'settle',
  1237. });
  1238. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1239. this.checkResult.source.bills.push(b);
  1240. }
  1241. }
  1242. } else {
  1243. for (const p of pr) {
  1244. if (p.settle_status !== settleStatus.finish) continue;
  1245. if (p.contract_qty || p.qc_qty || p.qc_minus_qty || p.positive_qc_qty || p.negative_qc_qty) {
  1246. this.checkResult.error.push({
  1247. ledger_id: b.ledger_id,
  1248. b_code: b.b_code,
  1249. name: b.name,
  1250. errorType: 'settle',
  1251. });
  1252. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1253. this.checkResult.source.bills.push(b);
  1254. for (const p of pr) {
  1255. this.checkResult.source.pos.push(p);
  1256. }
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. class reviseTree extends billsTree {
  1265. constructor (ctx, setting) {
  1266. super(ctx, setting);
  1267. this.price = [];
  1268. }
  1269. loadRevisePrice(price, decimal) {
  1270. this.decimal = decimal;
  1271. this.price = price || [];
  1272. this.rela_price = [];
  1273. this.common_price = [];
  1274. this.price.forEach(x => {
  1275. if (x.rela_lid) {
  1276. x.rela_lid = x.rela_lid.split(',');
  1277. this.rela_price.push(x);
  1278. } else {
  1279. this.common_price.push(x);
  1280. }
  1281. });
  1282. }
  1283. checkRevisePrice(d) {
  1284. if (d.settle_status) return false;
  1285. const helper = this.ctx.helper;
  1286. const setting = this.setting;
  1287. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1288. const checkRela = function(rela_lid) {
  1289. if (!rela_lid || rela_lid.length === 0) return false;
  1290. for (const lid of rela_lid) {
  1291. if (pid.indexOf(lid) >= 0) return true;
  1292. }
  1293. return false;
  1294. };
  1295. let p = this.rela_price.find(x => {
  1296. return x.b_code === d.b_code &&
  1297. ((!x.name && !d.name) || x.name === d.name) &&
  1298. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1299. helper.checkZero(x.org_price - d.unit_price) &&
  1300. checkRela(x.rela_lid);
  1301. });
  1302. if (!p) p = this.common_price.find(x => {
  1303. return x.b_code === d.b_code &&
  1304. ((!x.name && !d.name) || x.name === d.name) &&
  1305. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1306. helper.checkZero(x.org_price - d.unit_price);
  1307. });
  1308. if (!p) return false;
  1309. d.org_price = p.org_price;
  1310. d.unit_price = p.new_price;
  1311. d.deal_tp = helper.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1312. d.sgfh_tp = helper.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1313. d.sjcl_tp = helper.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1314. d.qtcl_tp = helper.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1315. d.total_price = helper.mul(d.quantity, d.unit_price, this.decimal.tp);
  1316. d.ex_tp1 = helper.mul(d.ex_qty1, d.unit_price, this.decimal.tp);
  1317. return true;
  1318. }
  1319. loadDatas(datas) {
  1320. super.loadDatas(datas);
  1321. if (this.price.length > 0) {
  1322. for (const d of this.datas) {
  1323. if (d.children && d.children.length > 0) continue;
  1324. if (!d.b_code) continue;
  1325. this.checkRevisePrice(d);
  1326. }
  1327. }
  1328. }
  1329. getUpdateReviseData() {
  1330. return this.datas.map(x => {
  1331. if (x.children && x.children.length > 0) {
  1332. return {
  1333. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1334. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 0,
  1335. node_type: x.node_type, check_calc: x.check_calc,
  1336. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1337. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1338. unit_price: 0, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1339. quantity: 0, total_price: 0,
  1340. sgfh_qty: 0, sgfh_tp: 0, sgfh_expr: '',
  1341. sjcl_qty: 0, sjcl_tp: 0, sjcl_expr: '',
  1342. qtcl_qty: 0, qtcl_tp: 0, qtcl_expr: '',
  1343. deal_qty: 0, deal_tp: 0,
  1344. ex_qty1: 0, ex_tp1: 0,
  1345. };
  1346. } else {
  1347. return {
  1348. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1349. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 1,
  1350. node_type: x.node_type, check_calc: x.check_calc,
  1351. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1352. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1353. unit_price: x.unit_price, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1354. quantity: x.quantity, total_price: x.total_price,
  1355. sgfh_qty: x.sgfh_qty, sgfh_tp: x.sgfh_tp, sgfh_expr: x.sgfh_expr,
  1356. sjcl_qty: x.sjcl_qty, sjcl_tp: x.sjcl_tp, sjcl_expr: x.sjcl_expr,
  1357. qtcl_qty: x.qtcl_qty, qtcl_tp: x.qtcl_tp, qtcl_expr: x.qtcl_expr,
  1358. deal_qty: x.deal_qty, deal_tp: x.deal_tp,
  1359. ex_qty1: x.ex_qty1, ex_tp1: x.ex_tp1,
  1360. };
  1361. }
  1362. });
  1363. }
  1364. sum() {
  1365. const result = { total_price: 0 };
  1366. for (const d of this.datas) {
  1367. if (d.children && d.children.length > 0) continue;
  1368. result.total_price = this.ctx.helper.add(result.total_price, d.total_price);
  1369. }
  1370. return result;
  1371. }
  1372. }
  1373. module.exports = {
  1374. baseTree,
  1375. billsTree,
  1376. pos,
  1377. filterTree,
  1378. filterGatherTree,
  1379. gatherTree,
  1380. gatherPos,
  1381. checkData,
  1382. reviseTree,
  1383. };