stage_im.js 52 KB

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