material.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const RptMemBase = require('./base');
  10. const bindData = {
  11. materialGather: ['mem_material_gather_bills', 'mem_material_gather_xmj', 'mem_material_gather_gl'],
  12. };
  13. const materialConst = require('../../const/material');
  14. const Ledger = require('../../lib/ledger');
  15. const billsFields = (function () {
  16. const cur = ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil'];
  17. const pre = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp'];
  18. const end = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp'];
  19. const final = ['final_tp', 'final_ratio'];
  20. const stageDgn = ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'];
  21. const stage = cur.concat(pre, end, final);
  22. const stageEnd = pre.concat(end, final);
  23. const bgl = ['qc_bgl_code'];
  24. const leafXmj = ['leaf_xmj_id'];
  25. return {cur, pre, end, final, stageDgn, stage, stageEnd, bgl, leafXmj};
  26. })();
  27. const posFields = (function () {
  28. const cur = ['contract_qty', 'qc_qty', 'gather_qty', 'postil'];
  29. const pre = ['pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty'];
  30. const end = ['end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  31. const final = ['final_ratio'];
  32. const stage = cur.concat(pre, end, final);
  33. const stageEnd = pre.concat(end, final);
  34. const bgl = ['qc_bgl_code'];
  35. return {cur, pre, end, final, stage, stageEnd, bgl};
  36. })();
  37. class rptMemChange extends RptMemBase {
  38. constructor(ctx) {
  39. super(ctx, bindData);
  40. }
  41. async doCheckMaterial(materialId) {
  42. if (this.ctx.material) return;
  43. this.ctx.material = await this.ctx.service.material.getDataByCondition({ id: materialId });
  44. }
  45. async doCheckTender(tenderId) {
  46. if (this.ctx.tender) return;
  47. this.ctx.tender = { id: tenderId };
  48. this.ctx.tender.data = await this.ctx.service.tender.getTender(tenderId);
  49. this.ctx.tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tenderId);
  50. }
  51. async doBeforeLoadReport(params) {
  52. await this.doCheckMaterial(params.material_id);
  53. await this.doCheckTender(this.ctx.material.tid);
  54. }
  55. _getNewPos(updateFields) {
  56. const helper = this.ctx.helper;
  57. return new Ledger.pos({
  58. id: 'id', ledgerId: 'lid',
  59. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  60. calc: function (p) {
  61. p.pre_gather_qty = helper.add(p.pre_contract_qty, p.pre_qc_qty);
  62. p.gather_qty = helper.add(p.contract_qty, p.qc_qty);
  63. p.gather_minus_qty = helper.add(p.gather_qty, p.qc_minus_qty);
  64. p.end_contract_qty = helper.add(p.pre_contract_qty, p.contract_qty);
  65. p.end_qc_qty = helper.add(p.pre_qc_qty, p.qc_qty);
  66. p.end_gather_qty = helper.add(p.pre_gather_qty, p.gather_qty);
  67. }
  68. });
  69. }
  70. _getNewBillsTree(calcFields) {
  71. return new Ledger.billsTree(this.ctx, {
  72. id: 'ledger_id',
  73. pid: 'ledger_pid',
  74. order: 'order',
  75. level: 'level',
  76. rootId: -1,
  77. keys: ['id', 'tender_id', 'ledger_id'],
  78. stageId: 'id',
  79. calcFields: calcFields || ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  80. calc: function (node, helper) {
  81. if (node.children && node.children.length === 0) {
  82. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  83. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  84. node.gather_minus_qty = helper.add(node.gather_qty, node.qc_minus_qty);
  85. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  86. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  87. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  88. }
  89. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  90. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  91. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  92. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  93. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  94. node.final_tp = helper.add(node.total_price, node.end_qc_tp);
  95. node.final_ratio = helper.mul(helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  96. }
  97. });
  98. }
  99. async getMaterialAuditors() {
  100. const auditors = await this.ctx.service.materialAudit.getFinalAuditGroup(this.ctx.material.id, this.ctx.material.curTimes);
  101. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.material.user_id);
  102. const result = [{
  103. aid: user.id,
  104. name: user.name,
  105. company: user.company,
  106. role: user.role,
  107. mobile: user.mobile,
  108. telephone: user.telephone,
  109. sign_path: user.sign_path,
  110. opinion: user.opinion,
  111. end_time: auditors && auditors.length > 0 ? auditors[0].begin_time : null,
  112. sort: 0,
  113. tp_data: this.ctx.material.tp_data,
  114. }, ...auditors];
  115. return result;
  116. }
  117. async _loadMaterialMonth(material, gl) {
  118. const materialMonth = await this.ctx.service.materialMonth.getAllDataByCondition({
  119. where: { mid: material.id },
  120. orders: [['mb_id', 'asc'], ['yearmonth', 'asc']],
  121. });
  122. const month = this.ctx.helper._.uniq(materialMonth.map(x => { return x.yearmonth; }));
  123. let g;
  124. for (const mm of materialMonth) {
  125. if (!g || g.id !== mm.mb_id) g = gl.find(x => { return mm.mb_id === x.id; });
  126. if (!g.month_msg_tp) g.month_msg_tp = [];
  127. if (!g.month) g.month = month.concat([]);
  128. const index = month.indexOf(mm.yearmonth);
  129. if (index >= 0) g.month_msg_tp[index] = mm.msg_tp;
  130. }
  131. }
  132. _completeMaterialGl(materialGl, decimal) {
  133. const tTypeStr = [], mTypeStr = [];
  134. for (const t of materialConst.t_type) {
  135. tTypeStr[t.value] = t.text;
  136. }
  137. for (const m of materialConst.m_type) {
  138. mTypeStr[m.value] = m.text;
  139. }
  140. for (const gl of materialGl) {
  141. gl.tp = this.ctx.helper.mul(gl.quantity, gl.m_spread, decimal.tp);
  142. gl.t_type_str = tTypeStr[gl.t_type];
  143. gl.m_type_str = mTypeStr[gl.m_type];
  144. gl.end_tp = this.ctx.helper.add(gl.tp, gl.pre_tp);
  145. }
  146. }
  147. async getMaterialGl(fields) {
  148. if (!this.ctx.material) return [];
  149. const materials = await this.ctx.service.material.getAllDataByCondition({
  150. where: {tid: this.ctx.material.tid},
  151. orders: [['order', 'desc']],
  152. });
  153. if (materials.length === 0) return [];
  154. let result, material, decimal;
  155. material = this.ctx.material;
  156. if (materials[0].order === material.order) {
  157. decimal = material.decimal ? JSON.parse(material.decimal) : { qty: 3, up: 3, tp: 2 };
  158. if (material.is_stage_self) {
  159. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, ms.sid, ms.`order` as s_order, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  160. ' msb.quantity, mb.expr,' +
  161. ' mb.basic_price, mb.basic_times, ' +
  162. ' msb.msg_tp, msb.msg_times, msb.msg_spread, mb.m_up_risk, mb.m_down_risk, msb.m_spread, msb.m_tp, mb.pre_tp, msb.m_tax_tp, mb.tax_pre_tp, mb.origin, ' +
  163. ' msb.remark, msb.is_summary, mb.m_tax, mb.in_time, mb.origin' +
  164. ` FROM ${this.ctx.service.materialStageBills.tableName} msb` +
  165. ` LEFT JOIN ${this.ctx.service.materialBills.tableName} mb ON msb.mb_id = mb.id` +
  166. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' ms ON msb.ms_id = ms.id ' +
  167. ` WHERE msb.mid = ?` +
  168. ' ORDER By msb.ms_id, mb.order';
  169. result = await this.ctx.app.mysql.query(sql, [material.id]);
  170. } else {
  171. result = await this.ctx.service.materialBills.getAllDataByCondition({
  172. where: {tid: material.tid}
  173. });
  174. }
  175. } else {
  176. decimal = material.decimal ? JSON.parse(material.decimal) : { tp: 2 };
  177. if (material.is_stage_self) {
  178. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, ms.sid, ms.`order` as s_order, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  179. ' msb.quantity, mbh.expr,' +
  180. ' mb.basic_price, mb.basic_times, ' +
  181. ' msb.msg_tp, msb.msg_times, msb.msg_spread, mbh.m_up_risk, mbh.m_down_risk, msb.m_spread, msb.m_tp, mbh.pre_tp, msb.m_tax_tp, mbh.tax_pre_tp, mbh.origin, ' +
  182. ' msb.remark, msb.is_summary, mbh.m_tax, mb.in_time, mbh.origin' +
  183. ` FROM ${this.ctx.service.materialStageBills.tableName} msb` +
  184. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ON msb.mb_id = mbh.mb_id AND msb.mid = mbh.mid' +
  185. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON msb.mb_id = mb.id ' +
  186. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' ms ON msb.ms_id = ms.id ' +
  187. ' WHERE msb.mid = ?'+
  188. ' ORDER By msb.ms_id, mb.order';
  189. result = await this.ctx.app.mysql.query(sql, [material.id]);
  190. } else {
  191. const sql = 'SELECT mb.id, mb.tid, mb.mid, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  192. ' mbh.quantity, mbh.expr,' +
  193. ' mb.basic_price, mb.basic_times, ' +
  194. ' mbh.msg_tp, mbh.msg_times, mbh.msg_spread, mbh.m_up_risk, mbh.m_down_risk, mbh.m_spread, mbh.m_tp, mbh.pre_tp, mbh.m_tax_tp, mbh.tax_pre_tp, mbh.origin, ' +
  195. ' mb.remark, mb.is_summary, mbh.m_tax, mb.in_time, mbh.origin' +
  196. ' FROM ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ' +
  197. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON mbh.mb_id = mb.id ' +
  198. ' WHERE mbh.tid = ? And mbh.mid = ?'+
  199. ' ORDER By mb.order';
  200. result = await this.ctx.app.mysql.query(sql, [material.tid, material.id]);
  201. }
  202. }
  203. this._completeMaterialGl(result, decimal);
  204. if (this._checkFieldsExist(fields, ['month_msg_tp', 'month'])) await this._loadMaterialMonth(material, result);
  205. return result;
  206. }
  207. async getMaterialGlDetail(fields) {
  208. const material = this.ctx.material;
  209. if (!material) return [];
  210. if (material.is_stage_self) {
  211. return await this.ctx.service.materialList.getMaterialStageData(material.tid, material.id);
  212. } else {
  213. return await this.ctx.service.materialList.getMaterialData(material.tid, material.id);
  214. }
  215. }
  216. async getMaterialBills(fields, showLevel) {
  217. const material = this.ctx.material;
  218. if (!material) return [];
  219. try {
  220. const billsData = await this.ctx.service.ledger.getData(material.tid);
  221. if (this._checkFieldsExist(fields, billsFields.stage)) {
  222. const curStage = await this.ctx.service.stageBills.getStagesData(material.tid, material.stage_id);
  223. this.ctx.helper.assignRelaData(billsData, [
  224. {data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'qc_minus_qty', 'postil'], prefix: '', relaId: 'lid'}
  225. ]);
  226. }
  227. const billsTree = this._getNewBillsTree();
  228. billsTree.loadDatas(billsData);
  229. billsTree.calculateAll();
  230. return showLevel ? billsTree.getDefaultDatasByLevel(this.ctx.tender.rpt_show_level) : billsTree.getDefaultDatas();
  231. } catch(err) {
  232. this.ctx.helper.log(err);
  233. return [];
  234. }
  235. }
  236. async getMaterialPos(fields) {
  237. const material = this.ctx.material;
  238. if (!material) return [];
  239. try {
  240. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: material.tid }});
  241. if (this._checkFieldsExist(fields, posFields.stage)) {
  242. const curPosStage = await this.ctx.service.stagePos.getStagesData(material.tid, material.stage_id);
  243. this.ctx.helper.assignRelaData(posData, [
  244. {data: curPosStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty', 'contract_expr', 'postil'], prefix: '', relaId: 'pid'}
  245. ]);
  246. }
  247. const pos = this._getNewPos();
  248. pos.loadDatas(posData);
  249. pos.calculateAll();
  250. return pos.getDatas();
  251. } catch (err) {
  252. this.ctx.helper.log(err);
  253. return [];
  254. }
  255. }
  256. async getMaterialStage(fields) {
  257. const material = this.ctx.material;
  258. if (!material) return [];
  259. if (material.is_stage_self) {
  260. return await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: material.id } });
  261. } else {
  262. return [{
  263. id: -1, tid: material.id, mid: material.id, sid: material.stage_id, order: material.stage_order,
  264. m_tp: material.m_tp, m_tax_tp: material.m_tax_tp,
  265. }];
  266. }
  267. }
  268. async getMaterialExponent(fields) {
  269. if (!this.ctx.material) return [];
  270. const materials = await this.ctx.service.material.getAllDataByCondition({
  271. where: {tid: this.ctx.material.tid},
  272. orders: [['order', 'desc']],
  273. });
  274. if (materials.length === 0) return [];
  275. let result, material;
  276. material = this.ctx.material;
  277. if (materials[0].order === material.order) {
  278. result = await this.ctx.service.materialExponent.getAllDataByCondition({
  279. where: { tid: material.tid }
  280. });
  281. } else {
  282. const sql = 'SELECT me.id, me.tid, me.mid, me.type, me.symbol, me.symbol_desc, me.code, me.remark, me.in_time,' +
  283. ' meh.type, meh.weight_num, meh.basic_price, meh.basic_times, meh.m_price, meh.calc_num, meh.is_summary' +
  284. ' FROM ' + this.ctx.service.materialExponentHistory.tableName + ' meh ' +
  285. ' LEFT JOIN ' + this.ctx.service.materialExponent.tableName + ' me ON meh.me_id = me.id ' +
  286. ' WHERE meh.tid = ? And meh.mid = ?'+
  287. ' ORDER By me.id';
  288. result = await this.ctx.app.mysql.query(sql, [material.tid, material.id]);
  289. }
  290. return result;
  291. }
  292. getCommonData(params, tableName, fields, customDefine, customSelect) {
  293. switch (tableName) {
  294. case 'mem_project':
  295. return this.ctx.service.project.getDataByCondition({ id: this.ctx.session.sessionProject.id });
  296. case 'mem_tender':
  297. return [this.ctx.tender.data];
  298. case 'mem_tender_info':
  299. return [this.ctx.tender.info];
  300. case 'mem_deal_bills':
  301. return this.ctx.service.dealBills.getAllDataByCondition({ where: { tender_id: this.ctx.tender.id } });
  302. case 'mem_material':
  303. return [this.ctx.material];
  304. case 'mem_material_audit':
  305. return this.getMaterialAuditors();
  306. case 'mem_material_gl':
  307. return this.getMaterialGl(fields);
  308. case 'mem_material_gl_detail':
  309. return this.getMaterialGlDetail(fields);
  310. case 'mem_material_bills':
  311. return this.getMaterialBills(fields);
  312. case 'mem_material_bills_filter':
  313. return this.getMaterialBills(fields, true);
  314. case 'mem_material_pos':
  315. return this.getMaterialPos(fields);
  316. case 'mem_material_stage':
  317. return this.getMaterialStage(fields);
  318. case 'mem_material_exponent':
  319. return this.getMaterialExponent(fields);
  320. default:
  321. return [];
  322. }
  323. }
  324. getMaterialCalcQty(qtySource, info, is_join) {
  325. let qty = 0;
  326. switch(qtySource) {
  327. case materialConst.qty_source_value.gather_qty: qty = info.gather_qty; break;
  328. case materialConst.qty_source_value.contract_qty: qty = info.contract_qty; break;
  329. case materialConst.qty_source_value.gather_minus_qty: qty = info.gather_minus_qty; break;
  330. default: throw '未配置计量来源';
  331. }
  332. if (qtySource !== materialConst.qty_source_value.contract_qty && is_join === 2) {
  333. qty = info.contract_qty;
  334. }
  335. return qty;
  336. }
  337. async _getMaterialStageGatherBills(tender_id, stage_id, stage_order, stageSelf, stageIndex = 0) {
  338. const decimal = this.materialGatherBase.decimal;
  339. const billsData = this.materialGatherBase.billsData;
  340. billsData.forEach(x => {
  341. for (const prop of ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty']) {
  342. x[prop] = undefined;
  343. }
  344. });
  345. const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, stage_id);
  346. this.ctx.helper.assignRelaData(billsData, [
  347. { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'qc_minus_qty'], prefix: '', relaId: 'lid' },
  348. ]);
  349. const billsTree = this._getNewBillsTree();
  350. billsTree.loadDatas(billsData);
  351. billsTree.calculateAll();
  352. const posData = this.materialGatherBase.posData;
  353. posData.forEach(x => {
  354. for (const prop of ['contract_qty', 'qc_qty', 'qc_minus_qty']) {
  355. x[prop] = undefined;
  356. }
  357. });
  358. const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, stage_id);
  359. this.ctx.helper.assignRelaData(posData, [
  360. { data: curStage, fields: ['contract_qty', 'qc_qty', 'qc_minus_qty'], prefix: '', relaId: 'pid' },
  361. ]);
  362. const pos = this._getNewPos();
  363. pos.loadDatas(posData);
  364. pos.calculateAll();
  365. const gclGatherModel = require('../gcl_gather').gclGather;
  366. const gatherUtil = new gclGatherModel(this.ctx);
  367. gatherUtil.gatherObj(billsTree, pos);
  368. const materialGl = stageSelf
  369. ? this.materialGatherBase.materialGl.filter(x => { return x.sid === parseInt(stage_id); })
  370. : this.materialGatherBase.materialGl;
  371. if (stageIndex) materialGl.forEach(x => { x.s_index = stageIndex });
  372. const materialNotJoin = this.materialGatherBase.materialNotJoin;
  373. const materialNotChange = this.materialGatherBase.materialNotChange;
  374. const helper = this.ctx.helper;
  375. for (const g of gatherUtil.gclList) {
  376. if (!g.contract_qty && !g.qc_qty && !g.qc_minus_qty) continue;
  377. g.sid = stage_id;
  378. g.sorder = stage_order;
  379. g.s_index = stageIndex;
  380. g.jiacha = 0;
  381. g.jiacha_qty = 0;
  382. for (const x of g.leafXmjs) {
  383. x.sid = stage_id;
  384. x.sorder = stage_order;
  385. x.s_index = stageIndex;
  386. x.jiacha = 0;
  387. const mnj = materialNotJoin.find(m => {
  388. return m.gcl_id === x.org_gcl_id && m.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === m.mx_id : true);
  389. });
  390. x.is_join = !mnj;
  391. const mnc = materialNotChange.find(m => {
  392. return m.gcl_id === x.org_gcl_id && m.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === m.mx_id : true);
  393. });
  394. x.is_change = mnc ? 2 : 1;
  395. const list = materialGl.filter(gl => {
  396. return gl.gcl_id === x.org_gcl_id && gl.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === gl.mx_id : true);
  397. });
  398. list.forEach(l => { l.gather_gcl_id = x.gcl_id});
  399. if (mnj) continue;
  400. x.jiacha_qty = this.getMaterialCalcQty(this.materialGatherBase.qtySource, x, x.is_change);
  401. for (const l of list) {
  402. x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.jiacha_qty, l.quantity), l.m_spread));
  403. }
  404. x.jiacha = helper.round(x.jiacha, decimal.tp);
  405. g.jiacha = helper.add(g.jiacha, x.jiacha);
  406. g.jiacha_qty = helper.add(g.jiacha_qty, x.jiacha_qty);
  407. }
  408. }
  409. return [gatherUtil.gclList, gatherUtil.leafXmjs];
  410. }
  411. async getMaterialGatherBills() {
  412. const material = this.ctx.material;
  413. if (!material) return {};
  414. const materials = await this.ctx.service.material.getAllDataByCondition({
  415. where: { tid: material.tid },
  416. orders: [['order', 'desc']],
  417. });
  418. if (materials.length === 0) return {};
  419. this.materialGatherBase = {};
  420. this.materialGatherBase.qtySource = material.qty_source;
  421. this.materialGatherBase.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  422. try {
  423. // 获取基础数据
  424. this.materialGatherBase.billsData = await this.ctx.service.ledger.getData(material.tid);
  425. this.materialGatherBase.posData = await this.ctx.service.pos.getPosData({ tid: material.tid });
  426. if (material.is_stage_self) {
  427. this.materialGatherBase.materialGl = material.order === materials[0].order
  428. ? await this.ctx.service.materialList.getMaterialStageData(material.tid, material.id)
  429. : await this.ctx.service.materialList.getPreMaterialStageData(material.tid, material.id);
  430. } else {
  431. this.materialGatherBase.materialGl = material.order === materials[0].order
  432. ? await this.ctx.service.materialList.getMaterialData(material.tid, material.id)
  433. : await this.ctx.service.materialList.getPreMaterialData(material.tid, material.id);
  434. }
  435. this.materialGatherBase.materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id, type: 1 } });
  436. this.materialGatherBase.materialNotChange = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id, type: 2 } });
  437. const mem_material_gather_bills = [], mem_material_gather_xmj = [];
  438. if (material.is_stage_self) {
  439. const stageIds = material.stage_id.split(',');
  440. const stageOrders = material.s_order.split(',');
  441. for (const [i, sid] of stageIds.entries()) {
  442. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(material.tid, sid, stageOrders[i], true, i + 1);
  443. mem_material_gather_bills.push(...gclList);
  444. mem_material_gather_xmj.push(...leafXmjs);
  445. }
  446. } else {
  447. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(material.tid, material.stage_id, material.stage_order, false);
  448. mem_material_gather_bills.push(...gclList);
  449. mem_material_gather_xmj.push(...leafXmjs);
  450. }
  451. return {mem_material_gather_bills, mem_material_gather_xmj, mem_material_gather_gl: this.materialGatherBase.materialGl};
  452. } catch (err) {
  453. this.ctx.log(err);
  454. return {};
  455. }
  456. }
  457. async getBindData(params, key, fields, customDefine, customSelect) {
  458. switch (key) {
  459. case 'materialGather':
  460. return await this.getMaterialGatherBills();
  461. default:
  462. return {};
  463. }
  464. }
  465. }
  466. module.exports = rptMemChange;