stage_im.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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_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.getAuditorStageData(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.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  75. this.ctx.helper.assignRelaData(billsData, [
  76. { data: curStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  77. ]);
  78. }
  79. const preStage = this.ctx.stage.order > 1 ? await this.ctx.service.stageBillsFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  80. this.ctx.helper.assignRelaData(billsData, [
  81. { data: preStage, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: 'pre_', relaId: 'lid' },
  82. ]);
  83. this.billsTree.loadDatas(billsData);
  84. this.billsTree.calculateAll();
  85. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  86. if (this.ctx.stage.readOnly) {
  87. const curPosStage = await this.ctx.service.stagePos.getAuditorStageData2(this.ctx.tender.id,
  88. this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  89. this.ctx.helper.assignRelaData(posData, [
  90. { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  91. ]);
  92. } else {
  93. const curPosStage = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id);
  94. this.ctx.helper.assignRelaData(posData, [
  95. { data: curPosStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  96. ]);
  97. }
  98. const prePosStage = this.ctx.stage.order > 1 ? await this.ctx.service.stagePosFinal.getFinalData(this.ctx.tender, this.ctx.stage.order - 1) : [];
  99. this.ctx.helper.assignRelaData(posData, [
  100. { data: prePosStage, fields: ['contract_qty', 'qc_qty'], prefix: 'pre_', relaId: 'pid' },
  101. ]);
  102. this.pos.loadDatas(posData);
  103. this.pos.calculateAll();
  104. }
  105. async _loadRelaData() {
  106. if (this.ctx.stage.readOnly) {
  107. this.details = await this.ctx.service.stageDetail.getAuditorStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  108. } else {
  109. this.details = await this.ctx.service.stageDetail.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id);
  110. }
  111. if (this.ctx.stage.readOnly) {
  112. this.changes = await this.ctx.service.stageChange.getAuditorAllStageData(this.ctx.tender.id, this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder);
  113. } else {
  114. this.changes = await this.ctx.service.stageChange.getLastestAllStageData(this.ctx.tender.id, this.ctx.stage.id);
  115. }
  116. }
  117. // 检查汇总节点
  118. async _checkGather() {
  119. const gatherNodes = this.ctx.stage.im_gather_node ? this.ctx.stage.im_gather_node.split(',') : [];
  120. for (const node of this.billsTree.datas) {
  121. node.check = gatherNodes.indexOf(node.id) !== -1;
  122. }
  123. }
  124. // 辅助取值方法
  125. _getNumberFormat(num, length) {
  126. let s = '0000000000';
  127. s = s + num;
  128. return s.substr(s.length - length);
  129. }
  130. _getNodeByLevel(node, level) {
  131. let cur = node;
  132. while (cur && cur.level > level) {
  133. cur = this.billsTree.getParent(cur);
  134. }
  135. return cur;
  136. }
  137. _checkPeg(text) {
  138. const pegReg = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  139. return pegReg.test(text);
  140. }
  141. _getPegStr(text) {
  142. const pegReg1 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?[~~—][a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  143. const result1 = text.match(pegReg1);
  144. if (result1) {
  145. return result1[0];
  146. }
  147. const pegReg2 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?-[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  148. const result2 = text.match(pegReg2);
  149. if (result2) {
  150. return result2[0];
  151. }
  152. const pegReg3 = /[a-zA-Z]?[kK][0-9]+[++][0-9]{3}([.][0-9]+)?/;
  153. const result3 = text.match(pegReg3);
  154. return result3 ? result3[0] : '';
  155. }
  156. _getPegNode(node) {
  157. if (node) {
  158. if (this._checkPeg(node.name)) {
  159. return node;
  160. }
  161. const parent = this.billsTree.getParent(node);
  162. return parent ? this._getPegNode(parent) : null;
  163. }
  164. }
  165. _getDrawingCode(node) {
  166. if (!node) {
  167. return '';
  168. } else if (node.drawing_code) {
  169. return node.drawing_code;
  170. }
  171. const parent = this.billsTree.getParent(node);
  172. return parent ? this._getDrawingCode(parent) : '';
  173. }
  174. _getZlNormalBw(node, peg) {
  175. if (peg) {
  176. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  177. const subPeg2 = this._getNodeByLevel(node, peg.level + 2);
  178. let result = peg.name;
  179. if (subPeg1 && subPeg1.id !== peg.id && subPeg1.id !== node.id) {
  180. result = result + mergeChar + subPeg1.name;
  181. if (subPeg2 && subPeg2.id !== subPeg1.id && subPeg2.id !== node.id) {
  182. result = result + mergeChar + subPeg2.name;
  183. }
  184. }
  185. return result;
  186. }
  187. if (node.level === 2 || node.level === 3) {
  188. return node.name;
  189. } else if (node.level >= 4) {
  190. let parent = this.billsTree.getParent(node),
  191. result = parent.name;
  192. while (parent.level > 3 && parent) {
  193. parent = this._getNodeByLevel(node, parent.level - 1);
  194. result = parent.name + mergeChar + result;
  195. }
  196. return result;
  197. }
  198. return '';
  199. }
  200. _getZlGatherBw(node, peg) {
  201. if (peg) {
  202. const subPeg1 = this._getNodeByLevel(node, peg.level + 1);
  203. let result = peg.name;
  204. if (subPeg1 && subPeg1.id !== peg.id) {
  205. result = result + mergeChar + subPeg1.name;
  206. }
  207. return result;
  208. }
  209. if (node.level < 3) {
  210. return node.name;
  211. }
  212. let parent = node,
  213. result = parent.name;
  214. while (parent.level > 3 && parent) {
  215. parent = this._getNodeByLevel(node, parent.level - 1);
  216. result = parent.name + mergeChar + result;
  217. }
  218. return result;
  219. }
  220. _getBwBillsBw(node) {
  221. if (node.level < 3) {
  222. return '';
  223. } else {
  224. let parent = node, result = parent.name;
  225. while (parent.level > 3 && parent) {
  226. parent = this._getNodeByLevel(node, parent.level - 1);
  227. result = parent.name + mergeChar + result;
  228. }
  229. return result;
  230. }
  231. }
  232. _checkCustomDetail(im) {
  233. const self = this;
  234. const cd = this._.find(this.details, function(d) {
  235. return im.lid === d.lid &&
  236. (!im.code || im.code === d.code) &&
  237. (!im.name || im.name === d.name) &&
  238. (!im.unit || im.unit === d.unit) &&
  239. self.ctx.helper.checkZero(self.ctx.helper.sub(im.unit_price, d.unit_price)) &&
  240. (!im.pid || im.pid === d.pid) &&
  241. (!im.pos_name || im.pos_name === d.pos_name);
  242. });
  243. if (cd) {
  244. im.custom_define = cd.custom_define ? cd.custom_define.split(',') : this.imFields;
  245. this._.assignInWith(im, cd, function(oV, sV, key) {
  246. return im.custom_define.indexOf(key) > -1 && sV !== undefined && sV !== null ? sV : oV;
  247. });
  248. }
  249. }
  250. _getCalcMemo(im) {
  251. if (im.gclBills && im.gclBills.length > 0) {
  252. const self = this;
  253. im.gclBills.sort(function (x, y) {
  254. return self.ctx.helper.compareCode(x.b_code, y.b_code);
  255. });
  256. for (const b of im.gclBills) {
  257. b.imid = im.id;
  258. this.ImBillsData.push(b);
  259. }
  260. }
  261. if (im.calc_memo !== undefined && im.calc_memo !== null && im.calc_memo !== '') return;
  262. if (im.leafXmjs && im.leafXmjs.length > 0) {
  263. const memo = ['本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit];
  264. for (const lx of im.leafXmjs) {
  265. for (const p of lx.pos) {
  266. memo.push(p.name + ':' + p.jl + ' ' + im.unit);
  267. }
  268. }
  269. im.calc_memo = memo.join('\n');
  270. } else if (im.gclBills && im.gclBills.length > 0) {
  271. const memo = [];
  272. for (const [i, b] of im.gclBills.entries()) {
  273. if (b.pos && b.pos.length > 0) {
  274. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name);
  275. for (const p of b.pos) {
  276. memo.push(p.name + ':' + p.jl + ' ' + b.unit);
  277. }
  278. } else {
  279. memo.push('清单' + (i + 1) + ':' + b.b_code + ' ' + b.name + ':' + (this.ctx.helper.checkZero(b.jl) ? 0 : b.jl) + ' ' + b.unit);
  280. }
  281. }
  282. im.calc_memo = memo.join('\n');
  283. } else {
  284. im.calc_memo = '';
  285. }
  286. }
  287. _getChangeInfo(im) {
  288. if (im.changes && im.changes.length > 0) {
  289. const code = this._.uniq(this._.map(im.changes, 'c_code'));
  290. if (!im.bgl_code || im.bgl_code === '') im.bgl_code = code.join(';');
  291. const new_code = this._.uniq(this._.map(im.changes, 'c_new_code'));
  292. if (!im.bgl_drawing_code || im.bgl_drawing_code === '') im.bgl_drawing_code = new_code.join(';');
  293. }
  294. }
  295. _generateTzPosData(node, gclBills) {
  296. if (!gclBills.pos) {
  297. gclBills.pos = [];
  298. }
  299. const posRange = this.pos.getLedgerPos(node.id);
  300. if (!posRange) { return; }
  301. for (const p of posRange) {
  302. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  303. let lp = this._.find(gclBills.pos, { name: p.name });
  304. if (!lp) {
  305. lp = { name: p.name };
  306. gclBills.pos.push(lp);
  307. }
  308. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  309. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  310. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  311. }
  312. }
  313. _recursiveGenerateTzGclBills(node, im) {
  314. for (const p of node.children) {
  315. if (p.children && p.children.length > 0) {
  316. this._recursiveGenerateTzGclBills(p, im);
  317. } else {
  318. if ((!p.b_code || p.b_code === '') || (p.children && p.children.length > 0)) {
  319. continue;
  320. }
  321. if ((!p.contract_tp || p.contract_tp === 0) && (!p.qc_tp || p.qc_tp === 0)) {
  322. continue;
  323. }
  324. let b = this._.find(im.gclBills, { bid: p.id });
  325. if (!b) {
  326. b = { imid: im.id, bid: p.id, b_code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price };
  327. im.gclBills.push(b);
  328. }
  329. b.quantity = this.ctx.helper.add(b.quantity, p.quantity);
  330. b.total_price = this.ctx.helper.add(b.total_price, p.total_price);
  331. b.jl = this.ctx.helper.add(b.jl, p.gather_qty);
  332. b.contract_jl = this.ctx.helper.add(b.contract_jl, p.contract_qty);
  333. b.qc_jl = this.ctx.helper.add(b.qc_jl, p.qc_qty);
  334. b.tp = this.ctx.helper.add(b.tp, p.gather_tp);
  335. b.contract_tp = this.ctx.helper.add(b.contract_tp, p.contract_tp);
  336. b.qc_tp = this.ctx.helper.add(b.qc_tp, p.qc_tp);
  337. b.pre_jl = this.ctx.helper.add(b.pre_jl, p.pre_gather_qty);
  338. b.pre_contract_jl = this.ctx.helper.add(b.pre_contract_jl, p.pre_contract_qty);
  339. b.pre_qc_jl = this.ctx.helper.add(b.pre_qc_jl, p.pre_qc_qty);
  340. b.pre_tp = this.ctx.helper.add(b.pre_tp, p.pre_gather_tp);
  341. b.pre_contract_tp = this.ctx.helper.add(b.pre_contract_tp, p.pre_contract_tp);
  342. b.pre_qc_tp = this.ctx.helper.add(b.pre_qc_tp, p.pre_qc_tp);
  343. b.end_jl = this.ctx.helper.add(b.end_jl, p.end_gather_qty);
  344. b.end_contract_jl = this.ctx.helper.add(b.end_contract_jl, p.end_contract_qty);
  345. b.end_qc_jl = this.ctx.helper.add(b.end_qc_jl, p.end_qc_qty);
  346. b.end_tp = this.ctx.helper.add(b.end_tp, p.end_gather_tp);
  347. b.end_contract_tp = this.ctx.helper.add(b.end_contract_tp, p.end_contract_tp);
  348. b.end_qc_tp = this.ctx.helper.add(b.end_qc_tp, p.end_qc_tp);
  349. this._generateTzPosData(p, b);
  350. }
  351. }
  352. }
  353. _generateTzGclBillsData(node, im) {
  354. if (!im.gclBills) {
  355. im.gclBills = [];
  356. }
  357. this._recursiveGenerateTzGclBills(node, im);
  358. }
  359. _generateTzChangeData(node, im) {
  360. if (!im.changes) {
  361. im.changes = [];
  362. }
  363. const posterity = this.billsTree.getPosterity(node);
  364. for (const p of posterity) {
  365. if (p.children && p.children.length > 0) {
  366. continue;
  367. }
  368. if ((!p.qc_tp || p.qc_tp === 0)) {
  369. continue;
  370. }
  371. const posRange = this.pos.getLedgerPos(p.id);
  372. if (!posRange) {
  373. for (const c of this.changes) {
  374. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  375. im.changes.push(c);
  376. }
  377. }
  378. } else {
  379. for (const pp of posRange) {
  380. if ((!pp.qc_qty || pp.qc_qty === 0)) {
  381. continue;
  382. }
  383. for (const c of this.changes) {
  384. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  385. im.changes.push(c);
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * 生成 0号台账 中间计量数据
  394. * @param {Object} node - 生成中间计量表的节点
  395. */
  396. _generateTzImData(node) {
  397. if (node.gather_tp) {
  398. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  399. const peg = this._getPegNode(node);
  400. const im = {
  401. id: this.ImData.length + 1,
  402. lid: node.id, pid: '', code: node.code,
  403. jl: node.gather_tp, contract_jl: node.contract_tp, qc_jl: node.qc_tp,
  404. pre_jl: node.pre_gather_tp, pre_contract_jl: node.pre_contract_tp, pre_qc_jl: node.pre_qc_tp,
  405. end_jl: node.end_gather_tp, end_contract_jl: node.end_contract_tp, end_qc_jl: node.end_qc_tp,
  406. peg: peg ? this._getPegStr(peg.name) : '', drawing_code: this._getDrawingCode(node),
  407. position: '',
  408. lIndex: nodeIndex,
  409. };
  410. if (this.ctx.stage.im_gather && node.check) {
  411. im.bw = this._getZlGatherBw(node, peg);
  412. im.xm = '';
  413. } else {
  414. im.bw = this._getZlNormalBw(node, peg);
  415. im.xm = node.name;
  416. }
  417. this._checkCustomDetail(im);
  418. im.check = this.ctx.stage.im_gather && node.check;
  419. this._generateTzGclBillsData(node, im);
  420. this.ImData.push(im);
  421. this._generateTzChangeData(node, im);
  422. }
  423. }
  424. _addBwBillsGclBills(im, bills, pos) {
  425. const helper = this.ctx.helper;
  426. const tp_decimal = this.ctx.tender.info.decimal.tp;
  427. if (!im.gclBills) im.gclBills = [];
  428. let gcl = im.gclBills.find(function (x) {
  429. return (!bills.b_code || bills.b_code === x.b_code) &&
  430. (!bills.name || bills.name === x.name) &&
  431. (!bills.unit || bills.unit === x.unit) &&
  432. helper.checkZero(helper.sub(bills.unit_price, x.unit_price));
  433. });
  434. if (!gcl) {
  435. gcl = {b_code: bills.b_code, name: bills.name, unit: bills.unit, unit_price: bills.unit_price};
  436. im.gclBills.push(gcl);
  437. this.ImBillsData.push(gcl);
  438. }
  439. if (pos) {
  440. gcl.quantity = helper.add(gcl.quantity, pos.quantity);
  441. gcl.total_price = helper.mul(gcl.unit_price, gcl.quantity, tp_decimal);
  442. gcl.jl = helper.add(gcl.jl, pos.gather_qty);
  443. gcl.contract_jl = helper.add(gcl.contract_jl, pos.contract_qty);
  444. gcl.qc_jl = helper.add(gcl.qc_jl, pos.qc_qty);
  445. gcl.tp = helper.mul(gcl.unit_price, gcl.jl, tp_decimal);
  446. gcl.contract_tp = helper.mul(gcl.unit_price, gcl.contract_jl, tp_decimal);
  447. gcl.qc_tp = helper.mul(gcl.unit_price, gcl.qc_jl, tp_decimal);
  448. gcl.pre_jl = helper.add(gcl.pre_jl, pos.pre_gather_qty);
  449. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, pos.pre_contract_qty);
  450. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, pos.pre_qc_qty);
  451. gcl.pre_tp = helper.mul(gcl.unit_price, gcl.pre_jl, tp_decimal);
  452. gcl.pre_contract_tp = helper.mul(gcl.unit_price, gcl.pre_contract_jl, tp_decimal);
  453. gcl.pre_qc_tp = helper.mul(gcl.unit_price, gcl.pre_qc_jl, tp_decimal);
  454. gcl.end_jl = helper.add(gcl.end_jl, pos.end_gather_qty);
  455. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, pos.end_contract_qty);
  456. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, pos.end_qc_qty);
  457. gcl.end_tp = helper.mul(gcl.unit_price, gcl.end_jl, tp_decimal);
  458. gcl.end_contract_tp = helper.mul(gcl.unit_price, gcl.end_contract_jl, tp_decimal);
  459. gcl.end_qc_tp = helper.mul(gcl.unit_price, gcl.end_qc_jl, tp_decimal);
  460. } else {
  461. gcl.quantity = helper.add(gcl.quantity, bills.quantity);
  462. gcl.total_price = helper.add(gcl.total_price, bills.total_price);
  463. gcl.jl = helper.add(gcl.jl, bills.gather_qty);
  464. gcl.contract_jl = helper.add(gcl.contract_jl, bills.contract_qty);
  465. gcl.qc_jl = helper.add(gcl.qc_jl, bills.qc_qty);
  466. gcl.tp = helper.add(gcl.tp, bills.gather_tp);
  467. gcl.contract_tp = helper.add(gcl.contract_tp, bills.contract_tp);
  468. gcl.qc_tp = helper.add(gcl.qc_tp, bills.qc_tp);
  469. gcl.pre_jl = helper.add(gcl.pre_jl, bills.pre_gather_qty);
  470. gcl.pre_contract_jl = helper.add(gcl.pre_contract_jl, bills.pre_contract_qty);
  471. gcl.pre_qc_jl = helper.add(gcl.pre_qc_jl, bills.pre_qc_qty);
  472. gcl.pre_tp = helper.add(gcl.pre_tp, bills.pre_gather_tp);
  473. gcl.pre_contract_tp = helper.add(gcl.pre_contract_tp, bills.pre_contract_tp);
  474. gcl.pre_qc_tp = helper.add(gcl.pre_qc_tp, bills.pre_qc_tp);
  475. gcl.end_jl = helper.add(gcl.end_jl, bills.end_gather_qty);
  476. gcl.end_contract_jl = helper.add(gcl.end_contract_jl, bills.end_contract_qty);
  477. gcl.end_qc_jl = helper.add(gcl.end_qc_jl, bills.end_qc_qty);
  478. gcl.end_tp = helper.add(gcl.end_tp, bills.end_gather_tp);
  479. gcl.end_contract_tp = helper.add(gcl.end_contract_tp, bills.end_contract_tp);
  480. gcl.end_qc_tp = helper.add(gcl.end_qc_tp, bills.end_qc_tp);
  481. }
  482. }
  483. _calculateBwBillsIm(im) {
  484. const tp_decimal = this.ctx.tender.info.decimal.tp;
  485. im.contract_jl = 0;
  486. im.qc_jl = 0;
  487. for (const b of im.gclBills) {
  488. im.contract_jl = this.ctx.helper.add(im.contract_jl, this.ctx.helper.mul(b.contract_jl, b.unit_price, tp_decimal));
  489. im.qc_jl = this.ctx.helper.add(im.qc_jl, this.ctx.helper.mul(b.qc_jl, b.unit_price, tp_decimal));
  490. }
  491. im.jl = this.ctx.helper.add(im.contract_jl, im.qc_jl);
  492. }
  493. _getBwBillsIm(node, peg, index, bw) {
  494. const im = {
  495. lid: node.id, pid: '', code: node.code, name: node.name, pos_name: '',
  496. peg: peg ? this._getPegStr(peg.name) : '',
  497. drawing_code: this._getDrawingCode(node),
  498. position: '',
  499. lIndex: index,
  500. bw: bw, jldy: node.name,
  501. changes: [], gclBills: [],
  502. };
  503. this._checkCustomDetail(im);
  504. return im;
  505. }
  506. _getBwBillsPosIm(imArr, node, peg, index, bw, posName) {
  507. let im = imArr.find(function (d) {
  508. return d.lid === node.id && d.pos_name === posName;
  509. });
  510. if (!im) {
  511. im = {
  512. lid: node.id, pid: '',
  513. code: node.code + '-' + (imArr.length + 1), name: node.name, pos_name: posName,
  514. peg: this._checkPeg(posName) ? posName : (peg ? this._getPegStr(peg.name) : ''),
  515. drawing_code: [], position: [],
  516. lIndex: index,
  517. bw: bw, jldy: posName,
  518. changes: [], gclBills: [],
  519. };
  520. imArr.push(im);
  521. this._checkCustomDetail(im);
  522. }
  523. return im;
  524. }
  525. _generateBwBillsImData (node) {
  526. if (!node.gather_tp && !node.contract_tp && !node.qc_tp) return;
  527. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  528. const peg = this._getPegNode(node);
  529. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  530. const bw = this._getBwBillsBw(node);
  531. const imDefault = this._getBwBillsIm(node, peg, nodeIndex, bw);
  532. for (const p of posterity) {
  533. if (p.children && p.children.length > 0) continue;
  534. if (!p.b_code || p.b_code === '') continue;
  535. if (!p.gather_tp || p.gather_tp === 0) continue;
  536. const pPos = this.pos.getLedgerPos(p.id);
  537. if (pPos && pPos.length > 0) {
  538. for (const pp of pPos) {
  539. if (!pp.gather_qty && !pp.contract_qty && !pp.qc_qty) continue;
  540. const im = this._getBwBillsPosIm(nodeImData, node, peg, nodeIndex, bw, pp.name);
  541. this._addBwBillsGclBills(im, p, pp);
  542. for (const c of this.changes) {
  543. if (c.lid === p.id && c.pid == pp.id && c.qty && c.qty !== 0) {
  544. im.changes.push(c);
  545. }
  546. }
  547. if (pp.drawing_code && im.drawing_code instanceof Array) im.drawing_code.push(pp.drawing_code);
  548. if (pp.position && im.position instanceof Array) im.position.push(pp.position);
  549. }
  550. } else {
  551. for (const c of this.changes) {
  552. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  553. imDefault.changes.push(c);
  554. }
  555. }
  556. imDefault.contract_jl = this.ctx.helper.add(imDefault.contract_jl, p.contract_qty);
  557. imDefault.qc_jl = this.ctx.helper.add(imDefault.qc_jl, p.qc_qty);
  558. this._addBwBillsGclBills(imDefault, p);
  559. }
  560. }
  561. if (imDefault.contract_jl || imDefault.qc_jl) {
  562. imDefault.jl = this.ctx.helper.add(imDefault.contract_jl, imDefault.qc_jl);
  563. imDefault.id = this.ImData.length + 1;
  564. this.ImData.push(imDefault);
  565. }
  566. for (const im of nodeImData) {
  567. this._calculateBwBillsIm(im);
  568. im.drawing_code = this.ctx.helper._.uniq(im.drawing_code).join(mergeChar);
  569. im.position = this.ctx.helper._.uniq(im.position).join(mergeChar);
  570. im.id = this.ImData.length + 1;
  571. this.ImData.push(im);
  572. }
  573. }
  574. _generateZlPosData(node, lx) {
  575. if (!lx.pos) {
  576. lx.pos = [];
  577. }
  578. const posRange = this.pos.getLedgerPos(node.id);
  579. if (!posRange) { return; }
  580. for (const p of posRange) {
  581. if (!p.gather_qty || this.ctx.helper.checkZero(p.gather_qty)) { continue; }
  582. let lp = this._.find(lx.pos, { name: p.name });
  583. if (!lp) {
  584. lp = { name: p.name };
  585. lx.pos.push(lp);
  586. }
  587. lp.jl = this.ctx.helper.add(lp.jl, p.gather_qty);
  588. lp.contract_jl = this.ctx.helper.add(lp.contract_jl, p.contract_qty);
  589. lp.qc_jl = this.ctx.helper.add(lp.qc_jl, p.qc_qty);
  590. }
  591. }
  592. _generateZlLeafXmjData(node, im) {
  593. if (!im.leafXmjs) {
  594. im.leafXmjs = [];
  595. }
  596. const leafXmj = this.billsTree.getLeafXmjParent(node);
  597. if (!leafXmj) { return; }
  598. let lx = this._.find(im.leafXmjs, { lxid: leafXmj.id });
  599. if (!lx) {
  600. lx = {
  601. lxid: leafXmj.id,
  602. code: leafXmj.code,
  603. name: leafXmj.name,
  604. };
  605. im.leafXmjs.push(lx);
  606. }
  607. lx.jl = this.ctx.helper.add(lx.jl, node.gather_qty);
  608. lx.contract_jl = this.ctx.helper.add(lx.contract_jl, node.contract_qty);
  609. lx.qc_jl = this.ctx.helper.add(lx.qc_jl, node.qc_qty);
  610. this._generateZlPosData(node, lx);
  611. }
  612. _generateZlChangeData(node, im) {
  613. if (!im.changes) {
  614. im.changes = [];
  615. }
  616. if ((!node.qc_qty || node.qc_qty === 0)) {
  617. return;
  618. }
  619. const posRange = this.pos.getLedgerPos(node.id);
  620. if (!posRange) {
  621. for (const c of this.changes) {
  622. if (c.lid === node.id && c.pid == -1 && c.qty && c.qty !== 0) {
  623. im.changes.push(c);
  624. }
  625. }
  626. } else {
  627. for (const p of posRange) {
  628. if ((!p.qc_qty || p.qc_qty === 0)) {
  629. continue;
  630. }
  631. for (const c of this.changes) {
  632. if (c.lid === node.id && c.pid === p.id && c.qty && c.qty !== 0) {
  633. im.changes.push(c);
  634. }
  635. }
  636. }
  637. }
  638. }
  639. /**
  640. * 生成 总量控制 中间计量数据
  641. * @param {Object} node - 生成中间计量表的节点
  642. */
  643. _generateZlImData(node) {
  644. const self = this;
  645. const nodeImData = [], posterity = this.billsTree.getPosterity(node);
  646. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  647. for (const p of posterity) {
  648. if (p.children && p.children.length > 0) { continue; }
  649. if (!p.b_code || p.b_code === '') { continue; }
  650. if (!p.gather_tp || p.gather_tp === 0) { continue; }
  651. // if (this.ctx.helper.checkZero(p.contract_qty) && this.ctx.helper.checkZero(p.qc_qty)) { continue; }
  652. let im = nodeImData.find(function(d) {
  653. return d.lid === node.id &&
  654. d.code === p.b_code && p.name === d.name && p.unit === d.unit &&
  655. self.ctx.helper.checkZero(self.ctx.helper.sub(p.unit_price, d.unit_price));
  656. });
  657. if (!im) {
  658. const peg = this._getPegNode(node);
  659. im = {
  660. id: this.ImData.length + 1,
  661. lid: node.id, pid: '', code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price,
  662. quantity: 0, total_price: 0,
  663. jl: 0, contract_jl: 0, qc_jl: 0,
  664. pre_jl: 0, pre_contract_jl: 0, pre_qc_jl: 0,
  665. end_jl: 0, end_contract_jl: 0, end_qc_jl: 0,
  666. tp: 0, contract_tp: 0, qc_tp: 0,
  667. pre_tp: 0, pre_contract_tp: 0, pre_qc_tp: 0,
  668. end_tp: 0, end_contract_tp: 0, end_qc_tp: 0,
  669. peg: peg ? this._getPegStr(peg.name) : '',
  670. position: '',
  671. lIndex: nodeIndex,
  672. };
  673. if (this.ctx.stage.im_gather && node.check) {
  674. im.check = true;
  675. im.bw = this._getZlGatherBw(node, peg);
  676. im.xm = '';
  677. im.drawing_code = this._getDrawingCode(node);
  678. } else {
  679. im.check = false;
  680. im.bw = this._getZlNormalBw(node, peg);
  681. im.xm = node.name;
  682. im.drawing_code = this._getDrawingCode(p);
  683. }
  684. nodeImData.push(im);
  685. this._checkCustomDetail(im);
  686. this.ImData.push(im);
  687. }
  688. // if (!this.ctx.stage.im_gather || !node.check) {
  689. this._generateZlLeafXmjData(p, im, 'gather_qty');
  690. // }
  691. this._generateZlChangeData(p, im);
  692. im.quantity = this.ctx.helper.add(im.quantity, p.quantity);
  693. im.total_price = this.ctx.helper.add(im.total_price, p.total_price);
  694. im.jl = this.ctx.helper.add(im.jl, p.gather_qty);
  695. im.contract_jl = this.ctx.helper.add(im.contract_jl, p.contract_qty);
  696. im.qc_jl = this.ctx.helper.add(im.qc_jl, p.qc_qty);
  697. im.tp = this.ctx.helper.add(im.tp, p.gather_tp);
  698. im.contract_tp = this.ctx.helper.add(im.contract_tp, p.contract_tp);
  699. im.qc_tp = this.ctx.helper.add(im.qc_tp, p.qc_tp);
  700. im.pre_jl = this.ctx.helper.add(im.pre_jl, p.pre_gather_qty);
  701. im.pre_contract_jl = this.ctx.helper.add(im.pre_contract_jl, p.pre_contract_qty);
  702. im.pre_qc_jl = this.ctx.helper.add(im.pre_qc_jl, p.pre_qc_qty);
  703. im.pre_tp = this.ctx.helper.add(im.pre_tp, p.pre_gather_tp);
  704. im.pre_contract_tp = this.ctx.helper.add(im.pre_contract_tp, p.pre_contract_tp);
  705. im.pre_qc_tp = this.ctx.helper.add(im.pre_qc_tp, p.pre_qc_tp);
  706. im.end_jl = this.ctx.helper.add(im.end_jl, p.end_gather_qty);
  707. im.end_contract_jl = this.ctx.helper.add(im.end_contract_jl, p.end_contract_qty);
  708. im.end_qc_jl = this.ctx.helper.add(im.end_qc_jl, p.end_qc_qty);
  709. im.end_tp = this.ctx.helper.add(im.end_tp, p.end_gather_tp);
  710. im.end_contract_tp = this.ctx.helper.add(im.end_contract_tp, p.end_contract_tp);
  711. im.end_qc_tp = this.ctx.helper.add(im.end_qc_tp, p.end_qc_tp);
  712. }
  713. }
  714. _generateBwImData(node) {
  715. const tp_decimal = this.ctx.tender.info.decimal.tp;
  716. const posterity = this.billsTree.getPosterity(node);
  717. for (const p of posterity) {
  718. if (p.children && p.children.length > 0) { continue; }
  719. if (!p.b_code || p.b_code === '') { continue; }
  720. const peg = this._getPegNode(node);
  721. const pPos = this.pos.getLedgerPos(p.id);
  722. const bw = this._getZlNormalBw(node, peg);
  723. if (pPos && pPos.length > 0) {
  724. const nodeIndex = this.billsTree.getNodeSerialNo(node);
  725. for (const pp of pPos) {
  726. if (this.ctx.helper.checkZero(pp.contract_qty) && this.ctx.helper.checkZero(pp.qc_qty)) { continue; }
  727. const im = {
  728. id: this.ImData.length + 1,
  729. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: pp.id,
  730. quantity: pp.quantity,
  731. jl: pp.gather_qty, contract_jl: pp.contract_qty, qc_jl: pp.qc_qty,
  732. pre_jl: pp.pre_gather_qty, pre_contract_jl: pp.pre_contract_qty, pre_qc_jl: pp.pre_qc_qty,
  733. end_jl: pp.end_gather_qty, end_contract_jl: pp.end_contract_qty, end_qc_jl: pp.end_qc_qty,
  734. bw,
  735. peg: this._checkPeg(pp.name) ? this._getPegStr(pp.name) : (peg ? this._getPegStr(peg.name) : ''),
  736. xm: pp.name, jldy: pp.name,
  737. drawing_code: pp.drawing_code,
  738. changes: [],
  739. position: pp.position,
  740. lIndex: nodeIndex * 1000 + pp.porder,
  741. };
  742. im.total_price = this.ctx.helper.mul(im.quantity, im.unit_price, tp_decimal);
  743. im.tp = this.ctx.helper.mul(im.jl, im.unit_price, tp_decimal);
  744. im.contract_tp = this.ctx.helper.mul(im.contract_jl, im.unit_price, tp_decimal);
  745. im.qc_tp = this.ctx.helper.mul(im.pre_qc_jl, im.unit_price, tp_decimal);
  746. im.pre_tp = this.ctx.helper.mul(im.pre_jl, im.unit_price, tp_decimal);
  747. im.pre_contract_tp = this.ctx.helper.mul(im.pre_contract_jl, im.unit_price, tp_decimal);
  748. im.pre_qc_tp = this.ctx.helper.mul(im.pre_qc_jl, im.unit_price, tp_decimal);
  749. im.end_tp = this.ctx.helper.mul(im.end_jl, im.unit_price, tp_decimal);
  750. im.end_contract_tp = this.ctx.helper.mul(im.end_contract_jl, im.unit_price, tp_decimal);
  751. im.end_qc_tp = this.ctx.helper.mul(im.end_qc_jl, im.unit_price, tp_decimal);
  752. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  753. this._checkCustomDetail(im);
  754. this.ImData.push(im);
  755. if (pp.qc_qty && pp.qc_qty !== 0) {
  756. for (const c of this.changes) {
  757. if (c.lid === p.id && c.pid === pp.id && c.qty && c.qty !== 0) {
  758. im.changes.push(c);
  759. }
  760. }
  761. }
  762. }
  763. } else {
  764. if (this.ctx.helper.checkZero(p.gather_qty) && this.ctx.helper.checkZero(p.gather_tp)) { continue; }
  765. const im = {
  766. id: this.ImData.length + 1,
  767. lid: node.id, code: p.b_code, name: p.name, unit: p.unit, unit_price: p.unit_price, pid: '',
  768. quantity: p.quantity, total_price: p.total_price,
  769. jl: p.gather_qty, contract_jl: p.contract_qty, qc_jl: p.qc_qty,
  770. pre_jl: p.pre_gather_qty, pre_contract_jl: p.pre_contract_qty, pre_qc_jl: p.pre_qc_qty,
  771. end_jl: p.end_gather_qty, end_contract_jl: p.end_contract_qty, end_qc_jl: p.end_qc_qty,
  772. tp: p.gather_tp, contract_tp: p.contract_tp, qc_tp: p.qc_tp,
  773. pre_tp: p.pre_gather_tp, pre_contract_tp: p.pre_contract_tp, pre_qc_tp: p.pre_qc_tp,
  774. end_tp: p.end_gather_tp, end_contract_tp: p.end_contract_tp, end_qc_tp: p.end_qc_tp,
  775. bw,
  776. peg: peg ? this._getPegStr(peg.name) : '',
  777. xm: node.name, jldy: node.name,
  778. drawing_code: this._getDrawingCode(p),
  779. changes: [],
  780. position: '',
  781. lIndex: this.billsTree.getNodeSerialNo(node),
  782. };
  783. im.calc_memo = '本期计量:' + (this.ctx.helper.checkZero(im.jl) ? 0 : im.jl) + ' ' + im.unit;
  784. this._checkCustomDetail(im);
  785. this.ImData.push(im);
  786. if (p.qc_qty && p.qc_qty !== 0) {
  787. for (const c of this.changes) {
  788. if (c.lid === p.id && c.pid == -1 && c.qty && c.qty !== 0) {
  789. im.changes.push(c);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. /**
  797. * 递归 生成中间计量表
  798. * @param {Array} nodes
  799. */
  800. _recursiveBuildImData(nodes) {
  801. const stage = this.ctx.stage;
  802. if (!nodes || nodes.length === 0) { return; }
  803. for (const node of nodes) {
  804. if (this.billsTree.isLeafXmj(node) ||
  805. ((stage.im_type !== imType.bw.value && stage.im_type !== imType.bb.value) && stage.im_gather && node.check)
  806. ) {
  807. if (this.ctx.stage.im_type === imType.tz.value) {
  808. this._generateTzImData(node);
  809. } else if (this.ctx.stage.im_type === imType.zl.value) {
  810. this._generateZlImData(node);
  811. } else if (this.ctx.stage.im_type === imType.bw.value) {
  812. this._generateBwImData(node);
  813. } else if (this.ctx.stage.im_type === imType.bb.value) {
  814. this._generateBwBillsImData(node);
  815. }
  816. } else {
  817. this._recursiveBuildImData(node.children);
  818. }
  819. }
  820. }
  821. // 生成中间计量数据
  822. async buildImData() {
  823. const self = this;
  824. // 初始化
  825. await this._loadMainData();
  826. await this._loadRelaData();
  827. if (this.ctx.stage.im_gather) {
  828. this._checkGather();
  829. }
  830. // 生成数据
  831. this._recursiveBuildImData(this.billsTree.children);
  832. // 排序
  833. if (this.ctx.stage.im_type !== imType.tz.value) {
  834. this.ImData.sort(function(x, y) {
  835. const iCode = self.ctx.helper.compareCode(x.code, y.code);
  836. return iCode === 0 ? x.lIndex - y.lIndex : iCode;
  837. });
  838. }
  839. // 生成数据(需要缓存,并清理缓存)
  840. const pre = (this.ctx.stage.im_pre && this.ctx.stage.im_pre !== '') ? this.ctx.stage.im_pre + this.splitChar : '';
  841. for (const [i, im] of this.ImData.entries()) {
  842. im.im_code = pre + this._getNumberFormat(this.ctx.stage.order, 2) + this.splitChar + this._getNumberFormat(i + 1, 3);
  843. if (im.gclBills) {
  844. for (const b of im.gclBills) {
  845. b.im_code = im.im_code;
  846. }
  847. }
  848. this._getCalcMemo(im);
  849. delete im.leafXmjs;
  850. delete im.gclBills;
  851. this._getChangeInfo(im);
  852. delete im.changes;
  853. }
  854. }
  855. }
  856. module.exports = StageIm;