material_list.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. module.exports = app => {
  11. class MaterialList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_list';
  21. }
  22. /**
  23. * 添加工料清单关联
  24. * @return {void}
  25. */
  26. async add(data, ms_id = null) {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const list = [];
  31. for (const mb of data.mb_id) {
  32. const newLists = {
  33. tid: this.ctx.tender.id,
  34. order: this.ctx.material.order,
  35. mid: this.ctx.material.id,
  36. mb_id: mb,
  37. gcl_id: data.gcl_id,
  38. xmj_id: data.xmj_id,
  39. mx_id: data.mx_id,
  40. contract_qty: data.contract_qty,
  41. qc_qty: data.qc_qty,
  42. qc_minus_qty: data.qc_minus_qty,
  43. gather_qty: data.gather_qty,
  44. is_join: data.is_join,
  45. is_self: 1,
  46. ms_id: ms_id ? ms_id : null,
  47. in_time: new Date(),
  48. };
  49. list.push(newLists);
  50. }
  51. // 新增工料
  52. const result = await this.db.insert(this.tableName, list);
  53. if (result.affectedRows === 0) {
  54. throw '新增工料数据失败';
  55. }
  56. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  57. }
  58. /**
  59. * 删除工料清单关联
  60. * @param {int} id 工料id
  61. * @return {void}
  62. */
  63. async del(id, mb_id, ms_id = null) {
  64. if (!this.ctx.tender || !this.ctx.material) {
  65. throw '数据错误';
  66. }
  67. const transaction = await this.db.beginTransaction();
  68. try {
  69. // 判断是否可删
  70. await transaction.delete(this.tableName, { id });
  71. await this.calcQuantityByML(transaction, mb_id, ms_id);
  72. await transaction.commit();
  73. return true;
  74. } catch (err) {
  75. await transaction.rollback();
  76. throw err;
  77. }
  78. }
  79. /**
  80. * 修改工料清单关联信息
  81. * @param {Object} data 工料内容
  82. * @param {int} order 期数
  83. * @return {void}
  84. */
  85. async save(data, ms_id = null) {
  86. if (!this.ctx.tender || !this.ctx.material) {
  87. throw '数据错误';
  88. }
  89. const transaction = await this.db.beginTransaction();
  90. try {
  91. const mb_id = data.mb_id;
  92. delete data.mb_id;
  93. await transaction.update(this.tableName, data);
  94. await this.calcQuantityByML(transaction, mb_id, ms_id);
  95. await transaction.commit();
  96. return true;
  97. } catch (err) {
  98. await transaction.rollback();
  99. throw err;
  100. }
  101. }
  102. /**
  103. * 修改工料信息
  104. * @param {Object} data 工料内容
  105. * @return {void}
  106. */
  107. async saveDatas(datas, ms_id = null) {
  108. if (!this.ctx.tender || !this.ctx.material) {
  109. throw '数据错误';
  110. }
  111. // 判断是否可修改
  112. // 判断t_type是否为费用
  113. const transaction = await this.db.beginTransaction();
  114. try {
  115. for (const data of datas) {
  116. const mb_id = data.mb_id;
  117. delete data.mb_id;
  118. await transaction.update(this.tableName, data);
  119. await this.calcQuantityByML(transaction, mb_id, ms_id);
  120. }
  121. await transaction.commit();
  122. return true;
  123. } catch (err) {
  124. await transaction.rollback();
  125. throw err;
  126. }
  127. }
  128. /**
  129. * 应用工料清单到其它清单中
  130. * @return {void}
  131. */
  132. async addOther(data) {
  133. if (!this.ctx.tender || !this.ctx.material) {
  134. throw '数据错误';
  135. }
  136. const transaction = await this.db.beginTransaction();
  137. try {
  138. // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
  139. // 还要找出删除的工料,更新单价数量
  140. const list = [];
  141. const delList = [];
  142. const materialBills = data.materialBills;
  143. const mb_idList = [];
  144. for (const xmj of data.addXmj) {
  145. const mlList = await this.getAllDataByCondition({
  146. where: {
  147. mid: this.ctx.material.id,
  148. gcl_id: xmj.gcl_id,
  149. xmj_id: xmj.id,
  150. mx_id: xmj.mx_id,
  151. },
  152. });
  153. const mbIdList = this._.map(mlList, 'mb_id');
  154. mb_idList.push(...mbIdList);
  155. delList.push(...this._.map(mlList, 'id'));
  156. for (const mb of materialBills) {
  157. const newLists = {
  158. tid: this.ctx.tender.id,
  159. order: mb.order,
  160. mid: this.ctx.material.id,
  161. mb_id: mb.mb_id,
  162. gcl_id: xmj.gcl_id,
  163. xmj_id: xmj.id,
  164. mx_id: xmj.mx_id,
  165. gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
  166. quantity: mb.quantity,
  167. expr: mb.expr,
  168. in_time: new Date(),
  169. };
  170. list.push(newLists);
  171. mb_idList.push(mb.mb_id);
  172. }
  173. }
  174. // 删除工料清单关联
  175. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  176. // 新增工料清单关联
  177. if (list.length > 0) {
  178. const result = await transaction.insert(this.tableName, list);
  179. if (result.affectedRows === 0) {
  180. throw '新增工料数据失败';
  181. }
  182. }
  183. // 重算工料和总金额
  184. const calcMBIdList = this._.uniq(mb_idList);
  185. if (calcMBIdList.length > 0) {
  186. for (const select of calcMBIdList) {
  187. await this.calcQuantityByML(transaction, select);
  188. }
  189. }
  190. // throw 'fail';
  191. // await this.calcQuantityByML(transaction, select.mb_id);
  192. await transaction.commit();
  193. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  194. } catch (err) {
  195. await transaction.rollback();
  196. throw err;
  197. }
  198. }
  199. /**
  200. * 修改material_bills的quantity值和计算本期金额
  201. * @param transaction
  202. * @param mb_id
  203. * @return {Promise<*>}
  204. */
  205. async calcQuantityByML(transaction, mb_id, ms_id = null, updateAllStage = '') {
  206. // 修改material_bills值
  207. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  208. if (!mbInfo) {
  209. throw '不存在该工料';
  210. }
  211. let m_spread = mbInfo.m_spread;
  212. let updateId = mb_id;
  213. if (ms_id) {
  214. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id, ms_id });
  215. m_spread = msbInfo.m_spread;
  216. updateId = msbInfo.id;
  217. }
  218. const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, ms_id);
  219. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, m_spread), this.ctx.material.decimal.tp);
  220. const updateData = {
  221. id: updateId,
  222. quantity: newQuantity,
  223. m_tp: newTp,
  224. 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),
  225. };
  226. if (ms_id) {
  227. await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
  228. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  229. if (updateAllStage === 'all') {
  230. const updateDatas = [];
  231. const updateMsIds = [];
  232. const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id } });
  233. for (const msb of msbList) {
  234. if (msb.ms_id !== parseInt(ms_id)) {
  235. const newQuantity4 = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, msb.ms_id);
  236. const newTp4 = this.ctx.helper.round(this.ctx.helper.mul(newQuantity4, msb.m_spread), this.ctx.material.decimal.tp);
  237. const updateData4 = {
  238. id: msb.id,
  239. quantity: newQuantity4,
  240. m_tp: newTp4,
  241. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp4, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  242. };
  243. updateDatas.push(updateData4);
  244. updateMsIds.push(msb.ms_id);
  245. }
  246. }
  247. if (updateDatas.length > 0) {
  248. await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateDatas);
  249. for (const msId of updateMsIds) {
  250. await this.ctx.service.materialStage.updateMtp(transaction, msId);
  251. }
  252. }
  253. }
  254. // 还要更新bills表的m_tp和m_tax_tp值
  255. const sql3 = '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';
  256. const sqlParam3 = [this.ctx.tender.id, this.ctx.material.id, mb_id];
  257. const tp3 = await transaction.queryOne(sql3, sqlParam3);
  258. const updateBillsData = {
  259. id: mb_id,
  260. m_tp: tp3.total_price,
  261. m_tax_tp: tp3.tax_total_price,
  262. };
  263. await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData);
  264. } else {
  265. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  266. }
  267. // 计算本期总金额
  268. const sql2 = '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.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  269. const sqlParam2 = [this.ctx.tender.id];
  270. const tp = await transaction.queryOne(sql2, sqlParam2);
  271. console.log(tp);
  272. const updateData2 = {
  273. id: this.ctx.material.id,
  274. m_tp: tp.total_price,
  275. m_tax_tp: tp.tax_total_price,
  276. };
  277. // 计算建筑税价
  278. if (this.ctx.material.is_stage_self) {
  279. const materialStages = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  280. let rate_tp = 0;
  281. for (const ms of materialStages) {
  282. const ms_rate_tp = this.ctx.helper.round(this.ctx.helper.mul(ms.m_tp, 1 + this.ctx.material.rate / 100), this.ctx.material.decimal.tp);
  283. rate_tp = this.ctx.helper.add(rate_tp, ms_rate_tp);
  284. }
  285. updateData2.rate_tp = rate_tp;
  286. } else {
  287. updateData2.rate_tp = this.ctx.helper.round(this.ctx.helper.mul(tp.total_price, 1 + this.ctx.material.rate / 100), this.ctx.material.decimal.tp);
  288. }
  289. const result = await transaction.update(this.ctx.service.material.tableName, updateData2);
  290. // 找出当前人并更新tp_data
  291. const tp_data = await this.ctx.service.materialAudit.getTpData(transaction, this.ctx.material.id);
  292. if (this.ctx.material.status === auditConst.status.uncheck || this.ctx.material.status === auditConst.status.checkNo) {
  293. await transaction.update(this.ctx.service.material.tableName, {
  294. id: this.ctx.material.id,
  295. tp_data: JSON.stringify(tp_data),
  296. });
  297. } else if (this.ctx.material.curAuditor) {
  298. await transaction.update(this.ctx.service.materialAudit.tableName, {
  299. id: this.ctx.material.curAuditor.id,
  300. tp_data: JSON.stringify(tp_data),
  301. });
  302. }
  303. return result;
  304. }
  305. /**
  306. * 修改material_bills的quantity值和计算本期金额
  307. * @param transaction
  308. * @param mb_id
  309. * @return {Promise<*>}
  310. */
  311. async calcAllQuantityByML(transaction, mbIds) {
  312. const mbList = await transaction.select(this.ctx.service.materialBills.tableName, { where: { id: mbIds } });
  313. // 修改material_bills值
  314. for (const mbInfo of mbList) {
  315. if (!mbInfo) {
  316. throw '不存在该工料';
  317. }
  318. if (this.ctx.material.is_stage_self) {
  319. const updateDatas = [];
  320. const updateMsIds = [];
  321. const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: mbInfo.id } });
  322. for (const msb of msbList) {
  323. const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id, msb.ms_id);
  324. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msb.m_spread), this.ctx.material.decimal.tp);
  325. const updateData = {
  326. id: msb.id,
  327. quantity: newQuantity,
  328. m_tp: newTp,
  329. 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),
  330. };
  331. updateDatas.push(updateData);
  332. updateMsIds.push(msb.ms_id);
  333. }
  334. if (updateDatas.length > 0) {
  335. await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateDatas);
  336. for (const msId of updateMsIds) {
  337. await this.ctx.service.materialStage.updateMtp(transaction, msId);
  338. }
  339. }
  340. // 还要更新bills表的m_tp和m_tax_tp值
  341. const sql3 = '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';
  342. const sqlParam3 = [this.ctx.tender.id, this.ctx.material.id, mbInfo.id];
  343. const tp3 = await transaction.queryOne(sql3, sqlParam3);
  344. const updateBillsData = {
  345. id: mbInfo.id,
  346. m_tp: tp3.total_price,
  347. m_tax_tp: tp3.tax_total_price,
  348. };
  349. await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData);
  350. } else {
  351. const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id);
  352. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  353. const updateData = {
  354. id: mbInfo.id,
  355. quantity: newQuantity,
  356. m_tp: newTp,
  357. 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),
  358. };
  359. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  360. }
  361. }
  362. // 计算本期总金额
  363. const sql2 = '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.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  364. const sqlParam2 = [this.ctx.tender.id];
  365. const tp = await transaction.queryOne(sql2, sqlParam2);
  366. console.log(tp);
  367. const updateData2 = {
  368. id: this.ctx.material.id,
  369. m_tp: tp.total_price,
  370. m_tax_tp: tp.tax_total_price,
  371. };
  372. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  373. }
  374. /**
  375. * 获取工料清单关联表
  376. * @param {int} tid 标段id
  377. * @param {Object} mid 期id
  378. * @return {void}
  379. */
  380. async getMaterialData(tid, mid) {
  381. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`contract_qty`, ml.`qc_qty`, ml.`qc_minus_qty`, ml.`gather_qty`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id`, ml.`tid`, ml.`mid`, mb.m_spread, ml.ms_id, ml.is_join' +
  382. ' FROM ' + this.tableName + ' as ml' +
  383. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  384. ' ON ml.`mb_id` = mb.`id`' +
  385. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  386. const sqlParam = [tid, mid];
  387. return await this.db.query(sql, sqlParam);
  388. }
  389. async getPreMaterialData(tid, mid) {
  390. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id`, ml.`tid`, ml.`mid`, mbh.m_spread, ml.ms_id, ml.is_join' +
  391. ' FROM ' + this.tableName + ' as ml' +
  392. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  393. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' as mbh ON ml.`mb_id` = mbh.`mb_id` and mbh.mid = ?' +
  394. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  395. const sqlParam = [mid, tid, mid];
  396. return await this.db.query(sql, sqlParam);
  397. }
  398. async getMaterialStageData(tid, mid) {
  399. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, msb.id AS mb_id, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id`, ml.`tid`, ml.`mid`, msb.m_spread, ml.ms_id, ms.sid, ms.order as s_order, ml.is_join' +
  400. ' FROM ' + this.tableName + ' as ml' +
  401. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  402. ' LEFT JOIN ' + this.ctx.service.materialStageBills.tableName + ' as msb ON ml.mb_id = msb.mb_id AND ml.ms_id = msb.ms_id' +
  403. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  404. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  405. const sqlParam = [tid, mid];
  406. return await this.db.query(sql, sqlParam);
  407. }
  408. async getPreMaterialStageData(tid, mid) {
  409. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, msb.id AS mb_id, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`ms_id`, ml.`tid`, ml.`mid`, msb.m_spread, ml.ms_id, ms.sid, ms.order as s_order, ml.is_join' +
  410. ' FROM ' + this.tableName + ' as ml' +
  411. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  412. ' LEFT JOIN ' + this.ctx.service.materialStageBills.tableName + ' as msb ON ml.`mb_id` = msb.mb_id AND ml.ms_id = msb.ms_id And ml.mid = msb.mid' +
  413. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' as ms ON ml.`ms_id` = ms.`id`' +
  414. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  415. const sqlParam = [tid, mid];
  416. return await this.db.query(sql, sqlParam);
  417. }
  418. /**
  419. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  420. * @param transaction
  421. * @param preMaterial
  422. * @param newMid
  423. * @return {Promise<void>}
  424. */
  425. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  426. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  427. const copyMLArray = [];
  428. for (const ml of materialListData) {
  429. // 获取小计值
  430. let qtys = null;
  431. if (ml.mx_id !== null && ml.mx_id !== '') {
  432. qtys = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  433. } else {
  434. qtys = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  435. }
  436. const newMaterialList = {
  437. tid: ml.tid,
  438. order: ml.order,
  439. mid: newMaterial.id,
  440. mb_id: ml.mb_id,
  441. gcl_id: ml.gcl_id,
  442. xmj_id: ml.xmj_id,
  443. mx_id: ml.mx_id,
  444. contract_qty: qtys ? qtys.contract_qty : null,
  445. qc_qty: qtys ? qtys.qc_qty : null,
  446. qc_minus_qty: qtys ? qtys.qc_minus_qty : null,
  447. gather_qty: qtys ? qtys.gather_qty : null,
  448. quantity: ml.quantity,
  449. expr: ml.expr,
  450. is_join: ml.is_join,
  451. in_time: new Date(),
  452. };
  453. copyMLArray.push(newMaterialList);
  454. }
  455. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  456. }
  457. /**
  458. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  459. * @param transaction
  460. * @param preMaterial
  461. * @param newMid
  462. * @return {Promise<void>}
  463. */
  464. async copyPreMaterialList2(transaction, materialListData, materialSelfListData, notJoinList, newMaterial, materialStageData) {
  465. if (materialListData && materialListData.length > 0) {
  466. const copyMLArray = [];
  467. for (const ml of materialListData) {
  468. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 1 });
  469. const is_change = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 2 });
  470. const newMaterialList = {
  471. tid: newMaterial.tid,
  472. order: ml.order,
  473. mid: newMaterial.id,
  474. mb_id: ml.mb_id,
  475. gcl_id: ml.gcl_id,
  476. xmj_id: ml.xmj_id,
  477. mx_id: ml.mx_id,
  478. contract_qty: ml.contract_qty,
  479. qc_qty: ml.qc_qty,
  480. qc_minus_qty: ml.qc_minus_qty,
  481. gather_qty: ml.gather_qty,
  482. quantity: ml.quantity ? ml.quantity : 0,
  483. expr: ml.expr ? ml.expr : '',
  484. is_join: is_join ? 0 : is_change ? 2 : 1,
  485. in_time: new Date(),
  486. };
  487. if (ml.sid) {
  488. const ms = this._.find(materialStageData, { sid: ml.sid });
  489. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  490. }
  491. copyMLArray.push(newMaterialList);
  492. }
  493. if (copyMLArray.length !== 0) await transaction.insert(this.tableName, copyMLArray);
  494. }
  495. if (materialSelfListData && materialSelfListData.length > 0) {
  496. const copyMLArray2 = [];
  497. for (const ml of materialSelfListData) {
  498. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 1 });
  499. const is_change = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id, type: 2 });
  500. const newMaterialList = {
  501. tid: newMaterial.tid,
  502. order: ml.order,
  503. mid: newMaterial.id,
  504. mb_id: ml.mb_id,
  505. gcl_id: ml.gcl_id,
  506. xmj_id: ml.xmj_id,
  507. mx_id: ml.mx_id,
  508. contract_qty: ml.contract_qty,
  509. qc_qty: ml.qc_qty,
  510. qc_minus_qty: ml.qc_minus_qty,
  511. gather_qty: ml.gather_qty,
  512. quantity: ml.quantity ? ml.quantity : 0,
  513. expr: ml.expr ? ml.expr : '',
  514. is_join: is_join ? 0 : is_change ? 2 : 1,
  515. is_self: 1,
  516. in_time: new Date(),
  517. };
  518. if (ml.sid) {
  519. const ms = this._.find(materialStageData, { sid: ml.sid });
  520. if (ms && ms.id) newMaterialList.ms_id = ms.id;
  521. }
  522. copyMLArray2.push(newMaterialList);
  523. }
  524. if (copyMLArray2.length !== 0) await transaction.insert(this.tableName, copyMLArray2);
  525. }
  526. }
  527. /**
  528. * 添加工料清单关联(多清单对应)
  529. * @return {void}
  530. */
  531. async adds(datas, checklist = false) {
  532. if (!this.ctx.tender || !this.ctx.material) {
  533. throw '数据错误';
  534. }
  535. const transaction = await this.db.beginTransaction();
  536. try {
  537. const list = [];
  538. const listGcl = [];
  539. const uplist = [];
  540. const uplistGcl = [];
  541. // const delList = [];
  542. // const mb_idList = [];
  543. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  544. const oldGclList = await transaction.select(this.ctx.service.materialListGcl.tableName, { where: { tid: this.ctx.tender.id } });
  545. const oldMaterialList = await transaction.select(this.ctx.service.materialList.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  546. for (const xmj of datas.xmjs) {
  547. for (const mb of datas.mbIds) {
  548. // // 旧数据兼容问题,要去删除相同已存在的工料
  549. // const mlInfo = await this.getDataByCondition({
  550. // mid: this.ctx.material.id,
  551. // gcl_id: xmj.gcl_id,
  552. // xmj_id: xmj.xmj_id,
  553. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  554. // mb_id: mb,
  555. // });
  556. // if (mlInfo) {
  557. // delList.push(mlInfo.id);
  558. // mb_idList.push(mb);
  559. // }
  560. const mbId = typeof mb === 'object' ? mb.id : mb;
  561. const quantity = typeof mb === 'object' ? mb.quantity : 0;
  562. if ((xmj.contract_qty || xmj.qc_qty || xmj.qc_minus_qty || xmj.gather_qty) && this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  563. const mlInfo = this._.find(oldMaterialList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id, mb_id: mbId, ms_id: xmj.ms_id ? xmj.ms_id : null });
  564. if (mlInfo) {
  565. uplist.push({ id: mlInfo.id, quantity, expr: '' });
  566. } else {
  567. const newLists = {
  568. tid: this.ctx.tender.id,
  569. order: this.ctx.material.order,
  570. mid: this.ctx.material.id,
  571. mb_id: mbId,
  572. gcl_id: xmj.gcl_id,
  573. xmj_id: xmj.xmj_id,
  574. mx_id: xmj.mx_id,
  575. ms_id: xmj.ms_id ? xmj.ms_id : null,
  576. contract_qty: xmj.contract_qty,
  577. qc_qty: xmj.qc_qty,
  578. qc_minus_qty: xmj.qc_minus_qty,
  579. gather_qty: xmj.gather_qty,
  580. quantity,
  581. in_time: new Date(),
  582. is_join: xmj.is_join,
  583. };
  584. list.push(newLists);
  585. }
  586. }
  587. const gclIndex = this._.findIndex(oldGclList, { gcl_id: xmj.gcl_id, mb_id: mbId });
  588. if (gclIndex === -1 && this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mbId }) === -1) {
  589. const newListGcl = {
  590. tid: this.ctx.tender.id,
  591. order: this.ctx.material.order,
  592. mid: this.ctx.material.id,
  593. mb_id: mbId,
  594. gcl_id: xmj.gcl_id,
  595. quantity,
  596. expr: '',
  597. };
  598. listGcl.push(newListGcl);
  599. } else if (gclIndex !== -1 && this._.findIndex(uplistGcl, { id: oldGclList[gclIndex].id }) === -1) {
  600. uplistGcl.push({
  601. id: oldGclList[gclIndex].id,
  602. expr: '',
  603. quantity,
  604. });
  605. }
  606. }
  607. }
  608. // 维护list_gcl表
  609. // 删除工料清单关联
  610. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  611. // 新增工料清单关联
  612. if (list.length > 0) {
  613. await this.insertBigDatas(transaction, list);
  614. }
  615. if (listGcl.length > 0) {
  616. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  617. if (result2.affectedRows === 0) {
  618. throw '新增工料关联数据失败';
  619. }
  620. }
  621. // 覆盖
  622. if (uplist.length > 0) {
  623. await transaction.updateRows(this.tableName, uplist);
  624. }
  625. if (uplistGcl.length > 0) {
  626. await transaction.updateRows(this.ctx.service.materialListGcl.tableName, uplistGcl);
  627. }
  628. if (checklist) {
  629. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  630. }
  631. // 重算工料和总金额
  632. // const calcMBIdList = this._.uniq(mb_idList);
  633. // if (calcMBIdList.length > 0) {
  634. // for (const select of calcMBIdList) {
  635. // await this.calcQuantityByML(transaction, select);
  636. // }
  637. // }
  638. if ((list.length > 0 || uplist.length > 0) && datas.export) {
  639. await this.calcAllQuantityByML(transaction, this._.map(datas.mbIds, 'id'));
  640. }
  641. await transaction.commit();
  642. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  643. return checklist ? gclList : {
  644. gclList,
  645. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  646. };
  647. } catch (err) {
  648. await transaction.rollback();
  649. throw err;
  650. }
  651. }
  652. /**
  653. * 删除工料清单关联(多清单对应)
  654. * @param {int} id 工料id
  655. * @return {void}
  656. */
  657. async dels(datas, checklist = false, fromCheckList = false, ms_id = null) {
  658. if (!this.ctx.tender || !this.ctx.material) {
  659. throw '数据错误';
  660. }
  661. const transaction = await this.db.beginTransaction();
  662. try {
  663. // 判断是否可删
  664. const listGcl = [];
  665. for (const xmj of datas.xmjs) {
  666. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id, is_self: 0 });
  667. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  668. await transaction.delete(this.service.materialListGcl.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id });
  669. listGcl.push(xmj.gcl_id);
  670. }
  671. }
  672. // await transaction.delete(this.tableName, { id });
  673. await this.calcQuantityByML(transaction, datas.mb_id, ms_id, 'all');
  674. if (checklist) {
  675. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  676. }
  677. await transaction.commit();
  678. // console.log(datas);
  679. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  680. return fromCheckList ? gclList : {
  681. gclList,
  682. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  683. };
  684. } catch (err) {
  685. await transaction.rollback();
  686. throw err;
  687. }
  688. }
  689. /**
  690. * 修改工料清单关联信息(多清单对应)
  691. * @param {Object} data 工料内容
  692. * @param {int} order 期数
  693. * @return {void}
  694. */
  695. async saves(datas, checklist = false, ms_id = null) {
  696. if (!this.ctx.tender || !this.ctx.material) {
  697. throw '数据错误';
  698. }
  699. const transaction = await this.db.beginTransaction();
  700. try {
  701. const mb_id = datas.mb_id;
  702. const updateDatas = [];
  703. const updateListGcl = [];
  704. const listGcl = [];
  705. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  706. for (const xmj of datas.xmjs) {
  707. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  708. const udata = {
  709. row: {
  710. expr: datas.expr,
  711. quantity: datas.quantity,
  712. },
  713. where: {
  714. tid: this.ctx.tender.id,
  715. mid: this.ctx.material.id,
  716. mb_id,
  717. gcl_id: xmj.gcl_id,
  718. xmj_id: xmj.xmj_id,
  719. mx_id: xmj.mx_id,
  720. },
  721. };
  722. updateDatas.push(udata);
  723. }
  724. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  725. listGcl.push(xmj.gcl_id);
  726. updateListGcl.push({
  727. row: {
  728. expr: datas.expr,
  729. quantity: datas.quantity,
  730. },
  731. where: {
  732. tid: this.ctx.tender.id,
  733. // mid: this.ctx.material.id,
  734. mb_id,
  735. gcl_id: xmj.gcl_id,
  736. },
  737. });
  738. }
  739. }
  740. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  741. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  742. await this.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  743. await transaction.commit();
  744. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  745. return checklist ? gclList : {
  746. gclList,
  747. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  748. };
  749. } catch (err) {
  750. await transaction.rollback();
  751. throw err;
  752. }
  753. }
  754. /**
  755. * 复制粘贴多工料信息(多清单对应)
  756. * @param {Object} data 工料内容
  757. * @return {void}
  758. */
  759. async savePastes(datas, checklist = false, ms_id = null) {
  760. if (!this.ctx.tender || !this.ctx.material) {
  761. throw '数据错误';
  762. }
  763. // 判断是否可修改
  764. // 判断t_type是否为费用
  765. const transaction = await this.db.beginTransaction();
  766. try {
  767. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  768. for (const data of datas.pasteData) {
  769. const updateDatas = [];
  770. const updateListGcl = [];
  771. const listGcl = [];
  772. for (const xmj of datas.xmjs) {
  773. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  774. const udata = {
  775. row: {
  776. expr: data.expr,
  777. quantity: data.quantity,
  778. },
  779. where: {
  780. tid: this.ctx.tender.id,
  781. mid: this.ctx.material.id,
  782. mb_id: data.mb_id,
  783. gcl_id: xmj.gcl_id,
  784. xmj_id: xmj.xmj_id,
  785. mx_id: xmj.mx_id,
  786. },
  787. };
  788. updateDatas.push(udata);
  789. }
  790. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  791. listGcl.push(xmj.gcl_id);
  792. updateListGcl.push({
  793. row: {
  794. expr: data.expr,
  795. quantity: data.quantity,
  796. },
  797. where: {
  798. tid: this.ctx.tender.id,
  799. // mid: this.ctx.material.id,
  800. mb_id: data.mb_id,
  801. gcl_id: xmj.gcl_id,
  802. },
  803. });
  804. }
  805. }
  806. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  807. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  808. await this.calcQuantityByML(transaction, data.mb_id, ms_id, 'all');
  809. }
  810. await transaction.commit();
  811. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  812. return checklist ? gclList : {
  813. gclList,
  814. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  815. };
  816. } catch (err) {
  817. await transaction.rollback();
  818. throw err;
  819. }
  820. }
  821. async saveQtyHistory(updateList) {
  822. if (!this.ctx.tender || !this.ctx.material) {
  823. throw '数据错误';
  824. }
  825. // 判断是否可修改
  826. // 判断t_type是否为费用
  827. const transaction = await this.db.beginTransaction();
  828. try {
  829. if (updateList.length > 0) await transaction.updateRows(this.tableName, updateList);
  830. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, is_new_qty: 1 });
  831. await transaction.commit();
  832. return {
  833. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id)
  834. };
  835. } catch (err) {
  836. await transaction.rollback();
  837. throw err;
  838. }
  839. }
  840. async getMbQuantity(transaction, mid, qty_source, qty_decimal, mb_id, ms_id = null, needRound = 1) {
  841. const msSql = ms_id ? ' AND `ms_id` = ' + ms_id : '';
  842. const sql = `
  843. SELECT
  844. SUM(CASE WHEN is_join = 1 THEN ${this.ctx.helper.getQtySource(qty_source)} * quantity ELSE 0 END) AS quantity1,
  845. SUM(CASE WHEN is_join = 2 THEN ${this.ctx.helper.getQtySource(qty_source, 1)} * quantity ELSE 0 END) AS quantity2
  846. FROM ${this.tableName}
  847. WHERE mid = ? AND mb_id = ?${msSql}
  848. `;
  849. const sqlParam = [mid, mb_id];
  850. const result = await transaction.queryOne(sql, sqlParam);
  851. const newQuantity = this.ctx.helper.add(result.quantity1, result.quantity2);
  852. return needRound ? this.ctx.helper.round(newQuantity, qty_decimal) : newQuantity;
  853. }
  854. async makeMaterialList(data) {
  855. if (!data.mid) {
  856. throw '参数错误';
  857. }
  858. const transaction = await this.db.beginTransaction();
  859. try {
  860. const material = await this.ctx.service.material.getDataById(data.mid);
  861. const newCalcStage = material.is_stage_self ? (material.calc_stage ? material.calc_stage.split(',') : []) : data.stage_id;
  862. if (material.is_stage_self) newCalcStage.push(data.stage_id);
  863. const materialStages = material.is_stage_self ? await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: material.id } }) : [];
  864. const notJoinList = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: material.id } });
  865. // 复制调差清单工料关联表
  866. // await this.ctx.service.materialList.copyPreMaterialList(transaction, preMaterial, newMaterial);
  867. await this.copyPreMaterialList2(transaction, data.material_list, [], notJoinList, material, materialStages);
  868. // 新增或删除list_gcl表
  869. await this.ctx.service.materialListGcl.insertOrDelGcl(transaction, data.insertGclList, data.removeGclList, material.id);
  870. // 修改本期应耗数量值和有效价差,需要剔除不参与调差的清单数据,并返回总金额
  871. const materials = await this.getAllDataByCondition({
  872. where: { tid: this.ctx.tender.id },
  873. order: ['order'],
  874. });
  875. const preMaterial = materials[materials.length - 1];
  876. if (material.is_stage_self) {
  877. await this.ctx.service.materialStageBills.insertMsBills(transaction, this.ctx.tender.id, material.id, this._.find(materialStages, { sid: parseInt(data.stage_id) }), JSON.parse(material.decimal), preMaterial.is_stage_self, material.qty_source, material.rate);
  878. await transaction.update(this.ctx.service.material.tableName, {
  879. id: material.id, calc_stage: newCalcStage.join(','),
  880. });
  881. } else {
  882. let m_tp = null;
  883. let m_tax_tp = null;
  884. let rate_tp = null;
  885. [m_tp, m_tax_tp, rate_tp] = await this.ctx.service.materialBills.updateNewMaterial(transaction, this.ctx.tender.id, material.id, this.ctx, material.stage_id, JSON.parse(material.decimal), preMaterial.is_stage_self, material.qty_source, material.rate);
  886. const updateMaterialData = {
  887. id: material.id,
  888. m_tp,
  889. m_tax_tp,
  890. rate_tp,
  891. calc_tp: 1,
  892. calc_stage: newCalcStage,
  893. };
  894. await transaction.update(this.ctx.service.material.tableName, updateMaterialData);
  895. }
  896. await transaction.commit();
  897. return true;
  898. } catch (err) {
  899. await transaction.rollback();
  900. throw err;
  901. }
  902. }
  903. async makeSelfList(data) {
  904. if (!data.mid) {
  905. throw '参数错误';
  906. }
  907. const transaction = await this.db.beginTransaction();
  908. try {
  909. const material = await this.ctx.service.material.getDataById(data.mid);
  910. const materialStages = material.is_stage_self ? await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: material.id } }) : [];
  911. const notJoinList = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: material.id } });
  912. // 复制调差清单工料关联表
  913. // await this.ctx.service.materialList.copyPreMaterialList(transaction, preMaterial, newMaterial);
  914. await this.copyPreMaterialList2(transaction, [], data.material_self_list, notJoinList, material, materialStages);
  915. // 设置list_gcl表old=>new更新
  916. await this.ctx.service.materialListGcl.setNewOldData(transaction, this.ctx.tender.id);
  917. // 修改本期应耗数量值和有效价差,需要剔除不参与调差的清单数据,并返回总金额
  918. // 找出当前人并更新tp_data
  919. const tp_data = await this.ctx.service.materialAudit.getTpData(transaction, material.id, JSON.parse(material.decimal));
  920. const updateMaterialData = {
  921. id: material.id,
  922. calc_tp: 1,
  923. tp_data: JSON.stringify(tp_data),
  924. };
  925. if (material.is_stage_self) {
  926. // 更新bill表和截止上期数据
  927. const materialDecimal = JSON.parse(material.decimal);
  928. const updateBillsData = [];
  929. const billsList = await transaction.select(this.ctx.service.materialBills.tableName, { where: { tid: this.ctx.tender.id } });
  930. const stageBillsDatas = await this.ctx.service.materialStageBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: material.id } });
  931. for (const mb of billsList) {
  932. const [newmsg_spread, newm_spread] = await this.ctx.service.materialStageBills.getSpread(mb, null, materialDecimal.up);
  933. const newTp = this._.sumBy(stageBillsDatas, function(item) {
  934. return item.mb_id === mb.id ? item.m_tp : 0;
  935. });
  936. const oneBillsData = {
  937. id: mb.id,
  938. quantity: null,
  939. expr: null,
  940. msg_tp: null,
  941. msg_times: null,
  942. msg_spread: newmsg_spread,
  943. m_spread: newm_spread,
  944. origin: null,
  945. m_tp: newTp,
  946. pre_tp: mb.m_tp !== null ? this.ctx.helper.add(mb.pre_tp, mb.m_tp) : mb.pre_tp,
  947. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), materialDecimal.tp),
  948. tax_pre_tp: mb.m_tax_tp !== null ? this.ctx.helper.add(mb.tax_pre_tp, mb.m_tax_tp) : mb.tax_pre_tp,
  949. };
  950. updateBillsData.push(oneBillsData);
  951. }
  952. await transaction.updateRows(this.ctx.service.materialBills.tableName, updateBillsData);
  953. // 计算得出本期总金额
  954. let m_tp = 0;
  955. let m_tax_tp = 0;
  956. let rate_tp = 0;
  957. for (const s of materialStages) {
  958. m_tp = this.ctx.helper.add(m_tp, s.m_tp);
  959. m_tax_tp = this.ctx.helper.add(m_tax_tp, s.m_tax_tp);
  960. const sRateTp = this.ctx.helper.round(this.ctx.helper.mul(s.m_tp, (1 + this.ctx.helper.div(material.rate, 100))), materialDecimal.tp);
  961. rate_tp = this.ctx.helper.add(rate_tp, sRateTp);
  962. }
  963. updateMaterialData.m_tp = m_tp;
  964. updateMaterialData.m_tax_tp = m_tax_tp;
  965. updateMaterialData.rate_tp = rate_tp;
  966. }
  967. await transaction.update(this.ctx.service.material.tableName, updateMaterialData);
  968. // 删除material_list表冗余数据,减少表数据量
  969. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, gather_qty: null, is_self: 0 });
  970. await transaction.commit();
  971. return true;
  972. } catch (err) {
  973. await transaction.rollback();
  974. throw err;
  975. }
  976. }
  977. }
  978. return MaterialList;
  979. };