stage_im.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Ledger = require('./ledger');
  10. const imType = require('../const/tender').imType;
  11. const mergeChar = ';';
  12. const version = '1.0';
  13. class StageIm {
  14. constructor(ctx) {
  15. const self = this;
  16. this.ctx = ctx;
  17. this._ = this.ctx.helper._;
  18. // mainData
  19. this.billsTree = new Ledger.billsTree(this.ctx, {
  20. id: 'ledger_id',
  21. pid: 'ledger_pid',
  22. order: 'order',
  23. level: 'level',
  24. rootId: -1,
  25. keys: ['id', 'tender_id', 'ledger_id'],
  26. stageId: 'id',
  27. calcFields: ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp'],
  28. calc(node) {
  29. if (node.children && node.children.length === 0) {
  30. node.pre_gather_qty = self.ctx.helper.add(node.pre_contract_qty, node.pre_qc_qty);
  31. node.gather_qty = self.ctx.helper.add(node.contract_qty, node.qc_qty);
  32. node.end_contract_qty = self.ctx.helper.add(node.pre_contract_qty, node.contract_qty);
  33. node.end_qc_qty = self.ctx.helper.add(node.pre_qc_qty, node.qc_qty);
  34. node.end_gather_qty = self.ctx.helper.add(node.pre_gather_qty, node.gather_qty);
  35. }
  36. node.pre_gather_tp = self.ctx.helper.add(node.pre_contract_tp, node.pre_qc_tp);
  37. node.gather_tp = self.ctx.helper.add(node.contract_tp, node.qc_tp);
  38. node.end_contract_tp = self.ctx.helper.add(node.pre_contract_tp, node.contract_tp);
  39. node.end_qc_tp = self.ctx.helper.add(node.pre_qc_tp, node.qc_tp);
  40. node.end_gather_tp = self.ctx.helper.add(node.pre_gather_tp, node.gather_tp);
  41. },
  42. });
  43. this.pos = new Ledger.pos({
  44. id: 'id', ledgerId: 'lid',
  45. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  46. calc(p) {
  47. p.pre_gather_qty = self.ctx.helper.add(p.pre_contract_qty, p.pre_qc_qty);
  48. p.gather_qty = self.ctx.helper.add(p.contract_qty, p.qc_qty);
  49. p.end_contract_qty = self.ctx.helper.add(p.pre_contract_qty, p.contract_qty);
  50. p.end_qc_qty = self.ctx.helper.add(p.pre_qc_qty, p.qc_qty);
  51. p.end_gather_qty = self.ctx.helper.add(p.pre_gather_qty, p.gather_qty);
  52. },
  53. });
  54. // relaData
  55. this.change = null;
  56. this.details = null;
  57. // result
  58. this.ImData = [];
  59. this.ImBillsData = [];
  60. //
  61. this.imFields = ['uuid', 'doc_code', 'peg', 'bw', 'xm', 'drawing_code', 'calc_memo', 'calc_memo_org', 'calc_img_remark', 'calc_img', 'position', 'jldy'];
  62. this.splitChar = '-';
  63. }
  64. // 加载数据
  65. async _loadMainData() {
  66. const billsData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
  67. if (this.ctx.stage.readOnly) {
  68. const curStage = await this.ctx.service.stageBills.getAuditorStageData2(this.ctx.tender.id,
  69. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  70. this.ctx.helper.assignRelaData(billsData, [
  71. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  72. ]);
  73. } else {
  74. const curStage = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  75. this.ctx.helper.assignRelaData(billsData, [
  76. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  77. ]);
  78. }
  79. const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  80. this.ctx.helper.assignRelaData(billsData, [
  81. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  82. ]);
  83. this.billsTree.loadDatas(billsData);
  84. this.billsTree.calculateAll();
  85. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  86. if (this.ctx.stage.readOnly) {
  87. const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
  88. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  89. this.ctx.helper.assignRelaData(posData, [
  90. { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  91. ]);
  92. } else {
  93. const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  94. this.ctx.helper.assignRelaData(posData, [
  95. { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  96. ]);
  97. }
  98. const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  99. this.ctx.helper.assignRelaData(posData, [
  100. { data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  101. ]);
  102. this.pos.loadDatas(posData);
  103. this.pos.calculateAll();
  104. }
  105. async _loadRelaData() {
  106. if (this.ctx.stage.readOnly) {
  107. this.details = await this.ctx.service.stageDetail.getAuditorStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  108. } else {
  109. this.details = await this.ctx.service.stageDetail.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  110. }
  111. if (this.ctx.stage.readOnly) {
  112. this.changes = await this.ctx.service.stageChange.getAuditorAllStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  113. } else {
  114. this.changes = await this.ctx.service.stageChange.getLastestAllStageData(this.ctx.tender.id, this.ctx.stage.id);
  115. }
  116. }
  117. // 检查汇总节点
  118. async _checkGather() {
  119. const gatherNodes = this.stage.im_gather_node ? this.stage.im_gather_node.split(',') : [];
  120. for (const node of this.billsTree.datas) {
  121. node.check = gatherNodes.indexOf(node.id) !== -1;
  122. }
  123. }
  124. // 辅助取值方法
  125. _getNumberFormat(num, length) {
  126. let s = '0000000000';
  127. s = s + num;
  128. return s.substr(s.length - length);
  129. }
  130. _getNodeByLevel(node, level) {
  131. let cur = node;
  132. while (cur && cur.level > level) {
  133. cur = this.billsTree.getParent(cur);
  134. }
  135. return cur;
  136. }
  137. _checkPeg(text) {
  138. const pegReg = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  139. return pegReg.test(text);
  140. }
  141. _getPegStr(text) {
  142. const pegReg1 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?[~~—][a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  143. const result1 = text.match(pegReg1);
  144. if (result1) {
  145. return result1[0];
  146. }
  147. const pegReg2 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?-[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  148. const result2 = text.match(pegReg2);
  149. if (result2) {
  150. return result2[0];
  151. }
  152. const pegReg3 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  153. const result3 = text.match(pegReg3);
  154. return result3 ? result3[0] : '';
  155. }
  156. _getPegNode(node) {
  157. if (node) {
  158. if (this._checkPeg(node.name)) {
  159. return node;
  160. }
  161. const parent = this.billsTree.getParent(node);
  162. return parent ? this._getPegNode(parent) : null;
  163. }
  164. }
  165. _getDrawingCode(node) {
  166. if (!node) {
  167. return '';
  168. } else if (node.drawing_code) {
  169. return node.drawing_code;
  170. }
  171. const parent = this.billsTree.getParent(node);
  172. return parent ? this._getDrawingCode(parent) : '';
  173. }
  174. _getZlNormalBw(node, peg) {
  175. if (peg) {
  176. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  177. const subPeg2 = this._getNodeByLevel(node, peg.level + 2);
  178. let result = peg.name;
  179. if (subPeg1 && subPeg1.id !== peg.id && subPeg1.id !== node.id) {
  180. result = result + mergeChar + subPeg1.name;
  181. if (subPeg2 && subPeg2.id !== subPeg1.id && subPeg2.id !== node.id) {
  182. result = result + mergeChar + subPeg2.name;
  183. }
  184. }
  185. return result;
  186. }
  187. if (node.level === 2 || node.level === 3) {
  188. return node.name;
  189. } else if (node.level >= 4) {
  190. let parent = this.billsTree.getParent(node),
  191. result = parent.name;
  192. while (parent.level > 3 && parent) {
  193. parent = this._getNodeByLevel(node, parent.level - 1);
  194. result = parent.name + mergeChar + result;
  195. }
  196. return result;
  197. }
  198. return '';
  199. }
  200. _getZlGatherBw(node, peg) {
  201. if (peg) {
  202. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  203. let result = peg.name;
  204. if (subPeg1 && subPeg1.id !== peg.id) {
  205. result = result + mergeChar + subPeg1.name;
  206. }
  207. return result;
  208. }
  209. if (node.level < 3) {
  210. return node.name;
  211. }
  212. let parent = node,
  213. result = parent.name;
  214. while (parent.level > 3 && parent) {
  215. parent = this._getNodeByLevel(node, parent.level - 1);
  216. result = parent.name + mergeChar + result;
  217. }
  218. return result;
  219. }
  220. _getBwBillsBw(node) {
  221. if (node.level < 3) {
  222. return '';
  223. } else {
  224. let parent = node, result = parent.name;
  225. while (parent.level > 3 && parent) {
  226. parent = this._getNodeByLevel(node, parent.level - 1);
  227. result = parent.name + mergeChar + result;
  228. }
  229. return result;
  230. }
  231. }
  232. /**
  233. * 获取 单位工程
  234. *
  235. * @param xmj - 计量单元(最底层项目节)
  236. * @returns {string}
  237. */
  238. _getDwgc(peg, xmj) {
  239. if (peg) {
  240. return peg.name;
  241. } else {
  242. const node = this._getNodeByLevel(xmj, 2);
  243. return node ? node.name : '';
  244. }
  245. }
  246. /**
  247. * 获取 分部工程
  248. *
  249. * @param peg - 桩号节点
  250. * @param xmj - 计量单元(最底层项目节)
  251. * @returns {string}
  252. */
  253. _getFbgc(peg, xmj) {
  254. if (peg && peg.id !== xmj.id) {
  255. const node = this._getNodeByLevel(xmj, peg.level + 1);
  256. return node ? node.name : '';
  257. } else {
  258. const node = this._getNodeByLevel(xmj, 3);
  259. return node ? node.name : '';
  260. }
  261. }
  262. /**
  263. * 获取 分项工程
  264. *
  265. * @param peg - 桩号节点
  266. * @param xmj - 计量单元(最底层项目节)
  267. * @returns {string}
  268. */
  269. _getFxgc(peg, xmj) {
  270. if (!peg) {
  271. const node = this._getNodeByLevel(xmj, 4);
  272. return node ? node.name : '';
  273. } else if (peg.id === xmj.id) {
  274. if (xmj.level > 4) {
  275. let value = '';
  276. for (let level = 4; level < xmj.level; level++) {
  277. const node = this._getNodeByLevel(xmj, level);
  278. value = value === '' ? node.name : value + mergeChar + node.name;
  279. }
  280. return value;
  281. } else {
  282. return '';
  283. }
  284. } else {
  285. if (peg.level + 2 < xmj.level) {
  286. let value = '';
  287. for (let level = peg.level + 2; level < xmj.level; level++) {
  288. const node = this._getNodeByLevel(xmj, level);
  289. value = value === '' ? node.name : value + mergeChar + node.name;
  290. }
  291. return value;
  292. } else {
  293. return '';
  294. }
  295. }
  296. }
  297. _checkCustomDetail(im) {
  298. const self = this;
  299. let cd;
  300. switch (this.stage.im_type) {
  301. case imType.tz.value:
  302. cd = this._.find(this.details, function (d) {
  303. return im.lid === d.lid &&
  304. (!im.code || im.code === d.code) &&
  305. (!im.name || im.name === d.name) &&
  306. (!im.unit || im.unit === d.unit) &&
  307. (!im.pid || im.pid === d.pid) &&
  308. (!im.pos_name || im.pos_name === d.pos_name);
  309. });
  310. break;
  311. case imType.zl.value:
  312. cd = this._.find(this.details, function (d) {
  313. return im.lid === d.lid &&
  314. (!im.code || im.code === d.code) &&
  315. (!im.name || im.name === d.name) &&
  316. (!im.unit || im.unit === d.unit) &&
  317. self.ctx.helper.checkZero(self.ctx.helper.sub(im[self.up_field], d[self.up_field])) &&
  318. (!im.pid || im.pid === d.pid) &&
  319. (!im.pos_name || im.pos_name === d.pos_name);
  320. });
  321. break;
  322. case imType.bw.value:
  323. cd = this._.find(this.details, function (d) {
  324. return im.lid === d.lid &&
  325. (!im.code || im.code === d.code) &&
  326. (!im.name || im.name === d.name) &&
  327. (!im.unit || im.unit === d.unit) &&
  328. self.ctx.helper.checkZero(self.ctx.helper.sub(im[self.up_field], d[self.up_field])) &&
  329. (!im.pid || im.pid === d.pid) &&
  330. (!im.pos_name || im.pos_name === d.pos_name);
  331. });
  332. break;
  333. case imType.bb.value:
  334. cd = this._.find(this.details, function (d) {
  335. return im.lid === d.lid &&
  336. (!im.name || im.name === d.name) &&
  337. (!im.unit || im.unit === d.unit) &&
  338. (!im.pid || im.pid === d.pid) &&
  339. (!im.pos_name || im.pos_name === d.pos_name);
  340. });
  341. break;
  342. }
  343. if (cd) {
  344. im.custom_define = im.custom_define !== null ? (cd.custom_define ? cd.custom_define.split(',') : []) : this.imFields;
  345. this._.assignInWith(im, cd, function(oV, sV, key) { return im.custom_define.indexOf(key) > -1 ? sV : oV; });
  346. }
  347. }
  348. _getCalcMemo(im) {
  349. if (im.gclBills && im.gclBills.length > 0) {
  350. const self = this;
  351. im.gclBills.sort(function (x, y) {
  352. return self.ctx.helper.compareCode(x.b_code, y.b_code);
  353. });
  354. for (const b of im.gclBills) {
  355. b.imid = im.id;
  356. this.ImBillsData.push(b);
  357. }
  358. }
  359. if (im.calc_memo !== undefined && im.calc_memo !== null && im.calc_memo !== '') return;
  360. if (im.leafXmjs && im.leafXmjs.length > 0) {
  361. const memo = ['本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit];
  362. for (const lx of im.leafXmjs) {
  363. for (const p of lx.pos) {
  364. memo.push(p.name + ':' + p.jl + ' ' + im.unit);
  365. }
  366. }
  367. im.calc_memo = memo.join('\n');
  368. } else if (im.gclBills && im.gclBills.length > 0) {
  369. const memo = [];
  370. for (const [i, b] of im.gclBills.entries()) {
  371. if (b.pos && b.pos.length > 0) {
  372. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name);
  373. for (const p of b.pos) {
  374. memo.push(p.name + ':' + p.jl + ' ' + b.unit);
  375. }
  376. } else {
  377. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name + ':' + (this.ctx.helper.checkZero(b.jl) ? 0 : b.jl) + ' ' + b.unit);
  378. }
  379. }
  380. im.calc_memo = memo.join('\n');
  381. } else {
  382. im.calc_memo = '';
  383. }
  384. }
  385. _getChangeInfo(im) {
  386. if (im.changes && im.changes.length > 0) {
  387. const code = this._.uniq(this._.map(im.changes, 'c_code'));
  388. if (!im.bgl_code || im.bgl_code === '') im.bgl_code = code.join(';');
  389. const new_code = this._.uniq(this._.map(im.changes, 'c_new_code'));
  390. if (!im.bgl_drawing_code || im.bgl_drawing_code === '') im.bgl_drawing_code = new_code.join(';');
  391. }
  392. }
  393. _generateTzPosData(node, gclBills) {
  394. if (!gclBills.pos) {
  395. gclBills.pos = [];
  396. }
  397. const posRange = this.pos.getLedgerPos(node.id);
  398. if (!posRange) { return; }
  399. for (const p of posRange) {
  400. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.contract_qty)) { continue; }
  401. let lp = this._.find(gclBills.pos, { name: p.name });
  402. if (!lp) {
  403. lp = { name: p.name };
  404. gclBills.pos.push(lp);
  405. }
  406. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  407. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  408. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  409. }
  410. }
  411. _recursiveGenerateTzGclBills(node, im) {
  412. for (const p of node.children) {
  413. if (p.children && p.children.length > 0) {
  414. this._recursiveGenerateTzGclBills(p, im);
  415. } else {
  416. if ((!p.b_code || p.b_code === '') || (p.children && p.children.length > 0)) continue;
  417. if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.contract_tp) &&
  418. this.ctx.helper.checkZero(p.qc_qty) && this.ctx.helper.checkZero(p.qc_tp)) {
  419. continue;
  420. }
  421. let b = this._.find(im.gclBills, { bid: p.id });
  422. if (!b) {
  423. b = { imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price };
  424. im.gclBills.push(b);
  425. }
  426. b.quantity = this.ctx.helper.add(b.quantity, p.quantity);
  427. b.total_price = this.ctx.helper.add(b.total_price, p.total_price);
  428. b.jl = this.ctx.helper.add(b.jl, p.gather_qty);
  429. b.contract_jl = this.ctx.helper.add(b.contract_jl, p.contract_qty);
  430. b.qc_jl = this.ctx.helper.add(b.qc_jl, p.qc_qty);
  431. b.tp = this.ctx.helper.add(b.tp, p.gather_tp);
  432. b.contract_tp = this.ctx.helper.add(b.contract_tp, p.contract_tp);
  433. b.qc_tp = this.ctx.helper.add(b.qc_tp, p.qc_tp);
  434. b.pre_jl = this.ctx.helper.add(b.pre_jl, p.pre_gather_qty);
  435. b.pre_contract_jl = this.ctx.helper.add(b.pre_contract_jl, p.pre_contract_qty);
  436. b.pre_qc_jl = this.ctx.helper.add(b.pre_qc_jl, p.pre_qc_qty);
  437. b.pre_tp = this.ctx.helper.add(b.pre_tp, p.pre_gather_tp);
  438. b.pre_contract_tp = this.ctx.helper.add(b.pre_contract_tp, p.pre_contract_tp);
  439. b.pre_qc_tp = this.ctx.helper.add(b.pre_qc_tp, p.pre_qc_tp);
  440. b.end_jl = this.ctx.helper.add(b.end_jl, p.end_gather_qty);
  441. b.end_contract_jl = this.ctx.helper.add(b.end_contract_jl, p.end_contract_qty);
  442. b.end_qc_jl = this.ctx.helper.add(b.end_qc_jl, p.end_qc_qty);
  443. b.end_tp = this.ctx.helper.add(b.end_tp, p.end_gather_tp);
  444. b.end_contract_tp = this.ctx.helper.add(b.end_contract_tp, p.end_contract_tp);
  445. b.end_qc_tp = this.ctx.helper.add(b.end_qc_tp, p.end_qc_tp);
  446. this._generateTzPosData(p, b);
  447. }
  448. }
  449. }
  450. _generateTzGclBillsData(node, im) {
  451. if (!im.gclBills) {
  452. im.gclBills = [];
  453. }
  454. this._recursiveGenerateTzGclBills(node, im);
  455. }
  456. _generateTzChangeData(node, im) {
  457. if (!im.changes) {
  458. im.changes = [];
  459. }
  460. const posterity = this.billsTree.getPosterity(node);
  461. for (const p of posterity) {
  462. if (p.children && p.children.length > 0) {
  463. continue;
  464. }
  465. if ((!p.qc_tp || p.qc_tp === 0)) {
  466. continue;
  467. }
  468. const posRange = this.pos.getLedgerPos(p.id);
  469. if (!posRange) {
  470. for (const c of this.changes) {
  471. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  472. im.changes.push(c);
  473. }
  474. }
  475. } else {
  476. for (const pp of posRange) {
  477. if ((!pp.qc_qty || pp.qc_qty === 0)) {
  478. continue;
  479. }
  480. for (const c of this.changes) {
  481. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  482. im.changes.push(c);
  483. }
  484. }
  485. }
  486. }
  487. }
  488. }
  489. _checkUsed(node) {
  490. const helper = this.ctx.helper;
  491. if (node.children && node.children.length > 0) {
  492. const posterity = this.billsTree.getPosterity(node);
  493. for (const p of posterity) {
  494. if (p.children && p.children.length > 0) continue;
  495. if (!helper.checkZero(p.contract_qty) || !helper.checkZero(p.contract_tp) ||
  496. !helper.checkZero(p.qc_qty) || !helper.checkZero(p.qc_tp))
  497. return true;
  498. const pPos = this.pos.getLedgerPos(p.id);
  499. if (!pPos || pPos.length === 0) continue;
  500. for (const pp of pPos) {
  501. if (!helper.checkZero(pp.contract_qty) || !helper.checkZero(pp.qc_qty)) return true;
  502. }
  503. }
  504. return false;
  505. } else {
  506. if (!helper.checkZero(node.contract_qty) || !helper.checkZero(node.contract_tp) ||
  507. !helper.checkZero(node.qc_qty) || !helper.checkZero(node.qc_tp))
  508. return true;
  509. const pPos = this.pos.getLedgerPos(node.id);
  510. if (!pPos || pPos.length === 0) return false;
  511. for (const pp of pPos) {
  512. if (!helper.checkZero(pp.contract_qty) || !helper.checkZero(pp.qc_qty)) return true;
  513. }
  514. }
  515. }
  516. /**
  517. * 生成 0号台账 中间计量数据
  518. * @param {Object} node - 生成中间计量表的节点
  519. */
  520. _generateTzImData(node) {
  521. if (!this._checkUsed(node)) return;
  522. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  523. const peg = this._getPegNode(node);
  524. const im = {
  525. id: this.ImData.length + 1,
  526. lid: node.id, pid: '', code: node.code,
  527. jl: node.gather_tp, contract_jl: node.contract_tp, qc_jl: node.qc_tp,
  528. pre_jl: node.pre_gather_tp, pre_contract_jl: node.pre_contract_tp, pre_qc_jl: node.pre_qc_tp,
  529. end_jl: node.end_gather_tp, end_contract_jl: node.end_contract_tp, end_qc_jl: node.end_qc_tp,
  530. peg: peg ? this._getPegStr(peg.name) : '', drawing_code: this._getDrawingCode(node),
  531. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  532. position: '',
  533. lIndex: nodeIndex,
  534. };
  535. if (this.stage.im_gather && node.check) {
  536. im.bw = this._getZlGatherBw(node, peg);
  537. im.xm = '';
  538. } else {
  539. im.bw = this._getZlNormalBw(node, peg);
  540. im.xm = node.name;
  541. }
  542. im.check = this.stage.im_gather && node.check;
  543. this._generateTzGclBillsData(node, im);
  544. this.ImData.push(im);
  545. this._generateTzChangeData(node, im);
  546. }
  547. _addBwBillsGclBills(im, bills, pos) {
  548. const helper = this.ctx.helper;
  549. const tp_decimal = this.ctx.tender.info.decimal.tp;
  550. if (!im.gclBills) im.gclBills = [];
  551. let gcl = im.gclBills.find(function (x) {
  552. return (!bills.b_code || bills.b_code === x.b_code) &&
  553. (!bills.name || bills.name === x.name) &&
  554. (!bills.unit || bills.unit === x.unit) &&
  555. helper.checkZero(helper.sub(bills.unit_price, x.unit_price));
  556. });
  557. if (!gcl) {
  558. gcl = {b_code: bills.b_code, name: bills.name, unit: bills.unit, unit_price: bills.unit_price};
  559. im.gclBills.push(gcl);
  560. }
  561. if (pos) {
  562. gcl.quantity = helper.add(gcl.quantity, pos.quantity);
  563. gcl.total_price = helper.mul(gcl.unit_price, gcl.quantity, tp_decimal);
  564. gcl.jl = helper.add(gcl.jl, pos.gather_qty);
  565. gcl.contract_jl = helper.add(gcl.contract_jl, pos.contract_qty);
  566. gcl.qc_jl = helper.add(gcl.qc_jl, pos.qc_qty);
  567. gcl.tp = helper.mul(gcl.unit_price, gcl.jl, tp_decimal);
  568. gcl.contract_tp = helper.mul(gcl.unit_price, gcl.contract_jl, tp_decimal);
  569. gcl.qc_tp = helper.mul(gcl.unit_price, gcl.qc_jl, tp_decimal);
  570. gcl.pre_jl = helper.add(gcl.pre_jl, pos.pre_gather_qty);
  571. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, pos.pre_contract_qty);
  572. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, pos.pre_qc_qty);
  573. gcl.pre_tp = helper.mul(gcl.unit_price, gcl.pre_jl, tp_decimal);
  574. gcl.pre_contract_tp = helper.mul(gcl.unit_price, gcl.pre_contract_jl, tp_decimal);
  575. gcl.pre_qc_tp = helper.mul(gcl.unit_price, gcl.pre_qc_jl, tp_decimal);
  576. gcl.end_jl = helper.add(gcl.end_jl, pos.end_gather_qty);
  577. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, pos.end_contract_qty);
  578. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, pos.end_qc_qty);
  579. gcl.end_tp = helper.mul(gcl.unit_price, gcl.end_jl, tp_decimal);
  580. gcl.end_contract_tp = helper.mul(gcl.unit_price, gcl.end_contract_jl, tp_decimal);
  581. gcl.end_qc_tp = helper.mul(gcl.unit_price, gcl.end_qc_jl, tp_decimal);
  582. } else {
  583. gcl.quantity = helper.add(gcl.quantity, bills.quantity);
  584. gcl.total_price = helper.add(gcl.total_price, bills.total_price);
  585. gcl.jl = helper.add(gcl.jl, bills.gather_qty);
  586. gcl.contract_jl = helper.add(gcl.contract_jl, bills.contract_qty);
  587. gcl.qc_jl = helper.add(gcl.qc_jl, bills.qc_qty);
  588. gcl.tp = helper.add(gcl.tp, bills.gather_tp);
  589. gcl.contract_tp = helper.add(gcl.contract_tp, bills.contract_tp);
  590. gcl.qc_tp = helper.add(gcl.qc_tp, bills.qc_tp);
  591. gcl.pre_jl = helper.add(gcl.pre_jl, bills.pre_gather_qty);
  592. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, bills.pre_contract_qty);
  593. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, bills.pre_qc_qty);
  594. gcl.pre_tp = helper.add(gcl.pre_tp, bills.pre_gather_tp);
  595. gcl.pre_contract_tp = helper.add(gcl.pre_contract_tp, bills.pre_contract_tp);
  596. gcl.pre_qc_tp = helper.add(gcl.pre_qc_tp, bills.pre_qc_tp);
  597. gcl.end_jl = helper.add(gcl.end_jl, bills.end_gather_qty);
  598. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, bills.end_contract_qty);
  599. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, bills.end_qc_qty);
  600. gcl.end_tp = helper.add(gcl.end_tp, bills.end_gather_tp);
  601. gcl.end_contract_tp = helper.add(gcl.end_contract_tp, bills.end_contract_tp);
  602. gcl.end_qc_tp = helper.add(gcl.end_qc_tp, bills.end_qc_tp);
  603. }
  604. }
  605. _calculateBwBillsIm(im) {
  606. const tp_decimal = this.ctx.tender.info.decimal.tp;
  607. im.contract_jl = 0;
  608. im.qc_jl = 0;
  609. for (const b of im.gclBills) {
  610. im.contract_jl = this.ctx.helper.add(im.contract_jl, this.ctx.helper.mul(b.contract_jl, b.unit_price, tp_decimal));
  611. im.pre_contract_jl = this.ctx.helper.add(im.pre_contract_jl, this.ctx.helper.mul(b.pre_contract_jl, b.unit_price, tp_decimal));
  612. im.end_contract_jl = this.ctx.helper.add(im.end_contract_jl, this.ctx.helper.mul(b.end_contract_jl, b.unit_price, tp_decimal));
  613. im.qc_jl = this.ctx.helper.add(im.qc_jl, this.ctx.helper.mul(b.qc_jl, b.unit_price, tp_decimal));
  614. im.pre_qc_jl = this.ctx.helper.add(im.pre_qc_jl, this.ctx.helper.mul(b.pre_qc_jl, b.unit_price, tp_decimal));
  615. im.end_qc_jl = this.ctx.helper.add(im.end_qc_jl, this.ctx.helper.mul(b.end_qc_jl, b.unit_price, tp_decimal));
  616. }
  617. im.jl = this.ctx.helper.add(im.contract_jl, im.qc_jl);
  618. im.pre_jl = this.ctx.helper.add(im.pre_contract_jl, im.pre_qc_jl);
  619. im.end_jl = this.ctx.helper.add(im.end_contract_jl, im.end_qc_jl);
  620. }
  621. _getBwBillsIm(node, peg, index, bw) {
  622. const im = {
  623. lid: node.id, pid: '', code: node.code, name: node.name, pos_name: '',
  624. peg: peg ? this._getPegStr(peg.name) : '',
  625. drawing_code: this._getDrawingCode(node),
  626. position: '',
  627. lIndex: index,
  628. bw: bw, jldy: node.name,
  629. changes: [], gclBills: [],
  630. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  631. };
  632. return im;
  633. }
  634. _getBwBillsPosIm(imArr, node, peg, index, bw, posName) {
  635. let im = imArr.find(function (d) {
  636. return d.lid === node.id && d.pos_name === posName;
  637. });
  638. if (!im) {
  639. im = {
  640. lid: node.id, pid: '',
  641. code: node.code + '-' + (imArr.length + 1), name: node.name, pos_name: posName,
  642. peg: this._checkPeg(posName) ? posName : (peg ? this._getPegStr(peg.name) : ''),
  643. drawing_code: [], position: [],
  644. lIndex: index,
  645. bw: bw, jldy: posName,
  646. changes: [], gclBills: [],
  647. };
  648. imArr.push(im);
  649. }
  650. return im;
  651. }
  652. _generateBwBillsImData (node) {
  653. if (!this._checkUsed(node)) return;
  654. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  655. const peg = this._getPegNode(node);
  656. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  657. const bw = this._getBwBillsBw(node);
  658. const imDefault = this._getBwBillsIm(node, peg, nodeIndex, bw);
  659. for (const p of posterity) {
  660. if (p.children && p.children.length > 0) continue;
  661. if (!p.b_code || p.b_code === '') continue;
  662. if (!this._checkUsed(p)) continue;
  663. const pPos = this.pos.getLedgerPos(p.id);
  664. if (pPos && pPos.length > 0) {
  665. for (const pp of pPos) {
  666. if (!pp.gather_qty && !pp.contract_qty && !pp.qc_qty) continue;
  667. const im = this._getBwBillsPosIm(nodeImData, node, peg, nodeIndex, bw, pp.name);
  668. this._addBwBillsGclBills(im, p, pp);
  669. for (const c of this.changes) {
  670. if (c.lid === p.id && c.pid == pp.id && c.qty && c.qty !== 0) {
  671. im.changes.push(c);
  672. }
  673. }
  674. if (pp.drawing_code && im.drawing_code instanceof Array) im.drawing_code.push(pp.drawing_code);
  675. if (pp.position && im.position instanceof Array) im.position.push(pp.position);
  676. }
  677. } else {
  678. for (const c of this.changes) {
  679. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  680. imDefault.changes.push(c);
  681. }
  682. }
  683. imDefault.contract_jl = this.ctx.helper.add(imDefault.contract_jl, p.contract_qty);
  684. imDefault.qc_jl = this.ctx.helper.add(imDefault.qc_jl, p.qc_qty);
  685. imDefault.pre_contract_jl = this.ctx.helper.add(imDefault.pre_contract_jl, p.pre_contract_qty);
  686. imDefault.pre_qc_jl = this.ctx.helper.add(imDefault.pre_qc_jl, p.pre_qc_qty);
  687. imDefault.end_contract_jl = this.ctx.helper.add(imDefault.end_contract_jl, p.end_contract_qty);
  688. imDefault.end_qc_jl = this.ctx.helper.add(imDefault.end_qc_jl, p.end_qc_qty);
  689. imDefault.used = true;
  690. this._addBwBillsGclBills(imDefault, p);
  691. }
  692. }
  693. if (imDefault.used) {
  694. imDefault.jl = this.ctx.helper.add(imDefault.contract_jl, imDefault.qc_jl);
  695. imDefault.pre_jl = this.ctx.helper.add(imDefault.pre_contract_jl, imDefault.pre_qc_jl);
  696. imDefault.end_jl = this.ctx.helper.add(imDefault.end_contract_jl, imDefault.end_qc_jl);
  697. imDefault.id = this.ImData.length + 1;
  698. this.ImData.push(imDefault);
  699. }
  700. for (const im of nodeImData) {
  701. this._calculateBwBillsIm(im);
  702. im.drawing_code = im.drawing_code instanceof Array
  703. ? this.ctx.helper._.uniq(im.drawing_code).join(mergeChar) : im.drawing_code;
  704. im.position = im.position instanceof Array
  705. ? this.ctx.helper._.uniq(im.position).join(mergeChar): im.position;
  706. im.id = this.ImData.length + 1;
  707. this.ImData.push(im);
  708. }
  709. }
  710. _generateZlPosData(node, lx) {
  711. if (!lx.pos) {
  712. lx.pos = [];
  713. }
  714. const posRange = this.pos.getLedgerPos(node.id);
  715. if (!posRange) { return; }
  716. for (const p of posRange) {
  717. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  718. let lp = this._.find(lx.pos, { name: p.name });
  719. if (!lp) {
  720. lp = { name: p.name };
  721. lx.pos.push(lp);
  722. }
  723. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  724. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  725. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  726. }
  727. }
  728. _generateZlLeafXmjData(node, im) {
  729. if (!im.leafXmjs) {
  730. im.leafXmjs = [];
  731. }
  732. const leafXmj = this.billsTree.getLeafXmjParent(node);
  733. if (!leafXmj) { return; }
  734. let lx = this._.find(im.leafXmjs, { lxid: leafXmj.id });
  735. if (!lx) {
  736. lx = {
  737. lxid: leafXmj.id,
  738. code: leafXmj.code,
  739. name: leafXmj.name,
  740. };
  741. im.leafXmjs.push(lx);
  742. }
  743. lx.jl = this.ctx.helper.add(lx.jl, node.gather_qty);
  744. lx.contract_jl = this.ctx.helper.add(lx.contract_jl, node.contract_qty);
  745. lx.qc_jl = this.ctx.helper.add(lx.qc_jl, node.qc_qty);
  746. this._generateZlPosData(node, lx);
  747. }
  748. _generateZlChangeData(node, im) {
  749. if (!im.changes) {
  750. im.changes = [];
  751. }
  752. if ((!node.qc_qty || node.qc_qty === 0)) {
  753. return;
  754. }
  755. const posRange = this.pos.getLedgerPos(node.id);
  756. if (!posRange) {
  757. for (const c of this.changes) {
  758. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  759. im.changes.push(c);
  760. }
  761. }
  762. } else {
  763. for (const p of posRange) {
  764. if ((!p.qc_qty || p.qc_qty === 0)) {
  765. continue;
  766. }
  767. for (const c of this.changes) {
  768. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  769. im.changes.push(c);
  770. }
  771. }
  772. }
  773. }
  774. }
  775. /**
  776. * 生成 总量控制 中间计量数据
  777. * @param {Object} node - 生成中间计量表的节点
  778. */
  779. _generateZlImData(node) {
  780. const self = this;
  781. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  782. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  783. for (const p of posterity) {
  784. if (p.children && p.children.length > 0) { continue; }
  785. if (!p.b_code || p.b_code === '') { continue; }
  786. if (!this._checkUsed(p)) { continue; }
  787. // if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue; }
  788. let im = nodeImData.find(function(d) {
  789. return d.lid === node.id &&
  790. d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
  791. self.ctx.helper.checkZero(self.ctx.helper.sub(p[self.up_field], d[self.up_field]));
  792. });
  793. if (!im) {
  794. const peg = this._getPegNode(node);
  795. im = {
  796. id: this.ImData.length + 1,
  797. lid: node.id, pid: '', code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, org_unit_price: p.org_unit_price,
  798. quantity: 0, total_price: 0,
  799. jl: 0, contract_jl: 0, qc_jl: 0,
  800. pre_jl: 0, pre_contract_jl: 0, pre_qc_jl: 0,
  801. end_jl: 0, end_contract_jl: 0, end_qc_jl: 0,
  802. tp: 0, contract_tp: 0, qc_tp: 0,
  803. pre_tp: 0, pre_contract_tp: 0, pre_qc_tp: 0,
  804. end_tp: 0, end_contract_tp: 0, end_qc_tp: 0,
  805. peg: peg ? this._getPegStr(peg.name) : '',
  806. position: '',
  807. lIndex: nodeIndex,
  808. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  809. };
  810. if (this.stage.im_gather && node.check) {
  811. im.check = true;
  812. im.bw = this._getZlGatherBw(node, peg);
  813. im.xm = '';
  814. im.drawing_code = this._getDrawingCode(node);
  815. } else {
  816. im.check = false;
  817. im.bw = this._getZlNormalBw(node, peg);
  818. im.xm = node.name;
  819. im.drawing_code = this._getDrawingCode(p);
  820. }
  821. nodeImData.push(im);
  822. this.ImData.push(im);
  823. }
  824. // if (!this.stage.im_gather || !node.check) {
  825. this._generateZlLeafXmjData(p, im, 'gather_qty');
  826. // }
  827. this._generateZlChangeData(p, im);
  828. im.quantity = this.ctx.helper.add(im.quantity, p.quantity);
  829. im.total_price = this.ctx.helper.add(im.total_price, p.total_price);
  830. im.jl = this.ctx.helper.add(im.jl, p.gather_qty);
  831. im.contract_jl = this.ctx.helper.add(im.contract_jl, p.contract_qty);
  832. im.qc_jl = this.ctx.helper.add(im.qc_jl, p.qc_qty);
  833. im.tp = this.ctx.helper.add(im.tp, p.gather_tp);
  834. im.contract_tp = this.ctx.helper.add(im.contract_tp, p.contract_tp);
  835. im.qc_tp = this.ctx.helper.add(im.qc_tp, p.qc_tp);
  836. im.pre_jl = this.ctx.helper.add(im.pre_jl, p.pre_gather_qty);
  837. im.pre_contract_jl = this.ctx.helper.add(im.pre_contract_jl, p.pre_contract_qty);
  838. im.pre_qc_jl = this.ctx.helper.add(im.pre_qc_jl, p.pre_qc_qty);
  839. im.pre_tp = this.ctx.helper.add(im.pre_tp, p.pre_gather_tp);
  840. im.pre_contract_tp = this.ctx.helper.add(im.pre_contract_tp, p.pre_contract_tp);
  841. im.pre_qc_tp = this.ctx.helper.add(im.pre_qc_tp, p.pre_qc_tp);
  842. im.end_jl = this.ctx.helper.add(im.end_jl, p.end_gather_qty);
  843. im.end_contract_jl = this.ctx.helper.add(im.end_contract_jl, p.end_contract_qty);
  844. im.end_qc_jl = this.ctx.helper.add(im.end_qc_jl, p.end_qc_qty);
  845. im.end_tp = this.ctx.helper.add(im.end_tp, p.end_gather_tp);
  846. im.end_contract_tp = this.ctx.helper.add(im.end_contract_tp, p.end_contract_tp);
  847. im.end_qc_tp = this.ctx.helper.add(im.end_qc_tp, p.end_qc_tp);
  848. }
  849. }
  850. _generateBwImData(node) {
  851. const tp_decimal = this.ctx.tender.info.decimal.tp;
  852. const posterity = this.billsTree.getPosterity(node);
  853. for (const p of posterity) {
  854. if (p.children && p.children.length > 0) { continue; }
  855. if (!p.b_code || p.b_code === '') { continue; }
  856. const peg = this._getPegNode(node);
  857. const pPos = this.pos.getLedgerPos(p.id);
  858. const bw = this._getZlNormalBw(node, peg);
  859. if (pPos && pPos.length > 0) {
  860. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  861. for (const pp of pPos) {
  862. if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue; }
  863. const im = {
  864. id: this.ImData.length + 1,
  865. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, org_unit_price: p.org_unit_price, pid: pp.id,
  866. quantity: pp.quantity,
  867. jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
  868. pre_jl: pp.pre_gather_qty, pre_contract_jl: pp.pre_contract_qty, pre_qc_jl: pp.pre_qc_qty,
  869. end_jl: pp.end_gather_qty, end_contract_jl: pp.end_contract_qty, end_qc_jl: pp.end_qc_qty,
  870. bw,
  871. peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
  872. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  873. xm: pp.name, jldy: pp.name,
  874. drawing_code: pp.drawing_code,
  875. changes: [],
  876. position: pp.position,
  877. lIndex: nodeIndex * 1000 + pp.porder,
  878. };
  879. im.total_price = this.ctx.helper.mul(im.quantity, im.unit_price, tp_decimal);
  880. im.tp = this.ctx.helper.mul(im.jl, im.unit_price, tp_decimal);
  881. im.contract_tp = this.ctx.helper.mul(im.contract_jl, im.unit_price, tp_decimal);
  882. im.qc_tp = this.ctx.helper.mul(im.qc_jl, im.unit_price, tp_decimal);
  883. im.pre_tp = this.ctx.helper.mul(im.pre_jl, im.unit_price, tp_decimal);
  884. im.pre_contract_tp = this.ctx.helper.mul(im.pre_contract_jl, im.unit_price, tp_decimal);
  885. im.pre_qc_tp = this.ctx.helper.mul(im.pre_qc_jl, im.unit_price, tp_decimal);
  886. im.end_tp = this.ctx.helper.mul(im.end_jl, im.unit_price, tp_decimal);
  887. im.end_contract_tp = this.ctx.helper.mul(im.end_contract_jl, im.unit_price, tp_decimal);
  888. im.end_qc_tp = this.ctx.helper.mul(im.end_qc_jl, im.unit_price, tp_decimal);
  889. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  890. this.ImData.push(im);
  891. if (pp.qc_qty && pp.qc_qty !== 0) {
  892. for (const c of this.changes) {
  893. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  894. im.changes.push(c);
  895. }
  896. }
  897. }
  898. }
  899. } else {
  900. if (this.ctx.helper.checkZero(p.gather_qty) && this.ctx.helper.checkZero(p.gather_tp)) { continue; }
  901. const im = {
  902. id: this.ImData.length + 1,
  903. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, org_unit_price: p.org_unit_price, pid: '',
  904. quantity: p.quantity, total_price: p.total_price,
  905. jl: p.gather_qty, contract_jl: p.contract_qty, qc_jl: p.qc_qty,
  906. pre_jl: p.pre_gather_qty, pre_contract_jl: p.pre_contract_qty, pre_qc_jl: p.pre_qc_qty,
  907. end_jl: p.end_gather_qty, end_contract_jl: p.end_contract_qty, end_qc_jl: p.end_qc_qty,
  908. tp: p.gather_tp, contract_tp: p.contract_tp, qc_tp: p.qc_tp,
  909. pre_tp: p.pre_gather_tp, pre_contract_tp: p.pre_contract_tp, pre_qc_tp: p.pre_qc_tp,
  910. end_tp: p.end_gather_tp, end_contract_tp: p.end_contract_tp, end_qc_tp: p.end_qc_tp,
  911. bw,
  912. peg: peg ? this._getPegStr(peg.name) : '',
  913. dwgc: this._getDwgc(peg, node), fbgc: this._getFbgc(peg, node), fxgc: this._getFxgc(peg, node),
  914. xm: node.name,
  915. drawing_code: this._getDrawingCode(p),
  916. changes: [],
  917. position: '',
  918. lIndex: this.billsTree.getNodeSerialNo(node),
  919. };
  920. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  921. this.ImData.push(im);
  922. if (p.qc_qty && p.qc_qty !== 0) {
  923. for (const c of this.changes) {
  924. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  925. im.changes.push(c);
  926. }
  927. }
  928. }
  929. }
  930. }
  931. }
  932. /**
  933. * 递归 生成中间计量表
  934. * @param {Array} nodes
  935. */
  936. _recursiveBuildImData(nodes) {
  937. if (!nodes || nodes.length === 0) { return; }
  938. for (const node of nodes) {
  939. if (this.billsTree.isLeafXmj(node) ||
  940. ((this.stage.im_type !== imType.bw.value && this.stage.im_type !== imType.bb.value) && this.stage.im_gather && node.check)
  941. ) {
  942. if (this.stage.im_type === imType.tz.value) {
  943. this._generateTzImData(node);
  944. } else if (this.stage.im_type === imType.zl.value) {
  945. this._generateZlImData(node);
  946. } else if (this.stage.im_type === imType.bw.value) {
  947. this._generateBwImData(node);
  948. } else if (this.stage.im_type === imType.bb.value) {
  949. this._generateBwBillsImData(node);
  950. }
  951. } else {
  952. this._recursiveBuildImData(node.children);
  953. }
  954. }
  955. }
  956. // 生成中间计量数据
  957. async buildImData() {
  958. this.up_field = 'unit_price';
  959. const self = this;
  960. this.stage = this.ctx.stage;
  961. // 初始化
  962. await this._loadMainData();
  963. await this._loadRelaData();
  964. if (this.stage.im_gather) {
  965. this._checkGather();
  966. }
  967. // 生成数据
  968. this._recursiveBuildImData(this.billsTree.children);
  969. // 排序
  970. if (this.stage.im_type !== imType.tz.value) {
  971. this.ImData.sort(function(x, y) {
  972. const iCode = self.ctx.helper.compareCode(x.code, y.code);
  973. return iCode === 0 ? x.lIndex - y.lIndex : iCode;
  974. });
  975. }
  976. // 生成数据(需要缓存,并清理缓存)
  977. const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
  978. for (const [i, im] of this.ImData.entries()) {
  979. im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + this.stage.im_start_num, 3);
  980. if (im.gclBills) {
  981. for (const b of im.gclBills) {
  982. b.im_code = im.im_code;
  983. }
  984. }
  985. this._getCalcMemo(im);
  986. this._checkCustomDetail(im);
  987. delete im.leafXmjs;
  988. delete im.gclBills;
  989. this._getChangeInfo(im);
  990. delete im.changes;
  991. }
  992. }
  993. buildRelaStageIm(stage, billsTree, pos, details, changes) {
  994. this.up_field = 'org_unit_price';
  995. this.billsTree = billsTree;
  996. this.pos = pos;
  997. this.stage = stage;
  998. this.details = details;
  999. this.changes = changes;
  1000. const self = this;
  1001. // 初始化
  1002. if (stage.im_gather) this._checkGather();
  1003. // 生成数据
  1004. this._recursiveBuildImData(this.billsTree.children);
  1005. // 排序
  1006. if (this.stage.im_type !== imType.tz.value) {
  1007. this.ImData.sort(function(x, y) {
  1008. const iCode = self.ctx.helper.compareCode(x.code, y.code);
  1009. return iCode === 0 ? x.lIndex - y.lIndex : iCode;
  1010. });
  1011. }
  1012. // 生成数据(需要缓存,并清理缓存)
  1013. const pre = (this.stage.im_pre && this.stage.im_pre !== '') ? this.stage.im_pre + this.splitChar : '';
  1014. for (const [i, im] of this.ImData.entries()) {
  1015. im.im_code = pre + this._getNumberFormat(this.stage.order, 2) + this.splitChar + this._getNumberFormat(i + this.stage.im_start_num, 3);
  1016. if (im.gclBills) {
  1017. for (const b of im.gclBills) {
  1018. b.im_code = im.im_code;
  1019. }
  1020. }
  1021. this._getCalcMemo(im);
  1022. this._checkCustomDetail(im);
  1023. delete im.leafXmjs;
  1024. delete im.gclBills;
  1025. this._getChangeInfo(im);
  1026. delete im.changes;
  1027. }
  1028. }
  1029. }
  1030. module.exports = StageIm;