material_bills.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. const materialConst = require('../const/material');
  11. const MaterialCalculator = require('../lib/material_calc');
  12. module.exports = app => {
  13. class MaterialBills extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'material_bills';
  23. }
  24. /**
  25. * 添加工料
  26. * @return {void}
  27. */
  28. async add() {
  29. if (!this.ctx.tender || !this.ctx.material) {
  30. throw '数据错误';
  31. }
  32. const order = await this._getMaxOrder(this.ctx.tender.id);
  33. const transaction = await this.db.beginTransaction();
  34. try {
  35. const resultData = {};
  36. const newBills = {
  37. tid: this.ctx.tender.id,
  38. mid: this.ctx.material.id,
  39. order: order + 1,
  40. in_time: new Date(),
  41. };
  42. // 新增工料
  43. const result = await transaction.insert(this.tableName, newBills);
  44. if (result.affectedRows !== 1) {
  45. throw '新增工料数据失败';
  46. }
  47. if (this.ctx.material.is_stage_self) {
  48. await this.ctx.service.materialStageBills.add(transaction, result.insertId);
  49. resultData.pushStageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: result.insertId } });
  50. }
  51. const insertArray = [];
  52. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  53. for (const ym of material_month) {
  54. const one_month = {
  55. tid: this.ctx.tender.id,
  56. mid: this.ctx.material.id,
  57. mb_id: result.insertId,
  58. msg_tp: null,
  59. yearmonth: ym,
  60. };
  61. insertArray.push(one_month);
  62. }
  63. if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
  64. await transaction.commit();
  65. resultData.info = await this.getDataById(result.insertId);
  66. return resultData;
  67. } catch (error) {
  68. console.log(error);
  69. await transaction.rollback();
  70. throw error;
  71. }
  72. }
  73. /**
  74. * 导入工料
  75. * @return {void}
  76. */
  77. async exportData(datas, includeSpec = false, ms_id = null) {
  78. if (!this.ctx.tender || !this.ctx.material) {
  79. throw '数据错误';
  80. }
  81. let order = await this._getMaxOrder(this.ctx.tender.id);
  82. const transaction = await this.db.beginTransaction();
  83. try {
  84. const resultData = {};
  85. let needUpdateTp = false;
  86. // 找出需要导入的工料
  87. const bills = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  88. const newBills = this._.differenceWith(datas, bills, function(item1, item2) {
  89. return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec.toString() === item2.spec.toString()));
  90. });
  91. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  92. if (newBills.length !== 0) {
  93. const newBillsData = [];
  94. const newMonthList = [];
  95. for (const t of newBills) {
  96. // 判断msg_tp是否为数字,不是则报错
  97. if (t.msg_tp && !this._.isNumber(t.msg_tp)) {
  98. throw '信息价数据存在非数字字符,无法导入';
  99. }
  100. t.msg_tp = t.msg_tp ? this.ctx.helper.round(t.msg_tp, this.ctx.material.decimal.up) : null;
  101. const newBill = {
  102. tid: this.ctx.tender.id,
  103. mid: this.ctx.material.id,
  104. order: order + 1,
  105. code: t.code.toString(),
  106. name: t.name,
  107. unit: t.unit,
  108. spec: t.spec,
  109. msg_tp: this.ctx.material.is_stage_self ? null : t.msg_tp,
  110. msg_times: this.ctx.material.is_stage_self ? null : t.msg_times,
  111. msg_spread: this.ctx.material.is_stage_self ? null : t.msg_tp,
  112. m_spread: this.ctx.material.is_stage_self ? null : t.msg_tp,
  113. m_tp: 0,
  114. m_tax_tp: 0,
  115. in_time: new Date(),
  116. };
  117. order = order + 1;
  118. newBillsData.push(newBill);
  119. }
  120. const result = await transaction.insert(this.tableName, newBillsData);
  121. resultData.addNum = newBillsData.length;
  122. for (let j = 0; j < newBills.length; j++) {
  123. newBills[j].id = result.insertId + j;
  124. if (material_month.length > 0) {
  125. for (const ym of material_month) {
  126. const one_month = {
  127. tid: this.ctx.tender.id,
  128. mid: this.ctx.material.id,
  129. mb_id: newBills[j].id,
  130. msg_tp: newBills[j].msg_tp,
  131. yearmonth: ym,
  132. };
  133. newMonthList.push(one_month);
  134. }
  135. }
  136. }
  137. if (this.ctx.material.is_stage_self) {
  138. // 获取刚批量添加的所有list
  139. await this.ctx.service.materialStageBills.adds(transaction, newBills, null, ms_id);
  140. }
  141. if (newMonthList.length > 0) await transaction.insert(this.ctx.service.materialMonth.tableName, newMonthList);
  142. }
  143. // 找出需要更新的工料
  144. const updateBills = this._.intersectionWith(bills, datas, function(item1, item2) {
  145. return item1.code.toString() === item2.code.toString() && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec.toString() === item2.spec.toString()));
  146. });
  147. if (updateBills.length !== 0) {
  148. const updateDatas = [];
  149. const updateMonthList = [];
  150. for (const u of updateBills) {
  151. const oneData = this._.find(datas, function(item) {
  152. return item.code.toString() === u.code.toString() && item.name === u.name && item.unit === u.unit && (!includeSpec || (includeSpec && item.spec.toString() === u.spec.toString()));
  153. });
  154. const upd = { id: u.id };
  155. oneData.msg_tp = this.ctx.helper.round(oneData.msg_tp, this.ctx.material.decimal.up);
  156. // 判断msg_tp是否为数字,不是则报错
  157. if (oneData.msg_tp && !this._.isNumber(oneData.msg_tp)) {
  158. throw '信息价数据存在非数字字符,无法导入';
  159. }
  160. if (!this.ctx.material.is_stage_self) {
  161. if (oneData.msg_tp !== u.msg_tp) {
  162. needUpdateTp = true;
  163. upd.msg_tp = oneData.msg_tp;
  164. const [newmsg_spread, newm_spread] = await this.getSpread(u, oneData.msg_tp);
  165. upd.msg_spread = newmsg_spread;
  166. upd.m_spread = newm_spread;
  167. const newTp = this.ctx.helper.round(this.ctx.helper.mul(u.quantity, newm_spread), this.ctx.material.decimal.tp);
  168. upd.m_tp = newTp;
  169. upd.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(u.m_tax, 100))), this.ctx.material.decimal.tp);
  170. }
  171. if (oneData.msg_times !== u.msg_times) {
  172. upd.msg_times = oneData.msg_times;
  173. }
  174. }
  175. if (!includeSpec && oneData.spec !== u.spec) {
  176. upd.spec = oneData.spec;
  177. }
  178. if (!this._.isEqual(upd, { id: u.id })) updateDatas.push(upd);
  179. // 判断是否有月信息价,如果有则msg_tp都更改为统一值
  180. if (material_month.length > 0) {
  181. const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: u.id, mid: this.ctx.material.id } });
  182. if (monthList.length !== 0) {
  183. for (const m of monthList) {
  184. // 更新月信息单价小数位
  185. if (oneData.msg_tp !== m.msg_tp) {
  186. m.msg_tp = oneData.msg_tp;
  187. updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
  188. }
  189. }
  190. }
  191. }
  192. }
  193. if (updateDatas.length !== 0) await transaction.updateRows(this.tableName, updateDatas);
  194. resultData.updateNum = updateDatas.length;
  195. if (updateMonthList.length !== 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
  196. if (this.ctx.material.is_stage_self) {
  197. needUpdateTp = await this.ctx.service.materialStageBills.updateByExport(transaction, updateBills, datas, includeSpec, ms_id);
  198. }
  199. }
  200. // 更新materialStage 值
  201. if (this.ctx.material.is_stage_self) {
  202. resultData.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
  203. resultData.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  204. }
  205. resultData.billsData = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
  206. if (material_month.length > 0) {
  207. resultData.monthsList = await this.ctx.service.materialMonth.getMonthList(resultData.billsData, transaction);
  208. }
  209. resultData.m_tp = needUpdateTp ? await this.calcMaterialMTp(transaction) : this.ctx.material.m_tp;
  210. await transaction.commit();
  211. return resultData;
  212. } catch (error) {
  213. console.log(error);
  214. await transaction.rollback();
  215. throw error;
  216. }
  217. }
  218. /**
  219. * 添加工料
  220. * @return {void}
  221. */
  222. async addByGlj(data, order = null) {
  223. if (!this.ctx.tender || !this.ctx.material) {
  224. throw '数据错误';
  225. }
  226. const newOrder = this._.isNumber(order) ? parseInt(order) + 1 : await this._getMaxOrder(this.ctx.tender.id);
  227. const transaction = await this.db.beginTransaction();
  228. try {
  229. // order以下的工料+1
  230. await this._syncOrder(transaction, this.ctx.tender.id, newOrder, '+');
  231. const resultData = {};
  232. const newBills = {
  233. tid: this.ctx.tender.id,
  234. mid: this.ctx.material.id,
  235. code: data.code,
  236. name: data.name,
  237. unit: data.unit,
  238. m_up_risk: data.rise_range,
  239. m_down_risk: data.fall_range,
  240. spec: data.spec,
  241. m_type: data.type,
  242. remark: data.memo,
  243. order: newOrder,
  244. in_time: new Date(),
  245. };
  246. // 新增工料
  247. const result = await transaction.insert(this.tableName, newBills);
  248. if (result.affectedRows !== 1) {
  249. throw '新增工料数据失败';
  250. }
  251. if (this.ctx.material.is_stage_self) {
  252. await this.ctx.service.materialStageBills.add(transaction, result.insertId, data.memo);
  253. resultData.pushStageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: result.insertId } });
  254. }
  255. const insertArray = [];
  256. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  257. for (const ym of material_month) {
  258. const one_month = {
  259. tid: this.ctx.tender.id,
  260. mid: this.ctx.material.id,
  261. mb_id: result.insertId,
  262. msg_tp: null,
  263. yearmonth: ym,
  264. };
  265. insertArray.push(one_month);
  266. }
  267. if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
  268. await transaction.commit();
  269. resultData.info = await this.getDataById(result.insertId);
  270. return resultData;
  271. } catch (error) {
  272. console.log(error);
  273. await transaction.rollback();
  274. throw error;
  275. }
  276. }
  277. /**
  278. * 移除清单时,同步其后清单order
  279. * @param transaction - 事务
  280. * @param {Number} cid - 变更cid
  281. * @param {Number} order - order之后的
  282. * @return {Promise<*>}
  283. * @private
  284. */
  285. async _syncOrder(transaction, tid, order, selfOperate = '-', num = 1) {
  286. this.initSqlBuilder();
  287. this.sqlBuilder.setAndWhere('tid', {
  288. value: this.db.escape(tid),
  289. operate: '=',
  290. });
  291. this.sqlBuilder.setAndWhere('order', {
  292. value: order,
  293. operate: '>=',
  294. });
  295. this.sqlBuilder.setUpdateData('order', {
  296. value: num,
  297. selfOperate,
  298. });
  299. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  300. const data = await transaction.query(sql, sqlParam);
  301. return data;
  302. }
  303. async _getMaxOrder(tenderId) {
  304. const sql = 'SELECT Max(??) As value FROM ?? Where tid = ' + tenderId;
  305. const sqlParam = ['order', this.tableName];
  306. const queryResult = await this.db.queryOne(sql, sqlParam);
  307. return queryResult.value ? queryResult.value : 0;
  308. }
  309. /**
  310. * 删除工料
  311. * @param {int} id 工料id
  312. * @return {void}
  313. */
  314. async del(id) {
  315. if (!this.ctx.tender || !this.ctx.material) {
  316. throw '数据错误';
  317. }
  318. // 判断t_type是否为费用,且存在quantity,m_spread值
  319. const transaction = await this.db.beginTransaction();
  320. try {
  321. // 防止多页面操作时,清单工料含量存在时工料可删
  322. const materialListNum = await this.ctx.service.materialList.count({ tid: this.ctx.tender.id, mb_id: id });
  323. if (materialListNum > 0) {
  324. throw '该工料已存在对应的清单工料含量,删除失败';
  325. }
  326. const mbInfo = await this.getDataById(id);
  327. await transaction.delete(this.tableName, { id });
  328. const m_tp = this.ctx.material.m_tp;
  329. const result = { m_tp };
  330. if (this.ctx.material.is_stage_self) {
  331. await transaction.delete(this.ctx.service.materialStageBills.tableName, { mb_id: id });
  332. // 金额发生变化,则重新计算本期金额
  333. for (const sid of this.ctx.material.stage_id.split(',')) {
  334. const msInfo = await transaction.get(this.ctx.service.materialStage.tableName, { tid: this.ctx.tender.id, sid });
  335. await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
  336. }
  337. result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
  338. result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  339. result.m_tp = await this.calcMaterialMTp(transaction);
  340. } else if (mbInfo.t_type === materialConst.t_type[1].value && mbInfo.quantity !== null && mbInfo.m_spread !== null) {
  341. // 金额发生变化,则重新计算本期金额
  342. result.m_tp = await this.calcMaterialMTp(transaction);
  343. }
  344. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  345. if (material_month.length > 0) {
  346. await transaction.delete(this.ctx.service.materialMonth.tableName, { mb_id: id });
  347. }
  348. // order以下的清单-1
  349. await this._syncOrder(transaction, this.ctx.tender.id, mbInfo.order, '-');
  350. await transaction.commit();
  351. return result;
  352. } catch (err) {
  353. await transaction.rollback();
  354. throw err;
  355. }
  356. }
  357. /**
  358. * 交换两个工料的顺序
  359. * @param {Number} id1 - 工料1的id
  360. * @param {Number} id2 - 工料2的id
  361. * @returns {Promise<void>}
  362. */
  363. async changeOrder(datas) {
  364. if (!this.ctx.tender || !this.ctx.material) {
  365. throw '数据错误';
  366. }
  367. // const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
  368. // const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
  369. // if (!bill1 || !bill2) {
  370. // throw '数据错误';
  371. // }
  372. const transaction = await this.db.beginTransaction();
  373. try {
  374. // const order = bill1.order;
  375. // bill1.order = bill2.order;
  376. // bill2.order = order;
  377. // await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
  378. // await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
  379. await transaction.updateRows(this.tableName, datas);
  380. await transaction.commit();
  381. return true;
  382. } catch (err) {
  383. await transaction.rollback();
  384. throw err;
  385. }
  386. }
  387. /**
  388. * 修改工料信息
  389. * @param {Object} data 工料内容
  390. * @return {void}
  391. */
  392. async save(data, ms_id = null) {
  393. if (!this.ctx.tender || !this.ctx.material) {
  394. throw '数据错误';
  395. }
  396. delete data.in_time;
  397. // delete data.m_tp;
  398. // 判断是否可修改
  399. // 判断t_type是否为费用
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. const result = {};
  403. if (this.ctx.material.is_stage_self) {
  404. if (!ms_id) {
  405. throw '期参数有误';
  406. }
  407. const needUp = await this.updateOneBillsData(transaction, data, ms_id);
  408. const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
  409. // 更新materialStage 值
  410. data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
  411. data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
  412. data.quantity = null;
  413. data.msg_tp = null;
  414. data.msg_times = null;
  415. data.msg_spread = null;
  416. data.m_spread = null;
  417. delete data.ms_id;
  418. // 更新materialStage 值
  419. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  420. result.stageBillsData = this.ctx.material.is_stage_self && needUp ?
  421. await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } }) :
  422. await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, ms_id, mb_id: data.id } });
  423. result.stageData = this.ctx.material.is_stage_self && needUp ?
  424. await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
  425. await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
  426. }
  427. await transaction.update(this.tableName, data);
  428. if (this.ctx.material.is_stage_self) {
  429. result.billsData = await transaction.select(this.tableName, { where: { id: data.id } });
  430. }
  431. result.m_tp = await this.calcMaterialMTp(transaction);
  432. await transaction.commit();
  433. return result;
  434. } catch (err) {
  435. await transaction.rollback();
  436. throw err;
  437. }
  438. }
  439. async updateOneBillsData(transaction, data, ms_id) {
  440. // 当以下值和bills值不相同时,需要同步更新最新的计算值到stageBills表里
  441. const updateColsArray = ['t_type', 'm_tax', 'basic_price', 'm_up_risk', 'm_down_risk', 'is_summary'];
  442. let needUp = null;
  443. const mbInfo = await this.getDataById(data.id);
  444. for (const uc of updateColsArray) {
  445. if (data[uc] !== undefined && data[uc] !== mbInfo[uc]) {
  446. needUp = uc;
  447. break;
  448. }
  449. }
  450. if (needUp) {
  451. const msList = await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: this.ctx.material.id } });
  452. for (const ms of msList) {
  453. if (ms.id !== parseInt(ms_id)) {
  454. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({
  455. mid: this.ctx.material.id,
  456. ms_id: ms.id,
  457. mb_id: data.id,
  458. });
  459. const updateData = {
  460. id: msbInfo.id,
  461. };
  462. if (needUp === 'm_tax') {
  463. updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(msbInfo.m_tp, (1 + this.ctx.helper.div(data.m_tax, 100))), this.ctx.material.decimal.tp);
  464. } else if (needUp === 'm_up_risk' || needUp === 'm_down_risk' || needUp === 'basic_price') {
  465. const basic_price = needUp === 'basic_price' ? data.basic_price : mbInfo.basic_price;
  466. const [msg_spread, m_spread] = await this.getSpread(mbInfo, msbInfo.msg_tp, this.ctx.material.decimal.up, basic_price);
  467. updateData.msg_spread = msg_spread;
  468. updateData.m_spread = m_spread;
  469. const newTp = this.ctx.helper.round(this.ctx.helper.mul(msbInfo.quantity, m_spread), this.ctx.material.decimal.tp);
  470. updateData.m_tp = newTp;
  471. updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp);
  472. } else if (needUp === 't_type') {
  473. updateData.quantity = null;
  474. updateData.m_tp = null;
  475. updateData.m_tax_tp = null;
  476. } else if (needUp === 'is_summary') {
  477. updateData.is_summary = data.is_summary;
  478. }
  479. await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
  480. await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
  481. }
  482. }
  483. }
  484. return needUp;
  485. }
  486. async saveOrigin(data) {
  487. if (!this.ctx.tender || !this.ctx.material) {
  488. throw '数据错误';
  489. }
  490. return await this.db.update(this.tableName, { id: data.mb_id, origin: data.value });
  491. }
  492. async saveOrigins(datas) {
  493. if (!this.ctx.tender || !this.ctx.material) {
  494. throw '数据错误';
  495. }
  496. const updateData = [];
  497. for (const data of datas) {
  498. updateData.push({
  499. id: data.mb_id,
  500. origin: data.origin,
  501. });
  502. }
  503. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  504. return true;
  505. }
  506. /**
  507. * 修改工料信息
  508. * @param {Object} data 工料内容
  509. * @return {void}
  510. */
  511. async saveDatas(datas, ms_id = null) {
  512. if (!this.ctx.tender || !this.ctx.material) {
  513. throw '数据错误';
  514. }
  515. // 判断是否可修改
  516. // 判断t_type是否为费用
  517. const transaction = await this.db.beginTransaction();
  518. try {
  519. for (const data of datas) {
  520. delete data.in_time;
  521. let needUp = null;
  522. if (this.ctx.material.is_stage_self) {
  523. needUp = await this.updateOneBillsData(transaction, data, ms_id);
  524. const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
  525. // 更新materialStage 值
  526. data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
  527. data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
  528. data.quantity = null;
  529. data.msg_tp = null;
  530. data.msg_times = null;
  531. data.msg_spread = null;
  532. data.msg_spread = null;
  533. data.m_spread = null;
  534. }
  535. // delete data.m_tp;
  536. // console.log(data);
  537. await transaction.update(this.tableName, data);
  538. }
  539. // 更新materialStage 值
  540. const result = {};
  541. if (this.ctx.material.is_stage_self) {
  542. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  543. result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
  544. result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  545. result.billsData = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id }, orders: [['order', 'asc']] });
  546. }
  547. result.m_tp = await this.calcMaterialMTp(transaction);
  548. await transaction.commit();
  549. return result;
  550. } catch (err) {
  551. await transaction.rollback();
  552. throw err;
  553. }
  554. }
  555. /**
  556. * 更新新一期的quantity和截止上期金额并返回本期总金额
  557. * @param transaction
  558. * @param tid
  559. * @param mid
  560. * @returns {Promise<number>}
  561. */
  562. async updateNewMaterial(transaction, tid, mid, ctx, stage_id, decimal, pre_is_stage_self = 0) {
  563. const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
  564. let m_tp = 0;
  565. let m_tax_tp = 0;
  566. const materialCalculator = new MaterialCalculator(ctx, stage_id, ctx.tender.info);
  567. for (const mb of materialBillsData) {
  568. const [one_tp, one_tax_tp] = await this.calcQuantityByMB(transaction, mid, mb, materialCalculator, decimal, pre_is_stage_self);
  569. m_tp = this.ctx.helper.add(m_tp, one_tp);
  570. m_tax_tp = this.ctx.helper.add(m_tax_tp, one_tax_tp);
  571. }
  572. return [m_tp, m_tax_tp];
  573. }
  574. /**
  575. * 修改quantity,m_spread值和返回单条调差金额(新增一期)
  576. * @param transaction
  577. * @param mid
  578. * @param mb
  579. * @returns {Promise<*>}
  580. */
  581. async calcQuantityByMB(transaction, mid, mb, materialCalculator, decimal, pre_is_stage_self) {
  582. const [newmsg_spread, newm_spread] = await this.getSpread(mb, null, decimal.up);
  583. if (mb.t_type === materialConst.t_type[0].value) {
  584. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  585. const sqlParam = [mid, mb.id];
  586. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  587. console.log(mb_quantity);
  588. // 取历史期记录获取截止上期调差金额,并清空本期单价和时间,来源地,重新计算价差和有效价差
  589. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, decimal.qty);
  590. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, newm_spread), decimal.tp);
  591. const updateData = {
  592. id: mb.id,
  593. quantity: newQuantity,
  594. msg_tp: null,
  595. msg_times: null,
  596. msg_spread: newmsg_spread,
  597. m_spread: newm_spread,
  598. origin: null,
  599. m_tp: newTp,
  600. pre_tp: mb.m_tp !== null ? this.ctx.helper.add(mb.pre_tp, mb.m_tp) : mb.pre_tp,
  601. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp),
  602. tax_pre_tp: mb.m_tax_tp !== null ? this.ctx.helper.add(mb.tax_pre_tp, mb.m_tax_tp) : mb.tax_pre_tp,
  603. };
  604. await transaction.update(this.tableName, updateData);
  605. const m_tp = mb.is_summary === 1 ? await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, newm_spread), decimal.tp) : 0;
  606. const m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(m_tp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp);
  607. return [m_tp, m_tax_tp];
  608. } else if (mb.t_type === materialConst.t_type[1].value) {
  609. const quantity = pre_is_stage_self ? 0 : await materialCalculator.calculateExpr(mb.expr);
  610. const newTp = quantity !== 0 && quantity !== null ? this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(quantity, decimal.qty), newm_spread), decimal.tp) : null;
  611. const updateData = {
  612. id: mb.id,
  613. quantity: quantity !== 0 && quantity !== null ? this.ctx.helper.round(quantity, decimal.qty) : null,
  614. expr: pre_is_stage_self ? null : mb.expr,
  615. msg_tp: null,
  616. msg_times: null,
  617. msg_spread: newmsg_spread,
  618. m_spread: newm_spread,
  619. origin: null,
  620. m_tp: newTp,
  621. pre_tp: mb.m_tp !== null ? this.ctx.helper.add(mb.pre_tp, mb.m_tp) : mb.pre_tp,
  622. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp),
  623. tax_pre_tp: mb.m_tax_tp !== null ? this.ctx.helper.add(mb.tax_pre_tp, mb.m_tax_tp) : mb.tax_pre_tp,
  624. };
  625. await transaction.update(this.tableName, updateData);
  626. const m_tp = mb.is_summary === 1 ? await this.ctx.helper.round(this.ctx.helper.mul(quantity, newm_spread), decimal.tp) : 0;
  627. const m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(m_tp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp);
  628. return [m_tp, m_tax_tp];
  629. }
  630. }
  631. /**
  632. * 清空本期信息价后更新价差和有效价差
  633. * @param data
  634. * @returns {Promise<void>}
  635. */
  636. async getSpread(data, msg_tp, newDecimalUp = this.ctx.material.decimal.up, basic_price = null) {
  637. data.msg_tp = msg_tp;
  638. const newBp = basic_price ? basic_price : data.basic_price;
  639. const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, newBp), newDecimalUp);
  640. const cor = msg_spread >= 0 ? this.ctx.helper.mul(newBp, this.ctx.helper.div(data.m_up_risk, 100)) : this.ctx.helper.mul(newBp, this.ctx.helper.div(data.m_down_risk, 100));
  641. const m_spread = Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? this.ctx.helper.round(this.ctx.helper.sub(msg_spread, cor), newDecimalUp) : this.ctx.helper.round(this.ctx.helper.add(msg_spread, cor), newDecimalUp)) : 0;
  642. return [msg_spread, m_spread];
  643. }
  644. /**
  645. * 修改 expr和quantity值,返回本期金额和单条数据
  646. * @param data
  647. * @returns {Promise<void>}
  648. */
  649. async updateFYQuantity(data) {
  650. if (!this.ctx.tender || !this.ctx.material) {
  651. throw '数据错误';
  652. }
  653. const transaction = await this.db.beginTransaction();
  654. try {
  655. const returnData = {};
  656. returnData.m_tp = this.ctx.material.m_tp;
  657. if (this.ctx.material.is_stage_self) {
  658. if (!data.ms_id) throw '参数有误';
  659. const mbInfo = await this.getDataById(data.id);
  660. const msInfo = await this.ctx.service.materialStage.getDataById(data.ms_id);
  661. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id: data.id, ms_id: data.ms_id });
  662. // let all_m_tp = 0;
  663. // let all_tax_tp = 0;
  664. const materialCalculator = new MaterialCalculator(this.ctx, msInfo.sid, this.ctx.tender.info);
  665. const quantity = await materialCalculator.calculateExpr(data.expr);
  666. const newQuantity = quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null;
  667. const m_tp = newQuantity ? this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msbInfo.m_spread), this.ctx.material.decimal.tp) : null;
  668. const updateData = {
  669. id: data.id,
  670. quantity: newQuantity,
  671. expr: data.expr,
  672. m_tp,
  673. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  674. };
  675. const [one_bill_m_tp, one_bill_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, updateData, msInfo.id);
  676. await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
  677. const all_m_tp = this.ctx.helper.round(one_bill_m_tp, this.ctx.material.decimal.tp);
  678. const all_tax_tp = this.ctx.helper.round(one_bill_tax_tp, this.ctx.material.decimal.tp);
  679. // for (const sid of this.ctx.material.stage_id.split(',')) {
  680. // const materialCalculator = new MaterialCalculator(this.ctx, sid, this.ctx.tender.info);
  681. // const quantity = await materialCalculator.calculateExpr(data.expr);
  682. // const msInfo = await this.ctx.service.materialStage.getDataByCondition({ mid: this.ctx.material.id, sid });
  683. // const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id: data.id, ms_id: msInfo.id });
  684. // const newQuantity = quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null;
  685. // const m_tp = newQuantity ? this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msbInfo.m_spread), this.ctx.material.decimal.tp) : null;
  686. // const updateData = {
  687. // id: data.id,
  688. // quantity: newQuantity,
  689. // m_tp,
  690. // m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  691. // };
  692. // const [one_bill_m_tp, one_bill_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, updateData, msInfo.id);
  693. // await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
  694. // all_m_tp = this.ctx.helper.round(one_bill_m_tp, this.ctx.material.decimal.tp);
  695. // all_tax_tp = this.ctx.helper.round(one_bill_tax_tp, this.ctx.material.decimal.tp);
  696. // }
  697. const updateBillsData = {
  698. id: data.id,
  699. // expr: data.expr,
  700. m_tp: all_m_tp ? all_m_tp : null,
  701. m_tax_tp: all_tax_tp ? all_tax_tp : null,
  702. };
  703. console.log(all_m_tp);
  704. await transaction.update(this.tableName, updateBillsData);
  705. returnData.m_tp = await this.calcMaterialMTp(transaction);
  706. returnData.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } });
  707. returnData.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  708. } else {
  709. const materialCalculator = new MaterialCalculator(this.ctx, this.ctx.material.stage_id, this.ctx.tender.info);
  710. const quantity = await materialCalculator.calculateExpr(data.expr);
  711. // 更新quantity值并重新返回计算本期金额,截止本期金额
  712. const updateData = {
  713. id: data.id,
  714. quantity: quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null,
  715. expr: data.expr,
  716. };
  717. const mbInfo = await this.getDataById(updateData.id);
  718. updateData.m_tp = this.ctx.helper.round(this.ctx.helper.mul(updateData.quantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  719. updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(updateData.m_tp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp);
  720. await transaction.update(this.tableName, updateData);
  721. if (mbInfo.quantity !== updateData.quantity) {
  722. returnData.m_tp = await this.calcMaterialMTp(transaction);
  723. }
  724. }
  725. await transaction.commit();
  726. returnData.info = await this.getDataById(data.id);
  727. return returnData;
  728. } catch (err) {
  729. await transaction.rollback();
  730. throw err;
  731. }
  732. }
  733. // 更改计算总金额并返回值
  734. async calcMaterialMTp(transaction) {
  735. // 金额发生变化,则重新计算本期金额
  736. const sql = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price FROM ' + this.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  737. const sqlParam = [this.ctx.tender.id];
  738. const tp = await transaction.queryOne(sql, sqlParam);
  739. const updateData2 = {
  740. id: this.ctx.material.id,
  741. m_tp: tp.total_price,
  742. m_tax_tp: tp.tax_total_price,
  743. };
  744. console.log(tp);
  745. // if (this.ctx.material.material_tax) {
  746. // updateData2.m_tax_tp = tp.tax_total_price;
  747. // }
  748. await transaction.update(this.ctx.service.material.tableName, updateData2);
  749. return tp.total_price;
  750. }
  751. // 小数位变化更新单价和金额
  752. async resetDecimal(transaction, newDecimalUp, newDecimalTp, newDecimalQty) {
  753. const mbList = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
  754. const updateList = [];
  755. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  756. const updateMonthList = [];
  757. const materialStageList = this.ctx.material.is_stage_self ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) : [];
  758. for (const mb of mbList) {
  759. const updateData = {
  760. id: mb.id,
  761. };
  762. if (this.ctx.material.is_stage_self) {
  763. const updateStageBillsList = [];
  764. for (const ms of materialStageList) {
  765. const msb = await transaction.get(this.ctx.service.materialStageBills.tableName, { mid: this.ctx.material.id, mb_id: mb.id, ms_id: ms.id });
  766. msb.m_up_risk = mb.m_up_risk;
  767. msb.m_down_risk = mb.m_down_risk;
  768. const updateStageBillData = {
  769. id: msb.id,
  770. };
  771. if (newDecimalQty !== this.ctx.material.decimal.qty) {
  772. // 通过管理重新算出quantity并保留小数位
  773. if (mb.t_type === materialConst.t_type[0].value) {
  774. // 通过管理重新算出quantity并保留小数位
  775. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1';
  776. const sqlParam = [this.ctx.material.id, mb.id, ms.id];
  777. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  778. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
  779. if (newQuantity !== msb.quantity) {
  780. updateStageBillData.quantity = newQuantity;
  781. msb.quantity = newQuantity;
  782. }
  783. } else if (mb.t_type === materialConst.t_type[1].value) {
  784. const materialCalculator = new MaterialCalculator(this.ctx, ms.sid, this.ctx.tender.info);
  785. const quantity = await materialCalculator.calculateExpr(msb.expr);
  786. const newQuantity = this.ctx.helper.round(quantity, newDecimalQty);
  787. if (newQuantity !== msb.quantity) {
  788. updateStageBillData.quantity = newQuantity;
  789. msb.quantity = newQuantity;
  790. }
  791. }
  792. }
  793. if (newDecimalUp !== this.ctx.material.decimal.up) {
  794. const newmsg_tp = this.ctx.helper.round(msb.msg_tp, newDecimalUp);
  795. msb.msg_tp = newmsg_tp;
  796. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  797. const [newmsg_spread, newm_spread] = await this.getSpread(msb, msb.msg_tp, newDecimalUp, newbasic_price);
  798. msb.m_spread = newm_spread;
  799. updateStageBillData.msg_tp = newmsg_tp;
  800. updateStageBillData.msg_spread = newmsg_spread;
  801. updateStageBillData.m_spread = newm_spread;
  802. }
  803. const newTp = this.ctx.helper.round(this.ctx.helper.mul(msb.quantity, msb.m_spread), newDecimalTp);
  804. updateStageBillData.m_tp = newTp;
  805. updateStageBillData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
  806. updateStageBillsList.push(updateStageBillData);
  807. }
  808. if (newDecimalUp !== this.ctx.material.decimal.up) {
  809. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  810. updateData.basic_price = newbasic_price;
  811. }
  812. if (updateStageBillsList.length > 0) await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateStageBillsList);
  813. const sql = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price FROM ' + this.ctx.service.materialStageBills.tableName + ' WHERE `tid` = ? AND `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
  814. const sqlParam = [this.ctx.tender.id, this.ctx.material.id, mb.id];
  815. const tp = await transaction.queryOne(sql, sqlParam);
  816. updateData.m_tp = this.ctx.helper.round(tp.total_price, newDecimalTp);
  817. updateData.m_tax_tp = this.ctx.helper.round(tp.tax_total_price, newDecimalTp);
  818. updateList.push(updateData);
  819. } else {
  820. if (newDecimalUp !== this.ctx.material.decimal.up) {
  821. let newmsg_tp = this.ctx.helper.round(mb.msg_tp, newDecimalUp);
  822. mb.msg_tp = newmsg_tp;
  823. // 判断是否有月信息价,如果有则msg_tp值由月信息价的平均单价获得,并更新月信息价单价
  824. if (material_month.length > 0) {
  825. const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: mb.id, mid: this.ctx.material.id } });
  826. if (monthList.length !== 0) {
  827. for (const m of monthList) {
  828. // 更新月信息单价小数位
  829. const newMonthMsgTP = this.ctx.helper.round(m.msg_tp, newDecimalUp);
  830. if (m.msg_tp && newMonthMsgTP !== m.msg_tp) {
  831. m.msg_tp = newMonthMsgTP;
  832. updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
  833. }
  834. }
  835. const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
  836. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
  837. newmsg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), newDecimalUp) : null;
  838. mb.msg_tp = newmsg_tp;
  839. }
  840. }
  841. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  842. mb.basic_price = newbasic_price;
  843. const [newmsg_spread, newm_spread] = await this.getSpread(mb, mb.msg_tp, newDecimalUp);
  844. mb.m_spread = newm_spread;
  845. updateData.basic_price = newbasic_price;
  846. updateData.msg_tp = newmsg_tp;
  847. updateData.msg_spread = newmsg_spread;
  848. updateData.m_spread = newm_spread;
  849. }
  850. if (newDecimalQty !== this.ctx.material.decimal.qty) {
  851. // 通过管理重新算出quantity并保留小数位
  852. if (mb.t_type === materialConst.t_type[0].value) {
  853. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  854. const sqlParam = [this.ctx.material.id, mb.id];
  855. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  856. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
  857. mb.quantity = newQuantity;
  858. updateData.quantity = newQuantity;
  859. } else if (mb.t_type === materialConst.t_type[1].value) {
  860. const materialCalculator = new MaterialCalculator(this.ctx, this.ctx.material.stage_id, this.ctx.tender.info);
  861. const quantity = await materialCalculator.calculateExpr(mb.expr);
  862. const newQuantity = this.ctx.helper.round(quantity, newDecimalQty);
  863. mb.quantity = newQuantity;
  864. updateData.quantity = newQuantity;
  865. }
  866. }
  867. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), newDecimalTp);
  868. updateData.m_tp = newTp;
  869. updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
  870. updateList.push(updateData);
  871. }
  872. }
  873. if (updateMonthList.length > 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
  874. if (updateList.length > 0) await transaction.updateRows(this.tableName, updateList);
  875. if (this.ctx.material.is_stage_self) {
  876. for (const ms of materialStageList) {
  877. await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
  878. }
  879. }
  880. }
  881. }
  882. return MaterialBills;
  883. };