material_bills.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. async _getMaxOrder(tenderId) {
  74. const sql = 'SELECT Max(??) As value FROM ?? Where tid = ' + tenderId;
  75. const sqlParam = ['order', this.tableName];
  76. const queryResult = await this.db.queryOne(sql, sqlParam);
  77. return queryResult.value ? queryResult.value : 0;
  78. }
  79. /**
  80. * 删除工料
  81. * @param {int} id 工料id
  82. * @return {void}
  83. */
  84. async del(id) {
  85. if (!this.ctx.tender || !this.ctx.material) {
  86. throw '数据错误';
  87. }
  88. // 判断t_type是否为费用,且存在quantity,m_spread值
  89. const transaction = await this.db.beginTransaction();
  90. try {
  91. // 防止多页面操作时,清单工料含量存在时工料可删
  92. const materialListNum = await this.ctx.service.materialList.count({ tid: this.ctx.tender.id, mb_id: id });
  93. if (materialListNum > 0) {
  94. throw '该工料已存在对应的清单工料含量,删除失败';
  95. }
  96. const mbInfo = await this.getDataById(id);
  97. await transaction.delete(this.tableName, { id });
  98. const m_tp = this.ctx.material.m_tp;
  99. const result = { m_tp };
  100. if (this.ctx.material.is_stage_self) {
  101. await transaction.delete(this.ctx.service.materialStageBills.tableName, { mb_id: id });
  102. // 金额发生变化,则重新计算本期金额
  103. for (const sid of this.ctx.material.stage_id.split(',')) {
  104. const msInfo = await transaction.get(this.ctx.service.materialStage.tableName, { tid: this.ctx.tender.id, sid });
  105. await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
  106. }
  107. result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
  108. result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  109. result.m_tp = await this.calcMaterialMTp(transaction);
  110. } else if (mbInfo.t_type === materialConst.t_type[1].value && mbInfo.quantity !== null && mbInfo.m_spread !== null) {
  111. // 金额发生变化,则重新计算本期金额
  112. result.m_tp = await this.calcMaterialMTp(transaction);
  113. }
  114. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  115. if (material_month.length > 0) {
  116. await transaction.delete(this.ctx.service.materialMonth.tableName, { mb_id: id });
  117. }
  118. await transaction.commit();
  119. return result;
  120. } catch (err) {
  121. await transaction.rollback();
  122. throw err;
  123. }
  124. }
  125. /**
  126. * 交换两个工料的顺序
  127. * @param {Number} id1 - 工料1的id
  128. * @param {Number} id2 - 工料2的id
  129. * @returns {Promise<void>}
  130. */
  131. async changeOrder(id1, id2) {
  132. if (!this.ctx.tender || !this.ctx.material) {
  133. throw '数据错误';
  134. }
  135. const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
  136. const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
  137. if (!bill1 || !bill2) {
  138. throw '数据错误';
  139. }
  140. const transaction = await this.db.beginTransaction();
  141. try {
  142. const order = bill1.order;
  143. bill1.order = bill2.order;
  144. bill2.order = order;
  145. await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
  146. await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
  147. await transaction.commit();
  148. return true;
  149. } catch (err) {
  150. await transaction.rollback();
  151. throw err;
  152. }
  153. }
  154. /**
  155. * 修改工料信息
  156. * @param {Object} data 工料内容
  157. * @return {void}
  158. */
  159. async save(data, ms_id = null) {
  160. if (!this.ctx.tender || !this.ctx.material) {
  161. throw '数据错误';
  162. }
  163. delete data.in_time;
  164. // delete data.m_tp;
  165. // 判断是否可修改
  166. // 判断t_type是否为费用
  167. const transaction = await this.db.beginTransaction();
  168. try {
  169. const result = {};
  170. if (this.ctx.material.is_stage_self) {
  171. if (!ms_id) {
  172. throw '期参数有误';
  173. }
  174. const needUp = await this.updateOneBillsData(transaction, data, ms_id);
  175. const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
  176. // 更新materialStage 值
  177. data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
  178. data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
  179. data.quantity = null;
  180. data.msg_tp = null;
  181. data.msg_times = null;
  182. data.msg_spread = null;
  183. data.m_spread = null;
  184. delete data.ms_id;
  185. // 更新materialStage 值
  186. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  187. result.stageBillsData = this.ctx.material.is_stage_self && needUp ?
  188. await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } }) :
  189. await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, ms_id, mb_id: data.id } });
  190. result.stageData = this.ctx.material.is_stage_self && needUp ?
  191. await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) :
  192. await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id, id: ms_id } });
  193. }
  194. await transaction.update(this.tableName, data);
  195. result.m_tp = await this.calcMaterialMTp(transaction);
  196. await transaction.commit();
  197. return result;
  198. } catch (err) {
  199. await transaction.rollback();
  200. throw err;
  201. }
  202. }
  203. async updateOneBillsData(transaction, data, ms_id) {
  204. // 当以下值和bills值不相同时,需要同步更新最新的计算值到stageBills表里
  205. const updateColsArray = ['t_type', 'm_tax', 'basic_price', 'm_up_risk', 'm_down_risk', 'is_summary'];
  206. let needUp = null;
  207. const mbInfo = await this.getDataById(data.id);
  208. for (const uc of updateColsArray) {
  209. if (data[uc] !== undefined && data[uc] !== mbInfo[uc]) {
  210. needUp = uc;
  211. break;
  212. }
  213. }
  214. if (needUp) {
  215. const msList = await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: this.ctx.material.id } });
  216. for (const ms of msList) {
  217. if (ms.id !== parseInt(ms_id)) {
  218. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({
  219. mid: this.ctx.material.id,
  220. ms_id: ms.id,
  221. mb_id: data.id,
  222. });
  223. const updateData = {
  224. id: msbInfo.id,
  225. };
  226. if (needUp === 'm_tax') {
  227. 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);
  228. } else if (needUp === 'm_up_risk' || needUp === 'm_down_risk' || needUp === 'basic_price') {
  229. const basic_price = needUp === 'basic_price' ? data.basic_price : mbInfo.basic_price;
  230. const [msg_spread, m_spread] = await this.getSpread(mbInfo, msbInfo.msg_tp, this.ctx.material.decimal.up, basic_price);
  231. updateData.msg_spread = msg_spread;
  232. updateData.m_spread = m_spread;
  233. const newTp = this.ctx.helper.round(this.ctx.helper.mul(msbInfo.quantity, m_spread), this.ctx.material.decimal.tp);
  234. updateData.m_tp = newTp;
  235. 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);
  236. } else if (needUp === 't_type') {
  237. updateData.quantity = null;
  238. updateData.m_tp = null;
  239. updateData.m_tax_tp = null;
  240. } else if (needUp === 'is_summary') {
  241. updateData.is_summary = data.is_summary;
  242. }
  243. await transaction.update(this.ctx.service.materialStageBills.tableName, updateData);
  244. await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
  245. }
  246. }
  247. }
  248. return needUp;
  249. }
  250. async saveOrigin(data) {
  251. if (!this.ctx.tender || !this.ctx.material) {
  252. throw '数据错误';
  253. }
  254. return await this.db.update(this.tableName, { id: data.mb_id, origin: data.value });
  255. }
  256. async saveOrigins(datas) {
  257. if (!this.ctx.tender || !this.ctx.material) {
  258. throw '数据错误';
  259. }
  260. const updateData = [];
  261. for (const data of datas) {
  262. updateData.push({
  263. id: data.mb_id,
  264. origin: data.origin,
  265. });
  266. }
  267. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  268. return true;
  269. }
  270. /**
  271. * 修改工料信息
  272. * @param {Object} data 工料内容
  273. * @return {void}
  274. */
  275. async saveDatas(datas, ms_id = null) {
  276. if (!this.ctx.tender || !this.ctx.material) {
  277. throw '数据错误';
  278. }
  279. // 判断是否可修改
  280. // 判断t_type是否为费用
  281. const transaction = await this.db.beginTransaction();
  282. try {
  283. for (const data of datas) {
  284. delete data.in_time;
  285. let needUp = null;
  286. if (this.ctx.material.is_stage_self) {
  287. needUp = await this.updateOneBillsData(transaction, data, ms_id);
  288. const [one_m_tp, one_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, data, ms_id);
  289. // 更新materialStage 值
  290. data.m_tp = this.ctx.helper.round(one_m_tp, this.ctx.material.decimal.tp);
  291. data.m_tax_tp = this.ctx.helper.round(one_tax_tp, this.ctx.material.decimal.tp);
  292. data.quantity = null;
  293. data.msg_tp = null;
  294. data.msg_times = null;
  295. data.msg_spread = null;
  296. data.msg_spread = null;
  297. data.m_spread = null;
  298. }
  299. // delete data.m_tp;
  300. // console.log(data);
  301. await transaction.update(this.tableName, data);
  302. }
  303. // 更新materialStage 值
  304. const result = {};
  305. if (this.ctx.material.is_stage_self) {
  306. await this.ctx.service.materialStage.updateMtp(transaction, ms_id);
  307. result.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id } });
  308. result.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  309. }
  310. result.m_tp = await this.calcMaterialMTp(transaction);
  311. await transaction.commit();
  312. return result;
  313. } catch (err) {
  314. await transaction.rollback();
  315. throw err;
  316. }
  317. }
  318. /**
  319. * 更新新一期的quantity和截止上期金额并返回本期总金额
  320. * @param transaction
  321. * @param tid
  322. * @param mid
  323. * @returns {Promise<number>}
  324. */
  325. async updateNewMaterial(transaction, tid, mid, ctx, stage_id, decimal) {
  326. const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
  327. let m_tp = 0;
  328. let m_tax_tp = 0;
  329. const materialCalculator = new MaterialCalculator(ctx, stage_id, ctx.tender.info);
  330. for (const mb of materialBillsData) {
  331. const [one_tp, one_tax_tp] = await this.calcQuantityByMB(transaction, mid, mb, materialCalculator, decimal);
  332. m_tp = this.ctx.helper.add(m_tp, one_tp);
  333. m_tax_tp = this.ctx.helper.add(m_tax_tp, one_tax_tp);
  334. }
  335. return [m_tp, m_tax_tp];
  336. }
  337. /**
  338. * 修改quantity,m_spread值和返回单条调差金额(新增一期)
  339. * @param transaction
  340. * @param mid
  341. * @param mb
  342. * @returns {Promise<*>}
  343. */
  344. async calcQuantityByMB(transaction, mid, mb, materialCalculator, decimal) {
  345. const [newmsg_spread, newm_spread] = await this.getSpread(mb, null, decimal.up);
  346. if (mb.t_type === materialConst.t_type[0].value) {
  347. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  348. const sqlParam = [mid, mb.id];
  349. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  350. console.log(mb_quantity);
  351. // 取历史期记录获取截止上期调差金额,并清空本期单价和时间,来源地,重新计算价差和有效价差
  352. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, decimal.qty);
  353. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, newm_spread), decimal.tp);
  354. const updateData = {
  355. id: mb.id,
  356. quantity: newQuantity,
  357. msg_tp: null,
  358. msg_times: null,
  359. msg_spread: newmsg_spread,
  360. m_spread: newm_spread,
  361. origin: null,
  362. m_tp: newTp,
  363. pre_tp: mb.m_tp !== null ? this.ctx.helper.add(mb.pre_tp, mb.m_tp) : mb.pre_tp,
  364. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp),
  365. tax_pre_tp: mb.m_tax_tp !== null ? this.ctx.helper.add(mb.tax_pre_tp, mb.m_tax_tp) : mb.tax_pre_tp,
  366. };
  367. await transaction.update(this.tableName, updateData);
  368. const m_tp = mb.is_summary === 1 ? await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, newm_spread), decimal.tp) : 0;
  369. 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);
  370. return [m_tp, m_tax_tp];
  371. } else if (mb.t_type === materialConst.t_type[1].value) {
  372. const quantity = await materialCalculator.calculateExpr(mb.expr);
  373. 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;
  374. const updateData = {
  375. id: mb.id,
  376. quantity: quantity !== 0 && quantity !== null ? this.ctx.helper.round(quantity, decimal.qty) : null,
  377. msg_tp: null,
  378. msg_times: null,
  379. msg_spread: newmsg_spread,
  380. m_spread: newm_spread,
  381. origin: null,
  382. m_tp: newTp,
  383. pre_tp: mb.m_tp !== null ? this.ctx.helper.add(mb.pre_tp, mb.m_tp) : mb.pre_tp,
  384. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), decimal.tp),
  385. tax_pre_tp: mb.m_tax_tp !== null ? this.ctx.helper.add(mb.tax_pre_tp, mb.m_tax_tp) : mb.tax_pre_tp,
  386. };
  387. await transaction.update(this.tableName, updateData);
  388. const m_tp = mb.is_summary === 1 ? await this.ctx.helper.round(this.ctx.helper.mul(quantity, newm_spread), decimal.tp) : 0;
  389. 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);
  390. return [m_tp, m_tax_tp];
  391. }
  392. }
  393. /**
  394. * 清空本期信息价后更新价差和有效价差
  395. * @param data
  396. * @returns {Promise<void>}
  397. */
  398. async getSpread(data, msg_tp, newDecimalUp = this.ctx.material.decimal.up, basic_price = null) {
  399. data.msg_tp = msg_tp;
  400. const newBp = basic_price ? basic_price : data.basic_price;
  401. const msg_spread = this.ctx.helper.round(this.ctx.helper.sub(data.msg_tp, newBp), newDecimalUp);
  402. 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));
  403. 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;
  404. return [msg_spread, m_spread];
  405. }
  406. /**
  407. * 修改 expr和quantity值,返回本期金额和单条数据
  408. * @param data
  409. * @returns {Promise<void>}
  410. */
  411. async updateFYQuantity(data) {
  412. if (!this.ctx.tender || !this.ctx.material) {
  413. throw '数据错误';
  414. }
  415. const transaction = await this.db.beginTransaction();
  416. try {
  417. const returnData = {};
  418. returnData.m_tp = this.ctx.material.m_tp;
  419. if (this.ctx.material.is_stage_self) {
  420. const mbInfo = await this.getDataById(data.id);
  421. let all_m_tp = 0;
  422. let all_tax_tp = 0;
  423. for (const sid of this.ctx.material.stage_id.split(',')) {
  424. const materialCalculator = new MaterialCalculator(this.ctx, sid, this.ctx.tender.info);
  425. const quantity = await materialCalculator.calculateExpr(data.expr);
  426. const msInfo = await this.ctx.service.materialStage.getDataByCondition({ mid: this.ctx.material.id, sid });
  427. const msbInfo = await this.ctx.service.materialStageBills.getDataByCondition({ mid: this.ctx.material.id, mb_id: data.id, ms_id: msInfo.id });
  428. const newQuantity = quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null;
  429. const m_tp = newQuantity ? this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msbInfo.m_spread), this.ctx.material.decimal.tp) : null;
  430. const updateData = {
  431. id: data.id,
  432. quantity: newQuantity,
  433. m_tp,
  434. 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),
  435. };
  436. const [one_bill_m_tp, one_bill_tax_tp] = await this.ctx.service.materialStageBills.update(transaction, updateData, msInfo.id);
  437. await this.ctx.service.materialStage.updateMtp(transaction, msInfo.id);
  438. all_m_tp = this.ctx.helper.round(one_bill_m_tp, this.ctx.material.decimal.tp);
  439. all_tax_tp = this.ctx.helper.round(one_bill_tax_tp, this.ctx.material.decimal.tp);
  440. }
  441. const updateBillsData = {
  442. id: data.id,
  443. expr: data.expr,
  444. m_tp: all_m_tp ? all_m_tp : null,
  445. m_tax_tp: all_tax_tp ? all_tax_tp : null,
  446. };
  447. console.log(all_m_tp);
  448. await transaction.update(this.tableName, updateBillsData);
  449. returnData.m_tp = await this.calcMaterialMTp(transaction);
  450. returnData.stageBillsData = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: data.id } });
  451. returnData.stageData = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  452. } else {
  453. const materialCalculator = new MaterialCalculator(this.ctx, this.ctx.material.stage_id, this.ctx.tender.info);
  454. const quantity = await materialCalculator.calculateExpr(data.expr);
  455. // 更新quantity值并重新返回计算本期金额,截止本期金额
  456. const updateData = {
  457. id: data.id,
  458. quantity: quantity !== 0 ? this.ctx.helper.round(quantity, this.ctx.material.decimal.qty) : null,
  459. expr: data.expr,
  460. };
  461. const mbInfo = await this.getDataById(updateData.id);
  462. updateData.m_tp = this.ctx.helper.round(this.ctx.helper.mul(updateData.quantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  463. 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);
  464. await transaction.update(this.tableName, updateData);
  465. if (mbInfo.quantity !== updateData.quantity) {
  466. returnData.m_tp = await this.calcMaterialMTp(transaction);
  467. }
  468. }
  469. await transaction.commit();
  470. returnData.info = await this.getDataById(data.id);
  471. return returnData;
  472. } catch (err) {
  473. await transaction.rollback();
  474. throw err;
  475. }
  476. }
  477. // 更改计算总金额并返回值
  478. async calcMaterialMTp(transaction) {
  479. // 金额发生变化,则重新计算本期金额
  480. 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';
  481. const sqlParam = [this.ctx.tender.id];
  482. const tp = await transaction.queryOne(sql, sqlParam);
  483. const updateData2 = {
  484. id: this.ctx.material.id,
  485. m_tp: tp.total_price,
  486. m_tax_tp: tp.tax_total_price,
  487. };
  488. console.log(tp);
  489. // if (this.ctx.material.material_tax) {
  490. // updateData2.m_tax_tp = tp.tax_total_price;
  491. // }
  492. await transaction.update(this.ctx.service.material.tableName, updateData2);
  493. return tp.total_price;
  494. }
  495. // 小数位变化更新单价和金额
  496. async resetDecimal(transaction, newDecimalUp, newDecimalTp, newDecimalQty) {
  497. const mbList = await transaction.select(this.tableName, { where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
  498. const updateList = [];
  499. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  500. const updateMonthList = [];
  501. const materialStageList = this.ctx.material.is_stage_self ? await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } }) : [];
  502. for (const mb of mbList) {
  503. const updateData = {
  504. id: mb.id,
  505. };
  506. if (this.ctx.material.is_stage_self) {
  507. const updateStageBillsList = [];
  508. for (const ms of materialStageList) {
  509. const msb = await transaction.get(this.ctx.service.materialStageBills.tableName, { mid: this.ctx.material.id, mb_id: mb.id, ms_id: ms.id });
  510. const updateStageBillData = {
  511. id: msb.id,
  512. };
  513. if (newDecimalQty !== this.ctx.material.decimal.qty) {
  514. // 通过管理重新算出quantity并保留小数位
  515. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1';
  516. const sqlParam = [this.ctx.material.id, mb.id, ms.id];
  517. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  518. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
  519. if (newQuantity !== msb.quantity) {
  520. updateStageBillData.quantity = newQuantity;
  521. msb.quantity = newQuantity;
  522. }
  523. }
  524. if (newDecimalUp !== this.ctx.material.decimal.up) {
  525. const newmsg_tp = this.ctx.helper.round(msb.msg_tp, newDecimalUp);
  526. msb.msg_tp = newmsg_tp;
  527. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  528. const [newmsg_spread, newm_spread] = await this.getSpread(msb, msb.msg_tp, newDecimalUp, newbasic_price);
  529. mb.m_spread = newm_spread;
  530. updateStageBillData.msg_tp = newmsg_tp;
  531. updateStageBillData.msg_spread = newmsg_spread;
  532. updateStageBillData.m_spread = newm_spread;
  533. }
  534. const newTp = this.ctx.helper.round(this.ctx.helper.mul(msb.quantity, msb.m_spread), newDecimalTp);
  535. updateStageBillData.m_tp = newTp;
  536. updateStageBillData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
  537. updateStageBillsList.push(updateStageBillData);
  538. }
  539. if (newDecimalUp !== this.ctx.material.decimal.up) {
  540. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  541. updateData.basic_price = newbasic_price;
  542. }
  543. 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 `mid` = ? AND `mb_id` = ? AND `is_summary` = 1';
  544. const sqlParam = [this.ctx.tender.id, this.ctx.material.id, mb.id];
  545. const tp = await transaction.queryOne(sql, sqlParam);
  546. updateData.m_tp = this.ctx.helper.round(tp.total_price, newDecimalTp);
  547. updateData.m_tax_tp = this.ctx.helper.round(tp.tax_total_price, newDecimalTp);
  548. updateList.push(updateData);
  549. if (updateStageBillsList.length > 0) await transaction.updateRows(this.ctx.service.materialStageBills.tableName, updateStageBillsList);
  550. } else {
  551. if (newDecimalUp !== this.ctx.material.decimal.up) {
  552. let newmsg_tp = this.ctx.helper.round(mb.msg_tp, newDecimalUp);
  553. mb.msg_tp = newmsg_tp;
  554. // 判断是否有月信息价,如果有则msg_tp值由月信息价的平均单价获得,并更新月信息价单价
  555. if (material_month.length > 0) {
  556. const monthList = await transaction.select(this.ctx.service.materialMonth.tableName, { where: { mb_id: mb.id, mid: this.ctx.material.id } });
  557. if (monthList.length !== 0) {
  558. for (const m of monthList) {
  559. // 更新月信息单价小数位
  560. const newMonthMsgTP = this.ctx.helper.round(m.msg_tp, newDecimalUp);
  561. if (m.msg_tp && newMonthMsgTP !== m.msg_tp) {
  562. m.msg_tp = newMonthMsgTP;
  563. updateMonthList.push({ id: m.id, msg_tp: m.msg_tp });
  564. }
  565. }
  566. const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
  567. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
  568. newmsg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), newDecimalUp) : null;
  569. mb.msg_tp = newmsg_tp;
  570. }
  571. }
  572. const newbasic_price = this.ctx.helper.round(mb.basic_price, newDecimalUp);
  573. mb.basic_price = newbasic_price;
  574. const [newmsg_spread, newm_spread] = await this.getSpread(mb, mb.msg_tp, newDecimalUp);
  575. mb.m_spread = newm_spread;
  576. updateData.basic_price = newbasic_price;
  577. updateData.msg_tp = newmsg_tp;
  578. updateData.msg_spread = newmsg_spread;
  579. updateData.m_spread = newm_spread;
  580. }
  581. if (newDecimalQty !== this.ctx.material.decimal.qty) {
  582. // 通过管理重新算出quantity并保留小数位
  583. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  584. const sqlParam = [this.ctx.material.id, mb.id];
  585. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  586. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, newDecimalQty);
  587. mb.quantity = newQuantity;
  588. updateData.quantity = newQuantity;
  589. }
  590. const newTp = this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), newDecimalTp);
  591. updateData.m_tp = newTp;
  592. updateData.m_tax_tp = this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mb.m_tax, 100))), newDecimalTp);
  593. updateList.push(updateData);
  594. }
  595. }
  596. if (this.ctx.material.is_stage_self) {
  597. for (const ms of materialStageList) {
  598. await this.ctx.service.materialStage.updateMtp(transaction, ms.id);
  599. }
  600. }
  601. if (updateMonthList.length > 0) await transaction.updateRows(this.ctx.service.materialMonth.tableName, updateMonthList);
  602. if (updateList.length > 0) await transaction.updateRows(this.tableName, updateList);
  603. }
  604. }
  605. return MaterialBills;
  606. };