ledger.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 ? 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. checkUsed(node, fields) {
  421. if (!node) return false;
  422. const checkNodes = node.children && node.children.length > 0 ? this.getPosterity(node) : [node];
  423. for (const cn of checkNodes) {
  424. if (cn.children && cn.children.length > 0) continue;
  425. for (const f of fields) {
  426. if (cn[f]) return true;
  427. }
  428. }
  429. return false;
  430. }
  431. checkNodesUsed(nodes, fields) {
  432. if (!nodes || nodes.length === 0) return false;
  433. for (const n of nodes) {
  434. if (this.checkUsed(n, fields)) return true;
  435. }
  436. return false;
  437. }
  438. }
  439. class filterTree extends baseTree {
  440. addData(data, fields) {
  441. const item = {};
  442. for (const prop in data) {
  443. if (fields.indexOf(prop) >= 0) {
  444. item[prop] = data[prop];
  445. }
  446. }
  447. const keyName = itemsPre + item[this.setting.id];
  448. if (!this.items[keyName]) {
  449. item.children = [];
  450. item.is_leaf = true;
  451. item.expanded = true;
  452. item.visible = true;
  453. this.items[keyName] = item;
  454. this.datas.push(item);
  455. if (item[this.setting.pid] === this.setting.rootId) {
  456. this.children.push(item);
  457. } else {
  458. const parent = this.getParent(item);
  459. if (parent) {
  460. parent.is_leaf = false;
  461. parent.children.push(item);
  462. }
  463. }
  464. } else {
  465. return this.items[keyName];
  466. }
  467. return item;
  468. }
  469. }
  470. class filterGatherTree extends baseTree {
  471. clearDatas() {
  472. this.items = {};
  473. this.nodes = [];
  474. this.datas = [];
  475. this.children = [];
  476. }
  477. get newId() {
  478. if (!this._maxId) {
  479. this._maxId = 0;
  480. }
  481. this._maxId++;
  482. return this._maxId;
  483. }
  484. addNode(data, parent, checkSame = true) {
  485. data[this.setting.pid] = parent ? parent[this.setting.id] : this.setting.rootId;
  486. let item = checkSame ? this.ctx.helper._.find(parent ? parent.children : this.children, data) : null;
  487. if (item) return item;
  488. item = data;
  489. item.drawing_code = [];
  490. item.memo = [];
  491. item.ex_memo1 = [];
  492. item.ex_memo2 = [];
  493. item.ex_memo3 = [];
  494. item.postil = [];
  495. item[this.setting.id] = this.newId;
  496. const keyName = itemsPre + item[this.setting.id];
  497. item.children = [];
  498. item.is_leaf = true;
  499. item.expanded = true;
  500. item.visible = true;
  501. this.items[keyName] = item;
  502. this.datas.push(item);
  503. if (parent) {
  504. item[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + item[this.setting.id];
  505. item[this.setting.level] = parent[this.setting.level] + 1;
  506. item[this.setting.order] = parent.children.length + 1;
  507. parent.is_leaf = false;
  508. parent.children.push(item);
  509. } else {
  510. item[this.setting.fullPath] = '' + item[this.setting.id];
  511. item[this.setting.level] = 1;
  512. item[this.setting.order] = this.children.length + 1;
  513. this.children.push(item);
  514. }
  515. return item;
  516. }
  517. generateSortNodes() {
  518. const self = this;
  519. const addSortNode = function (node) {
  520. self.nodes.push(node);
  521. for (const c of node.children) {
  522. addSortNode(c);
  523. }
  524. };
  525. this.nodes = [];
  526. for (const n of this.children) {
  527. addSortNode(n);
  528. }
  529. }
  530. sortTreeNodeCustom(fun) {
  531. const sortNodes = function (nodes) {
  532. nodes.sort(fun);
  533. for (const [i, node] of nodes.entries()) {
  534. node.order = i + 1;
  535. }
  536. for (const node of nodes) {
  537. if (node.children && node.children.length > 1) {
  538. sortNodes(node.children);
  539. }
  540. }
  541. };
  542. this.nodes = [];
  543. this.children = this.getChildren(null);
  544. sortNodes(this.children);
  545. this.generateSortNodes();
  546. }
  547. }
  548. class gatherTree extends baseTree {
  549. constructor(ctx, setting) {
  550. super(ctx, setting);
  551. this._newId = 1;
  552. }
  553. get newId() {
  554. return this._newId++;
  555. }
  556. loadGatherNode(node, parent, loadFun, loadPosFun) {
  557. const siblings = parent ? parent.children : this.children;
  558. let cur = siblings.find(function (x) {
  559. return node.b_code
  560. ? x.b_code === node.b_code && x.name === node.name && x.unit === node.unit && x.unit_price === node.unit_price
  561. : x.code === node.code && x.name === node.name;
  562. });
  563. if (!cur) {
  564. const id = this.newId;
  565. cur = {
  566. id: id,
  567. pid: parent ? parent.id : this.setting.rootId,
  568. full_path: parent ? parent.full_path + '-' + id : '' + id,
  569. level: parent ? parent.level + 1 : 1,
  570. order: siblings.length + 1,
  571. children: [],
  572. code: node.code, b_code: node.b_code, name: node.name,
  573. unit: node.unit, unit_price: node.unit_price, drawing_code: node.drawing_code,
  574. };
  575. siblings.push(cur);
  576. this.datas.push(cur);
  577. }
  578. if (!cur.memo && !!node.memo) cur.memo = node.memo;
  579. if (!cur.ex_memo1 && !!node.ex_memo1) cur.ex_memo1 = node.ex_memo1;
  580. if (!cur.ex_memo2 && !!node.ex_memo2) cur.ex_memo2 = node.ex_memo2;
  581. if (!cur.ex_memo3 && !!node.ex_memo3) cur.ex_memo3 = node.ex_memo3;
  582. loadFun(cur, node);
  583. if (node.children && node.children.length > 0) {
  584. for (const c of node.children) {
  585. this.loadGatherNode(c, cur, loadFun, loadPosFun);
  586. }
  587. } else if (loadPosFun) {
  588. loadPosFun(cur, node);
  589. }
  590. }
  591. generateSortNodes() {
  592. const self = this;
  593. const addSortNode = function (node) {
  594. self.nodes.push(node);
  595. for (const c of node.children) {
  596. addSortNode(c);
  597. }
  598. };
  599. this.nodes = [];
  600. for (const n of this.children) {
  601. addSortNode(n);
  602. }
  603. }
  604. loadGatherTree(sourceTree, loadFun, loadPosFun) {
  605. for (const c of sourceTree.children) {
  606. this.loadGatherNode(c, null, loadFun, loadPosFun);
  607. }
  608. }
  609. resortChildrenByCustom(fun) {
  610. for (const n of this.datas) {
  611. if (n.children && n.children.length > 1) {
  612. n.children.sort(fun);
  613. n.children.forEach((x, i) => { x.order = i + 1; });
  614. }
  615. }
  616. this.generateSortNodes();
  617. }
  618. resortChildrenDefault() {
  619. const helper = this.ctx.helper;
  620. this.resortChildrenByCustom((x, y) => {
  621. const iCode = (x.code || y.code) ? helper.compareCode(x.code, y.code) : helper.compareCode(x.b_code, y.b_code);
  622. if (iCode) return iCode;
  623. if (!x.name) return -1;
  624. if (!y.name) return 1;
  625. return x.name.localeCompare(y.name);
  626. })
  627. }
  628. calculateSum() {
  629. if (this.setting.calcSum) {
  630. for (const d of this.datas) {
  631. this.setting.calcSum(d, this.count);
  632. }
  633. }
  634. }
  635. }
  636. class pos {
  637. /**
  638. * 构造函数
  639. * @param {id|Number, masterId|Number} setting
  640. */
  641. constructor (setting) {
  642. // 无索引
  643. this.datas = [];
  644. // 以key为索引
  645. this.items = {};
  646. // 以分类id为索引的有序
  647. this.ledgerPos = {};
  648. // pos设置
  649. this.setting = setting;
  650. }
  651. /**
  652. * 加载部位明细数据
  653. * @param datas
  654. */
  655. loadDatas(datas) {
  656. this.datas = datas;
  657. this.items = {};
  658. this.ledgerPos = {};
  659. for (const data of this.datas) {
  660. const key = itemsPre + data[this.setting.id];
  661. this.items[key] = data;
  662. const masterKey = itemsPre + data[this.setting.ledgerId];
  663. if (!this.ledgerPos[masterKey]) {
  664. this.ledgerPos[masterKey] = [];
  665. }
  666. this.ledgerPos[masterKey].push(data);
  667. }
  668. for (const prop in this.ledgerPos) {
  669. this.resortLedgerPos(this.ledgerPos[prop]);
  670. }
  671. }
  672. getPos(id) {
  673. return this.items[itemsPre + id];
  674. }
  675. getLedgerPosKey() {
  676. const result = [];
  677. for (const prop in this.ledgerPos) {
  678. result.push(prop);
  679. }
  680. return result;
  681. }
  682. getLedgerPos(mid) {
  683. return this.ledgerPos[itemsPre + mid];
  684. }
  685. resortLedgerPos(ledgerPos) {
  686. const orderField = this.setting.orderField || 'porder';
  687. if (ledgerPos instanceof Array) {
  688. ledgerPos.sort(function (a, b) {
  689. return a[orderField] - b[orderField];
  690. })
  691. }
  692. }
  693. /**
  694. * 计算全部
  695. */
  696. calculateAll(fun) {
  697. const calcFun = fun ? fun : this.setting.calc;
  698. if (!calcFun) return;
  699. for (const pos of this.datas) {
  700. calcFun(pos);
  701. }
  702. }
  703. getDatas () {
  704. return this.datas;
  705. }
  706. }
  707. class gatherPos extends pos {
  708. loadGatherPos(ledgerId, sourcePosRange, loadFun) {
  709. let posRange = this.getLedgerPos(itemsPre + ledgerId);
  710. if (!posRange) {
  711. posRange = [];
  712. this.ledgerPos[itemsPre + ledgerId] = posRange;
  713. }
  714. for (const spr of sourcePosRange) {
  715. let gp = posRange.find(x => { return x.name === spr.name; });
  716. if (!gp) {
  717. gp = { name: spr.name };
  718. gp[this.setting.ledgerId] = ledgerId;
  719. this.datas.push(gp);
  720. posRange.push(gp);
  721. }
  722. if (!gp.ex_memo1 && !!spr.ex_memo1) gp.ex_memo1 = spr.ex_memo1;
  723. if (!gp.ex_memo2 && !!spr.ex_memo2) gp.ex_memo2 = spr.ex_memo2;
  724. if (!gp.ex_memo3 && !!spr.ex_memo3) gp.ex_memo3 = spr.ex_memo3;
  725. loadFun(gp, spr);
  726. }
  727. }
  728. }
  729. class checkData {
  730. constructor(ctx, measureType) {
  731. this.ctx = ctx;
  732. this.checkBills = new billsTree(ctx, { id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1 });
  733. this.checkPos = new pos({ id: 'id', ledgerId: 'lid' });
  734. this.checkResult = {
  735. error: [],
  736. source: {
  737. bills: [],
  738. pos: [],
  739. },
  740. };
  741. this.measureType = measureType;
  742. }
  743. _check3f(data, limit, ratio) {
  744. if (limit === 0) {
  745. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  746. }
  747. if (limit === 1) {
  748. if (ratio === 0) {
  749. if (!data.contract_tp && !data.pre_contract_tp) return 2; // 漏计
  750. } else {
  751. const tp = this.ctx.helper.mul(data.final_1_tp, this.ctx.helper.div(ratio, 100, 4), this.ctx.tender.info.decimal.tp);
  752. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp);
  753. if (tp > checkTp) return 1; // 违规
  754. if (tp < checkTp) return 2; // 漏计
  755. }
  756. }
  757. return 0; // 合法
  758. }
  759. _check3fQty(data, limit, ratio, unit) {
  760. if (limit === 0) {
  761. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  762. }
  763. if (limit === 1) {
  764. if (!ratio || ratio === 0) {
  765. if (!data.contract_qty && !data.qc_qty && !data.pre_contract_qty && !data.pre_qc_qty) return 2; // 漏计
  766. } else {
  767. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  768. const checkQty = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(ratio, 100, 4), precision.value);
  769. const qty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty);
  770. if (qty > checkQty) return 1; // 违规
  771. if (qty < checkQty) return 2; // 漏计
  772. }
  773. }
  774. return 0; // 合法
  775. }
  776. _getRatio(type, status) {
  777. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  778. const sc = statusConst.find(x => { return x.value === status });
  779. return sc ? sc.ratio : null;
  780. }
  781. _getValid = function (type, status, limit) {
  782. if (limit) {
  783. const statusConst = type === 'gxby' ? this.ctx.session.sessionProject.gxby_status : this.ctx.session.sessionProject.dagl_status;
  784. const sc = statusConst.find(x => { return x.value === status; });
  785. return sc ? (sc.limit ? 1 : 0) : 0;
  786. } else {
  787. return -1;
  788. }
  789. };
  790. _checkLeafBills3fLimit(checkType, bills, checkInfo) {
  791. const over = [], lost = [];
  792. const posRange = this.checkPos.getLedgerPos(bills.id);
  793. if (posRange && posRange.length > 0) {
  794. for (const p of posRange) {
  795. const posCheckInfo = this.ctx.helper._.assign({}, checkInfo);
  796. for (const ct of checkType) {
  797. if (p[ct + '_limit'] > 0) {
  798. posCheckInfo[ct + '_limit'] = p[ct + '_limit'];
  799. }
  800. }
  801. for (const ct of checkType) {
  802. const checkResult = this._check3fQty(p, this._getValid(ct, p[ct + '_status'], posCheckInfo[ct + '_limit']), this._getRatio(ct, p[ct+'_status']), bills.unit);
  803. if (checkResult === 1) {
  804. if (over.indexOf(ct) === -1) over.push(ct);
  805. }
  806. if (checkResult === 2) {
  807. if (lost.indexOf(ct) === -1) lost.push(ct);
  808. }
  809. }
  810. }
  811. } else {
  812. for (const ct of checkType) {
  813. const checkResult = bills.is_tp
  814. ? this._check3f(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']))
  815. : this._check3fQty(bills, this._getValid(ct, bills[ct + '_status'], checkInfo[ct + '_limit']), this._getRatio(ct, bills[ct+'_status']), bills.unit);
  816. if (checkResult === 1) {
  817. if (over.indexOf(ct) === -1) over.push(ct);
  818. }
  819. if (checkResult === 2) {
  820. if (lost.indexOf(ct) === -1) lost.push(ct);
  821. }
  822. }
  823. }
  824. if (over.length + lost.length > 0) {
  825. for (const o of over) {
  826. this.checkResult.error.push({
  827. ledger_id: bills.ledger_id,
  828. b_code: bills.b_code,
  829. name: bills.name,
  830. errorType: 's2b_over_' + o,
  831. });
  832. }
  833. for (const l of lost) {
  834. this.checkResult.error.push({
  835. ledger_id: bills.ledger_id,
  836. b_code: bills.b_code,
  837. name: bills.name,
  838. errorType: 's2b_lost_' + l,
  839. });
  840. }
  841. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === bills.ledger_id})) {
  842. this.checkResult.source.bills.push(bills);
  843. if (posRange && posRange.length > 0) this.checkResult.source.pos.push(...posRange);
  844. }
  845. }
  846. }
  847. _recursiveCheckBills3fLimit(checkType, bills, parentCheckInfo) {
  848. const checkInfo = this.ctx.helper._.assign({}, parentCheckInfo);
  849. for (const ct of checkType) {
  850. if (bills[ct + '_limit'] > 0) {
  851. checkInfo[ct + '_limit'] = bills[ct + '_limit'];
  852. }
  853. }
  854. if (bills.children && bills.children.length > 0) {
  855. for (const c of bills.children) {
  856. this._recursiveCheckBills3fLimit(checkType, c, checkInfo);
  857. }
  858. } else {
  859. this._checkLeafBills3fLimit(checkType, bills, checkInfo);
  860. }
  861. }
  862. _checkMultiCondition(status, check, condition) {
  863. let result = true;
  864. for (const c of condition) {
  865. const data = !c.source || c.source === '3f' ? status : check;
  866. result = c.rela && c.rela === 'or'
  867. ? (result || this.valueCheck[c.check](data[c.field], c.value, c.operation))
  868. : (result && this.valueCheck[c.check](data[c.field], c.value, c.operation));
  869. }
  870. return result;
  871. }
  872. _check3fMultiTp(data, limit, range) {
  873. if (limit === 0) {
  874. if (data.contract_tp || data.pre_contract_tp) return 1; // 违规
  875. }
  876. if (limit === 1) {
  877. const lower = this.ctx.helper.mul(data.total_price, this.ctx.helper.div(range.lower, 100, 4), this.ctx.tender.info.decimal.tp);
  878. const upper = this.ctx.helper.mul(data.total_price, this.ctx.helper.div(range.upper, 100, 4), this.ctx.tender.info.decimal.tp);
  879. const checkTp = this.ctx.helper.add(data.contract_tp, data.pre_contract_tp) || 0;
  880. if (checkTp > upper) return 1; // 违规
  881. if (checkTp < lower) return 2; // 漏计
  882. }
  883. return 0; // 合法
  884. }
  885. _check3fMultiQty(data, limit, range, unit) {
  886. if (limit === 0) {
  887. if (data.contract_qty || data.qc_qty || data.pre_contract_qty || data.pre_qc_qty) return 1; // 违规
  888. }
  889. if (limit === 1) {
  890. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, unit);
  891. const lower = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(range.lower, 100, 4), precision.value);
  892. const upper = this.ctx.helper.mul(data.final_1_qty, this.ctx.helper.div(range.upper, 100, 4), precision.value);
  893. const checkQty = this.ctx.helper.add(data.contract_qty, data.pre_contract_qty) || 0;
  894. if (checkQty > upper) return 1; // 违规
  895. if (checkQty < lower) return 2; // 漏计
  896. }
  897. return 0; // 合法
  898. }
  899. _checkMulti3f(bills, checkData, statusData, limitOption){
  900. if (!this._checkMultiCondition(statusData, checkData, limitOption.condition)) return;
  901. if (bills.is_tp) return this._check3fMultiTp(checkData, limitOption.limit, limitOption);
  902. return this._check3fMultiQty(checkData, limitOption.limit, limitOption, bills.unit);
  903. }
  904. _getMulti3fErrorInfo(bills, lo, checkResult, pos = null) {
  905. if (!checkResult || checkResult <= 0) return;
  906. const errorType = checkResult === 1 ? '超计' : '漏计';
  907. if (!lo.rangeHint) lo.rangeHint = lo.limit ? (lo.lower === lo.upper ? `${lo.lower}%` : `${lo.lower}%~${lo.upper}%`) : '不允许计量';
  908. if (pos) {
  909. this.checkResult.error.push({
  910. 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})`
  911. });
  912. } else {
  913. this.checkResult.error.push({
  914. 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})`
  915. });
  916. }
  917. }
  918. _checkLeafBills3fMultiLimit(bills, multiLimit, leafXmj) {
  919. if (!multiLimit) return;
  920. const limitOption = this.limits.find(x => { return x.limit_id === multiLimit; });
  921. if (!limitOption) return;
  922. const posRange = this.checkPos.getLedgerPos(bills.id);
  923. if (posRange && posRange.length > 0) {
  924. for (const p of posRange) {
  925. for (const lo of limitOption.options) {
  926. const checkResult = this._checkMulti3f(bills, p, p, lo);
  927. this._getMulti3fErrorInfo(bills, lo, checkResult, p);
  928. }
  929. }
  930. } else {
  931. for (const lo of limitOption.options) {
  932. const checkResult = this._checkMulti3f(bills, bills, leafXmj, lo);
  933. this._getMulti3fErrorInfo(bills, lo, checkResult);
  934. }
  935. }
  936. }
  937. _recursiveCheckBills3fMultiLimit(bills, parentLimit = '', leafXmj = null) {
  938. const limit = bills.multi_limit || parentLimit;
  939. if (bills.children && bills.children.length > 0) {
  940. for (const c of bills.children) {
  941. this._recursiveCheckBills3fMultiLimit(c, limit, c.b_code ? (bills.b_code ? leafXmj : bills) : c);
  942. }
  943. } else {
  944. this._checkLeafBills3fMultiLimit(bills, limit, bills.b_code ? leafXmj : bills);
  945. }
  946. }
  947. loadData(bills, pos) {
  948. this.checkBills.loadDatas(bills);
  949. this.checkPos.loadDatas(pos);
  950. }
  951. checkSibling() {
  952. for (const node of this.checkBills.nodes) {
  953. if (!node.children || node.children.length === 0) continue;
  954. let hasXmj, hasGcl;
  955. for (const child of node.children) {
  956. if (child.b_code) hasXmj = true;
  957. if (!child.b_code) hasGcl = true;
  958. }
  959. if (hasXmj && hasGcl) this.checkResult.error.push({
  960. ledger_id: node.ledger_id,
  961. b_code: node.b_code,
  962. name: node.name,
  963. errorType: 'sibling',
  964. });
  965. }
  966. }
  967. checkSameCode() {
  968. //let xmj = this.checkBills.nodes.filter(x => { return /^((GD*)|G)?[0-9]+/.test(x.code); });
  969. let xmj = [];
  970. const addXmjCheck = function (node) {
  971. if (/^((GD*)|G)?[0-9]+/.test(node.code)) xmj.push(node);
  972. for (const child of node.children) {
  973. addXmjCheck(child);
  974. }
  975. };
  976. for (const topLevel of this.checkBills.children) {
  977. if ([1, 2, 3, 4].indexOf(topLevel.node_type) < 0) continue;
  978. addXmjCheck(topLevel);
  979. }
  980. const xmjPart = {}, xmjIndex = [];
  981. for (const x of xmj) {
  982. if (!xmjPart[x.code]) {
  983. xmjPart[x.code] = [];
  984. xmjIndex.push(x.code);
  985. }
  986. xmjPart[x.code].push(x);
  987. }
  988. for (const x of xmjIndex) {
  989. if (xmjPart[x].length <= 1) continue;
  990. for (const xp of xmjPart[x]) {
  991. this.checkResult.error.push({
  992. ledger_id: xp.ledger_id,
  993. b_code: xp.b_code,
  994. name: xp.name,
  995. errorType: 'same_code',
  996. })
  997. }
  998. }
  999. let check = null;
  1000. while (xmj.length > 0) {
  1001. [check, xmj] = this.ctx.helper._.partition(xmj, x => { return x.code === xmj[0].code; });
  1002. if (check.length > 1) {
  1003. for (const c of check) {
  1004. this.checkResult.error.push({
  1005. ledger_id: c.ledger_id,
  1006. b_code: c.b_code,
  1007. name: c.name,
  1008. errorType: 'same_code',
  1009. })
  1010. }
  1011. }
  1012. }
  1013. }
  1014. check3fLimit(tender) {
  1015. const check = [];
  1016. if (tender.s2b_gxby_limit) check.push('gxby');
  1017. if (tender.s2b_dagl_limit) check.push('dagl');
  1018. if (check.length === 0) return;
  1019. for (const b of this.checkBills.children) {
  1020. this._recursiveCheckBills3fLimit(check, b, {});
  1021. }
  1022. }
  1023. check3fMultiLimit(tender, limits) {
  1024. this.limits = limits;
  1025. if (!tender.s2b_multi_limit) return;
  1026. this.valueCheck = new ValueCheck(this.ctx);
  1027. for (const b of this.checkBills.children) {
  1028. this._recursiveCheckBills3fMultiLimit(b);
  1029. }
  1030. }
  1031. checkBillsQty(fields) {
  1032. for (const b of this.checkBills.nodes) {
  1033. if (b.children && b.children.length > 0) continue;
  1034. const pr = this.checkPos.getLedgerPos(b.id);
  1035. if (!pr || pr.length === 0) continue;
  1036. const checkData = {},
  1037. calcData = {};
  1038. for (const field of fields) {
  1039. checkData[field] = b[field] ? b[field] : 0;
  1040. }
  1041. for (const p of pr) {
  1042. for (const field of fields) {
  1043. calcData[field] = this.ctx.helper.add(calcData[field], p[field]);
  1044. }
  1045. }
  1046. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  1047. this.checkResult.error.push({
  1048. ledger_id: b.ledger_id,
  1049. b_code: b.b_code,
  1050. name: b.name,
  1051. errorType: 'qty',
  1052. error: { checkData, calcData },
  1053. });
  1054. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1055. this.checkResult.source.bills.push(b);
  1056. for (const p of pr) {
  1057. this.checkResult.source.pos.push(p);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. checkBillsTp(field, decimal, filter) {
  1064. for (const b of this.checkBills.nodes) {
  1065. if ((b.children && b.children.length > 0)) continue;
  1066. if (filter && filter(b)) continue;
  1067. const checkData = {}, calcData = {};
  1068. for (const f of field) {
  1069. checkData[f.tp] = b[f.tp] || 0;
  1070. calcData[f.tp] = this.ctx.helper.mul(b.unit_price, b[f.qty], decimal.tp) || 0;
  1071. }
  1072. if (!this.ctx.helper._.isMatch(checkData, calcData)) {
  1073. this.checkResult.error.push({
  1074. ledger_id: b.ledger_id,
  1075. b_code: b.b_code,
  1076. name: b.name,
  1077. errorType: 'tp',
  1078. error: { checkData, calcData },
  1079. });
  1080. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1081. this.checkResult.source.bills.push(b);
  1082. }
  1083. }
  1084. }
  1085. }
  1086. _checkPosOverRangeTz(p, coe) {
  1087. const end_contract_qty = this.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  1088. const base_qty = p.quantity;
  1089. const compare_qty = this.ctx.helper.mul(p.final_1_qty, coe);
  1090. if (!base_qty) return !!end_contract_qty;
  1091. return base_qty > 0
  1092. ? end_contract_qty > compare_qty
  1093. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1094. }
  1095. _checkPosOverRange(p, checkInfo) {
  1096. const checkTz = checkInfo.checkTz ? this._checkPosOverRangeTz(p, checkInfo.coe) : false;
  1097. const checkDeal = false;
  1098. return checkTz || checkDeal;
  1099. }
  1100. _checkBillsOverRangeTz(bills, coe) {
  1101. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  1102. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  1103. if (bills.is_tp) {
  1104. const base_tp = bills.total_price;
  1105. const compare_tp = this.ctx.helper.mul(base_tp, coe);
  1106. if (!base_tp) return !!end_contract_tp;
  1107. return base_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  1108. } else {
  1109. const base_qty = bills.quantity;
  1110. const compare_qty = this.ctx.helper.mul(bills.final_1_qty, coe);
  1111. if (!base_qty) return !!end_contract_qty;
  1112. return base_qty > 0
  1113. ? end_contract_qty > compare_qty
  1114. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1115. }
  1116. }
  1117. _checkBillsOverRangeDeal(bills, coe) {
  1118. const end_contract_qty = this.ctx.helper.add(bills.contract_qty, bills.pre_contract_qty);
  1119. const end_contract_tp = this.ctx.helper.add(bills.contract_tp, bills.pre_contract_tp);
  1120. if (bills.is_tp) {
  1121. const base_tp = bills.deal_tp;
  1122. const compare_tp = this.ctx.helper.mul(base_tp, coe);
  1123. if (!base_tp) return !!end_contract_tp;
  1124. return base_tp >= 0 ? end_contract_tp > compare_tp : end_contract_tp < compare_tp || end_contract_tp > 0;
  1125. } else {
  1126. const base_qty = bills.deal_qty;
  1127. const compare_qty = this.ctx.helper.mul(bills.deal_final_1_qty, coe);
  1128. if (!base_qty) return !!end_contract_qty;
  1129. return base_qty > 0
  1130. ? end_contract_qty > compare_qty
  1131. : (compare_qty > 0 ? true : end_contract_qty < compare_qty || end_contract_qty > 0);
  1132. }
  1133. }
  1134. _checkBillsOverRange(bills, posRange, checkInfo) {
  1135. if (checkInfo.hasPosCheckPos && posRange.length > 0) {
  1136. for (const p of posRange) {
  1137. if (this._checkPosOverRange(p, checkInfo)) return true;
  1138. }
  1139. }
  1140. if (checkInfo.hasPosCheckBills || posRange.length === 0) {
  1141. const checkTz = checkInfo.checkTz ? this._checkBillsOverRangeTz(bills, checkInfo.coe) : false;
  1142. const checkDeal = checkInfo.checkDeal ? this._checkBillsOverRangeDeal(bills, checkInfo.coe) : false;
  1143. return checkTz || checkDeal;
  1144. }
  1145. return false;
  1146. }
  1147. checkOverRange(checkInfo) {
  1148. for (const b of this.checkBills.nodes) {
  1149. if (b.children && b.children.length > 0) continue;
  1150. const pr = this.checkPos.getLedgerPos(b.id) || [];
  1151. if (this._checkBillsOverRange(b, pr, checkInfo)) {
  1152. this.checkResult.error.push({
  1153. ledger_id: b.ledger_id,
  1154. b_code: b.b_code,
  1155. name: b.name,
  1156. errorType: 'over',
  1157. });
  1158. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1159. this.checkResult.source.bills.push(b);
  1160. if (pr.length > 0) this.checkResult.source.pos.push(...pr);
  1161. }
  1162. }
  1163. }
  1164. }
  1165. checkMinusChangeBills(change, changeBills, finalStageChange) {
  1166. const error = this.checkResult.error;
  1167. const helper = this.ctx.helper;
  1168. const changeIndex = {};
  1169. change.forEach(c => {
  1170. changeIndex[c.cid] = c;
  1171. c.bills = [];
  1172. c.billsIndex = {};
  1173. c.stageChange = [];
  1174. });
  1175. changeBills.forEach(cb => {
  1176. const c = changeIndex[cb.cid];
  1177. if (c) c.bills.push(cb);
  1178. c.billsIndex[cb.id] = cb;
  1179. cb.used_qty = 0;
  1180. cb.qty = parseFloat(cb.samount);
  1181. });
  1182. finalStageChange.forEach(sc => {
  1183. if (!sc.qty) return;
  1184. const c = changeIndex[sc.cid];
  1185. if (c) {
  1186. c.used = true;
  1187. const cb = c.billsIndex[sc.cbid];
  1188. if (cb) cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1189. }
  1190. });
  1191. change.forEach(c => {
  1192. if (!c.used) return;
  1193. c.bills.forEach(b => {
  1194. if (b.qty >= 0) return;
  1195. if (!helper.numEqual(b.used_qty, b.qty)) error.push({ b_code: b.code, name: b.name, errorType: 'minus_cb', memo: c.code });
  1196. });
  1197. });
  1198. }
  1199. checkChangeBillsOver(change, changeBills, finalStageChange, curStageId) {
  1200. const error = this.checkResult.error;
  1201. const helper = this.ctx.helper;
  1202. const changeIndex = {};
  1203. change.forEach(c => {
  1204. changeIndex[c.cid] = c;
  1205. c.bills = [];
  1206. c.billsIndex = {};
  1207. c.stageChange = [];
  1208. });
  1209. changeBills.forEach(cb => {
  1210. const c = changeIndex[cb.cid];
  1211. if (c) c.bills.push(cb);
  1212. c.billsIndex[cb.id] = cb;
  1213. cb.used_qty = 0;
  1214. cb.qty = parseFloat(cb.samount);
  1215. });
  1216. finalStageChange.forEach(sc => {
  1217. if (!sc.qty) return;
  1218. const c = changeIndex[sc.cid];
  1219. if (c) {
  1220. c.used = true;
  1221. const cb = c.billsIndex[sc.cbid];
  1222. if (cb) {
  1223. cb.used_qty = helper.add(cb.used_qty, sc.qty);
  1224. if (sc.sid === curStageId) {
  1225. cb.cur_used = true;
  1226. cb.lid = sc.lid;
  1227. }
  1228. }
  1229. }
  1230. });
  1231. change.forEach(c => {
  1232. if (!c.used) return;
  1233. c.bills.forEach(b => {
  1234. if (!b.cur_used) return;
  1235. const qtyDecimal = helper.findDecimal(b.unit);
  1236. const limitQty = helper.mul(b.qty, helper.div(b.delimit, 100, 2), qtyDecimal);
  1237. 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 });
  1238. });
  1239. });
  1240. }
  1241. checkSettle() {
  1242. const settleStatus = this.ctx.service.settle.settleStatus;
  1243. for (const b of this.checkBills.nodes) {
  1244. if (b.children && b.children.length > 0) continue;
  1245. if (!b.settleStatus) continue;
  1246. const pr = this.checkPos.getLedgerPos(b.id);
  1247. if (!pr || pr.length === 0) {
  1248. if (b.settleStatus !== settleStatus.finish) continue;
  1249. if (b.contract_qty || b.contract_tp || b.qc_qty || b.qc_minus_qty || b.positive_qc_qty || b.negative_qc_qty) {
  1250. this.checkResult.error.push({
  1251. ledger_id: b.ledger_id,
  1252. b_code: b.b_code,
  1253. name: b.name,
  1254. errorType: 'settle',
  1255. });
  1256. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1257. this.checkResult.source.bills.push(b);
  1258. }
  1259. }
  1260. } else {
  1261. for (const p of pr) {
  1262. if (p.settle_status !== settleStatus.finish) continue;
  1263. if (p.contract_qty || p.qc_qty || p.qc_minus_qty || p.positive_qc_qty || p.negative_qc_qty) {
  1264. this.checkResult.error.push({
  1265. ledger_id: b.ledger_id,
  1266. b_code: b.b_code,
  1267. name: b.name,
  1268. errorType: 'settle',
  1269. });
  1270. if (!this.checkResult.source.bills.find(x => {return x.ledger_id === b.ledger_id})) {
  1271. this.checkResult.source.bills.push(b);
  1272. for (const p of pr) {
  1273. this.checkResult.source.pos.push(p);
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. }
  1282. class reviseTree extends billsTree {
  1283. constructor (ctx, setting) {
  1284. super(ctx, setting);
  1285. this.price = [];
  1286. }
  1287. loadRevisePrice(price, decimal) {
  1288. this.decimal = decimal;
  1289. this.price = price || [];
  1290. this.rela_price = [];
  1291. this.common_price = [];
  1292. this.price.forEach(x => {
  1293. if (x.rela_lid) {
  1294. x.rela_lid = x.rela_lid.split(',');
  1295. this.rela_price.push(x);
  1296. } else {
  1297. this.common_price.push(x);
  1298. }
  1299. });
  1300. }
  1301. checkRevisePrice(d) {
  1302. if (d.settle_status) return false;
  1303. const helper = this.ctx.helper;
  1304. const setting = this.setting;
  1305. const pid = this.getAllParents(d).map(x => { return x[setting.id] + ''; });
  1306. const checkRela = function(rela_lid) {
  1307. if (!rela_lid || rela_lid.length === 0) return false;
  1308. for (const lid of rela_lid) {
  1309. if (pid.indexOf(lid) >= 0) return true;
  1310. }
  1311. return false;
  1312. };
  1313. let p = this.rela_price.find(x => {
  1314. return x.b_code === d.b_code &&
  1315. ((!x.name && !d.name) || x.name === d.name) &&
  1316. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1317. helper.checkZero(x.org_price - d.unit_price) &&
  1318. checkRela(x.rela_lid);
  1319. });
  1320. if (!p) p = this.common_price.find(x => {
  1321. return x.b_code === d.b_code &&
  1322. ((!x.name && !d.name) || x.name === d.name) &&
  1323. ((!x.unit && !d.unit) || x.unit === d.unit) &&
  1324. helper.checkZero(x.org_price - d.unit_price);
  1325. });
  1326. if (!p) return false;
  1327. d.org_price = p.org_price;
  1328. d.unit_price = p.new_price;
  1329. d.deal_tp = helper.mul(d.deal_qty, d.unit_price, this.decimal.tp);
  1330. d.sgfh_tp = helper.mul(d.sgfh_qty, d.unit_price, this.decimal.tp);
  1331. d.sjcl_tp = helper.mul(d.sjcl_qty, d.unit_price, this.decimal.tp);
  1332. d.qtcl_tp = helper.mul(d.qtcl_qty, d.unit_price, this.decimal.tp);
  1333. d.total_price = helper.mul(d.quantity, d.unit_price, this.decimal.tp);
  1334. d.ex_tp1 = helper.mul(d.ex_qty1, d.unit_price, this.decimal.tp);
  1335. return true;
  1336. }
  1337. loadDatas(datas) {
  1338. super.loadDatas(datas);
  1339. if (this.price.length > 0) {
  1340. for (const d of this.datas) {
  1341. if (d.children && d.children.length > 0) continue;
  1342. if (!d.b_code) continue;
  1343. this.checkRevisePrice(d);
  1344. }
  1345. }
  1346. }
  1347. getUpdateReviseData() {
  1348. return this.datas.map(x => {
  1349. if (x.children && x.children.length > 0) {
  1350. return {
  1351. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1352. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 0,
  1353. node_type: x.node_type, check_calc: x.check_calc,
  1354. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1355. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1356. unit_price: 0, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1357. quantity: 0, total_price: 0,
  1358. sgfh_qty: 0, sgfh_tp: 0, sgfh_expr: '',
  1359. sjcl_qty: 0, sjcl_tp: 0, sjcl_expr: '',
  1360. qtcl_qty: 0, qtcl_tp: 0, qtcl_expr: '',
  1361. deal_qty: 0, deal_tp: 0,
  1362. ex_qty1: 0, ex_tp1: 0,
  1363. };
  1364. } else {
  1365. return {
  1366. id: x.id, tender_id: x.tender_id, crid: x.crid,
  1367. ledger_id: x.ledger_id, ledger_pid: x.ledger_pid, full_path: x.full_path, order: x.order, level: x.level, is_leaf: 1,
  1368. node_type: x.node_type, check_calc: x.check_calc,
  1369. code: x.code, b_code: x.b_code, name: x.name, unit: x.unit, position: x.position,
  1370. drawing_code: x.drawing_code, memo: x.memo, add_user: x.add_user, in_time: x.in_time,
  1371. unit_price: x.unit_price, dgn_qty1: x.dgn_qty1, dgn_qty2: x.dgn_qty2,
  1372. quantity: x.quantity, total_price: x.total_price,
  1373. sgfh_qty: x.sgfh_qty, sgfh_tp: x.sgfh_tp, sgfh_expr: x.sgfh_expr,
  1374. sjcl_qty: x.sjcl_qty, sjcl_tp: x.sjcl_tp, sjcl_expr: x.sjcl_expr,
  1375. qtcl_qty: x.qtcl_qty, qtcl_tp: x.qtcl_tp, qtcl_expr: x.qtcl_expr,
  1376. deal_qty: x.deal_qty, deal_tp: x.deal_tp,
  1377. ex_qty1: x.ex_qty1, ex_tp1: x.ex_tp1,
  1378. };
  1379. }
  1380. });
  1381. }
  1382. sum() {
  1383. const result = { total_price: 0 };
  1384. for (const d of this.datas) {
  1385. if (d.children && d.children.length > 0) continue;
  1386. result.total_price = this.ctx.helper.add(result.total_price, d.total_price);
  1387. }
  1388. return result;
  1389. }
  1390. }
  1391. const treeUtils = {
  1392. loadChangeData: function(tree, pos, changes, tenderInfo, helper) {
  1393. const findEmptyBills = function(tree, change, changeBills) {
  1394. const cb = {
  1395. b_code: changeBills.code || '',
  1396. name: changeBills.name || '',
  1397. unit: changeBills.unit || '',
  1398. unit_price: changeBills.unit_price || 0,
  1399. };
  1400. for (const node of tree.nodes) {
  1401. if (node.children && node.children.length > 0) continue;
  1402. const b = {
  1403. b_code: node.b_code || '',
  1404. name: node.name || '',
  1405. unit: node.unit || '',
  1406. unit_price: node.unit_price || 0,
  1407. };
  1408. if (helper._.isMatch(cb, b)) return node;
  1409. }
  1410. return null;
  1411. };
  1412. for (const change of changes) {
  1413. for (const changeBills of change.bills) {
  1414. const node = changeBills.gcl_id
  1415. ? tree.nodes.find(x => {return x.id === changeBills.gcl_id})
  1416. : findEmptyBills(tree, change, changeBills);
  1417. if (!node) continue;
  1418. if (!node.changeBills) node.changeBills = [];
  1419. node.tz_qc_qty = helper.add(node.tz_qc_qty, changeBills.checked_amount);
  1420. if (tenderInfo.calc_type === 'up') {
  1421. node.tz_qc_tp = helper.add(node.tz_qc_tp, changeBills.checked_price);
  1422. } else {
  1423. node.tz_qc_tp = helper.add(node.tz_qc_tp, changeBills.tp);
  1424. }
  1425. const posData = pos ? pos.getLedgerPos(node.id) : undefined;
  1426. if (posData && posData.length > 0) {
  1427. for (const p of changeBills.pos) {
  1428. const changePos = p.bwmx ? posData.find(x => { return x.name === p.bwmx; }) : posData[0];
  1429. if (changePos) {
  1430. changePos.tz_qc_qty = helper.add(changePos.tz_qc_qty, p.checked_amount);
  1431. if (!changePos.changeBills) changePos.changeBills = [];
  1432. changePos.changeBills.push({
  1433. code: change.code || '', name: change.name || '', is_valuation: p.is_valuation,
  1434. qty: p.checked_amount || 0, tp: p.checked_price || 0, pos: changePos.name,
  1435. });
  1436. node.changeBills.push({
  1437. code: change.code || '', name: change.name || '', is_valuation: p.is_valuation,
  1438. qty: p.checked_amount || 0, tp: p.checked_price || 0, pos: changePos.name,
  1439. });
  1440. }
  1441. }
  1442. } else {
  1443. for (const p of changeBills.pos) {
  1444. node.changeBills.push({
  1445. code: change.code || '', name: change.name || '', is_valuation: p.is_valuation,
  1446. qty: p.checked_amount || 0, tp: p.checked_price || 0, pos: '',
  1447. });
  1448. }
  1449. }
  1450. }
  1451. }
  1452. }
  1453. };
  1454. module.exports = {
  1455. baseTree,
  1456. billsTree,
  1457. pos,
  1458. filterTree,
  1459. filterGatherTree,
  1460. gatherTree,
  1461. gatherPos,
  1462. checkData,
  1463. reviseTree,
  1464. treeUtils,
  1465. };