material_bills.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. module.exports = app => {
  12. class MaterialBills extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'material_bills';
  22. }
  23. /**
  24. * 添加工料
  25. * @return {void}
  26. */
  27. async add() {
  28. if (!this.ctx.tender || !this.ctx.material) {
  29. throw '数据错误';
  30. }
  31. const newBills = {
  32. tid: this.ctx.tender.id,
  33. mid: this.ctx.material.id,
  34. in_time: new Date(),
  35. };
  36. // 新增工料
  37. const result = await this.db.insert(this.tableName, newBills);
  38. if (result.affectedRows !== 1) {
  39. throw '新增工料数据失败';
  40. }
  41. return await this.getDataById(result.insertId);
  42. }
  43. /**
  44. * 删除工料
  45. * @param {int} id 工料id
  46. * @return {void}
  47. */
  48. async del(id) {
  49. if (!this.ctx.tender || !this.ctx.material) {
  50. throw '数据错误';
  51. }
  52. // 判断t_type是否为费用,且存在quantity,m_spread值
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. const mbInfo = await this.getDataById(id);
  56. await transaction.delete(this.tableName, { id });
  57. let m_tp = this.ctx.material.m_tp;
  58. if (mbInfo.t_type === materialConst.t_type[1].value && mbInfo.quantity !== null && mbInfo.m_spread !== null) {
  59. // 金额发生变化,则重新计算本期金额
  60. m_tp = await this.calcMaterialMTp(transaction);
  61. }
  62. await transaction.commit();
  63. return m_tp;
  64. } catch (err) {
  65. await transaction.rollback();
  66. throw err;
  67. }
  68. }
  69. /**
  70. * 修改工料信息
  71. * @param {Object} data 工料内容
  72. * @return {void}
  73. */
  74. async save(data) {
  75. if (!this.ctx.tender || !this.ctx.material) {
  76. throw '数据错误';
  77. }
  78. delete data.in_time;
  79. delete data.m_tp;
  80. // 判断是否可修改
  81. // 判断t_type是否为费用
  82. const transaction = await this.db.beginTransaction();
  83. try {
  84. await transaction.update(this.tableName, data);
  85. let m_tp = this.ctx.material.m_tp;
  86. if (data.t_type === materialConst.t_type[1].value) {
  87. m_tp = await this.calcMaterialMTp(transaction);
  88. }
  89. await transaction.commit();
  90. return m_tp;
  91. } catch (err) {
  92. await transaction.rollback();
  93. throw err;
  94. }
  95. }
  96. /**
  97. * 更新新一期的quantity和截止上期金额并返回本期总金额
  98. * @param transaction
  99. * @param tid
  100. * @param mid
  101. * @returns {Promise<number>}
  102. */
  103. async updateNewMaterial(transaction, tid, mid) {
  104. const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
  105. let m_tp = 0;
  106. for (const mb of materialBillsData) {
  107. const one_tp = await this.calcQuantityByMB(transaction, mid, mb);
  108. m_tp = this.ctx.helper.add(m_tp, one_tp);
  109. }
  110. return m_tp;
  111. }
  112. /**
  113. * 修改quantity,m_spread值和返回单条调差金额(新增一期)
  114. * @param transaction
  115. * @param mid
  116. * @param mb
  117. * @returns {Promise<*>}
  118. */
  119. async calcQuantityByMB(transaction, mid, mb) {
  120. if (mb.t_type === 1) {
  121. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  122. const sqlParam = [mid, mb.id];
  123. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  124. console.log(mb_quantity);
  125. // 取历史期记录获取截止上期调差金额
  126. const updateData = {
  127. id: mb.id,
  128. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  129. pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
  130. };
  131. await transaction.update(this.tableName, updateData);
  132. return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, mb.m_spread), 2);
  133. }
  134. const updateData = {
  135. id: mb.id,
  136. pre_tp: mb.quantity && mb.m_spread !== null ? this.ctx.helper.round(this.ctx.helper.add(mb.pre_tp, this.ctx.helper.mul(mb.quantity, mb.m_spread)), 2) : mb.pre_tp,
  137. };
  138. await transaction.update(this.tableName, updateData);
  139. return await this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), 2);
  140. }
  141. /**
  142. * 修改 expr和quantity值,返回本期金额和单条数据
  143. * @param data
  144. * @returns {Promise<void>}
  145. */
  146. async updateFYQuantity(data) {
  147. if (!this.ctx.tender || !this.ctx.material) {
  148. throw '数据错误';
  149. }
  150. const transaction = await this.db.beginTransaction();
  151. try {
  152. const mbInfo = await this.getDataById(data.id);
  153. await transaction.update(this.tableName, data);
  154. let m_tp = this.ctx.material.m_tp;
  155. if (mbInfo.quantity !== data.quantity) {
  156. m_tp = await this.calcMaterialMTp(transaction);
  157. }
  158. await transaction.commit();
  159. const returnData = {
  160. m_tp,
  161. info: await this.getDataById(data.id),
  162. }
  163. return returnData;
  164. } catch (err) {
  165. await transaction.rollback();
  166. throw err;
  167. }
  168. }
  169. // 更改计算总金额并返回值
  170. async calcMaterialMTp(transaction) {
  171. // 金额发生变化,则重新计算本期金额
  172. const sql = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.tableName + ' WHERE `tid` = ?';
  173. const sqlParam = [this.ctx.tender.id];
  174. const tp = await transaction.queryOne(sql, sqlParam);
  175. console.log(tp);
  176. const updateData2 = {
  177. id: this.ctx.material.id,
  178. m_tp: tp.total_price,
  179. };
  180. await transaction.update(this.ctx.service.material.tableName, updateData2);
  181. return tp.total_price;
  182. }
  183. }
  184. return MaterialBills;
  185. };