material.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Material 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';
  21. }
  22. /**
  23. * 获取 最新一期 材料调差期计量
  24. * @param tenderId
  25. * @param includeUnCheck
  26. * @returns {Promise<*>}
  27. */
  28. async getLastestMaterial(tenderId, includeUnCheck = false) {
  29. this.initSqlBuilder();
  30. this.sqlBuilder.setAndWhere('tid', {
  31. value: tenderId,
  32. operate: '=',
  33. });
  34. if (!includeUnCheck) {
  35. this.sqlBuilder.setAndWhere('status', {
  36. value: auditConst.status.uncheck,
  37. operate: '!=',
  38. });
  39. }
  40. this.sqlBuilder.orderBy = [['order', 'desc']];
  41. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  42. const material = await this.db.queryOne(sql, sqlParam);
  43. return material;
  44. }
  45. /**
  46. * 获取 最新一期 审批完成的 材料调差期计量
  47. * @param tenderId
  48. * @returns {Promise<*>}
  49. */
  50. async getLastestCompleteMaterial(tenderId) {
  51. this.initSqlBuilder();
  52. this.sqlBuilder.setAndWhere('tid', {
  53. value: tenderId,
  54. operate: '=',
  55. });
  56. this.sqlBuilder.setAndWhere('status', {
  57. value: auditConst.status.checked,
  58. operate: '=',
  59. });
  60. this.sqlBuilder.orderBy = [['order', 'desc']];
  61. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  62. const material = await this.db.queryOne(sql, sqlParam);
  63. return material;
  64. }
  65. /**
  66. * 获取标段下的全部计量期,按倒序
  67. * @param tenderId
  68. * @returns {Promise<void>}
  69. */
  70. async getValidMaterials(tenderId) {
  71. const materials = await this.db.select(this.tableName, {
  72. where: { tid: tenderId },
  73. orders: [['order', 'desc']],
  74. });
  75. if (materials.length !== 0) {
  76. const lastMaterial = materials[materials.length - 1];
  77. if (lastMaterial.status === auditConst.status.uncheck && lastMaterial.user_id !== this.ctx.session.sessionUser.accountId) {
  78. materials.splice(materials.length - 1, 1);
  79. }
  80. }
  81. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  82. if (materials.length > 0 && materials[0].status !== auditConst.status.checked) {
  83. const material = materials[0];
  84. const curAuditor = await this.ctx.service.materialAudit.getCurAuditor(material.id, material.times);
  85. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : material.user_id === this.ctx.session.sessionUser.accountId;
  86. if (isActive) {
  87. material.curTimes = material.times;
  88. material.curOrder = curAuditor ? curAuditor.order : 0;
  89. }
  90. }
  91. return materials;
  92. }
  93. /**
  94. * 添加材料调差期
  95. * @param tenderId - 标段id
  96. * @param data - post的数据
  97. * @returns {Promise<void>}
  98. */
  99. async addMaterial(tenderId, data) {
  100. const materials = await this.getAllDataByCondition({
  101. where: { tid: tenderId },
  102. order: ['order'],
  103. });
  104. const preMaterial = materials[materials.length - 1];
  105. if (materials.length > 0 && materials[materials.length - 1].status !== auditConst.status.checked) {
  106. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  107. }
  108. const order = materials.length + 1;
  109. const newMaterial = {
  110. tid: tenderId,
  111. order,
  112. in_time: new Date(),
  113. times: 1,
  114. status: auditConst.status.uncheck,
  115. user_id: this.ctx.session.sessionUser.accountId,
  116. stage_id: data.stage_id.join(','),
  117. s_order: data.s_order,
  118. };
  119. const transaction = await this.db.beginTransaction();
  120. try {
  121. if (preMaterial) {
  122. newMaterial.rate = preMaterial.rate;
  123. newMaterial.pre_tp = this.ctx.helper.add(preMaterial.m_tp, preMaterial.pre_tp);
  124. }
  125. // 新增期记录
  126. const result = await transaction.insert(this.tableName, newMaterial);
  127. if (result.affectedRows === 1) {
  128. newMaterial.id = result.insertId;
  129. } else {
  130. throw '新增期数据失败';
  131. }
  132. // 存在上一期时,复制上一期审批流程、不参与调差的清单、上期清单并算本期有效价差,本期应耗数量,并算本期总金额
  133. if (preMaterial) {
  134. const auditResult = await this.ctx.service.materialAudit.copyPreMaterialAuditors(transaction, preMaterial, newMaterial);
  135. if (!auditResult) {
  136. throw '复制上一期审批流程失败';
  137. }
  138. // 复制不参与调差清单
  139. const preNotJoinList = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  140. await this.ctx.service.materialListNotjoin.copyNewStageNotJoinList(transaction, preNotJoinList, newMaterial.id);
  141. // 复制调差清单工料关联表
  142. await this.ctx.service.materialList.copyPreMaterialList(transaction, preMaterial, newMaterial);
  143. // 修改本期应耗数量值和有效价差,需要剔除不参与调差的清单数据,并返回总金额
  144. const m_tp = await this.ctx.service.materialBills.updateNewMaterial(transaction, this.ctx.tender.id, newMaterial.id, this.ctx, newMaterial.stage_id);
  145. // 计算得出本期总金额
  146. const updateMaterialData = {
  147. id: newMaterial.id,
  148. m_tp,
  149. };
  150. await transaction.update(this.tableName, updateMaterialData);
  151. }
  152. await transaction.commit();
  153. return newMaterial;
  154. } catch (err) {
  155. await transaction.rollback();
  156. throw err;
  157. }
  158. }
  159. /**
  160. * 删除材料调差期
  161. *
  162. * @param {Number} id - 期Id
  163. * @returns {Promise<void>}
  164. */
  165. async deleteMaterial(id) {
  166. const transaction = await this.db.beginTransaction();
  167. try {
  168. await transaction.delete(this.ctx.service.materialAudit.tableName, { mid: id });
  169. await transaction.delete(this.ctx.service.materialBills.tableName, { mid: id });
  170. await transaction.delete(this.ctx.service.materialList.tableName, { mid: id });
  171. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { mid: id });
  172. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { mid: id });
  173. // 如果存在上一期,把上一期的quantity,pre_tp添加到bill中
  174. const materialInfo = await this.getDataById(id);
  175. if (materialInfo.order > 1) {
  176. const sql = 'UPDATE ' + this.ctx.service.materialBills.tableName + ' as mb, ' +
  177. this.ctx.service.materialBillsHistory.tableName + ' as mbh ' +
  178. 'SET mb.`quantity` = mbh.`quantity`, mb.`pre_tp` = mbh.`pre_tp` ' +
  179. 'WHERE mbh.`tid` = ? AND mbh.`order` = ? AND mbh.`mb_id` = mb.`id`';
  180. const sqlParam = [this.ctx.tender.id, materialInfo.order - 1];
  181. await transaction.query(sql, sqlParam);
  182. }
  183. await transaction.delete(this.tableName, { id });
  184. await transaction.commit();
  185. return true;
  186. } catch (err) {
  187. await transaction.rollback();
  188. throw err;
  189. }
  190. }
  191. /**
  192. * 获取包含当前期之前的调差期id
  193. *
  194. * @param {Number} id - 期Id
  195. * @returns {Promise<void>}
  196. */
  197. async getPreMidList(tid, order) {
  198. const midList = await this.getAllDataByCondition({
  199. where: { tid },
  200. columns: ['id'],
  201. limit: order,
  202. offset: 0,
  203. });
  204. const list = [];
  205. for (const ml of midList) {
  206. list.push(ml.id);
  207. }
  208. return list;
  209. }
  210. /**
  211. * 修改增税税率
  212. * @param {int} rate 税率
  213. * @returns {Promise<*>}
  214. */
  215. async changeRate(rate) {
  216. const updateData = {
  217. id: this.ctx.material.id,
  218. rate,
  219. };
  220. return await this.db.update(this.tableName, updateData);
  221. }
  222. }
  223. return Material;
  224. };