material_bills.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 MaterialBills 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_bills';
  21. }
  22. /**
  23. * 添加工料
  24. * @return {void}
  25. */
  26. async add() {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const newBills = {
  31. tid: this.ctx.tender.id,
  32. mid: this.ctx.material.id,
  33. in_time: new Date(),
  34. };
  35. // 新增工料
  36. const result = await this.db.insert(this.tableName, newBills);
  37. if (result.affectedRows !== 1) {
  38. throw '新增工料数据失败';
  39. }
  40. return await this.getDataById(result.insertId);
  41. }
  42. /**
  43. * 删除工料
  44. * @param {int} id 工料id
  45. * @return {void}
  46. */
  47. async del(id) {
  48. if (!this.ctx.tender || !this.ctx.material) {
  49. throw '数据错误';
  50. }
  51. // 判断是否可删
  52. return await this.deleteById(id);
  53. }
  54. /**
  55. * 修改工料信息
  56. * @param {Object} data 工料内容
  57. * @return {void}
  58. */
  59. async save(data) {
  60. if (!this.ctx.tender || !this.ctx.material) {
  61. throw '数据错误';
  62. }
  63. delete data.in_time;
  64. delete data.m_tp;
  65. // 判断是否可修改
  66. return await this.db.update(this.tableName, data);
  67. }
  68. async updateNewMaterial(transaction, tid, mid) {
  69. const materialBillsData = await this.getAllDataByCondition({ where: { tid } });
  70. let m_tp = 0;
  71. for (const mb of materialBillsData) {
  72. const one_tp = await this.calcQuantityByMB(transaction, mid, mb);
  73. m_tp = this.ctx.helper.add(m_tp, one_tp);
  74. }
  75. return m_tp;
  76. }
  77. /**
  78. * 修改quantity,m_spread值和返回单条调差金额(新增一期)
  79. * @param transaction
  80. * @param mid
  81. * @param mb
  82. * @returns {Promise<*>}
  83. */
  84. async calcQuantityByMB(transaction, mid, mb) {
  85. if (mb.t_type === 1) {
  86. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.ctx.service.materialList.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  87. const sqlParam = [mid, mb.id];
  88. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  89. console.log(mb_quantity);
  90. // 取历史期记录获取截止上期调差金额
  91. const updateData = {
  92. id: mb.id,
  93. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  94. 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,
  95. };
  96. await transaction.update(this.tableName, updateData);
  97. return await this.ctx.helper.round(this.ctx.helper.mul(mb_quantity.quantity, mb.m_spread), 2);
  98. }
  99. const updateData = {
  100. id: mb.id,
  101. 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,
  102. };
  103. await transaction.update(this.tableName, updateData);
  104. return await this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, mb.m_spread), 2);
  105. }
  106. }
  107. return MaterialBills;
  108. };