stage_im.js 49 KB

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